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/asn1t.h>
14 #include "internal/numbers.h"
15 #include "test_main.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);
144 int skip; /* 1 if this package should be skipped */
146 /* An array of structures to compare decoded custom data with */
147 void *encode_expectations;
148 size_t encode_expectations_size;
149 size_t encode_expectations_elem_size;
152 * An array of structures that are encoded into a DER blob, which is
153 * then decoded, and result gets compared with the original.
156 size_t encdec_data_size;
157 size_t encdec_data_elem_size;
159 /* The i2d function to use with this type */
161 /* The d2i function to use with this type */
163 /* Function to free a decoded structure */
167 /* To facilitate the creation of an encdec_data array */
168 #define ENCDEC_DATA(num, znum) \
169 { 0xff, num, 1 }, { 0xff, 1, znum }
170 #define ENCDEC_ARRAY(max, zmax, min, zmin) \
171 ENCDEC_DATA(max,zmax), \
172 ENCDEC_DATA(min,zmin), \
174 ENCDEC_DATA(-1, -1), \
175 ENCDEC_DATA(0, ASN1_LONG_UNDEF)
177 #if OPENSSL_API_COMPAT < 0x10200000L
178 /***** LONG ******************************************************************/
181 /* If decoding is expected to succeed, set this to 1, otherwise 0 */
182 ASN1_BOOLEAN success;
187 ASN1_SEQUENCE(ASN1_LONG_DATA) = {
188 ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN),
189 ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG),
190 ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0)
191 } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
193 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
194 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
196 static ASN1_LONG_DATA long_expected_32bit[] = {
197 /* The following should fail on the second because it's the default */
198 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
199 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
200 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
201 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
202 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
203 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
204 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
205 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
206 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
207 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
208 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
209 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
210 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
211 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
212 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
213 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
215 static ASN1_LONG_DATA long_encdec_data_32bit[] = {
216 ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN),
217 /* Check that default numbers fail */
218 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
221 static TEST_PACKAGE long_test_package_32bit = {
222 "LONG", sizeof(long) != 4,
224 sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]),
225 long_encdec_data_32bit,
226 sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]),
227 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
228 (ifree_fn *)ASN1_LONG_DATA_free
231 static ASN1_LONG_DATA long_expected_64bit[] = {
232 /* The following should fail on the second because it's the default */
233 { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */
234 { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */
235 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
236 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
237 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
238 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
239 CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */
240 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
241 CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */
242 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
243 CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */
244 CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */
245 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
246 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
247 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
248 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
250 static ASN1_LONG_DATA long_encdec_data_64bit[] = {
251 ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN),
252 /* Check that default numbers fail */
253 { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 }
256 static TEST_PACKAGE long_test_package_64bit = {
257 "LONG", sizeof(long) != 8,
259 sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]),
260 long_encdec_data_64bit,
261 sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]),
262 (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA,
263 (ifree_fn *)ASN1_LONG_DATA_free
267 /***** INT32 *****************************************************************/
270 ASN1_BOOLEAN success;
275 ASN1_SEQUENCE(ASN1_INT32_DATA) = {
276 ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN),
277 ASN1_SIMPLE(ASN1_INT32_DATA, test_int32, INT32),
278 ASN1_EXP_OPT(ASN1_INT32_DATA, test_zint32, ZINT32, 0)
279 } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
281 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
282 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
284 static ASN1_INT32_DATA int32_expected[] = {
285 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
286 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
287 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
288 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
289 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
290 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
291 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
292 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
293 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
294 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
295 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
296 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */
297 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
298 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
299 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
300 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
302 static ASN1_INT32_DATA int32_encdec_data[] = {
303 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
306 static TEST_PACKAGE int32_test_package = {
308 int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]),
309 int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]),
310 (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA,
311 (ifree_fn *)ASN1_INT32_DATA_free
314 /***** UINT32 ****************************************************************/
317 ASN1_BOOLEAN success;
318 uint32_t test_uint32;
319 uint32_t test_zuint32;
322 ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
323 ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN),
324 ASN1_SIMPLE(ASN1_UINT32_DATA, test_uint32, UINT32),
325 ASN1_EXP_OPT(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0)
326 } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
328 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
329 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
331 static ASN1_UINT32_DATA uint32_expected[] = {
332 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
333 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
334 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
335 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
336 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
337 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */
338 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */
339 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
340 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
341 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
342 CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */
343 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
344 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
345 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
346 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
347 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
349 static ASN1_UINT32_DATA uint32_encdec_data[] = {
350 ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0),
353 static TEST_PACKAGE uint32_test_package = {
355 uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]),
356 uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]),
357 (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA,
358 (ifree_fn *)ASN1_UINT32_DATA_free
361 /***** INT64 *****************************************************************/
364 ASN1_BOOLEAN success;
369 ASN1_SEQUENCE(ASN1_INT64_DATA) = {
370 ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN),
371 ASN1_SIMPLE(ASN1_INT64_DATA, test_int64, INT64),
372 ASN1_EXP_OPT(ASN1_INT64_DATA, test_zint64, ZINT64, 0)
373 } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
375 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
376 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
378 static ASN1_INT64_DATA int64_expected[] = {
379 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
380 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
381 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
382 CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */
383 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
384 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */
385 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
386 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */
387 CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */
388 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */
389 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
390 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
391 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
392 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
393 CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */
394 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
396 static ASN1_INT64_DATA int64_encdec_data[] = {
397 ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN),
398 ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN),
401 static TEST_PACKAGE int64_test_package = {
403 int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]),
404 int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]),
405 (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA,
406 (ifree_fn *)ASN1_INT64_DATA_free
409 /***** UINT64 ****************************************************************/
412 ASN1_BOOLEAN success;
413 uint64_t test_uint64;
414 uint64_t test_zuint64;
417 ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
418 ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN),
419 ASN1_SIMPLE(ASN1_UINT64_DATA, test_uint64, UINT64),
420 ASN1_EXP_OPT(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0)
421 } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
423 IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
424 IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
426 static ASN1_UINT64_DATA uint64_expected[] = {
427 CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */
428 CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */
429 CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */
430 CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */
431 CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */
432 CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1),
434 CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */
435 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */
436 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */
437 CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */
438 CUSTOM_EXPECTED_SUCCESS(0x1ffffffff, 0x1ffffffff), /* t_5bytes_1 */
439 CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */
440 CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */
441 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */
442 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */
443 CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */
445 static ASN1_UINT64_DATA uint64_encdec_data[] = {
446 ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0),
449 static TEST_PACKAGE uint64_test_package = {
451 uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]),
452 uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]),
453 (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA,
454 (ifree_fn *)ASN1_UINT64_DATA_free
457 /***** General testing functions *********************************************/
460 /* Template structure to map onto any test data structure */
462 ASN1_BOOLEAN success;
463 unsigned char bytes[1]; /* In reality, there's more */
467 * do_decode returns a tristate:
470 * 0 decoded structure wasn't what was expected (failure)
471 * 1 decoded structure was what was expected (success)
473 static int do_decode(unsigned char *bytes, long nbytes,
474 const EXPECTED *expected, size_t expected_size,
475 const TEST_PACKAGE *package)
477 EXPECTED *enctst = NULL;
478 const unsigned char *start;
482 enctst = package->d2i(NULL, &bytes, nbytes);
483 if (enctst == NULL) {
484 if (expected->success == 0) {
491 if (start + nbytes == bytes
492 && memcmp(enctst, expected, expected_size) == 0)
498 package->ifree(enctst);
503 * do_encode returns a tristate:
506 * 0 encoded DER wasn't what was expected (failure)
507 * 1 encoded DER was what was expected (success)
509 static int do_encode(EXPECTED *input,
510 const unsigned char *expected, size_t expected_len,
511 const TEST_PACKAGE *package)
513 unsigned char *data = NULL;
517 len = package->i2d(input, &data);
521 if ((size_t)len != expected_len
522 || memcmp(data, expected, expected_len) != 0) {
523 if (input->success == 0) {
537 /* Do an encode/decode round trip */
538 static int do_enc_dec(EXPECTED *bytes, long nbytes,
539 const TEST_PACKAGE *package)
541 unsigned char *data = NULL;
546 len = package->i2d(p, &data);
550 ret = do_decode(data, len, bytes, nbytes, package);
555 static size_t der_encode_length(size_t len, unsigned char **pp)
559 OPENSSL_assert(len < 0x8000);
571 *(*pp)++ = lenbytes - 1;
573 *(*pp)++ = 0x80 | len;
575 *(*pp)++ = 0x80 | (len >> 8);
576 *(*pp)++ = len & 0xff;
583 static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data,
584 unsigned char **encoding, int explicit_default)
586 size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes;
587 const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff };
588 unsigned char *p = NULL;
592 * The first item is just an INTEGER tag, INTEGER length and INTEGER content
595 1 + der_encode_length(custom_data->nbytes1, NULL)
596 + custom_data->nbytes1;
598 for (i = custom_data->nbytes2; i > 0; i--) {
599 if (custom_data->bytes2[i - 1] != '\0')
602 if (explicit_default || i > 0) {
604 * The second item is an explicit tag, content length, INTEGER tag,
605 * INTEGER length, INTEGER bytes
608 1 + der_encode_length(custom_data->nbytes2, NULL)
609 + custom_data->nbytes2;
611 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner;
615 * The whole sequence is the sequence tag, content length, BOOLEAN true
616 * (copied from t_true), the first (firstbytes) and second (secondbytes)
620 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL)
621 + sizeof(t_true) + firstbytes + secondbytes;
623 *encoding = p = OPENSSL_malloc(seqbytes);
624 if (*encoding == NULL)
629 der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p);
631 /* ASN1_BOOLEAN TRUE */
632 memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */
635 /* First INTEGER item (non-optional) */
636 *p++ = V_ASN1_INTEGER;
637 der_encode_length(custom_data->nbytes1, &p);
638 memcpy(p, custom_data->bytes1, custom_data->nbytes1);
639 p += custom_data->nbytes1;
641 if (secondbytes > 0) {
642 /* Second INTEGER item (optional) */
643 /* Start with the explicit optional tag */
645 der_encode_length(secondbytesinner, &p);
646 *p++ = V_ASN1_INTEGER;
647 der_encode_length(custom_data->nbytes2, &p);
648 memcpy(p, custom_data->bytes2, custom_data->nbytes2);
649 p += custom_data->nbytes2;
652 OPENSSL_assert(seqbytes == (size_t)(p - *encoding));
657 /* Attempt to decode a custom encoding of the test structure */
658 static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data,
659 const EXPECTED *expected, size_t expected_size,
660 const TEST_PACKAGE *package)
662 unsigned char *encoding = NULL;
664 * We force the defaults to be explicitely encoded to make sure we test
665 * for defaults that shouldn't be present (i.e. we check for failure)
667 size_t encoding_length = make_custom_der(custom_data, &encoding, 1);
670 if (encoding_length == 0)
673 ret = do_decode(encoding, encoding_length, expected, expected_size,
675 OPENSSL_free(encoding);
680 /* Attempt to encode the test structure and compare it to custom DER */
681 static int do_encode_custom(EXPECTED *input,
682 const TEST_CUSTOM_DATA *custom_data,
683 const TEST_PACKAGE *package)
685 unsigned char *expected = NULL;
686 size_t expected_length = make_custom_der(custom_data, &expected, 0);
689 if (expected_length == 0)
692 ret = do_encode(input, expected, expected_length, package);
693 OPENSSL_free(expected);
699 static int test_intern(const TEST_PACKAGE *package)
708 /* Do decode_custom checks */
709 nelems = package->encode_expectations_size
710 / package->encode_expectations_elem_size;
711 OPENSSL_assert(nelems ==
712 sizeof(test_custom_data) / sizeof(test_custom_data[0]));
713 for (i = 0; i < nelems; i++) {
714 size_t pos = i * package->encode_expectations_elem_size;
715 switch (do_encode_custom((EXPECTED *)&((unsigned char *)package
716 ->encode_expectations)[pos],
717 &test_custom_data[i], package)) {
719 fprintf(stderr, "Failed custom encode round trip %u of %s\n",
721 ERR_print_errors_fp(stderr);
726 fprintf(stderr, "Custom encode round trip %u of %s mismatch\n",
728 ERR_print_errors_fp(stderr);
735 OPENSSL_die("do_encode_custom() return unknown value",
738 switch (do_decode_custom(&test_custom_data[i],
739 (EXPECTED *)&((unsigned char *)package
740 ->encode_expectations)[pos],
741 package->encode_expectations_elem_size,
744 fprintf(stderr, "Failed custom decode round trip %u of %s\n",
746 ERR_print_errors_fp(stderr);
751 fprintf(stderr, "Custom decode round trip %u of %s mismatch\n",
753 ERR_print_errors_fp(stderr);
760 OPENSSL_die("do_decode_custom() return unknown value",
765 /* Do enc_dec checks */
766 nelems = package->encdec_data_size / package->encdec_data_elem_size;
767 for (i = 0; i < nelems; i++) {
768 size_t pos = i * package->encdec_data_elem_size;
769 switch (do_enc_dec((EXPECTED *)&((unsigned char *)package
771 package->encdec_data_elem_size,
774 fprintf(stderr, "Failed encode/decode round trip %u of %s\n",
776 ERR_print_errors_fp(stderr);
781 fprintf(stderr, "Encode/decode round trip %u of %s mismatch\n",
788 OPENSSL_die("do_enc_dec() return unknown value",
796 #if OPENSSL_API_COMPAT < 0x10200000L
797 static int test_long_32bit(void)
799 return test_intern(&long_test_package_32bit);
802 static int test_long_64bit(void)
804 return test_intern(&long_test_package_64bit);
808 static int test_int32(void)
810 return test_intern(&int32_test_package);
813 static int test_uint32(void)
815 return test_intern(&uint32_test_package);
818 static int test_int64(void)
820 return test_intern(&int64_test_package);
823 static int test_uint64(void)
825 return test_intern(&uint64_test_package);
828 void register_tests(void)
830 #if OPENSSL_API_COMPAT < 0x10200000L
831 ADD_TEST(test_long_32bit);
832 ADD_TEST(test_long_64bit);
834 ADD_TEST(test_int32);
835 ADD_TEST(test_uint32);
836 ADD_TEST(test_int64);
837 ADD_TEST(test_uint64);