1 /* $OpenBSD: x509_lib.c,v 1.15 2024/01/25 12:20:17 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 extern const X509V3_EXT_METHOD v3_bcons, v3_nscert, v3_key_usage, v3_ext_ku; 69 extern const X509V3_EXT_METHOD v3_pkey_usage_period, v3_info, v3_sinfo; 70 extern const X509V3_EXT_METHOD v3_ns_ia5_list[], v3_alt[], v3_skey_id, v3_akey_id; 71 extern const X509V3_EXT_METHOD v3_crl_num, v3_crl_reason, v3_crl_invdate; 72 extern const X509V3_EXT_METHOD v3_delta_crl, v3_cpols, v3_crld, v3_freshest_crl; 73 extern const X509V3_EXT_METHOD v3_ocsp_nonce, v3_ocsp_accresp, v3_ocsp_acutoff; 74 extern const X509V3_EXT_METHOD v3_ocsp_crlid, v3_ocsp_nocheck, v3_ocsp_serviceloc; 75 extern const X509V3_EXT_METHOD v3_crl_hold; 76 extern const X509V3_EXT_METHOD v3_policy_mappings, v3_policy_constraints; 77 extern const X509V3_EXT_METHOD v3_name_constraints, v3_inhibit_anyp, v3_idp; 78 extern const X509V3_EXT_METHOD v3_addr, v3_asid; 79 extern const X509V3_EXT_METHOD v3_ct_scts[3]; 80 81 /* 82 * This table needs to be sorted by increasing ext_nid values for OBJ_bsearch_. 83 */ 84 85 static const X509V3_EXT_METHOD *standard_exts[] = { 86 &v3_nscert, 87 &v3_ns_ia5_list[0], 88 &v3_ns_ia5_list[1], 89 &v3_ns_ia5_list[2], 90 &v3_ns_ia5_list[3], 91 &v3_ns_ia5_list[4], 92 &v3_ns_ia5_list[5], 93 &v3_ns_ia5_list[6], 94 &v3_skey_id, 95 &v3_key_usage, 96 &v3_pkey_usage_period, 97 &v3_alt[0], 98 &v3_alt[1], 99 &v3_bcons, 100 &v3_crl_num, 101 &v3_cpols, 102 &v3_akey_id, 103 &v3_crld, 104 &v3_ext_ku, 105 &v3_delta_crl, 106 &v3_crl_reason, 107 #ifndef OPENSSL_NO_OCSP 108 &v3_crl_invdate, 109 #endif 110 &v3_info, 111 #ifndef OPENSSL_NO_RFC3779 112 &v3_addr, 113 &v3_asid, 114 #endif 115 #ifndef OPENSSL_NO_OCSP 116 &v3_ocsp_nonce, 117 &v3_ocsp_crlid, 118 &v3_ocsp_accresp, 119 &v3_ocsp_nocheck, 120 &v3_ocsp_acutoff, 121 &v3_ocsp_serviceloc, 122 #endif 123 &v3_sinfo, 124 &v3_policy_constraints, 125 #ifndef OPENSSL_NO_OCSP 126 &v3_crl_hold, 127 #endif 128 &v3_name_constraints, 129 &v3_policy_mappings, 130 &v3_inhibit_anyp, 131 &v3_idp, 132 &v3_alt[2], 133 &v3_freshest_crl, 134 #ifndef OPENSSL_NO_CT 135 &v3_ct_scts[0], 136 &v3_ct_scts[1], 137 &v3_ct_scts[2], 138 #endif 139 }; 140 141 #define STANDARD_EXTENSION_COUNT (sizeof(standard_exts) / sizeof(standard_exts[0])) 142 143 const X509V3_EXT_METHOD * 144 X509V3_EXT_get_nid(int nid) 145 { 146 size_t i; 147 148 for (i = 0; i < STANDARD_EXTENSION_COUNT; i++) { 149 if (standard_exts[i]->ext_nid == nid) 150 return standard_exts[i]; 151 } 152 153 return NULL; 154 } 155 LCRYPTO_ALIAS(X509V3_EXT_get_nid); 156 157 const X509V3_EXT_METHOD * 158 X509V3_EXT_get(X509_EXTENSION *ext) 159 { 160 int nid; 161 162 if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) 163 return NULL; 164 return X509V3_EXT_get_nid(nid); 165 } 166 LCRYPTO_ALIAS(X509V3_EXT_get); 167 168 int 169 X509V3_add_standard_extensions(void) 170 { 171 return 1; 172 } 173 LCRYPTO_ALIAS(X509V3_add_standard_extensions); 174 175 /* Return an extension internal structure */ 176 177 void * 178 X509V3_EXT_d2i(X509_EXTENSION *ext) 179 { 180 const X509V3_EXT_METHOD *method; 181 const unsigned char *p; 182 183 if (!(method = X509V3_EXT_get(ext))) 184 return NULL; 185 p = ext->value->data; 186 if (method->it) 187 return ASN1_item_d2i(NULL, &p, ext->value->length, 188 method->it); 189 return method->d2i(NULL, &p, ext->value->length); 190 } 191 LCRYPTO_ALIAS(X509V3_EXT_d2i); 192 193 /* Get critical flag and decoded version of extension from a NID. 194 * The "idx" variable returns the last found extension and can 195 * be used to retrieve multiple extensions of the same NID. 196 * However multiple extensions with the same NID is usually 197 * due to a badly encoded certificate so if idx is NULL we 198 * choke if multiple extensions exist. 199 * The "crit" variable is set to the critical value. 200 * The return value is the decoded extension or NULL on 201 * error. The actual error can have several different causes, 202 * the value of *crit reflects the cause: 203 * >= 0, extension found but not decoded (reflects critical value). 204 * -1 extension not found. 205 * -2 extension occurs more than once. 206 */ 207 208 void * 209 X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx) 210 { 211 int lastpos, i; 212 X509_EXTENSION *ex, *found_ex = NULL; 213 214 if (!x) { 215 if (idx) 216 *idx = -1; 217 if (crit) 218 *crit = -1; 219 return NULL; 220 } 221 if (idx) 222 lastpos = *idx + 1; 223 else 224 lastpos = 0; 225 if (lastpos < 0) 226 lastpos = 0; 227 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) { 228 ex = sk_X509_EXTENSION_value(x, i); 229 if (OBJ_obj2nid(ex->object) == nid) { 230 if (idx) { 231 *idx = i; 232 found_ex = ex; 233 break; 234 } else if (found_ex) { 235 /* Found more than one */ 236 if (crit) 237 *crit = -2; 238 return NULL; 239 } 240 found_ex = ex; 241 } 242 } 243 if (found_ex) { 244 /* Found it */ 245 if (crit) 246 *crit = X509_EXTENSION_get_critical(found_ex); 247 return X509V3_EXT_d2i(found_ex); 248 } 249 250 /* Extension not found */ 251 if (idx) 252 *idx = -1; 253 if (crit) 254 *crit = -1; 255 return NULL; 256 } 257 LCRYPTO_ALIAS(X509V3_get_d2i); 258 259 /* This function is a general extension append, replace and delete utility. 260 * The precise operation is governed by the 'flags' value. The 'crit' and 261 * 'value' arguments (if relevant) are the extensions internal structure. 262 */ 263 264 int 265 X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, 266 int crit, unsigned long flags) 267 { 268 int extidx = -1; 269 int errcode; 270 X509_EXTENSION *ext, *extmp; 271 unsigned long ext_op = flags & X509V3_ADD_OP_MASK; 272 273 /* If appending we don't care if it exists, otherwise 274 * look for existing extension. 275 */ 276 if (ext_op != X509V3_ADD_APPEND) 277 extidx = X509v3_get_ext_by_NID(*x, nid, -1); 278 279 /* See if extension exists */ 280 if (extidx >= 0) { 281 /* If keep existing, nothing to do */ 282 if (ext_op == X509V3_ADD_KEEP_EXISTING) 283 return 1; 284 /* If default then its an error */ 285 if (ext_op == X509V3_ADD_DEFAULT) { 286 errcode = X509V3_R_EXTENSION_EXISTS; 287 goto err; 288 } 289 /* If delete, just delete it */ 290 if (ext_op == X509V3_ADD_DELETE) { 291 if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL) 292 return -1; 293 X509_EXTENSION_free(extmp); 294 return 1; 295 } 296 } else { 297 /* If replace existing or delete, error since 298 * extension must exist 299 */ 300 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || 301 (ext_op == X509V3_ADD_DELETE)) { 302 errcode = X509V3_R_EXTENSION_NOT_FOUND; 303 goto err; 304 } 305 } 306 307 /* If we get this far then we have to create an extension: 308 * could have some flags for alternative encoding schemes... 309 */ 310 311 ext = X509V3_EXT_i2d(nid, crit, value); 312 313 if (!ext) { 314 X509V3error(X509V3_R_ERROR_CREATING_EXTENSION); 315 return 0; 316 } 317 318 /* If extension exists replace it.. */ 319 if (extidx >= 0) { 320 extmp = sk_X509_EXTENSION_value(*x, extidx); 321 X509_EXTENSION_free(extmp); 322 if (!sk_X509_EXTENSION_set(*x, extidx, ext)) 323 return -1; 324 return 1; 325 } 326 327 if (!*x && !(*x = sk_X509_EXTENSION_new_null())) 328 return -1; 329 if (!sk_X509_EXTENSION_push(*x, ext)) 330 return -1; 331 332 return 1; 333 334 err: 335 if (!(flags & X509V3_ADD_SILENT)) 336 X509V3error(errcode); 337 return 0; 338 } 339 LCRYPTO_ALIAS(X509V3_add1_i2d); 340 341 /* 342 * XXX - remove all the functions below in the next major bump. 343 */ 344 345 int 346 X509V3_EXT_add(X509V3_EXT_METHOD *ext) 347 { 348 X509V3error(ERR_R_DISABLED); 349 return 0; 350 } 351 LCRYPTO_ALIAS(X509V3_EXT_add); 352 353 int 354 X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist) 355 { 356 X509V3error(ERR_R_DISABLED); 357 return 0; 358 } 359 LCRYPTO_ALIAS(X509V3_EXT_add_list); 360 361 int 362 X509V3_EXT_add_alias(int nid_to, int nid_from) 363 { 364 X509V3error(ERR_R_DISABLED); 365 return 0; 366 } 367 LCRYPTO_ALIAS(X509V3_EXT_add_alias); 368 369 void 370 X509V3_EXT_cleanup(void) 371 { 372 } 373 LCRYPTO_ALIAS(X509V3_EXT_cleanup); 374