strongswan/src/charon/testing/rsa_test.c

227 lines
12 KiB
C

/**
* @file rsa_test.h
*
* @brief Tests for the hasher_t classes.
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <string.h>
#include "rsa_test.h"
#include <daemon.h>
#include <utils/logger.h>
#include <crypto/x509.h>
char private_key_buffer[] = {
0x30,0x82,0x04,0xa2,0x02,0x00,0x02,0x82,0x01,0x00,0x6f,0x25,0x74,0x63,0x2a,0x2f,
0x5d,0xd4,0x54,0x03,0xbe,0xd5,0x34,0x71,0xe0,0x30,0x37,0xe5,0x2e,0x39,0xda,0xe7,
0x04,0xd4,0xe2,0x5b,0x43,0xc3,0x6a,0x50,0x61,0xe8,0x4b,0x5d,0x58,0x30,0xa4,0xcc,
0x6d,0xab,0xdf,0x8b,0x75,0x8c,0x22,0x43,0xd4,0xd0,0x18,0x5e,0x32,0x24,0xba,0x38,
0x6f,0xab,0x64,0x86,0x8f,0x54,0x40,0x77,0xcb,0x3a,0xb5,0x30,0xde,0xb4,0xcd,0x98,
0xbb,0xb1,0xdb,0x7a,0xd4,0xc7,0x0e,0xc7,0x99,0xc4,0x05,0xd5,0xa4,0x96,0x2a,0x3c,
0x71,0x0f,0x31,0xa4,0xcd,0xc2,0x15,0x28,0xec,0x16,0x02,0x28,0x61,0x5e,0x8e,0xcf,
0xb6,0x0b,0x8c,0x81,0x79,0x58,0xfc,0x9b,0x5b,0x32,0x26,0xcb,0xbc,0xf2,0xc9,0x8a,
0x76,0x26,0x4e,0x87,0xaa,0x1b,0xd4,0xa7,0xb3,0xc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ee,0x2e,0xe1,0x4a,0x6d,0x64,0xde,0x72,0x7b,0xd0,
0xfd,0x88,0x10,0xba,0xd5,0x9d,0x52,0x17,0x2a,0x4a,0x00,0x45,0xec,0x55,0x00,0x1f,
0x6d,0x33,0x58,0xef,0xfd,0x1b,0x96,0xea,0xc4,0x44,0x82,0xb2,0x89,0x53,0xe8,0x02,
0xba,0x0c,0x28,0xff,0xd1,0xda,0xdc,0xea,0xae,0x80,0xbe,0x33,0x86,0xbc,0x38,0xe9,
0x1c,0xa9,0x39,0xc5,0x28,0x14,0x53,0x5a,0x52,0x3e,0xff,0xb8,0xc6,0x77,0x7c,0xe2,
0xf9,0x50,0x9a,0x58,0x46,0x4e,0xdf,0x11,0x0f,0x4d,0x70,0xf3,0xe7,0xe7,0x9a,0x8a,
0x9f,0x58,0x05,0x54,0xda,0x60,0x52,0xec,0xa8,0x10,0x60,0x9c,0x83,0xf0,0xd7,0x15,
0xaf,0xf9,0x44,0xe8,0x3e,0x68,0xb2,0x06,0xa5,0x6d,0x6d,0xc1,0x8d,0x55,0xa3,0x6e,
0x7e,0xe4,0x86,0x2a,0x6e,0x23,0xe0,0xf5,0xe6,0x08,0x05,0xb5,0x1a,0x6d,0x9c,0xf4,
0xbd,0x18,0x20,0x58,0x43,0x67,0x72,0xde,0x47,0x56,0xee,0x94,0xc6,0x70,0xc1,0xda,
0x15,0x2a,0xb9,0xb7,0x6a,0x10,0xc4,0x02,0x6e,0xae,0x93,0xe8,0x5e,0x8f,0x55,0x80,
0x4e,0x9a,0x75,0x02,0x81,0x81,0x00,0xc4,0x25,0x05,0xab,0x8d,0x6b,0xa0,0xac,0x82,
0x44,0xe2,0x13,0x06,0x2e,0x1c,0x7b,0x6b,0x37,0x69,0x93,0x9e,0x33,0x41,0xb1,0xf0,
0x54,0x6e,0xe1,0x52,0x98,0x83,0x36,0x2b,0xe4,0x86,0x85,0x19,0x53,0x1f,0xd7,0x2f,
0xbb,0x76,0xef,0x8d,0xb1,0x42,0xd3,0x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6,0xdc,0xb5,0x73,0xcc,0xa6,0xa7,0x90,0x7f,0xad,0x95,0x7c,
0x02,0x38,0x8e,0xe8,0x8c,0x91,0x8e,0x51,0xcf,0x91,0x11,0x66,0x72,0xab,0x10,0xf0,
0x32,0xd6,0x0c,0x0d,0x0c,0x18,0x09,0x12,0x79,0x91,0x67,0x98,0x82,0xb1,0xf6,0x6a,
0x96,0x68,0xf6,0x59,0x6d,0xcf,0xdb,0xc2,0xc1,0x9d,0x93,0x7f,0xa9,0xad,0x69,0x38,
0x4e,0xec,0xd7,0x86,0x66,0xaa,0x20,0x41,0x89,0x47,0xb5,0x52,0x53,0x18,0x4c,0xb2,
0x3e,0x8f,0x3d,0x28,0x92,0x7b,0x96,0x61,0x29,0x35,0x59,0xd0,0xd9,0x66,0x80,0x00,
0x4e,0x53,0xf3,0xb1,0x57,0x0c,0xf6,0x27,0x95,0xe2,0x35,0x64,0xc6,0xa9,0xdb,0x49,
0xbe,0x6c,0x13,0xe1,0xf6,0xef,0xb9,0x89,0x69,0xd4,0x1b,0x7b,0xb3,0x58,0xc9,0x02,
0x81,0x80,0x40,0x28,0x3d,0xce,0x37,0xea,0x05,0x43,0x2d,0xda,0xed,0xf0,0xd7,0xdd,
0xd8,0x05,0xbc,0x3b,0x14,0xe6,0x78,0x4c,0x00,0xc6,0x25,0xca,0xfa,0xb8,0x00,0x72,
0xf0,0xe6,0xd3,0x19,0xfa,0xb4,0xda,0x6b,0xcc,0x95,0x06,0xf9,0x00,0x10,0x9e,0x19,
0x69,0x69,0xee,0x90,0xb1,0x25,0x6b,0x38,0xee,0x87,0x6b,0x9a,0x8b,0x0a,0x77,0x0a,
0xb4,0xa2,0x4c,0x54,0xe1,0x36,0x4a,0xfc,0x40,0x38,0x6f,0x52,0x0d,0x21,0xcc,0x03,
0xd8,0xf4,0x82,0x0e,0xc5,0x97,0xec,0x06,0x35,0x37,0x4d,0xb3,0x5c,0x4a,0x9b,0xe4,
0x34,0xc6,0x97,0xb0,0x85,0xb6,0x59,0x6d,0x3d,0x87,0xb0,0x66,0xba,0xd4,0x25,0x12,
0xd6,0x2a,0xc3,0x75,0xf3,0xd6,0xca,0xff,0x12,0x27,0x3e,0xf7,0x7a,0x99,0xbd,0x61,
0x65,0x0f,0x02,0x81,0x81,0x00,0xb2,0xcb,0x21,0xf9,0x77,0x44,0x20,0xee,0xe9,0x60,
0xf2,0x32,0x7e,0xd0,0xb2,0x8b,0xa7,0x96,0x20,0x20,0xf2,0x88,0xbd,0xbe,0x1f,0x92,
0x59,0x26,0x7c,0x26,0x64,0x13,0xfc,0x9a,0x1c,0xd6,0x48,0xbf,0xe3,0xad,0x2d,0x89,
0xd4,0x11,0x9b,0xed,0x38,0x99,0x3e,0xf4,0xe3,0x54,0xa3,0x0c,0x2a,0x91,0xdc,0xf9,
0x38,0x94,0xbe,0xd7,0x90,0xc2,0x8d,0xcc,0x5a,0x28,0xbd,0x46,0x4e,0xd7,0x86,0x52,
0x95,0xb1,0x39,0xb9,0x30,0x33,0x1f,0xe8,0xe7,0x37,0xfe,0x37,0xa5,0x20,0x82,0x1a,
0xfd,0xc3,0x30,0xd0,0xdc,0x8d,0x71,0x66,0x30,0xb4,0x9a,0xb2,0xd6,0x03,0xfe,0xc5,
0x4b,0xfd,0xd2,0x1b,0x3e,0x4e,0xc6,0xb0,0xe8,0x6c,0x83,0x44,0x6b,0xaa,0x05,0x51,
0xd3,0xb2,0x04,0xca,0xf6,0xf3,
};
char public_key_buffer[] = {
// 0x30,0x82,0x01,0x21,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,
// 0x01,0x05,0x00,0x03,0x82,0x01,0x0e,0x00,
0x30,0x82,0x01,0x09,0x02,0x82,0x01,0x00,
0x6f,0x25,0x74,0x63,0x2a,0x2f,0x5d,0xd4,0x54,0x03,0xbe,0xd5,0x34,0x71,0xe0,0x30,
0x37,0xe5,0x2e,0x39,0xda,0xe7,0x04,0xd4,0xe2,0x5b,0x43,0x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xb6,0x96,0x08,0xd2,0xe0,0x8a,0x28,0xeb,
0x02,0x03,0x01,0x00,0x01
};
/*
* described in Header-File
*/
void test_rsa(protected_tester_t *tester)
{
rsa_private_key_t *private_key;
rsa_public_key_t *public_key;
x509_t *certificate;
chunk_t data, signature;
chunk_t der_private_key = {private_key_buffer, sizeof(private_key_buffer)};
chunk_t der_public_key = {public_key_buffer, sizeof(public_key_buffer)};
logger_t *logger;
status_t status;
u_int8_t test_data[] = {
0x01,0x02,0x03,0x04,
0x11,0x12,0x13,0x14,
0x21,0x22,0x23,0x24,
0x31,0x32,0x33,0x34,
0x41,0x42,0x43,0x44,
0x51,0x52,0x53,0x54,
0x61,0x62,0x63,0x64,
0x71,0x72,0x73,0x74,
0x81,0x82,0x83,0x84,
};
data.ptr = test_data;
data.len = sizeof(test_data);
logger = logger_manager->get_logger(logger_manager, TESTER);
logger->disable_level(logger, FULL);
/* key generation and signing */
// private_key = rsa_private_key_create(512);
// tester->assert_true(tester, private_key != NULL, "generating private key");
//
// status = private_key->build_emsa_pkcs1_signature(private_key, HASH_MD5, data, &signature);
// tester->assert_true(tester, status == SUCCESS, "build emsa_pkcs1_signature (genkey)");
//
// public_key = private_key->get_public_key(private_key);
// tester->assert_true(tester, public_key != NULL, "extracting public key");
//
// status = public_key->verify_emsa_pkcs1_signature(public_key, data, signature);
// tester->assert_true(tester, status == SUCCESS, "verify emsa_pkcs1_signature (genkey)");
//
// free(signature.ptr);
//
// private_key->destroy(private_key);
// public_key->destroy(public_key);
/* key setting */
private_key = rsa_private_key_create_from_chunk(der_private_key);
tester->assert_true(tester, private_key != NULL, "loading private key from chunk");
public_key = rsa_public_key_create_from_chunk(der_public_key);
tester->assert_true(tester, public_key != NULL, "loading public key from chunk");
status = private_key->build_emsa_pkcs1_signature(private_key, HASH_MD5, data, &signature);
tester->assert_true(tester, status == SUCCESS, "build emsa_pkcs1_signature (setkey)");
status = public_key->verify_emsa_pkcs1_signature(public_key, data, signature);
tester->assert_true(tester, status == SUCCESS, "verify emsa_pkcs1_signature (setkey)");
free(signature.ptr);
/* key comparison */
tester->assert_true(tester, private_key->belongs_to(private_key, public_key), "key belongs to");
private_key->destroy(private_key);
private_key = rsa_private_key_create(512);
tester->assert_false(tester, private_key->belongs_to(private_key, public_key), "key belongs not to");
public_key->destroy(public_key);
private_key->destroy(private_key);
/* key loading */
private_key = rsa_private_key_create_from_file("alice.der", NULL);
tester->assert_true(tester, private_key != NULL, "loading private key from file");
certificate = x509_create_from_file("alice-cert.der", "testcert");
tester->assert_true(tester, public_key != NULL, "loading certificate from file");
public_key = certificate->get_public_key(certificate);
tester->assert_true(tester, public_key != NULL, "loading public key from certificate");
tester->assert_true(tester, private_key->belongs_to(private_key, public_key), "key belongs to");
status = private_key->build_emsa_pkcs1_signature(private_key, HASH_SHA1, data, &signature);
tester->assert_true(tester, status == SUCCESS, "build emsa_pkcs1_signature (loadkey)");
status = public_key->verify_emsa_pkcs1_signature(public_key, data, signature);
tester->assert_true(tester, status == SUCCESS, "verify emsa_pkcs1_signature (loadkey)");
free(signature.ptr);
certificate->destroy(certificate);
public_key->destroy(public_key);
private_key->destroy(private_key);
}