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