1 /* $OpenBSD: x509_lib.c,v 1.14 2023/04/25 10:56:58 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_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_info, 113 #ifndef OPENSSL_NO_RFC3779 114 &v3_addr, 115 &v3_asid, 116 #endif 117 #ifndef OPENSSL_NO_OCSP 118 &v3_ocsp_nonce, 119 &v3_ocsp_crlid, 120 &v3_ocsp_accresp, 121 &v3_ocsp_nocheck, 122 &v3_ocsp_acutoff, 123 &v3_ocsp_serviceloc, 124 #endif 125 &v3_sinfo, 126 &v3_policy_constraints, 127 #ifndef OPENSSL_NO_OCSP 128 &v3_crl_hold, 129 #endif 130 &v3_name_constraints, 131 &v3_policy_mappings, 132 &v3_inhibit_anyp, 133 &v3_idp, 134 &v3_alt[2], 135 &v3_freshest_crl, 136 #ifndef OPENSSL_NO_CT 137 &v3_ct_scts[0], 138 &v3_ct_scts[1], 139 &v3_ct_scts[2], 140 #endif 141 }; 142 143 #define STANDARD_EXTENSION_COUNT (sizeof(standard_exts) / sizeof(standard_exts[0])) 144 145 static int 146 ext_cmp(const X509V3_EXT_METHOD * const *a, const X509V3_EXT_METHOD * const *b) 147 { 148 return ((*a)->ext_nid - (*b)->ext_nid); 149 } 150 151 int 152 X509V3_EXT_add(X509V3_EXT_METHOD *ext) 153 { 154 if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) { 155 X509V3error(ERR_R_MALLOC_FAILURE); 156 return 0; 157 } 158 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { 159 X509V3error(ERR_R_MALLOC_FAILURE); 160 return 0; 161 } 162 return 1; 163 } 164 LCRYPTO_ALIAS(X509V3_EXT_add); 165 166 static int 167 ext_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) 168 { 169 const X509V3_EXT_METHOD * const *a = a_; 170 const X509V3_EXT_METHOD * const *b = b_; 171 return ext_cmp(a, b); 172 } 173 174 static const X509V3_EXT_METHOD ** 175 OBJ_bsearch_ext(const X509V3_EXT_METHOD **key, 176 const X509V3_EXT_METHOD *const *base, int num) 177 { 178 return (const X509V3_EXT_METHOD **)OBJ_bsearch_(key, base, num, 179 sizeof(const X509V3_EXT_METHOD *), ext_cmp_BSEARCH_CMP_FN); 180 } 181 182 const X509V3_EXT_METHOD * 183 X509V3_EXT_get_nid(int nid) 184 { 185 X509V3_EXT_METHOD tmp; 186 const X509V3_EXT_METHOD *t = &tmp, * const *ret; 187 int idx; 188 189 if (nid < 0) 190 return NULL; 191 tmp.ext_nid = nid; 192 ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT); 193 if (ret) 194 return *ret; 195 if (!ext_list) 196 return NULL; 197 idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp); 198 if (idx == -1) 199 return NULL; 200 return sk_X509V3_EXT_METHOD_value(ext_list, idx); 201 } 202 LCRYPTO_ALIAS(X509V3_EXT_get_nid); 203 204 const X509V3_EXT_METHOD * 205 X509V3_EXT_get(X509_EXTENSION *ext) 206 { 207 int nid; 208 209 if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) 210 return NULL; 211 return X509V3_EXT_get_nid(nid); 212 } 213 LCRYPTO_ALIAS(X509V3_EXT_get); 214 215 int 216 X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist) 217 { 218 for (; extlist->ext_nid!=-1; extlist++) 219 if (!X509V3_EXT_add(extlist)) 220 return 0; 221 return 1; 222 } 223 LCRYPTO_ALIAS(X509V3_EXT_add_list); 224 225 int 226 X509V3_EXT_add_alias(int nid_to, int nid_from) 227 { 228 const X509V3_EXT_METHOD *ext; 229 X509V3_EXT_METHOD *tmpext; 230 231 if (!(ext = X509V3_EXT_get_nid(nid_from))) { 232 X509V3error(X509V3_R_EXTENSION_NOT_FOUND); 233 return 0; 234 } 235 if (!(tmpext = malloc(sizeof(X509V3_EXT_METHOD)))) { 236 X509V3error(ERR_R_MALLOC_FAILURE); 237 return 0; 238 } 239 *tmpext = *ext; 240 tmpext->ext_nid = nid_to; 241 tmpext->ext_flags |= X509V3_EXT_DYNAMIC; 242 if (!X509V3_EXT_add(tmpext)) { 243 free(tmpext); 244 return 0; 245 } 246 return 1; 247 } 248 LCRYPTO_ALIAS(X509V3_EXT_add_alias); 249 250 static void 251 ext_list_free(X509V3_EXT_METHOD *ext) 252 { 253 if (ext->ext_flags & X509V3_EXT_DYNAMIC) 254 free(ext); 255 } 256 257 void 258 X509V3_EXT_cleanup(void) 259 { 260 sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free); 261 ext_list = NULL; 262 } 263 LCRYPTO_ALIAS(X509V3_EXT_cleanup); 264 265 int 266 X509V3_add_standard_extensions(void) 267 { 268 return 1; 269 } 270 LCRYPTO_ALIAS(X509V3_add_standard_extensions); 271 272 /* Return an extension internal structure */ 273 274 void * 275 X509V3_EXT_d2i(X509_EXTENSION *ext) 276 { 277 const X509V3_EXT_METHOD *method; 278 const unsigned char *p; 279 280 if (!(method = X509V3_EXT_get(ext))) 281 return NULL; 282 p = ext->value->data; 283 if (method->it) 284 return ASN1_item_d2i(NULL, &p, ext->value->length, 285 method->it); 286 return method->d2i(NULL, &p, ext->value->length); 287 } 288 LCRYPTO_ALIAS(X509V3_EXT_d2i); 289 290 /* Get critical flag and decoded version of extension from a NID. 291 * The "idx" variable returns the last found extension and can 292 * be used to retrieve multiple extensions of the same NID. 293 * However multiple extensions with the same NID is usually 294 * due to a badly encoded certificate so if idx is NULL we 295 * choke if multiple extensions exist. 296 * The "crit" variable is set to the critical value. 297 * The return value is the decoded extension or NULL on 298 * error. The actual error can have several different causes, 299 * the value of *crit reflects the cause: 300 * >= 0, extension found but not decoded (reflects critical value). 301 * -1 extension not found. 302 * -2 extension occurs more than once. 303 */ 304 305 void * 306 X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx) 307 { 308 int lastpos, i; 309 X509_EXTENSION *ex, *found_ex = NULL; 310 311 if (!x) { 312 if (idx) 313 *idx = -1; 314 if (crit) 315 *crit = -1; 316 return NULL; 317 } 318 if (idx) 319 lastpos = *idx + 1; 320 else 321 lastpos = 0; 322 if (lastpos < 0) 323 lastpos = 0; 324 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) { 325 ex = sk_X509_EXTENSION_value(x, i); 326 if (OBJ_obj2nid(ex->object) == nid) { 327 if (idx) { 328 *idx = i; 329 found_ex = ex; 330 break; 331 } else if (found_ex) { 332 /* Found more than one */ 333 if (crit) 334 *crit = -2; 335 return NULL; 336 } 337 found_ex = ex; 338 } 339 } 340 if (found_ex) { 341 /* Found it */ 342 if (crit) 343 *crit = X509_EXTENSION_get_critical(found_ex); 344 return X509V3_EXT_d2i(found_ex); 345 } 346 347 /* Extension not found */ 348 if (idx) 349 *idx = -1; 350 if (crit) 351 *crit = -1; 352 return NULL; 353 } 354 LCRYPTO_ALIAS(X509V3_get_d2i); 355 356 /* This function is a general extension append, replace and delete utility. 357 * The precise operation is governed by the 'flags' value. The 'crit' and 358 * 'value' arguments (if relevant) are the extensions internal structure. 359 */ 360 361 int 362 X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, 363 int crit, unsigned long flags) 364 { 365 int extidx = -1; 366 int errcode; 367 X509_EXTENSION *ext, *extmp; 368 unsigned long ext_op = flags & X509V3_ADD_OP_MASK; 369 370 /* If appending we don't care if it exists, otherwise 371 * look for existing extension. 372 */ 373 if (ext_op != X509V3_ADD_APPEND) 374 extidx = X509v3_get_ext_by_NID(*x, nid, -1); 375 376 /* See if extension exists */ 377 if (extidx >= 0) { 378 /* If keep existing, nothing to do */ 379 if (ext_op == X509V3_ADD_KEEP_EXISTING) 380 return 1; 381 /* If default then its an error */ 382 if (ext_op == X509V3_ADD_DEFAULT) { 383 errcode = X509V3_R_EXTENSION_EXISTS; 384 goto err; 385 } 386 /* If delete, just delete it */ 387 if (ext_op == X509V3_ADD_DELETE) { 388 if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL) 389 return -1; 390 X509_EXTENSION_free(extmp); 391 return 1; 392 } 393 } else { 394 /* If replace existing or delete, error since 395 * extension must exist 396 */ 397 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || 398 (ext_op == X509V3_ADD_DELETE)) { 399 errcode = X509V3_R_EXTENSION_NOT_FOUND; 400 goto err; 401 } 402 } 403 404 /* If we get this far then we have to create an extension: 405 * could have some flags for alternative encoding schemes... 406 */ 407 408 ext = X509V3_EXT_i2d(nid, crit, value); 409 410 if (!ext) { 411 X509V3error(X509V3_R_ERROR_CREATING_EXTENSION); 412 return 0; 413 } 414 415 /* If extension exists replace it.. */ 416 if (extidx >= 0) { 417 extmp = sk_X509_EXTENSION_value(*x, extidx); 418 X509_EXTENSION_free(extmp); 419 if (!sk_X509_EXTENSION_set(*x, extidx, ext)) 420 return -1; 421 return 1; 422 } 423 424 if (!*x && !(*x = sk_X509_EXTENSION_new_null())) 425 return -1; 426 if (!sk_X509_EXTENSION_push(*x, ext)) 427 return -1; 428 429 return 1; 430 431 err: 432 if (!(flags & X509V3_ADD_SILENT)) 433 X509V3error(errcode); 434 return 0; 435 } 436 LCRYPTO_ALIAS(X509V3_add1_i2d); 437