summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2021-12-04 05:15:09 +0000
committerjsing <>2021-12-04 05:15:09 +0000
commit0121f925f58e525d0c2c34fdf6b3d69a72616ee2 (patch)
tree8c7096e13ad527edb366951953926282fb1f50c4 /src
parent70e457ecdac2e754c7695e07db8ede5dbd075676 (diff)
downloadopenbsd-0121f925f58e525d0c2c34fdf6b3d69a72616ee2.tar.gz
openbsd-0121f925f58e525d0c2c34fdf6b3d69a72616ee2.tar.bz2
openbsd-0121f925f58e525d0c2c34fdf6b3d69a72616ee2.zip
Add regress for ECPKParameters ASN.1 encoding/decoding.
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/ec/Makefile6
-rw-r--r--src/regress/lib/libcrypto/ec/ec_asn1_test.c206
2 files changed, 210 insertions, 2 deletions
diff --git a/src/regress/lib/libcrypto/ec/Makefile b/src/regress/lib/libcrypto/ec/Makefile
index 394ac51226..59f5077bd2 100644
--- a/src/regress/lib/libcrypto/ec/Makefile
+++ b/src/regress/lib/libcrypto/ec/Makefile
@@ -1,6 +1,7 @@
1# $OpenBSD: Makefile,v 1.5 2021/04/21 20:15:08 tb Exp $ 1# $OpenBSD: Makefile,v 1.6 2021/12/04 05:15:09 jsing Exp $
2 2
3PROGS += ectest 3PROGS += ectest
4PROGS += ec_asn1_test
4PROGS += ec_point_conversion 5PROGS += ec_point_conversion
5 6
6.for t in ${PROGS} 7.for t in ${PROGS}
@@ -10,7 +11,8 @@ REGRESS_TARGETS += run-$t
10LDADD = ${CRYPTO_INT} 11LDADD = ${CRYPTO_INT}
11DPADD = ${LIBCRYPTO} 12DPADD = ${LIBCRYPTO}
12WARNINGS = Yes 13WARNINGS = Yes
13CFLAGS += -DLIBRESSL_INTERNAL -Wall -Wundef -Werror 14CFLAGS += -DLIBRESSL_CRYPTO_INTERNAL -DLIBRESSL_INTERNAL
15CFLAGS += -Wall -Wundef -Werror
14 16
15CLEANFILES += ${PROGS} 17CLEANFILES += ${PROGS}
16 18
diff --git a/src/regress/lib/libcrypto/ec/ec_asn1_test.c b/src/regress/lib/libcrypto/ec/ec_asn1_test.c
new file mode 100644
index 0000000000..a9b7abaa26
--- /dev/null
+++ b/src/regress/lib/libcrypto/ec/ec_asn1_test.c
@@ -0,0 +1,206 @@
1/* $OpenBSD: ec_asn1_test.c,v 1.1 2021/12/04 05:15:09 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 <err.h>
19#include <string.h>
20
21#include <openssl/bio.h>
22#include <openssl/ec.h>
23#include <openssl/objects.h>
24
25const uint8_t ec_secp256r1_pkparameters_named_curve[] = {
26 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03,
27 0x01, 0x07,
28};
29
30const uint8_t ec_secp256r1_pkparameters_parameters[] = {
31 0x30, 0x81, 0xf7, 0x02, 0x01, 0x01, 0x30, 0x2c,
32 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01,
33 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff,
34 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
37 0xff, 0xff, 0xff, 0xff, 0x30, 0x5b, 0x04, 0x20,
38 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
41 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
42 0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a,
43 0x93, 0xe7, 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98,
44 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
45 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2,
46 0x60, 0x4b, 0x03, 0x15, 0x00, 0xc4, 0x9d, 0x36,
47 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78,
48 0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e,
49 0x90, 0x04, 0x41, 0x04, 0x6b, 0x17, 0xd1, 0xf2,
50 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5,
51 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81,
52 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45,
53 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2,
54 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a,
55 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
56 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
57 0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff,
58 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff,
59 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc,
60 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3,
61 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51, 0x02,
62 0x01, 0x01,
63};
64
65static void
66hexdump(const unsigned char *buf, size_t len)
67{
68 size_t i;
69
70 for (i = 1; i <= len; i++)
71 fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "" : "\n");
72
73 fprintf(stderr, "\n");
74}
75
76static int
77compare_data(const char *label, const unsigned char *d1, size_t d1_len,
78 const unsigned char *d2, size_t d2_len)
79{
80 if (d1_len != d2_len) {
81 fprintf(stderr, "FAIL: got %s with length %zu, want %zu\n",
82 label, d1_len, d2_len);
83 return -1;
84 }
85 if (memcmp(d1, d2, d1_len) != 0) {
86 fprintf(stderr, "FAIL: %sdiffer\n", label);
87 fprintf(stderr, "got:\n");
88 hexdump(d1, d1_len);
89 fprintf(stderr, "want:\n");
90 hexdump(d2, d2_len);
91 return -1;
92 }
93 return 0;
94}
95
96static int
97ec_group_pkparameters_test(const char *label, int asn1_flag,
98 const uint8_t *test_data, size_t test_data_len)
99{
100 EC_GROUP *group_a = NULL, *group_b = NULL;
101 unsigned char *out = NULL, *data = NULL;
102 const unsigned char *p;
103 BIO *bio_mem = NULL;
104 int failure = 1;
105 int len;
106
107 /*
108 * Test i2d_ECPKParameters/d2i_ECPKParameters.
109 */
110 if ((group_a = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)) == NULL)
111 errx(1, "failed to create EC_GROUP");
112
113 EC_GROUP_set_asn1_flag(group_a, asn1_flag);
114
115 if ((len = i2d_ECPKParameters(group_a, &out)) < 0) {
116 fprintf(stderr, "FAIL: i2d_ECPKParameters failed\n");
117 goto done;
118 }
119 if (compare_data(label, out, len, test_data, test_data_len) == -1)
120 goto done;
121
122 p = out;
123 if ((group_b = d2i_ECPKParameters(NULL, &p, len)) == NULL) {
124 fprintf(stderr, "FAIL: d2i_ECPKParameters failed\n");
125 goto done;
126 }
127
128 if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
129 fprintf(stderr, "FAIL: EC_GROUPs do not match!\n");
130 goto done;
131 }
132
133 p = out;
134 if ((group_a = d2i_ECPKParameters(&group_a, &p, len)) == NULL) {
135 fprintf(stderr, "FAIL: d2i_ECPKParameters failed\n");
136 goto done;
137 }
138
139 if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
140 fprintf(stderr, "FAIL: EC_GROUPs do not match!\n");
141 goto done;
142 }
143
144 /*
145 * Test i2d_ECPKParameters_bio/d2i_ECPKParameters_bio.
146 */
147 if ((bio_mem = BIO_new(BIO_s_mem())) == NULL)
148 errx(1, "BIO_new failed for BIO_s_mem");
149
150 if ((len = i2d_ECPKParameters_bio(bio_mem, group_a)) < 0) {
151 fprintf(stderr, "FAIL: i2d_ECPKParameters_bio failed\n");
152 goto done;
153 }
154
155 len = BIO_get_mem_data(bio_mem, &data);
156 if (compare_data(label, out, len, test_data, test_data_len) == -1)
157 goto done;
158
159 EC_GROUP_free(group_b);
160 if ((group_b = d2i_ECPKParameters_bio(bio_mem, NULL)) == NULL) {
161 fprintf(stderr, "FAIL: d2i_ECPKParameters_bio failed\n");
162 goto done;
163 }
164
165 if (EC_GROUP_cmp(group_a, group_b, NULL) != 0) {
166 fprintf(stderr, "FAIL: EC_GROUPs do not match!\n");
167 goto done;
168 }
169
170 failure = 0;
171
172 done:
173 BIO_free_all(bio_mem);
174 EC_GROUP_free(group_a);
175 EC_GROUP_free(group_b);
176 free(out);
177
178 return (failure);
179}
180
181static int
182ec_group_pkparameters_named_curve_test(void)
183{
184 return ec_group_pkparameters_test("ECPKPARAMETERS named curve",
185 OPENSSL_EC_NAMED_CURVE, ec_secp256r1_pkparameters_named_curve,
186 sizeof(ec_secp256r1_pkparameters_named_curve));
187}
188
189static int
190ec_group_pkparameters_parameters_test(void)
191{
192 return ec_group_pkparameters_test("ECPKPARAMETERS parameters",
193 OPENSSL_EC_EXPLICIT_CURVE, ec_secp256r1_pkparameters_parameters,
194 sizeof(ec_secp256r1_pkparameters_parameters));
195}
196
197int
198main(int argc, char **argv)
199{
200 int failed = 0;
201
202 failed |= ec_group_pkparameters_named_curve_test();
203 failed |= ec_group_pkparameters_parameters_test();
204
205 return (failed);
206}