| 1 |
/* |
| 2 |
* Copyright (c) 1999 - 2007 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 2009 Apple Inc. All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* |
| 12 |
* 1. Redistributions of source code must retain the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer. |
| 14 |
* |
| 15 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 16 |
* notice, this list of conditions and the following disclaimer in the |
| 17 |
* documentation and/or other materials provided with the distribution. |
| 18 |
* |
| 19 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 20 |
* may be used to endorse or promote products derived from this software |
| 21 |
* without specific prior written permission. |
| 22 |
* |
| 23 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 29 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 30 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 32 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 |
* SUCH DAMAGE. |
| 34 |
*/ |
| 35 |
|
| 36 |
#include "der_locl.h" |
| 37 |
#include <err.h> |
| 38 |
#include <roken.h> |
| 39 |
|
| 40 |
#include <asn1-common.h> |
| 41 |
#include <asn1_err.h> |
| 42 |
#include <der.h> |
| 43 |
|
| 44 |
#include "check-common.h" |
| 45 |
|
| 46 |
RCSID("$Id$"); |
| 47 |
|
| 48 |
static int |
| 49 |
cmp_integer (void *a, void *b) |
| 50 |
{ |
| 51 |
int *ia = (int *)a; |
| 52 |
int *ib = (int *)b; |
| 53 |
|
| 54 |
return *ib - *ia; |
| 55 |
} |
| 56 |
|
| 57 |
static int |
| 58 |
test_integer (void) |
| 59 |
{ |
| 60 |
struct test_case tests[] = { |
| 61 |
{NULL, 1, "\x00", NULL }, |
| 62 |
{NULL, 1, "\x7f", NULL }, |
| 63 |
{NULL, 2, "\x00\x80", NULL }, |
| 64 |
{NULL, 2, "\x01\x00", NULL }, |
| 65 |
{NULL, 1, "\x80", NULL }, |
| 66 |
{NULL, 2, "\xff\x7f", NULL }, |
| 67 |
{NULL, 1, "\xff", NULL }, |
| 68 |
{NULL, 2, "\xff\x01", NULL }, |
| 69 |
{NULL, 2, "\x00\xff", NULL }, |
| 70 |
{NULL, 2, "\xfe\x01", NULL }, |
| 71 |
{NULL, 4, "\x7f\xff\xff\xff", NULL } |
| 72 |
}; |
| 73 |
|
| 74 |
int values[] = {0, 127, 128, 256, -128, -129, -1, -255, 255, |
| 75 |
-511, 0x7fffffff}; |
| 76 |
int i, ret; |
| 77 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 78 |
|
| 79 |
for (i = 0; i < ntests; ++i) { |
| 80 |
tests[i].val = &values[i]; |
| 81 |
if (asprintf (&tests[i].name, "integer %d", values[i]) < 0) |
| 82 |
errx(1, "malloc"); |
| 83 |
if (tests[i].name == NULL) |
| 84 |
errx(1, "malloc"); |
| 85 |
} |
| 86 |
|
| 87 |
ret = generic_test (tests, ntests, sizeof(int), |
| 88 |
(generic_encode)der_put_integer, |
| 89 |
(generic_length) der_length_integer, |
| 90 |
(generic_decode)der_get_integer, |
| 91 |
(generic_free)NULL, |
| 92 |
cmp_integer, |
| 93 |
NULL); |
| 94 |
|
| 95 |
for (i = 0; i < ntests; ++i) |
| 96 |
free (tests[i].name); |
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
static int |
| 101 |
test_one_int(int val) |
| 102 |
{ |
| 103 |
int ret, dval; |
| 104 |
unsigned char *buf; |
| 105 |
size_t len_len, len; |
| 106 |
|
| 107 |
len = _heim_len_int(val); |
| 108 |
|
| 109 |
buf = emalloc(len + 2); |
| 110 |
|
| 111 |
buf[0] = '\xff'; |
| 112 |
buf[len + 1] = '\xff'; |
| 113 |
memset(buf + 1, 0, len); |
| 114 |
|
| 115 |
ret = der_put_integer(buf + 1 + len - 1, len, &val, &len_len); |
| 116 |
if (ret) { |
| 117 |
printf("integer %d encode failed %d\n", val, ret); |
| 118 |
return 1; |
| 119 |
} |
| 120 |
if (len != len_len) { |
| 121 |
printf("integer %d encode fail with %d len %lu, result len %lu\n", |
| 122 |
val, ret, (unsigned long)len, (unsigned long)len_len); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
ret = der_get_integer(buf + 1, len, &dval, &len_len); |
| 127 |
if (ret) { |
| 128 |
printf("integer %d decode failed %d\n", val, ret); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
if (len != len_len) { |
| 132 |
printf("integer %d decoded diffrent len %lu != %lu", |
| 133 |
val, (unsigned long)len, (unsigned long)len_len); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
if (val != dval) { |
| 137 |
printf("decode decoded to diffrent value %d != %d", |
| 138 |
val, dval); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
if (buf[0] != (unsigned char)'\xff') { |
| 143 |
printf("precanary dead %d\n", val); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
if (buf[len + 1] != (unsigned char)'\xff') { |
| 147 |
printf("postecanary dead %d\n", val); |
| 148 |
return 1; |
| 149 |
} |
| 150 |
free(buf); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
static int |
| 155 |
test_integer_more (void) |
| 156 |
{ |
| 157 |
int64_t i, n1, n2, n3, n4, n5, n6; |
| 158 |
|
| 159 |
n2 = 0; |
| 160 |
for (i = 0; i < (sizeof(int) * 8); i++) { |
| 161 |
n1 = 0x01LL << i; |
| 162 |
n2 = n2 | n1; |
| 163 |
n3 = ~n1; |
| 164 |
n4 = ~n2; |
| 165 |
n5 = (-1LL) & ~(0x3fLL << i); |
| 166 |
n6 = (-1LL) & ~(0x7fLL << i); |
| 167 |
|
| 168 |
test_one_int(n1); |
| 169 |
test_one_int(n2); |
| 170 |
test_one_int(n3); |
| 171 |
test_one_int(n4); |
| 172 |
test_one_int(n5); |
| 173 |
test_one_int(n6); |
| 174 |
} |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
static int |
| 179 |
cmp_unsigned (void *a, void *b) |
| 180 |
{ |
| 181 |
return *(unsigned int*)b - *(unsigned int*)a; |
| 182 |
} |
| 183 |
|
| 184 |
static int |
| 185 |
test_unsigned (void) |
| 186 |
{ |
| 187 |
struct test_case tests[] = { |
| 188 |
{NULL, 1, "\x00", NULL }, |
| 189 |
{NULL, 1, "\x7f", NULL }, |
| 190 |
{NULL, 2, "\x00\x80", NULL }, |
| 191 |
{NULL, 2, "\x01\x00", NULL }, |
| 192 |
{NULL, 2, "\x02\x00", NULL }, |
| 193 |
{NULL, 3, "\x00\x80\x00", NULL }, |
| 194 |
{NULL, 5, "\x00\x80\x00\x00\x00", NULL }, |
| 195 |
{NULL, 4, "\x7f\xff\xff\xff", NULL } |
| 196 |
}; |
| 197 |
|
| 198 |
unsigned int values[] = {0, 127, 128, 256, 512, 32768, |
| 199 |
0x80000000, 0x7fffffff}; |
| 200 |
int i, ret; |
| 201 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 202 |
|
| 203 |
for (i = 0; i < ntests; ++i) { |
| 204 |
tests[i].val = &values[i]; |
| 205 |
if (asprintf (&tests[i].name, "unsigned %u", values[i]) < 0) |
| 206 |
errx(1, "malloc"); |
| 207 |
if (tests[i].name == NULL) |
| 208 |
errx(1, "malloc"); |
| 209 |
} |
| 210 |
|
| 211 |
ret = generic_test (tests, ntests, sizeof(int), |
| 212 |
(generic_encode)der_put_unsigned, |
| 213 |
(generic_length)der_length_unsigned, |
| 214 |
(generic_decode)der_get_unsigned, |
| 215 |
(generic_free)NULL, |
| 216 |
cmp_unsigned, |
| 217 |
NULL); |
| 218 |
for (i = 0; i < ntests; ++i) |
| 219 |
free (tests[i].name); |
| 220 |
return ret; |
| 221 |
} |
| 222 |
|
| 223 |
static int |
| 224 |
cmp_octet_string (void *a, void *b) |
| 225 |
{ |
| 226 |
return der_heim_octet_string_cmp(a, b); |
| 227 |
} |
| 228 |
|
| 229 |
static int |
| 230 |
test_octet_string (void) |
| 231 |
{ |
| 232 |
heim_octet_string s1 = {8, "\x01\x23\x45\x67\x89\xab\xcd\xef"}; |
| 233 |
|
| 234 |
struct test_case tests[] = { |
| 235 |
{NULL, 8, "\x01\x23\x45\x67\x89\xab\xcd\xef", NULL } |
| 236 |
}; |
| 237 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 238 |
int ret; |
| 239 |
|
| 240 |
tests[0].val = &s1; |
| 241 |
if (asprintf (&tests[0].name, "a octet string") < 0) |
| 242 |
errx(1, "malloc"); |
| 243 |
if (tests[0].name == NULL) |
| 244 |
errx(1, "malloc"); |
| 245 |
|
| 246 |
ret = generic_test (tests, ntests, sizeof(heim_octet_string), |
| 247 |
(generic_encode)der_put_octet_string, |
| 248 |
(generic_length)der_length_octet_string, |
| 249 |
(generic_decode)der_get_octet_string, |
| 250 |
(generic_free)der_free_octet_string, |
| 251 |
cmp_octet_string, |
| 252 |
NULL); |
| 253 |
free(tests[0].name); |
| 254 |
return ret; |
| 255 |
} |
| 256 |
|
| 257 |
static int |
| 258 |
cmp_bmp_string (void *a, void *b) |
| 259 |
{ |
| 260 |
heim_bmp_string *oa = (heim_bmp_string *)a; |
| 261 |
heim_bmp_string *ob = (heim_bmp_string *)b; |
| 262 |
|
| 263 |
return der_heim_bmp_string_cmp(oa, ob); |
| 264 |
} |
| 265 |
|
| 266 |
static uint16_t bmp_d1[] = { 32 }; |
| 267 |
static uint16_t bmp_d2[] = { 32, 32 }; |
| 268 |
|
| 269 |
static int |
| 270 |
test_bmp_string (void) |
| 271 |
{ |
| 272 |
heim_bmp_string s1 = { 1, bmp_d1 }; |
| 273 |
heim_bmp_string s2 = { 2, bmp_d2 }; |
| 274 |
|
| 275 |
struct test_case tests[] = { |
| 276 |
{NULL, 2, "\x00\x20", NULL }, |
| 277 |
{NULL, 4, "\x00\x20\x00\x20", NULL } |
| 278 |
}; |
| 279 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 280 |
int ret; |
| 281 |
|
| 282 |
tests[0].val = &s1; |
| 283 |
if (asprintf (&tests[0].name, "a bmp string") < 0) |
| 284 |
errx(1, "malloc"); |
| 285 |
if (tests[0].name == NULL) |
| 286 |
errx(1, "malloc"); |
| 287 |
tests[1].val = &s2; |
| 288 |
if (asprintf (&tests[1].name, "second bmp string") < 0) |
| 289 |
errx(1, "malloc"); |
| 290 |
if (tests[1].name == NULL) |
| 291 |
errx(1, "malloc"); |
| 292 |
|
| 293 |
ret = generic_test (tests, ntests, sizeof(heim_bmp_string), |
| 294 |
(generic_encode)der_put_bmp_string, |
| 295 |
(generic_length)der_length_bmp_string, |
| 296 |
(generic_decode)der_get_bmp_string, |
| 297 |
(generic_free)der_free_bmp_string, |
| 298 |
cmp_bmp_string, |
| 299 |
NULL); |
| 300 |
free(tests[0].name); |
| 301 |
free(tests[1].name); |
| 302 |
return ret; |
| 303 |
} |
| 304 |
|
| 305 |
static int |
| 306 |
cmp_universal_string (void *a, void *b) |
| 307 |
{ |
| 308 |
heim_universal_string *oa = (heim_universal_string *)a; |
| 309 |
heim_universal_string *ob = (heim_universal_string *)b; |
| 310 |
|
| 311 |
return der_heim_universal_string_cmp(oa, ob); |
| 312 |
} |
| 313 |
|
| 314 |
static uint32_t universal_d1[] = { 32 }; |
| 315 |
static uint32_t universal_d2[] = { 32, 32 }; |
| 316 |
|
| 317 |
static int |
| 318 |
test_universal_string (void) |
| 319 |
{ |
| 320 |
heim_universal_string s1 = { 1, universal_d1 }; |
| 321 |
heim_universal_string s2 = { 2, universal_d2 }; |
| 322 |
|
| 323 |
struct test_case tests[] = { |
| 324 |
{NULL, 4, "\x00\x00\x00\x20", NULL }, |
| 325 |
{NULL, 8, "\x00\x00\x00\x20\x00\x00\x00\x20", NULL } |
| 326 |
}; |
| 327 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 328 |
int ret; |
| 329 |
|
| 330 |
tests[0].val = &s1; |
| 331 |
if (asprintf (&tests[0].name, "a universal string") < 0) |
| 332 |
errx(1, "malloc"); |
| 333 |
if (tests[0].name == NULL) |
| 334 |
errx(1, "malloc"); |
| 335 |
tests[1].val = &s2; |
| 336 |
if (asprintf (&tests[1].name, "second universal string") < 0) |
| 337 |
errx(1, "malloc"); |
| 338 |
if (tests[1].name == NULL) |
| 339 |
errx(1, "malloc"); |
| 340 |
|
| 341 |
ret = generic_test (tests, ntests, sizeof(heim_universal_string), |
| 342 |
(generic_encode)der_put_universal_string, |
| 343 |
(generic_length)der_length_universal_string, |
| 344 |
(generic_decode)der_get_universal_string, |
| 345 |
(generic_free)der_free_universal_string, |
| 346 |
cmp_universal_string, |
| 347 |
NULL); |
| 348 |
free(tests[0].name); |
| 349 |
free(tests[1].name); |
| 350 |
return ret; |
| 351 |
} |
| 352 |
|
| 353 |
static int |
| 354 |
cmp_general_string (void *a, void *b) |
| 355 |
{ |
| 356 |
char **sa = (char **)a; |
| 357 |
char **sb = (char **)b; |
| 358 |
|
| 359 |
return strcmp (*sa, *sb); |
| 360 |
} |
| 361 |
|
| 362 |
static int |
| 363 |
test_general_string (void) |
| 364 |
{ |
| 365 |
char *s1 = "Test User 1"; |
| 366 |
|
| 367 |
struct test_case tests[] = { |
| 368 |
{NULL, 11, "\x54\x65\x73\x74\x20\x55\x73\x65\x72\x20\x31", NULL } |
| 369 |
}; |
| 370 |
int ret, ntests = sizeof(tests) / sizeof(*tests); |
| 371 |
|
| 372 |
tests[0].val = &s1; |
| 373 |
if (asprintf (&tests[0].name, "the string \"%s\"", s1) < 0) |
| 374 |
errx(1, "malloc"); |
| 375 |
if (tests[0].name == NULL) |
| 376 |
errx(1, "malloc"); |
| 377 |
|
| 378 |
ret = generic_test (tests, ntests, sizeof(unsigned char *), |
| 379 |
(generic_encode)der_put_general_string, |
| 380 |
(generic_length)der_length_general_string, |
| 381 |
(generic_decode)der_get_general_string, |
| 382 |
(generic_free)der_free_general_string, |
| 383 |
cmp_general_string, |
| 384 |
NULL); |
| 385 |
free(tests[0].name); |
| 386 |
return ret; |
| 387 |
} |
| 388 |
|
| 389 |
static int |
| 390 |
cmp_generalized_time (void *a, void *b) |
| 391 |
{ |
| 392 |
time_t *ta = (time_t *)a; |
| 393 |
time_t *tb = (time_t *)b; |
| 394 |
|
| 395 |
return (int)(*tb - *ta); |
| 396 |
} |
| 397 |
|
| 398 |
static int |
| 399 |
test_generalized_time (void) |
| 400 |
{ |
| 401 |
struct test_case tests[] = { |
| 402 |
{NULL, 15, "19700101000000Z", NULL }, |
| 403 |
{NULL, 15, "19851106210627Z", NULL } |
| 404 |
}; |
| 405 |
time_t values[] = {0, 500159187}; |
| 406 |
int i, ret; |
| 407 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 408 |
|
| 409 |
for (i = 0; i < ntests; ++i) { |
| 410 |
tests[i].val = &values[i]; |
| 411 |
if (asprintf (&tests[i].name, "time %d", (int)values[i]) < 0) |
| 412 |
errx(1, "malloc"); |
| 413 |
if (tests[i].name == NULL) |
| 414 |
errx(1, "malloc"); |
| 415 |
} |
| 416 |
|
| 417 |
ret = generic_test (tests, ntests, sizeof(time_t), |
| 418 |
(generic_encode)der_put_generalized_time, |
| 419 |
(generic_length)der_length_generalized_time, |
| 420 |
(generic_decode)der_get_generalized_time, |
| 421 |
(generic_free)NULL, |
| 422 |
cmp_generalized_time, |
| 423 |
NULL); |
| 424 |
for (i = 0; i < ntests; ++i) |
| 425 |
free(tests[i].name); |
| 426 |
return ret; |
| 427 |
} |
| 428 |
|
| 429 |
static int |
| 430 |
test_cmp_oid (void *a, void *b) |
| 431 |
{ |
| 432 |
return der_heim_oid_cmp((heim_oid *)a, (heim_oid *)b); |
| 433 |
} |
| 434 |
|
| 435 |
static unsigned oid_comp1[] = { 1, 1, 1 }; |
| 436 |
static unsigned oid_comp2[] = { 1, 1 }; |
| 437 |
static unsigned oid_comp3[] = { 6, 15, 1 }; |
| 438 |
static unsigned oid_comp4[] = { 6, 15 }; |
| 439 |
|
| 440 |
static int |
| 441 |
test_oid (void) |
| 442 |
{ |
| 443 |
struct test_case tests[] = { |
| 444 |
{NULL, 2, "\x29\x01", NULL }, |
| 445 |
{NULL, 1, "\x29", NULL }, |
| 446 |
{NULL, 2, "\xff\x01", NULL }, |
| 447 |
{NULL, 1, "\xff", NULL } |
| 448 |
}; |
| 449 |
heim_oid values[] = { |
| 450 |
{ 3, oid_comp1 }, |
| 451 |
{ 2, oid_comp2 }, |
| 452 |
{ 3, oid_comp3 }, |
| 453 |
{ 2, oid_comp4 } |
| 454 |
}; |
| 455 |
int i, ret; |
| 456 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 457 |
|
| 458 |
for (i = 0; i < ntests; ++i) { |
| 459 |
tests[i].val = &values[i]; |
| 460 |
if (asprintf (&tests[i].name, "oid %d", i) < 0) |
| 461 |
errx(1, "malloc"); |
| 462 |
if (tests[i].name == NULL) |
| 463 |
errx(1, "malloc"); |
| 464 |
} |
| 465 |
|
| 466 |
ret = generic_test (tests, ntests, sizeof(heim_oid), |
| 467 |
(generic_encode)der_put_oid, |
| 468 |
(generic_length)der_length_oid, |
| 469 |
(generic_decode)der_get_oid, |
| 470 |
(generic_free)der_free_oid, |
| 471 |
test_cmp_oid, |
| 472 |
NULL); |
| 473 |
for (i = 0; i < ntests; ++i) |
| 474 |
free(tests[i].name); |
| 475 |
return ret; |
| 476 |
} |
| 477 |
|
| 478 |
static int |
| 479 |
test_cmp_bit_string (void *a, void *b) |
| 480 |
{ |
| 481 |
return der_heim_bit_string_cmp((heim_bit_string *)a, (heim_bit_string *)b); |
| 482 |
} |
| 483 |
|
| 484 |
static int |
| 485 |
test_bit_string (void) |
| 486 |
{ |
| 487 |
struct test_case tests[] = { |
| 488 |
{NULL, 1, "\x00", NULL } |
| 489 |
}; |
| 490 |
heim_bit_string values[] = { |
| 491 |
{ 0, "" } |
| 492 |
}; |
| 493 |
int i, ret; |
| 494 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 495 |
|
| 496 |
for (i = 0; i < ntests; ++i) { |
| 497 |
tests[i].val = &values[i]; |
| 498 |
if (asprintf (&tests[i].name, "bit_string %d", i) < 0) |
| 499 |
errx(1, "malloc"); |
| 500 |
if (tests[i].name == NULL) |
| 501 |
errx(1, "malloc"); |
| 502 |
} |
| 503 |
|
| 504 |
ret = generic_test (tests, ntests, sizeof(heim_bit_string), |
| 505 |
(generic_encode)der_put_bit_string, |
| 506 |
(generic_length)der_length_bit_string, |
| 507 |
(generic_decode)der_get_bit_string, |
| 508 |
(generic_free)der_free_bit_string, |
| 509 |
test_cmp_bit_string, |
| 510 |
NULL); |
| 511 |
for (i = 0; i < ntests; ++i) |
| 512 |
free(tests[i].name); |
| 513 |
return ret; |
| 514 |
} |
| 515 |
|
| 516 |
static int |
| 517 |
test_cmp_heim_integer (void *a, void *b) |
| 518 |
{ |
| 519 |
return der_heim_integer_cmp((heim_integer *)a, (heim_integer *)b); |
| 520 |
} |
| 521 |
|
| 522 |
static int |
| 523 |
test_heim_integer (void) |
| 524 |
{ |
| 525 |
struct test_case tests[] = { |
| 526 |
{NULL, 1, "\xff", NULL }, |
| 527 |
{NULL, 2, "\xff\x01", NULL }, |
| 528 |
{NULL, 2, "\xfe\x01", NULL }, |
| 529 |
{NULL, 2, "\xef\x01", NULL }, |
| 530 |
{NULL, 3, "\xff\x00\xff", NULL }, |
| 531 |
{NULL, 3, "\xff\x01\x00", NULL }, |
| 532 |
{NULL, 1, "\x00", NULL }, |
| 533 |
{NULL, 1, "\x01", NULL }, |
| 534 |
{NULL, 2, "\x00\x80", NULL }, |
| 535 |
}; |
| 536 |
|
| 537 |
heim_integer values[] = { |
| 538 |
{ 1, "\x01", 1 }, |
| 539 |
{ 1, "\xff", 1 }, |
| 540 |
{ 2, "\x01\xff", 1 }, |
| 541 |
{ 2, "\x10\xff", 1 }, |
| 542 |
{ 2, "\xff\x01", 1 }, |
| 543 |
{ 2, "\xff\x00", 1 }, |
| 544 |
{ 0, "", 0 }, |
| 545 |
{ 1, "\x01", 0 }, |
| 546 |
{ 1, "\x80", 0 }, |
| 547 |
}; |
| 548 |
int i, ret; |
| 549 |
int ntests = sizeof(tests) / sizeof(tests[0]); |
| 550 |
size_t size; |
| 551 |
heim_integer i2; |
| 552 |
|
| 553 |
for (i = 0; i < ntests; ++i) { |
| 554 |
tests[i].val = &values[i]; |
| 555 |
if (asprintf (&tests[i].name, "heim_integer %d", i) < 0) |
| 556 |
errx(1, "malloc"); |
| 557 |
if (tests[i].name == NULL) |
| 558 |
errx(1, "malloc"); |
| 559 |
} |
| 560 |
|
| 561 |
ret = generic_test (tests, ntests, sizeof(heim_integer), |
| 562 |
(generic_encode)der_put_heim_integer, |
| 563 |
(generic_length)der_length_heim_integer, |
| 564 |
(generic_decode)der_get_heim_integer, |
| 565 |
(generic_free)der_free_heim_integer, |
| 566 |
test_cmp_heim_integer, |
| 567 |
NULL); |
| 568 |
for (i = 0; i < ntests; ++i) |
| 569 |
free (tests[i].name); |
| 570 |
if (ret) |
| 571 |
return ret; |
| 572 |
|
| 573 |
/* test zero length integer (BER format) */ |
| 574 |
ret = der_get_heim_integer(NULL, 0, &i2, &size); |
| 575 |
if (ret) |
| 576 |
errx(1, "der_get_heim_integer"); |
| 577 |
if (i2.length != 0) |
| 578 |
errx(1, "der_get_heim_integer wrong length"); |
| 579 |
der_free_heim_integer(&i2); |
| 580 |
|
| 581 |
return 0; |
| 582 |
} |
| 583 |
|
| 584 |
static int |
| 585 |
test_cmp_boolean (void *a, void *b) |
| 586 |
{ |
| 587 |
return !!*(int *)a != !!*(int *)b; |
| 588 |
} |
| 589 |
|
| 590 |
static int |
| 591 |
test_boolean (void) |
| 592 |
{ |
| 593 |
struct test_case tests[] = { |
| 594 |
{NULL, 1, "\xff", NULL }, |
| 595 |
{NULL, 1, "\x00", NULL } |
| 596 |
}; |
| 597 |
|
| 598 |
int values[] = { 1, 0 }; |
| 599 |
int i, ret; |
| 600 |
int ntests = sizeof(tests) / sizeof(tests[0]); |
| 601 |
size_t size; |
| 602 |
heim_integer i2; |
| 603 |
|
| 604 |
for (i = 0; i < ntests; ++i) { |
| 605 |
tests[i].val = &values[i]; |
| 606 |
if (asprintf (&tests[i].name, "heim_boolean %d", i) < 0) |
| 607 |
errx(1, "malloc"); |
| 608 |
if (tests[i].name == NULL) |
| 609 |
errx(1, "malloc"); |
| 610 |
} |
| 611 |
|
| 612 |
ret = generic_test (tests, ntests, sizeof(int), |
| 613 |
(generic_encode)der_put_boolean, |
| 614 |
(generic_length)der_length_boolean, |
| 615 |
(generic_decode)der_get_boolean, |
| 616 |
(generic_free)NULL, |
| 617 |
test_cmp_boolean, |
| 618 |
NULL); |
| 619 |
for (i = 0; i < ntests; ++i) |
| 620 |
free (tests[i].name); |
| 621 |
if (ret) |
| 622 |
return ret; |
| 623 |
|
| 624 |
/* test zero length integer (BER format) */ |
| 625 |
ret = der_get_heim_integer(NULL, 0, &i2, &size); |
| 626 |
if (ret) |
| 627 |
errx(1, "der_get_heim_integer"); |
| 628 |
if (i2.length != 0) |
| 629 |
errx(1, "der_get_heim_integer wrong length"); |
| 630 |
der_free_heim_integer(&i2); |
| 631 |
|
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
static int |
| 636 |
check_fail_unsigned(void) |
| 637 |
{ |
| 638 |
struct test_case tests[] = { |
| 639 |
{NULL, sizeof(unsigned) + 1, |
| 640 |
"\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" } |
| 641 |
}; |
| 642 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 643 |
|
| 644 |
return generic_decode_fail(tests, ntests, sizeof(unsigned), |
| 645 |
(generic_decode)der_get_unsigned); |
| 646 |
} |
| 647 |
|
| 648 |
static int |
| 649 |
check_fail_integer(void) |
| 650 |
{ |
| 651 |
struct test_case tests[] = { |
| 652 |
{NULL, sizeof(int) + 1, |
| 653 |
"\x01\x01\x01\x01\x01\x01\x01\x01\x01", "data overrun" } |
| 654 |
}; |
| 655 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 656 |
|
| 657 |
return generic_decode_fail(tests, ntests, sizeof(int), |
| 658 |
(generic_decode)der_get_integer); |
| 659 |
} |
| 660 |
|
| 661 |
static int |
| 662 |
check_fail_length(void) |
| 663 |
{ |
| 664 |
struct test_case tests[] = { |
| 665 |
{NULL, 0, "", "empty input data"}, |
| 666 |
{NULL, 1, "\x82", "internal length overrun" } |
| 667 |
}; |
| 668 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 669 |
|
| 670 |
return generic_decode_fail(tests, ntests, sizeof(size_t), |
| 671 |
(generic_decode)der_get_length); |
| 672 |
} |
| 673 |
|
| 674 |
static int |
| 675 |
check_fail_boolean(void) |
| 676 |
{ |
| 677 |
struct test_case tests[] = { |
| 678 |
{NULL, 0, "", "empty input data"} |
| 679 |
}; |
| 680 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 681 |
|
| 682 |
return generic_decode_fail(tests, ntests, sizeof(int), |
| 683 |
(generic_decode)der_get_boolean); |
| 684 |
} |
| 685 |
|
| 686 |
static int |
| 687 |
check_fail_general_string(void) |
| 688 |
{ |
| 689 |
struct test_case tests[] = { |
| 690 |
{ NULL, 3, "A\x00i", "NUL char in string"} |
| 691 |
}; |
| 692 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 693 |
|
| 694 |
return generic_decode_fail(tests, ntests, sizeof(heim_general_string), |
| 695 |
(generic_decode)der_get_general_string); |
| 696 |
} |
| 697 |
|
| 698 |
static int |
| 699 |
check_fail_bmp_string(void) |
| 700 |
{ |
| 701 |
struct test_case tests[] = { |
| 702 |
{NULL, 1, "\x00", "odd (1) length bmpstring"}, |
| 703 |
{NULL, 3, "\x00\x00\x00", "odd (3) length bmpstring"} |
| 704 |
}; |
| 705 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 706 |
|
| 707 |
return generic_decode_fail(tests, ntests, sizeof(heim_bmp_string), |
| 708 |
(generic_decode)der_get_bmp_string); |
| 709 |
} |
| 710 |
|
| 711 |
static int |
| 712 |
check_fail_universal_string(void) |
| 713 |
{ |
| 714 |
struct test_case tests[] = { |
| 715 |
{NULL, 1, "\x00", "x & 3 == 1 universal string"}, |
| 716 |
{NULL, 2, "\x00\x00", "x & 3 == 2 universal string"}, |
| 717 |
{NULL, 3, "\x00\x00\x00", "x & 3 == 3 universal string"}, |
| 718 |
{NULL, 5, "\x00\x00\x00\x00\x00", "x & 3 == 1 universal string"}, |
| 719 |
{NULL, 6, "\x00\x00\x00\x00\x00\x00", "x & 3 == 2 universal string"}, |
| 720 |
{NULL, 7, "\x00\x00\x00\x00\x00\x00\x00", "x & 3 == 3 universal string"} |
| 721 |
}; |
| 722 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 723 |
|
| 724 |
return generic_decode_fail(tests, ntests, sizeof(heim_universal_string), |
| 725 |
(generic_decode)der_get_universal_string); |
| 726 |
} |
| 727 |
|
| 728 |
static int |
| 729 |
check_fail_heim_integer(void) |
| 730 |
{ |
| 731 |
#if 0 |
| 732 |
struct test_case tests[] = { |
| 733 |
}; |
| 734 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 735 |
|
| 736 |
return generic_decode_fail(tests, ntests, sizeof(heim_integer), |
| 737 |
(generic_decode)der_get_heim_integer); |
| 738 |
#else |
| 739 |
return 0; |
| 740 |
#endif |
| 741 |
} |
| 742 |
|
| 743 |
static int |
| 744 |
check_fail_generalized_time(void) |
| 745 |
{ |
| 746 |
struct test_case tests[] = { |
| 747 |
{NULL, 1, "\x00", "no time"} |
| 748 |
}; |
| 749 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 750 |
|
| 751 |
return generic_decode_fail(tests, ntests, sizeof(time_t), |
| 752 |
(generic_decode)der_get_generalized_time); |
| 753 |
} |
| 754 |
|
| 755 |
static int |
| 756 |
check_fail_oid(void) |
| 757 |
{ |
| 758 |
struct test_case tests[] = { |
| 759 |
{NULL, 0, "", "empty input data"}, |
| 760 |
{NULL, 2, "\x00\x80", "last byte continuation" }, |
| 761 |
{NULL, 11, "\x00\x81\x80\x80\x80\x80\x80\x80\x80\x80\x00", |
| 762 |
"oid element overflow" } |
| 763 |
}; |
| 764 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 765 |
|
| 766 |
return generic_decode_fail(tests, ntests, sizeof(heim_oid), |
| 767 |
(generic_decode)der_get_oid); |
| 768 |
} |
| 769 |
|
| 770 |
static int |
| 771 |
check_fail_bitstring(void) |
| 772 |
{ |
| 773 |
struct test_case tests[] = { |
| 774 |
{NULL, 0, "", "empty input data"}, |
| 775 |
{NULL, 1, "\x08", "larger then 8 bits trailer"}, |
| 776 |
{NULL, 1, "\x01", "to few bytes for bits"}, |
| 777 |
{NULL, -2, "\x00", "length overrun"}, |
| 778 |
{NULL, -1, "", "length to short"} |
| 779 |
}; |
| 780 |
int ntests = sizeof(tests) / sizeof(*tests); |
| 781 |
|
| 782 |
return generic_decode_fail(tests, ntests, sizeof(heim_bit_string), |
| 783 |
(generic_decode)der_get_bit_string); |
| 784 |
} |
| 785 |
|
| 786 |
static int |
| 787 |
check_heim_integer_same(const char *p, const char *norm_p, heim_integer *i) |
| 788 |
{ |
| 789 |
heim_integer i2; |
| 790 |
char *str; |
| 791 |
int ret; |
| 792 |
|
| 793 |
ret = der_print_hex_heim_integer(i, &str); |
| 794 |
if (ret) |
| 795 |
errx(1, "der_print_hex_heim_integer: %d", ret); |
| 796 |
|
| 797 |
if (strcmp(str, norm_p) != 0) |
| 798 |
errx(1, "der_print_hex_heim_integer: %s != %s", str, p); |
| 799 |
|
| 800 |
ret = der_parse_hex_heim_integer(str, &i2); |
| 801 |
if (ret) |
| 802 |
errx(1, "der_parse_hex_heim_integer: %d", ret); |
| 803 |
|
| 804 |
if (der_heim_integer_cmp(i, &i2) != 0) |
| 805 |
errx(1, "der_heim_integer_cmp: p %s", p); |
| 806 |
|
| 807 |
der_free_heim_integer(&i2); |
| 808 |
free(str); |
| 809 |
|
| 810 |
ret = der_parse_hex_heim_integer(p, &i2); |
| 811 |
if (ret) |
| 812 |
errx(1, "der_parse_hex_heim_integer: %d", ret); |
| 813 |
|
| 814 |
if (der_heim_integer_cmp(i, &i2) != 0) |
| 815 |
errx(1, "der_heim_integer_cmp: norm"); |
| 816 |
|
| 817 |
der_free_heim_integer(&i2); |
| 818 |
|
| 819 |
return 0; |
| 820 |
} |
| 821 |
|
| 822 |
static int |
| 823 |
test_heim_int_format(void) |
| 824 |
{ |
| 825 |
heim_integer i = { 1, "\x10", 0 }; |
| 826 |
heim_integer i2 = { 1, "\x10", 1 }; |
| 827 |
heim_integer i3 = { 1, "\01", 0 }; |
| 828 |
char *p = |
| 829 |
"FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1" |
| 830 |
"29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD" |
| 831 |
"EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245" |
| 832 |
"E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED" |
| 833 |
"EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381" |
| 834 |
"FFFFFFFF" "FFFFFFFF"; |
| 835 |
heim_integer bni = { |
| 836 |
128, |
| 837 |
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC9\x0F\xDA\xA2" |
| 838 |
"\x21\x68\xC2\x34\xC4\xC6\x62\x8B\x80\xDC\x1C\xD1" |
| 839 |
"\x29\x02\x4E\x08\x8A\x67\xCC\x74\x02\x0B\xBE\xA6" |
| 840 |
"\x3B\x13\x9B\x22\x51\x4A\x08\x79\x8E\x34\x04\xDD" |
| 841 |
"\xEF\x95\x19\xB3\xCD\x3A\x43\x1B\x30\x2B\x0A\x6D" |
| 842 |
"\xF2\x5F\x14\x37\x4F\xE1\x35\x6D\x6D\x51\xC2\x45" |
| 843 |
"\xE4\x85\xB5\x76\x62\x5E\x7E\xC6\xF4\x4C\x42\xE9" |
| 844 |
"\xA6\x37\xED\x6B\x0B\xFF\x5C\xB6\xF4\x06\xB7\xED" |
| 845 |
"\xEE\x38\x6B\xFB\x5A\x89\x9F\xA5\xAE\x9F\x24\x11" |
| 846 |
"\x7C\x4B\x1F\xE6\x49\x28\x66\x51\xEC\xE6\x53\x81" |
| 847 |
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", |
| 848 |
0 |
| 849 |
}; |
| 850 |
heim_integer f; |
| 851 |
int ret = 0; |
| 852 |
|
| 853 |
ret += check_heim_integer_same(p, p, &bni); |
| 854 |
ret += check_heim_integer_same("10", "10", &i); |
| 855 |
ret += check_heim_integer_same("00000010", "10", &i); |
| 856 |
ret += check_heim_integer_same("-10", "-10", &i2); |
| 857 |
ret += check_heim_integer_same("-00000010", "-10", &i2); |
| 858 |
ret += check_heim_integer_same("01", "01", &i3); |
| 859 |
ret += check_heim_integer_same("1", "01", &i3); |
| 860 |
|
| 861 |
{ |
| 862 |
int r; |
| 863 |
r = der_parse_hex_heim_integer("-", &f); |
| 864 |
if (r == 0) { |
| 865 |
der_free_heim_integer(&f); |
| 866 |
ret++; |
| 867 |
} |
| 868 |
/* used to cause UMR */ |
| 869 |
r = der_parse_hex_heim_integer("00", &f); |
| 870 |
if (r == 0) |
| 871 |
der_free_heim_integer(&f); |
| 872 |
else |
| 873 |
ret++; |
| 874 |
} |
| 875 |
|
| 876 |
return ret; |
| 877 |
} |
| 878 |
|
| 879 |
static int |
| 880 |
test_heim_oid_format_same(const char *str, const heim_oid *oid) |
| 881 |
{ |
| 882 |
int ret; |
| 883 |
char *p; |
| 884 |
heim_oid o2; |
| 885 |
|
| 886 |
ret = der_print_heim_oid(oid, ' ', &p); |
| 887 |
if (ret) { |
| 888 |
printf("fail to print oid: %s\n", str); |
| 889 |
return 1; |
| 890 |
} |
| 891 |
ret = strcmp(p, str); |
| 892 |
if (ret) { |
| 893 |
printf("oid %s != formated oid %s\n", str, p); |
| 894 |
free(p); |
| 895 |
return ret; |
| 896 |
} |
| 897 |
|
| 898 |
ret = der_parse_heim_oid(p, " ", &o2); |
| 899 |
if (ret) { |
| 900 |
printf("failed to parse %s\n", p); |
| 901 |
free(p); |
| 902 |
return ret; |
| 903 |
} |
| 904 |
free(p); |
| 905 |
ret = der_heim_oid_cmp(&o2, oid); |
| 906 |
der_free_oid(&o2); |
| 907 |
|
| 908 |
return ret; |
| 909 |
} |
| 910 |
|
| 911 |
static unsigned sha1_oid_tree[] = { 1, 3, 14, 3, 2, 26 }; |
| 912 |
|
| 913 |
static int |
| 914 |
test_heim_oid_format(void) |
| 915 |
{ |
| 916 |
heim_oid sha1 = { 6, sha1_oid_tree }; |
| 917 |
int ret = 0; |
| 918 |
|
| 919 |
ret += test_heim_oid_format_same("1 3 14 3 2 26", &sha1); |
| 920 |
|
| 921 |
return ret; |
| 922 |
} |
| 923 |
|
| 924 |
static int |
| 925 |
check_trailing_nul(void) |
| 926 |
{ |
| 927 |
int i, ret; |
| 928 |
struct { |
| 929 |
int fail; |
| 930 |
const unsigned char *p; |
| 931 |
size_t len; |
| 932 |
const char *s; |
| 933 |
size_t size; |
| 934 |
} foo[] = { |
| 935 |
{ 1, (const unsigned char *)"foo\x00o", 5, NULL, 0 }, |
| 936 |
{ 1, (const unsigned char *)"\x00o", 2, NULL, 0 }, |
| 937 |
{ 0, (const unsigned char *)"\x00\x00\x00\x00\x00", 5, "", 5 }, |
| 938 |
{ 0, (const unsigned char *)"\x00", 1, "", 1 }, |
| 939 |
{ 0, (const unsigned char *)"", 0, "", 0 }, |
| 940 |
{ 0, (const unsigned char *)"foo\x00\x00", 5, "foo", 5 }, |
| 941 |
{ 0, (const unsigned char *)"foo\0", 4, "foo", 4 }, |
| 942 |
{ 0, (const unsigned char *)"foo", 3, "foo", 3 } |
| 943 |
}; |
| 944 |
|
| 945 |
for (i = 0; i < sizeof(foo)/sizeof(foo[0]); i++) { |
| 946 |
char *s; |
| 947 |
size_t size; |
| 948 |
ret = der_get_general_string(foo[i].p, foo[i].len, &s, &size); |
| 949 |
if (foo[i].fail) { |
| 950 |
if (ret == 0) |
| 951 |
errx(1, "check %d NULL didn't fail", i); |
| 952 |
continue; |
| 953 |
} |
| 954 |
if (ret) |
| 955 |
errx(1, "NULL check %d der_get_general_string failed", i); |
| 956 |
if (foo[i].size != size) |
| 957 |
errx(1, "NUL check i = %d size failed", i); |
| 958 |
if (strcmp(foo[i].s, s) != 0) |
| 959 |
errx(1, "NUL check i = %d content failed", i); |
| 960 |
free(s); |
| 961 |
} |
| 962 |
return 0; |
| 963 |
} |
| 964 |
|
| 965 |
static int |
| 966 |
test_misc_cmp(void) |
| 967 |
{ |
| 968 |
int ret; |
| 969 |
|
| 970 |
/* diffrent lengths are diffrent */ |
| 971 |
{ |
| 972 |
const heim_octet_string os1 = { 1, "a" } , os2 = { 0, NULL }; |
| 973 |
ret = der_heim_octet_string_cmp(&os1, &os2); |
| 974 |
if (ret == 0) |
| 975 |
return 1; |
| 976 |
} |
| 977 |
/* diffrent data are diffrent */ |
| 978 |
{ |
| 979 |
const heim_octet_string os1 = { 1, "a" } , os2 = { 1, "b" }; |
| 980 |
ret = der_heim_octet_string_cmp(&os1, &os2); |
| 981 |
if (ret == 0) |
| 982 |
return 1; |
| 983 |
} |
| 984 |
/* diffrent lengths are diffrent */ |
| 985 |
{ |
| 986 |
const heim_bit_string bs1 = { 8, "a" } , bs2 = { 7, "a" }; |
| 987 |
ret = der_heim_bit_string_cmp(&bs1, &bs2); |
| 988 |
if (ret == 0) |
| 989 |
return 1; |
| 990 |
} |
| 991 |
/* diffrent data are diffrent */ |
| 992 |
{ |
| 993 |
const heim_bit_string bs1 = { 7, "\x0f" } , bs2 = { 7, "\x02" }; |
| 994 |
ret = der_heim_bit_string_cmp(&bs1, &bs2); |
| 995 |
if (ret == 0) |
| 996 |
return 1; |
| 997 |
} |
| 998 |
/* diffrent lengths are diffrent */ |
| 999 |
{ |
| 1000 |
uint16_t data = 1; |
| 1001 |
heim_bmp_string bs1 = { 1, NULL } , bs2 = { 0, NULL }; |
| 1002 |
bs1.data = &data; |
| 1003 |
ret = der_heim_bmp_string_cmp(&bs1, &bs2); |
| 1004 |
if (ret == 0) |
| 1005 |
return 1; |
| 1006 |
} |
| 1007 |
/* diffrent lengths are diffrent */ |
| 1008 |
{ |
| 1009 |
uint32_t data; |
| 1010 |
heim_universal_string us1 = { 1, NULL } , us2 = { 0, NULL }; |
| 1011 |
us1.data = &data; |
| 1012 |
ret = der_heim_universal_string_cmp(&us1, &us2); |
| 1013 |
if (ret == 0) |
| 1014 |
return 1; |
| 1015 |
} |
| 1016 |
/* same */ |
| 1017 |
{ |
| 1018 |
uint32_t data = (uint32_t)'a'; |
| 1019 |
heim_universal_string us1 = { 1, NULL } , us2 = { 1, NULL }; |
| 1020 |
us1.data = &data; |
| 1021 |
us2.data = &data; |
| 1022 |
ret = der_heim_universal_string_cmp(&us1, &us2); |
| 1023 |
if (ret != 0) |
| 1024 |
return 1; |
| 1025 |
} |
| 1026 |
|
| 1027 |
return 0; |
| 1028 |
} |
| 1029 |
|
| 1030 |
static int |
| 1031 |
corner_generalized_time(void) |
| 1032 |
{ |
| 1033 |
const char *str = "760520140000Z"; |
| 1034 |
size_t size; |
| 1035 |
time_t t; |
| 1036 |
int ret; |
| 1037 |
|
| 1038 |
ret = der_get_generalized_time((const unsigned char*)str, strlen(str), |
| 1039 |
&t, &size); |
| 1040 |
if (ret) |
| 1041 |
return 1; |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
|
| 1045 |
static int |
| 1046 |
corner_tag(void) |
| 1047 |
{ |
| 1048 |
struct { |
| 1049 |
int ok; |
| 1050 |
const char *ptr; |
| 1051 |
size_t len; |
| 1052 |
} tests[] = { |
| 1053 |
{ 1, "\x00", 1 }, |
| 1054 |
{ 0, "\xff", 1 }, |
| 1055 |
{ 0, "\xff\xff\xff\xff\xff\xff\xff\xff", 8 } |
| 1056 |
}; |
| 1057 |
int i, ret; |
| 1058 |
Der_class cl; |
| 1059 |
Der_type ty; |
| 1060 |
unsigned int tag; |
| 1061 |
size_t size; |
| 1062 |
|
| 1063 |
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) { |
| 1064 |
ret = der_get_tag((const unsigned char*)tests[i].ptr, |
| 1065 |
tests[i].len, &cl, &ty, &tag, &size); |
| 1066 |
if (ret) { |
| 1067 |
if (tests[i].ok) |
| 1068 |
errx(1, "failed while shouldn't"); |
| 1069 |
} else { |
| 1070 |
if (!tests[i].ok) |
| 1071 |
errx(1, "passed while shouldn't"); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
return 0; |
| 1075 |
} |
| 1076 |
|
| 1077 |
struct randomcheck { |
| 1078 |
asn1_type_decode decoder; |
| 1079 |
asn1_type_release release; |
| 1080 |
size_t typesize; |
| 1081 |
size_t inputsize; |
| 1082 |
} randomcheck[] = { |
| 1083 |
#define el(name, type, maxlen) { \ |
| 1084 |
(asn1_type_decode)der_get_##name, \ |
| 1085 |
(asn1_type_release)der_free_##name, \ |
| 1086 |
sizeof(type), \ |
| 1087 |
maxlen \ |
| 1088 |
} |
| 1089 |
el(integer, int, 6), |
| 1090 |
el(heim_integer, heim_integer, 12), |
| 1091 |
el(integer, int, 6), |
| 1092 |
el(unsigned, unsigned, 6), |
| 1093 |
el(general_string, heim_general_string, 12), |
| 1094 |
el(octet_string, heim_octet_string, 12), |
| 1095 |
{ (asn1_type_decode)der_get_octet_string_ber, |
| 1096 |
(asn1_type_release)der_free_octet_string, |
| 1097 |
sizeof(heim_octet_string), 20 }, |
| 1098 |
el(generalized_time, time_t, 20), |
| 1099 |
el(utctime, time_t, 20), |
| 1100 |
el(bit_string, heim_bit_string, 10), |
| 1101 |
el(oid, heim_oid, 10), |
| 1102 |
{ NULL, NULL, 0, 0 } |
| 1103 |
#undef el |
| 1104 |
}; |
| 1105 |
|
| 1106 |
static void |
| 1107 |
asn1rand(uint8_t *randbytes, size_t len) |
| 1108 |
{ |
| 1109 |
while (len) { |
| 1110 |
*randbytes++ = rk_random(); |
| 1111 |
len--; |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
static int |
| 1116 |
check_random(void) |
| 1117 |
{ |
| 1118 |
struct randomcheck *r = randomcheck; |
| 1119 |
uint8_t *input; |
| 1120 |
void *type; |
| 1121 |
size_t size, insize; |
| 1122 |
int ret; |
| 1123 |
|
| 1124 |
while (r->decoder) { |
| 1125 |
type = emalloc(r->typesize); |
| 1126 |
memset(type, 0, r->typesize); |
| 1127 |
|
| 1128 |
input = emalloc(r->inputsize); |
| 1129 |
|
| 1130 |
/* try all zero first */ |
| 1131 |
memset(input, 0, r->inputsize); |
| 1132 |
|
| 1133 |
ret = r->decoder(input, r->inputsize, type, &size); |
| 1134 |
if (ret) |
| 1135 |
r->release(type); |
| 1136 |
|
| 1137 |
/* try all one first */ |
| 1138 |
memset(input, 0xff, r->inputsize); |
| 1139 |
ret = r->decoder(input, r->inputsize, type, &size); |
| 1140 |
if (ret) |
| 1141 |
r->release(type); |
| 1142 |
|
| 1143 |
/* try 0x41 too */ |
| 1144 |
memset(input, 0x41, r->inputsize); |
| 1145 |
ret = r->decoder(input, r->inputsize, type, &size); |
| 1146 |
if (ret) |
| 1147 |
r->release(type); |
| 1148 |
|
| 1149 |
/* random */ |
| 1150 |
asn1rand(input, r->inputsize); |
| 1151 |
ret = r->decoder(input, r->inputsize, type, &size); |
| 1152 |
if (ret) |
| 1153 |
r->release(type); |
| 1154 |
|
| 1155 |
/* let make buffer smaller */ |
| 1156 |
insize = r->inputsize; |
| 1157 |
do { |
| 1158 |
insize--; |
| 1159 |
asn1rand(input, insize); |
| 1160 |
|
| 1161 |
ret = r->decoder(input, insize, type, &size); |
| 1162 |
if (ret == 0) |
| 1163 |
r->release(type); |
| 1164 |
} while(insize > 0); |
| 1165 |
|
| 1166 |
free(type); |
| 1167 |
|
| 1168 |
r++; |
| 1169 |
} |
| 1170 |
return 0; |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
int |
| 1176 |
main(int argc, char **argv) |
| 1177 |
{ |
| 1178 |
int ret = 0; |
| 1179 |
|
| 1180 |
ret += test_integer (); |
| 1181 |
ret += test_integer_more(); |
| 1182 |
ret += test_unsigned (); |
| 1183 |
ret += test_octet_string (); |
| 1184 |
ret += test_bmp_string (); |
| 1185 |
ret += test_universal_string (); |
| 1186 |
ret += test_general_string (); |
| 1187 |
ret += test_generalized_time (); |
| 1188 |
ret += test_oid (); |
| 1189 |
ret += test_bit_string(); |
| 1190 |
ret += test_heim_integer(); |
| 1191 |
ret += test_boolean(); |
| 1192 |
|
| 1193 |
ret += check_fail_unsigned(); |
| 1194 |
ret += check_fail_integer(); |
| 1195 |
ret += check_fail_length(); |
| 1196 |
ret += check_fail_boolean(); |
| 1197 |
ret += check_fail_general_string(); |
| 1198 |
ret += check_fail_bmp_string(); |
| 1199 |
ret += check_fail_universal_string(); |
| 1200 |
ret += check_fail_heim_integer(); |
| 1201 |
ret += check_fail_generalized_time(); |
| 1202 |
ret += check_fail_oid(); |
| 1203 |
ret += check_fail_bitstring(); |
| 1204 |
ret += test_heim_int_format(); |
| 1205 |
ret += test_heim_oid_format(); |
| 1206 |
ret += check_trailing_nul(); |
| 1207 |
ret += test_misc_cmp(); |
| 1208 |
ret += corner_generalized_time(); |
| 1209 |
ret += corner_tag(); |
| 1210 |
ret += check_random(); |
| 1211 |
|
| 1212 |
return ret; |
| 1213 |
} |