xref: /freebsd/crypto/openssl/test/v3ext.c (revision 44096ebd22ddd0081a357011714eff8963614b65)
1e0c4386eSCy Schubert /*
2*44096ebdSEnji Cooper  * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3e0c4386eSCy Schubert  *
4e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8e0c4386eSCy Schubert  */
9e0c4386eSCy Schubert 
10e0c4386eSCy Schubert #include <stdio.h>
11e0c4386eSCy Schubert #include <string.h>
12e0c4386eSCy Schubert #include <openssl/x509.h>
13e0c4386eSCy Schubert #include <openssl/x509v3.h>
14e0c4386eSCy Schubert #include <openssl/pem.h>
15e0c4386eSCy Schubert #include <openssl/err.h>
16e0c4386eSCy Schubert #include "internal/nelem.h"
17e0c4386eSCy Schubert 
18e0c4386eSCy Schubert #include "testutil.h"
19e0c4386eSCy Schubert 
20e0c4386eSCy Schubert static const char *infile;
21e0c4386eSCy Schubert 
22e0c4386eSCy Schubert static int test_pathlen(void)
23e0c4386eSCy Schubert {
24e0c4386eSCy Schubert     X509 *x = NULL;
25e0c4386eSCy Schubert     BIO *b = NULL;
26e0c4386eSCy Schubert     long pathlen;
27e0c4386eSCy Schubert     int ret = 0;
28e0c4386eSCy Schubert 
29e0c4386eSCy Schubert     if (!TEST_ptr(b = BIO_new_file(infile, "r"))
30e0c4386eSCy Schubert             || !TEST_ptr(x = PEM_read_bio_X509(b, NULL, NULL, NULL))
31e0c4386eSCy Schubert             || !TEST_int_eq(pathlen = X509_get_pathlen(x), 6))
32e0c4386eSCy Schubert         goto end;
33e0c4386eSCy Schubert 
34e0c4386eSCy Schubert     ret = 1;
35e0c4386eSCy Schubert 
36e0c4386eSCy Schubert end:
37e0c4386eSCy Schubert     BIO_free(b);
38e0c4386eSCy Schubert     X509_free(x);
39e0c4386eSCy Schubert     return ret;
40e0c4386eSCy Schubert }
41e0c4386eSCy Schubert 
42e0c4386eSCy Schubert #ifndef OPENSSL_NO_RFC3779
43e0c4386eSCy Schubert static int test_asid(void)
44e0c4386eSCy Schubert {
45e0c4386eSCy Schubert     ASN1_INTEGER *val1 = NULL, *val2 = NULL;
46e0c4386eSCy Schubert     ASIdentifiers *asid1 = ASIdentifiers_new(), *asid2 = ASIdentifiers_new(),
47e0c4386eSCy Schubert                   *asid3 = ASIdentifiers_new(), *asid4 = ASIdentifiers_new();
48e0c4386eSCy Schubert     int testresult = 0;
49e0c4386eSCy Schubert 
50e0c4386eSCy Schubert     if (!TEST_ptr(asid1)
51e0c4386eSCy Schubert             || !TEST_ptr(asid2)
52e0c4386eSCy Schubert             || !TEST_ptr(asid3))
53e0c4386eSCy Schubert         goto err;
54e0c4386eSCy Schubert 
55e0c4386eSCy Schubert     if (!TEST_ptr(val1 = ASN1_INTEGER_new())
56e0c4386eSCy Schubert             || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496)))
57e0c4386eSCy Schubert         goto err;
58e0c4386eSCy Schubert 
59e0c4386eSCy Schubert     if (!TEST_true(X509v3_asid_add_id_or_range(asid1, V3_ASID_ASNUM, val1, NULL)))
60e0c4386eSCy Schubert         goto err;
61e0c4386eSCy Schubert 
62e0c4386eSCy Schubert     val1 = NULL;
63e0c4386eSCy Schubert     if (!TEST_ptr(val2 = ASN1_INTEGER_new())
64e0c4386eSCy Schubert             || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
65e0c4386eSCy Schubert         goto err;
66e0c4386eSCy Schubert 
67e0c4386eSCy Schubert     if (!TEST_true(X509v3_asid_add_id_or_range(asid2, V3_ASID_ASNUM, val2, NULL)))
68e0c4386eSCy Schubert         goto err;
69e0c4386eSCy Schubert 
70e0c4386eSCy Schubert     val2 = NULL;
71e0c4386eSCy Schubert     if (!TEST_ptr(val1 = ASN1_INTEGER_new())
72e0c4386eSCy Schubert             || !TEST_true(ASN1_INTEGER_set_int64(val1, 64496))
73e0c4386eSCy Schubert             || !TEST_ptr(val2 = ASN1_INTEGER_new())
74e0c4386eSCy Schubert             || !TEST_true(ASN1_INTEGER_set_int64(val2, 64497)))
75e0c4386eSCy Schubert         goto err;
76e0c4386eSCy Schubert 
77e0c4386eSCy Schubert     /*
78e0c4386eSCy Schubert      * Just tests V3_ASID_ASNUM for now. Could be extended at some point to also
79e0c4386eSCy Schubert      * test V3_ASID_RDI if we think it is worth it.
80e0c4386eSCy Schubert      */
81e0c4386eSCy Schubert     if (!TEST_true(X509v3_asid_add_id_or_range(asid3, V3_ASID_ASNUM, val1, val2)))
82e0c4386eSCy Schubert         goto err;
83e0c4386eSCy Schubert     val1 = val2 = NULL;
84e0c4386eSCy Schubert 
85e0c4386eSCy Schubert     /* Actual subsets */
86e0c4386eSCy Schubert     if (!TEST_true(X509v3_asid_subset(NULL, NULL))
87e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(NULL, asid1))
88e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid1, asid1))
89e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid2, asid2))
90e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid1, asid3))
91e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid2, asid3))
92e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid3, asid3))
93e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid4, asid1))
94e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid4, asid2))
95e0c4386eSCy Schubert             || !TEST_true(X509v3_asid_subset(asid4, asid3)))
96e0c4386eSCy Schubert         goto err;
97e0c4386eSCy Schubert 
98e0c4386eSCy Schubert     /* Not subsets */
99e0c4386eSCy Schubert     if (!TEST_false(X509v3_asid_subset(asid1, NULL))
100e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid1, asid2))
101e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid2, asid1))
102e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid3, asid1))
103e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid3, asid2))
104e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid1, asid4))
105e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid2, asid4))
106e0c4386eSCy Schubert             || !TEST_false(X509v3_asid_subset(asid3, asid4)))
107e0c4386eSCy Schubert         goto err;
108e0c4386eSCy Schubert 
109e0c4386eSCy Schubert     testresult = 1;
110e0c4386eSCy Schubert  err:
111e0c4386eSCy Schubert     ASN1_INTEGER_free(val1);
112e0c4386eSCy Schubert     ASN1_INTEGER_free(val2);
113e0c4386eSCy Schubert     ASIdentifiers_free(asid1);
114e0c4386eSCy Schubert     ASIdentifiers_free(asid2);
115e0c4386eSCy Schubert     ASIdentifiers_free(asid3);
116e0c4386eSCy Schubert     ASIdentifiers_free(asid4);
117e0c4386eSCy Schubert     return testresult;
118e0c4386eSCy Schubert }
119e0c4386eSCy Schubert 
120e0c4386eSCy Schubert static struct ip_ranges_st {
121e0c4386eSCy Schubert     const unsigned int afi;
122e0c4386eSCy Schubert     const char *ip1;
123e0c4386eSCy Schubert     const char *ip2;
124e0c4386eSCy Schubert     int rorp;
125e0c4386eSCy Schubert } ranges[] = {
126e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.1", IPAddressOrRange_addressPrefix},
127e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.2", IPAddressOrRange_addressRange},
128e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.3", IPAddressOrRange_addressPrefix},
129e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.254", IPAddressOrRange_addressRange},
130e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.0.255", IPAddressOrRange_addressPrefix},
131e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.255", IPAddressOrRange_addressRange},
132e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.1", "192.168.0.1", IPAddressOrRange_addressPrefix},
133e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.0.0", "192.168.255.255", IPAddressOrRange_addressPrefix},
134e0c4386eSCy Schubert     { IANA_AFI_IPV4, "192.168.1.0", "192.168.255.255", IPAddressOrRange_addressRange},
135e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::1", IPAddressOrRange_addressPrefix},
136e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::2", IPAddressOrRange_addressRange},
137e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::3", IPAddressOrRange_addressPrefix},
138e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::fffe", IPAddressOrRange_addressRange},
139e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0", "2001:0db8::ffff", IPAddressOrRange_addressPrefix},
140e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::ffff", IPAddressOrRange_addressRange},
141e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::1", "2001:0db8::1", IPAddressOrRange_addressPrefix},
142e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::0:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressPrefix},
143e0c4386eSCy Schubert     { IANA_AFI_IPV6, "2001:0db8::1:0", "2001:0db8::ffff:ffff", IPAddressOrRange_addressRange}
144e0c4386eSCy Schubert };
145e0c4386eSCy Schubert 
146e0c4386eSCy Schubert static int check_addr(IPAddrBlocks *addr, int type)
147e0c4386eSCy Schubert {
148e0c4386eSCy Schubert     IPAddressFamily *fam;
149e0c4386eSCy Schubert     IPAddressOrRange *aorr;
150e0c4386eSCy Schubert 
151e0c4386eSCy Schubert     if (!TEST_int_eq(sk_IPAddressFamily_num(addr), 1))
152e0c4386eSCy Schubert         return 0;
153e0c4386eSCy Schubert 
154e0c4386eSCy Schubert     fam = sk_IPAddressFamily_value(addr, 0);
155e0c4386eSCy Schubert     if (!TEST_ptr(fam))
156e0c4386eSCy Schubert         return 0;
157e0c4386eSCy Schubert 
158e0c4386eSCy Schubert     if (!TEST_int_eq(fam->ipAddressChoice->type, IPAddressChoice_addressesOrRanges))
159e0c4386eSCy Schubert         return 0;
160e0c4386eSCy Schubert 
161e0c4386eSCy Schubert     if (!TEST_int_eq(sk_IPAddressOrRange_num(fam->ipAddressChoice->u.addressesOrRanges), 1))
162e0c4386eSCy Schubert         return 0;
163e0c4386eSCy Schubert 
164e0c4386eSCy Schubert     aorr = sk_IPAddressOrRange_value(fam->ipAddressChoice->u.addressesOrRanges, 0);
165e0c4386eSCy Schubert     if (!TEST_ptr(aorr))
166e0c4386eSCy Schubert         return 0;
167e0c4386eSCy Schubert 
168e0c4386eSCy Schubert     if (!TEST_int_eq(aorr->type, type))
169e0c4386eSCy Schubert         return 0;
170e0c4386eSCy Schubert 
171e0c4386eSCy Schubert     return 1;
172e0c4386eSCy Schubert }
173e0c4386eSCy Schubert 
174e0c4386eSCy Schubert static int test_addr_ranges(void)
175e0c4386eSCy Schubert {
176e0c4386eSCy Schubert     IPAddrBlocks *addr = NULL;
177e0c4386eSCy Schubert     ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
178e0c4386eSCy Schubert     size_t i;
179e0c4386eSCy Schubert     int testresult = 0;
180e0c4386eSCy Schubert 
181e0c4386eSCy Schubert     for (i = 0; i < OSSL_NELEM(ranges); i++) {
182e0c4386eSCy Schubert         addr = sk_IPAddressFamily_new_null();
183e0c4386eSCy Schubert         if (!TEST_ptr(addr))
184e0c4386eSCy Schubert             goto end;
185e0c4386eSCy Schubert         /*
186e0c4386eSCy Schubert          * Has the side effect of installing the comparison function onto the
187e0c4386eSCy Schubert          * stack.
188e0c4386eSCy Schubert          */
189e0c4386eSCy Schubert         if (!TEST_true(X509v3_addr_canonize(addr)))
190e0c4386eSCy Schubert             goto end;
191e0c4386eSCy Schubert 
192e0c4386eSCy Schubert         ip1 = a2i_IPADDRESS(ranges[i].ip1);
193e0c4386eSCy Schubert         if (!TEST_ptr(ip1))
194e0c4386eSCy Schubert             goto end;
195e0c4386eSCy Schubert         if (!TEST_true(ip1->length == 4 || ip1->length == 16))
196e0c4386eSCy Schubert             goto end;
197e0c4386eSCy Schubert         ip2 = a2i_IPADDRESS(ranges[i].ip2);
198e0c4386eSCy Schubert         if (!TEST_ptr(ip2))
199e0c4386eSCy Schubert             goto end;
200e0c4386eSCy Schubert         if (!TEST_int_eq(ip2->length, ip1->length))
201e0c4386eSCy Schubert             goto end;
202e0c4386eSCy Schubert         if (!TEST_true(memcmp(ip1->data, ip2->data, ip1->length) <= 0))
203e0c4386eSCy Schubert             goto end;
204e0c4386eSCy Schubert 
205e0c4386eSCy Schubert         if (!TEST_true(X509v3_addr_add_range(addr, ranges[i].afi, NULL, ip1->data, ip2->data)))
206e0c4386eSCy Schubert             goto end;
207e0c4386eSCy Schubert 
208e0c4386eSCy Schubert         if (!TEST_true(X509v3_addr_is_canonical(addr)))
209e0c4386eSCy Schubert             goto end;
210e0c4386eSCy Schubert 
211e0c4386eSCy Schubert         if (!check_addr(addr, ranges[i].rorp))
212e0c4386eSCy Schubert             goto end;
213e0c4386eSCy Schubert 
214e0c4386eSCy Schubert         sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
215e0c4386eSCy Schubert         addr = NULL;
216e0c4386eSCy Schubert         ASN1_OCTET_STRING_free(ip1);
217e0c4386eSCy Schubert         ASN1_OCTET_STRING_free(ip2);
218e0c4386eSCy Schubert         ip1 = ip2 = NULL;
219e0c4386eSCy Schubert     }
220e0c4386eSCy Schubert 
221e0c4386eSCy Schubert     testresult = 1;
222e0c4386eSCy Schubert  end:
223e0c4386eSCy Schubert     sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
224e0c4386eSCy Schubert     ASN1_OCTET_STRING_free(ip1);
225e0c4386eSCy Schubert     ASN1_OCTET_STRING_free(ip2);
226e0c4386eSCy Schubert     return testresult;
227e0c4386eSCy Schubert }
228e0c4386eSCy Schubert 
229e0c4386eSCy Schubert static int test_addr_fam_len(void)
230e0c4386eSCy Schubert {
231e0c4386eSCy Schubert     int testresult = 0;
232e0c4386eSCy Schubert     IPAddrBlocks *addr = NULL;
233e0c4386eSCy Schubert     IPAddressFamily *f1 = NULL;
234e0c4386eSCy Schubert     ASN1_OCTET_STRING *ip1 = NULL, *ip2 = NULL;
235e0c4386eSCy Schubert     unsigned char key[6];
236e0c4386eSCy Schubert     unsigned int keylen;
237e0c4386eSCy Schubert     unsigned afi = IANA_AFI_IPV4;
238e0c4386eSCy Schubert 
239e0c4386eSCy Schubert     /* Create the IPAddrBlocks with a good IPAddressFamily */
240e0c4386eSCy Schubert     addr = sk_IPAddressFamily_new_null();
241e0c4386eSCy Schubert     if (!TEST_ptr(addr))
242e0c4386eSCy Schubert         goto end;
243e0c4386eSCy Schubert     ip1 = a2i_IPADDRESS(ranges[0].ip1);
244e0c4386eSCy Schubert     if (!TEST_ptr(ip1))
245e0c4386eSCy Schubert         goto end;
246e0c4386eSCy Schubert     ip2 = a2i_IPADDRESS(ranges[0].ip2);
247e0c4386eSCy Schubert     if (!TEST_ptr(ip2))
248e0c4386eSCy Schubert         goto end;
249e0c4386eSCy Schubert     if (!TEST_true(X509v3_addr_add_range(addr, ranges[0].afi, NULL, ip1->data, ip2->data)))
250e0c4386eSCy Schubert         goto end;
251e0c4386eSCy Schubert     if (!TEST_true(X509v3_addr_is_canonical(addr)))
252e0c4386eSCy Schubert         goto end;
253e0c4386eSCy Schubert 
254e0c4386eSCy Schubert     /* Create our malformed IPAddressFamily */
255e0c4386eSCy Schubert     key[0] = (afi >> 8) & 0xFF;
256e0c4386eSCy Schubert     key[1] = afi & 0xFF;
257e0c4386eSCy Schubert     key[2] = 0xD;
258e0c4386eSCy Schubert     key[3] = 0xE;
259e0c4386eSCy Schubert     key[4] = 0xA;
260e0c4386eSCy Schubert     key[5] = 0xD;
261e0c4386eSCy Schubert     keylen = 6;
262e0c4386eSCy Schubert     if ((f1 = IPAddressFamily_new()) == NULL)
263e0c4386eSCy Schubert         goto end;
264e0c4386eSCy Schubert     if (f1->ipAddressChoice == NULL &&
265e0c4386eSCy Schubert         (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
266e0c4386eSCy Schubert         goto end;
267e0c4386eSCy Schubert     if (f1->addressFamily == NULL &&
268e0c4386eSCy Schubert         (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
269e0c4386eSCy Schubert         goto end;
270e0c4386eSCy Schubert     if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
271e0c4386eSCy Schubert         goto end;
272*44096ebdSEnji Cooper 
273*44096ebdSEnji Cooper     /* Push and transfer memory ownership to stack */
274e0c4386eSCy Schubert     if (!sk_IPAddressFamily_push(addr, f1))
275e0c4386eSCy Schubert         goto end;
276*44096ebdSEnji Cooper     f1 = NULL;
277e0c4386eSCy Schubert 
278e0c4386eSCy Schubert     /* Shouldn't be able to canonize this as the len is > 3*/
279e0c4386eSCy Schubert     if (!TEST_false(X509v3_addr_canonize(addr)))
280e0c4386eSCy Schubert         goto end;
281e0c4386eSCy Schubert 
282*44096ebdSEnji Cooper     /* Pop and free the new stack element */
283*44096ebdSEnji Cooper     IPAddressFamily_free(sk_IPAddressFamily_pop(addr));
284e0c4386eSCy Schubert 
285*44096ebdSEnji Cooper     /* Create a well-formed IPAddressFamily */
286e0c4386eSCy Schubert     key[0] = (afi >> 8) & 0xFF;
287e0c4386eSCy Schubert     key[1] = afi & 0xFF;
288e0c4386eSCy Schubert     key[2] = 0x1;
289e0c4386eSCy Schubert     keylen = 3;
290e0c4386eSCy Schubert     if ((f1 = IPAddressFamily_new()) == NULL)
291e0c4386eSCy Schubert         goto end;
292e0c4386eSCy Schubert     if (f1->ipAddressChoice == NULL &&
293e0c4386eSCy Schubert         (f1->ipAddressChoice = IPAddressChoice_new()) == NULL)
294e0c4386eSCy Schubert         goto end;
295e0c4386eSCy Schubert     if (f1->addressFamily == NULL &&
296e0c4386eSCy Schubert         (f1->addressFamily = ASN1_OCTET_STRING_new()) == NULL)
297e0c4386eSCy Schubert         goto end;
298e0c4386eSCy Schubert     if (!ASN1_OCTET_STRING_set(f1->addressFamily, key, keylen))
299e0c4386eSCy Schubert         goto end;
300e0c4386eSCy Schubert 
301e0c4386eSCy Schubert     /* Mark this as inheritance so we skip some of the is_canonize checks */
302e0c4386eSCy Schubert     f1->ipAddressChoice->type = IPAddressChoice_inherit;
303*44096ebdSEnji Cooper 
304*44096ebdSEnji Cooper     /* Push and transfer memory ownership to stack */
305e0c4386eSCy Schubert     if (!sk_IPAddressFamily_push(addr, f1))
306e0c4386eSCy Schubert         goto end;
307*44096ebdSEnji Cooper     f1 = NULL;
308e0c4386eSCy Schubert 
309e0c4386eSCy Schubert     /* Should be able to canonize now */
310e0c4386eSCy Schubert     if (!TEST_true(X509v3_addr_canonize(addr)))
311e0c4386eSCy Schubert         goto end;
312e0c4386eSCy Schubert 
313e0c4386eSCy Schubert     testresult = 1;
314e0c4386eSCy Schubert   end:
315*44096ebdSEnji Cooper     /* Free stack and any memory owned by detached element */
316*44096ebdSEnji Cooper     IPAddressFamily_free(f1);
317e0c4386eSCy Schubert     sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free);
318*44096ebdSEnji Cooper 
319e0c4386eSCy Schubert     ASN1_OCTET_STRING_free(ip1);
320e0c4386eSCy Schubert     ASN1_OCTET_STRING_free(ip2);
321e0c4386eSCy Schubert     return testresult;
322e0c4386eSCy Schubert }
323e0c4386eSCy Schubert 
324e0c4386eSCy Schubert static struct extvalues_st {
325e0c4386eSCy Schubert     const char *value;
326e0c4386eSCy Schubert     int pass;
327e0c4386eSCy Schubert } extvalues[] = {
328e0c4386eSCy Schubert     /* No prefix is ok */
329e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.1\n", 1 },
330e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0/0\n", 1 },
331e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0/1\n", 1 },
332e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0/32\n", 1 },
333e0c4386eSCy Schubert     /* Prefix is too long */
334e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0/33\n", 0 },
335e0c4386eSCy Schubert     /* Unreasonably large prefix */
336e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0/12341234\n", 0 },
337e0c4386eSCy Schubert     /* Invalid IP addresses */
338e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0\n", 0 },
339e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:256.0.0.0\n", 0 },
340e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:-1.0.0.0\n", 0 },
341e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv4:192.0.0.0.0\n", 0 },
342e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv3:192.0.0.0\n", 0 },
343e0c4386eSCy Schubert 
344e0c4386eSCy Schubert     /* IPv6 */
345e0c4386eSCy Schubert     /* No prefix is ok */
346e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::\n", 1 },
347e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001::db8\n", 1 },
348e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000\n", 1 },
349e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/0\n", 1 },
350e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/1\n", 1 },
351e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/32\n", 1 },
352e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000/32\n", 1 },
353e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/128\n", 1 },
354e0c4386eSCy Schubert     /* Prefix is too long */
355e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/129\n", 0 },
356e0c4386eSCy Schubert     /* Unreasonably large prefix */
357e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:db8::/12341234\n", 0 },
358e0c4386eSCy Schubert     /* Invalid IP addresses */
359e0c4386eSCy Schubert     /* Not enough blocks of numbers */
360e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000\n", 0 },
361e0c4386eSCy Schubert     /* Too many blocks of numbers */
362e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:2001:0db8:0000:0000:0000:0000:0000:0000:0000\n", 0 },
363e0c4386eSCy Schubert     /* First value too large */
364e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:1ffff:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
365e0c4386eSCy Schubert     /* First value with invalid characters */
366e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:fffg:0db8:0000:0000:0000:0000:0000:0000\n", 0 },
367e0c4386eSCy Schubert     /* First value is negative */
368e0c4386eSCy Schubert     { "sbgp-ipAddrBlock = IPv6:-1:0db8:0000:0000:0000:0000:0000:0000\n", 0 }
369e0c4386eSCy Schubert };
370e0c4386eSCy Schubert 
371e0c4386eSCy Schubert static int test_ext_syntax(void)
372e0c4386eSCy Schubert {
373e0c4386eSCy Schubert     size_t i;
374e0c4386eSCy Schubert     int testresult = 1;
375e0c4386eSCy Schubert 
376e0c4386eSCy Schubert     for (i = 0; i < OSSL_NELEM(extvalues); i++) {
377e0c4386eSCy Schubert         X509V3_CTX ctx;
378e0c4386eSCy Schubert         BIO *extbio = BIO_new_mem_buf(extvalues[i].value,
379e0c4386eSCy Schubert                                       strlen(extvalues[i].value));
380e0c4386eSCy Schubert         CONF *conf;
381e0c4386eSCy Schubert         long eline;
382e0c4386eSCy Schubert 
383e0c4386eSCy Schubert         if (!TEST_ptr(extbio))
384e0c4386eSCy Schubert             return 0 ;
385e0c4386eSCy Schubert 
386e0c4386eSCy Schubert         conf = NCONF_new_ex(NULL, NULL);
387e0c4386eSCy Schubert         if (!TEST_ptr(conf)) {
388e0c4386eSCy Schubert             BIO_free(extbio);
389e0c4386eSCy Schubert             return 0;
390e0c4386eSCy Schubert         }
391e0c4386eSCy Schubert         if (!TEST_long_gt(NCONF_load_bio(conf, extbio, &eline), 0)) {
392e0c4386eSCy Schubert             testresult = 0;
393e0c4386eSCy Schubert         } else {
394e0c4386eSCy Schubert             X509V3_set_ctx_test(&ctx);
395e0c4386eSCy Schubert             X509V3_set_nconf(&ctx, conf);
396e0c4386eSCy Schubert 
397e0c4386eSCy Schubert             if (extvalues[i].pass) {
398e0c4386eSCy Schubert                 if (!TEST_true(X509V3_EXT_add_nconf(conf, &ctx, "default",
399e0c4386eSCy Schubert                                                     NULL))) {
400e0c4386eSCy Schubert                     TEST_info("Value: %s", extvalues[i].value);
401e0c4386eSCy Schubert                     testresult = 0;
402e0c4386eSCy Schubert                 }
403e0c4386eSCy Schubert             } else {
404e0c4386eSCy Schubert                 ERR_set_mark();
405e0c4386eSCy Schubert                 if (!TEST_false(X509V3_EXT_add_nconf(conf, &ctx, "default",
406e0c4386eSCy Schubert                                                      NULL))) {
407e0c4386eSCy Schubert                     testresult = 0;
408e0c4386eSCy Schubert                     TEST_info("Value: %s", extvalues[i].value);
409e0c4386eSCy Schubert                     ERR_clear_last_mark();
410e0c4386eSCy Schubert                 } else {
411e0c4386eSCy Schubert                     ERR_pop_to_mark();
412e0c4386eSCy Schubert                 }
413e0c4386eSCy Schubert             }
414e0c4386eSCy Schubert         }
415e0c4386eSCy Schubert         BIO_free(extbio);
416e0c4386eSCy Schubert         NCONF_free(conf);
417e0c4386eSCy Schubert     }
418e0c4386eSCy Schubert 
419e0c4386eSCy Schubert     return testresult;
420e0c4386eSCy Schubert }
421e0c4386eSCy Schubert 
422e0c4386eSCy Schubert static int test_addr_subset(void)
423e0c4386eSCy Schubert {
424e0c4386eSCy Schubert     int i;
425e0c4386eSCy Schubert     int ret = 0;
426e0c4386eSCy Schubert     IPAddrBlocks *addrEmpty = NULL;
427e0c4386eSCy Schubert     IPAddrBlocks *addr[3] = { NULL, NULL };
428e0c4386eSCy Schubert     ASN1_OCTET_STRING *ip1[3] = { NULL, NULL };
429e0c4386eSCy Schubert     ASN1_OCTET_STRING *ip2[3] = { NULL, NULL };
430e0c4386eSCy Schubert     int sz = OSSL_NELEM(addr);
431e0c4386eSCy Schubert 
432e0c4386eSCy Schubert     for (i = 0; i < sz; ++i) {
433e0c4386eSCy Schubert         /* Create the IPAddrBlocks with a good IPAddressFamily */
434e0c4386eSCy Schubert         if (!TEST_ptr(addr[i] = sk_IPAddressFamily_new_null())
435e0c4386eSCy Schubert             || !TEST_ptr(ip1[i] = a2i_IPADDRESS(ranges[i].ip1))
436e0c4386eSCy Schubert             || !TEST_ptr(ip2[i] = a2i_IPADDRESS(ranges[i].ip2))
437e0c4386eSCy Schubert             || !TEST_true(X509v3_addr_add_range(addr[i], ranges[i].afi, NULL,
438e0c4386eSCy Schubert                                                 ip1[i]->data, ip2[i]->data)))
439e0c4386eSCy Schubert             goto end;
440e0c4386eSCy Schubert     }
441e0c4386eSCy Schubert 
442e0c4386eSCy Schubert     ret = TEST_ptr(addrEmpty = sk_IPAddressFamily_new_null())
443e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(NULL, NULL))
444e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(NULL, addr[0]))
445e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(addrEmpty, addr[0]))
446e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(addr[0], addr[0]))
447e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(addr[0], addr[1]))
448e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(addr[0], addr[2]))
449e0c4386eSCy Schubert           && TEST_true(X509v3_addr_subset(addr[1], addr[2]))
450e0c4386eSCy Schubert           && TEST_false(X509v3_addr_subset(addr[0], NULL))
451e0c4386eSCy Schubert           && TEST_false(X509v3_addr_subset(addr[1], addr[0]))
452e0c4386eSCy Schubert           && TEST_false(X509v3_addr_subset(addr[2], addr[1]))
453e0c4386eSCy Schubert           && TEST_false(X509v3_addr_subset(addr[0], addrEmpty));
454e0c4386eSCy Schubert end:
455e0c4386eSCy Schubert     sk_IPAddressFamily_pop_free(addrEmpty, IPAddressFamily_free);
456e0c4386eSCy Schubert     for (i = 0; i < sz; ++i) {
457e0c4386eSCy Schubert         sk_IPAddressFamily_pop_free(addr[i], IPAddressFamily_free);
458e0c4386eSCy Schubert         ASN1_OCTET_STRING_free(ip1[i]);
459e0c4386eSCy Schubert         ASN1_OCTET_STRING_free(ip2[i]);
460e0c4386eSCy Schubert     }
461e0c4386eSCy Schubert     return ret;
462e0c4386eSCy Schubert }
463e0c4386eSCy Schubert 
464e0c4386eSCy Schubert #endif /* OPENSSL_NO_RFC3779 */
465e0c4386eSCy Schubert 
466e0c4386eSCy Schubert OPT_TEST_DECLARE_USAGE("cert.pem\n")
467e0c4386eSCy Schubert 
468e0c4386eSCy Schubert int setup_tests(void)
469e0c4386eSCy Schubert {
470e0c4386eSCy Schubert     if (!test_skip_common_options()) {
471e0c4386eSCy Schubert         TEST_error("Error parsing test options\n");
472e0c4386eSCy Schubert         return 0;
473e0c4386eSCy Schubert     }
474e0c4386eSCy Schubert 
475e0c4386eSCy Schubert     if (!TEST_ptr(infile = test_get_argument(0)))
476e0c4386eSCy Schubert         return 0;
477e0c4386eSCy Schubert 
478e0c4386eSCy Schubert     ADD_TEST(test_pathlen);
479e0c4386eSCy Schubert #ifndef OPENSSL_NO_RFC3779
480e0c4386eSCy Schubert     ADD_TEST(test_asid);
481e0c4386eSCy Schubert     ADD_TEST(test_addr_ranges);
482e0c4386eSCy Schubert     ADD_TEST(test_ext_syntax);
483e0c4386eSCy Schubert     ADD_TEST(test_addr_fam_len);
484e0c4386eSCy Schubert     ADD_TEST(test_addr_subset);
485e0c4386eSCy Schubert #endif /* OPENSSL_NO_RFC3779 */
486e0c4386eSCy Schubert     return 1;
487e0c4386eSCy Schubert }
488