diff options
Diffstat (limited to 'crypto/bytestring/bytestring_test.c')
-rw-r--r-- | crypto/bytestring/bytestring_test.c | 169 |
1 files changed, 100 insertions, 69 deletions
diff --git a/crypto/bytestring/bytestring_test.c b/crypto/bytestring/bytestring_test.c index 20ce571..e4afccd 100644 --- a/crypto/bytestring/bytestring_test.c +++ b/crypto/bytestring/bytestring_test.c @@ -15,10 +15,13 @@ #include <stdio.h> #include <stdlib.h> +#include <openssl/crypto.h> #include <openssl/bytestring.h> +#include "internal.h" -static int test_skip() { + +static int test_skip(void) { static const uint8_t kData[] = {1, 2, 3}; CBS data; @@ -31,7 +34,7 @@ static int test_skip() { !CBS_skip(&data, 1); } -static int test_get_u() { +static int test_get_u(void) { static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; uint8_t u8; uint16_t u16; @@ -50,7 +53,7 @@ static int test_get_u() { !CBS_get_u8(&data, &u8); } -static int test_get_prefixed() { +static int test_get_prefixed(void) { static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1}; uint8_t u8; uint16_t u16; @@ -72,7 +75,7 @@ static int test_get_prefixed() { u32 == 0x30201; } -static int test_get_prefixed_bad() { +static int test_get_prefixed_bad(void) { static const uint8_t kData1[] = {2, 1}; static const uint8_t kData2[] = {0, 2, 1}; static const uint8_t kData3[] = {0, 0, 2, 1}; @@ -96,7 +99,7 @@ static int test_get_prefixed_bad() { return 1; } -static int test_get_asn1() { +static int test_get_asn1(void) { static const uint8_t kData1[] = {0x30, 2, 1, 2}; static const uint8_t kData2[] = {0x30, 3, 1, 2}; static const uint8_t kData3[] = {0x30, 0x80}; @@ -145,62 +148,7 @@ static int test_get_asn1() { return 1; } -static int test_get_indef() { - static const uint8_t kData1[] = {0x30, 0x80, 0x00, 0x00}; - static const uint8_t kDataWithoutEOC[] = {0x30, 0x80, 0x01, 0x00}; - static const uint8_t kDataWithBadInternalLength[] = {0x30, 0x80, 0x01, 0x01}; - static const uint8_t kDataNested[] = {0x30, 0x80, 0x30, 0x80, 0x30, 0x80, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - static const uint8_t kDataPrimitive[] = {0x02, 0x80, 0x00, 0x00}; - - CBS data, contents; - CBS_init(&data, kData1, sizeof(kData1)); - if (CBS_get_asn1(&data, &contents, 0x30)) { - /* Indefinite lengths should not be supported in DER mode. */ - fprintf(stderr, "Indefinite length parsed by CBS_get_asn1.\n"); - return 0; - } - - if (!CBS_get_asn1_ber(&data, &contents, 0x30) || - CBS_len(&contents) != 0 || - CBS_len(&data) != 0) { - fprintf(stderr, "Simple indefinite length failed.\n"); - return 0; - } - - CBS_init(&data, kDataWithoutEOC, sizeof(kDataWithoutEOC)); - if (CBS_get_asn1_ber(&data, &contents, 0x30)) { - fprintf(stderr, "Parsed without EOC.\n"); - return 0; - } - - CBS_init(&data, kDataWithBadInternalLength, - sizeof(kDataWithBadInternalLength)); - if (CBS_get_asn1_ber(&data, &contents, 0x30)) { - fprintf(stderr, "Parsed with internal length.\n"); - return 0; - } - - CBS_init(&data, kDataNested, sizeof(kDataNested)); - if (!CBS_get_asn1_ber(&data, &contents, 0x30) || - CBS_len(&contents) != 8 || - CBS_len(&data) != 0) { - fprintf(stderr, "Nested indefinite lengths failed.\n"); - return 0; - } - - CBS_init(&data, kDataPrimitive, sizeof(kDataPrimitive)); - if (CBS_get_asn1_ber(&data, &contents, 0x02)) { - /* Indefinite lengths should not be supported for non-constructed - * elements. */ - fprintf(stderr, "Parsed non-constructed element with indefinite length\n"); - return 0; - } - - return 1; -} - -static int test_cbb_basic() { +static int test_cbb_basic(void) { static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8}; uint8_t *buf; size_t buf_len; @@ -226,7 +174,7 @@ static int test_cbb_basic() { return ok; } -static int test_cbb_fixed() { +static int test_cbb_fixed(void) { CBB cbb; uint8_t buf[1]; uint8_t *out_buf; @@ -253,7 +201,7 @@ static int test_cbb_fixed() { return 1; } -static int test_cbb_finish_child() { +static int test_cbb_finish_child(void) { CBB cbb, child; uint8_t *out_buf; size_t out_size; @@ -271,7 +219,7 @@ static int test_cbb_finish_child() { return 1; } -static int test_cbb_prefixed() { +static int test_cbb_prefixed(void) { static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, 4, 5, 6, 5, 4, 1, 0, 1, 2}; uint8_t *buf; @@ -301,7 +249,7 @@ static int test_cbb_prefixed() { return ok; } -static int test_cbb_misuse() { +static int test_cbb_misuse(void) { CBB cbb, child, contents; uint8_t *buf; size_t buf_len; @@ -337,7 +285,7 @@ static int test_cbb_misuse() { return 1; } -static int test_cbb_asn1() { +static int test_cbb_asn1(void) { static const uint8_t kExpected[] = {0x30, 3, 1, 2, 3}; uint8_t *buf, *test_data; size_t buf_len; @@ -405,19 +353,102 @@ static int test_cbb_asn1() { return 1; } -int main() { +static int do_ber_convert(const char *name, + const uint8_t *der_expected, size_t der_len, + const uint8_t *ber, size_t ber_len) { + CBS in; + uint8_t *out; + size_t out_len; + + CBS_init(&in, ber, ber_len); + if (!CBS_asn1_ber_to_der(&in, &out, &out_len)) { + fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name); + return 0; + } + + if (out == NULL) { + if (ber_len != der_len || + memcmp(der_expected, ber, ber_len) != 0) { + fprintf(stderr, "%s: incorrect unconverted result.\n", name); + return 0; + } + + return 1; + } + + if (out_len != der_len || + memcmp(out, der_expected, der_len) != 0) { + fprintf(stderr, "%s: incorrect converted result.\n", name); + return 0; + } + + free(out); + return 1; +} + +static int test_ber_convert(void) { + static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00}; + + /* kIndefBER contains a SEQUENCE with an indefinite length. */ + static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00}; + static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02}; + + /* kOctetStringBER contains an indefinite length OCTETSTRING with two parts. + * These parts need to be concatenated in DER form. */ + static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1, + 0x04, 0x02, 2, 3, 0x00, 0x00}; + static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3}; + + /* kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite + * length elements extensively. */ + static const uint8_t kNSSBER[] = { + 0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, + 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39, + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, + 0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, + 0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, + 0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, + 0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00, + }; + + static const uint8_t kNSSDER[] = { + 0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86, + 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04, + 0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06, + 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84, + 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8, + 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38, + 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0, + 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, + }; + + return do_ber_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER), + kSimpleBER, sizeof(kSimpleBER)) && + do_ber_convert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER, + sizeof(kIndefBER)) && + do_ber_convert("kOctetStringBER", kOctetStringDER, + sizeof(kOctetStringDER), kOctetStringBER, + sizeof(kOctetStringBER)) && + do_ber_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER, + sizeof(kNSSBER)); +} + +int main(void) { + CRYPTO_library_init(); + if (!test_skip() || !test_get_u() || !test_get_prefixed() || !test_get_prefixed_bad() || !test_get_asn1() || - !test_get_indef() || !test_cbb_basic() || !test_cbb_fixed() || !test_cbb_finish_child() || !test_cbb_misuse() || !test_cbb_prefixed() || - !test_cbb_asn1()) { + !test_cbb_asn1() || + !test_ber_convert()) { return 1; } |