1 /* $OpenBSD: x509_lib.c,v 1.13 2023/04/17 06:46:19 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 1999. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* X509 v3 extension utilities */ 59 60 #include <stdio.h> 61 62 #include <openssl/conf.h> 63 #include <openssl/err.h> 64 #include <openssl/x509v3.h> 65 66 #include "x509_local.h" 67 68 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; 69 70 extern const X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku; 71 extern const X509V3_EXT_METHOD v3_pkey_usage_period, v3_sxnet, v3_info, v3_sinfo; 72 extern const X509V3_EXT_METHOD v3_ns_ia5_list[], v3_alt[], v3_skey_id, v3_akey_id; 73 extern const X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_crl_invdate; 74 extern const X509V3_EXT_METHOD v3_delta_crl, v3_cpols, v3_crld, v3_freshest_crl; 75 extern const X509V3_EXT_METHOD v3_ocsp_nonce, v3_ocsp_accresp, v3_ocsp_acutoff; 76 extern const X509V3_EXT_METHOD v3_ocsp_crlid, v3_ocsp_nocheck, v3_ocsp_serviceloc; 77 extern const X509V3_EXT_METHOD v3_crl_hold; 78 extern const X509V3_EXT_METHOD v3_policy_mappings, v3_policy_constraints; 79 extern const X509V3_EXT_METHOD v3_name_constraints, v3_inhibit_anyp, v3_idp; 80 extern const X509V3_EXT_METHOD v3_addr, v3_asid; 81 extern const X509V3_EXT_METHOD v3_ct_scts[3]; 82 83 /* 84 * This table needs to be sorted by increasing ext_nid values for OBJ_bsearch_. 85 */ 86 87 static const X509V3_EXT_METHOD *standard_exts[] = { 88 &v3_nscert, 89 &v3_ns_ia5_list[0], 90 &v3_ns_ia5_list[1], 91 &v3_ns_ia5_list[2], 92 &v3_ns_ia5_list[3], 93 &v3_ns_ia5_list[4], 94 &v3_ns_ia5_list[5], 95 &v3_ns_ia5_list[6], 96 &v3_skey_id, 97 &v3_key_usage, 98 &v3_pkey_usage_period, 99 &v3_alt[0], 100 &v3_alt[1], 101 &v3_bcons, 102 &v3_crl_num, 103 &v3_cpols, 104 &v3_akey_id, 105 &v3_crld, 106 &v3_ext_ku, 107 &v3_delta_crl, 108 &v3_crl_reason, 109 #ifndef OPENSSL_NO_OCSP 110 &v3_crl_invdate, 111 #endif 112 &v3_sxnet, 113 &v3_info, 114 #ifndef OPENSSL_NO_RFC3779 115 &v3_addr, 116 &v3_asid, 117 #endif 118 #ifndef OPENSSL_NO_OCSP 119 &v3_ocsp_nonce, 120 &v3_ocsp_crlid, 121 &v3_ocsp_accresp, 122 &v3_ocsp_nocheck, 123 &v3_ocsp_acutoff, 124 &v3_ocsp_serviceloc, 125 #endif 126 &v3_sinfo, 127 &v3_policy_constraints, 128 #ifndef OPENSSL_NO_OCSP 129 &v3_crl_hold, 130 #endif 131 &v3_name_constraints, 132 &v3_policy_mappings, 133 &v3_inhibit_anyp, 134 &v3_idp, 135 &v3_alt[2], 136 &v3_freshest_crl, 137 #ifndef OPENSSL_NO_CT 138 &v3_ct_scts[0], 139 &v3_ct_scts[1], 140 &v3_ct_scts[2], 141 #endif 142 }; 143 144 #define STANDARD_EXTENSION_COUNT (sizeof(standard_exts) / sizeof(standard_exts[0])) 145 146 static int 147 ext_cmp(const X509V3_EXT_METHOD * const *a, const X509V3_EXT_METHOD * const *b) 148 { 149 return ((*a)->ext_nid - (*b)->ext_nid); 150 } 151 152 int 153 X509V3_EXT_add(X509V3_EXT_METHOD *ext) 154 { 155 if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) { 156 X509V3error(ERR_R_MALLOC_FAILURE); 157 return 0; 158 } 159 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { 160 X509V3error(ERR_R_MALLOC_FAILURE); 161 return 0; 162 } 163 return 1; 164 } 165 LCRYPTO_ALIAS(X509V3_EXT_add); 166 167 static int 168 ext_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) 169 { 170 const X509V3_EXT_METHOD * const *a = a_; 171 const X509V3_EXT_METHOD * const *b = b_; 172 return ext_cmp(a, b); 173 } 174 175 static const X509V3_EXT_METHOD ** 176 OBJ_bsearch_ext(const X509V3_EXT_METHOD **key, 177 const X509V3_EXT_METHOD *const *base, int num) 178 { 179 return (const X509V3_EXT_METHOD **)OBJ_bsearch_(key, base, num, 180 sizeof(const X509V3_EXT_METHOD *), ext_cmp_BSEARCH_CMP_FN); 181 } 182 183 const X509V3_EXT_METHOD * 184 X509V3_EXT_get_nid(int nid) 185 { 186 X509V3_EXT_METHOD tmp; 187 const X509V3_EXT_METHOD *t = &tmp, * const *ret; 188 int idx; 189 190 if (nid < 0) 191 return NULL; 192 tmp.ext_nid = nid; 193 ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT); 194 if (ret) 195 return *ret; 196 if (!ext_list) 197 return NULL; 198 idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp); 199 if (idx == -1) 200 return NULL; 201 return sk_X509V3_EXT_METHOD_value(ext_list, idx); 202 } 203 LCRYPTO_ALIAS(X509V3_EXT_get_nid); 204 205 const X509V3_EXT_METHOD * 206 X509V3_EXT_get(X509_EXTENSION *ext) 207 { 208 int nid; 209 210 if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) 211 return NULL; 212 return X509V3_EXT_get_nid(nid); 213 } 214 LCRYPTO_ALIAS(X509V3_EXT_get); 215 216 int 217 X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist) 218 { 219 for (; extlist->ext_nid!=-1; extlist++) 220 if (!X509V3_EXT_add(extlist)) 221 return 0; 222 return 1; 223 } 224 LCRYPTO_ALIAS(X509V3_EXT_add_list); 225 226 int 227 X509V3_EXT_add_alias(int nid_to, int nid_from) 228 { 229 const X509V3_EXT_METHOD *ext; 230 X509V3_EXT_METHOD *tmpext; 231 232 if (!(ext = X509V3_EXT_get_nid(nid_from))) { 233 X509V3error(X509V3_R_EXTENSION_NOT_FOUND); 234 return 0; 235 } 236 if (!(tmpext = malloc(sizeof(X509V3_EXT_METHOD)))) { 237 X509V3error(ERR_R_MALLOC_FAILURE); 238 return 0; 239 } 240 *tmpext = *ext; 241 tmpext->ext_nid = nid_to; 242 tmpext->ext_flags |= X509V3_EXT_DYNAMIC; 243 if (!X509V3_EXT_add(tmpext)) { 244 free(tmpext); 245 return 0; 246 } 247 return 1; 248 } 249 LCRYPTO_ALIAS(X509V3_EXT_add_alias); 250 251 static void 252 ext_list_free(X509V3_EXT_METHOD *ext) 253 { 254 if (ext->ext_flags & X509V3_EXT_DYNAMIC) 255 free(ext); 256 } 257 258 void 259 X509V3_EXT_cleanup(void) 260 { 261 sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free); 262 ext_list = NULL; 263 } 264 LCRYPTO_ALIAS(X509V3_EXT_cleanup); 265 266 int 267 X509V3_add_standard_extensions(void) 268 { 269 return 1; 270 } 271 LCRYPTO_ALIAS(X509V3_add_standard_extensions); 272 273 /* Return an extension internal structure */ 274 275 void * 276 X509V3_EXT_d2i(X509_EXTENSION *ext) 277 { 278 const X509V3_EXT_METHOD *method; 279 const unsigned char *p; 280 281 if (!(method = X509V3_EXT_get(ext))) 282 return NULL; 283 p = ext->value->data; 284 if (method->it) 285 return ASN1_item_d2i(NULL, &p, ext->value->length, 286 method->it); 287 return method->d2i(NULL, &p, ext->value->length); 288 } 289 LCRYPTO_ALIAS(X509V3_EXT_d2i); 290 291 /* Get critical flag and decoded version of extension from a NID. 292 * The "idx" variable returns the last found extension and can 293 * be used to retrieve multiple extensions of the same NID. 294 * However multiple extensions with the same NID is usually 295 * due to a badly encoded certificate so if idx is NULL we 296 * choke if multiple extensions exist. 297 * The "crit" variable is set to the critical value. 298 * The return value is the decoded extension or NULL on 299 * error. The actual error can have several different causes, 300 * the value of *crit reflects the cause: 301 * >= 0, extension found but not decoded (reflects critical value). 302 * -1 extension not found. 303 * -2 extension occurs more than once. 304 */ 305 306 void * 307 X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx) 308 { 309 int lastpos, i; 310 X509_EXTENSION *ex, *found_ex = NULL; 311 312 if (!x) { 313 if (idx) 314 *idx = -1; 315 if (crit) 316 *crit = -1; 317 return NULL; 318 } 319 if (idx) 320 lastpos = *idx + 1; 321 else 322 lastpos = 0; 323 if (lastpos < 0) 324 lastpos = 0; 325 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) { 326 ex = sk_X509_EXTENSION_value(x, i); 327 if (OBJ_obj2nid(ex->object) == nid) { 328 if (idx) { 329 *idx = i; 330 found_ex = ex; 331 break; 332 } else if (found_ex) { 333 /* Found more than one */ 334 if (crit) 335 *crit = -2; 336 return NULL; 337 } 338 found_ex = ex; 339 } 340 } 341 if (found_ex) { 342 /* Found it */ 343 if (crit) 344 *crit = X509_EXTENSION_get_critical(found_ex); 345 return X509V3_EXT_d2i(found_ex); 346 } 347 348 /* Extension not found */ 349 if (idx) 350 *idx = -1; 351 if (crit) 352 *crit = -1; 353 return NULL; 354 } 355 LCRYPTO_ALIAS(X509V3_get_d2i); 356 357 /* This function is a general extension append, replace and delete utility. 358 * The precise operation is governed by the 'flags' value. The 'crit' and 359 * 'value' arguments (if relevant) are the extensions internal structure. 360 */ 361 362 int 363 X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, 364 int crit, unsigned long flags) 365 { 366 int extidx = -1; 367 int errcode; 368 X509_EXTENSION *ext, *extmp; 369 unsigned long ext_op = flags & X509V3_ADD_OP_MASK; 370 371 /* If appending we don't care if it exists, otherwise 372 * look for existing extension. 373 */ 374 if (ext_op != X509V3_ADD_APPEND) 375 extidx = X509v3_get_ext_by_NID(*x, nid, -1); 376 377 /* See if extension exists */ 378 if (extidx >= 0) { 379 /* If keep existing, nothing to do */ 380 if (ext_op == X509V3_ADD_KEEP_EXISTING) 381 return 1; 382 /* If default then its an error */ 383 if (ext_op == X509V3_ADD_DEFAULT) { 384 errcode = X509V3_R_EXTENSION_EXISTS; 385 goto err; 386 } 387 /* If delete, just delete it */ 388 if (ext_op == X509V3_ADD_DELETE) { 389 if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL) 390 return -1; 391 X509_EXTENSION_free(extmp); 392 return 1; 393 } 394 } else { 395 /* If replace existing or delete, error since 396 * extension must exist 397 */ 398 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || 399 (ext_op == X509V3_ADD_DELETE)) { 400 errcode = X509V3_R_EXTENSION_NOT_FOUND; 401 goto err; 402 } 403 } 404 405 /* If we get this far then we have to create an extension: 406 * could have some flags for alternative encoding schemes... 407 */ 408 409 ext = X509V3_EXT_i2d(nid, crit, value); 410 411 if (!ext) { 412 X509V3error(X509V3_R_ERROR_CREATING_EXTENSION); 413 return 0; 414 } 415 416 /* If extension exists replace it.. */ 417 if (extidx >= 0) { 418 extmp = sk_X509_EXTENSION_value(*x, extidx); 419 X509_EXTENSION_free(extmp); 420 if (!sk_X509_EXTENSION_set(*x, extidx, ext)) 421 return -1; 422 return 1; 423 } 424 425 if (!*x && !(*x = sk_X509_EXTENSION_new_null())) 426 return -1; 427 if (!sk_X509_EXTENSION_push(*x, ext)) 428 return -1; 429 430 return 1; 431 432 err: 433 if (!(flags & X509V3_ADD_SILENT)) 434 X509V3error(errcode); 435 return 0; 436 } 437 LCRYPTO_ALIAS(X509V3_add1_i2d); 438