1/* $OpenBSD: x509_v3.c,v 1.30 2024/05/23 02:00:38 tb Exp $ */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59#include <stdio.h> 60 61#include <openssl/asn1.h> 62#include <openssl/err.h> 63#include <openssl/evp.h> 64#include <openssl/objects.h> 65#include <openssl/stack.h> 66#include <openssl/x509.h> 67#include <openssl/x509v3.h> 68 69#include "x509_local.h" 70 71int 72X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *sk) 73{ 74 if (sk == NULL) 75 return 0; 76 77 return sk_X509_EXTENSION_num(sk); 78} 79LCRYPTO_ALIAS(X509v3_get_ext_count); 80 81int 82X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *sk, int nid, int lastpos) 83{ 84 const ASN1_OBJECT *obj; 85 86 if ((obj = OBJ_nid2obj(nid)) == NULL) 87 return -2; 88 89 return X509v3_get_ext_by_OBJ(sk, obj, lastpos); 90} 91LCRYPTO_ALIAS(X509v3_get_ext_by_NID); 92 93int 94X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, 95 const ASN1_OBJECT *obj, int lastpos) 96{ 97 int n; 98 X509_EXTENSION *ext; 99 100 if (sk == NULL) 101 return -1; 102 lastpos++; 103 if (lastpos < 0) 104 lastpos = 0; 105 n = sk_X509_EXTENSION_num(sk); 106 for (; lastpos < n; lastpos++) { 107 ext = sk_X509_EXTENSION_value(sk, lastpos); 108 if (OBJ_cmp(ext->object, obj) == 0) 109 return lastpos; 110 } 111 return -1; 112} 113LCRYPTO_ALIAS(X509v3_get_ext_by_OBJ); 114 115int 116X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit, 117 int lastpos) 118{ 119 int n; 120 X509_EXTENSION *ext; 121 122 if (sk == NULL) 123 return -1; 124 lastpos++; 125 if (lastpos < 0) 126 lastpos = 0; 127 n = sk_X509_EXTENSION_num(sk); 128 for (; lastpos < n; lastpos++) { 129 ext = sk_X509_EXTENSION_value(sk, lastpos); 130 if ((ext->critical > 0 && crit) || 131 (ext->critical <= 0 && !crit)) 132 return lastpos; 133 } 134 return -1; 135} 136LCRYPTO_ALIAS(X509v3_get_ext_by_critical); 137 138X509_EXTENSION * 139X509v3_get_ext(const STACK_OF(X509_EXTENSION) *sk, int loc) 140{ 141 if (sk == NULL || sk_X509_EXTENSION_num(sk) <= loc || loc < 0) 142 return NULL; 143 144 return sk_X509_EXTENSION_value(sk, loc); 145} 146LCRYPTO_ALIAS(X509v3_get_ext); 147 148X509_EXTENSION * 149X509v3_delete_ext(STACK_OF(X509_EXTENSION) *sk, int loc) 150{ 151 if (sk == NULL || sk_X509_EXTENSION_num(sk) <= loc || loc < 0) 152 return NULL; 153 154 return sk_X509_EXTENSION_delete(sk, loc); 155} 156LCRYPTO_ALIAS(X509v3_delete_ext); 157 158STACK_OF(X509_EXTENSION) * 159X509v3_add_ext(STACK_OF(X509_EXTENSION) **x, X509_EXTENSION *ext, int loc) 160{ 161 X509_EXTENSION *new_ext = NULL; 162 int n; 163 STACK_OF(X509_EXTENSION) *sk = NULL; 164 165 if (x == NULL) { 166 X509error(ERR_R_PASSED_NULL_PARAMETER); 167 goto err2; 168 } 169 170 if (*x == NULL) { 171 if ((sk = sk_X509_EXTENSION_new_null()) == NULL) 172 goto err; 173 } else 174 sk= *x; 175 176 n = sk_X509_EXTENSION_num(sk); 177 if (loc > n) 178 loc = n; 179 else if (loc < 0) 180 loc = n; 181 182 if ((new_ext = X509_EXTENSION_dup(ext)) == NULL) 183 goto err2; 184 if (!sk_X509_EXTENSION_insert(sk, new_ext, loc)) 185 goto err; 186 if (*x == NULL) 187 *x = sk; 188 return sk; 189 190 err: 191 X509error(ERR_R_MALLOC_FAILURE); 192 err2: 193 if (new_ext != NULL) 194 X509_EXTENSION_free(new_ext); 195 if (sk != NULL && x != NULL && sk != *x) 196 sk_X509_EXTENSION_free(sk); 197 return NULL; 198} 199LCRYPTO_ALIAS(X509v3_add_ext); 200 201X509_EXTENSION * 202X509_EXTENSION_create_by_NID(X509_EXTENSION **ext, int nid, int crit, 203 ASN1_OCTET_STRING *data) 204{ 205 ASN1_OBJECT *obj; 206 X509_EXTENSION *ret; 207 208 obj = OBJ_nid2obj(nid); 209 if (obj == NULL) { 210 X509error(X509_R_UNKNOWN_NID); 211 return NULL; 212 } 213 ret = X509_EXTENSION_create_by_OBJ(ext, obj, crit, data); 214 if (ret == NULL) 215 ASN1_OBJECT_free(obj); 216 return ret; 217} 218LCRYPTO_ALIAS(X509_EXTENSION_create_by_NID); 219 220X509_EXTENSION * 221X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ext, const ASN1_OBJECT *obj, 222 int crit, ASN1_OCTET_STRING *data) 223{ 224 X509_EXTENSION *ret; 225 226 if (ext == NULL || *ext == NULL) { 227 if ((ret = X509_EXTENSION_new()) == NULL) { 228 X509error(ERR_R_MALLOC_FAILURE); 229 return NULL; 230 } 231 } else 232 ret= *ext; 233 234 if (!X509_EXTENSION_set_object(ret, obj)) 235 goto err; 236 if (!X509_EXTENSION_set_critical(ret, crit)) 237 goto err; 238 if (!X509_EXTENSION_set_data(ret, data)) 239 goto err; 240 241 if (ext != NULL && *ext == NULL) 242 *ext = ret; 243 return ret; 244 245 err: 246 if (ext == NULL || ret != *ext) 247 X509_EXTENSION_free(ret); 248 return NULL; 249} 250LCRYPTO_ALIAS(X509_EXTENSION_create_by_OBJ); 251 252int 253X509_EXTENSION_set_object(X509_EXTENSION *ext, const ASN1_OBJECT *obj) 254{ 255 if (ext == NULL || obj == NULL) 256 return 0; 257 258 ASN1_OBJECT_free(ext->object); 259 ext->object = OBJ_dup(obj); 260 261 return ext->object != NULL; 262} 263LCRYPTO_ALIAS(X509_EXTENSION_set_object); 264 265int 266X509_EXTENSION_set_critical(X509_EXTENSION *ext, int crit) 267{ 268 if (ext == NULL) 269 return 0; 270 271 ext->critical = crit ? 0xFF : -1; 272 273 return 1; 274} 275LCRYPTO_ALIAS(X509_EXTENSION_set_critical); 276 277int 278X509_EXTENSION_set_data(X509_EXTENSION *ext, ASN1_OCTET_STRING *data) 279{ 280 if (ext == NULL) 281 return 0; 282 283 return ASN1_STRING_set(ext->value, data->data, data->length); 284} 285LCRYPTO_ALIAS(X509_EXTENSION_set_data); 286 287ASN1_OBJECT * 288X509_EXTENSION_get_object(X509_EXTENSION *ext) 289{ 290 if (ext == NULL) 291 return NULL; 292 293 return ext->object; 294} 295LCRYPTO_ALIAS(X509_EXTENSION_get_object); 296 297ASN1_OCTET_STRING * 298X509_EXTENSION_get_data(X509_EXTENSION *ext) 299{ 300 if (ext == NULL) 301 return NULL; 302 303 return ext->value; 304} 305LCRYPTO_ALIAS(X509_EXTENSION_get_data); 306 307int 308X509_EXTENSION_get_critical(const X509_EXTENSION *ext) 309{ 310 if (ext == NULL) 311 return 0; 312 if (ext->critical > 0) 313 return 1; 314 return 0; 315} 316LCRYPTO_ALIAS(X509_EXTENSION_get_critical); 317