diff options
| author | tb <> | 2025-12-18 09:15:28 +0000 |
|---|---|---|
| committer | tb <> | 2025-12-18 09:15:28 +0000 |
| commit | 2681c4505dbb93940f461387b894b73e6ffedaff (patch) | |
| tree | 8f93bf87122e928563bff5aa3a2324d14939f36b | |
| parent | 341829195e0c845cf26339768f275f021b69fedc (diff) | |
| download | openbsd-2681c4505dbb93940f461387b894b73e6ffedaff.tar.gz openbsd-2681c4505dbb93940f461387b894b73e6ffedaff.tar.bz2 openbsd-2681c4505dbb93940f461387b894b73e6ffedaff.zip | |
Exercises the batshit crazy truncation behavior of ASN1_BIT_STRING_set_bit()
Based on https://boringssl-review.googlesource.com/c/boringssl/+/48225
(still under ISC).
| -rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1basic.c | 426 |
1 files changed, 425 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c index 1a873bf25d..f900e45be5 100644 --- a/src/regress/lib/libcrypto/asn1/asn1basic.c +++ b/src/regress/lib/libcrypto/asn1/asn1basic.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* $OpenBSD: asn1basic.c,v 1.16 2024/02/04 13:07:02 tb Exp $ */ | 1 | /* $OpenBSD: asn1basic.c,v 1.17 2025/12/18 09:15:28 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> |
| 4 | * Copyright (c) 2021 Google, Inc | ||
| 4 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> | 5 | * Copyright (c) 2023 Theo Buehler <tb@openbsd.org> |
| 5 | * | 6 | * |
| 6 | * Permission to use, copy, modify, and distribute this software for any | 7 | * Permission to use, copy, modify, and distribute this software for any |
| @@ -174,6 +175,428 @@ asn1_bit_string_test(void) | |||
| 174 | return failed; | 175 | return failed; |
| 175 | } | 176 | } |
| 176 | 177 | ||
| 178 | static const uint8_t asn1_bit_string_empty[] = { | ||
| 179 | 0x03, 0x01, 0x00, | ||
| 180 | }; | ||
| 181 | |||
| 182 | static const uint8_t asn1_bit_string_1101[] = { | ||
| 183 | 0x03, 0x02, 0x04, 0xd0, | ||
| 184 | }; | ||
| 185 | |||
| 186 | static const uint8_t asn1_bit_string_1001[] = { | ||
| 187 | 0x03, 0x02, 0x04, 0x90, | ||
| 188 | }; | ||
| 189 | |||
| 190 | static const uint8_t asn1_bit_string_1[] = { | ||
| 191 | 0x03, 0x02, 0x07, 0x80, | ||
| 192 | }; | ||
| 193 | |||
| 194 | static const uint8_t asn1_bit_string_1zeroes1[] = { | ||
| 195 | 0x03, 0x09, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, | ||
| 196 | 0x00, 0x00, 0x01, | ||
| 197 | }; | ||
| 198 | |||
| 199 | static const uint8_t asn1_bit_string_10010[] = { | ||
| 200 | 0x03, 0x02, 0x03, 0x90, | ||
| 201 | }; | ||
| 202 | |||
| 203 | static int | ||
| 204 | asn1_bit_string_set_bit_test(void) | ||
| 205 | { | ||
| 206 | ASN1_BIT_STRING *abs; | ||
| 207 | const unsigned char *p; | ||
| 208 | unsigned char *der = NULL; | ||
| 209 | int der_len = 0; | ||
| 210 | int p_len; | ||
| 211 | int got; | ||
| 212 | int failed = 1; | ||
| 213 | |||
| 214 | /* | ||
| 215 | * A new ASN1_BIT_STRING serializes to the empty BIT STRING | ||
| 216 | */ | ||
| 217 | |||
| 218 | if ((abs = ASN1_BIT_STRING_new()) == NULL) { | ||
| 219 | fprintf(stderr, "FAIL: ASN1_BIT_STRING_new()\n"); | ||
| 220 | goto failed; | ||
| 221 | } | ||
| 222 | |||
| 223 | freezero(der, der_len); | ||
| 224 | der = NULL; | ||
| 225 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 226 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 227 | der_len = 0; | ||
| 228 | goto failed; | ||
| 229 | } | ||
| 230 | if (!asn1_compare_bytes("new BIT STRING", der, der_len, | ||
| 231 | asn1_bit_string_empty, sizeof(asn1_bit_string_empty))) | ||
| 232 | goto failed; | ||
| 233 | |||
| 234 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 0) { | ||
| 235 | fprintf(stderr, "FAIL: new BIT STRING bit 0: want %d, got %d\n", | ||
| 236 | 0, got); | ||
| 237 | goto failed; | ||
| 238 | } | ||
| 239 | if ((got = ASN1_BIT_STRING_get_bit(abs, 100)) != 0) { | ||
| 240 | fprintf(stderr, "FAIL: new BIT STRING bit 100: want %d, got %d\n", | ||
| 241 | 0, got); | ||
| 242 | goto failed; | ||
| 243 | } | ||
| 244 | |||
| 245 | /* | ||
| 246 | * Now set a few bits via ASN1_BIT_STRING_set_bit() | ||
| 247 | */ | ||
| 248 | |||
| 249 | if (!ASN1_BIT_STRING_set_bit(abs, 0, 1) || | ||
| 250 | !ASN1_BIT_STRING_set_bit(abs, 1, 1) || | ||
| 251 | !ASN1_BIT_STRING_set_bit(abs, 2, 0) || | ||
| 252 | !ASN1_BIT_STRING_set_bit(abs, 3, 1)) { | ||
| 253 | fprintf(stderr, "FAIL: BIT STRING 1101 ASN1_BIT_STRING_set_bit\n"); | ||
| 254 | goto failed; | ||
| 255 | } | ||
| 256 | |||
| 257 | freezero(der, der_len); | ||
| 258 | der = NULL; | ||
| 259 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 260 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 261 | der_len = 0; | ||
| 262 | goto failed; | ||
| 263 | } | ||
| 264 | if (!asn1_compare_bytes("BIT STRING 1101", der, der_len, | ||
| 265 | asn1_bit_string_1101, sizeof(asn1_bit_string_1101))) | ||
| 266 | goto failed; | ||
| 267 | |||
| 268 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 269 | fprintf(stderr, "FAIL: BIT STRING 1101 bit 0: want %d, got %d\n", | ||
| 270 | 1, got); | ||
| 271 | goto failed; | ||
| 272 | } | ||
| 273 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 1) { | ||
| 274 | fprintf(stderr, "FAIL: BIT STRING 1101 bit 1: want %d, got %d\n", | ||
| 275 | 1, got); | ||
| 276 | goto failed; | ||
| 277 | } | ||
| 278 | if ((got = ASN1_BIT_STRING_get_bit(abs, 2)) != 0) { | ||
| 279 | fprintf(stderr, "FAIL: BIT STRING 1101 bit 2: want %d, got %d\n", | ||
| 280 | 0, got); | ||
| 281 | goto failed; | ||
| 282 | } | ||
| 283 | if ((got = ASN1_BIT_STRING_get_bit(abs, 3)) != 1) { | ||
| 284 | fprintf(stderr, "FAIL: BIT STRING 1101 bit 3: want %d, got %d\n", | ||
| 285 | 1, got); | ||
| 286 | goto failed; | ||
| 287 | } | ||
| 288 | if ((got = ASN1_BIT_STRING_get_bit(abs, 4)) != 0) { | ||
| 289 | fprintf(stderr, "FAIL: BIT STRING 1101 bit 4: want %d, got %d\n", | ||
| 290 | 0, got); | ||
| 291 | goto failed; | ||
| 292 | } | ||
| 293 | |||
| 294 | /* | ||
| 295 | * Bits that were set may be cleared. | ||
| 296 | */ | ||
| 297 | |||
| 298 | if (!ASN1_BIT_STRING_set_bit(abs, 1, 0)) { | ||
| 299 | fprintf(stderr, "FAIL: BIT STRING 1101, clear bit 1\n"); | ||
| 300 | goto failed; | ||
| 301 | } | ||
| 302 | |||
| 303 | freezero(der, der_len); | ||
| 304 | der = NULL; | ||
| 305 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 306 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 307 | der_len = 0; | ||
| 308 | goto failed; | ||
| 309 | } | ||
| 310 | if (!asn1_compare_bytes("BIT STRING 1001", der, der_len, | ||
| 311 | asn1_bit_string_1001, sizeof(asn1_bit_string_1001))) | ||
| 312 | goto failed; | ||
| 313 | |||
| 314 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 315 | fprintf(stderr, "FAIL: BIT STRING 1001 bit 0: want %d, got %d\n", | ||
| 316 | 1, got); | ||
| 317 | goto failed; | ||
| 318 | } | ||
| 319 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 0) { | ||
| 320 | fprintf(stderr, "FAIL: BIT STRING 1001 bit 1: want %d, got %d\n", | ||
| 321 | 0, got); | ||
| 322 | goto failed; | ||
| 323 | } | ||
| 324 | if ((got = ASN1_BIT_STRING_get_bit(abs, 2)) != 0) { | ||
| 325 | fprintf(stderr, "FAIL: BIT STRING 1001 bit 2: want %d, got %d\n", | ||
| 326 | 0, got); | ||
| 327 | goto failed; | ||
| 328 | } | ||
| 329 | if ((got = ASN1_BIT_STRING_get_bit(abs, 3)) != 1) { | ||
| 330 | fprintf(stderr, "FAIL: BIT STRING 1001 bit 3: want %d, got %d\n", | ||
| 331 | 1, got); | ||
| 332 | goto failed; | ||
| 333 | } | ||
| 334 | if ((got = ASN1_BIT_STRING_get_bit(abs, 4)) != 0) { | ||
| 335 | fprintf(stderr, "FAIL: BIT STRING 1001 bit 4: want %d, got %d\n", | ||
| 336 | 0, got); | ||
| 337 | goto failed; | ||
| 338 | } | ||
| 339 | |||
| 340 | /* | ||
| 341 | * Clearing trailing bits truncates the string. | ||
| 342 | */ | ||
| 343 | |||
| 344 | if (!ASN1_BIT_STRING_set_bit(abs, 3, 0)) { | ||
| 345 | fprintf(stderr, "FAIL: BIT STRING 1001, clear bit 3\n"); | ||
| 346 | goto failed; | ||
| 347 | } | ||
| 348 | |||
| 349 | freezero(der, der_len); | ||
| 350 | der = NULL; | ||
| 351 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 352 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 353 | der_len = 0; | ||
| 354 | goto failed; | ||
| 355 | } | ||
| 356 | if (!asn1_compare_bytes("BIT STRING 1", der, der_len, | ||
| 357 | asn1_bit_string_1, sizeof(asn1_bit_string_1))) | ||
| 358 | goto failed; | ||
| 359 | |||
| 360 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 361 | fprintf(stderr, "FAIL: BIT STRING 1 bit 0: want %d, got %d\n", | ||
| 362 | 1, got); | ||
| 363 | goto failed; | ||
| 364 | } | ||
| 365 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 0) { | ||
| 366 | fprintf(stderr, "FAIL: BIT STRING 1 bit 1: want %d, got %d\n", | ||
| 367 | 0, got); | ||
| 368 | goto failed; | ||
| 369 | } | ||
| 370 | if ((got = ASN1_BIT_STRING_get_bit(abs, 2)) != 0) { | ||
| 371 | fprintf(stderr, "FAIL: BIT STRING 1 bit 2: want %d, got %d\n", | ||
| 372 | 0, got); | ||
| 373 | goto failed; | ||
| 374 | } | ||
| 375 | if ((got = ASN1_BIT_STRING_get_bit(abs, 3)) != 0) { | ||
| 376 | fprintf(stderr, "FAIL: BIT STRING 1 bit 3: want %d, got %d\n", | ||
| 377 | 0, got); | ||
| 378 | goto failed; | ||
| 379 | } | ||
| 380 | if ((got = ASN1_BIT_STRING_get_bit(abs, 4)) != 0) { | ||
| 381 | fprintf(stderr, "FAIL: BIT STRING 1 bit 4: want %d, got %d\n", | ||
| 382 | 0, got); | ||
| 383 | goto failed; | ||
| 384 | } | ||
| 385 | |||
| 386 | /* | ||
| 387 | * Bits may be set beyond the end of the string. | ||
| 388 | */ | ||
| 389 | |||
| 390 | if (!ASN1_BIT_STRING_set_bit(abs, 63, 1)) { | ||
| 391 | fprintf(stderr, "FAIL: BIT STRING 1 set bit 63\n"); | ||
| 392 | goto failed; | ||
| 393 | } | ||
| 394 | |||
| 395 | freezero(der, der_len); | ||
| 396 | der = NULL; | ||
| 397 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 398 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 399 | der_len = 0; | ||
| 400 | goto failed; | ||
| 401 | } | ||
| 402 | if (!asn1_compare_bytes("BIT STRING 1zeroes1", der, der_len, | ||
| 403 | asn1_bit_string_1zeroes1, sizeof(asn1_bit_string_1zeroes1))) | ||
| 404 | goto failed; | ||
| 405 | |||
| 406 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 407 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 bit 0: want %d, got %d\n", | ||
| 408 | 1, got); | ||
| 409 | goto failed; | ||
| 410 | } | ||
| 411 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 0) { | ||
| 412 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 bit 1: want %d, got %d\n", | ||
| 413 | 0, got); | ||
| 414 | goto failed; | ||
| 415 | } | ||
| 416 | if ((got = ASN1_BIT_STRING_get_bit(abs, 62)) != 0) { | ||
| 417 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 bit 62: want %d, got %d\n", | ||
| 418 | 0, got); | ||
| 419 | goto failed; | ||
| 420 | } | ||
| 421 | if ((got = ASN1_BIT_STRING_get_bit(abs, 63)) != 1) { | ||
| 422 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 bit 63: want %d, got %d\n", | ||
| 423 | 1, got); | ||
| 424 | goto failed; | ||
| 425 | } | ||
| 426 | if ((got = ASN1_BIT_STRING_get_bit(abs, 64)) != 0) { | ||
| 427 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 bit 64: want %d, got %d\n", | ||
| 428 | 0, got); | ||
| 429 | goto failed; | ||
| 430 | } | ||
| 431 | |||
| 432 | /* | ||
| 433 | * We can truncate the string back down again. | ||
| 434 | */ | ||
| 435 | |||
| 436 | if (!ASN1_BIT_STRING_set_bit(abs, 63, 0)) { | ||
| 437 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1, clear bit 63\n"); | ||
| 438 | goto failed; | ||
| 439 | } | ||
| 440 | |||
| 441 | freezero(der, der_len); | ||
| 442 | der = NULL; | ||
| 443 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 444 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 445 | der_len = 0; | ||
| 446 | goto failed; | ||
| 447 | } | ||
| 448 | if (!asn1_compare_bytes("BIT STRING 1zeroes", der, der_len, | ||
| 449 | asn1_bit_string_1, sizeof(asn1_bit_string_1))) | ||
| 450 | goto failed; | ||
| 451 | |||
| 452 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 453 | fprintf(stderr, "FAIL: BIT STRING 1zeroes bit 0: want %d, got %d\n", | ||
| 454 | 1, got); | ||
| 455 | goto failed; | ||
| 456 | } | ||
| 457 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 0) { | ||
| 458 | fprintf(stderr, "FAIL: BIT STRING 1zeroes bit 1: want %d, got %d\n", | ||
| 459 | 0, got); | ||
| 460 | goto failed; | ||
| 461 | } | ||
| 462 | if ((got = ASN1_BIT_STRING_get_bit(abs, 62)) != 0) { | ||
| 463 | fprintf(stderr, "FAIL: BIT STRING 1zeroes bit 62: want %d, got %d\n", | ||
| 464 | 0, got); | ||
| 465 | goto failed; | ||
| 466 | } | ||
| 467 | if ((got = ASN1_BIT_STRING_get_bit(abs, 63)) != 0) { | ||
| 468 | fprintf(stderr, "FAIL: BIT STRING 1zeroes bit 63: want %d, got %d\n", | ||
| 469 | 0, got); | ||
| 470 | goto failed; | ||
| 471 | } | ||
| 472 | if ((got = ASN1_BIT_STRING_get_bit(abs, 64)) != 0) { | ||
| 473 | fprintf(stderr, "FAIL: BIT STRING 1zeroes bit 64: want %d, got %d\n", | ||
| 474 | 0, got); | ||
| 475 | goto failed; | ||
| 476 | } | ||
| 477 | |||
| 478 | /* | ||
| 479 | * ASN1_BIT_STRING_set_bit() truncation also happens for a parsed string. | ||
| 480 | */ | ||
| 481 | |||
| 482 | ASN1_BIT_STRING_free(abs); | ||
| 483 | abs = NULL; | ||
| 484 | |||
| 485 | p = asn1_bit_string_1zeroes1; | ||
| 486 | p_len = sizeof(asn1_bit_string_1zeroes1); | ||
| 487 | if ((abs = d2i_ASN1_BIT_STRING(NULL, &p, p_len)) == NULL) { | ||
| 488 | fprintf(stderr, "FAIL: BIT STRING 1zereos1 d2i_ASN1_BIT_STRING\n"); | ||
| 489 | goto failed; | ||
| 490 | } | ||
| 491 | |||
| 492 | freezero(der, der_len); | ||
| 493 | der = NULL; | ||
| 494 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 495 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 496 | der_len = 0; | ||
| 497 | goto failed; | ||
| 498 | } | ||
| 499 | if (!asn1_compare_bytes("BIT STRING 1zeroes1 (after d2i)", der, der_len, | ||
| 500 | asn1_bit_string_1zeroes1, sizeof(asn1_bit_string_1zeroes1))) | ||
| 501 | goto failed; | ||
| 502 | |||
| 503 | if (!ASN1_BIT_STRING_set_bit(abs, 63, 0)) { | ||
| 504 | fprintf(stderr, "FAIL: BIT STRING 1zeroes1 (after d2i), clear bit 63\n"); | ||
| 505 | goto failed; | ||
| 506 | } | ||
| 507 | |||
| 508 | freezero(der, der_len); | ||
| 509 | der = NULL; | ||
| 510 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 511 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 512 | der_len = 0; | ||
| 513 | goto failed; | ||
| 514 | } | ||
| 515 | if (!asn1_compare_bytes("BIT STRING 1zeroes (after d2i)", der, der_len, | ||
| 516 | asn1_bit_string_1, sizeof(asn1_bit_string_1))) | ||
| 517 | goto failed; | ||
| 518 | |||
| 519 | if ((got = ASN1_BIT_STRING_get_bit(abs, 0)) != 1) { | ||
| 520 | fprintf(stderr, "FAIL: BIT STRING 1zeroes (after d2i) bit 0: want %d, got %d\n", | ||
| 521 | 1, got); | ||
| 522 | goto failed; | ||
| 523 | } | ||
| 524 | if ((got = ASN1_BIT_STRING_get_bit(abs, 1)) != 0) { | ||
| 525 | fprintf(stderr, "FAIL: BIT STRING 1zeroes (after d2i) bit 1: want %d, got %d\n", | ||
| 526 | 0, got); | ||
| 527 | goto failed; | ||
| 528 | } | ||
| 529 | if ((got = ASN1_BIT_STRING_get_bit(abs, 62)) != 0) { | ||
| 530 | fprintf(stderr, "FAIL: BIT STRING 1zeroes (after d2i) bit 62: want %d, got %d\n", | ||
| 531 | 0, got); | ||
| 532 | goto failed; | ||
| 533 | } | ||
| 534 | if ((got = ASN1_BIT_STRING_get_bit(abs, 63)) != 0) { | ||
| 535 | fprintf(stderr, "FAIL: BIT STRING 1zeroes (after d2i) bit 63: want %d, got %d\n", | ||
| 536 | 0, got); | ||
| 537 | goto failed; | ||
| 538 | } | ||
| 539 | if ((got = ASN1_BIT_STRING_get_bit(abs, 64)) != 0) { | ||
| 540 | fprintf(stderr, "FAIL: BIT STRING 1zeroes (after d2i) bit 64: want %d, got %d\n", | ||
| 541 | 0, got); | ||
| 542 | goto failed; | ||
| 543 | } | ||
| 544 | |||
| 545 | /* | ||
| 546 | * A parsed bit string keeps its trailing zero bits. | ||
| 547 | */ | ||
| 548 | |||
| 549 | ASN1_BIT_STRING_free(abs); | ||
| 550 | abs = NULL; | ||
| 551 | |||
| 552 | p = asn1_bit_string_10010; | ||
| 553 | p_len = sizeof(asn1_bit_string_10010); | ||
| 554 | if ((abs = d2i_ASN1_BIT_STRING(NULL, &p, p_len)) == NULL) { | ||
| 555 | fprintf(stderr, "FAIL: BIT STRING 10010 d2i_ASN1_BIT_STRING\n"); | ||
| 556 | goto failed; | ||
| 557 | } | ||
| 558 | |||
| 559 | freezero(der, der_len); | ||
| 560 | der = NULL; | ||
| 561 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 562 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 563 | der_len = 0; | ||
| 564 | goto failed; | ||
| 565 | } | ||
| 566 | if (!asn1_compare_bytes("BIT STRING 10010", der, der_len, | ||
| 567 | asn1_bit_string_10010, sizeof(asn1_bit_string_10010))) | ||
| 568 | goto failed; | ||
| 569 | |||
| 570 | /* | ||
| 571 | * Of course, ASN1_BIT_STRING_set_bit() still truncates, even if it's | ||
| 572 | * a noop. | ||
| 573 | */ | ||
| 574 | |||
| 575 | if (!ASN1_BIT_STRING_set_bit(abs, 0, 1)) { | ||
| 576 | fprintf(stderr, "FAIL: BIT STRING 10010 set bit 0 to 1\n"); | ||
| 577 | goto failed; | ||
| 578 | } | ||
| 579 | |||
| 580 | freezero(der, der_len); | ||
| 581 | der = NULL; | ||
| 582 | if ((der_len = i2d_ASN1_BIT_STRING(abs, &der)) <= 0) { | ||
| 583 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 584 | der_len = 0; | ||
| 585 | goto failed; | ||
| 586 | } | ||
| 587 | if (!asn1_compare_bytes("BIT STRING 10010 after set bit", der, der_len, | ||
| 588 | asn1_bit_string_1001, sizeof(asn1_bit_string_1001))) | ||
| 589 | goto failed; | ||
| 590 | |||
| 591 | failed = 0; | ||
| 592 | |||
| 593 | failed: | ||
| 594 | ASN1_BIT_STRING_free(abs); | ||
| 595 | freezero(der, der_len); | ||
| 596 | |||
| 597 | return failed; | ||
| 598 | } | ||
| 599 | |||
| 177 | const uint8_t asn1_boolean_false[] = { | 600 | const uint8_t asn1_boolean_false[] = { |
| 178 | 0x01, 0x01, 0x00, | 601 | 0x01, 0x01, 0x00, |
| 179 | }; | 602 | }; |
| @@ -1129,6 +1552,7 @@ main(int argc, char **argv) | |||
| 1129 | int failed = 0; | 1552 | int failed = 0; |
| 1130 | 1553 | ||
| 1131 | failed |= asn1_bit_string_test(); | 1554 | failed |= asn1_bit_string_test(); |
| 1555 | failed |= asn1_bit_string_set_bit_test(); | ||
| 1132 | failed |= asn1_boolean_test(); | 1556 | failed |= asn1_boolean_test(); |
| 1133 | failed |= asn1_integer_test(); | 1557 | failed |= asn1_integer_test(); |
| 1134 | failed |= asn1_string_test(); | 1558 | failed |= asn1_string_test(); |
