2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 DEFINE_STACK_OF(X509V3_EXT_METHOD
)
21 static STACK_OF(X509V3_EXT_METHOD
) *ext_list
= NULL
;
23 static int ext_cmp(const X509V3_EXT_METHOD
*const *a
,
24 const X509V3_EXT_METHOD
*const *b
);
25 static void ext_list_free(X509V3_EXT_METHOD
*ext
);
27 int X509V3_EXT_add(X509V3_EXT_METHOD
*ext
)
30 && (ext_list
= sk_X509V3_EXT_METHOD_new(ext_cmp
)) == NULL
) {
31 X509V3err(X509V3_F_X509V3_EXT_ADD
, ERR_R_MALLOC_FAILURE
);
34 if (!sk_X509V3_EXT_METHOD_push(ext_list
, ext
)) {
35 X509V3err(X509V3_F_X509V3_EXT_ADD
, ERR_R_MALLOC_FAILURE
);
41 static int ext_cmp(const X509V3_EXT_METHOD
*const *a
,
42 const X509V3_EXT_METHOD
*const *b
)
44 return ((*a
)->ext_nid
- (*b
)->ext_nid
);
47 DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD
*,
48 const X509V3_EXT_METHOD
*, ext
);
49 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD
*,
50 const X509V3_EXT_METHOD
*, ext
);
52 #include "standard_exts.h"
54 const X509V3_EXT_METHOD
*X509V3_EXT_get_nid(int nid
)
56 X509V3_EXT_METHOD tmp
;
57 const X509V3_EXT_METHOD
*t
= &tmp
, *const *ret
;
63 ret
= OBJ_bsearch_ext(&t
, standard_exts
, STANDARD_EXTENSION_COUNT
);
68 idx
= sk_X509V3_EXT_METHOD_find(ext_list
, &tmp
);
69 return sk_X509V3_EXT_METHOD_value(ext_list
, idx
);
72 const X509V3_EXT_METHOD
*X509V3_EXT_get(X509_EXTENSION
*ext
)
75 if ((nid
= OBJ_obj2nid(X509_EXTENSION_get_object(ext
))) == NID_undef
)
77 return X509V3_EXT_get_nid(nid
);
80 int X509V3_EXT_add_list(X509V3_EXT_METHOD
*extlist
)
82 for (; extlist
->ext_nid
!= -1; extlist
++)
83 if (!X509V3_EXT_add(extlist
))
88 int X509V3_EXT_add_alias(int nid_to
, int nid_from
)
90 const X509V3_EXT_METHOD
*ext
;
91 X509V3_EXT_METHOD
*tmpext
;
93 if ((ext
= X509V3_EXT_get_nid(nid_from
)) == NULL
) {
94 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS
, X509V3_R_EXTENSION_NOT_FOUND
);
97 if ((tmpext
= OPENSSL_malloc(sizeof(*tmpext
))) == NULL
) {
98 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS
, ERR_R_MALLOC_FAILURE
);
102 tmpext
->ext_nid
= nid_to
;
103 tmpext
->ext_flags
|= X509V3_EXT_DYNAMIC
;
104 return X509V3_EXT_add(tmpext
);
107 void X509V3_EXT_cleanup(void)
109 sk_X509V3_EXT_METHOD_pop_free(ext_list
, ext_list_free
);
113 static void ext_list_free(X509V3_EXT_METHOD
*ext
)
115 if (ext
->ext_flags
& X509V3_EXT_DYNAMIC
)
120 * Legacy function: we don't need to add standard extensions any more because
121 * they are now kept in ext_dat.h.
124 int X509V3_add_standard_extensions(void)
129 /* Return an extension internal structure */
131 void *X509V3_EXT_d2i(X509_EXTENSION
*ext
)
133 const X509V3_EXT_METHOD
*method
;
134 const unsigned char *p
;
135 ASN1_STRING
*extvalue
;
138 if ((method
= X509V3_EXT_get(ext
)) == NULL
)
140 extvalue
= X509_EXTENSION_get_data(ext
);
141 p
= ASN1_STRING_get0_data(extvalue
);
142 extlen
= ASN1_STRING_length(extvalue
);
144 return ASN1_item_d2i(NULL
, &p
, extlen
, ASN1_ITEM_ptr(method
->it
));
145 return method
->d2i(NULL
, &p
, extlen
);
149 * Get critical flag and decoded version of extension from a NID.
150 * The "idx" variable returns the last found extension and can
151 * be used to retrieve multiple extensions of the same NID.
152 * However multiple extensions with the same NID is usually
153 * due to a badly encoded certificate so if idx is NULL we
154 * choke if multiple extensions exist.
155 * The "crit" variable is set to the critical value.
156 * The return value is the decoded extension or NULL on
157 * error. The actual error can have several different causes,
158 * the value of *crit reflects the cause:
159 * >= 0, extension found but not decoded (reflects critical value).
160 * -1 extension not found.
161 * -2 extension occurs more than once.
164 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION
) *x
, int nid
, int *crit
,
168 X509_EXTENSION
*ex
, *found_ex
= NULL
;
183 for (i
= lastpos
; i
< sk_X509_EXTENSION_num(x
); i
++) {
184 ex
= sk_X509_EXTENSION_value(x
, i
);
185 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex
)) == nid
) {
190 } else if (found_ex
) {
191 /* Found more than one */
202 *crit
= X509_EXTENSION_get_critical(found_ex
);
203 return X509V3_EXT_d2i(found_ex
);
206 /* Extension not found */
215 * This function is a general extension append, replace and delete utility.
216 * The precise operation is governed by the 'flags' value. The 'crit' and
217 * 'value' arguments (if relevant) are the extensions internal structure.
220 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION
) **x
, int nid
, void *value
,
221 int crit
, unsigned long flags
)
223 int errcode
, extidx
= -1;
224 X509_EXTENSION
*ext
= NULL
, *extmp
;
225 STACK_OF(X509_EXTENSION
) *ret
= NULL
;
226 unsigned long ext_op
= flags
& X509V3_ADD_OP_MASK
;
229 * If appending we don't care if it exists, otherwise look for existing
232 if (ext_op
!= X509V3_ADD_APPEND
)
233 extidx
= X509v3_get_ext_by_NID(*x
, nid
, -1);
235 /* See if extension exists */
237 /* If keep existing, nothing to do */
238 if (ext_op
== X509V3_ADD_KEEP_EXISTING
)
240 /* If default then its an error */
241 if (ext_op
== X509V3_ADD_DEFAULT
) {
242 errcode
= X509V3_R_EXTENSION_EXISTS
;
245 /* If delete, just delete it */
246 if (ext_op
== X509V3_ADD_DELETE
) {
247 if (!sk_X509_EXTENSION_delete(*x
, extidx
))
253 * If replace existing or delete, error since extension must exist
255 if ((ext_op
== X509V3_ADD_REPLACE_EXISTING
) ||
256 (ext_op
== X509V3_ADD_DELETE
)) {
257 errcode
= X509V3_R_EXTENSION_NOT_FOUND
;
263 * If we get this far then we have to create an extension: could have
264 * some flags for alternative encoding schemes...
267 ext
= X509V3_EXT_i2d(nid
, crit
, value
);
270 X509V3err(X509V3_F_X509V3_ADD1_I2D
,
271 X509V3_R_ERROR_CREATING_EXTENSION
);
275 /* If extension exists replace it.. */
277 extmp
= sk_X509_EXTENSION_value(*x
, extidx
);
278 X509_EXTENSION_free(extmp
);
279 if (!sk_X509_EXTENSION_set(*x
, extidx
, ext
))
286 && (ret
= sk_X509_EXTENSION_new_null()) == NULL
)
288 if (!sk_X509_EXTENSION_push(ret
, ext
))
295 /* X509V3err(X509V3_F_X509V3_ADD1_I2D, ERR_R_MALLOC_FAILURE); */
297 sk_X509_EXTENSION_free(ret
);
298 X509_EXTENSION_free(ext
);
302 if (!(flags
& X509V3_ADD_SILENT
))
303 X509V3err(X509V3_F_X509V3_ADD1_I2D
, errcode
);