PolarSSL v1.2.8
test_suite_debug.c
Go to the documentation of this file.
1 #include "fct.h"
2 #include <polarssl/config.h>
3 
4 #include <polarssl/debug.h>
5 
6 struct buffer_data
7 {
8  char buf[2000];
9  char *ptr;
10 };
11 
12 void string_debug(void *data, int level, const char *str)
13 {
14  struct buffer_data *buffer = (struct buffer_data *) data;
15  ((void) level);
16 
17  memcpy(buffer->ptr, str, strlen(str));
18  buffer->ptr += strlen(str);
19 }
20 
21 #ifdef _MSC_VER
22 #include <basetsd.h>
23 typedef UINT32 uint32_t;
24 #else
25 #include <inttypes.h>
26 #endif
27 
28 /*
29  * 32-bit integer manipulation macros (big endian)
30  */
31 #ifndef GET_UINT32_BE
32 #define GET_UINT32_BE(n,b,i) \
33 { \
34  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
35  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
36  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
37  | ( (uint32_t) (b)[(i) + 3] ); \
38 }
39 #endif
40 
41 #ifndef PUT_UINT32_BE
42 #define PUT_UINT32_BE(n,b,i) \
43 { \
44  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
45  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
46  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
47  (b)[(i) + 3] = (unsigned char) ( (n) ); \
48 }
49 #endif
50 
51 int unhexify(unsigned char *obuf, const char *ibuf)
52 {
53  unsigned char c, c2;
54  int len = strlen(ibuf) / 2;
55  assert(!(strlen(ibuf) %1)); // must be even number of bytes
56 
57  while (*ibuf != 0)
58  {
59  c = *ibuf++;
60  if( c >= '0' && c <= '9' )
61  c -= '0';
62  else if( c >= 'a' && c <= 'f' )
63  c -= 'a' - 10;
64  else if( c >= 'A' && c <= 'F' )
65  c -= 'A' - 10;
66  else
67  assert( 0 );
68 
69  c2 = *ibuf++;
70  if( c2 >= '0' && c2 <= '9' )
71  c2 -= '0';
72  else if( c2 >= 'a' && c2 <= 'f' )
73  c2 -= 'a' - 10;
74  else if( c2 >= 'A' && c2 <= 'F' )
75  c2 -= 'A' - 10;
76  else
77  assert( 0 );
78 
79  *obuf++ = ( c << 4 ) | c2;
80  }
81 
82  return len;
83 }
84 
85 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
86 {
87  unsigned char l, h;
88 
89  while (len != 0)
90  {
91  h = (*ibuf) / 16;
92  l = (*ibuf) % 16;
93 
94  if( h < 10 )
95  *obuf++ = '0' + h;
96  else
97  *obuf++ = 'a' + h - 10;
98 
99  if( l < 10 )
100  *obuf++ = '0' + l;
101  else
102  *obuf++ = 'a' + l - 10;
103 
104  ++ibuf;
105  len--;
106  }
107 }
108 
118 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
119 {
120  size_t i;
121 
122  if( rng_state != NULL )
123  rng_state = NULL;
124 
125  for( i = 0; i < len; ++i )
126  output[i] = rand();
127 
128  return( 0 );
129 }
130 
136 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
137 {
138  if( rng_state != NULL )
139  rng_state = NULL;
140 
141  memset( output, 0, len );
142 
143  return( 0 );
144 }
145 
146 typedef struct
147 {
148  unsigned char *buf;
149  size_t length;
150 } rnd_buf_info;
151 
163 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
164 {
165  rnd_buf_info *info = (rnd_buf_info *) rng_state;
166  size_t use_len;
167 
168  if( rng_state == NULL )
169  return( rnd_std_rand( NULL, output, len ) );
170 
171  use_len = len;
172  if( len > info->length )
173  use_len = info->length;
174 
175  if( use_len )
176  {
177  memcpy( output, info->buf, use_len );
178  info->buf += use_len;
179  info->length -= use_len;
180  }
181 
182  if( len - use_len > 0 )
183  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
184 
185  return( 0 );
186 }
187 
195 typedef struct
196 {
197  uint32_t key[16];
198  uint32_t v0, v1;
200 
209 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
210 {
211  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
212  uint32_t i, *k, sum, delta=0x9E3779B9;
213  unsigned char result[4];
214 
215  if( rng_state == NULL )
216  return( rnd_std_rand( NULL, output, len ) );
217 
218  k = info->key;
219 
220  while( len > 0 )
221  {
222  size_t use_len = ( len > 4 ) ? 4 : len;
223  sum = 0;
224 
225  for( i = 0; i < 32; i++ )
226  {
227  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
228  sum += delta;
229  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
230  }
231 
232  PUT_UINT32_BE( info->v0, result, 0 );
233  memcpy( output, result, use_len );
234  len -= use_len;
235  }
236 
237  return( 0 );
238 }
239 
240 
242 {
243 #ifdef POLARSSL_DEBUG_C
244 #ifdef POLARSSL_BIGNUM_C
245 #ifdef POLARSSL_SSL_TLS_C
246 #ifdef POLARSSL_RSA_C
247 
248 
249  FCT_SUITE_BGN(test_suite_debug)
250  {
251 #ifdef POLARSSL_FS_IO
252 #ifdef POLARSSL_PEM_C
253 #ifdef POLARSSL_BASE64_C
254 
255  FCT_TEST_BGN(debug_print_certificate_1)
256  {
257  x509_cert crt;
258  ssl_context ssl;
259  struct buffer_data buffer;
260 
261  memset( &crt, 0, sizeof( x509_cert ) );
262  memset( &ssl, 0, sizeof( ssl_context ) );
263  memset( buffer.buf, 0, 2000 );
264  buffer.ptr = buffer.buf;
265 
266  ssl_set_dbg(&ssl, string_debug, &buffer);
267 
268  fct_chk( x509parse_crtfile( &crt, "data_files/server1.crt" ) == 0 );
269  debug_print_crt( &ssl, 0, "MyFile", 999, "PREFIX_", &crt);
270 
271  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 );
272 
273  x509_free( &crt );
274  }
275  FCT_TEST_END();
276 #endif /* POLARSSL_FS_IO */
277 #endif /* POLARSSL_PEM_C */
278 #endif /* POLARSSL_BASE64_C */
279 
280 
281  FCT_TEST_BGN(debug_print_mpi_1)
282  {
283  ssl_context ssl;
284  struct buffer_data buffer;
285  mpi val;
286 
287  mpi_init( &val );
288 
289  memset( &ssl, 0, sizeof( ssl_context ) );
290  memset( buffer.buf, 0, 2000 );
291  buffer.ptr = buffer.buf;
292 
293  fct_chk( mpi_read_string( &val, 16, "01020304050607" ) == 0 );
294  ssl_set_dbg(&ssl, string_debug, &buffer);
295 
296  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
297 
298  fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (49 bits) is:\nMyFile(0999): 01 02 03 04 05 06 07\n" ) == 0 );
299 
300  mpi_free( &val );
301  }
302  FCT_TEST_END();
303 
304 
305  FCT_TEST_BGN(debug_print_mpi_2)
306  {
307  ssl_context ssl;
308  struct buffer_data buffer;
309  mpi val;
310 
311  mpi_init( &val );
312 
313  memset( &ssl, 0, sizeof( ssl_context ) );
314  memset( buffer.buf, 0, 2000 );
315  buffer.ptr = buffer.buf;
316 
317  fct_chk( mpi_read_string( &val, 16, "00000000000007" ) == 0 );
318  ssl_set_dbg(&ssl, string_debug, &buffer);
319 
320  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
321 
322  fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (3 bits) is:\nMyFile(0999): 07\n" ) == 0 );
323 
324  mpi_free( &val );
325  }
326  FCT_TEST_END();
327 
328 
329  FCT_TEST_BGN(debug_print_mpi_3)
330  {
331  ssl_context ssl;
332  struct buffer_data buffer;
333  mpi val;
334 
335  mpi_init( &val );
336 
337  memset( &ssl, 0, sizeof( ssl_context ) );
338  memset( buffer.buf, 0, 2000 );
339  buffer.ptr = buffer.buf;
340 
341  fct_chk( mpi_read_string( &val, 16, "00000000000000" ) == 0 );
342  ssl_set_dbg(&ssl, string_debug, &buffer);
343 
344  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
345 
346  fct_chk( strcmp( buffer.buf, "MyFile(0999): value of 'VALUE' (0 bits) is:\nMyFile(0999): 00\n" ) == 0 );
347 
348  mpi_free( &val );
349  }
350  FCT_TEST_END();
351 
352 
353  FCT_TEST_BGN(debug_print_mpi_4)
354  {
355  ssl_context ssl;
356  struct buffer_data buffer;
357  mpi val;
358 
359  mpi_init( &val );
360 
361  memset( &ssl, 0, sizeof( ssl_context ) );
362  memset( buffer.buf, 0, 2000 );
363  buffer.ptr = buffer.buf;
364 
365  fct_chk( mpi_read_string( &val, 16, "0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
366  ssl_set_dbg(&ssl, string_debug, &buffer);
367 
368  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
369 
370  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 );
371 
372  mpi_free( &val );
373  }
374  FCT_TEST_END();
375 
376 
377  FCT_TEST_BGN(debug_print_mpi_5)
378  {
379  ssl_context ssl;
380  struct buffer_data buffer;
381  mpi val;
382 
383  mpi_init( &val );
384 
385  memset( &ssl, 0, sizeof( ssl_context ) );
386  memset( buffer.buf, 0, 2000 );
387  buffer.ptr = buffer.buf;
388 
389  fct_chk( mpi_read_string( &val, 16, "0000000000000000000000000000000000000000000000000000000941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
390  ssl_set_dbg(&ssl, string_debug, &buffer);
391 
392  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
393 
394  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 );
395 
396  mpi_free( &val );
397  }
398  FCT_TEST_END();
399 
400 
401  FCT_TEST_BGN(debug_print_mpi_6)
402  {
403  ssl_context ssl;
404  struct buffer_data buffer;
405  mpi val;
406 
407  mpi_init( &val );
408 
409  memset( &ssl, 0, sizeof( ssl_context ) );
410  memset( buffer.buf, 0, 2000 );
411  buffer.ptr = buffer.buf;
412 
413  fct_chk( mpi_read_string( &val, 16, "0000000000000000000000000000000000000000000000000000000041379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424" ) == 0 );
414  ssl_set_dbg(&ssl, string_debug, &buffer);
415 
416  debug_print_mpi( &ssl, 0, "MyFile", 999, "VALUE", &val);
417 
418  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 );
419 
420  mpi_free( &val );
421  }
422  FCT_TEST_END();
423 
424  }
425  FCT_SUITE_END();
426 
427 #endif /* POLARSSL_DEBUG_C */
428 #endif /* POLARSSL_BIGNUM_C */
429 #endif /* POLARSSL_SSL_TLS_C */
430 #endif /* POLARSSL_RSA_C */
431 
432 }
433 FCT_END();
434