2 * Copyright 2017-2018 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_minus_256[] = {
47 static unsigned char t_longundef[] = {
48 0x7f, 0xff, 0xff, 0xff
50 static unsigned char t_9bytes_1[] = {
51 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
53 static unsigned char t_8bytes_1[] = {
54 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
56 static unsigned char t_8bytes_2[] = {
57 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
59 static unsigned char t_8bytes_3_pad[] = {
60 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
62 static unsigned char t_8bytes_4_neg[] = {
63 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
65 static unsigned char t_8bytes_5_negpad[] = {
66 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
70 static unsigned char t_5bytes_1[] = {
71 0x01, 0xff, 0xff, 0xff, 0xff
73 static unsigned char t_4bytes_1[] = {
74 0x00, 0x80, 0x00, 0x00, 0x00
76 /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */
77 static unsigned char t_4bytes_2[] = {
78 0x7f, 0xff, 0xff, 0xfe
80 static unsigned char t_4bytes_3_pad[] = {
81 0x00, 0x7f, 0xff, 0xff, 0xfe
83 static unsigned char t_4bytes_4_neg[] = {
84 0x80, 0x00, 0x00, 0x00
86 static unsigned char t_4bytes_5_negpad[] = {
87 0xff, 0x80, 0x00, 0x00, 0x00
91 unsigned char *bytes1;
93 unsigned char *bytes2;
96 #define CUSTOM_DATA(v) \
97 { v, sizeof(v), t_one, sizeof(t_one) }, \
98 { t_one, sizeof(t_one), v, sizeof(v) }
100 static TEST_CUSTOM_DATA test_custom_data[] = {
102 CUSTOM_DATA(t_longundef),
104 CUSTOM_DATA(t_one_neg),
105 CUSTOM_DATA(t_minus_256),
106 CUSTOM_DATA(t_9bytes_1),
107 CUSTOM_DATA(t_8bytes_1),
108 CUSTOM_DATA(t_8bytes_2),
109 CUSTOM_DATA(t_8bytes_3_pad),
110 CUSTOM_DATA(t_8bytes_4_neg),
111 CUSTOM_DATA(t_8bytes_5_negpad),
112 CUSTOM_DATA(t_5bytes_1),
113 CUSTOM_DATA(t_4bytes_1),
114 CUSTOM_DATA(t_4bytes_2),
115 CUSTOM_DATA(t_4bytes_3_pad),
116 CUSTOM_DATA(t_4bytes_4_neg),
117 CUSTOM_DATA(t_4bytes_5_negpad),
121 /***** Type specific test data ***********************************************/
124 * First, a few utility things that all type specific data can use, or in some
129 * For easy creation of arrays of expected data. These macros correspond to
130 * the uses of CUSTOM_DATA above.
132 #define CUSTOM_EXPECTED_SUCCESS(num, znum) \
135 #define CUSTOM_EXPECTED_FAILURE \
140 * A structure to collect all test information in. There MUST be one instance
141 * of this for each test
143 typedef int i2d_fn(void *a, unsigned char **pp);
144 typedef void *d2i_fn(void **a, unsigned char **pp, long length);
145 typedef void ifree_fn(void *a);
147 ASN1_ITEM_EXP *asn1_type;
149 int skip; /* 1 if this package should be skipped */
151 /* An array of structures to compare decoded custom data with */
152 void *encode_expectations;
153 size_t encode_expectations_size;
154 size_t encode_expectations_elem_size;
157 * An array of structures that are encoded into a DER blob, which is
158 * then decoded, and result gets compared with the original.
161 size_t encdec_data_size;
162 size_t encdec_data_elem_size;
164 /* The i2d function to use with this type */
166 /* The d2i function to use with this type */
168 /* Function to free a decoded structure */
172 /* To facilitate the creation of an encdec_data array */
173 #define ENCDEC_DATA(num, znum) \
174 { 0xff, num, 1 }, { 0xff, 1, znum }
175 #define ENCDEC_ARRAY(max, zmax, min, zmin) \
176 ENCDEC_DATA(max,zmax), \
177 ENCDEC_DATA(min,zmin), \
179 ENCDEC_DATA(-1, -1), \
180 ENCDEC_DATA(0, ASN1_LONG_UNDEF)
182 #if OPENSSL_API_COMPAT < 0x10200000L
183 /***** LONG ******************************************************************/
186 /* If decoding is expected to succeed, set this to 1, otherwise 0 */
187 ASN1_BOOLEAN success;
192 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
193 ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN),
194 ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
195 ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
196 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
198 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
199 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
201 static ASN1_LONG_DATA long_expected_32bit[] = {
202 /* The following should fail on the second because it's the default */
203 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
204 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
205 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
206 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
207 CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
208 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
209 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
210 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
211 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
212 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
213 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
214 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
215 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
216 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
217 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
218 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
219 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
221 static ASN1_LONG_DATA long_encdec_data_32bit[] = {
222 ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
223 /* Check that default numbers fail */
224 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
227 static TEST_PACKAGE long_test_package_32bit = {
228 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4,
230 sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
231 long_encdec_data_32bit,
232 sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
233 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
234 (ifree_fn *)ASN1_LONG_DATA_free
237 static ASN1_LONG_DATA long_expected_64bit[] = {
238 /* The following should fail on the second because it's the default */
239 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
240 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
241 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
242 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
243 CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
244 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
245 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
246 CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
247 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
248 CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
249 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
250 CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
251 CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
252 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
253 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
254 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
255 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
257 static ASN1_LONG_DATA long_encdec_data_64bit[] = {
258 ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
259 /* Check that default numbers fail */
260 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
263 static TEST_PACKAGE long_test_package_64bit = {
264 ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8,
266 sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
267 long_encdec_data_64bit,
268 sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
269 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
270 (ifree_fn *)ASN1_LONG_DATA_free
274 /***** INT32 *****************************************************************/
277 ASN1_BOOLEAN success;
282 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
283 ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN),
284 ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
285 ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
286 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
288 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
289 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
291 static ASN1_INT32_DATA int32_expected[] = {
292 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
293 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
294 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
295 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
296 CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
297 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
298 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
299 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
300 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
301 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
302 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
303 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
304 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
305 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
306 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
307 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
308 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
310 static ASN1_INT32_DATA int32_encdec_data[] = {
311 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
314 static TEST_PACKAGE int32_test_package = {
315 ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0,
316 int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
317 int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
318 (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
319 (ifree_fn *)ASN1_INT32_DATA_free
322 /***** UINT32 ****************************************************************/
325 ASN1_BOOLEAN success;
326 uint32_t test_uint32;
327 uint32_t test_zuint32;
330 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
331 ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN),
332 ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
333 ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
334 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
336 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
337 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
339 static ASN1_UINT32_DATA uint32_expected[] = {
340 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
341 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
342 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
343 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
344 CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */
345 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
346 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
347 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
348 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
349 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
350 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
351 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
352 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
353 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
354 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
355 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
356 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
358 static ASN1_UINT32_DATA uint32_encdec_data[] = {
359 ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
362 static TEST_PACKAGE uint32_test_package = {
363 ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0,
364 uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
365 uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
366 (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
367 (ifree_fn *)ASN1_UINT32_DATA_free
370 /***** INT64 *****************************************************************/
373 ASN1_BOOLEAN success;
378 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
379 ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN),
380 ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
381 ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
382 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
384 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
385 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
387 static ASN1_INT64_DATA int64_expected[] = {
388 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
389 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
390 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
391 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
392 CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */
393 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
394 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */
395 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
396 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
397 CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
398 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
399 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
400 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
401 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
402 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
403 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
404 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
406 static ASN1_INT64_DATA int64_encdec_data[] = {
407 ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
408 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
411 static TEST_PACKAGE int64_test_package = {
412 ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0,
413 int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
414 int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
415 (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
416 (ifree_fn *)ASN1_INT64_DATA_free
419 /***** UINT64 ****************************************************************/
422 ASN1_BOOLEAN success;
423 uint64_t test_uint64;
424 uint64_t test_zuint64;
427 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
428 ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN),
429 ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
430 ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
431 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
433 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
434 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
436 static ASN1_UINT64_DATA uint64_expected[] = {
437 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
438 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
439 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
440 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
441 CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */
442 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
443 CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
445 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
446 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
447 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
448 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
449 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
450 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
451 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
452 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
453 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
454 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
456 static ASN1_UINT64_DATA uint64_encdec_data[] = {
457 ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
460 static TEST_PACKAGE uint64_test_package = {
461 ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0,
462 uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
463 uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
464 (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
465 (ifree_fn *)ASN1_UINT64_DATA_free
468 /***** General testing functions *********************************************/
471 /* Template structure to map onto any test data structure */
473 ASN1_BOOLEAN success;
474 unsigned char bytes[1]; /* In reality, there's more */
478 * do_decode returns a tristate:
481 * 0 decoded structure wasn't what was expected (failure)
482 * 1 decoded structure was what was expected (success)
484 static int do_decode(unsigned char *bytes, long nbytes,
485 const EXPECTED *expected, size_t expected_size,
486 const TEST_PACKAGE *package)
488 EXPECTED *enctst = NULL;
489 const unsigned char *start;
493 enctst = package->d2i(NULL, &bytes, nbytes);
494 if (enctst == NULL) {
495 if (expected->success == 0) {
502 if (start + nbytes == bytes
503 && memcmp(enctst, expected, expected_size) == 0)
509 package->ifree(enctst);
514 * do_encode returns a tristate:
517 * 0 encoded DER wasn't what was expected (failure)
518 * 1 encoded DER was what was expected (success)
520 static int do_encode(EXPECTED *input,
521 const unsigned char *expected, size_t expected_len,
522 const TEST_PACKAGE *package)
524 unsigned char *data = NULL;
528 len = package->i2d(input, &data);
532 if ((size_t)len != expected_len
533 || memcmp(data, expected, expected_len) != 0) {
534 if (input->success == 0) {
548 /* Do an encode/decode round trip */
549 static int do_enc_dec(EXPECTED *bytes, long nbytes,
550 const TEST_PACKAGE *package)
552 unsigned char *data = NULL;
557 len = package->i2d(p, &data);
561 ret = do_decode(data, len, bytes, nbytes, package);
566 static size_t der_encode_length(size_t len, unsigned char **pp)
570 OPENSSL_assert(len < 0x8000);
580 *(*pp)++ = (unsigned char)len;
582 *(*pp)++ = (unsigned char)(lenbytes - 1);
584 *(*pp)++ = (unsigned char)(0x80 | len);
586 *(*pp)++ = (unsigned char)(0x80 | (len >> 8));
587 *(*pp)++ = (unsigned char)(len);
594 static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
595 unsigned char **encoding, int explicit_default)
597 size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
598 const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
599 unsigned char *p = NULL;
603 * The first item is just an INTEGER tag, INTEGER length and INTEGER content
606 1 + der_encode_length(custom_data->nbytes1, NULL)
607 + custom_data->nbytes1;
609 for (i = custom_data->nbytes2; i > 0; i--) {
610 if (custom_data->bytes2[i - 1] != '\0')
613 if (explicit_default || i > 0) {
615 * The second item is an explicit tag, content length, INTEGER tag,
616 * INTEGER length, INTEGER bytes
619 1 + der_encode_length(custom_data->nbytes2, NULL)
620 + custom_data->nbytes2;
622 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
626 * The whole sequence is the sequence tag, content length, BOOLEAN true
627 * (copied from t_true), the first (firstbytes) and second (secondbytes)
631 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
632 + sizeof(t_true) + firstbytes + secondbytes;
634 *encoding = p = OPENSSL_malloc(seqbytes);
635 if (*encoding == NULL)
640 der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
642 /* ASN1_BOOLEAN TRUE */
643 memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
646 /* First INTEGER item (non-optional) */
647 *p++ = V_ASN1_INTEGER;
648 der_encode_length(custom_data->nbytes1, &p);
649 memcpy(p, custom_data->bytes1, custom_data->nbytes1);
650 p += custom_data->nbytes1;
652 if (secondbytes > 0) {
653 /* Second INTEGER item (optional) */
654 /* Start with the explicit optional tag */
656 der_encode_length(secondbytesinner, &p);
657 *p++ = V_ASN1_INTEGER;
658 der_encode_length(custom_data->nbytes2, &p);
659 memcpy(p, custom_data->bytes2, custom_data->nbytes2);
660 p += custom_data->nbytes2;
663 OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
668 /* Attempt to decode a custom encoding of the test structure */
669 static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
670 const EXPECTED *expected, size_t expected_size,
671 const TEST_PACKAGE *package)
673 unsigned char *encoding = NULL;
675 * We force the defaults to be explicitly encoded to make sure we test
676 * for defaults that shouldn't be present (i.e. we check for failure)
678 size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
681 if (encoding_length == 0)
684 ret = do_decode(encoding, encoding_length, expected, expected_size,
686 OPENSSL_free(encoding);
691 /* Attempt to encode the test structure and compare it to custom DER */
692 static int do_encode_custom(EXPECTED *input,
693 const TEST_CUSTOM_DATA *custom_data,
694 const TEST_PACKAGE *package)
696 unsigned char *expected = NULL;
697 size_t expected_length = make_custom_der(custom_data, &expected, 0);
700 if (expected_length == 0)
703 ret = do_encode(input, expected, expected_length, package);
704 OPENSSL_free(expected);
709 static int do_print_item(const TEST_PACKAGE *package)
711 #define DATA_BUF_SIZE 256
712 const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type);
716 OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE);
717 if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL)
720 (void)RAND_bytes((unsigned char*)o,
721 (int)package->encode_expectations_elem_size);
722 ret = ASN1_item_print(bio_err, o, 0, i, NULL);
729 static int test_intern(const TEST_PACKAGE *package)
738 /* Do decode_custom checks */
739 nelems = package->encode_expectations_size
740 / package->encode_expectations_elem_size;
741 OPENSSL_assert(nelems ==
742 sizeof(test_custom_data) / sizeof(test_custom_data[0]));
743 for (i = 0; i < nelems; i++) {
744 size_t pos = i * package->encode_expectations_elem_size;
745 switch (do_encode_custom((EXPECTED *)&((unsigned char *)package
746 ->encode_expectations)[pos],
747 &test_custom_data[i], package)) {
749 TEST_error("Failed custom encode round trip %u of %s",
751 TEST_openssl_errors();
755 TEST_error("Custom encode round trip %u of %s mismatch",
757 TEST_openssl_errors();
763 OPENSSL_die("do_encode_custom() return unknown value",
766 switch (do_decode_custom(&test_custom_data[i],
767 (EXPECTED *)&((unsigned char *)package
768 ->encode_expectations)[pos],
769 package->encode_expectations_elem_size,
772 TEST_error("Failed custom decode round trip %u of %s",
774 TEST_openssl_errors();
778 TEST_error("Custom decode round trip %u of %s mismatch",
780 TEST_openssl_errors();
786 OPENSSL_die("do_decode_custom() return unknown value",
791 /* Do enc_dec checks */
792 nelems = package->encdec_data_size / package->encdec_data_elem_size;
793 for (i = 0; i < nelems; i++) {
794 size_t pos = i * package->encdec_data_elem_size;
795 switch (do_enc_dec((EXPECTED *)&((unsigned char *)package
797 package->encdec_data_elem_size,
800 TEST_error("Failed encode/decode round trip %u of %s",
802 TEST_openssl_errors();
806 TEST_error("Encode/decode round trip %u of %s mismatch",
813 OPENSSL_die("do_enc_dec() return unknown value",
818 if (!do_print_item(package)) {
819 TEST_error("Printing of %s failed", package->name);
820 TEST_openssl_errors();
827 #if OPENSSL_API_COMPAT < 0x10200000L
828 static int test_long_32bit(void)
830 return test_intern(&long_test_package_32bit);
833 static int test_long_64bit(void)
835 return test_intern(&long_test_package_64bit);
839 static int test_int32(void)
841 return test_intern(&int32_test_package);
844 static int test_uint32(void)
846 return test_intern(&uint32_test_package);
849 static int test_int64(void)
851 return test_intern(&int64_test_package);
854 static int test_uint64(void)
856 return test_intern(&uint64_test_package);
859 int setup_tests(void)
861 #if OPENSSL_API_COMPAT < 0x10200000L
862 ADD_TEST(test_long_32bit);
863 ADD_TEST(test_long_64bit);
865 ADD_TEST(test_int32);
866 ADD_TEST(test_uint32);
867 ADD_TEST(test_int64);
868 ADD_TEST(test_uint64);