1 /*
2 * Copyright 1999-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /* X509 v3 extension utilities */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
16
17 #include "ext_dat.h"
18
19 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
20
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);
24
X509V3_EXT_add(X509V3_EXT_METHOD * ext)25 int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
26 {
27 if (ext_list == NULL
28 && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
29 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
30 return 0;
31 }
32 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
33 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
34 return 0;
35 }
36 return 1;
37 }
38
ext_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)39 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
40 const X509V3_EXT_METHOD *const *b)
41 {
42 return ((*a)->ext_nid - (*b)->ext_nid);
43 }
44
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);
49
50 #include "standard_exts.h"
51
X509V3_EXT_get_nid(int nid)52 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
53 {
54 X509V3_EXT_METHOD tmp;
55 const X509V3_EXT_METHOD *t = &tmp, *const *ret;
56 int idx;
57
58 if (nid < 0)
59 return NULL;
60 tmp.ext_nid = nid;
61 ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
62 if (ret)
63 return *ret;
64 if (!ext_list)
65 return NULL;
66 idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
67 return sk_X509V3_EXT_METHOD_value(ext_list, idx);
68 }
69
X509V3_EXT_get(X509_EXTENSION * ext)70 const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
71 {
72 int nid;
73 if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
74 return NULL;
75 return X509V3_EXT_get_nid(nid);
76 }
77
X509V3_EXT_add_list(X509V3_EXT_METHOD * extlist)78 int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
79 {
80 for (; extlist->ext_nid != -1; extlist++)
81 if (!X509V3_EXT_add(extlist))
82 return 0;
83 return 1;
84 }
85
X509V3_EXT_add_alias(int nid_to,int nid_from)86 int X509V3_EXT_add_alias(int nid_to, int nid_from)
87 {
88 const X509V3_EXT_METHOD *ext;
89 X509V3_EXT_METHOD *tmpext;
90
91 if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
92 ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NOT_FOUND);
93 return 0;
94 }
95 if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL) {
96 ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
97 return 0;
98 }
99 *tmpext = *ext;
100 tmpext->ext_nid = nid_to;
101 tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
102 if (!X509V3_EXT_add(tmpext)) {
103 OPENSSL_free(tmpext);
104 return 0;
105 }
106 return 1;
107 }
108
X509V3_EXT_cleanup(void)109 void X509V3_EXT_cleanup(void)
110 {
111 sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
112 ext_list = NULL;
113 }
114
ext_list_free(X509V3_EXT_METHOD * ext)115 static void ext_list_free(X509V3_EXT_METHOD *ext)
116 {
117 if (ext->ext_flags & X509V3_EXT_DYNAMIC)
118 OPENSSL_free(ext);
119 }
120
121 /*
122 * Legacy function: we don't need to add standard extensions any more because
123 * they are now kept in ext_dat.h.
124 */
125
X509V3_add_standard_extensions(void)126 int X509V3_add_standard_extensions(void)
127 {
128 return 1;
129 }
130
131 /* Return an extension internal structure */
132
X509V3_EXT_d2i(X509_EXTENSION * ext)133 void *X509V3_EXT_d2i(X509_EXTENSION *ext)
134 {
135 const X509V3_EXT_METHOD *method;
136 const unsigned char *p;
137 ASN1_STRING *extvalue;
138 int extlen;
139
140 if ((method = X509V3_EXT_get(ext)) == NULL)
141 return NULL;
142 extvalue = X509_EXTENSION_get_data(ext);
143 p = ASN1_STRING_get0_data(extvalue);
144 extlen = ASN1_STRING_length(extvalue);
145 if (method->it)
146 return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
147 return method->d2i(NULL, &p, extlen);
148 }
149
150 /*-
151 * Get critical flag and decoded version of extension from a NID.
152 * The "idx" variable returns the last found extension and can
153 * be used to retrieve multiple extensions of the same NID.
154 * However multiple extensions with the same NID is usually
155 * due to a badly encoded certificate so if idx is NULL we
156 * choke if multiple extensions exist.
157 * The "crit" variable is set to the critical value.
158 * The return value is the decoded extension or NULL on
159 * error. The actual error can have several different causes,
160 * the value of *crit reflects the cause:
161 * >= 0, extension found but not decoded (reflects critical value).
162 * -1 extension not found.
163 * -2 extension occurs more than once.
164 */
165
X509V3_get_d2i(const STACK_OF (X509_EXTENSION)* x,int nid,int * crit,int * idx)166 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
167 int *idx)
168 {
169 int lastpos, i;
170 X509_EXTENSION *ex, *found_ex = NULL;
171
172 if (!x) {
173 if (idx)
174 *idx = -1;
175 if (crit)
176 *crit = -1;
177 return NULL;
178 }
179 if (idx)
180 lastpos = *idx + 1;
181 else
182 lastpos = 0;
183 if (lastpos < 0)
184 lastpos = 0;
185 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
186 ex = sk_X509_EXTENSION_value(x, i);
187 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
188 if (idx) {
189 *idx = i;
190 found_ex = ex;
191 break;
192 } else if (found_ex) {
193 /* Found more than one */
194 if (crit)
195 *crit = -2;
196 return NULL;
197 }
198 found_ex = ex;
199 }
200 }
201 if (found_ex) {
202 /* Found it */
203 if (crit)
204 *crit = X509_EXTENSION_get_critical(found_ex);
205 return X509V3_EXT_d2i(found_ex);
206 }
207
208 /* Extension not found */
209 if (idx)
210 *idx = -1;
211 if (crit)
212 *crit = -1;
213 return NULL;
214 }
215
216 /*
217 * This function is a general extension append, replace and delete utility.
218 * The precise operation is governed by the 'flags' value. The 'crit' and
219 * 'value' arguments (if relevant) are the extensions internal structure.
220 */
221
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)222 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
223 int crit, unsigned long flags)
224 {
225 int errcode, extidx = -1;
226 X509_EXTENSION *ext = NULL, *extmp;
227 STACK_OF(X509_EXTENSION) *ret = NULL;
228 unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
229
230 /*
231 * If appending we don't care if it exists, otherwise look for existing
232 * extension.
233 */
234 if (ext_op != X509V3_ADD_APPEND)
235 extidx = X509v3_get_ext_by_NID(*x, nid, -1);
236
237 /* See if extension exists */
238 if (extidx >= 0) {
239 /* If keep existing, nothing to do */
240 if (ext_op == X509V3_ADD_KEEP_EXISTING)
241 return 1;
242 /* If default then its an error */
243 if (ext_op == X509V3_ADD_DEFAULT) {
244 errcode = X509V3_R_EXTENSION_EXISTS;
245 goto err;
246 }
247 /* If delete, just delete it */
248 if (ext_op == X509V3_ADD_DELETE) {
249 extmp = sk_X509_EXTENSION_delete(*x, extidx);
250 if (extmp == NULL)
251 return -1;
252 X509_EXTENSION_free(extmp);
253 return 1;
254 }
255 } else {
256 /*
257 * If replace existing or delete, error since extension must exist
258 */
259 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
260 (ext_op == X509V3_ADD_DELETE)) {
261 errcode = X509V3_R_EXTENSION_NOT_FOUND;
262 goto err;
263 }
264 }
265
266 /*
267 * If we get this far then we have to create an extension: could have
268 * some flags for alternative encoding schemes...
269 */
270
271 ext = X509V3_EXT_i2d(nid, crit, value);
272
273 if (!ext) {
274 ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
275 return 0;
276 }
277
278 /* If extension exists replace it.. */
279 if (extidx >= 0) {
280 extmp = sk_X509_EXTENSION_value(*x, extidx);
281 X509_EXTENSION_free(extmp);
282 if (!sk_X509_EXTENSION_set(*x, extidx, ext))
283 return -1;
284 return 1;
285 }
286
287 ret = *x;
288 if (*x == NULL
289 && (ret = sk_X509_EXTENSION_new_null()) == NULL)
290 goto m_fail;
291 if (!sk_X509_EXTENSION_push(ret, ext))
292 goto m_fail;
293
294 *x = ret;
295 return 1;
296
297 m_fail:
298 /* ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); */
299 if (ret != *x)
300 sk_X509_EXTENSION_free(ret);
301 X509_EXTENSION_free(ext);
302 return -1;
303
304 err:
305 if (!(flags & X509V3_ADD_SILENT))
306 ERR_raise(ERR_LIB_X509V3, errcode);
307 return 0;
308 }
309