• 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   X509_EXTENSION *ex, *found_ex = NULL;
199   if (!extensions) {
200     if (out_idx) {
201       *out_idx = -1;
202     }
203     if (out_critical) {
204       *out_critical = -1;
205     }
206     return NULL;
207   }
208   if (out_idx) {
209     lastpos = *out_idx + 1;
210   } else {
211     lastpos = 0;
212   }
213   if (lastpos < 0) {
214     lastpos = 0;
215   }
216   for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) {
217     ex = sk_X509_EXTENSION_value(extensions, i);
218     if (OBJ_obj2nid(ex->object) == nid) {
219       if (out_idx) {
220         // TODO(https://crbug.com/boringssl/379): Consistently reject
221         // duplicate extensions.
222         *out_idx = (int)i;
223         found_ex = ex;
224         break;
225       } else if (found_ex) {
226         // Found more than one
227         if (out_critical) {
228           *out_critical = -2;
229         }
230         return NULL;
231       }
232       found_ex = ex;
233     }
234   }
235   if (found_ex) {
236     // Found it
237     if (out_critical) {
238       *out_critical = X509_EXTENSION_get_critical(found_ex);
239     }
240     return X509V3_EXT_d2i(found_ex);
241   }
242 
243   // Extension not found
244   if (out_idx) {
245     *out_idx = -1;
246   }
247   if (out_critical) {
248     *out_critical = -1;
249   }
250   return NULL;
251 }
252 
253 // This function is a general extension append, replace and delete utility.
254 // The precise operation is governed by the 'flags' value. The 'crit' and
255 // 'value' arguments (if relevant) are the extensions internal structure.
256 
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)257 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
258                     int crit, unsigned long flags) {
259   int errcode, extidx = -1;
260   X509_EXTENSION *ext = NULL, *extmp;
261   STACK_OF(X509_EXTENSION) *ret = NULL;
262   unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
263 
264   // If appending we don't care if it exists, otherwise look for existing
265   // extension.
266   if (ext_op != X509V3_ADD_APPEND) {
267     extidx = X509v3_get_ext_by_NID(*x, nid, -1);
268   }
269 
270   // See if extension exists
271   if (extidx >= 0) {
272     // If keep existing, nothing to do
273     if (ext_op == X509V3_ADD_KEEP_EXISTING) {
274       return 1;
275     }
276     // If default then its an error
277     if (ext_op == X509V3_ADD_DEFAULT) {
278       errcode = X509V3_R_EXTENSION_EXISTS;
279       goto err;
280     }
281     // If delete, just delete it
282     if (ext_op == X509V3_ADD_DELETE) {
283       X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx);
284       if (prev_ext == NULL) {
285         return -1;
286       }
287       X509_EXTENSION_free(prev_ext);
288       return 1;
289     }
290   } else {
291     // If replace existing or delete, error since extension must exist
292     if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
293         (ext_op == X509V3_ADD_DELETE)) {
294       errcode = X509V3_R_EXTENSION_NOT_FOUND;
295       goto err;
296     }
297   }
298 
299   // If we get this far then we have to create an extension: could have
300   // some flags for alternative encoding schemes...
301 
302   ext = X509V3_EXT_i2d(nid, crit, value);
303 
304   if (!ext) {
305     OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
306     return 0;
307   }
308 
309   // If extension exists replace it..
310   if (extidx >= 0) {
311     extmp = sk_X509_EXTENSION_value(*x, extidx);
312     X509_EXTENSION_free(extmp);
313     if (!sk_X509_EXTENSION_set(*x, extidx, ext)) {
314       return -1;
315     }
316     return 1;
317   }
318 
319   if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) {
320     goto m_fail;
321   }
322   if (!sk_X509_EXTENSION_push(ret, ext)) {
323     goto m_fail;
324   }
325 
326   *x = ret;
327   return 1;
328 
329 m_fail:
330   if (ret != *x) {
331     sk_X509_EXTENSION_free(ret);
332   }
333   X509_EXTENSION_free(ext);
334   return -1;
335 
336 err:
337   if (!(flags & X509V3_ADD_SILENT)) {
338     OPENSSL_PUT_ERROR(X509V3, errcode);
339   }
340   return 0;
341 }
342