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
);
51 * This table will be searched using OBJ_bsearch so it *must* kept in order
52 * of the ext_nid values.
55 static const X509V3_EXT_METHOD
*standard_exts
[] = {
66 &v3_pkey_usage_period
,
77 #ifndef OPENSSL_NO_OCSP
82 #ifndef OPENSSL_NO_RFC3779
86 #ifndef OPENSSL_NO_OCSP
95 &v3_policy_constraints
,
96 #ifndef OPENSSL_NO_OCSP
100 &v3_name_constraints
,
106 #ifndef OPENSSL_NO_CT
114 /* Number of standard extensions */
116 #define STANDARD_EXTENSION_COUNT OSSL_NELEM(standard_exts)
118 const X509V3_EXT_METHOD
*X509V3_EXT_get_nid(int nid
)
120 X509V3_EXT_METHOD tmp
;
121 const X509V3_EXT_METHOD
*t
= &tmp
, *const *ret
;
126 ret
= OBJ_bsearch_ext(&t
, standard_exts
, STANDARD_EXTENSION_COUNT
);
131 idx
= sk_X509V3_EXT_METHOD_find(ext_list
, &tmp
);
134 return sk_X509V3_EXT_METHOD_value(ext_list
, idx
);
137 const X509V3_EXT_METHOD
*X509V3_EXT_get(X509_EXTENSION
*ext
)
140 if ((nid
= OBJ_obj2nid(X509_EXTENSION_get_object(ext
))) == NID_undef
)
142 return X509V3_EXT_get_nid(nid
);
145 int X509V3_EXT_add_list(X509V3_EXT_METHOD
*extlist
)
147 for (; extlist
->ext_nid
!= -1; extlist
++)
148 if (!X509V3_EXT_add(extlist
))
153 int X509V3_EXT_add_alias(int nid_to
, int nid_from
)
155 const X509V3_EXT_METHOD
*ext
;
156 X509V3_EXT_METHOD
*tmpext
;
158 if ((ext
= X509V3_EXT_get_nid(nid_from
)) == NULL
) {
159 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS
, X509V3_R_EXTENSION_NOT_FOUND
);
162 if ((tmpext
= OPENSSL_malloc(sizeof(*tmpext
))) == NULL
) {
163 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS
, ERR_R_MALLOC_FAILURE
);
167 tmpext
->ext_nid
= nid_to
;
168 tmpext
->ext_flags
|= X509V3_EXT_DYNAMIC
;
169 return X509V3_EXT_add(tmpext
);
172 void X509V3_EXT_cleanup(void)
174 sk_X509V3_EXT_METHOD_pop_free(ext_list
, ext_list_free
);
178 static void ext_list_free(X509V3_EXT_METHOD
*ext
)
180 if (ext
->ext_flags
& X509V3_EXT_DYNAMIC
)
185 * Legacy function: we don't need to add standard extensions any more because
186 * they are now kept in ext_dat.h.
189 int X509V3_add_standard_extensions(void)
194 /* Return an extension internal structure */
196 void *X509V3_EXT_d2i(X509_EXTENSION
*ext
)
198 const X509V3_EXT_METHOD
*method
;
199 const unsigned char *p
;
200 ASN1_STRING
*extvalue
;
203 if ((method
= X509V3_EXT_get(ext
)) == NULL
)
205 extvalue
= X509_EXTENSION_get_data(ext
);
206 p
= ASN1_STRING_data(extvalue
);
207 extlen
= ASN1_STRING_length(extvalue
);
209 return ASN1_item_d2i(NULL
, &p
, extlen
, ASN1_ITEM_ptr(method
->it
));
210 return method
->d2i(NULL
, &p
, extlen
);
214 * Get critical flag and decoded version of extension from a NID.
215 * The "idx" variable returns the last found extension and can
216 * be used to retrieve multiple extensions of the same NID.
217 * However multiple extensions with the same NID is usually
218 * due to a badly encoded certificate so if idx is NULL we
219 * choke if multiple extensions exist.
220 * The "crit" variable is set to the critical value.
221 * The return value is the decoded extension or NULL on
222 * error. The actual error can have several different causes,
223 * the value of *crit reflects the cause:
224 * >= 0, extension found but not decoded (reflects critical value).
225 * -1 extension not found.
226 * -2 extension occurs more than once.
229 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION
) *x
, int nid
, int *crit
,
233 X509_EXTENSION
*ex
, *found_ex
= NULL
;
247 for (i
= lastpos
; i
< sk_X509_EXTENSION_num(x
); i
++) {
248 ex
= sk_X509_EXTENSION_value(x
, i
);
249 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex
)) == nid
) {
254 } else if (found_ex
) {
255 /* Found more than one */
266 *crit
= X509_EXTENSION_get_critical(found_ex
);
267 return X509V3_EXT_d2i(found_ex
);
270 /* Extension not found */
279 * This function is a general extension append, replace and delete utility.
280 * The precise operation is governed by the 'flags' value. The 'crit' and
281 * 'value' arguments (if relevant) are the extensions internal structure.
284 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION
) **x
, int nid
, void *value
,
285 int crit
, unsigned long flags
)
289 X509_EXTENSION
*ext
, *extmp
;
290 unsigned long ext_op
= flags
& X509V3_ADD_OP_MASK
;
293 * If appending we don't care if it exists, otherwise look for existing
296 if (ext_op
!= X509V3_ADD_APPEND
)
297 extidx
= X509v3_get_ext_by_NID(*x
, nid
, -1);
299 /* See if extension exists */
301 /* If keep existing, nothing to do */
302 if (ext_op
== X509V3_ADD_KEEP_EXISTING
)
304 /* If default then its an error */
305 if (ext_op
== X509V3_ADD_DEFAULT
) {
306 errcode
= X509V3_R_EXTENSION_EXISTS
;
309 /* If delete, just delete it */
310 if (ext_op
== X509V3_ADD_DELETE
) {
311 if (!sk_X509_EXTENSION_delete(*x
, extidx
))
317 * If replace existing or delete, error since extension must exist
319 if ((ext_op
== X509V3_ADD_REPLACE_EXISTING
) ||
320 (ext_op
== X509V3_ADD_DELETE
)) {
321 errcode
= X509V3_R_EXTENSION_NOT_FOUND
;
327 * If we get this far then we have to create an extension: could have
328 * some flags for alternative encoding schemes...
331 ext
= X509V3_EXT_i2d(nid
, crit
, value
);
334 X509V3err(X509V3_F_X509V3_ADD1_I2D
,
335 X509V3_R_ERROR_CREATING_EXTENSION
);
339 /* If extension exists replace it.. */
341 extmp
= sk_X509_EXTENSION_value(*x
, extidx
);
342 X509_EXTENSION_free(extmp
);
343 if (!sk_X509_EXTENSION_set(*x
, extidx
, ext
))
349 && (*x
= sk_X509_EXTENSION_new_null()) == NULL
)
351 if (!sk_X509_EXTENSION_push(*x
, ext
))
357 if (!(flags
& X509V3_ADD_SILENT
))
358 X509V3err(X509V3_F_X509V3_ADD1_I2D
, errcode
);