2 * Copyright 2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/rand.h>
14 #include <openssl/asn1t.h>
15 #include "internal/numbers.h"
16 #include "test_main.h"
20 # pragma GCC diagnostic ignored "-Wunused-function"
21 # pragma GCC diagnostic ignored "-Wformat"
24 # pragma clang diagnostic ignored "-Wunused-function"
25 # pragma clang diagnostic ignored "-Wformat"
28 /***** Custom test data ******************************************************/
31 * We conduct tests with these arrays for every type we try out.
32 * You will find the expected results together with the test structures
33 * for each type, further down.
36 static unsigned char t_zero[] = {
39 static unsigned char t_one[] = {
42 static unsigned char t_one_neg[] = {
45 static unsigned char t_longundef[] = {
46 0x7f, 0xff, 0xff, 0xff
48 static unsigned char t_9bytes_1[] = {
49 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
51 static unsigned char t_8bytes_1[] = {
52 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
54 static unsigned char t_8bytes_2[] = {
55 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
57 static unsigned char t_8bytes_3_pad[] = {
58 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
60 static unsigned char t_8bytes_4_neg[] = {
61 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
63 static unsigned char t_8bytes_5_negpad[] = {
64 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
68 static unsigned char t_5bytes_1[] = {
69 0x01, 0xff, 0xff, 0xff, 0xff
71 static unsigned char t_4bytes_1[] = {
72 0x00, 0x80, 0x00, 0x00, 0x00
74 /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */
75 static unsigned char t_4bytes_2[] = {
76 0x7f, 0xff, 0xff, 0xfe
78 static unsigned char t_4bytes_3_pad[] = {
79 0x00, 0x7f, 0xff, 0xff, 0xfe
81 static unsigned char t_4bytes_4_neg[] = {
82 0x80, 0x00, 0x00, 0x00
84 static unsigned char t_4bytes_5_negpad[] = {
85 0xff, 0x80, 0x00, 0x00, 0x00
89 unsigned char *bytes1;
91 unsigned char *bytes2;
94 #define CUSTOM_DATA(v) \
95 { v, sizeof(v), t_one, sizeof(t_one) }, \
96 { t_one, sizeof(t_one), v, sizeof(v) }
98 static TEST_CUSTOM_DATA test_custom_data[] = {
100 CUSTOM_DATA(t_longundef),
102 CUSTOM_DATA(t_one_neg),
103 CUSTOM_DATA(t_9bytes_1),
104 CUSTOM_DATA(t_8bytes_1),
105 CUSTOM_DATA(t_8bytes_2),
106 CUSTOM_DATA(t_8bytes_3_pad),
107 CUSTOM_DATA(t_8bytes_4_neg),
108 CUSTOM_DATA(t_8bytes_5_negpad),
109 CUSTOM_DATA(t_5bytes_1),
110 CUSTOM_DATA(t_4bytes_1),
111 CUSTOM_DATA(t_4bytes_2),
112 CUSTOM_DATA(t_4bytes_3_pad),
113 CUSTOM_DATA(t_4bytes_4_neg),
114 CUSTOM_DATA(t_4bytes_5_negpad),
118 /***** Type specific test data ***********************************************/
121 * First, a few utility things that all type specific data can use, or in some
126 * For easy creation of arrays of expected data. These macros correspond to
127 * the uses of CUSTOM_DATA above.
129 #define CUSTOM_EXPECTED_SUCCESS(num, znum) \
132 #define CUSTOM_EXPECTED_FAILURE \
137 * A structure to collect all test information in. There MUST be one instance
138 * of this for each test
140 typedef int i2d_fn(void *a, unsigned char **pp);
141 typedef void *d2i_fn(void **a, unsigned char **pp, long length);
142 typedef void ifree_fn(void *a);
144 ASN1_ITEM_EXP *asn1_type;
146 int skip; /* 1 if this package should be skipped */
148 /* An array of structures to compare decoded custom data with */
149 void *encode_expectations;
150 size_t encode_expectations_size;
151 size_t encode_expectations_elem_size;
154 * An array of structures that are encoded into a DER blob, which is
155 * then decoded, and result gets compared with the original.
158 size_t encdec_data_size;
159 size_t encdec_data_elem_size;
161 /* The i2d function to use with this type */
163 /* The d2i function to use with this type */
165 /* Function to free a decoded structure */
169 /* To facilitate the creation of an encdec_data array */
170 #define ENCDEC_DATA(num, znum) \
171 { 0xff, num, 1 }, { 0xff, 1, znum }
172 #define ENCDEC_ARRAY(max, zmax, min, zmin) \
173 ENCDEC_DATA(max,zmax), \
174 ENCDEC_DATA(min,zmin), \
176 ENCDEC_DATA(-1, -1), \
177 ENCDEC_DATA(0, ASN1_LONG_UNDEF)
179 #if OPENSSL_API_COMPAT < 0x10200000L
180 /***** LONG ******************************************************************/
183 /* If decoding is expected to succeed, set this to 1, otherwise 0 */
184 ASN1_BOOLEAN success;
189 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
190 ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN),
191 ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
192 ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
193 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
195 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
196 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
198 static ASN1_LONG_DATA long_expected_32bit[] = {
199 /* The following should fail on the second because it's the default */
200 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
201 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
202 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
203 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
204 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
205 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
206 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
207 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
208 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
209 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
210 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
211 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
212 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
213 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
214 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
215 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
217 static ASN1_LONG_DATA long_encdec_data_32bit[] = {
218 ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
219 /* Check that default numbers fail */
220 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
223 static TEST_PACKAGE long_test_package_32bit = {
224 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
226 sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
227 long_encdec_data_32bit,
228 sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
229 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
230 (ifree_fn *)ASN1_LONG_DATA_free
233 static ASN1_LONG_DATA long_expected_64bit[] = {
234 /* The following should fail on the second because it's the default */
235 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
236 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
237 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
238 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
239 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
240 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
241 CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
242 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
243 CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
244 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
245 CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
246 CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
247 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
248 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
249 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
250 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
252 static ASN1_LONG_DATA long_encdec_data_64bit[] = {
253 ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
254 /* Check that default numbers fail */
255 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
258 static TEST_PACKAGE long_test_package_64bit = {
259 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
261 sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
262 long_encdec_data_64bit,
263 sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
264 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
265 (ifree_fn *)ASN1_LONG_DATA_free
269 /***** INT32 *****************************************************************/
272 ASN1_BOOLEAN success;
277 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
278 ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN),
279 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
280 ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
281 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
283 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
284 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
286 static ASN1_INT32_DATA int32_expected[] = {
287 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
288 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
289 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
290 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
291 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
292 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
293 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
294 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
295 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
296 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
297 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
298 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
299 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
300 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
301 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
302 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
304 static ASN1_INT32_DATA int32_encdec_data[] = {
305 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
308 static TEST_PACKAGE int32_test_package = {
309 ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
310 int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
311 int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
312 (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
313 (ifree_fn *)ASN1_INT32_DATA_free
316 /***** UINT32 ****************************************************************/
319 ASN1_BOOLEAN success;
320 uint32_t test_uint32;
321 uint32_t test_zuint32;
324 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
325 ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN),
326 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
327 ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
328 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
330 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
331 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
333 static ASN1_UINT32_DATA uint32_expected[] = {
334 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
335 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
336 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
337 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
338 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
339 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
340 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
341 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
342 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
343 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
344 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
345 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
346 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
347 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
348 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
349 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
351 static ASN1_UINT32_DATA uint32_encdec_data[] = {
352 ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
355 static TEST_PACKAGE uint32_test_package = {
356 ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
357 uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
358 uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
359 (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
360 (ifree_fn *)ASN1_UINT32_DATA_free
363 /***** INT64 *****************************************************************/
366 ASN1_BOOLEAN success;
371 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
372 ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN),
373 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
374 ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
375 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
377 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
378 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
380 static ASN1_INT64_DATA int64_expected[] = {
381 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
382 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
383 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
384 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
385 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
386 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */
387 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
388 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
389 CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
390 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
391 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
392 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
393 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
394 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
395 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
396 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
398 static ASN1_INT64_DATA int64_encdec_data[] = {
399 ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
400 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
403 static TEST_PACKAGE int64_test_package = {
404 ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
405 int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
406 int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
407 (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
408 (ifree_fn *)ASN1_INT64_DATA_free
411 /***** UINT64 ****************************************************************/
414 ASN1_BOOLEAN success;
415 uint64_t test_uint64;
416 uint64_t test_zuint64;
419 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
420 ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN),
421 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
422 ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
423 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
425 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
426 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
428 static ASN1_UINT64_DATA uint64_expected[] = {
429 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
430 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
431 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
432 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
433 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
434 CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
436 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
437 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
438 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
439 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
440 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
441 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
442 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
443 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
444 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
445 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
447 static ASN1_UINT64_DATA uint64_encdec_data[] = {
448 ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
451 static TEST_PACKAGE uint64_test_package = {
452 ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
453 uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
454 uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
455 (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
456 (ifree_fn *)ASN1_UINT64_DATA_free
459 /***** General testing functions *********************************************/
462 /* Template structure to map onto any test data structure */
464 ASN1_BOOLEAN success;
465 unsigned char bytes[1]; /* In reality, there's more */
469 * do_decode returns a tristate:
472 * 0 decoded structure wasn't what was expected (failure)
473 * 1 decoded structure was what was expected (success)
475 static int do_decode(unsigned char *bytes, long nbytes,
476 const EXPECTED *expected, size_t expected_size,
477 const TEST_PACKAGE *package)
479 EXPECTED *enctst = NULL;
480 const unsigned char *start;
484 enctst = package->d2i(NULL, &bytes, nbytes);
485 if (enctst == NULL) {
486 if (expected->success == 0) {
493 if (start + nbytes == bytes
494 && memcmp(enctst, expected, expected_size) == 0)
500 package->ifree(enctst);
505 * do_encode returns a tristate:
508 * 0 encoded DER wasn't what was expected (failure)
509 * 1 encoded DER was what was expected (success)
511 static int do_encode(EXPECTED *input,
512 const unsigned char *expected, size_t expected_len,
513 const TEST_PACKAGE *package)
515 unsigned char *data = NULL;
519 len = package->i2d(input, &data);
523 if ((size_t)len != expected_len
524 || memcmp(data, expected, expected_len) != 0) {
525 if (input->success == 0) {
539 /* Do an encode/decode round trip */
540 static int do_enc_dec(EXPECTED *bytes, long nbytes,
541 const TEST_PACKAGE *package)
543 unsigned char *data = NULL;
548 len = package->i2d(p, &data);
552 ret = do_decode(data, len, bytes, nbytes, package);
557 static size_t der_encode_length(size_t len, unsigned char **pp)
561 OPENSSL_assert(len < 0x8000);
573 *(*pp)++ = lenbytes - 1;
575 *(*pp)++ = 0x80 | len;
577 *(*pp)++ = 0x80 | (len >> 8);
578 *(*pp)++ = len & 0xff;
585 static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
586 unsigned char **encoding, int explicit_default)
588 size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
589 const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
590 unsigned char *p = NULL;
594 * The first item is just an INTEGER tag, INTEGER length and INTEGER content
597 1 + der_encode_length(custom_data->nbytes1, NULL)
598 + custom_data->nbytes1;
600 for (i = custom_data->nbytes2; i > 0; i--) {
601 if (custom_data->bytes2[i - 1] != '\0')
604 if (explicit_default || i > 0) {
606 * The second item is an explicit tag, content length, INTEGER tag,
607 * INTEGER length, INTEGER bytes
610 1 + der_encode_length(custom_data->nbytes2, NULL)
611 + custom_data->nbytes2;
613 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
617 * The whole sequence is the sequence tag, content length, BOOLEAN true
618 * (copied from t_true), the first (firstbytes) and second (secondbytes)
622 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
623 + sizeof(t_true) + firstbytes + secondbytes;
625 *encoding = p = OPENSSL_malloc(seqbytes);
626 if (*encoding == NULL)
631 der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
633 /* ASN1_BOOLEAN TRUE */
634 memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
637 /* First INTEGER item (non-optional) */
638 *p++ = V_ASN1_INTEGER;
639 der_encode_length(custom_data->nbytes1, &p);
640 memcpy(p, custom_data->bytes1, custom_data->nbytes1);
641 p += custom_data->nbytes1;
643 if (secondbytes > 0) {
644 /* Second INTEGER item (optional) */
645 /* Start with the explicit optional tag */
647 der_encode_length(secondbytesinner, &p);
648 *p++ = V_ASN1_INTEGER;
649 der_encode_length(custom_data->nbytes2, &p);
650 memcpy(p, custom_data->bytes2, custom_data->nbytes2);
651 p += custom_data->nbytes2;
654 OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
659 /* Attempt to decode a custom encoding of the test structure */
660 static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
661 const EXPECTED *expected, size_t expected_size,
662 const TEST_PACKAGE *package)
664 unsigned char *encoding = NULL;
666 * We force the defaults to be explicitely encoded to make sure we test
667 * for defaults that shouldn't be present (i.e. we check for failure)
669 size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
672 if (encoding_length == 0)
675 ret = do_decode(encoding, encoding_length, expected, expected_size,
677 OPENSSL_free(encoding);
682 /* Attempt to encode the test structure and compare it to custom DER */
683 static int do_encode_custom(EXPECTED *input,
684 const TEST_CUSTOM_DATA *custom_data,
685 const TEST_PACKAGE *package)
687 unsigned char *expected = NULL;
688 size_t expected_length = make_custom_der(custom_data, &expected, 0);
691 if (expected_length == 0)
694 ret = do_encode(input, expected, expected_length, package);
695 OPENSSL_free(expected);
700 static int do_print_item(const TEST_PACKAGE *package)
702 #define DATA_BUF_SIZE 256
703 unsigned char buf[DATA_BUF_SIZE];
704 const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
705 ASN1_VALUE *o = (ASN1_VALUE *)&buf;
706 BIO *bio = BIO_new_fp(stdout, 0);
709 OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
711 (void)RAND_bytes(buf, (int)package->encode_expectations_elem_size);
712 ret = ASN1_item_print(bio, o, 0, i, NULL);
719 static int test_intern(const TEST_PACKAGE *package)
728 /* Do decode_custom checks */
729 nelems = package->encode_expectations_size
730 / package->encode_expectations_elem_size;
731 OPENSSL_assert(nelems ==
732 sizeof(test_custom_data) / sizeof(test_custom_data[0]));
733 for (i = 0; i < nelems; i++) {
734 size_t pos = i * package->encode_expectations_elem_size;
735 switch (do_encode_custom((EXPECTED *)&((unsigned char *)package
736 ->encode_expectations)[pos],
737 &test_custom_data[i], package)) {
739 fprintf(stderr, "Failed custom encode round trip %u of %s\n",
741 ERR_print_errors_fp(stderr);
746 fprintf(stderr, "Custom encode round trip %u of %s mismatch\n",
748 ERR_print_errors_fp(stderr);
755 OPENSSL_die("do_encode_custom() return unknown value",
758 switch (do_decode_custom(&test_custom_data[i],
759 (EXPECTED *)&((unsigned char *)package
760 ->encode_expectations)[pos],
761 package->encode_expectations_elem_size,
764 fprintf(stderr, "Failed custom decode round trip %u of %s\n",
766 ERR_print_errors_fp(stderr);
771 fprintf(stderr, "Custom decode round trip %u of %s mismatch\n",
773 ERR_print_errors_fp(stderr);
780 OPENSSL_die("do_decode_custom() return unknown value",
785 /* Do enc_dec checks */
786 nelems = package->encdec_data_size / package->encdec_data_elem_size;
787 for (i = 0; i < nelems; i++) {
788 size_t pos = i * package->encdec_data_elem_size;
789 switch (do_enc_dec((EXPECTED *)&((unsigned char *)package
791 package->encdec_data_elem_size,
794 fprintf(stderr, "Failed encode/decode round trip %u of %s\n",
796 ERR_print_errors_fp(stderr);
801 fprintf(stderr, "Encode/decode round trip %u of %s mismatch\n",
808 OPENSSL_die("do_enc_dec() return unknown value",
813 if (!do_print_item(package)) {
814 fprintf(stderr, "Printing of %s failed\n", package->name);
815 ERR_print_errors_fp(stderr);
822 #if OPENSSL_API_COMPAT < 0x10200000L
823 static int test_long_32bit(void)
825 return test_intern(&long_test_package_32bit);
828 static int test_long_64bit(void)
830 return test_intern(&long_test_package_64bit);
834 static int test_int32(void)
836 return test_intern(&int32_test_package);
839 static int test_uint32(void)
841 return test_intern(&uint32_test_package);
844 static int test_int64(void)
846 return test_intern(&int64_test_package);
849 static int test_uint64(void)
851 return test_intern(&uint64_test_package);
854 void register_tests(void)
856 #if OPENSSL_API_COMPAT < 0x10200000L
857 ADD_TEST(test_long_32bit);
858 ADD_TEST(test_long_64bit);
860 ADD_TEST(test_int32);
861 ADD_TEST(test_uint32);
862 ADD_TEST(test_int64);
863 ADD_TEST(test_uint64);