9
9
10
10
#include <string.h>
11
11
#include <openssl/buffer.h>
12
+ #include <openssl/rand.h>
12
13
#include "internal/packet.h"
13
14
#include "testutil.h"
14
15
@@ -21,6 +22,9 @@ static const unsigned char empty[] = { 0x00 };
21
22
static const unsigned char alloc [] = { 0x02 , 0xfe , 0xff };
22
23
static const unsigned char submem [] = { 0x03 , 0x02 , 0xfe , 0xff };
23
24
static const unsigned char fixed [] = { 0xff , 0xff , 0xff };
25
+ static const unsigned char simpleder [] = {
26
+ 0xfc , 0x04 , 0x00 , 0x01 , 0x02 , 0x03 , 0xff , 0xfe , 0xfd
27
+ };
24
28
25
29
static BUF_MEM * buf ;
26
30
@@ -349,6 +353,68 @@ static int test_WPACKET_memcpy(void)
349
353
return 1 ;
350
354
}
351
355
356
+ static int test_WPACKET_init_der (void )
357
+ {
358
+ WPACKET pkt ;
359
+ unsigned char sbuf [1024 ];
360
+ unsigned char testdata [] = { 0x00 , 0x01 , 0x02 , 0x03 };
361
+ unsigned char testdata2 [259 ] = { 0x82 , 0x01 , 0x00 };
362
+ size_t written [2 ];
363
+ int i ;
364
+
365
+ /* Test initialising for writing DER */
366
+ if (!TEST_true (WPACKET_init_der (& pkt , sbuf , sizeof (sbuf )))
367
+ || !TEST_true (WPACKET_put_bytes_u24 (& pkt , 0xfffefd ))
368
+ /* Test writing data in a length prefixed sub-packet */
369
+ || !TEST_true (WPACKET_start_sub_packet (& pkt ))
370
+ || !TEST_true (WPACKET_memcpy (& pkt , testdata , sizeof (testdata )))
371
+ || !TEST_true (WPACKET_close (& pkt ))
372
+ || !TEST_true (WPACKET_put_bytes_u8 (& pkt , 0xfc ))
373
+ || !TEST_true (WPACKET_finish (& pkt ))
374
+ || !TEST_true (WPACKET_get_total_written (& pkt , & written [0 ]))
375
+ || !TEST_mem_eq (WPACKET_get_curr (& pkt ), written [0 ], simpleder ,
376
+ sizeof (simpleder )))
377
+ return cleanup (& pkt );
378
+
379
+ /* Generate random packet data for test */
380
+ if (!TEST_true (RAND_bytes (& testdata2 [3 ], sizeof (testdata2 ) - 3 )))
381
+ return 0 ;
382
+
383
+ /*
384
+ * Test with a sub-packet that has 2 length bytes. We do 2 passes - first
385
+ * with a NULL buffer, just to calculate lengths, and a second pass with a
386
+ * real buffer to actually generate a packet
387
+ */
388
+ for (i = 0 ; i < 2 ; i ++ ) {
389
+ if (i == 0 ) {
390
+ if (!TEST_true (WPACKET_init_null_der (& pkt )))
391
+ return 0 ;
392
+ } else {
393
+ if (!TEST_true (WPACKET_init_der (& pkt , sbuf , sizeof (sbuf ))))
394
+ return 0 ;
395
+ }
396
+ if (!TEST_true (WPACKET_start_sub_packet (& pkt ))
397
+ || !TEST_true (WPACKET_memcpy (& pkt , & testdata2 [3 ],
398
+ sizeof (testdata2 ) - 3 ))
399
+ || !TEST_true (WPACKET_close (& pkt ))
400
+ || !TEST_true (WPACKET_finish (& pkt ))
401
+ || !TEST_true (WPACKET_get_total_written (& pkt , & written [i ])))
402
+ return cleanup (& pkt );
403
+ }
404
+
405
+ /*
406
+ * Check that the size calculated in the first pass equals the size of the
407
+ * packet actually generated in the second pass. Also check the generated
408
+ * packet looks as we expect it to.
409
+ */
410
+ if (!TEST_size_t_eq (written [0 ], written [1 ])
411
+ || !TEST_mem_eq (WPACKET_get_curr (& pkt ), written [1 ], testdata2 ,
412
+ sizeof (testdata2 )))
413
+ return 0 ;
414
+
415
+ return 1 ;
416
+ }
417
+
352
418
int setup_tests (void )
353
419
{
354
420
if (!TEST_ptr (buf = BUF_MEM_new ()))
@@ -360,6 +426,7 @@ int setup_tests(void)
360
426
ADD_TEST (test_WPACKET_set_flags );
361
427
ADD_TEST (test_WPACKET_allocate_bytes );
362
428
ADD_TEST (test_WPACKET_memcpy );
429
+ ADD_TEST (test_WPACKET_init_der );
363
430
return 1 ;
364
431
}
365
432
0 commit comments