diff options
author | jsing <> | 2017-11-28 16:47:55 +0000 |
---|---|---|
committer | jsing <> | 2017-11-28 16:47:55 +0000 |
commit | 9d2e79481e8236db55e7cef6b5df7e7a5c9f021b (patch) | |
tree | ccbbf2fa20269e35f7d8514ba293e726e2a9db65 | |
parent | 87fa230da49456f81bbf5b5f65e35d79e48f9664 (diff) | |
download | openbsd-9d2e79481e8236db55e7cef6b5df7e7a5c9f021b.tar.gz openbsd-9d2e79481e8236db55e7cef6b5df7e7a5c9f021b.tar.bz2 openbsd-9d2e79481e8236db55e7cef6b5df7e7a5c9f021b.zip |
Add regress coverage for ASN1_TYPE_{get,set}_int_octetstring()
-rw-r--r-- | src/regress/lib/libcrypto/asn1/Makefile | 6 | ||||
-rw-r--r-- | src/regress/lib/libcrypto/asn1/asn1evp.c | 143 |
2 files changed, 147 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/asn1/Makefile b/src/regress/lib/libcrypto/asn1/Makefile index d4da1bf7a9..abb69cb379 100644 --- a/src/regress/lib/libcrypto/asn1/Makefile +++ b/src/regress/lib/libcrypto/asn1/Makefile | |||
@@ -1,7 +1,9 @@ | |||
1 | # $OpenBSD: Makefile,v 1.2 2015/09/29 04:54:23 beck Exp $ | 1 | # $OpenBSD: Makefile,v 1.3 2017/11/28 16:47:55 jsing Exp $ |
2 | 2 | ||
3 | TESTS = \ | 3 | TESTS = \ |
4 | asn1time rfc5280time | 4 | asn1evp \ |
5 | asn1time \ | ||
6 | rfc5280time | ||
5 | 7 | ||
6 | REGRESS_TARGETS= all_tests | 8 | REGRESS_TARGETS= all_tests |
7 | 9 | ||
diff --git a/src/regress/lib/libcrypto/asn1/asn1evp.c b/src/regress/lib/libcrypto/asn1/asn1evp.c new file mode 100644 index 0000000000..dd87bbb020 --- /dev/null +++ b/src/regress/lib/libcrypto/asn1/asn1evp.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /* $OpenBSD: asn1evp.c,v 1.1 2017/11/28 16:47:55 jsing Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2017 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 <limits.h> | ||
19 | #include <stdio.h> | ||
20 | #include <string.h> | ||
21 | |||
22 | #include <openssl/asn1.h> | ||
23 | |||
24 | unsigned char asn1_atios[] = { | ||
25 | 0x30, 0x14, 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, | ||
26 | 0xff, 0xff, 0xff, 0xff, 0x04, 0x08, 0x00, 0x01, | ||
27 | 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
28 | }; | ||
29 | |||
30 | unsigned char test_octetstring[] = { | ||
31 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | ||
32 | }; | ||
33 | |||
34 | static void | ||
35 | hexdump(const unsigned char *buf, size_t len) | ||
36 | { | ||
37 | size_t i; | ||
38 | |||
39 | for (i = 1; i <= len; i++) | ||
40 | fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n"); | ||
41 | |||
42 | fprintf(stderr, "\n"); | ||
43 | } | ||
44 | |||
45 | static int | ||
46 | compare_data(const char *label, const unsigned char *d1, size_t d1_len, | ||
47 | const unsigned char *d2, size_t d2_len) | ||
48 | { | ||
49 | if (d1_len != d2_len) { | ||
50 | fprintf(stderr, "FAIL: got %s with length %zu, want %zu\n", | ||
51 | label, d1_len, d2_len); | ||
52 | return -1; | ||
53 | } | ||
54 | if (memcmp(d1, d2, d1_len) != 0) { | ||
55 | fprintf(stderr, "FAIL: %s differs\n", label); | ||
56 | fprintf(stderr, "got:\n"); | ||
57 | hexdump(d1, d1_len); | ||
58 | fprintf(stderr, "want:\n"); | ||
59 | hexdump(d2, d2_len); | ||
60 | return -1; | ||
61 | } | ||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | int | ||
66 | main(int argc, char **argv) | ||
67 | { | ||
68 | unsigned char data[16]; | ||
69 | long num = LONG_MAX; | ||
70 | int failed = 1; | ||
71 | ASN1_TYPE at; | ||
72 | int len; | ||
73 | |||
74 | memset(&at, 0, sizeof(at)); | ||
75 | |||
76 | if (!ASN1_TYPE_set_int_octetstring(&at, num, test_octetstring, | ||
77 | sizeof(test_octetstring))) { | ||
78 | fprintf(stderr, "FAIL: ASN1_TYPE_set_int_octetstring failed\n"); | ||
79 | goto done; | ||
80 | } | ||
81 | if (at.type != V_ASN1_SEQUENCE) { | ||
82 | fprintf(stderr, "FAIL: not a V_ASN1_SEQUENCE (%i != %i)\n", | ||
83 | at.type, V_ASN1_SEQUENCE); | ||
84 | goto done; | ||
85 | } | ||
86 | if (at.value.sequence->type != V_ASN1_OCTET_STRING) { | ||
87 | fprintf(stderr, "FAIL: not a V_ASN1_OCTET_STRING (%i != %i)\n", | ||
88 | at.type, V_ASN1_OCTET_STRING); | ||
89 | goto done; | ||
90 | } | ||
91 | if (compare_data("sequence", at.value.sequence->data, | ||
92 | at.value.sequence->length, asn1_atios, sizeof(asn1_atios)) == -1) | ||
93 | goto done; | ||
94 | |||
95 | memset(&data, 0, sizeof(data)); | ||
96 | num = 0; | ||
97 | |||
98 | if ((len = ASN1_TYPE_get_int_octetstring(&at, &num, data, | ||
99 | sizeof(data))) < 0) { | ||
100 | fprintf(stderr, "FAIL: ASN1_TYPE_get_int_octetstring failed\n"); | ||
101 | goto done; | ||
102 | } | ||
103 | if (num != LONG_MAX) { | ||
104 | fprintf(stderr, "FAIL: got num %li, want %li\n", num, LONG_MAX); | ||
105 | goto done; | ||
106 | } | ||
107 | if (compare_data("octet string", data, len, | ||
108 | test_octetstring, sizeof(test_octetstring)) == -1) | ||
109 | goto done; | ||
110 | if (data[len] != 0) { | ||
111 | fprintf(stderr, "FAIL: octet string overflowed buffer\n"); | ||
112 | goto done; | ||
113 | } | ||
114 | |||
115 | memset(&data, 0, sizeof(data)); | ||
116 | num = 0; | ||
117 | |||
118 | /* With a limit buffer, the output should be truncated... */ | ||
119 | if ((len = ASN1_TYPE_get_int_octetstring(&at, &num, data, 4)) < 0) { | ||
120 | fprintf(stderr, "FAIL: ASN1_TYPE_get_int_octetstring failed\n"); | ||
121 | goto done; | ||
122 | } | ||
123 | if (num != LONG_MAX) { | ||
124 | fprintf(stderr, "FAIL: got num %li, want %li\n", num, LONG_MAX); | ||
125 | goto done; | ||
126 | } | ||
127 | if (len != sizeof(test_octetstring)) { | ||
128 | fprintf(stderr, "FAIL: got length mismatch (%i != %zu)\n", | ||
129 | len, sizeof(test_octetstring)); | ||
130 | goto done; | ||
131 | } | ||
132 | if (compare_data("octet string", data, 4, test_octetstring, 4) == -1) | ||
133 | goto done; | ||
134 | if (data[4] != 0) { | ||
135 | fprintf(stderr, "FAIL: octet string overflowed buffer\n"); | ||
136 | goto done; | ||
137 | } | ||
138 | |||
139 | failed = 0; | ||
140 | |||
141 | done: | ||
142 | return failed; | ||
143 | } | ||