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"
19 # pragma GCC diagnostic ignored "-Wunused-function"
20 # pragma GCC diagnostic ignored "-Wformat"
23 # pragma clang diagnostic ignored "-Wunused-function"
24 # pragma clang diagnostic ignored "-Wformat"
27 /***** Custom test data ******************************************************/
30 * We conduct tests with these arrays for every type we try out.
31 * You will find the expected results together with the test structures
32 * for each type, further down.
35 static unsigned char t_zero[] = {
38 static unsigned char t_one[] = {
41 static unsigned char t_one_neg[] = {
44 static unsigned char t_longundef[] = {
45 0x7f, 0xff, 0xff, 0xff
47 static unsigned char t_9bytes_1[] = {
48 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
50 static unsigned char t_8bytes_1[] = {
51 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
53 static unsigned char t_8bytes_2[] = {
54 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
56 static unsigned char t_8bytes_3_pad[] = {
57 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
59 static unsigned char t_8bytes_4_neg[] = {
60 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62 static unsigned char t_8bytes_5_negpad[] = {
63 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
67 static unsigned char t_5bytes_1[] = {
68 0x01, 0xff, 0xff, 0xff, 0xff
70 static unsigned char t_4bytes_1[] = {
71 0x00, 0x80, 0x00, 0x00, 0x00
73 /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */
74 static unsigned char t_4bytes_2[] = {
75 0x7f, 0xff, 0xff, 0xfe
77 static unsigned char t_4bytes_3_pad[] = {
78 0x00, 0x7f, 0xff, 0xff, 0xfe
80 static unsigned char t_4bytes_4_neg[] = {
81 0x80, 0x00, 0x00, 0x00
83 static unsigned char t_4bytes_5_negpad[] = {
84 0xff, 0x80, 0x00, 0x00, 0x00
88 unsigned char *bytes1;
90 unsigned char *bytes2;
93 #define CUSTOM_DATA(v) \
94 { v, sizeof(v), t_one, sizeof(t_one) }, \
95 { t_one, sizeof(t_one), v, sizeof(v) }
97 static TEST_CUSTOM_DATA test_custom_data[] = {
99 CUSTOM_DATA(t_longundef),
101 CUSTOM_DATA(t_one_neg),
102 CUSTOM_DATA(t_9bytes_1),
103 CUSTOM_DATA(t_8bytes_1),
104 CUSTOM_DATA(t_8bytes_2),
105 CUSTOM_DATA(t_8bytes_3_pad),
106 CUSTOM_DATA(t_8bytes_4_neg),
107 CUSTOM_DATA(t_8bytes_5_negpad),
108 CUSTOM_DATA(t_5bytes_1),
109 CUSTOM_DATA(t_4bytes_1),
110 CUSTOM_DATA(t_4bytes_2),
111 CUSTOM_DATA(t_4bytes_3_pad),
112 CUSTOM_DATA(t_4bytes_4_neg),
113 CUSTOM_DATA(t_4bytes_5_negpad),
117 /***** Type specific test data ***********************************************/
120 * First, a few utility things that all type specific data can use, or in some
125 * For easy creation of arrays of expected data. These macros correspond to
126 * the uses of CUSTOM_DATA above.
128 #define CUSTOM_EXPECTED_SUCCESS(num, znum) \
131 #define CUSTOM_EXPECTED_FAILURE \
136 * A structure to collect all test information in. There MUST be one instance
137 * of this for each test
139 typedef int i2d_fn(void *a, unsigned char **pp);
140 typedef void *d2i_fn(void **a, unsigned char **pp, long length);
141 typedef void ifree_fn(void *a);
143 ASN1_ITEM_EXP *asn1_type;
145 int skip; /* 1 if this package should be skipped */
147 /* An array of structures to compare decoded custom data with */
148 void *encode_expectations;
149 size_t encode_expectations_size;
150 size_t encode_expectations_elem_size;
153 * An array of structures that are encoded into a DER blob, which is
154 * then decoded, and result gets compared with the original.
157 size_t encdec_data_size;
158 size_t encdec_data_elem_size;
160 /* The i2d function to use with this type */
162 /* The d2i function to use with this type */
164 /* Function to free a decoded structure */
168 /* To facilitate the creation of an encdec_data array */
169 #define ENCDEC_DATA(num, znum) \
170 { 0xff, num, 1 }, { 0xff, 1, znum }
171 #define ENCDEC_ARRAY(max, zmax, min, zmin) \
172 ENCDEC_DATA(max,zmax), \
173 ENCDEC_DATA(min,zmin), \
175 ENCDEC_DATA(-1, -1), \
176 ENCDEC_DATA(0, ASN1_LONG_UNDEF)
178 #if OPENSSL_API_COMPAT < 0x10200000L
179 /***** LONG ******************************************************************/
182 /* If decoding is expected to succeed, set this to 1, otherwise 0 */
183 ASN1_BOOLEAN success;
188 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
189 ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN),
190 ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
191 ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
192 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
194 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
195 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
197 static ASN1_LONG_DATA long_expected_32bit[] = {
198 /* The following should fail on the second because it's the default */
199 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
200 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
201 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
202 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
203 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
204 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
205 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
206 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
207 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
208 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
209 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
210 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
211 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
212 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
213 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
214 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
216 static ASN1_LONG_DATA long_encdec_data_32bit[] = {
217 ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
218 /* Check that default numbers fail */
219 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
222 static TEST_PACKAGE long_test_package_32bit = {
223 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
225 sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
226 long_encdec_data_32bit,
227 sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
228 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
229 (ifree_fn *)ASN1_LONG_DATA_free
232 static ASN1_LONG_DATA long_expected_64bit[] = {
233 /* The following should fail on the second because it's the default */
234 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
235 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
236 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
237 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
238 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
239 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
240 CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
241 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
242 CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
243 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
244 CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
245 CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
246 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
247 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
248 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
249 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
251 static ASN1_LONG_DATA long_encdec_data_64bit[] = {
252 ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
253 /* Check that default numbers fail */
254 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
257 static TEST_PACKAGE long_test_package_64bit = {
258 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
260 sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
261 long_encdec_data_64bit,
262 sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
263 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
264 (ifree_fn *)ASN1_LONG_DATA_free
268 /***** INT32 *****************************************************************/
271 ASN1_BOOLEAN success;
276 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
277 ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN),
278 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
279 ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
280 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
282 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
283 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
285 static ASN1_INT32_DATA int32_expected[] = {
286 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
287 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
288 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
289 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
290 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
291 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
292 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
293 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
294 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
295 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
296 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
297 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
298 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
299 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
300 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
301 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
303 static ASN1_INT32_DATA int32_encdec_data[] = {
304 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
307 static TEST_PACKAGE int32_test_package = {
308 ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
309 int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
310 int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
311 (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
312 (ifree_fn *)ASN1_INT32_DATA_free
315 /***** UINT32 ****************************************************************/
318 ASN1_BOOLEAN success;
319 uint32_t test_uint32;
320 uint32_t test_zuint32;
323 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
324 ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN),
325 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
326 ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
327 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
329 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
330 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
332 static ASN1_UINT32_DATA uint32_expected[] = {
333 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
334 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
335 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
336 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
337 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
338 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
339 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
340 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
341 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
342 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
343 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
344 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
345 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
346 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
347 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
348 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
350 static ASN1_UINT32_DATA uint32_encdec_data[] = {
351 ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
354 static TEST_PACKAGE uint32_test_package = {
355 ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
356 uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
357 uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
358 (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
359 (ifree_fn *)ASN1_UINT32_DATA_free
362 /***** INT64 *****************************************************************/
365 ASN1_BOOLEAN success;
370 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
371 ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN),
372 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
373 ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
374 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
376 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
377 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
379 static ASN1_INT64_DATA int64_expected[] = {
380 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
381 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
382 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
383 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
384 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
385 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */
386 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
387 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
388 CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
389 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
390 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
391 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
392 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
393 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
394 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
395 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
397 static ASN1_INT64_DATA int64_encdec_data[] = {
398 ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
399 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
402 static TEST_PACKAGE int64_test_package = {
403 ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
404 int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
405 int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
406 (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
407 (ifree_fn *)ASN1_INT64_DATA_free
410 /***** UINT64 ****************************************************************/
413 ASN1_BOOLEAN success;
414 uint64_t test_uint64;
415 uint64_t test_zuint64;
418 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
419 ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN),
420 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
421 ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
422 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
424 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
425 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
427 static ASN1_UINT64_DATA uint64_expected[] = {
428 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
429 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
430 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
431 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
432 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
433 CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
435 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
436 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
437 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
438 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
439 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
440 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
441 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
442 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
443 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
444 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
446 static ASN1_UINT64_DATA uint64_encdec_data[] = {
447 ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
450 static TEST_PACKAGE uint64_test_package = {
451 ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
452 uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
453 uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
454 (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
455 (ifree_fn *)ASN1_UINT64_DATA_free
458 /***** General testing functions *********************************************/
461 /* Template structure to map onto any test data structure */
463 ASN1_BOOLEAN success;
464 unsigned char bytes[1]; /* In reality, there's more */
468 * do_decode returns a tristate:
471 * 0 decoded structure wasn't what was expected (failure)
472 * 1 decoded structure was what was expected (success)
474 static int do_decode(unsigned char *bytes, long nbytes,
475 const EXPECTED *expected, size_t expected_size,
476 const TEST_PACKAGE *package)
478 EXPECTED *enctst = NULL;
479 const unsigned char *start;
483 enctst = package->d2i(NULL, &bytes, nbytes);
484 if (enctst == NULL) {
485 if (expected->success == 0) {
492 if (start + nbytes == bytes
493 && memcmp(enctst, expected, expected_size) == 0)
499 package->ifree(enctst);
504 * do_encode returns a tristate:
507 * 0 encoded DER wasn't what was expected (failure)
508 * 1 encoded DER was what was expected (success)
510 static int do_encode(EXPECTED *input,
511 const unsigned char *expected, size_t expected_len,
512 const TEST_PACKAGE *package)
514 unsigned char *data = NULL;
518 len = package->i2d(input, &data);
522 if ((size_t)len != expected_len
523 || memcmp(data, expected, expected_len) != 0) {
524 if (input->success == 0) {
538 /* Do an encode/decode round trip */
539 static int do_enc_dec(EXPECTED *bytes, long nbytes,
540 const TEST_PACKAGE *package)
542 unsigned char *data = NULL;
547 len = package->i2d(p, &data);
551 ret = do_decode(data, len, bytes, nbytes, package);
556 static size_t der_encode_length(size_t len, unsigned char **pp)
560 OPENSSL_assert(len < 0x8000);
572 *(*pp)++ = lenbytes - 1;
574 *(*pp)++ = 0x80 | len;
576 *(*pp)++ = 0x80 | (len >> 8);
577 *(*pp)++ = len & 0xff;
584 static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
585 unsigned char **encoding, int explicit_default)
587 size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
588 const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
589 unsigned char *p = NULL;
593 * The first item is just an INTEGER tag, INTEGER length and INTEGER content
596 1 + der_encode_length(custom_data->nbytes1, NULL)
597 + custom_data->nbytes1;
599 for (i = custom_data->nbytes2; i > 0; i--) {
600 if (custom_data->bytes2[i - 1] != '\0')
603 if (explicit_default || i > 0) {
605 * The second item is an explicit tag, content length, INTEGER tag,
606 * INTEGER length, INTEGER bytes
609 1 + der_encode_length(custom_data->nbytes2, NULL)
610 + custom_data->nbytes2;
612 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
616 * The whole sequence is the sequence tag, content length, BOOLEAN true
617 * (copied from t_true), the first (firstbytes) and second (secondbytes)
621 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
622 + sizeof(t_true) + firstbytes + secondbytes;
624 *encoding = p = OPENSSL_malloc(seqbytes);
625 if (*encoding == NULL)
630 der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
632 /* ASN1_BOOLEAN TRUE */
633 memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
636 /* First INTEGER item (non-optional) */
637 *p++ = V_ASN1_INTEGER;
638 der_encode_length(custom_data->nbytes1, &p);
639 memcpy(p, custom_data->bytes1, custom_data->nbytes1);
640 p += custom_data->nbytes1;
642 if (secondbytes > 0) {
643 /* Second INTEGER item (optional) */
644 /* Start with the explicit optional tag */
646 der_encode_length(secondbytesinner, &p);
647 *p++ = V_ASN1_INTEGER;
648 der_encode_length(custom_data->nbytes2, &p);
649 memcpy(p, custom_data->bytes2, custom_data->nbytes2);
650 p += custom_data->nbytes2;
653 OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
658 /* Attempt to decode a custom encoding of the test structure */
659 static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
660 const EXPECTED *expected, size_t expected_size,
661 const TEST_PACKAGE *package)
663 unsigned char *encoding = NULL;
665 * We force the defaults to be explicitely encoded to make sure we test
666 * for defaults that shouldn't be present (i.e. we check for failure)
668 size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
671 if (encoding_length == 0)
674 ret = do_decode(encoding, encoding_length, expected, expected_size,
676 OPENSSL_free(encoding);
681 /* Attempt to encode the test structure and compare it to custom DER */
682 static int do_encode_custom(EXPECTED *input,
683 const TEST_CUSTOM_DATA *custom_data,
684 const TEST_PACKAGE *package)
686 unsigned char *expected = NULL;
687 size_t expected_length = make_custom_der(custom_data, &expected, 0);
690 if (expected_length == 0)
693 ret = do_encode(input, expected, expected_length, package);
694 OPENSSL_free(expected);
699 static int do_print_item(const TEST_PACKAGE *package)
701 #define DATA_BUF_SIZE 256
702 unsigned char buf[DATA_BUF_SIZE];
703 const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
704 ASN1_VALUE *o = (ASN1_VALUE *)&buf;
705 BIO *bio = BIO_new_fp(stdout, 0);
708 OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
710 (void)RAND_bytes(buf, (int)package->encode_expectations_elem_size);
711 ret = ASN1_item_print(bio, o, 0, i, NULL);
718 static int test_intern(const TEST_PACKAGE *package)
727 /* Do decode_custom checks */
728 nelems = package->encode_expectations_size
729 / package->encode_expectations_elem_size;
730 OPENSSL_assert(nelems ==
731 sizeof(test_custom_data) / sizeof(test_custom_data[0]));
732 for (i = 0; i < nelems; i++) {
733 size_t pos = i * package->encode_expectations_elem_size;
734 switch (do_encode_custom((EXPECTED *)&((unsigned char *)package
735 ->encode_expectations)[pos],
736 &test_custom_data[i], package)) {
738 fprintf(stderr, "Failed custom encode round trip %u of %s\n",
740 ERR_print_errors_fp(stderr);
745 fprintf(stderr, "Custom encode round trip %u of %s mismatch\n",
747 ERR_print_errors_fp(stderr);
754 OPENSSL_die("do_encode_custom() return unknown value",
757 switch (do_decode_custom(&test_custom_data[i],
758 (EXPECTED *)&((unsigned char *)package
759 ->encode_expectations)[pos],
760 package->encode_expectations_elem_size,
763 fprintf(stderr, "Failed custom decode round trip %u of %s\n",
765 ERR_print_errors_fp(stderr);
770 fprintf(stderr, "Custom decode round trip %u of %s mismatch\n",
772 ERR_print_errors_fp(stderr);
779 OPENSSL_die("do_decode_custom() return unknown value",
784 /* Do enc_dec checks */
785 nelems = package->encdec_data_size / package->encdec_data_elem_size;
786 for (i = 0; i < nelems; i++) {
787 size_t pos = i * package->encdec_data_elem_size;
788 switch (do_enc_dec((EXPECTED *)&((unsigned char *)package
790 package->encdec_data_elem_size,
793 fprintf(stderr, "Failed encode/decode round trip %u of %s\n",
795 ERR_print_errors_fp(stderr);
800 fprintf(stderr, "Encode/decode round trip %u of %s mismatch\n",
807 OPENSSL_die("do_enc_dec() return unknown value",
812 if (!do_print_item(package)) {
813 fprintf(stderr, "Printing of %s failed\n", package->name);
814 ERR_print_errors_fp(stderr);
821 #if OPENSSL_API_COMPAT < 0x10200000L
822 static int test_long_32bit(void)
824 return test_intern(&long_test_package_32bit);
827 static int test_long_64bit(void)
829 return test_intern(&long_test_package_64bit);
833 static int test_int32(void)
835 return test_intern(&int32_test_package);
838 static int test_uint32(void)
840 return test_intern(&uint32_test_package);
843 static int test_int64(void)
845 return test_intern(&int64_test_package);
848 static int test_uint64(void)
850 return test_intern(&uint64_test_package);
853 void register_tests(void)
855 #if OPENSSL_API_COMPAT < 0x10200000L
856 ADD_TEST(test_long_32bit);
857 ADD_TEST(test_long_64bit);
859 ADD_TEST(test_int32);
860 ADD_TEST(test_uint32);
861 ADD_TEST(test_int64);
862 ADD_TEST(test_uint64);