From 7f1f0926e4306274173243ba29417477df9ad928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 12 Sep 2013 03:31:34 +0200 Subject: [PATCH] Add test for x509write_key --- library/x509write.c | 21 ---------------- tests/suites/test_suite_x509write.data | 4 +++ tests/suites/test_suite_x509write.function | 29 ++++++++++++++++++++++ 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/library/x509write.c b/library/x509write.c index e769edbe4..937b899fc 100644 --- a/library/x509write.c +++ b/library/x509write.c @@ -563,27 +563,6 @@ int x509write_key_der( rsa_context *rsa, unsigned char *buf, size_t size ) ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ); - // TODO: Make NON RSA Specific variant later on -/* *--c = 0; - len += 1; - - len += asn1_write_len( &c, len); - len += asn1_write_tag( &c, ASN1_BIT_STRING ); - - len += asn1_write_oid( &c, OID_PKCS1_RSA ); - - len += asn1_write_int( &c, 0 ); - - len += asn1_write_len( &c, len); - len += asn1_write_tag( &c, ASN1_CONSTRUCTED | ASN1_SEQUENCE );*/ - -/* for(i = 0; i < len; ++i) - { - if (i % 16 == 0 ) printf("\n"); - printf("%02x ", c[i]); - } - printf("\n");*/ - return( len ); } diff --git a/tests/suites/test_suite_x509write.data b/tests/suites/test_suite_x509write.data index 22d338c41..0c300d543 100644 --- a/tests/suites/test_suite_x509write.data +++ b/tests/suites/test_suite_x509write.data @@ -37,3 +37,7 @@ x509_pubkey_check:"data_files/server1.pubkey" Public key write check EC depends_on:POLARSSL_ECP_C:POLARSSL_BASE64_C x509_pubkey_check:"data_files/ec_pub.pem" + +Private key write check RSA +depends_on:POLARSSL_RSA_C:POLARSSL_BASE64_C +x509_key_check:"data_files/server1.key" diff --git a/tests/suites/test_suite_x509write.function b/tests/suites/test_suite_x509write.function index 09dfff874..13931036b 100644 --- a/tests/suites/test_suite_x509write.function +++ b/tests/suites/test_suite_x509write.function @@ -153,3 +153,32 @@ void x509_pubkey_check( char *key_file ) pk_free( &key ); } /* END_CASE */ + +/* BEGIN_CASE */ +void x509_key_check( char *key_file ) +{ + pk_context key; + unsigned char buf[5000]; + unsigned char check_buf[5000]; + int ret; + FILE *f; + + memset( buf, 0, sizeof( buf ) ); + memset( check_buf, 0, sizeof( check_buf ) ); + + pk_init( &key ); + TEST_ASSERT( x509parse_keyfile( &key, key_file, NULL ) == 0 ); + + ret = x509write_key_pem( pk_rsa( key ), buf, sizeof( buf ) - 1); + TEST_ASSERT( ret >= 0 ); + + f = fopen( key_file, "r" ); + TEST_ASSERT( f != NULL ); + fread( check_buf, 1, sizeof( check_buf ) - 1, f ); + fclose( f ); + + TEST_ASSERT( strncmp( (char *) buf, (char *) check_buf, sizeof( buf ) ) == 0 ); + + pk_free( &key ); +} +/* END_CASE */