PolarSSL v1.1.4
test_suite_debug.c
Go to the documentation of this file.
00001 #include "fct.h"
00002 
00003 #include <polarssl/debug.h>
00004 
00005 struct buffer_data
00006 {
00007     char buf[2000];
00008     char *ptr;
00009 };
00010 
00011 void string_debug(void *data, int level, const char *str)
00012 {
00013     struct buffer_data *buffer = (struct buffer_data *) data;
00014     ((void) level);
00015 
00016     memcpy(buffer->ptr, str, strlen(str));
00017     buffer->ptr += strlen(str);
00018 }
00019 
00020 #include <polarssl/config.h>
00021 
00022 #ifdef _MSC_VER
00023 #include <basetsd.h>
00024 typedef UINT32 uint32_t;
00025 #else
00026 #include <inttypes.h>
00027 #endif
00028 
00029 /*
00030  * 32-bit integer manipulation macros (big endian)
00031  */
00032 #ifndef GET_ULONG_BE
00033 #define GET_ULONG_BE(n,b,i)                             \
00034 {                                                       \
00035     (n) = ( (unsigned long) (b)[(i)    ] << 24 )        \
00036         | ( (unsigned long) (b)[(i) + 1] << 16 )        \
00037         | ( (unsigned long) (b)[(i) + 2] <<  8 )        \
00038         | ( (unsigned long) (b)[(i) + 3]       );       \
00039 }
00040 #endif
00041 
00042 #ifndef PUT_ULONG_BE
00043 #define PUT_ULONG_BE(n,b,i)                             \
00044 {                                                       \
00045     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
00046     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
00047     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
00048     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
00049 }
00050 #endif
00051 
00052 int unhexify(unsigned char *obuf, const char *ibuf)
00053 {
00054     unsigned char c, c2;
00055     int len = strlen(ibuf) / 2;
00056     assert(!(strlen(ibuf) %1)); // must be even number of bytes
00057 
00058     while (*ibuf != 0)
00059     {
00060         c = *ibuf++;
00061         if( c >= '0' && c <= '9' )
00062             c -= '0';
00063         else if( c >= 'a' && c <= 'f' )
00064             c -= 'a' - 10;
00065         else if( c >= 'A' && c <= 'F' )
00066             c -= 'A' - 10;
00067         else
00068             assert( 0 );
00069 
00070         c2 = *ibuf++;
00071         if( c2 >= '0' && c2 <= '9' )
00072             c2 -= '0';
00073         else if( c2 >= 'a' && c2 <= 'f' )
00074             c2 -= 'a' - 10;
00075         else if( c2 >= 'A' && c2 <= 'F' )
00076             c2 -= 'A' - 10;
00077         else
00078             assert( 0 );
00079 
00080         *obuf++ = ( c << 4 ) | c2;
00081     }
00082 
00083     return len;
00084 }
00085 
00086 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
00087 {
00088     unsigned char l, h;
00089 
00090     while (len != 0)
00091     {
00092         h = (*ibuf) / 16;
00093         l = (*ibuf) % 16;
00094 
00095         if( h < 10 )
00096             *obuf++ = '0' + h;
00097         else
00098             *obuf++ = 'a' + h - 10;
00099 
00100         if( l < 10 )
00101             *obuf++ = '0' + l;
00102         else
00103             *obuf++ = 'a' + l - 10;
00104 
00105         ++ibuf;
00106         len--;
00107     }
00108 }
00109 
00119 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
00120 {
00121     size_t i;
00122 
00123     if( rng_state != NULL )
00124         rng_state  = NULL;
00125 
00126     for( i = 0; i < len; ++i )
00127         output[i] = rand();
00128 
00129     return( 0 );
00130 }
00131 
00137 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
00138 {
00139     if( rng_state != NULL )
00140         rng_state  = NULL;
00141 
00142     memset( output, 0, len );
00143 
00144     return( 0 );
00145 }
00146 
00147 typedef struct
00148 {
00149     unsigned char *buf;
00150     size_t length;
00151 } rnd_buf_info;
00152 
00164 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
00165 {
00166     rnd_buf_info *info = (rnd_buf_info *) rng_state;
00167     size_t use_len;
00168 
00169     if( rng_state == NULL )
00170         return( rnd_std_rand( NULL, output, len ) );
00171 
00172     use_len = len;
00173     if( len > info->length )
00174         use_len = info->length;
00175 
00176     if( use_len )
00177     {
00178         memcpy( output, info->buf, use_len );
00179         info->buf += use_len;
00180         info->length -= use_len;
00181     }
00182 
00183     if( len - use_len > 0 )
00184         return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
00185 
00186     return( 0 );
00187 }
00188 
00196 typedef struct
00197 {
00198     uint32_t key[16];
00199     uint32_t v0, v1;
00200 } rnd_pseudo_info;
00201 
00210 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
00211 {
00212     rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
00213     uint32_t i, *k, sum, delta=0x9E3779B9;
00214     unsigned char result[4];
00215 
00216     if( rng_state == NULL )
00217         return( rnd_std_rand( NULL, output, len ) );
00218 
00219     k = info->key;
00220 
00221     while( len > 0 )
00222     {
00223         size_t use_len = ( len > 4 ) ? 4 : len;
00224         sum = 0;
00225 
00226         for( i = 0; i < 32; i++ )
00227         {
00228             info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
00229             sum += delta;
00230             info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
00231         }
00232 
00233         PUT_ULONG_BE( info->v0, result, 0 );
00234         memcpy( output, result, use_len );
00235         len -= use_len;
00236     }
00237 
00238     return( 0 );
00239 }
00240 
00241 
00242 FCT_BGN()
00243 {
00244 #ifdef POLARSSL_DEBUG_C
00245 #ifdef POLARSSL_BIGNUM_C
00246 #ifdef POLARSSL_SSL_TLS_C
00247 #ifdef POLARSSL_RSA_C
00248 
00249 
00250     FCT_SUITE_BGN(test_suite_debug)
00251     {
00252 #ifdef POLARSSL_FS_IO
00253 #ifdef POLARSSL_PEM_C
00254 #ifdef POLARSSL_BASE64_C
00255 
00256         FCT_TEST_BGN(debug_print_certificate_1)
00257         {
00258             x509_cert   crt;
00259             ssl_context ssl;
00260             struct buffer_data buffer;
00261         
00262             memset( &crt, 0, sizeof( x509_cert ) );
00263             memset( &ssl, 0, sizeof( ssl_context ) );
00264             memset( buffer.buf, 0, 2000 );
00265             buffer.ptr = buffer.buf; 
00266         
00267             ssl_set_dbg(&ssl, string_debug, &buffer);
00268         
00269             fct_chk( x509parse_crtfile( &crt, "data_files/server1.crt" ) == 0 );
00270             debug_print_crt( &ssl, 0, "MyFile", 999, "PREFIX_", &crt);
00271         
00272             fct_chk( strcmp( buffer.buf, "MyFile(0999): PREFIX_ #1:\nMyFile(0999): cert. version : 3\nMyFile(0999): serial number : 01\nMyFile(0999): issuer name   : C=NL, O=PolarSSL, CN=PolarSSL Test CA\nMyFile(0999): subject name  : C=NL, O=PolarSSL, CN=PolarSSL Server 1\nMyFile(0999): issued  on    : 2011-02-12 14:44:06\nMyFile(0999): expires on    : 2021-02-12 14:44:06\nMyFile(0999): signed using  : RSA+SHA1\nMyFile(0999): RSA key size  : 2048 bits\nMyFile(0999): value of 'crt->rsa.N' (2048 bits) is:\nMyFile(0999):  a9 02 1f 3d 40 6a d5 55 53 8b fd 36 ee 82 65 2e\nMyFile(0999):  15 61 5e 89 bf b8 e8 45 90 db ee 88 16 52 d3 f1\nMyFile(0999):  43 50 47 96 12 59 64 87 6b fd 2b e0 46 f9 73 be\nMyFile(0999):  dd cf 92 e1 91 5b ed 66 a0 6f 89 29 79 45 80 d0\nMyFile(0999):  83 6a d5 41 43 77 5f 39 7c 09 04 47 82 b0 57 39\nMyFile(0999):  70 ed a3 ec 15 19 1e a8 33 08 47 c1 05 42 a9 fd\nMyFile(0999):  4c c3 b4 df dd 06 1f 4d 10 51 40 67 73 13 0f 40\nMyFile(0999):  f8 6d 81 25 5f 0a b1 53 c6 30 7e 15 39 ac f9 5a\nMyFile(0999):  ee 7f 92 9e a6 05 5b e7 13 97 85 b5 23 92 d9 d4\nMyFile(0999):  24 06 d5 09 25 89 75 07 dd a6 1a 8f 3f 09 19 be\nMyFile(0999):  ad 65 2c 64 eb 95 9b dc fe 41 5e 17 a6 da 6c 5b\nMyFile(0999):  69 cc 02 ba 14 2c 16 24 9c 4a dc cd d0 f7 52 67\nMyFile(0999):  73 f1 2d a0 23 fd 7e f4 31 ca 2d 70 ca 89 0b 04\nMyFile(0999):  db 2e a6 4f 70 6e 9e ce bd 58 89 e2 53 59 9e 6e\nMyFile(0999):  5a 92 65 e2 88 3f 0c 94 19 a3 dd e5 e8 9d 95 13\nMyFile(0999):  ed 29 db ab 70 12 dc 5a ca 6b 17 ab 52 82 54 b1\nMyFile(0999): value of 'crt->rsa.E' (17 bits) is:\nMyFile(0999):  01 00 01\n" ) == 0 );
00273         }
00274         FCT_TEST_END();
00275 #endif /* POLARSSL_FS_IO */
00276 #endif /* POLARSSL_PEM_C */
00277 #endif /* POLARSSL_BASE64_C */
00278 
00279 
00280         FCT_TEST_BGN(debug_print_mpi_1)
00281         {
00282             ssl_context ssl;
00283             struct buffer_data buffer;
00284             mpi val;
00285         
00286             mpi_init( &val );
00287         
00288             memset( &ssl, 0, sizeof( ssl_context ) );
00289             memset( buffer.buf, 0, 2000 );
00290             buffer.ptr = buffer.buf; 
00291         
00292             fct_chk( mpi_read_string( &val, 16, "01020304050607" ) == 0 );
00293             ssl_set_dbg(&ssl, string_debug, &buffer);
00294         
00295             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00296         
00297             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (49 bits) is:\nMyFile(0999):  01 02 03 04 05 06 07\n" ) == 0 );
00298         
00299             mpi_free( &val );
00300         }
00301         FCT_TEST_END();
00302 
00303 
00304         FCT_TEST_BGN(debug_print_mpi_2)
00305         {
00306             ssl_context ssl;
00307             struct buffer_data buffer;
00308             mpi val;
00309         
00310             mpi_init( &val );
00311         
00312             memset( &ssl, 0, sizeof( ssl_context ) );
00313             memset( buffer.buf, 0, 2000 );
00314             buffer.ptr = buffer.buf; 
00315         
00316             fct_chk( mpi_read_string( &val, 16, "00000000000007" ) == 0 );
00317             ssl_set_dbg(&ssl, string_debug, &buffer);
00318         
00319             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00320         
00321             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (3 bits) is:\nMyFile(0999):  07\n" ) == 0 );
00322         
00323             mpi_free( &val );
00324         }
00325         FCT_TEST_END();
00326 
00327 
00328         FCT_TEST_BGN(debug_print_mpi_3)
00329         {
00330             ssl_context ssl;
00331             struct buffer_data buffer;
00332             mpi val;
00333         
00334             mpi_init( &val );
00335         
00336             memset( &ssl, 0, sizeof( ssl_context ) );
00337             memset( buffer.buf, 0, 2000 );
00338             buffer.ptr = buffer.buf; 
00339         
00340             fct_chk( mpi_read_string( &val, 16, "00000000000000" ) == 0 );
00341             ssl_set_dbg(&ssl, string_debug, &buffer);
00342         
00343             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00344         
00345             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (0 bits) is:\nMyFile(0999):  00\n" ) == 0 );
00346         
00347             mpi_free( &val );
00348         }
00349         FCT_TEST_END();
00350 
00351 
00352         FCT_TEST_BGN(debug_print_mpi_4)
00353         {
00354             ssl_context ssl;
00355             struct buffer_data buffer;
00356             mpi val;
00357         
00358             mpi_init( &val );
00359         
00360             memset( &ssl, 0, sizeof( ssl_context ) );
00361             memset( buffer.buf, 0, 2000 );
00362             buffer.ptr = buffer.buf; 
00363         
00364             fct_chk( mpi_read_string( &val, 16, "0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
00365             ssl_set_dbg(&ssl, string_debug, &buffer);
00366         
00367             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00368         
00369             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (764 bits) is:\nMyFile(0999):  09 41 37 9d 00 fe d1 49 1f e1 5d f2 84 df de 4a\nMyFile(0999):  14 2f 68 aa 8d 41 20 23 19 5c ee 66 88 3e 62 90\nMyFile(0999):  ff e7 03 f4 ea 59 63 bf 21 27 13 ce e4 6b 10 7c\nMyFile(0999):  09 18 2b 5e dc d9 55 ad ac 41 8b f4 91 8e 28 89\nMyFile(0999):  af 48 e1 09 9d 51 38 30 ce c8 5c 26 ac 1e 15 8b\nMyFile(0999):  52 62 0e 33 ba 86 92 f8 93 ef bb 2f 95 8b 44 24\n" ) == 0 );
00370         
00371             mpi_free( &val );
00372         }
00373         FCT_TEST_END();
00374 
00375 
00376         FCT_TEST_BGN(debug_print_mpi_5)
00377         {
00378             ssl_context ssl;
00379             struct buffer_data buffer;
00380             mpi val;
00381         
00382             mpi_init( &val );
00383         
00384             memset( &ssl, 0, sizeof( ssl_context ) );
00385             memset( buffer.buf, 0, 2000 );
00386             buffer.ptr = buffer.buf; 
00387         
00388             fct_chk( mpi_read_string( &val, 16, "0000000000000000000000000000000000000000000000000000000941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
00389             ssl_set_dbg(&ssl, string_debug, &buffer);
00390         
00391             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00392         
00393             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (764 bits) is:\nMyFile(0999):  09 41 37 9d 00 fe d1 49 1f e1 5d f2 84 df de 4a\nMyFile(0999):  14 2f 68 aa 8d 41 20 23 19 5c ee 66 88 3e 62 90\nMyFile(0999):  ff e7 03 f4 ea 59 63 bf 21 27 13 ce e4 6b 10 7c\nMyFile(0999):  09 18 2b 5e dc d9 55 ad ac 41 8b f4 91 8e 28 89\nMyFile(0999):  af 48 e1 09 9d 51 38 30 ce c8 5c 26 ac 1e 15 8b\nMyFile(0999):  52 62 0e 33 ba 86 92 f8 93 ef bb 2f 95 8b 44 24\n" ) == 0 );
00394         
00395             mpi_free( &val );
00396         }
00397         FCT_TEST_END();
00398 
00399 
00400         FCT_TEST_BGN(debug_print_mpi_6)
00401         {
00402             ssl_context ssl;
00403             struct buffer_data buffer;
00404             mpi val;
00405         
00406             mpi_init( &val );
00407         
00408             memset( &ssl, 0, sizeof( ssl_context ) );
00409             memset( buffer.buf, 0, 2000 );
00410             buffer.ptr = buffer.buf; 
00411         
00412             fct_chk( mpi_read_string( &val, 16, "0000000000000000000000000000000000000000000000000000000041379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
00413             ssl_set_dbg(&ssl, string_debug, &buffer);
00414         
00415             debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
00416         
00417             fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (759 bits) is:\nMyFile(0999):  41 37 9d 00 fe d1 49 1f e1 5d f2 84 df de 4a 14\nMyFile(0999):  2f 68 aa 8d 41 20 23 19 5c ee 66 88 3e 62 90 ff\nMyFile(0999):  e7 03 f4 ea 59 63 bf 21 27 13 ce e4 6b 10 7c 09\nMyFile(0999):  18 2b 5e dc d9 55 ad ac 41 8b f4 91 8e 28 89 af\nMyFile(0999):  48 e1 09 9d 51 38 30 ce c8 5c 26 ac 1e 15 8b 52\nMyFile(0999):  62 0e 33 ba 86 92 f8 93 ef bb 2f 95 8b 44 24\n" ) == 0 );
00418         
00419             mpi_free( &val );
00420         }
00421         FCT_TEST_END();
00422 
00423     }
00424     FCT_SUITE_END();
00425 
00426 #endif /* POLARSSL_DEBUG_C */
00427 #endif /* POLARSSL_BIGNUM_C */
00428 #endif /* POLARSSL_SSL_TLS_C */
00429 #endif /* POLARSSL_RSA_C */
00430 
00431 }
00432 FCT_END();
00433