VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.7/test/v3ext.c

最後變更 在這個檔案是 105949,由 vboxsync 提交於 2 月 前

openssl-3.1.7: Applied and adjusted our OpenSSL changes to 3.1.7. bugref:10757

檔案大小: 15.8 KB
 
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
20static const char *infile;
21
22static 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
36end:
37 BIO_free(b);
38 X509_free(x);
39 return ret;
40}
41
42#ifndef OPENSSL_NO_RFC3779
43static 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
120static 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
146static 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
174static 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
229static int test_addr_fam_len(void)
230{
231 int testresult = 0;
232 IPAddrBlocks *addr = NULL;
233 IPAddressFamily *f1 = NULL;
234 ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
235 unsigned char key[6];
236 unsigned int keylen;
237 unsigned afi = IANA_AFI_IPV4;
238
239 /* Create the IPAddrBlocks with a good IPAddressFamily */
240 addr = sk_IPAddressFamily_new_null();
241 if (!TEST_ptr(addr))
242 goto end;
243 ip1 = a2i_IPADDRESS(ranges[0].ip1);
244 if (!TEST_ptr(ip1))
245 goto end;
246 ip2 = a2i_IPADDRESS(ranges[0].ip2);
247 if (!TEST_ptr(ip2))
248 goto end;
249 if (!TEST_true(X509v3_addr_add_range(addr, ranges[0].afi, NULL, ip1->data, ip2->data)))
250 goto end;
251 if (!TEST_true(X509v3_addr_is_canonical(addr)))
252 goto end;
253
254 /* Create our malformed IPAddressFamily */
255 key[0] = (afi >> 8) & 0xFF;
256 key[1] = afi & 0xFF;
257 key[2] = 0xD;
258 key[3] = 0xE;
259 key[4] = 0xA;
260 key[5] = 0xD;
261 keylen = 6;
262 if ((f1 = IPAddressFamily_new()) == NULL)
263 goto end;
264 if (f1->ipAddressChoice == NULL &&
265 (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
266 goto end;
267 if (f1->addressFamily == NULL &&
268 (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
269 goto end;
270 if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
271 goto end;
272 if (!sk_IPAddressFamily_push(addr, f1))
273 goto end;
274
275 /* Shouldn't be able to canonize this as the len is > 3*/
276 if (!TEST_false(X509v3_addr_canonize(addr)))
277 goto end;
278
279 /* Create a well formed IPAddressFamily */
280 f1 = sk_IPAddressFamily_pop(addr);
281 IPAddressFamily_free(f1);
282
283 key[0] = (afi >> 8) & 0xFF;
284 key[1] = afi & 0xFF;
285 key[2] = 0x1;
286 keylen = 3;
287 if ((f1 = IPAddressFamily_new()) == NULL)
288 goto end;
289 if (f1->ipAddressChoice == NULL &&
290 (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
291 goto end;
292 if (f1->addressFamily == NULL &&
293 (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
294 goto end;
295 if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
296 goto end;
297
298 /* Mark this as inheritance so we skip some of the is_canonize checks */
299 f1->ipAddressChoice->type = IPAddressChoice_inherit;
300 if (!sk_IPAddressFamily_push(addr, f1))
301 goto end;
302
303 /* Should be able to canonize now */
304 if (!TEST_true(X509v3_addr_canonize(addr)))
305 goto end;
306
307 testresult = 1;
308 end:
309 sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
310 ASN1_OCTET_STRING_free(ip1);
311 ASN1_OCTET_STRING_free(ip2);
312 return testresult;
313}
314
315static struct extvalues_st {
316 const char *value;
317 int pass;
318} extvalues[] = {
319 /* No prefix is ok */
320 { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
321 { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
322 { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
323 { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
324 /* Prefix is too long */
325 { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
326 /* Unreasonably large prefix */
327 { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
328 /* Invalid IP addresses */
329 { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
330 { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
331 { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
332 { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
333 { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
334
335 /* IPv6 */
336 /* No prefix is ok */
337 { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
338 { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
339 { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
340 { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
341 { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
342 { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
343 { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
344 { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
345 /* Prefix is too long */
346 { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
347 /* Unreasonably large prefix */
348 { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
349 /* Invalid IP addresses */
350 /* Not enough blocks of numbers */
351 { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
352 /* Too many blocks of numbers */
353 { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
354 /* First value too large */
355 { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
356 /* First value with invalid characters */
357 { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
358 /* First value is negative */
359 { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
360};
361
362static int test_ext_syntax(void)
363{
364 size_t i;
365 int testresult = 1;
366
367 for (i = 0; i < OSSL_NELEM(extvalues); i++) {
368 X509V3_CTX ctx;
369 BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
370 strlen(extvalues[i].value));
371 CONF *conf;
372 long eline;
373
374 if (!TEST_ptr(extbio))
375 return 0 ;
376
377 conf = NCONF_new_ex(NULL, NULL);
378 if (!TEST_ptr(conf)) {
379 BIO_free(extbio);
380 return 0;
381 }
382 if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
383 testresult = 0;
384 } else {
385 X509V3_set_ctx_test(&ctx);
386 X509V3_set_nconf(&ctx, conf);
387
388 if (extvalues[i].pass) {
389 if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
390 NULL))) {
391 TEST_info("Value: %s", extvalues[i].value);
392 testresult = 0;
393 }
394 } else {
395 ERR_set_mark();
396 if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
397 NULL))) {
398 testresult = 0;
399 TEST_info("Value: %s", extvalues[i].value);
400 ERR_clear_last_mark();
401 } else {
402 ERR_pop_to_mark();
403 }
404 }
405 }
406 BIO_free(extbio);
407 NCONF_free(conf);
408 }
409
410 return testresult;
411}
412
413static int test_addr_subset(void)
414{
415 int i;
416 int ret = 0;
417 IPAddrBlocks *addrEmpty = NULL;
418 IPAddrBlocks *addr[3] = { NULL, NULL };
419 ASN1_OCTET_STRING *ip1[3] = { NULL, NULL };
420 ASN1_OCTET_STRING *ip2[3] = { NULL, NULL };
421 int sz = OSSL_NELEM(addr);
422
423 for (i = 0; i < sz; ++i) {
424 /* Create the IPAddrBlocks with a good IPAddressFamily */
425 if (!TEST_ptr(addr[i] = sk_IPAddressFamily_new_null())
426 || !TEST_ptr(ip1[i] = a2i_IPADDRESS(ranges[i].ip1))
427 || !TEST_ptr(ip2[i] = a2i_IPADDRESS(ranges[i].ip2))
428 || !TEST_true(X509v3_addr_add_range(addr[i], ranges[i].afi, NULL,
429 ip1[i]->data, ip2[i]->data)))
430 goto end;
431 }
432
433 ret = TEST_ptr(addrEmpty = sk_IPAddressFamily_new_null())
434 && TEST_true(X509v3_addr_subset(NULL, NULL))
435 && TEST_true(X509v3_addr_subset(NULL, addr[0]))
436 && TEST_true(X509v3_addr_subset(addrEmpty, addr[0]))
437 && TEST_true(X509v3_addr_subset(addr[0], addr[0]))
438 && TEST_true(X509v3_addr_subset(addr[0], addr[1]))
439 && TEST_true(X509v3_addr_subset(addr[0], addr[2]))
440 && TEST_true(X509v3_addr_subset(addr[1], addr[2]))
441 && TEST_false(X509v3_addr_subset(addr[0], NULL))
442 && TEST_false(X509v3_addr_subset(addr[1], addr[0]))
443 && TEST_false(X509v3_addr_subset(addr[2], addr[1]))
444 && TEST_false(X509v3_addr_subset(addr[0], addrEmpty));
445end:
446 sk_IPAddressFamily_pop_free(addrEmpty, IPAddressFamily_free);
447 for (i = 0; i < sz; ++i) {
448 sk_IPAddressFamily_pop_free(addr[i], IPAddressFamily_free);
449 ASN1_OCTET_STRING_free(ip1[i]);
450 ASN1_OCTET_STRING_free(ip2[i]);
451 }
452 return ret;
453}
454
455#endif /* OPENSSL_NO_RFC3779 */
456
457OPT_TEST_DECLARE_USAGE("cert.pem\n")
458
459int setup_tests(void)
460{
461 if (!test_skip_common_options()) {
462 TEST_error("Error parsing test options\n");
463 return 0;
464 }
465
466 if (!TEST_ptr(infile = test_get_argument(0)))
467 return 0;
468
469 ADD_TEST(test_pathlen);
470#ifndef OPENSSL_NO_RFC3779
471 ADD_TEST(test_asid);
472 ADD_TEST(test_addr_ranges);
473 ADD_TEST(test_ext_syntax);
474 ADD_TEST(test_addr_fam_len);
475 ADD_TEST(test_addr_subset);
476#endif /* OPENSSL_NO_RFC3779 */
477 return 1;
478}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette