diff options
| author | jsing <> | 2021-12-09 16:30:05 +0000 |
|---|---|---|
| committer | jsing <> | 2021-12-09 16:30:05 +0000 |
| commit | de9d5284a8abf50d04e00e922c4137a782feb12a (patch) | |
| tree | 73779060747d26c7f49c4048e8a3043f135a63d3 /src | |
| parent | 768b3a9bead901037968ccf53dd89ab5b64be269 (diff) | |
| download | openbsd-de9d5284a8abf50d04e00e922c4137a782feb12a.tar.gz openbsd-de9d5284a8abf50d04e00e922c4137a782feb12a.tar.bz2 openbsd-de9d5284a8abf50d04e00e922c4137a782feb12a.zip | |
Add initial tests for coverage of ASN.1 basic/primitive types.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/asn1/Makefile | 3 | ||||
| -rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1basic.c | 133 |
2 files changed, 135 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile index 4b47bfdc2b..046d85bc81 100644 --- a/src/regress/lib/libcrypto/asn1/Makefile +++ b/src/regress/lib/libcrypto/asn1/Makefile | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.7 2021/11/30 07:34:29 jsing Exp $ | 1 | # $OpenBSD: Makefile,v 1.8 2021/12/09 16:30:05 jsing Exp $ |
| 2 | 2 | ||
| 3 | TESTS = \ | 3 | TESTS = \ |
| 4 | asn1basic \ | ||
| 4 | asn1evp \ | 5 | asn1evp \ |
| 5 | asn1string_copy \ | 6 | asn1string_copy \ |
| 6 | asn1time \ | 7 | asn1time \ |
diff --git a/src/regress/lib/libcrypto/asn1/asn1basic.c b/src/regress/lib/libcrypto/asn1/asn1basic.c new file mode 100644 index 0000000000..d2a1fc97ba --- /dev/null +++ b/src/regress/lib/libcrypto/asn1/asn1basic.c | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | /* $OpenBSD: asn1basic.c,v 1.1 2021/12/09 16:30:05 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2017, 2021 Joel Sing <jsing@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <openssl/asn1.h> | ||
| 19 | |||
| 20 | #include <err.h> | ||
| 21 | #include <stdio.h> | ||
| 22 | #include <string.h> | ||
| 23 | |||
| 24 | static void | ||
| 25 | hexdump(const unsigned char *buf, size_t len) | ||
| 26 | { | ||
| 27 | size_t i; | ||
| 28 | |||
| 29 | for (i = 1; i <= len; i++) | ||
| 30 | fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n"); | ||
| 31 | |||
| 32 | fprintf(stderr, "\n"); | ||
| 33 | } | ||
| 34 | |||
| 35 | static int | ||
| 36 | asn1_compare_bytes(const char *label, const unsigned char *d1, int len1, | ||
| 37 | const unsigned char *d2, int len2) | ||
| 38 | { | ||
| 39 | if (len1 != len2) { | ||
| 40 | fprintf(stderr, "FAIL: %s - byte lengths differ " | ||
| 41 | "(%i != %i)\n", label, len1, len2); | ||
| 42 | return 0; | ||
| 43 | } | ||
| 44 | if (memcmp(d1, d2, len1) != 0) { | ||
| 45 | fprintf(stderr, "FAIL: %s - bytes differ\n", label); | ||
| 46 | fprintf(stderr, "Got:\n"); | ||
| 47 | hexdump(d1, len1); | ||
| 48 | fprintf(stderr, "Want:\n"); | ||
| 49 | hexdump(d2, len2); | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | const uint8_t asn1_boolean_false[] = { | ||
| 56 | 0x01, 0x01, 0x00, | ||
| 57 | }; | ||
| 58 | const uint8_t asn1_boolean_true[] = { | ||
| 59 | 0x01, 0x01, 0x01, | ||
| 60 | }; | ||
| 61 | |||
| 62 | static int | ||
| 63 | asn1_boolean_test(void) | ||
| 64 | { | ||
| 65 | uint8_t *p = NULL, *pp; | ||
| 66 | const uint8_t *q; | ||
| 67 | int len; | ||
| 68 | int failed = 1; | ||
| 69 | |||
| 70 | if ((len = i2d_ASN1_BOOLEAN(0, NULL)) < 0) { | ||
| 71 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false with NULL\n"); | ||
| 72 | goto failed; | ||
| 73 | } | ||
| 74 | if ((p = calloc(1, len)) == NULL) | ||
| 75 | errx(1, "calloc"); | ||
| 76 | pp = p; | ||
| 77 | if ((i2d_ASN1_BOOLEAN(0, &pp)) != len) { | ||
| 78 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN false\n"); | ||
| 79 | goto failed; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (!asn1_compare_bytes("BOOLEAN false", p, len, asn1_boolean_false, | ||
| 83 | sizeof(asn1_boolean_false))) | ||
| 84 | goto failed; | ||
| 85 | |||
| 86 | q = p; | ||
| 87 | if (d2i_ASN1_BOOLEAN(NULL, &q, len) != 0) { | ||
| 88 | fprintf(stderr, "FAIL: BOOLEAN false did not decode to 0\n"); | ||
| 89 | goto failed; | ||
| 90 | } | ||
| 91 | |||
| 92 | free(p); | ||
| 93 | p = NULL; | ||
| 94 | |||
| 95 | if ((len = i2d_ASN1_BOOLEAN(1, NULL)) < 0) { | ||
| 96 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN true with NULL\n"); | ||
| 97 | goto failed; | ||
| 98 | } | ||
| 99 | if ((p = calloc(1, len)) == NULL) | ||
| 100 | errx(1, "calloc"); | ||
| 101 | pp = p; | ||
| 102 | if ((i2d_ASN1_BOOLEAN(1, &pp)) != len) { | ||
| 103 | fprintf(stderr, "FAIL: i2d_ASN1_BOOLEAN true\n"); | ||
| 104 | goto failed; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (!asn1_compare_bytes("BOOLEAN true", p, len, asn1_boolean_true, | ||
| 108 | sizeof(asn1_boolean_true))) | ||
| 109 | goto failed; | ||
| 110 | |||
| 111 | q = p; | ||
| 112 | if (d2i_ASN1_BOOLEAN(NULL, &q, len) != 1) { | ||
| 113 | fprintf(stderr, "FAIL: BOOLEAN true did not decode to 0\n"); | ||
| 114 | goto failed; | ||
| 115 | } | ||
| 116 | |||
| 117 | failed = 0; | ||
| 118 | |||
| 119 | failed: | ||
| 120 | free(p); | ||
| 121 | |||
| 122 | return failed; | ||
| 123 | } | ||
| 124 | |||
| 125 | int | ||
| 126 | main(int argc, char **argv) | ||
| 127 | { | ||
| 128 | int failed = 0; | ||
| 129 | |||
| 130 | failed |= asn1_boolean_test(); | ||
| 131 | |||
| 132 | return (failed); | ||
| 133 | } | ||
