1 | /*
|
---|
2 | * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use
|
---|
5 | * this file except in compliance with the License. You can obtain a copy
|
---|
6 | * in the file LICENSE in the source distribution or at
|
---|
7 | * https://www.openssl.org/source/license.html
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <stdio.h>
|
---|
11 | #include <string.h>
|
---|
12 | #include <openssl/x509.h>
|
---|
13 | #include <openssl/x509v3.h>
|
---|
14 | #include <openssl/pem.h>
|
---|
15 | #include <openssl/err.h>
|
---|
16 | #include "internal/nelem.h"
|
---|
17 |
|
---|
18 | #include "testutil.h"
|
---|
19 |
|
---|
20 | static const char *infile;
|
---|
21 |
|
---|
22 | static int test_pathlen(void)
|
---|
23 | {
|
---|
24 | X509 *x = NULL;
|
---|
25 | BIO *b = NULL;
|
---|
26 | long pathlen;
|
---|
27 | int ret = 0;
|
---|
28 |
|
---|
29 | if (!TEST_ptr(b = BIO_new_file(infile, "r"))
|
---|
30 | || !TEST_ptr(x = PEM_read_bio_X509(b, NULL, NULL, NULL))
|
---|
31 | || !TEST_int_eq(pathlen = X509_get_pathlen(x), 6))
|
---|
32 | goto end;
|
---|
33 |
|
---|
34 | ret = 1;
|
---|
35 |
|
---|
36 | end:
|
---|
37 | BIO_free(b);
|
---|
38 | X509_free(x);
|
---|
39 | return ret;
|
---|
40 | }
|
---|
41 |
|
---|
42 | #ifndef OPENSSL_NO_RFC3779
|
---|
43 | static int test_asid(void)
|
---|
44 | {
|
---|
45 | ASN1_INTEGER *val1 = NULL, *val2 = NULL;
|
---|
46 | ASIdentifiers *asid1 = ASIdentifiers_new(), *asid2 = ASIdentifiers_new(),
|
---|
47 | *asid3 = ASIdentifiers_new(), *asid4 = ASIdentifiers_new();
|
---|
48 | int testresult = 0;
|
---|
49 |
|
---|
50 | if (!TEST_ptr(asid1)
|
---|
51 | || !TEST_ptr(asid2)
|
---|
52 | || !TEST_ptr(asid3))
|
---|
53 | goto err;
|
---|
54 |
|
---|
55 | if (!TEST_ptr(val1 = ASN1_INTEGER_new())
|
---|
56 | || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496)))
|
---|
57 | goto err;
|
---|
58 |
|
---|
59 | if (!TEST_true(X509v3_asid_add_id_or_range(asid1, V3_ASID_ASNUM, val1, NULL)))
|
---|
60 | goto err;
|
---|
61 |
|
---|
62 | val1 = NULL;
|
---|
63 | if (!TEST_ptr(val2 = ASN1_INTEGER_new())
|
---|
64 | || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
|
---|
65 | goto err;
|
---|
66 |
|
---|
67 | if (!TEST_true(X509v3_asid_add_id_or_range(asid2, V3_ASID_ASNUM, val2, NULL)))
|
---|
68 | goto err;
|
---|
69 |
|
---|
70 | val2 = NULL;
|
---|
71 | if (!TEST_ptr(val1 = ASN1_INTEGER_new())
|
---|
72 | || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496))
|
---|
73 | || !TEST_ptr(val2 = ASN1_INTEGER_new())
|
---|
74 | || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
|
---|
75 | goto err;
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Just tests V3_ASID_ASNUM for now. Could be extended at some point to also
|
---|
79 | * test V3_ASID_RDI if we think it is worth it.
|
---|
80 | */
|
---|
81 | if (!TEST_true(X509v3_asid_add_id_or_range(asid3, V3_ASID_ASNUM, val1, val2)))
|
---|
82 | goto err;
|
---|
83 | val1 = val2 = NULL;
|
---|
84 |
|
---|
85 | /* Actual subsets */
|
---|
86 | if (!TEST_true(X509v3_asid_subset(NULL, NULL))
|
---|
87 | || !TEST_true(X509v3_asid_subset(NULL, asid1))
|
---|
88 | || !TEST_true(X509v3_asid_subset(asid1, asid1))
|
---|
89 | || !TEST_true(X509v3_asid_subset(asid2, asid2))
|
---|
90 | || !TEST_true(X509v3_asid_subset(asid1, asid3))
|
---|
91 | || !TEST_true(X509v3_asid_subset(asid2, asid3))
|
---|
92 | || !TEST_true(X509v3_asid_subset(asid3, asid3))
|
---|
93 | || !TEST_true(X509v3_asid_subset(asid4, asid1))
|
---|
94 | || !TEST_true(X509v3_asid_subset(asid4, asid2))
|
---|
95 | || !TEST_true(X509v3_asid_subset(asid4, asid3)))
|
---|
96 | goto err;
|
---|
97 |
|
---|
98 | /* Not subsets */
|
---|
99 | if (!TEST_false(X509v3_asid_subset(asid1, NULL))
|
---|
100 | || !TEST_false(X509v3_asid_subset(asid1, asid2))
|
---|
101 | || !TEST_false(X509v3_asid_subset(asid2, asid1))
|
---|
102 | || !TEST_false(X509v3_asid_subset(asid3, asid1))
|
---|
103 | || !TEST_false(X509v3_asid_subset(asid3, asid2))
|
---|
104 | || !TEST_false(X509v3_asid_subset(asid1, asid4))
|
---|
105 | || !TEST_false(X509v3_asid_subset(asid2, asid4))
|
---|
106 | || !TEST_false(X509v3_asid_subset(asid3, asid4)))
|
---|
107 | goto err;
|
---|
108 |
|
---|
109 | testresult = 1;
|
---|
110 | err:
|
---|
111 | ASN1_INTEGER_free(val1);
|
---|
112 | ASN1_INTEGER_free(val2);
|
---|
113 | ASIdentifiers_free(asid1);
|
---|
114 | ASIdentifiers_free(asid2);
|
---|
115 | ASIdentifiers_free(asid3);
|
---|
116 | ASIdentifiers_free(asid4);
|
---|
117 | return testresult;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static struct ip_ranges_st {
|
---|
121 | const unsigned int afi;
|
---|
122 | const char *ip1;
|
---|
123 | const char *ip2;
|
---|
124 | int rorp;
|
---|
125 | } ranges[] = {
|
---|
126 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.1", IPAddressOrRange_addressPrefix},
|
---|
127 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.2", IPAddressOrRange_addressRange},
|
---|
128 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.3", IPAddressOrRange_addressPrefix},
|
---|
129 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.254", IPAddressOrRange_addressRange},
|
---|
130 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.255", IPAddressOrRange_addressPrefix},
|
---|
131 | { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.255", IPAddressOrRange_addressRange},
|
---|
132 | { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.1", IPAddressOrRange_addressPrefix},
|
---|
133 | { IANA_AFI_IPV4, "192.168.0.0", "192.168.255.255", IPAddressOrRange_addressPrefix},
|
---|
134 | { IANA_AFI_IPV4, "192.168.1.0", "192.168.255.255", IPAddressOrRange_addressRange},
|
---|
135 | { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::1", IPAddressOrRange_addressPrefix},
|
---|
136 | { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::2", IPAddressOrRange_addressRange},
|
---|
137 | { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::3", IPAddressOrRange_addressPrefix},
|
---|
138 | { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::fffe", IPAddressOrRange_addressRange},
|
---|
139 | { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::ffff", IPAddressOrRange_addressPrefix},
|
---|
140 | { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::ffff", IPAddressOrRange_addressRange},
|
---|
141 | { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::1", IPAddressOrRange_addressPrefix},
|
---|
142 | { IANA_AFI_IPV6, "2001:0db8::0:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressPrefix},
|
---|
143 | { IANA_AFI_IPV6, "2001:0db8::1:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressRange}
|
---|
144 | };
|
---|
145 |
|
---|
146 | static int check_addr(IPAddrBlocks *addr, int type)
|
---|
147 | {
|
---|
148 | IPAddressFamily *fam;
|
---|
149 | IPAddressOrRange *aorr;
|
---|
150 |
|
---|
151 | if (!TEST_int_eq(sk_IPAddressFamily_num(addr), 1))
|
---|
152 | return 0;
|
---|
153 |
|
---|
154 | fam = sk_IPAddressFamily_value(addr, 0);
|
---|
155 | if (!TEST_ptr(fam))
|
---|
156 | return 0;
|
---|
157 |
|
---|
158 | if (!TEST_int_eq(fam->ipAddressChoice->type, IPAddressChoice_addressesOrRanges))
|
---|
159 | return 0;
|
---|
160 |
|
---|
161 | if (!TEST_int_eq(sk_IPAddressOrRange_num(fam->ipAddressChoice->u.addressesOrRanges), 1))
|
---|
162 | return 0;
|
---|
163 |
|
---|
164 | aorr = sk_IPAddressOrRange_value(fam->ipAddressChoice->u.addressesOrRanges, 0);
|
---|
165 | if (!TEST_ptr(aorr))
|
---|
166 | return 0;
|
---|
167 |
|
---|
168 | if (!TEST_int_eq(aorr->type, type))
|
---|
169 | return 0;
|
---|
170 |
|
---|
171 | return 1;
|
---|
172 | }
|
---|
173 |
|
---|
174 | static int test_addr_ranges(void)
|
---|
175 | {
|
---|
176 | IPAddrBlocks *addr = NULL;
|
---|
177 | ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
|
---|
178 | size_t i;
|
---|
179 | int testresult = 0;
|
---|
180 |
|
---|
181 | for (i = 0; i < OSSL_NELEM(ranges); i++) {
|
---|
182 | addr = sk_IPAddressFamily_new_null();
|
---|
183 | if (!TEST_ptr(addr))
|
---|
184 | goto end;
|
---|
185 | /*
|
---|
186 | * Has the side effect of installing the comparison function onto the
|
---|
187 | * stack.
|
---|
188 | */
|
---|
189 | if (!TEST_true(X509v3_addr_canonize(addr)))
|
---|
190 | goto end;
|
---|
191 |
|
---|
192 | ip1 = a2i_IPADDRESS(ranges[i].ip1);
|
---|
193 | if (!TEST_ptr(ip1))
|
---|
194 | goto end;
|
---|
195 | if (!TEST_true(ip1->length == 4 || ip1->length == 16))
|
---|
196 | goto end;
|
---|
197 | ip2 = a2i_IPADDRESS(ranges[i].ip2);
|
---|
198 | if (!TEST_ptr(ip2))
|
---|
199 | goto end;
|
---|
200 | if (!TEST_int_eq(ip2->length, ip1->length))
|
---|
201 | goto end;
|
---|
202 | if (!TEST_true(memcmp(ip1->data, ip2->data, ip1->length) <= 0))
|
---|
203 | goto end;
|
---|
204 |
|
---|
205 | if (!TEST_true(X509v3_addr_add_range(addr, ranges[i].afi, NULL, ip1->data, ip2->data)))
|
---|
206 | goto end;
|
---|
207 |
|
---|
208 | if (!TEST_true(X509v3_addr_is_canonical(addr)))
|
---|
209 | goto end;
|
---|
210 |
|
---|
211 | if (!check_addr(addr, ranges[i].rorp))
|
---|
212 | goto end;
|
---|
213 |
|
---|
214 | sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
|
---|
215 | addr = NULL;
|
---|
216 | ASN1_OCTET_STRING_free(ip1);
|
---|
217 | ASN1_OCTET_STRING_free(ip2);
|
---|
218 | ip1 = ip2 = NULL;
|
---|
219 | }
|
---|
220 |
|
---|
221 | testresult = 1;
|
---|
222 | end:
|
---|
223 | sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
|
---|
224 | ASN1_OCTET_STRING_free(ip1);
|
---|
225 | ASN1_OCTET_STRING_free(ip2);
|
---|
226 | return testresult;
|
---|
227 | }
|
---|
228 |
|
---|
229 | static struct extvalues_st {
|
---|
230 | const char *value;
|
---|
231 | int pass;
|
---|
232 | } extvalues[] = {
|
---|
233 | /* No prefix is ok */
|
---|
234 | { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
|
---|
235 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
|
---|
236 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
|
---|
237 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
|
---|
238 | /* Prefix is too long */
|
---|
239 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
|
---|
240 | /* Unreasonably large prefix */
|
---|
241 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
|
---|
242 | /* Invalid IP addresses */
|
---|
243 | { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
|
---|
244 | { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
|
---|
245 | { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
|
---|
246 | { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
|
---|
247 | { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
|
---|
248 |
|
---|
249 | /* IPv6 */
|
---|
250 | /* No prefix is ok */
|
---|
251 | { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
|
---|
252 | { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
|
---|
253 | { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
|
---|
254 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
|
---|
255 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
|
---|
256 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
|
---|
257 | { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
|
---|
258 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
|
---|
259 | /* Prefix is too long */
|
---|
260 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
|
---|
261 | /* Unreasonably large prefix */
|
---|
262 | { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
|
---|
263 | /* Invalid IP addresses */
|
---|
264 | /* Not enough blocks of numbers */
|
---|
265 | { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
|
---|
266 | /* Too many blocks of numbers */
|
---|
267 | { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
|
---|
268 | /* First value too large */
|
---|
269 | { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
|
---|
270 | /* First value with invalid characters */
|
---|
271 | { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
|
---|
272 | /* First value is negative */
|
---|
273 | { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
|
---|
274 | };
|
---|
275 |
|
---|
276 | static int test_ext_syntax(void)
|
---|
277 | {
|
---|
278 | size_t i;
|
---|
279 | int testresult = 1;
|
---|
280 |
|
---|
281 | for (i = 0; i < OSSL_NELEM(extvalues); i++) {
|
---|
282 | X509V3_CTX ctx;
|
---|
283 | BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
|
---|
284 | strlen(extvalues[i].value));
|
---|
285 | CONF *conf;
|
---|
286 | long eline;
|
---|
287 |
|
---|
288 | if (!TEST_ptr(extbio))
|
---|
289 | return 0 ;
|
---|
290 |
|
---|
291 | conf = NCONF_new_ex(NULL, NULL);
|
---|
292 | if (!TEST_ptr(conf)) {
|
---|
293 | BIO_free(extbio);
|
---|
294 | return 0;
|
---|
295 | }
|
---|
296 | if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
|
---|
297 | testresult = 0;
|
---|
298 | } else {
|
---|
299 | X509V3_set_ctx_test(&ctx);
|
---|
300 | X509V3_set_nconf(&ctx, conf);
|
---|
301 |
|
---|
302 | if (extvalues[i].pass) {
|
---|
303 | if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
|
---|
304 | NULL))) {
|
---|
305 | TEST_info("Value: %s", extvalues[i].value);
|
---|
306 | testresult = 0;
|
---|
307 | }
|
---|
308 | } else {
|
---|
309 | ERR_set_mark();
|
---|
310 | if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
|
---|
311 | NULL))) {
|
---|
312 | testresult = 0;
|
---|
313 | TEST_info("Value: %s", extvalues[i].value);
|
---|
314 | ERR_clear_last_mark();
|
---|
315 | } else {
|
---|
316 | ERR_pop_to_mark();
|
---|
317 | }
|
---|
318 | }
|
---|
319 | }
|
---|
320 | BIO_free(extbio);
|
---|
321 | NCONF_free(conf);
|
---|
322 | }
|
---|
323 |
|
---|
324 | return testresult;
|
---|
325 | }
|
---|
326 | #endif /* OPENSSL_NO_RFC3779 */
|
---|
327 |
|
---|
328 | OPT_TEST_DECLARE_USAGE("cert.pem\n")
|
---|
329 |
|
---|
330 | int setup_tests(void)
|
---|
331 | {
|
---|
332 | if (!test_skip_common_options()) {
|
---|
333 | TEST_error("Error parsing test options\n");
|
---|
334 | return 0;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (!TEST_ptr(infile = test_get_argument(0)))
|
---|
338 | return 0;
|
---|
339 |
|
---|
340 | ADD_TEST(test_pathlen);
|
---|
341 | #ifndef OPENSSL_NO_RFC3779
|
---|
342 | ADD_TEST(test_asid);
|
---|
343 | ADD_TEST(test_addr_ranges);
|
---|
344 | ADD_TEST(test_ext_syntax);
|
---|
345 | #endif /* OPENSSL_NO_RFC3779 */
|
---|
346 | return 1;
|
---|
347 | }
|
---|