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