• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) 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  *    licensing@OpenSSL.org.
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  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
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/x509v3.h>
68 
69 #include "../x509/internal.h"
70 
71 #include "ext_dat.h"
72 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
73 
ext_stack_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)74 static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a,
75                          const X509V3_EXT_METHOD *const *b) {
76   return ((*a)->ext_nid - (*b)->ext_nid);
77 }
78 
X509V3_EXT_add(X509V3_EXT_METHOD * ext)79 int X509V3_EXT_add(X509V3_EXT_METHOD *ext) {
80   // We only support |ASN1_ITEM|-based extensions.
81   assert(ext->it != NULL);
82 
83   // TODO(davidben): This should be locked. Also check for duplicates.
84   if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) {
85     return 0;
86   }
87   if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
88     return 0;
89   }
90   sk_X509V3_EXT_METHOD_sort(ext_list);
91   return 1;
92 }
93 
ext_cmp(const void * void_a,const void * void_b)94 static int ext_cmp(const void *void_a, const void *void_b) {
95   const X509V3_EXT_METHOD **a = (const X509V3_EXT_METHOD **)void_a;
96   const X509V3_EXT_METHOD **b = (const X509V3_EXT_METHOD **)void_b;
97   return ext_stack_cmp(a, b);
98 }
99 
X509V3_EXT_get_nid(int nid)100 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid) {
101   X509V3_EXT_METHOD tmp;
102   const X509V3_EXT_METHOD *t = &tmp, *const * ret;
103   size_t idx;
104 
105   if (nid < 0) {
106     return NULL;
107   }
108   tmp.ext_nid = nid;
109   ret = bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT,
110                 sizeof(X509V3_EXT_METHOD *), ext_cmp);
111   if (ret) {
112     return *ret;
113   }
114   if (!ext_list) {
115     return NULL;
116   }
117 
118   if (!sk_X509V3_EXT_METHOD_find(ext_list, &idx, &tmp)) {
119     return NULL;
120   }
121   return sk_X509V3_EXT_METHOD_value(ext_list, idx);
122 }
123 
X509V3_EXT_get(const X509_EXTENSION * ext)124 const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) {
125   int nid;
126   if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) {
127     return NULL;
128   }
129   return X509V3_EXT_get_nid(nid);
130 }
131 
X509V3_EXT_free(int nid,void * ext_data)132 int X509V3_EXT_free(int nid, void *ext_data) {
133   const X509V3_EXT_METHOD *ext_method = X509V3_EXT_get_nid(nid);
134   if (ext_method == NULL) {
135     OPENSSL_PUT_ERROR(X509V3, X509V3_R_CANNOT_FIND_FREE_FUNCTION);
136     return 0;
137   }
138 
139   ASN1_item_free(ext_data, ASN1_ITEM_ptr(ext_method->it));
140   return 1;
141 }
142 
X509V3_EXT_add_alias(int nid_to,int nid_from)143 int X509V3_EXT_add_alias(int nid_to, int nid_from) {
144 OPENSSL_BEGIN_ALLOW_DEPRECATED
145   const X509V3_EXT_METHOD *ext;
146   X509V3_EXT_METHOD *tmpext;
147 
148   if (!(ext = X509V3_EXT_get_nid(nid_from))) {
149     OPENSSL_PUT_ERROR(X509V3, X509V3_R_EXTENSION_NOT_FOUND);
150     return 0;
151   }
152   if (!(tmpext =
153             (X509V3_EXT_METHOD *)OPENSSL_malloc(sizeof(X509V3_EXT_METHOD)))) {
154     return 0;
155   }
156   *tmpext = *ext;
157   tmpext->ext_nid = nid_to;
158   if (!X509V3_EXT_add(tmpext)) {
159     OPENSSL_free(tmpext);
160     return 0;
161   }
162   return 1;
163 OPENSSL_END_ALLOW_DEPRECATED
164 }
165 
166 // Legacy function: we don't need to add standard extensions any more because
167 // they are now kept in ext_dat.h.
168 
X509V3_add_standard_extensions(void)169 int X509V3_add_standard_extensions(void) { return 1; }
170 
171 // Return an extension internal structure
172 
X509V3_EXT_d2i(const X509_EXTENSION * ext)173 void *X509V3_EXT_d2i(const X509_EXTENSION *ext) {
174   const X509V3_EXT_METHOD *method;
175   const unsigned char *p;
176 
177   if (!(method = X509V3_EXT_get(ext))) {
178     return NULL;
179   }
180   p = ext->value->data;
181   void *ret =
182       ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
183   if (ret == NULL) {
184     return NULL;
185   }
186   // Check for trailing data.
187   if (p != ext->value->data + ext->value->length) {
188     ASN1_item_free(ret, ASN1_ITEM_ptr(method->it));
189     OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION);
190     return NULL;
191   }
192   return ret;
193 }
194 
X509V3_get_d2i(const STACK_OF (X509_EXTENSION)* extensions,int nid,int * out_critical,int * out_idx)195 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid,
196                      int *out_critical, int *out_idx) {
197   int lastpos;
198   size_t i;
199   X509_EXTENSION *ex, *found_ex = NULL;
200   if (!extensions) {
201     if (out_idx) {
202       *out_idx = -1;
203     }
204     if (out_critical) {
205       *out_critical = -1;
206     }
207     return NULL;
208   }
209   if (out_idx) {
210     lastpos = *out_idx + 1;
211   } else {
212     lastpos = 0;
213   }
214   if (lastpos < 0) {
215     lastpos = 0;
216   }
217   for (i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) {
218     ex = sk_X509_EXTENSION_value(extensions, i);
219     if (OBJ_obj2nid(ex->object) == nid) {
220       if (out_idx) {
221         // TODO(https://crbug.com/boringssl/379): Consistently reject
222         // duplicate extensions.
223         *out_idx = i;
224         found_ex = ex;
225         break;
226       } else if (found_ex) {
227         // Found more than one
228         if (out_critical) {
229           *out_critical = -2;
230         }
231         return NULL;
232       }
233       found_ex = ex;
234     }
235   }
236   if (found_ex) {
237     // Found it
238     if (out_critical) {
239       *out_critical = X509_EXTENSION_get_critical(found_ex);
240     }
241     return X509V3_EXT_d2i(found_ex);
242   }
243 
244   // Extension not found
245   if (out_idx) {
246     *out_idx = -1;
247   }
248   if (out_critical) {
249     *out_critical = -1;
250   }
251   return NULL;
252 }
253 
254 // This function is a general extension append, replace and delete utility.
255 // The precise operation is governed by the 'flags' value. The 'crit' and
256 // 'value' arguments (if relevant) are the extensions internal structure.
257 
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)258 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
259                     int crit, unsigned long flags) {
260   int errcode, extidx = -1;
261   X509_EXTENSION *ext = NULL, *extmp;
262   STACK_OF(X509_EXTENSION) *ret = NULL;
263   unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
264 
265   // If appending we don't care if it exists, otherwise look for existing
266   // extension.
267   if (ext_op != X509V3_ADD_APPEND) {
268     extidx = X509v3_get_ext_by_NID(*x, nid, -1);
269   }
270 
271   // See if extension exists
272   if (extidx >= 0) {
273     // If keep existing, nothing to do
274     if (ext_op == X509V3_ADD_KEEP_EXISTING) {
275       return 1;
276     }
277     // If default then its an error
278     if (ext_op == X509V3_ADD_DEFAULT) {
279       errcode = X509V3_R_EXTENSION_EXISTS;
280       goto err;
281     }
282     // If delete, just delete it
283     if (ext_op == X509V3_ADD_DELETE) {
284       X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx);
285       if (prev_ext == NULL) {
286         return -1;
287       }
288       X509_EXTENSION_free(prev_ext);
289       return 1;
290     }
291   } else {
292     // If replace existing or delete, error since extension must exist
293     if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
294         (ext_op == X509V3_ADD_DELETE)) {
295       errcode = X509V3_R_EXTENSION_NOT_FOUND;
296       goto err;
297     }
298   }
299 
300   // If we get this far then we have to create an extension: could have
301   // some flags for alternative encoding schemes...
302 
303   ext = X509V3_EXT_i2d(nid, crit, value);
304 
305   if (!ext) {
306     OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
307     return 0;
308   }
309 
310   // If extension exists replace it..
311   if (extidx >= 0) {
312     extmp = sk_X509_EXTENSION_value(*x, extidx);
313     X509_EXTENSION_free(extmp);
314     if (!sk_X509_EXTENSION_set(*x, extidx, ext)) {
315       return -1;
316     }
317     return 1;
318   }
319 
320   if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) {
321     goto m_fail;
322   }
323   if (!sk_X509_EXTENSION_push(ret, ext)) {
324     goto m_fail;
325   }
326 
327   *x = ret;
328   return 1;
329 
330 m_fail:
331   if (ret != *x) {
332     sk_X509_EXTENSION_free(ret);
333   }
334   X509_EXTENSION_free(ext);
335   return -1;
336 
337 err:
338   if (!(flags & X509V3_ADD_SILENT)) {
339     OPENSSL_PUT_ERROR(X509V3, errcode);
340   }
341   return 0;
342 }
343