diff options
| author | jsing <> | 2022-01-06 15:21:33 +0000 |
|---|---|---|
| committer | jsing <> | 2022-01-06 15:21:33 +0000 |
| commit | 5c467d13562fcca8393d2a129c4169bfe3ca8c55 (patch) | |
| tree | 317cdf93466f7ff46a5c11dd63e10f69a840f6a8 /src | |
| parent | b792111bd996ccf4464f74ed5df03b9f3bc708cc (diff) | |
| download | openbsd-5c467d13562fcca8393d2a129c4169bfe3ca8c55.tar.gz openbsd-5c467d13562fcca8393d2a129c4169bfe3ca8c55.tar.bz2 openbsd-5c467d13562fcca8393d2a129c4169bfe3ca8c55.zip | |
Add regress tests for ASN1_BIT_STRING.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1basic.c | 115 |
1 files changed, 113 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c index bb657058c2..c879ee6329 100644 --- a/src/regress/lib/libcrypto/asn1/asn1basic.c +++ b/src/regress/lib/libcrypto/asn1/asn1basic.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: asn1basic.c,v 1.2 2021/12/23 18:12:58 tb Exp $ */ | 1 | /* $OpenBSD: asn1basic.c,v 1.3 2022/01/06 15:21:33 jsing Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -39,6 +39,10 @@ asn1_compare_bytes(const char *label, const unsigned char *d1, int len1, | |||
| 39 | if (len1 != len2) { | 39 | if (len1 != len2) { |
| 40 | fprintf(stderr, "FAIL: %s - byte lengths differ " | 40 | fprintf(stderr, "FAIL: %s - byte lengths differ " |
| 41 | "(%i != %i)\n", label, len1, len2); | 41 | "(%i != %i)\n", label, len1, len2); |
| 42 | fprintf(stderr, "Got:\n"); | ||
| 43 | hexdump(d1, len1); | ||
| 44 | fprintf(stderr, "Want:\n"); | ||
| 45 | hexdump(d2, len2); | ||
| 42 | return 0; | 46 | return 0; |
| 43 | } | 47 | } |
| 44 | if (memcmp(d1, d2, len1) != 0) { | 48 | if (memcmp(d1, d2, len1) != 0) { |
| @@ -52,6 +56,111 @@ asn1_compare_bytes(const char *label, const unsigned char *d1, int len1, | |||
| 52 | return 1; | 56 | return 1; |
| 53 | } | 57 | } |
| 54 | 58 | ||
| 59 | const uint8_t asn1_bit_string_primitive[] = { | ||
| 60 | 0x03, 0x07, | ||
| 61 | 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0, | ||
| 62 | }; | ||
| 63 | |||
| 64 | static int | ||
| 65 | asn1_bit_string_test(void) | ||
| 66 | { | ||
| 67 | uint8_t bs[] = {0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0}; | ||
| 68 | ASN1_BIT_STRING *abs; | ||
| 69 | uint8_t *p = NULL, *pp; | ||
| 70 | const uint8_t *q; | ||
| 71 | int bit, i, len; | ||
| 72 | int failed = 1; | ||
| 73 | |||
| 74 | if ((abs = ASN1_BIT_STRING_new()) == NULL) { | ||
| 75 | fprintf(stderr, "FAIL: ASN1_BIT_STRING_new() == NULL\n"); | ||
| 76 | goto failed; | ||
| 77 | } | ||
| 78 | if (!ASN1_BIT_STRING_set(abs, bs, sizeof(bs))) { | ||
| 79 | fprintf(stderr, "FAIL: failed to set bit string\n"); | ||
| 80 | goto failed; | ||
| 81 | } | ||
| 82 | |||
| 83 | if ((len = i2d_ASN1_BIT_STRING(abs, NULL)) < 0) { | ||
| 84 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING with NULL\n"); | ||
| 85 | goto failed; | ||
| 86 | } | ||
| 87 | if ((p = malloc(len)) == NULL) | ||
| 88 | errx(1, "malloc"); | ||
| 89 | memset(p, 0xbd, len); | ||
| 90 | pp = p; | ||
| 91 | if ((i2d_ASN1_BIT_STRING(abs, &pp)) != len) { | ||
| 92 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 93 | goto failed; | ||
| 94 | } | ||
| 95 | |||
| 96 | if (!asn1_compare_bytes("BIT_STRING", p, len, asn1_bit_string_primitive, | ||
| 97 | sizeof(asn1_bit_string_primitive))) | ||
| 98 | goto failed; | ||
| 99 | |||
| 100 | /* Test primitive decoding. */ | ||
| 101 | q = p; | ||
| 102 | if (d2i_ASN1_BIT_STRING(&abs, &q, len) == NULL) { | ||
| 103 | fprintf(stderr, "FAIL: d2i_ASN1_BIT_STRING primitive\n"); | ||
| 104 | goto failed; | ||
| 105 | } | ||
| 106 | if (!asn1_compare_bytes("BIT_STRING primitive data", abs->data, abs->length, | ||
| 107 | bs, sizeof(bs))) | ||
| 108 | goto failed; | ||
| 109 | |||
| 110 | /* Test ASN1_BIT_STRING_get_bit(). */ | ||
| 111 | for (i = 0; i < ((int)sizeof(bs) * 8); i++) { | ||
| 112 | bit = (bs[i / 8] >> (7 - i % 8)) & 1; | ||
| 113 | |||
| 114 | if (ASN1_BIT_STRING_get_bit(abs, i) != bit) { | ||
| 115 | fprintf(stderr, "FAIL: ASN1_BIT_STRING_get_bit(_, %d) " | ||
| 116 | "= %d, want %d\n", i, | ||
| 117 | ASN1_BIT_STRING_get_bit(abs, i), bit); | ||
| 118 | goto failed; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | /* Test ASN1_BIT_STRING_set_bit(). */ | ||
| 123 | for (i = 0; i < ((int)sizeof(bs) * 8); i++) { | ||
| 124 | if (!ASN1_BIT_STRING_set_bit(abs, i, 1)) { | ||
| 125 | fprintf(stderr, "FAIL: ASN1_BIT_STRING_set_bit 1\n"); | ||
| 126 | goto failed; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | for (i = ((int)sizeof(bs) * 8) - 1; i >= 0; i--) { | ||
| 130 | bit = (bs[i / 8] >> (7 - i % 8)) & 1; | ||
| 131 | if (bit == 1) | ||
| 132 | continue; | ||
| 133 | if (!ASN1_BIT_STRING_set_bit(abs, i, 0)) { | ||
| 134 | fprintf(stderr, "FAIL: ASN1_BIT_STRING_set_bit\n"); | ||
| 135 | goto failed; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | if ((i2d_ASN1_BIT_STRING(abs, NULL)) != len) { | ||
| 140 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 141 | goto failed; | ||
| 142 | } | ||
| 143 | |||
| 144 | memset(p, 0xbd, len); | ||
| 145 | pp = p; | ||
| 146 | if ((i2d_ASN1_BIT_STRING(abs, &pp)) != len) { | ||
| 147 | fprintf(stderr, "FAIL: i2d_ASN1_BIT_STRING\n"); | ||
| 148 | goto failed; | ||
| 149 | } | ||
| 150 | |||
| 151 | if (!asn1_compare_bytes("BIT_STRING set", p, len, asn1_bit_string_primitive, | ||
| 152 | sizeof(asn1_bit_string_primitive))) | ||
| 153 | goto failed; | ||
| 154 | |||
| 155 | failed = 0; | ||
| 156 | |||
| 157 | failed: | ||
| 158 | ASN1_BIT_STRING_free(abs); | ||
| 159 | free(p); | ||
| 160 | |||
| 161 | return failed; | ||
| 162 | } | ||
| 163 | |||
| 55 | const uint8_t asn1_boolean_false[] = { | 164 | const uint8_t asn1_boolean_false[] = { |
| 56 | 0x01, 0x01, 0x00, | 165 | 0x01, 0x01, 0x00, |
| 57 | }; | 166 | }; |
| @@ -71,8 +180,9 @@ asn1_boolean_test(void) | |||
| 71 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false with NULL\n"); | 180 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false with NULL\n"); |
| 72 | goto failed; | 181 | goto failed; |
| 73 | } | 182 | } |
| 74 | if ((p = calloc(1, len)) == NULL) | 183 | if ((p = malloc(len)) == NULL) |
| 75 | errx(1, "calloc"); | 184 | errx(1, "calloc"); |
| 185 | memset(p, 0xbd, len); | ||
| 76 | pp = p; | 186 | pp = p; |
| 77 | if ((i2d_ASN1_BOOLEAN(0, &pp)) != len) { | 187 | if ((i2d_ASN1_BOOLEAN(0, &pp)) != len) { |
| 78 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false\n"); | 188 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false\n"); |
| @@ -127,6 +237,7 @@ main(int argc, char **argv) | |||
| 127 | { | 237 | { |
| 128 | int failed = 0; | 238 | int failed = 0; |
| 129 | 239 | ||
| 240 | failed |= asn1_bit_string_test(); | ||
| 130 | failed |= asn1_boolean_test(); | 241 | failed |= asn1_boolean_test(); |
| 131 | 242 | ||
| 132 | return (failed); | 243 | return (failed); |
