| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
// X509 v3 extension utilities |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include <openssl/conf.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/obj.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
|
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
#include "ext_dat.h" |
| 19 |
|
| 20 |
DEFINE_STACK_OF(X509V3_EXT_METHOD) |
| 21 |
|
| 22 |
static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
| 23 |
|
| 24 |
static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a, |
| 25 |
const X509V3_EXT_METHOD *const *b) { |
| 26 |
return ((*a)->ext_nid - (*b)->ext_nid); |
| 27 |
} |
| 28 |
|
| 29 |
int X509V3_EXT_add(X509V3_EXT_METHOD *ext) { |
| 30 |
// We only support |ASN1_ITEM|-based extensions. |
| 31 |
assert(ext->it != NULL); |
| 32 |
|
| 33 |
// TODO(davidben): This should be locked. Also check for duplicates. |
| 34 |
if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) { |
| 35 |
return 0; |
| 36 |
} |
| 37 |
if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
sk_X509V3_EXT_METHOD_sort(ext_list); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
static int ext_cmp(const void *void_a, const void *void_b) { |
| 45 |
const X509V3_EXT_METHOD **a = (const X509V3_EXT_METHOD **)void_a; |
| 46 |
const X509V3_EXT_METHOD **b = (const X509V3_EXT_METHOD **)void_b; |
| 47 |
return ext_stack_cmp(a, b); |
| 48 |
} |
| 49 |
|
| 50 |
static int x509v3_ext_method_validate(const X509V3_EXT_METHOD *ext_method) { |
| 51 |
if (ext_method == NULL) { |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
if (ext_method->ext_nid == NID_id_pkix_OCSP_Nonce && |
| 56 |
ext_method->d2i != NULL && ext_method->i2d != NULL && |
| 57 |
ext_method->ext_new != NULL && ext_method->ext_free != NULL) { |
| 58 |
// |NID_id_pkix_OCSP_Nonce| is the only extension using the "old-style" |
| 59 |
// ASN.1 callbacks for backwards compatibility reasons. |
| 60 |
// Note: See |v3_ext_method| under "include/openssl/x509.h". |
| 61 |
return 1; |
| 62 |
} |
| 63 |
|
| 64 |
if (ext_method->it == NULL) { |
| 65 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
return 1; |
| 69 |
} |
| 70 |
|
| 71 |
const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) { |
| 72 |
X509V3_EXT_METHOD tmp; |
| 73 |
const X509V3_EXT_METHOD *t = &tmp, *const * ret; |
| 74 |
size_t idx; |
| 75 |
|
| 76 |
if (nid < 0) { |
| 77 |
return NULL; |
| 78 |
} |
| 79 |
tmp.ext_nid = nid; |
| 80 |
ret = bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT, |
| 81 |
sizeof(X509V3_EXT_METHOD *), ext_cmp); |
| 82 |
if (ret != NULL && x509v3_ext_method_validate(*ret)) { |
| 83 |
return *ret; |
| 84 |
} |
| 85 |
if (!ext_list) { |
| 86 |
return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
if (!sk_X509V3_EXT_METHOD_find_awslc(ext_list, &idx, &tmp)) { |
| 90 |
return NULL; |
| 91 |
} |
| 92 |
|
| 93 |
const X509V3_EXT_METHOD *method = sk_X509V3_EXT_METHOD_value(ext_list, idx); |
| 94 |
if (method != NULL && x509v3_ext_method_validate(method)) { |
| 95 |
return method; |
| 96 |
} |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) { |
| 101 |
int nid; |
| 102 |
if (ext == NULL || (nid = OBJ_obj2nid(ext->object)) == NID_undef) { |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
return X509V3_EXT_get_nid(nid); |
| 106 |
} |
| 107 |
|
| 108 |
int x509v3_ext_free_with_method(const X509V3_EXT_METHOD *ext_method, |
| 109 |
void *ext_data) { |
| 110 |
if (ext_method == NULL) { |
| 111 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION); |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
if (ext_method->it != NULL) { |
| 116 |
ASN1_item_free(ext_data, ASN1_ITEM_ptr(ext_method->it)); |
| 117 |
} else if (ext_method->ext_nid == NID_id_pkix_OCSP_Nonce && |
| 118 |
ext_method->ext_free != NULL) { |
| 119 |
// |NID_id_pkix_OCSP_Nonce| is the only extension using the "old-style" |
| 120 |
// ASN.1 callbacks for backwards compatibility reasons. |
| 121 |
// Note: See |v3_ext_method| under "include/openssl/x509.h". |
| 122 |
ext_method->ext_free(ext_data); |
| 123 |
} else { |
| 124 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION); |
| 125 |
return 0; |
| 126 |
} |
| 127 |
return 1; |
| 128 |
} |
| 129 |
|
| 130 |
int X509V3_EXT_free(int nid, void *ext_data) { |
| 131 |
return x509v3_ext_free_with_method(X509V3_EXT_get_nid(nid), ext_data); |
| 132 |
} |
| 133 |
|
| 134 |
int X509V3_EXT_add_alias(int nid_to, int nid_from) { |
| 135 |
OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 136 |
const X509V3_EXT_METHOD *ext; |
| 137 |
X509V3_EXT_METHOD *tmpext; |
| 138 |
|
| 139 |
if (!(ext = X509V3_EXT_get_nid(nid_from))) { |
| 140 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NOT_FOUND); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
if (!(tmpext = |
| 144 |
(X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) { |
| 145 |
return 0; |
| 146 |
} |
| 147 |
*tmpext = *ext; |
| 148 |
tmpext->ext_nid = nid_to; |
| 149 |
if (!X509V3_EXT_add(tmpext)) { |
| 150 |
OPENSSL_free(tmpext); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
return 1; |
| 154 |
OPENSSL_END_ALLOW_DEPRECATED |
| 155 |
} |
| 156 |
|
| 157 |
// Legacy function: we don't need to add standard extensions any more because |
| 158 |
// they are now kept in ext_dat.h. |
| 159 |
|
| 160 |
int X509V3_add_standard_extensions(void) { return 1; } |
| 161 |
|
| 162 |
// Return an extension internal structure |
| 163 |
|
| 164 |
void *X509V3_EXT_d2i(const X509_EXTENSION *ext) { |
| 165 |
const X509V3_EXT_METHOD *method; |
| 166 |
const unsigned char *p; |
| 167 |
|
| 168 |
if (!(method = X509V3_EXT_get(ext))) { |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
p = ext->value->data; |
| 172 |
void *ret = NULL; |
| 173 |
if (method->it) { |
| 174 |
ret = |
| 175 |
ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it)); |
| 176 |
} else if (method->ext_nid == NID_id_pkix_OCSP_Nonce && method->d2i != NULL) { |
| 177 |
// |NID_id_pkix_OCSP_Nonce| is the only extension using the "old-style" |
| 178 |
// ASN.1 callbacks for backwards compatibility reasons. |
| 179 |
// Note: See |v3_ext_method| under "include/openssl/x509.h". |
| 180 |
ret = method->d2i(NULL, &p, ext->value->length); |
| 181 |
} else { |
| 182 |
assert(0); |
| 183 |
} |
| 184 |
|
| 185 |
if (ret == NULL) { |
| 186 |
return NULL; |
| 187 |
} |
| 188 |
// Check for trailing data. |
| 189 |
if (p != ext->value->data + ext->value->length) { |
| 190 |
x509v3_ext_free_with_method(method, ret); |
| 191 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
return ret; |
| 195 |
} |
| 196 |
|
| 197 |
void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid, |
| 198 |
int *out_critical, int *out_idx) { |
| 199 |
int lastpos; |
| 200 |
X509_EXTENSION *ex, *found_ex = NULL; |
| 201 |
if (!extensions) { |
| 202 |
if (out_idx) { |
| 203 |
*out_idx = -1; |
| 204 |
} |
| 205 |
if (out_critical) { |
| 206 |
*out_critical = -1; |
| 207 |
} |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
if (out_idx) { |
| 211 |
lastpos = *out_idx + 1; |
| 212 |
} else { |
| 213 |
lastpos = 0; |
| 214 |
} |
| 215 |
if (lastpos < 0) { |
| 216 |
lastpos = 0; |
| 217 |
} |
| 218 |
for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) { |
| 219 |
ex = sk_X509_EXTENSION_value(extensions, i); |
| 220 |
if (OBJ_obj2nid(ex->object) == nid) { |
| 221 |
if (out_idx) { |
| 222 |
// TODO(https://crbug.com/boringssl/379): Consistently reject |
| 223 |
// duplicate extensions. |
| 224 |
*out_idx = (int)i; |
| 225 |
found_ex = ex; |
| 226 |
break; |
| 227 |
} else if (found_ex) { |
| 228 |
// Found more than one |
| 229 |
if (out_critical) { |
| 230 |
*out_critical = -2; |
| 231 |
} |
| 232 |
return NULL; |
| 233 |
} |
| 234 |
found_ex = ex; |
| 235 |
} |
| 236 |
} |
| 237 |
if (found_ex) { |
| 238 |
// Found it |
| 239 |
if (out_critical) { |
| 240 |
*out_critical = X509_EXTENSION_get_critical(found_ex); |
| 241 |
} |
| 242 |
return X509V3_EXT_d2i(found_ex); |
| 243 |
} |
| 244 |
|
| 245 |
// Extension not found |
| 246 |
if (out_idx) { |
| 247 |
*out_idx = -1; |
| 248 |
} |
| 249 |
if (out_critical) { |
| 250 |
*out_critical = -1; |
| 251 |
} |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
|
| 255 |
// This function is a general extension append, replace and delete utility. |
| 256 |
// The precise operation is governed by the 'flags' value. The 'crit' and |
| 257 |
// 'value' arguments (if relevant) are the extensions internal structure. |
| 258 |
|
| 259 |
int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, |
| 260 |
int crit, unsigned long flags) { |
| 261 |
int errcode, extidx = -1; |
| 262 |
X509_EXTENSION *ext = NULL, *extmp; |
| 263 |
STACK_OF(X509_EXTENSION) *ret = NULL; |
| 264 |
unsigned long ext_op = flags & X509V3_ADD_OP_MASK; |
| 265 |
|
| 266 |
// If appending we don't care if it exists, otherwise look for existing |
| 267 |
// extension. |
| 268 |
if (ext_op != X509V3_ADD_APPEND) { |
| 269 |
extidx = X509v3_get_ext_by_NID(*x, nid, -1); |
| 270 |
} |
| 271 |
|
| 272 |
// See if extension exists |
| 273 |
if (extidx >= 0) { |
| 274 |
// If keep existing, nothing to do |
| 275 |
if (ext_op == X509V3_ADD_KEEP_EXISTING) { |
| 276 |
return 1; |
| 277 |
} |
| 278 |
// If default then its an error |
| 279 |
if (ext_op == X509V3_ADD_DEFAULT) { |
| 280 |
errcode = X509V3_R_EXTENSION_EXISTS; |
| 281 |
goto err; |
| 282 |
} |
| 283 |
// If delete, just delete it |
| 284 |
if (ext_op == X509V3_ADD_DELETE) { |
| 285 |
X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx); |
| 286 |
if (prev_ext == NULL) { |
| 287 |
return -1; |
| 288 |
} |
| 289 |
X509_EXTENSION_free(prev_ext); |
| 290 |
return 1; |
| 291 |
} |
| 292 |
} else { |
| 293 |
// If replace existing or delete, error since extension must exist |
| 294 |
if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || |
| 295 |
(ext_op == X509V3_ADD_DELETE)) { |
| 296 |
errcode = X509V3_R_EXTENSION_NOT_FOUND; |
| 297 |
goto err; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// If we get this far then we have to create an extension: could have |
| 302 |
// some flags for alternative encoding schemes... |
| 303 |
|
| 304 |
ext = X509V3_EXT_i2d(nid, crit, value); |
| 305 |
|
| 306 |
if (!ext) { |
| 307 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION); |
| 308 |
return 0; |
| 309 |
} |
| 310 |
|
| 311 |
// If extension exists replace it.. |
| 312 |
if (extidx >= 0) { |
| 313 |
extmp = sk_X509_EXTENSION_value(*x, extidx); |
| 314 |
X509_EXTENSION_free(extmp); |
| 315 |
if (!sk_X509_EXTENSION_set(*x, extidx, ext)) { |
| 316 |
return -1; |
| 317 |
} |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) { |
| 322 |
goto m_fail; |
| 323 |
} |
| 324 |
if (!sk_X509_EXTENSION_push(ret, ext)) { |
| 325 |
goto m_fail; |
| 326 |
} |
| 327 |
|
| 328 |
*x = ret; |
| 329 |
return 1; |
| 330 |
|
| 331 |
m_fail: |
| 332 |
if (ret != *x) { |
| 333 |
sk_X509_EXTENSION_free(ret); |
| 334 |
} |
| 335 |
X509_EXTENSION_free(ext); |
| 336 |
return -1; |
| 337 |
|
| 338 |
err: |
| 339 |
if (!(flags & X509V3_ADD_SILENT)) { |
| 340 |
OPENSSL_PUT_ERROR(X509V3, errcode); |
| 341 |
} |
| 342 |
return 0; |
| 343 |
} |