2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* X509 v3 extension utilities */
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
19 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
21 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
22 const X509V3_EXT_METHOD *const *b);
23 static void ext_list_free(X509V3_EXT_METHOD *ext);
25 int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
28 && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
29 X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE);
32 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
33 X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE);
39 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
40 const X509V3_EXT_METHOD *const *b)
42 return ((*a)->ext_nid - (*b)->ext_nid);
45 DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
46 const X509V3_EXT_METHOD *, ext);
47 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
48 const X509V3_EXT_METHOD *, ext);
50 #include "standard_exts.h"
52 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
54 X509V3_EXT_METHOD tmp;
55 const X509V3_EXT_METHOD *t = &tmp, *const *ret;
60 ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
65 idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
68 return sk_X509V3_EXT_METHOD_value(ext_list, idx);
71 const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
74 if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
76 return X509V3_EXT_get_nid(nid);
79 int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
81 for (; extlist->ext_nid != -1; extlist++)
82 if (!X509V3_EXT_add(extlist))
87 int X509V3_EXT_add_alias(int nid_to, int nid_from)
89 const X509V3_EXT_METHOD *ext;
90 X509V3_EXT_METHOD *tmpext;
92 if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
93 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, X509V3_R_EXTENSION_NOT_FOUND);
96 if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL) {
97 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, ERR_R_MALLOC_FAILURE);
101 tmpext->ext_nid = nid_to;
102 tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
103 return X509V3_EXT_add(tmpext);
106 void X509V3_EXT_cleanup(void)
108 sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
112 static void ext_list_free(X509V3_EXT_METHOD *ext)
114 if (ext->ext_flags & X509V3_EXT_DYNAMIC)
119 * Legacy function: we don't need to add standard extensions any more because
120 * they are now kept in ext_dat.h.
123 int X509V3_add_standard_extensions(void)
128 /* Return an extension internal structure */
130 void *X509V3_EXT_d2i(X509_EXTENSION *ext)
132 const X509V3_EXT_METHOD *method;
133 const unsigned char *p;
134 ASN1_STRING *extvalue;
137 if ((method = X509V3_EXT_get(ext)) == NULL)
139 extvalue = X509_EXTENSION_get_data(ext);
140 p = ASN1_STRING_get0_data(extvalue);
141 extlen = ASN1_STRING_length(extvalue);
143 return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
144 return method->d2i(NULL, &p, extlen);
148 * Get critical flag and decoded version of extension from a NID.
149 * The "idx" variable returns the last found extension and can
150 * be used to retrieve multiple extensions of the same NID.
151 * However multiple extensions with the same NID is usually
152 * due to a badly encoded certificate so if idx is NULL we
153 * choke if multiple extensions exist.
154 * The "crit" variable is set to the critical value.
155 * The return value is the decoded extension or NULL on
156 * error. The actual error can have several different causes,
157 * the value of *crit reflects the cause:
158 * >= 0, extension found but not decoded (reflects critical value).
159 * -1 extension not found.
160 * -2 extension occurs more than once.
163 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
167 X509_EXTENSION *ex, *found_ex = NULL;
181 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
182 ex = sk_X509_EXTENSION_value(x, i);
183 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
188 } else if (found_ex) {
189 /* Found more than one */
200 *crit = X509_EXTENSION_get_critical(found_ex);
201 return X509V3_EXT_d2i(found_ex);
204 /* Extension not found */
213 * This function is a general extension append, replace and delete utility.
214 * The precise operation is governed by the 'flags' value. The 'crit' and
215 * 'value' arguments (if relevant) are the extensions internal structure.
218 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
219 int crit, unsigned long flags)
223 X509_EXTENSION *ext, *extmp;
224 unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
227 * If appending we don't care if it exists, otherwise look for existing
230 if (ext_op != X509V3_ADD_APPEND)
231 extidx = X509v3_get_ext_by_NID(*x, nid, -1);
233 /* See if extension exists */
235 /* If keep existing, nothing to do */
236 if (ext_op == X509V3_ADD_KEEP_EXISTING)
238 /* If default then its an error */
239 if (ext_op == X509V3_ADD_DEFAULT) {
240 errcode = X509V3_R_EXTENSION_EXISTS;
243 /* If delete, just delete it */
244 if (ext_op == X509V3_ADD_DELETE) {
245 if (!sk_X509_EXTENSION_delete(*x, extidx))
251 * If replace existing or delete, error since extension must exist
253 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
254 (ext_op == X509V3_ADD_DELETE)) {
255 errcode = X509V3_R_EXTENSION_NOT_FOUND;
261 * If we get this far then we have to create an extension: could have
262 * some flags for alternative encoding schemes...
265 ext = X509V3_EXT_i2d(nid, crit, value);
268 X509V3err(X509V3_F_X509V3_ADD1_I2D,
269 X509V3_R_ERROR_CREATING_EXTENSION);
273 /* If extension exists replace it.. */
275 extmp = sk_X509_EXTENSION_value(*x, extidx);
276 X509_EXTENSION_free(extmp);
277 if (!sk_X509_EXTENSION_set(*x, extidx, ext))
283 && (*x = sk_X509_EXTENSION_new_null()) == NULL)
285 if (!sk_X509_EXTENSION_push(*x, ext))
291 if (!(flags & X509V3_ADD_SILENT))
292 X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode);