Add missing header for memcmp
[openssl.git] / test / v3ext.c
1 /*
2  * Copyright 2016-2020 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 #endif /* OPENSSL_NO_RFC3779 */
229
230 OPT_TEST_DECLARE_USAGE("cert.pem\n")
231
232 int setup_tests(void)
233 {
234     if (!test_skip_common_options()) {
235         TEST_error("Error parsing test options\n");
236         return 0;
237     }
238
239     if (!TEST_ptr(infile = test_get_argument(0)))
240         return 0;
241
242     ADD_TEST(test_pathlen);
243 #ifndef OPENSSL_NO_RFC3779
244     ADD_TEST(test_asid);
245     ADD_TEST(test_addr_ranges);
246 #endif /* OPENSSL_NO_RFC3779 */
247     return 1;
248 }