diff options
| author | jsing <> | 2022-04-28 18:36:38 +0000 |
|---|---|---|
| committer | jsing <> | 2022-04-28 18:36:38 +0000 |
| commit | bd169e0415ec7e637a065aa58f5a3f922080bd81 (patch) | |
| tree | 99f2febe2b3073014226877549caa9f62ed60651 /src | |
| parent | 05884faa77375103b83e2804adf80d2daadbca40 (diff) | |
| download | openbsd-bd169e0415ec7e637a065aa58f5a3f922080bd81.tar.gz openbsd-bd169e0415ec7e637a065aa58f5a3f922080bd81.tar.bz2 openbsd-bd169e0415ec7e637a065aa58f5a3f922080bd81.zip | |
Add tests that decode sequences into ASN.1 strings.
Test decoding of sequences with length and indefinite length into an ASN.1
string - in this case the ASN.1 is not decoded, rather the octets are
stored directly as the content of the string.
This exercises a specific path through the ASN.1 decoder.
(you know asn1complex is living up to its name when you have to import
openssl/asn1t.h directly...)
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1complex.c | 98 |
1 files changed, 97 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/asn1complex.c b/src/regress/lib/libcrypto/asn1/asn1complex.c index dfad345c6d..e75ebc753e 100644 --- a/src/regress/lib/libcrypto/asn1/asn1complex.c +++ b/src/regress/lib/libcrypto/asn1/asn1complex.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: asn1complex.c,v 1.2 2022/04/27 17:43:41 jsing Exp $ */ | 1 | /* $OpenBSD: asn1complex.c,v 1.3 2022/04/28 18:36:38 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 | * |
| @@ -16,6 +16,7 @@ | |||
| 16 | */ | 16 | */ |
| 17 | 17 | ||
| 18 | #include <openssl/asn1.h> | 18 | #include <openssl/asn1.h> |
| 19 | #include <openssl/asn1t.h> | ||
| 19 | #include <openssl/err.h> | 20 | #include <openssl/err.h> |
| 20 | 21 | ||
| 21 | #include <err.h> | 22 | #include <err.h> |
| @@ -217,12 +218,107 @@ do_asn1_constructed_tests(void) | |||
| 217 | return failed; | 218 | return failed; |
| 218 | } | 219 | } |
| 219 | 220 | ||
| 221 | /* Sequence with length. */ | ||
| 222 | const uint8_t asn1_sequence_ber[] = { | ||
| 223 | 0x30, 0x16, | ||
| 224 | 0x04, 0x01, 0x01, | ||
| 225 | 0x04, 0x02, 0x01, 0x02, | ||
| 226 | 0x04, 0x03, 0x01, 0x02, 0x03, | ||
| 227 | 0x30, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00, | ||
| 228 | 0x04, 0x01, 0x01, | ||
| 229 | |||
| 230 | 0x04, 0x01, 0x01, /* Trailing data. */ | ||
| 231 | }; | ||
| 232 | |||
| 233 | const uint8_t asn1_sequence_content[] = { | ||
| 234 | 0x30, 0x16, 0x04, 0x01, 0x01, 0x04, 0x02, 0x01, | ||
| 235 | 0x02, 0x04, 0x03, 0x01, 0x02, 0x03, 0x30, 0x80, | ||
| 236 | 0x04, 0x01, 0x01, 0x00, 0x00, 0x04, 0x01, 0x01, | ||
| 237 | }; | ||
| 238 | |||
| 239 | /* Sequence with indefinite length. */ | ||
| 240 | const uint8_t asn1_sequence_indefinite_ber[] = { | ||
| 241 | 0x30, 0x80, | ||
| 242 | 0x04, 0x01, 0x01, | ||
| 243 | 0x04, 0x02, 0x01, 0x02, | ||
| 244 | 0x04, 0x03, 0x01, 0x02, 0x03, | ||
| 245 | 0x30, 0x80, 0x04, 0x01, 0x01, 0x00, 0x00, | ||
| 246 | 0x04, 0x01, 0x01, | ||
| 247 | 0x00, 0x00, | ||
| 248 | |||
| 249 | 0x04, 0x01, 0x01, /* Trailing data. */ | ||
| 250 | }; | ||
| 251 | |||
| 252 | const uint8_t asn1_sequence_indefinite_content[] = { | ||
| 253 | 0x30, 0x80, 0x04, 0x01, 0x01, 0x04, 0x02, 0x01, | ||
| 254 | 0x02, 0x04, 0x03, 0x01, 0x02, 0x03, 0x30, 0x80, | ||
| 255 | 0x04, 0x01, 0x01, 0x00, 0x00, 0x04, 0x01, 0x01, | ||
| 256 | 0x00, 0x00, | ||
| 257 | }; | ||
| 258 | |||
| 259 | static int | ||
| 260 | do_asn1_sequence_string_tests(void) | ||
| 261 | { | ||
| 262 | ASN1_STRING *astr = NULL; | ||
| 263 | const uint8_t *p; | ||
| 264 | long len; | ||
| 265 | int failed = 1; | ||
| 266 | |||
| 267 | ERR_clear_error(); | ||
| 268 | |||
| 269 | /* | ||
| 270 | * Test decoding of sequence with length and indefinite length into | ||
| 271 | * a string - in this case the ASN.1 is not decoded and is stored | ||
| 272 | * directly as the content for the string. | ||
| 273 | */ | ||
| 274 | if ((astr = ASN1_STRING_new()) == NULL) { | ||
| 275 | fprintf(stderr, "FAIL: ASN1_STRING_new() returned NULL\n"); | ||
| 276 | goto failed; | ||
| 277 | } | ||
| 278 | |||
| 279 | p = asn1_sequence_ber; | ||
| 280 | len = sizeof(asn1_sequence_ber); | ||
| 281 | if (ASN1_item_d2i((ASN1_VALUE **)&astr, &p, len, | ||
| 282 | &ASN1_SEQUENCE_it) == NULL) { | ||
| 283 | fprintf(stderr, "FAIL: failed to decode ASN1_SEQUENCE\n"); | ||
| 284 | ERR_print_errors_fp(stderr); | ||
| 285 | goto failed; | ||
| 286 | } | ||
| 287 | |||
| 288 | if (!asn1_compare_bytes("sequence", ASN1_STRING_data(astr), | ||
| 289 | ASN1_STRING_length(astr), asn1_sequence_content, | ||
| 290 | sizeof(asn1_sequence_content))) | ||
| 291 | goto failed; | ||
| 292 | |||
| 293 | p = asn1_sequence_indefinite_ber; | ||
| 294 | len = sizeof(asn1_sequence_indefinite_ber); | ||
| 295 | if (ASN1_item_d2i((ASN1_VALUE **)&astr, &p, len, | ||
| 296 | &ASN1_SEQUENCE_it) == NULL) { | ||
| 297 | fprintf(stderr, "FAIL: failed to decode ASN1_SEQUENCE\n"); | ||
| 298 | ERR_print_errors_fp(stderr); | ||
| 299 | goto failed; | ||
| 300 | } | ||
| 301 | |||
| 302 | if (!asn1_compare_bytes("sequence indefinite", ASN1_STRING_data(astr), | ||
| 303 | ASN1_STRING_length(astr), asn1_sequence_indefinite_content, | ||
| 304 | sizeof(asn1_sequence_indefinite_content))) | ||
| 305 | goto failed; | ||
| 306 | |||
| 307 | failed = 0; | ||
| 308 | |||
| 309 | failed: | ||
| 310 | ASN1_STRING_free(astr); | ||
| 311 | |||
| 312 | return failed; | ||
| 313 | } | ||
| 314 | |||
| 220 | int | 315 | int |
| 221 | main(int argc, char **argv) | 316 | main(int argc, char **argv) |
| 222 | { | 317 | { |
| 223 | int failed = 0; | 318 | int failed = 0; |
| 224 | 319 | ||
| 225 | failed |= do_asn1_constructed_tests(); | 320 | failed |= do_asn1_constructed_tests(); |
| 321 | failed |= do_asn1_sequence_string_tests(); | ||
| 226 | 322 | ||
| 227 | return (failed); | 323 | return (failed); |
| 228 | } | 324 | } |
