/src/libressl/crypto/x509/x509_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: x509_lib.c,v 1.4 2022/07/24 21:41:29 tb Exp $ */ |
2 | | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | | * project 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 <stdio.h> |
61 | | |
62 | | #include <openssl/conf.h> |
63 | | #include <openssl/err.h> |
64 | | #include <openssl/x509v3.h> |
65 | | |
66 | | #include "ext_dat.h" |
67 | | #include "x509_lcl.h" |
68 | | |
69 | | static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL; |
70 | | |
71 | | static int ext_cmp(const X509V3_EXT_METHOD * const *a, |
72 | | const X509V3_EXT_METHOD * const *b); |
73 | | static void ext_list_free(X509V3_EXT_METHOD *ext); |
74 | | |
75 | | int |
76 | | X509V3_EXT_add(X509V3_EXT_METHOD *ext) |
77 | 0 | { |
78 | 0 | if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp))) { |
79 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
80 | 0 | return 0; |
81 | 0 | } |
82 | 0 | if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) { |
83 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
84 | 0 | return 0; |
85 | 0 | } |
86 | 0 | return 1; |
87 | 0 | } |
88 | | |
89 | | static int |
90 | | ext_cmp(const X509V3_EXT_METHOD * const *a, const X509V3_EXT_METHOD * const *b) |
91 | 199k | { |
92 | 199k | return ((*a)->ext_nid - (*b)->ext_nid); |
93 | 199k | } |
94 | | |
95 | | static int ext_cmp_BSEARCH_CMP_FN(const void *, const void *); |
96 | | static int ext_cmp(const X509V3_EXT_METHOD * const *, const X509V3_EXT_METHOD * const *); |
97 | | static const X509V3_EXT_METHOD * *OBJ_bsearch_ext(const X509V3_EXT_METHOD * *key, const X509V3_EXT_METHOD * const *base, int num); |
98 | | |
99 | | static int |
100 | | ext_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) |
101 | 199k | { |
102 | 199k | const X509V3_EXT_METHOD * const *a = a_; |
103 | 199k | const X509V3_EXT_METHOD * const *b = b_; |
104 | 199k | return ext_cmp(a, b); |
105 | 199k | } |
106 | | |
107 | | static const X509V3_EXT_METHOD ** |
108 | | OBJ_bsearch_ext(const X509V3_EXT_METHOD **key, |
109 | | const X509V3_EXT_METHOD *const *base, int num) |
110 | 48.3k | { |
111 | 48.3k | return (const X509V3_EXT_METHOD **)OBJ_bsearch_(key, base, num, |
112 | 48.3k | sizeof(const X509V3_EXT_METHOD *), ext_cmp_BSEARCH_CMP_FN); |
113 | 48.3k | } |
114 | | |
115 | | const X509V3_EXT_METHOD * |
116 | | X509V3_EXT_get_nid(int nid) |
117 | 48.3k | { |
118 | 48.3k | X509V3_EXT_METHOD tmp; |
119 | 48.3k | const X509V3_EXT_METHOD *t = &tmp, * const *ret; |
120 | 48.3k | int idx; |
121 | | |
122 | 48.3k | if (nid < 0) |
123 | 0 | return NULL; |
124 | 48.3k | tmp.ext_nid = nid; |
125 | 48.3k | ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT); |
126 | 48.3k | if (ret) |
127 | 48.3k | return *ret; |
128 | 0 | if (!ext_list) |
129 | 0 | return NULL; |
130 | 0 | idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp); |
131 | 0 | if (idx == -1) |
132 | 0 | return NULL; |
133 | 0 | return sk_X509V3_EXT_METHOD_value(ext_list, idx); |
134 | 0 | } |
135 | | |
136 | | const X509V3_EXT_METHOD * |
137 | | X509V3_EXT_get(X509_EXTENSION *ext) |
138 | 48.3k | { |
139 | 48.3k | int nid; |
140 | | |
141 | 48.3k | if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) |
142 | 0 | return NULL; |
143 | 48.3k | return X509V3_EXT_get_nid(nid); |
144 | 48.3k | } |
145 | | |
146 | | int |
147 | | X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist) |
148 | 0 | { |
149 | 0 | for (; extlist->ext_nid!=-1; extlist++) |
150 | 0 | if (!X509V3_EXT_add(extlist)) |
151 | 0 | return 0; |
152 | 0 | return 1; |
153 | 0 | } |
154 | | |
155 | | int |
156 | | X509V3_EXT_add_alias(int nid_to, int nid_from) |
157 | 0 | { |
158 | 0 | const X509V3_EXT_METHOD *ext; |
159 | 0 | X509V3_EXT_METHOD *tmpext; |
160 | |
|
161 | 0 | if (!(ext = X509V3_EXT_get_nid(nid_from))) { |
162 | 0 | X509V3error(X509V3_R_EXTENSION_NOT_FOUND); |
163 | 0 | return 0; |
164 | 0 | } |
165 | 0 | if (!(tmpext = malloc(sizeof(X509V3_EXT_METHOD)))) { |
166 | 0 | X509V3error(ERR_R_MALLOC_FAILURE); |
167 | 0 | return 0; |
168 | 0 | } |
169 | 0 | *tmpext = *ext; |
170 | 0 | tmpext->ext_nid = nid_to; |
171 | 0 | tmpext->ext_flags |= X509V3_EXT_DYNAMIC; |
172 | 0 | if (!X509V3_EXT_add(tmpext)) { |
173 | 0 | free(tmpext); |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | return 1; |
177 | 0 | } |
178 | | |
179 | | void |
180 | | X509V3_EXT_cleanup(void) |
181 | 0 | { |
182 | 0 | sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free); |
183 | 0 | ext_list = NULL; |
184 | 0 | } |
185 | | |
186 | | static void |
187 | | ext_list_free(X509V3_EXT_METHOD *ext) |
188 | 0 | { |
189 | 0 | if (ext->ext_flags & X509V3_EXT_DYNAMIC) |
190 | 0 | free(ext); |
191 | 0 | } |
192 | | |
193 | | /* Legacy function: we don't need to add standard extensions |
194 | | * any more because they are now kept in ext_dat.h. |
195 | | */ |
196 | | |
197 | | int |
198 | | X509V3_add_standard_extensions(void) |
199 | 0 | { |
200 | 0 | return 1; |
201 | 0 | } |
202 | | |
203 | | /* Return an extension internal structure */ |
204 | | |
205 | | void * |
206 | | X509V3_EXT_d2i(X509_EXTENSION *ext) |
207 | 48.3k | { |
208 | 48.3k | const X509V3_EXT_METHOD *method; |
209 | 48.3k | const unsigned char *p; |
210 | | |
211 | 48.3k | if (!(method = X509V3_EXT_get(ext))) |
212 | 0 | return NULL; |
213 | 48.3k | p = ext->value->data; |
214 | 48.3k | if (method->it) |
215 | 48.3k | return ASN1_item_d2i(NULL, &p, ext->value->length, |
216 | 48.3k | method->it); |
217 | 0 | return method->d2i(NULL, &p, ext->value->length); |
218 | 48.3k | } |
219 | | |
220 | | /* Get critical flag and decoded version of extension from a NID. |
221 | | * The "idx" variable returns the last found extension and can |
222 | | * be used to retrieve multiple extensions of the same NID. |
223 | | * However multiple extensions with the same NID is usually |
224 | | * due to a badly encoded certificate so if idx is NULL we |
225 | | * choke if multiple extensions exist. |
226 | | * The "crit" variable is set to the critical value. |
227 | | * The return value is the decoded extension or NULL on |
228 | | * error. The actual error can have several different causes, |
229 | | * the value of *crit reflects the cause: |
230 | | * >= 0, extension found but not decoded (reflects critical value). |
231 | | * -1 extension not found. |
232 | | * -2 extension occurs more than once. |
233 | | */ |
234 | | |
235 | | void * |
236 | | X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit, int *idx) |
237 | 106k | { |
238 | 106k | int lastpos, i; |
239 | 106k | X509_EXTENSION *ex, *found_ex = NULL; |
240 | | |
241 | 106k | if (!x) { |
242 | 0 | if (idx) |
243 | 0 | *idx = -1; |
244 | 0 | if (crit) |
245 | 0 | *crit = -1; |
246 | 0 | return NULL; |
247 | 0 | } |
248 | 106k | if (idx) |
249 | 0 | lastpos = *idx + 1; |
250 | 106k | else |
251 | 106k | lastpos = 0; |
252 | 106k | if (lastpos < 0) |
253 | 0 | lastpos = 0; |
254 | 245k | for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) { |
255 | 138k | ex = sk_X509_EXTENSION_value(x, i); |
256 | 138k | if (OBJ_obj2nid(ex->object) == nid) { |
257 | 48.4k | if (idx) { |
258 | 0 | *idx = i; |
259 | 0 | found_ex = ex; |
260 | 0 | break; |
261 | 48.4k | } else if (found_ex) { |
262 | | /* Found more than one */ |
263 | 24 | if (crit) |
264 | 0 | *crit = -2; |
265 | 24 | return NULL; |
266 | 24 | } |
267 | 48.3k | found_ex = ex; |
268 | 48.3k | } |
269 | 138k | } |
270 | 106k | if (found_ex) { |
271 | | /* Found it */ |
272 | 48.3k | if (crit) |
273 | 37.7k | *crit = X509_EXTENSION_get_critical(found_ex); |
274 | 48.3k | return X509V3_EXT_d2i(found_ex); |
275 | 48.3k | } |
276 | | |
277 | | /* Extension not found */ |
278 | 58.4k | if (idx) |
279 | 0 | *idx = -1; |
280 | 58.4k | if (crit) |
281 | 53.4k | *crit = -1; |
282 | 58.4k | return NULL; |
283 | 106k | } |
284 | | |
285 | | /* This function is a general extension append, replace and delete utility. |
286 | | * The precise operation is governed by the 'flags' value. The 'crit' and |
287 | | * 'value' arguments (if relevant) are the extensions internal structure. |
288 | | */ |
289 | | |
290 | | int |
291 | | X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value, |
292 | | int crit, unsigned long flags) |
293 | 0 | { |
294 | 0 | int extidx = -1; |
295 | 0 | int errcode; |
296 | 0 | X509_EXTENSION *ext, *extmp; |
297 | 0 | unsigned long ext_op = flags & X509V3_ADD_OP_MASK; |
298 | | |
299 | | /* If appending we don't care if it exists, otherwise |
300 | | * look for existing extension. |
301 | | */ |
302 | 0 | if (ext_op != X509V3_ADD_APPEND) |
303 | 0 | extidx = X509v3_get_ext_by_NID(*x, nid, -1); |
304 | | |
305 | | /* See if extension exists */ |
306 | 0 | if (extidx >= 0) { |
307 | | /* If keep existing, nothing to do */ |
308 | 0 | if (ext_op == X509V3_ADD_KEEP_EXISTING) |
309 | 0 | return 1; |
310 | | /* If default then its an error */ |
311 | 0 | if (ext_op == X509V3_ADD_DEFAULT) { |
312 | 0 | errcode = X509V3_R_EXTENSION_EXISTS; |
313 | 0 | goto err; |
314 | 0 | } |
315 | | /* If delete, just delete it */ |
316 | 0 | if (ext_op == X509V3_ADD_DELETE) { |
317 | 0 | if ((extmp = sk_X509_EXTENSION_delete(*x, extidx)) == NULL) |
318 | 0 | return -1; |
319 | 0 | X509_EXTENSION_free(extmp); |
320 | 0 | return 1; |
321 | 0 | } |
322 | 0 | } else { |
323 | | /* If replace existing or delete, error since |
324 | | * extension must exist |
325 | | */ |
326 | 0 | if ((ext_op == X509V3_ADD_REPLACE_EXISTING) || |
327 | 0 | (ext_op == X509V3_ADD_DELETE)) { |
328 | 0 | errcode = X509V3_R_EXTENSION_NOT_FOUND; |
329 | 0 | goto err; |
330 | 0 | } |
331 | 0 | } |
332 | | |
333 | | /* If we get this far then we have to create an extension: |
334 | | * could have some flags for alternative encoding schemes... |
335 | | */ |
336 | | |
337 | 0 | ext = X509V3_EXT_i2d(nid, crit, value); |
338 | |
|
339 | 0 | if (!ext) { |
340 | 0 | X509V3error(X509V3_R_ERROR_CREATING_EXTENSION); |
341 | 0 | return 0; |
342 | 0 | } |
343 | | |
344 | | /* If extension exists replace it.. */ |
345 | 0 | if (extidx >= 0) { |
346 | 0 | extmp = sk_X509_EXTENSION_value(*x, extidx); |
347 | 0 | X509_EXTENSION_free(extmp); |
348 | 0 | if (!sk_X509_EXTENSION_set(*x, extidx, ext)) |
349 | 0 | return -1; |
350 | 0 | return 1; |
351 | 0 | } |
352 | | |
353 | 0 | if (!*x && !(*x = sk_X509_EXTENSION_new_null())) |
354 | 0 | return -1; |
355 | 0 | if (!sk_X509_EXTENSION_push(*x, ext)) |
356 | 0 | return -1; |
357 | | |
358 | 0 | return 1; |
359 | | |
360 | 0 | err: |
361 | 0 | if (!(flags & X509V3_ADD_SILENT)) |
362 | 0 | X509V3error(errcode); |
363 | 0 | return 0; |
364 | 0 | } |