1 /*
2 * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
3 * 1999.
4 */
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * [email protected].
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * ([email protected]). This product includes software written by Tim
55 * Hudson ([email protected]).
56 *
57 */
58 /* X509 v3 extension utilities */
59
60 #include <assert.h>
61 #include <stdio.h>
62
63 #include <openssl/conf.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66 #include <openssl/obj.h>
67 #include <openssl/x509.h>
68
69 #include "internal.h"
70
71 #include "ext_dat.h"
72
73 DEFINE_STACK_OF(X509V3_EXT_METHOD)
74
75 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
76
ext_stack_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)77 static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a,
78 const X509V3_EXT_METHOD *const *b) {
79 return ((*a)->ext_nid - (*b)->ext_nid);
80 }
81
X509V3_EXT_add(X509V3_EXT_METHOD * ext)82 int X509V3_EXT_add(X509V3_EXT_METHOD *ext) {
83 // We only support |ASN1_ITEM|-based extensions.
84 assert(ext->it != NULL);
85
86 // TODO(davidben): This should be locked. Also check for duplicates.
87 if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) {
88 return 0;
89 }
90 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
91 return 0;
92 }
93 sk_X509V3_EXT_METHOD_sort(ext_list);
94 return 1;
95 }
96
ext_cmp(const void * void_a,const void * void_b)97 static int ext_cmp(const void *void_a, const void *void_b) {
98 const X509V3_EXT_METHOD **a = (const X509V3_EXT_METHOD **)void_a;
99 const X509V3_EXT_METHOD **b = (const X509V3_EXT_METHOD **)void_b;
100 return ext_stack_cmp(a, b);
101 }
102
X509V3_EXT_get_nid(int nid)103 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) {
104 X509V3_EXT_METHOD tmp;
105 const X509V3_EXT_METHOD *t = &tmp, *const * ret;
106 size_t idx;
107
108 if (nid < 0) {
109 return NULL;
110 }
111 tmp.ext_nid = nid;
112 ret = bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT,
113 sizeof(X509V3_EXT_METHOD *), ext_cmp);
114 if (ret) {
115 return *ret;
116 }
117 if (!ext_list) {
118 return NULL;
119 }
120
121 if (!sk_X509V3_EXT_METHOD_find(ext_list, &idx, &tmp)) {
122 return NULL;
123 }
124 return sk_X509V3_EXT_METHOD_value(ext_list, idx);
125 }
126
X509V3_EXT_get(const X509_EXTENSION * ext)127 const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) {
128 int nid;
129 if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) {
130 return NULL;
131 }
132 return X509V3_EXT_get_nid(nid);
133 }
134
X509V3_EXT_free(int nid,void * ext_data)135 int X509V3_EXT_free(int nid, void *ext_data) {
136 const X509V3_EXT_METHOD *ext_method = X509V3_EXT_get_nid(nid);
137 if (ext_method == NULL) {
138 OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION);
139 return 0;
140 }
141
142 ASN1_item_free(ext_data, ASN1_ITEM_ptr(ext_method->it));
143 return 1;
144 }
145
X509V3_EXT_add_alias(int nid_to,int nid_from)146 int X509V3_EXT_add_alias(int nid_to, int nid_from) {
147 OPENSSL_BEGIN_ALLOW_DEPRECATED
148 const X509V3_EXT_METHOD *ext;
149 X509V3_EXT_METHOD *tmpext;
150
151 if (!(ext = X509V3_EXT_get_nid(nid_from))) {
152 OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NOT_FOUND);
153 return 0;
154 }
155 if (!(tmpext =
156 (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) {
157 return 0;
158 }
159 *tmpext = *ext;
160 tmpext->ext_nid = nid_to;
161 if (!X509V3_EXT_add(tmpext)) {
162 OPENSSL_free(tmpext);
163 return 0;
164 }
165 return 1;
166 OPENSSL_END_ALLOW_DEPRECATED
167 }
168
169 // Legacy function: we don't need to add standard extensions any more because
170 // they are now kept in ext_dat.h.
171
X509V3_add_standard_extensions(void)172 int X509V3_add_standard_extensions(void) { return 1; }
173
174 // Return an extension internal structure
175
X509V3_EXT_d2i(const X509_EXTENSION * ext)176 void *X509V3_EXT_d2i(const X509_EXTENSION *ext) {
177 const X509V3_EXT_METHOD *method;
178 const unsigned char *p;
179
180 if (!(method = X509V3_EXT_get(ext))) {
181 return NULL;
182 }
183 p = ext->value->data;
184 void *ret =
185 ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
186 if (ret == NULL) {
187 return NULL;
188 }
189 // Check for trailing data.
190 if (p != ext->value->data + ext->value->length) {
191 ASN1_item_free(ret, ASN1_ITEM_ptr(method->it));
192 OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION);
193 return NULL;
194 }
195 return ret;
196 }
197
X509V3_get_d2i(const STACK_OF (X509_EXTENSION)* extensions,int nid,int * out_critical,int * out_idx)198 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid,
199 int *out_critical, int *out_idx) {
200 int lastpos;
201 X509_EXTENSION *ex, *found_ex = NULL;
202 if (!extensions) {
203 if (out_idx) {
204 *out_idx = -1;
205 }
206 if (out_critical) {
207 *out_critical = -1;
208 }
209 return NULL;
210 }
211 if (out_idx) {
212 lastpos = *out_idx + 1;
213 } else {
214 lastpos = 0;
215 }
216 if (lastpos < 0) {
217 lastpos = 0;
218 }
219 for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) {
220 ex = sk_X509_EXTENSION_value(extensions, i);
221 if (OBJ_obj2nid(ex->object) == nid) {
222 if (out_idx) {
223 // TODO(https://crbug.com/boringssl/379): Consistently reject
224 // duplicate extensions.
225 *out_idx = (int)i;
226 found_ex = ex;
227 break;
228 } else if (found_ex) {
229 // Found more than one
230 if (out_critical) {
231 *out_critical = -2;
232 }
233 return NULL;
234 }
235 found_ex = ex;
236 }
237 }
238 if (found_ex) {
239 // Found it
240 if (out_critical) {
241 *out_critical = X509_EXTENSION_get_critical(found_ex);
242 }
243 return X509V3_EXT_d2i(found_ex);
244 }
245
246 // Extension not found
247 if (out_idx) {
248 *out_idx = -1;
249 }
250 if (out_critical) {
251 *out_critical = -1;
252 }
253 return NULL;
254 }
255
256 // This function is a general extension append, replace and delete utility.
257 // The precise operation is governed by the 'flags' value. The 'crit' and
258 // 'value' arguments (if relevant) are the extensions internal structure.
259
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)260 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
261 int crit, unsigned long flags) {
262 int errcode, extidx = -1;
263 X509_EXTENSION *ext = NULL, *extmp;
264 STACK_OF(X509_EXTENSION) *ret = NULL;
265 unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
266
267 // If appending we don't care if it exists, otherwise look for existing
268 // extension.
269 if (ext_op != X509V3_ADD_APPEND) {
270 extidx = X509v3_get_ext_by_NID(*x, nid, -1);
271 }
272
273 // See if extension exists
274 if (extidx >= 0) {
275 // If keep existing, nothing to do
276 if (ext_op == X509V3_ADD_KEEP_EXISTING) {
277 return 1;
278 }
279 // If default then its an error
280 if (ext_op == X509V3_ADD_DEFAULT) {
281 errcode = X509V3_R_EXTENSION_EXISTS;
282 goto err;
283 }
284 // If delete, just delete it
285 if (ext_op == X509V3_ADD_DELETE) {
286 X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx);
287 if (prev_ext == NULL) {
288 return -1;
289 }
290 X509_EXTENSION_free(prev_ext);
291 return 1;
292 }
293 } else {
294 // If replace existing or delete, error since extension must exist
295 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
296 (ext_op == X509V3_ADD_DELETE)) {
297 errcode = X509V3_R_EXTENSION_NOT_FOUND;
298 goto err;
299 }
300 }
301
302 // If we get this far then we have to create an extension: could have
303 // some flags for alternative encoding schemes...
304
305 ext = X509V3_EXT_i2d(nid, crit, value);
306
307 if (!ext) {
308 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
309 return 0;
310 }
311
312 // If extension exists replace it..
313 if (extidx >= 0) {
314 extmp = sk_X509_EXTENSION_value(*x, extidx);
315 X509_EXTENSION_free(extmp);
316 if (!sk_X509_EXTENSION_set(*x, extidx, ext)) {
317 return -1;
318 }
319 return 1;
320 }
321
322 if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) {
323 goto m_fail;
324 }
325 if (!sk_X509_EXTENSION_push(ret, ext)) {
326 goto m_fail;
327 }
328
329 *x = ret;
330 return 1;
331
332 m_fail:
333 if (ret != *x) {
334 sk_X509_EXTENSION_free(ret);
335 }
336 X509_EXTENSION_free(ext);
337 return -1;
338
339 err:
340 if (!(flags & X509V3_ADD_SILENT)) {
341 OPENSSL_PUT_ERROR(X509V3, errcode);
342 }
343 return 0;
344 }
345