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