]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/x509v3/v3_lib.c
Fix "defined but not used" warnings.
[thirdparty/openssl.git] / crypto / x509v3 / v3_lib.c
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 #include "internal/cryptlib.h"
63 #include <openssl/conf.h>
64 #include <openssl/x509v3.h>
65
66 #include "ext_dat.h"
67
68 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
69
70 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
71 const X509V3_EXT_METHOD *const *b);
72 static void ext_list_free(X509V3_EXT_METHOD *ext);
73
74 int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
75 {
76 if (ext_list == NULL
77 && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
78 X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE);
79 return 0;
80 }
81 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
82 X509V3err(X509V3_F_X509V3_EXT_ADD, ERR_R_MALLOC_FAILURE);
83 return 0;
84 }
85 return 1;
86 }
87
88 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
89 const X509V3_EXT_METHOD *const *b)
90 {
91 return ((*a)->ext_nid - (*b)->ext_nid);
92 }
93
94 DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
95 const X509V3_EXT_METHOD *, ext);
96 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
97 const X509V3_EXT_METHOD *, ext);
98
99 /*
100 * This table will be searched using OBJ_bsearch so it *must* kept in order
101 * of the ext_nid values.
102 */
103
104 static const X509V3_EXT_METHOD *standard_exts[] = {
105 &v3_nscert,
106 &v3_ns_ia5_list[0],
107 &v3_ns_ia5_list[1],
108 &v3_ns_ia5_list[2],
109 &v3_ns_ia5_list[3],
110 &v3_ns_ia5_list[4],
111 &v3_ns_ia5_list[5],
112 &v3_ns_ia5_list[6],
113 &v3_skey_id,
114 &v3_key_usage,
115 &v3_pkey_usage_period,
116 &v3_alt[0],
117 &v3_alt[1],
118 &v3_bcons,
119 &v3_crl_num,
120 &v3_cpols,
121 &v3_akey_id,
122 &v3_crld,
123 &v3_ext_ku,
124 &v3_delta_crl,
125 &v3_crl_reason,
126 #ifndef OPENSSL_NO_OCSP
127 &v3_crl_invdate,
128 #endif
129 &v3_sxnet,
130 &v3_info,
131 #ifndef OPENSSL_NO_RFC3779
132 &v3_addr,
133 &v3_asid,
134 #endif
135 #ifndef OPENSSL_NO_OCSP
136 &v3_ocsp_nonce,
137 &v3_ocsp_crlid,
138 &v3_ocsp_accresp,
139 &v3_ocsp_nocheck,
140 &v3_ocsp_acutoff,
141 &v3_ocsp_serviceloc,
142 #endif
143 &v3_sinfo,
144 &v3_policy_constraints,
145 #ifndef OPENSSL_NO_OCSP
146 &v3_crl_hold,
147 #endif
148 &v3_pci,
149 &v3_name_constraints,
150 &v3_policy_mappings,
151 &v3_inhibit_anyp,
152 &v3_idp,
153 &v3_alt[2],
154 &v3_freshest_crl,
155 #ifndef OPENSSL_NO_SCT
156 &v3_ct_scts[0],
157 &v3_ct_scts[1],
158 #endif
159 };
160
161 /* Number of standard extensions */
162
163 #define STANDARD_EXTENSION_COUNT OSSL_NELEM(standard_exts)
164
165 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
166 {
167 X509V3_EXT_METHOD tmp;
168 const X509V3_EXT_METHOD *t = &tmp, *const *ret;
169 int idx;
170 if (nid < 0)
171 return NULL;
172 tmp.ext_nid = nid;
173 ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
174 if (ret)
175 return *ret;
176 if (!ext_list)
177 return NULL;
178 idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
179 if (idx == -1)
180 return NULL;
181 return sk_X509V3_EXT_METHOD_value(ext_list, idx);
182 }
183
184 const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
185 {
186 int nid;
187 if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
188 return NULL;
189 return X509V3_EXT_get_nid(nid);
190 }
191
192 int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
193 {
194 for (; extlist->ext_nid != -1; extlist++)
195 if (!X509V3_EXT_add(extlist))
196 return 0;
197 return 1;
198 }
199
200 int X509V3_EXT_add_alias(int nid_to, int nid_from)
201 {
202 const X509V3_EXT_METHOD *ext;
203 X509V3_EXT_METHOD *tmpext;
204
205 if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
206 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, X509V3_R_EXTENSION_NOT_FOUND);
207 return 0;
208 }
209 if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL) {
210 X509V3err(X509V3_F_X509V3_EXT_ADD_ALIAS, ERR_R_MALLOC_FAILURE);
211 return 0;
212 }
213 *tmpext = *ext;
214 tmpext->ext_nid = nid_to;
215 tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
216 return X509V3_EXT_add(tmpext);
217 }
218
219 void X509V3_EXT_cleanup(void)
220 {
221 sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
222 ext_list = NULL;
223 }
224
225 static void ext_list_free(X509V3_EXT_METHOD *ext)
226 {
227 if (ext->ext_flags & X509V3_EXT_DYNAMIC)
228 OPENSSL_free(ext);
229 }
230
231 /*
232 * Legacy function: we don't need to add standard extensions any more because
233 * they are now kept in ext_dat.h.
234 */
235
236 int X509V3_add_standard_extensions(void)
237 {
238 return 1;
239 }
240
241 /* Return an extension internal structure */
242
243 void *X509V3_EXT_d2i(X509_EXTENSION *ext)
244 {
245 const X509V3_EXT_METHOD *method;
246 const unsigned char *p;
247 ASN1_STRING *extvalue;
248 int extlen;
249
250 if ((method = X509V3_EXT_get(ext)) == NULL)
251 return NULL;
252 extvalue = X509_EXTENSION_get_data(ext);
253 p = ASN1_STRING_data(extvalue);
254 extlen = ASN1_STRING_length(extvalue);
255 if (method->it)
256 return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
257 return method->d2i(NULL, &p, extlen);
258 }
259
260 /*-
261 * Get critical flag and decoded version of extension from a NID.
262 * The "idx" variable returns the last found extension and can
263 * be used to retrieve multiple extensions of the same NID.
264 * However multiple extensions with the same NID is usually
265 * due to a badly encoded certificate so if idx is NULL we
266 * choke if multiple extensions exist.
267 * The "crit" variable is set to the critical value.
268 * The return value is the decoded extension or NULL on
269 * error. The actual error can have several different causes,
270 * the value of *crit reflects the cause:
271 * >= 0, extension found but not decoded (reflects critical value).
272 * -1 extension not found.
273 * -2 extension occurs more than once.
274 */
275
276 void *X509V3_get_d2i(STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
277 int *idx)
278 {
279 int lastpos, i;
280 X509_EXTENSION *ex, *found_ex = NULL;
281 if (!x) {
282 if (idx)
283 *idx = -1;
284 if (crit)
285 *crit = -1;
286 return NULL;
287 }
288 if (idx)
289 lastpos = *idx + 1;
290 else
291 lastpos = 0;
292 if (lastpos < 0)
293 lastpos = 0;
294 for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
295 ex = sk_X509_EXTENSION_value(x, i);
296 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
297 if (idx) {
298 *idx = i;
299 found_ex = ex;
300 break;
301 } else if (found_ex) {
302 /* Found more than one */
303 if (crit)
304 *crit = -2;
305 return NULL;
306 }
307 found_ex = ex;
308 }
309 }
310 if (found_ex) {
311 /* Found it */
312 if (crit)
313 *crit = X509_EXTENSION_get_critical(found_ex);
314 return X509V3_EXT_d2i(found_ex);
315 }
316
317 /* Extension not found */
318 if (idx)
319 *idx = -1;
320 if (crit)
321 *crit = -1;
322 return NULL;
323 }
324
325 /*
326 * This function is a general extension append, replace and delete utility.
327 * The precise operation is governed by the 'flags' value. The 'crit' and
328 * 'value' arguments (if relevant) are the extensions internal structure.
329 */
330
331 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
332 int crit, unsigned long flags)
333 {
334 int extidx = -1;
335 int errcode;
336 X509_EXTENSION *ext, *extmp;
337 unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
338
339 /*
340 * If appending we don't care if it exists, otherwise look for existing
341 * extension.
342 */
343 if (ext_op != X509V3_ADD_APPEND)
344 extidx = X509v3_get_ext_by_NID(*x, nid, -1);
345
346 /* See if extension exists */
347 if (extidx >= 0) {
348 /* If keep existing, nothing to do */
349 if (ext_op == X509V3_ADD_KEEP_EXISTING)
350 return 1;
351 /* If default then its an error */
352 if (ext_op == X509V3_ADD_DEFAULT) {
353 errcode = X509V3_R_EXTENSION_EXISTS;
354 goto err;
355 }
356 /* If delete, just delete it */
357 if (ext_op == X509V3_ADD_DELETE) {
358 if (!sk_X509_EXTENSION_delete(*x, extidx))
359 return -1;
360 return 1;
361 }
362 } else {
363 /*
364 * If replace existing or delete, error since extension must exist
365 */
366 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
367 (ext_op == X509V3_ADD_DELETE)) {
368 errcode = X509V3_R_EXTENSION_NOT_FOUND;
369 goto err;
370 }
371 }
372
373 /*
374 * If we get this far then we have to create an extension: could have
375 * some flags for alternative encoding schemes...
376 */
377
378 ext = X509V3_EXT_i2d(nid, crit, value);
379
380 if (!ext) {
381 X509V3err(X509V3_F_X509V3_ADD1_I2D,
382 X509V3_R_ERROR_CREATING_EXTENSION);
383 return 0;
384 }
385
386 /* If extension exists replace it.. */
387 if (extidx >= 0) {
388 extmp = sk_X509_EXTENSION_value(*x, extidx);
389 X509_EXTENSION_free(extmp);
390 if (!sk_X509_EXTENSION_set(*x, extidx, ext))
391 return -1;
392 return 1;
393 }
394
395 if (*x == NULL
396 && (*x = sk_X509_EXTENSION_new_null()) == NULL)
397 return -1;
398 if (!sk_X509_EXTENSION_push(*x, ext))
399 return -1;
400
401 return 1;
402
403 err:
404 if (!(flags & X509V3_ADD_SILENT))
405 X509V3err(X509V3_F_X509V3_ADD1_I2D, errcode);
406 return 0;
407 }