1 1.1 christos /* 2 1.1 christos * Copyright 2017-2020 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the OpenSSL license (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos #include <stdio.h> 11 1.1 christos #include <string.h> 12 1.1 christos 13 1.1 christos #include <openssl/rand.h> 14 1.1 christos #include <openssl/asn1t.h> 15 1.1 christos #include "internal/numbers.h" 16 1.1 christos #include "testutil.h" 17 1.1 christos 18 1.1 christos #ifdef __GNUC__ 19 1.1 christos # pragma GCC diagnostic ignored "-Wunused-function" 20 1.1 christos # pragma GCC diagnostic ignored "-Wformat" 21 1.1 christos #endif 22 1.1 christos #ifdef __clang__ 23 1.1 christos # pragma clang diagnostic ignored "-Wunused-function" 24 1.1 christos # pragma clang diagnostic ignored "-Wformat" 25 1.1 christos #endif 26 1.1 christos 27 1.1 christos /***** Custom test data ******************************************************/ 28 1.1 christos 29 1.1 christos /* 30 1.1 christos * We conduct tests with these arrays for every type we try out. 31 1.1 christos * You will find the expected results together with the test structures 32 1.1 christos * for each type, further down. 33 1.1 christos */ 34 1.1 christos 35 1.1 christos static unsigned char t_zero[] = { 36 1.1 christos 0x00 37 1.1 christos }; 38 1.1 christos static unsigned char t_one[] = { 39 1.1 christos 0x01 40 1.1 christos }; 41 1.1 christos static unsigned char t_one_neg[] = { 42 1.1 christos 0xff 43 1.1 christos }; 44 1.1 christos static unsigned char t_minus_256[] = { 45 1.1 christos 0xff, 0x00 46 1.1 christos }; 47 1.1 christos static unsigned char t_longundef[] = { 48 1.1 christos 0x7f, 0xff, 0xff, 0xff 49 1.1 christos }; 50 1.1 christos static unsigned char t_9bytes_1[] = { 51 1.1 christos 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 52 1.1 christos }; 53 1.1 christos static unsigned char t_8bytes_1[] = { 54 1.1 christos 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 55 1.1 christos }; 56 1.1 christos static unsigned char t_8bytes_2[] = { 57 1.1 christos 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 58 1.1 christos }; 59 1.1 christos static unsigned char t_8bytes_3_pad[] = { 60 1.1 christos 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff 61 1.1 christos }; 62 1.1 christos static unsigned char t_8bytes_4_neg[] = { 63 1.1 christos 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 64 1.1 christos }; 65 1.1 christos static unsigned char t_8bytes_5_negpad[] = { 66 1.1 christos 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 67 1.1 christos }; 68 1.1 christos 69 1.1 christos /* 32-bit long */ 70 1.1 christos static unsigned char t_5bytes_1[] = { 71 1.1 christos 0x01, 0xff, 0xff, 0xff, 0xff 72 1.1 christos }; 73 1.1 christos static unsigned char t_4bytes_1[] = { 74 1.1 christos 0x00, 0x80, 0x00, 0x00, 0x00 75 1.1 christos }; 76 1.1 christos /* We make the last byte 0xfe to avoid a clash with ASN1_LONG_UNDEF */ 77 1.1 christos static unsigned char t_4bytes_2[] = { 78 1.1 christos 0x7f, 0xff, 0xff, 0xfe 79 1.1 christos }; 80 1.1 christos static unsigned char t_4bytes_3_pad[] = { 81 1.1 christos 0x00, 0x7f, 0xff, 0xff, 0xfe 82 1.1 christos }; 83 1.1 christos static unsigned char t_4bytes_4_neg[] = { 84 1.1 christos 0x80, 0x00, 0x00, 0x00 85 1.1 christos }; 86 1.1 christos static unsigned char t_4bytes_5_negpad[] = { 87 1.1 christos 0xff, 0x80, 0x00, 0x00, 0x00 88 1.1 christos }; 89 1.1 christos 90 1.1 christos typedef struct { 91 1.1 christos unsigned char *bytes1; 92 1.1 christos size_t nbytes1; 93 1.1 christos unsigned char *bytes2; 94 1.1 christos size_t nbytes2; 95 1.1 christos } TEST_CUSTOM_DATA; 96 1.1 christos #define CUSTOM_DATA(v) \ 97 1.1 christos { v, sizeof(v), t_one, sizeof(t_one) }, \ 98 1.1 christos { t_one, sizeof(t_one), v, sizeof(v) } 99 1.1 christos 100 1.1 christos static TEST_CUSTOM_DATA test_custom_data[] = { 101 1.1 christos CUSTOM_DATA(t_zero), 102 1.1 christos CUSTOM_DATA(t_longundef), 103 1.1 christos CUSTOM_DATA(t_one), 104 1.1 christos CUSTOM_DATA(t_one_neg), 105 1.1 christos CUSTOM_DATA(t_minus_256), 106 1.1 christos CUSTOM_DATA(t_9bytes_1), 107 1.1 christos CUSTOM_DATA(t_8bytes_1), 108 1.1 christos CUSTOM_DATA(t_8bytes_2), 109 1.1 christos CUSTOM_DATA(t_8bytes_3_pad), 110 1.1 christos CUSTOM_DATA(t_8bytes_4_neg), 111 1.1 christos CUSTOM_DATA(t_8bytes_5_negpad), 112 1.1 christos CUSTOM_DATA(t_5bytes_1), 113 1.1 christos CUSTOM_DATA(t_4bytes_1), 114 1.1 christos CUSTOM_DATA(t_4bytes_2), 115 1.1 christos CUSTOM_DATA(t_4bytes_3_pad), 116 1.1 christos CUSTOM_DATA(t_4bytes_4_neg), 117 1.1 christos CUSTOM_DATA(t_4bytes_5_negpad), 118 1.1 christos }; 119 1.1 christos 120 1.1 christos 121 1.1 christos /***** Type specific test data ***********************************************/ 122 1.1 christos 123 1.1 christos /* 124 1.1 christos * First, a few utility things that all type specific data can use, or in some 125 1.1 christos * cases, MUST use. 126 1.1 christos */ 127 1.1 christos 128 1.1 christos /* 129 1.1 christos * For easy creation of arrays of expected data. These macros correspond to 130 1.1 christos * the uses of CUSTOM_DATA above. 131 1.1 christos */ 132 1.1 christos #define CUSTOM_EXPECTED_SUCCESS(num, znum) \ 133 1.1 christos { 0xff, num, 1 }, \ 134 1.1 christos { 0xff, 1, znum } 135 1.1 christos #define CUSTOM_EXPECTED_FAILURE \ 136 1.1 christos { 0, 0, 0 }, \ 137 1.1 christos { 0, 0, 0 } 138 1.1 christos 139 1.1 christos /* 140 1.1 christos * A structure to collect all test information in. There MUST be one instance 141 1.1 christos * of this for each test 142 1.1 christos */ 143 1.1 christos typedef int i2d_fn(void *a, unsigned char **pp); 144 1.1 christos typedef void *d2i_fn(void **a, unsigned char **pp, long length); 145 1.1 christos typedef void ifree_fn(void *a); 146 1.1 christos typedef struct { 147 1.1 christos ASN1_ITEM_EXP *asn1_type; 148 1.1 christos const char *name; 149 1.1 christos int skip; /* 1 if this package should be skipped */ 150 1.1 christos 151 1.1 christos /* An array of structures to compare decoded custom data with */ 152 1.1 christos void *encode_expectations; 153 1.1 christos size_t encode_expectations_size; 154 1.1 christos size_t encode_expectations_elem_size; 155 1.1 christos 156 1.1 christos /* 157 1.1 christos * An array of structures that are encoded into a DER blob, which is 158 1.1 christos * then decoded, and result gets compared with the original. 159 1.1 christos */ 160 1.1 christos void *encdec_data; 161 1.1 christos size_t encdec_data_size; 162 1.1 christos size_t encdec_data_elem_size; 163 1.1 christos 164 1.1 christos /* The i2d function to use with this type */ 165 1.1 christos i2d_fn *i2d; 166 1.1 christos /* The d2i function to use with this type */ 167 1.1 christos d2i_fn *d2i; 168 1.1 christos /* Function to free a decoded structure */ 169 1.1 christos ifree_fn *ifree; 170 1.1 christos } TEST_PACKAGE; 171 1.1 christos 172 1.1 christos /* To facilitate the creation of an encdec_data array */ 173 1.1 christos #define ENCDEC_DATA(num, znum) \ 174 1.1 christos { 0xff, num, 1 }, { 0xff, 1, znum } 175 1.1 christos #define ENCDEC_ARRAY(max, zmax, min, zmin) \ 176 1.1 christos ENCDEC_DATA(max,zmax), \ 177 1.1 christos ENCDEC_DATA(min,zmin), \ 178 1.1 christos ENCDEC_DATA(1, 1), \ 179 1.1 christos ENCDEC_DATA(-1, -1), \ 180 1.1 christos ENCDEC_DATA(0, ASN1_LONG_UNDEF) 181 1.1 christos 182 1.1 christos #if OPENSSL_API_COMPAT < 0x10200000L 183 1.1 christos /***** LONG ******************************************************************/ 184 1.1 christos 185 1.1 christos typedef struct { 186 1.1 christos /* If decoding is expected to succeed, set this to 1, otherwise 0 */ 187 1.1 christos ASN1_BOOLEAN success; 188 1.1 christos long test_long; 189 1.1 christos long test_zlong; 190 1.1 christos } ASN1_LONG_DATA; 191 1.1 christos 192 1.1 christos ASN1_SEQUENCE(ASN1_LONG_DATA) = { 193 1.1 christos ASN1_SIMPLE(ASN1_LONG_DATA, success, ASN1_FBOOLEAN), 194 1.1 christos ASN1_SIMPLE(ASN1_LONG_DATA, test_long, LONG), 195 1.1 christos ASN1_EXP_OPT(ASN1_LONG_DATA, test_zlong, ZLONG, 0) 196 1.1 christos } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA) 197 1.1 christos 198 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA) 199 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA) 200 1.1 christos 201 1.1 christos static ASN1_LONG_DATA long_expected_32bit[] = { 202 1.1 christos /* The following should fail on the second because it's the default */ 203 1.1 christos { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */ 204 1.1 christos { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */ 205 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 206 1.1 christos CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */ 207 1.1 christos CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */ 208 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 209 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */ 210 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */ 211 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */ 212 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */ 213 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */ 214 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */ 215 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */ 216 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 217 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 218 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */ 219 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 220 1.1 christos }; 221 1.1 christos static ASN1_LONG_DATA long_encdec_data_32bit[] = { 222 1.1 christos ENCDEC_ARRAY(LONG_MAX - 1, LONG_MAX, LONG_MIN, LONG_MIN), 223 1.1 christos /* Check that default numbers fail */ 224 1.1 christos { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 } 225 1.1 christos }; 226 1.1 christos 227 1.1 christos static TEST_PACKAGE long_test_package_32bit = { 228 1.1 christos ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 4, 229 1.1 christos long_expected_32bit, 230 1.1 christos sizeof(long_expected_32bit), sizeof(long_expected_32bit[0]), 231 1.1 christos long_encdec_data_32bit, 232 1.1 christos sizeof(long_encdec_data_32bit), sizeof(long_encdec_data_32bit[0]), 233 1.1 christos (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA, 234 1.1 christos (ifree_fn *)ASN1_LONG_DATA_free 235 1.1 christos }; 236 1.1 christos 237 1.1 christos static ASN1_LONG_DATA long_expected_64bit[] = { 238 1.1 christos /* The following should fail on the second because it's the default */ 239 1.1 christos { 0xff, 0, 1 }, { 0, 0, 0 }, /* t_zero */ 240 1.1 christos { 0, 0, 0 }, { 0xff, 1, 0x7fffffff }, /* t_longundef */ 241 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 242 1.1 christos CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */ 243 1.1 christos CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */ 244 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 245 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */ 246 1.1 christos CUSTOM_EXPECTED_SUCCESS(LONG_MAX, LONG_MAX), /* t_8bytes_2 */ 247 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */ 248 1.1 christos CUSTOM_EXPECTED_SUCCESS(LONG_MIN, LONG_MIN), /* t_8bytes_4_neg */ 249 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */ 250 1.1 christos CUSTOM_EXPECTED_SUCCESS((long)0x1ffffffff, (long)0x1ffffffff), /* t_5bytes_1 */ 251 1.1 christos CUSTOM_EXPECTED_SUCCESS((long)0x80000000, (long)0x80000000), /* t_4bytes_1 */ 252 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 253 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 254 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */ 255 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 256 1.1 christos }; 257 1.1 christos static ASN1_LONG_DATA long_encdec_data_64bit[] = { 258 1.1 christos ENCDEC_ARRAY(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN), 259 1.1 christos /* Check that default numbers fail */ 260 1.1 christos { 0, ASN1_LONG_UNDEF, 1 }, { 0, 1, 0 } 261 1.1 christos }; 262 1.1 christos 263 1.1 christos static TEST_PACKAGE long_test_package_64bit = { 264 1.1 christos ASN1_ITEM_ref(ASN1_LONG_DATA), "LONG", sizeof(long) != 8, 265 1.1 christos long_expected_64bit, 266 1.1 christos sizeof(long_expected_64bit), sizeof(long_expected_64bit[0]), 267 1.1 christos long_encdec_data_64bit, 268 1.1 christos sizeof(long_encdec_data_64bit), sizeof(long_encdec_data_64bit[0]), 269 1.1 christos (i2d_fn *)i2d_ASN1_LONG_DATA, (d2i_fn *)d2i_ASN1_LONG_DATA, 270 1.1 christos (ifree_fn *)ASN1_LONG_DATA_free 271 1.1 christos }; 272 1.1 christos #endif 273 1.1 christos 274 1.1 christos /***** INT32 *****************************************************************/ 275 1.1 christos 276 1.1 christos typedef struct { 277 1.1 christos ASN1_BOOLEAN success; 278 1.1 christos int32_t test_int32; 279 1.1 christos int32_t test_zint32; 280 1.1 christos } ASN1_INT32_DATA; 281 1.1 christos 282 1.1 christos ASN1_SEQUENCE(ASN1_INT32_DATA) = { 283 1.1 christos ASN1_SIMPLE(ASN1_INT32_DATA, success, ASN1_FBOOLEAN), 284 1.1 christos ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32), 285 1.1 christos ASN1_EXP_OPT_EMBED(ASN1_INT32_DATA, test_zint32, ZINT32, 0) 286 1.1 christos } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA) 287 1.1 christos 288 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA) 289 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA) 290 1.1 christos 291 1.1 christos static ASN1_INT32_DATA int32_expected[] = { 292 1.1 christos CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */ 293 1.1 christos CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */ 294 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 295 1.1 christos CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */ 296 1.1 christos CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */ 297 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 298 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */ 299 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */ 300 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */ 301 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */ 302 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */ 303 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */ 304 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_1 (too large positive) */ 305 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 306 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 307 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */ 308 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 309 1.1 christos }; 310 1.1 christos static ASN1_INT32_DATA int32_encdec_data[] = { 311 1.1 christos ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN), 312 1.1 christos }; 313 1.1 christos 314 1.1 christos static TEST_PACKAGE int32_test_package = { 315 1.1 christos ASN1_ITEM_ref(ASN1_INT32_DATA), "INT32", 0, 316 1.1 christos int32_expected, sizeof(int32_expected), sizeof(int32_expected[0]), 317 1.1 christos int32_encdec_data, sizeof(int32_encdec_data), sizeof(int32_encdec_data[0]), 318 1.1 christos (i2d_fn *)i2d_ASN1_INT32_DATA, (d2i_fn *)d2i_ASN1_INT32_DATA, 319 1.1 christos (ifree_fn *)ASN1_INT32_DATA_free 320 1.1 christos }; 321 1.1 christos 322 1.1 christos /***** UINT32 ****************************************************************/ 323 1.1 christos 324 1.1 christos typedef struct { 325 1.1 christos ASN1_BOOLEAN success; 326 1.1 christos uint32_t test_uint32; 327 1.1 christos uint32_t test_zuint32; 328 1.1 christos } ASN1_UINT32_DATA; 329 1.1 christos 330 1.1 christos ASN1_SEQUENCE(ASN1_UINT32_DATA) = { 331 1.1 christos ASN1_SIMPLE(ASN1_UINT32_DATA, success, ASN1_FBOOLEAN), 332 1.1 christos ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32), 333 1.1 christos ASN1_EXP_OPT_EMBED(ASN1_UINT32_DATA, test_zuint32, ZUINT32, 0) 334 1.1 christos } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA) 335 1.1 christos 336 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA) 337 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA) 338 1.1 christos 339 1.1 christos static ASN1_UINT32_DATA uint32_expected[] = { 340 1.1 christos CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */ 341 1.1 christos CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */ 342 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 343 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */ 344 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */ 345 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 346 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 */ 347 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_2 */ 348 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */ 349 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */ 350 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */ 351 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_5bytes_1 */ 352 1.1 christos CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */ 353 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 354 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 355 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */ 356 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 357 1.1 christos }; 358 1.1 christos static ASN1_UINT32_DATA uint32_encdec_data[] = { 359 1.1 christos ENCDEC_ARRAY(UINT32_MAX, UINT32_MAX, 0, 0), 360 1.1 christos }; 361 1.1 christos 362 1.1 christos static TEST_PACKAGE uint32_test_package = { 363 1.1 christos ASN1_ITEM_ref(ASN1_UINT32_DATA), "UINT32", 0, 364 1.1 christos uint32_expected, sizeof(uint32_expected), sizeof(uint32_expected[0]), 365 1.1 christos uint32_encdec_data, sizeof(uint32_encdec_data), sizeof(uint32_encdec_data[0]), 366 1.1 christos (i2d_fn *)i2d_ASN1_UINT32_DATA, (d2i_fn *)d2i_ASN1_UINT32_DATA, 367 1.1 christos (ifree_fn *)ASN1_UINT32_DATA_free 368 1.1 christos }; 369 1.1 christos 370 1.1 christos /***** INT64 *****************************************************************/ 371 1.1 christos 372 1.1 christos typedef struct { 373 1.1 christos ASN1_BOOLEAN success; 374 1.1 christos int64_t test_int64; 375 1.1 christos int64_t test_zint64; 376 1.1 christos } ASN1_INT64_DATA; 377 1.1 christos 378 1.1 christos ASN1_SEQUENCE(ASN1_INT64_DATA) = { 379 1.1 christos ASN1_SIMPLE(ASN1_INT64_DATA, success, ASN1_FBOOLEAN), 380 1.1 christos ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64), 381 1.1 christos ASN1_EXP_OPT_EMBED(ASN1_INT64_DATA, test_zint64, ZINT64, 0) 382 1.1 christos } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA) 383 1.1 christos 384 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA) 385 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA) 386 1.1 christos 387 1.1 christos static ASN1_INT64_DATA int64_expected[] = { 388 1.1 christos CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */ 389 1.1 christos CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */ 390 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 391 1.1 christos CUSTOM_EXPECTED_SUCCESS(-1, -1), /* t_one_neg */ 392 1.1 christos CUSTOM_EXPECTED_SUCCESS(-256, -256), /* t_minus_256 */ 393 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 394 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_1 (too large positive) */ 395 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */ 396 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad (illegal padding) */ 397 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT64_MIN, INT64_MIN), /* t_8bytes_4_neg */ 398 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad (illegal padding) */ 399 1.1 christos CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */ 400 1.1 christos CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */ 401 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 402 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 403 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MIN, INT32_MIN), /* t_4bytes_4_neg */ 404 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 405 1.1 christos }; 406 1.1 christos static ASN1_INT64_DATA int64_encdec_data[] = { 407 1.1 christos ENCDEC_ARRAY(INT64_MAX, INT64_MAX, INT64_MIN, INT64_MIN), 408 1.1 christos ENCDEC_ARRAY(INT32_MAX, INT32_MAX, INT32_MIN, INT32_MIN), 409 1.1 christos }; 410 1.1 christos 411 1.1 christos static TEST_PACKAGE int64_test_package = { 412 1.1 christos ASN1_ITEM_ref(ASN1_INT64_DATA), "INT64", 0, 413 1.1 christos int64_expected, sizeof(int64_expected), sizeof(int64_expected[0]), 414 1.1 christos int64_encdec_data, sizeof(int64_encdec_data), sizeof(int64_encdec_data[0]), 415 1.1 christos (i2d_fn *)i2d_ASN1_INT64_DATA, (d2i_fn *)d2i_ASN1_INT64_DATA, 416 1.1 christos (ifree_fn *)ASN1_INT64_DATA_free 417 1.1 christos }; 418 1.1 christos 419 1.1 christos /***** UINT64 ****************************************************************/ 420 1.1 christos 421 1.1 christos typedef struct { 422 1.1 christos ASN1_BOOLEAN success; 423 1.1 christos uint64_t test_uint64; 424 1.1 christos uint64_t test_zuint64; 425 1.1 christos } ASN1_UINT64_DATA; 426 1.1 christos 427 1.1 christos ASN1_SEQUENCE(ASN1_UINT64_DATA) = { 428 1.1 christos ASN1_SIMPLE(ASN1_UINT64_DATA, success, ASN1_FBOOLEAN), 429 1.1 christos ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64), 430 1.1 christos ASN1_EXP_OPT_EMBED(ASN1_UINT64_DATA, test_zuint64, ZUINT64, 0) 431 1.1 christos } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA) 432 1.1 christos 433 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA) 434 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA) 435 1.1 christos 436 1.1 christos static ASN1_UINT64_DATA uint64_expected[] = { 437 1.1 christos CUSTOM_EXPECTED_SUCCESS(0, 0), /* t_zero */ 438 1.1 christos CUSTOM_EXPECTED_SUCCESS(ASN1_LONG_UNDEF, ASN1_LONG_UNDEF), /* t_zero */ 439 1.1 christos CUSTOM_EXPECTED_SUCCESS(1, 1), /* t_one */ 440 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_one_neg (illegal negative value) */ 441 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_minus_256 (illegal negative value) */ 442 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_9bytes_1 */ 443 1.1 christos CUSTOM_EXPECTED_SUCCESS((uint64_t)INT64_MAX+1, (uint64_t)INT64_MAX+1), 444 1.1 christos /* t_8bytes_1 */ 445 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT64_MAX, INT64_MAX), /* t_8bytes_2 */ 446 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_3_pad */ 447 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_4_neg */ 448 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_8bytes_5_negpad */ 449 1.1 christos CUSTOM_EXPECTED_SUCCESS(0x1ffffffffULL, 0x1ffffffffULL), /* t_5bytes_1 */ 450 1.1 christos CUSTOM_EXPECTED_SUCCESS(0x80000000, 0x80000000), /* t_4bytes_1 */ 451 1.1 christos CUSTOM_EXPECTED_SUCCESS(INT32_MAX - 1, INT32_MAX -1), /* t_4bytes_2 */ 452 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_3_pad (illegal padding) */ 453 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_4_neg (illegal negative value) */ 454 1.1 christos CUSTOM_EXPECTED_FAILURE, /* t_4bytes_5_negpad (illegal padding) */ 455 1.1 christos }; 456 1.1 christos static ASN1_UINT64_DATA uint64_encdec_data[] = { 457 1.1 christos ENCDEC_ARRAY(UINT64_MAX, UINT64_MAX, 0, 0), 458 1.1 christos }; 459 1.1 christos 460 1.1 christos static TEST_PACKAGE uint64_test_package = { 461 1.1 christos ASN1_ITEM_ref(ASN1_UINT64_DATA), "UINT64", 0, 462 1.1 christos uint64_expected, sizeof(uint64_expected), sizeof(uint64_expected[0]), 463 1.1 christos uint64_encdec_data, sizeof(uint64_encdec_data), sizeof(uint64_encdec_data[0]), 464 1.1 christos (i2d_fn *)i2d_ASN1_UINT64_DATA, (d2i_fn *)d2i_ASN1_UINT64_DATA, 465 1.1 christos (ifree_fn *)ASN1_UINT64_DATA_free 466 1.1 christos }; 467 1.1 christos 468 1.1 christos /***** General testing functions *********************************************/ 469 1.1 christos 470 1.1 christos 471 1.1 christos /* Template structure to map onto any test data structure */ 472 1.1 christos typedef struct { 473 1.1 christos ASN1_BOOLEAN success; 474 1.1 christos unsigned char bytes[1]; /* In reality, there's more */ 475 1.1 christos } EXPECTED; 476 1.1 christos 477 1.1 christos /* 478 1.1 christos * do_decode returns a tristate: 479 1.1 christos * 480 1.1 christos * -1 Couldn't decode 481 1.1 christos * 0 decoded structure wasn't what was expected (failure) 482 1.1 christos * 1 decoded structure was what was expected (success) 483 1.1 christos */ 484 1.1 christos static int do_decode(unsigned char *bytes, long nbytes, 485 1.1 christos const EXPECTED *expected, size_t expected_size, 486 1.1 christos const TEST_PACKAGE *package) 487 1.1 christos { 488 1.1 christos EXPECTED *enctst = NULL; 489 1.1 christos const unsigned char *start; 490 1.1 christos int ret = 0; 491 1.1 christos 492 1.1 christos start = bytes; 493 1.1 christos enctst = package->d2i(NULL, &bytes, nbytes); 494 1.1 christos if (enctst == NULL) { 495 1.1 christos if (expected->success == 0) { 496 1.1 christos ret = 1; 497 1.1 christos ERR_clear_error(); 498 1.1 christos } else { 499 1.1 christos ret = -1; 500 1.1 christos } 501 1.1 christos } else { 502 1.1 christos if (start + nbytes == bytes 503 1.1 christos && memcmp(enctst, expected, expected_size) == 0) 504 1.1 christos ret = 1; 505 1.1 christos else 506 1.1 christos ret = 0; 507 1.1 christos } 508 1.1 christos 509 1.1 christos package->ifree(enctst); 510 1.1 christos return ret; 511 1.1 christos } 512 1.1 christos 513 1.1 christos /* 514 1.1 christos * do_encode returns a tristate: 515 1.1 christos * 516 1.1 christos * -1 Couldn't encode 517 1.1 christos * 0 encoded DER wasn't what was expected (failure) 518 1.1 christos * 1 encoded DER was what was expected (success) 519 1.1 christos */ 520 1.1 christos static int do_encode(EXPECTED *input, 521 1.1 christos const unsigned char *expected, size_t expected_len, 522 1.1 christos const TEST_PACKAGE *package) 523 1.1 christos { 524 1.1 christos unsigned char *data = NULL; 525 1.1 christos int len; 526 1.1 christos int ret = 0; 527 1.1 christos 528 1.1 christos len = package->i2d(input, &data); 529 1.1 christos if (len < 0) 530 1.1 christos return -1; 531 1.1 christos 532 1.1 christos if ((size_t)len != expected_len 533 1.1 christos || memcmp(data, expected, expected_len) != 0) { 534 1.1 christos if (input->success == 0) { 535 1.1 christos ret = 1; 536 1.1 christos ERR_clear_error(); 537 1.1 christos } else { 538 1.1 christos ret = 0; 539 1.1 christos } 540 1.1 christos } else { 541 1.1 christos ret = 1; 542 1.1 christos } 543 1.1 christos 544 1.1 christos OPENSSL_free(data); 545 1.1 christos return ret; 546 1.1 christos } 547 1.1 christos 548 1.1 christos /* Do an encode/decode round trip */ 549 1.1 christos static int do_enc_dec(EXPECTED *bytes, long nbytes, 550 1.1 christos const TEST_PACKAGE *package) 551 1.1 christos { 552 1.1 christos unsigned char *data = NULL; 553 1.1 christos int len; 554 1.1 christos int ret = 0; 555 1.1 christos void *p = bytes; 556 1.1 christos 557 1.1 christos len = package->i2d(p, &data); 558 1.1 christos if (len < 0) 559 1.1 christos return -1; 560 1.1 christos 561 1.1 christos ret = do_decode(data, len, bytes, nbytes, package); 562 1.1 christos OPENSSL_free(data); 563 1.1 christos return ret; 564 1.1 christos } 565 1.1 christos 566 1.1 christos static size_t der_encode_length(size_t len, unsigned char **pp) 567 1.1 christos { 568 1.1 christos size_t lenbytes; 569 1.1 christos 570 1.1 christos OPENSSL_assert(len < 0x8000); 571 1.1 christos if (len > 255) 572 1.1 christos lenbytes = 3; 573 1.1 christos else if (len > 127) 574 1.1 christos lenbytes = 2; 575 1.1 christos else 576 1.1 christos lenbytes = 1; 577 1.1 christos 578 1.1 christos if (pp != NULL) { 579 1.1 christos if (lenbytes == 1) { 580 1.1 christos *(*pp)++ = (unsigned char)len; 581 1.1 christos } else { 582 1.1 christos *(*pp)++ = (unsigned char)(lenbytes - 1); 583 1.1 christos if (lenbytes == 2) { 584 1.1 christos *(*pp)++ = (unsigned char)(0x80 | len); 585 1.1 christos } else { 586 1.1 christos *(*pp)++ = (unsigned char)(0x80 | (len >> 8)); 587 1.1 christos *(*pp)++ = (unsigned char)(len); 588 1.1 christos } 589 1.1 christos } 590 1.1 christos } 591 1.1 christos return lenbytes; 592 1.1 christos } 593 1.1 christos 594 1.1 christos static size_t make_custom_der(const TEST_CUSTOM_DATA *custom_data, 595 1.1 christos unsigned char **encoding, int explicit_default) 596 1.1 christos { 597 1.1 christos size_t firstbytes, secondbytes = 0, secondbytesinner = 0, seqbytes; 598 1.1 christos const unsigned char t_true[] = { V_ASN1_BOOLEAN, 0x01, 0xff }; 599 1.1 christos unsigned char *p = NULL; 600 1.1 christos size_t i; 601 1.1 christos 602 1.1 christos /* 603 1.1 christos * The first item is just an INTEGER tag, INTEGER length and INTEGER content 604 1.1 christos */ 605 1.1 christos firstbytes = 606 1.1 christos 1 + der_encode_length(custom_data->nbytes1, NULL) 607 1.1 christos + custom_data->nbytes1; 608 1.1 christos 609 1.1 christos for (i = custom_data->nbytes2; i > 0; i--) { 610 1.1 christos if (custom_data->bytes2[i - 1] != '\0') 611 1.1 christos break; 612 1.1 christos } 613 1.1 christos if (explicit_default || i > 0) { 614 1.1 christos /* 615 1.1 christos * The second item is an explicit tag, content length, INTEGER tag, 616 1.1 christos * INTEGER length, INTEGER bytes 617 1.1 christos */ 618 1.1 christos secondbytesinner = 619 1.1 christos 1 + der_encode_length(custom_data->nbytes2, NULL) 620 1.1 christos + custom_data->nbytes2; 621 1.1 christos secondbytes = 622 1.1 christos 1 + der_encode_length(secondbytesinner, NULL) + secondbytesinner; 623 1.1 christos } 624 1.1 christos 625 1.1 christos /* 626 1.1 christos * The whole sequence is the sequence tag, content length, BOOLEAN true 627 1.1 christos * (copied from t_true), the first (firstbytes) and second (secondbytes) 628 1.1 christos * items 629 1.1 christos */ 630 1.1 christos seqbytes = 631 1.1 christos 1 + der_encode_length(sizeof(t_true) + firstbytes + secondbytes, NULL) 632 1.1 christos + sizeof(t_true) + firstbytes + secondbytes; 633 1.1 christos 634 1.1 christos *encoding = p = OPENSSL_malloc(seqbytes); 635 1.1 christos if (*encoding == NULL) 636 1.1 christos return 0; 637 1.1 christos 638 1.1 christos /* Sequence tag */ 639 1.1 christos *p++ = 0x30; 640 1.1 christos der_encode_length(sizeof(t_true) + firstbytes + secondbytes, &p); 641 1.1 christos 642 1.1 christos /* ASN1_BOOLEAN TRUE */ 643 1.1 christos memcpy(p, t_true, sizeof(t_true)); /* Marks decoding success */ 644 1.1 christos p += sizeof(t_true); 645 1.1 christos 646 1.1 christos /* First INTEGER item (non-optional) */ 647 1.1 christos *p++ = V_ASN1_INTEGER; 648 1.1 christos der_encode_length(custom_data->nbytes1, &p); 649 1.1 christos memcpy(p, custom_data->bytes1, custom_data->nbytes1); 650 1.1 christos p += custom_data->nbytes1; 651 1.1 christos 652 1.1 christos if (secondbytes > 0) { 653 1.1 christos /* Second INTEGER item (optional) */ 654 1.1 christos /* Start with the explicit optional tag */ 655 1.1 christos *p++ = 0xa0; 656 1.1 christos der_encode_length(secondbytesinner, &p); 657 1.1 christos *p++ = V_ASN1_INTEGER; 658 1.1 christos der_encode_length(custom_data->nbytes2, &p); 659 1.1 christos memcpy(p, custom_data->bytes2, custom_data->nbytes2); 660 1.1 christos p += custom_data->nbytes2; 661 1.1 christos } 662 1.1 christos 663 1.1 christos OPENSSL_assert(seqbytes == (size_t)(p - *encoding)); 664 1.1 christos 665 1.1 christos return seqbytes; 666 1.1 christos } 667 1.1 christos 668 1.1 christos /* Attempt to decode a custom encoding of the test structure */ 669 1.1 christos static int do_decode_custom(const TEST_CUSTOM_DATA *custom_data, 670 1.1 christos const EXPECTED *expected, size_t expected_size, 671 1.1 christos const TEST_PACKAGE *package) 672 1.1 christos { 673 1.1 christos unsigned char *encoding = NULL; 674 1.1 christos /* 675 1.1 christos * We force the defaults to be explicitly encoded to make sure we test 676 1.1 christos * for defaults that shouldn't be present (i.e. we check for failure) 677 1.1 christos */ 678 1.1 christos size_t encoding_length = make_custom_der(custom_data, &encoding, 1); 679 1.1 christos int ret; 680 1.1 christos 681 1.1 christos if (encoding_length == 0) 682 1.1 christos return -1; 683 1.1 christos 684 1.1 christos ret = do_decode(encoding, encoding_length, expected, expected_size, 685 1.1 christos package); 686 1.1 christos OPENSSL_free(encoding); 687 1.1 christos 688 1.1 christos return ret; 689 1.1 christos } 690 1.1 christos 691 1.1 christos /* Attempt to encode the test structure and compare it to custom DER */ 692 1.1 christos static int do_encode_custom(EXPECTED *input, 693 1.1 christos const TEST_CUSTOM_DATA *custom_data, 694 1.1 christos const TEST_PACKAGE *package) 695 1.1 christos { 696 1.1 christos unsigned char *expected = NULL; 697 1.1 christos size_t expected_length = make_custom_der(custom_data, &expected, 0); 698 1.1 christos int ret; 699 1.1 christos 700 1.1 christos if (expected_length == 0) 701 1.1 christos return -1; 702 1.1 christos 703 1.1 christos ret = do_encode(input, expected, expected_length, package); 704 1.1 christos OPENSSL_free(expected); 705 1.1 christos 706 1.1 christos return ret; 707 1.1 christos } 708 1.1 christos 709 1.1 christos static int do_print_item(const TEST_PACKAGE *package) 710 1.1 christos { 711 1.1 christos #define DATA_BUF_SIZE 256 712 1.1 christos const ASN1_ITEM *i = ASN1_ITEM_ptr(package->asn1_type); 713 1.1 christos ASN1_VALUE *o; 714 1.1 christos int ret; 715 1.1 christos 716 1.1 christos OPENSSL_assert(package->encode_expectations_elem_size <= DATA_BUF_SIZE); 717 1.1 christos if ((o = OPENSSL_malloc(DATA_BUF_SIZE)) == NULL) 718 1.1 christos return 0; 719 1.1 christos 720 1.1 christos (void)RAND_bytes((unsigned char*)o, 721 1.1 christos (int)package->encode_expectations_elem_size); 722 1.1 christos ret = ASN1_item_print(bio_err, o, 0, i, NULL); 723 1.1 christos OPENSSL_free(o); 724 1.1 christos 725 1.1 christos return ret; 726 1.1 christos } 727 1.1 christos 728 1.1 christos 729 1.1 christos static int test_intern(const TEST_PACKAGE *package) 730 1.1 christos { 731 1.1 christos unsigned int i; 732 1.1 christos size_t nelems; 733 1.1 christos int fail = 0; 734 1.1 christos 735 1.1 christos if (package->skip) 736 1.1 christos return 1; 737 1.1 christos 738 1.1 christos /* Do decode_custom checks */ 739 1.1 christos nelems = package->encode_expectations_size 740 1.1 christos / package->encode_expectations_elem_size; 741 1.1 christos OPENSSL_assert(nelems == 742 1.1 christos sizeof(test_custom_data) / sizeof(test_custom_data[0])); 743 1.1 christos for (i = 0; i < nelems; i++) { 744 1.1 christos size_t pos = i * package->encode_expectations_elem_size; 745 1.1 christos switch (do_encode_custom((EXPECTED *)&((unsigned char *)package 746 1.1 christos ->encode_expectations)[pos], 747 1.1 christos &test_custom_data[i], package)) { 748 1.1 christos case -1: 749 1.1 christos TEST_error("Failed custom encode round trip %u of %s", 750 1.1 christos i, package->name); 751 1.1 christos TEST_openssl_errors(); 752 1.1 christos fail++; 753 1.1 christos break; 754 1.1 christos case 0: 755 1.1 christos TEST_error("Custom encode round trip %u of %s mismatch", 756 1.1 christos i, package->name); 757 1.1 christos TEST_openssl_errors(); 758 1.1 christos fail++; 759 1.1 christos break; 760 1.1 christos case 1: 761 1.1 christos break; 762 1.1 christos default: 763 1.1 christos OPENSSL_die("do_encode_custom() return unknown value", 764 1.1 christos __FILE__, __LINE__); 765 1.1 christos } 766 1.1 christos switch (do_decode_custom(&test_custom_data[i], 767 1.1 christos (EXPECTED *)&((unsigned char *)package 768 1.1 christos ->encode_expectations)[pos], 769 1.1 christos package->encode_expectations_elem_size, 770 1.1 christos package)) { 771 1.1 christos case -1: 772 1.1 christos TEST_error("Failed custom decode round trip %u of %s", 773 1.1 christos i, package->name); 774 1.1 christos TEST_openssl_errors(); 775 1.1 christos fail++; 776 1.1 christos break; 777 1.1 christos case 0: 778 1.1 christos TEST_error("Custom decode round trip %u of %s mismatch", 779 1.1 christos i, package->name); 780 1.1 christos TEST_openssl_errors(); 781 1.1 christos fail++; 782 1.1 christos break; 783 1.1 christos case 1: 784 1.1 christos break; 785 1.1 christos default: 786 1.1 christos OPENSSL_die("do_decode_custom() return unknown value", 787 1.1 christos __FILE__, __LINE__); 788 1.1 christos } 789 1.1 christos } 790 1.1 christos 791 1.1 christos /* Do enc_dec checks */ 792 1.1 christos nelems = package->encdec_data_size / package->encdec_data_elem_size; 793 1.1 christos for (i = 0; i < nelems; i++) { 794 1.1 christos size_t pos = i * package->encdec_data_elem_size; 795 1.1 christos switch (do_enc_dec((EXPECTED *)&((unsigned char *)package 796 1.1 christos ->encdec_data)[pos], 797 1.1 christos package->encdec_data_elem_size, 798 1.1 christos package)) { 799 1.1 christos case -1: 800 1.1 christos TEST_error("Failed encode/decode round trip %u of %s", 801 1.1 christos i, package->name); 802 1.1 christos TEST_openssl_errors(); 803 1.1 christos fail++; 804 1.1 christos break; 805 1.1 christos case 0: 806 1.1 christos TEST_error("Encode/decode round trip %u of %s mismatch", 807 1.1 christos i, package->name); 808 1.1 christos fail++; 809 1.1 christos break; 810 1.1 christos case 1: 811 1.1 christos break; 812 1.1 christos default: 813 1.1 christos OPENSSL_die("do_enc_dec() return unknown value", 814 1.1 christos __FILE__, __LINE__); 815 1.1 christos } 816 1.1 christos } 817 1.1 christos 818 1.1 christos if (!do_print_item(package)) { 819 1.1 christos TEST_error("Printing of %s failed", package->name); 820 1.1 christos TEST_openssl_errors(); 821 1.1 christos fail++; 822 1.1 christos } 823 1.1 christos 824 1.1 christos return fail == 0; 825 1.1 christos } 826 1.1 christos 827 1.1 christos #if OPENSSL_API_COMPAT < 0x10200000L 828 1.1 christos static int test_long_32bit(void) 829 1.1 christos { 830 1.1 christos return test_intern(&long_test_package_32bit); 831 1.1 christos } 832 1.1 christos 833 1.1 christos static int test_long_64bit(void) 834 1.1 christos { 835 1.1 christos return test_intern(&long_test_package_64bit); 836 1.1 christos } 837 1.1 christos #endif 838 1.1 christos 839 1.1 christos static int test_int32(void) 840 1.1 christos { 841 1.1 christos return test_intern(&int32_test_package); 842 1.1 christos } 843 1.1 christos 844 1.1 christos static int test_uint32(void) 845 1.1 christos { 846 1.1 christos return test_intern(&uint32_test_package); 847 1.1 christos } 848 1.1 christos 849 1.1 christos static int test_int64(void) 850 1.1 christos { 851 1.1 christos return test_intern(&int64_test_package); 852 1.1 christos } 853 1.1 christos 854 1.1 christos static int test_uint64(void) 855 1.1 christos { 856 1.1 christos return test_intern(&uint64_test_package); 857 1.1 christos } 858 1.1 christos 859 1.1 christos typedef struct { 860 1.1 christos ASN1_STRING *invalidDirString; 861 1.1 christos } INVALIDTEMPLATE; 862 1.1 christos 863 1.1 christos ASN1_SEQUENCE(INVALIDTEMPLATE) = { 864 1.1 christos /* 865 1.1 christos * DirectoryString is a CHOICE type so it must use explicit tagging - 866 1.1 christos * but we deliberately use implicit here, which makes this template invalid. 867 1.1 christos */ 868 1.1 christos ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12) 869 1.1 christos } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE) 870 1.1 christos 871 1.1 christos IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE) 872 1.1 christos IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE) 873 1.1 christos 874 1.1 christos static int test_invalid_template(void) 875 1.1 christos { 876 1.1 christos INVALIDTEMPLATE *temp = INVALIDTEMPLATE_new(); 877 1.1 christos int ret; 878 1.1 christos 879 1.1 christos if (!TEST_ptr(temp)) 880 1.1 christos return 0; 881 1.1 christos 882 1.1 christos ret = i2d_INVALIDTEMPLATE(temp, NULL); 883 1.1 christos 884 1.1 christos INVALIDTEMPLATE_free(temp); 885 1.1 christos 886 1.1 christos /* We expect the i2d operation to fail */ 887 1.1 christos return ret < 0; 888 1.1 christos } 889 1.1 christos 890 1.1 christos 891 1.1 christos int setup_tests(void) 892 1.1 christos { 893 1.1 christos #if OPENSSL_API_COMPAT < 0x10200000L 894 1.1 christos ADD_TEST(test_long_32bit); 895 1.1 christos ADD_TEST(test_long_64bit); 896 1.1 christos #endif 897 1.1 christos ADD_TEST(test_int32); 898 1.1 christos ADD_TEST(test_uint32); 899 1.1 christos ADD_TEST(test_int64); 900 1.1 christos ADD_TEST(test_uint64); 901 1.1 christos ADD_TEST(test_invalid_template); 902 1.1 christos return 1; 903 1.1 christos } 904