blob: d25b83979bb022d56f0def4830eff549fe6a115c [file] [log] [blame]
Adam Langley57707c72016-01-14 11:25:12 -08001/*
2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3 * 1999.
Adam Langley95c29f32014-06-20 12:00:00 -07004 */
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
Adam Langley57707c72016-01-14 11:25:12 -080013 * notice, this list of conditions and the following disclaimer.
Adam Langley95c29f32014-06-20 12:00:00 -070014 *
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
David Benjamin8abd1b52023-04-12 10:07:53 -040060#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -070061#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
David Benjamin94a608a2021-07-14 13:46:27 -040069#include "../x509/internal.h"
70
Adam Langley95c29f32014-06-20 12:00:00 -070071#include "ext_dat.h"
72static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
73
David Benjaminaf0739f2023-01-09 10:03:02 -080074static int ext_stack_cmp(const X509V3_EXT_METHOD *const *a,
75 const X509V3_EXT_METHOD *const *b) {
David Benjamin260a10c2022-06-16 13:58:28 -040076 return ((*a)->ext_nid - (*b)->ext_nid);
Adam Langley95c29f32014-06-20 12:00:00 -070077}
78
David Benjamin260a10c2022-06-16 13:58:28 -040079int X509V3_EXT_add(X509V3_EXT_METHOD *ext) {
David Benjamin8abd1b52023-04-12 10:07:53 -040080 // We only support |ASN1_ITEM|-based extensions.
81 assert(ext->it != NULL);
82
David Benjamin97d48db2023-03-29 03:09:15 +090083 // TODO(davidben): This should be locked. Also check for duplicates.
David Benjamin260a10c2022-06-16 13:58:28 -040084 if (!ext_list && !(ext_list = sk_X509V3_EXT_METHOD_new(ext_stack_cmp))) {
David Benjamin260a10c2022-06-16 13:58:28 -040085 return 0;
86 }
87 if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
David Benjamin260a10c2022-06-16 13:58:28 -040088 return 0;
89 }
David Benjamin97d48db2023-03-29 03:09:15 +090090 sk_X509V3_EXT_METHOD_sort(ext_list);
David Benjamin260a10c2022-06-16 13:58:28 -040091 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -070092}
93
David Benjamin260a10c2022-06-16 13:58:28 -040094static 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);
Adam Langley95c29f32014-06-20 12:00:00 -070098}
99
David Benjamin260a10c2022-06-16 13:58:28 -0400100const 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;
Adam Langley95c29f32014-06-20 12:00:00 -0700104
David Benjaminc0b87a02022-06-16 14:02:15 -0400105 if (nid < 0) {
David Benjamin260a10c2022-06-16 13:58:28 -0400106 return NULL;
David Benjaminc0b87a02022-06-16 14:02:15 -0400107 }
David Benjamin260a10c2022-06-16 13:58:28 -0400108 tmp.ext_nid = nid;
109 ret = bsearch(&t, standard_exts, STANDARD_EXTENSION_COUNT,
Adam Langley57707c72016-01-14 11:25:12 -0800110 sizeof(X509V3_EXT_METHOD *), ext_cmp);
David Benjaminc0b87a02022-06-16 14:02:15 -0400111 if (ret) {
David Benjamin260a10c2022-06-16 13:58:28 -0400112 return *ret;
David Benjaminc0b87a02022-06-16 14:02:15 -0400113 }
114 if (!ext_list) {
David Benjamin260a10c2022-06-16 13:58:28 -0400115 return NULL;
David Benjaminc0b87a02022-06-16 14:02:15 -0400116 }
Adam Langley95c29f32014-06-20 12:00:00 -0700117
David Benjaminc0b87a02022-06-16 14:02:15 -0400118 if (!sk_X509V3_EXT_METHOD_find(ext_list, &idx, &tmp)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400119 return NULL;
David Benjaminc0b87a02022-06-16 14:02:15 -0400120 }
David Benjamin260a10c2022-06-16 13:58:28 -0400121 return sk_X509V3_EXT_METHOD_value(ext_list, idx);
Adam Langley95c29f32014-06-20 12:00:00 -0700122}
123
David Benjamin260a10c2022-06-16 13:58:28 -0400124const X509V3_EXT_METHOD *X509V3_EXT_get(const X509_EXTENSION *ext) {
125 int nid;
David Benjaminc0b87a02022-06-16 14:02:15 -0400126 if ((nid = OBJ_obj2nid(ext->object)) == NID_undef) {
David Benjamin260a10c2022-06-16 13:58:28 -0400127 return NULL;
David Benjaminc0b87a02022-06-16 14:02:15 -0400128 }
David Benjamin260a10c2022-06-16 13:58:28 -0400129 return X509V3_EXT_get_nid(nid);
Adam Langley95c29f32014-06-20 12:00:00 -0700130}
131
David Benjamin260a10c2022-06-16 13:58:28 -0400132int 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 }
Adam Langley6d43d0c2014-06-20 12:00:00 -0700138
David Benjamin8abd1b52023-04-12 10:07:53 -0400139 ASN1_item_free(ext_data, ASN1_ITEM_ptr(ext_method->it));
David Benjamin260a10c2022-06-16 13:58:28 -0400140 return 1;
Adam Langley6d43d0c2014-06-20 12:00:00 -0700141}
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjamin260a10c2022-06-16 13:58:28 -0400143int X509V3_EXT_add_alias(int nid_to, int nid_from) {
Bob Beckac6d5582023-03-28 14:23:03 -0600144OPENSSL_BEGIN_ALLOW_DEPRECATED
David Benjamin260a10c2022-06-16 13:58:28 -0400145 const X509V3_EXT_METHOD *ext;
146 X509V3_EXT_METHOD *tmpext;
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjamin260a10c2022-06-16 13:58:28 -0400148 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)))) {
David Benjamin260a10c2022-06-16 13:58:28 -0400154 return 0;
155 }
156 *tmpext = *ext;
157 tmpext->ext_nid = nid_to;
David Benjamin5fb362c2023-04-13 10:21:47 -0400158 if (!X509V3_EXT_add(tmpext)) {
159 OPENSSL_free(tmpext);
160 return 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400161 }
David Benjamin5fb362c2023-04-13 10:21:47 -0400162 return 1;
Bob Beckac6d5582023-03-28 14:23:03 -0600163OPENSSL_END_ALLOW_DEPRECATED
Adam Langley95c29f32014-06-20 12:00:00 -0700164}
165
David Benjamin46350482022-06-16 14:03:12 -0400166// Legacy function: we don't need to add standard extensions any more because
167// they are now kept in ext_dat.h.
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin260a10c2022-06-16 13:58:28 -0400169int X509V3_add_standard_extensions(void) { return 1; }
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjamin46350482022-06-16 14:03:12 -0400171// Return an extension internal structure
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjamin260a10c2022-06-16 13:58:28 -0400173void *X509V3_EXT_d2i(const X509_EXTENSION *ext) {
174 const X509V3_EXT_METHOD *method;
175 const unsigned char *p;
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjaminc0b87a02022-06-16 14:02:15 -0400177 if (!(method = X509V3_EXT_get(ext))) {
David Benjamin260a10c2022-06-16 13:58:28 -0400178 return NULL;
David Benjaminc0b87a02022-06-16 14:02:15 -0400179 }
David Benjamin260a10c2022-06-16 13:58:28 -0400180 p = ext->value->data;
David Benjamin8abd1b52023-04-12 10:07:53 -0400181 void *ret =
182 ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
David Benjamin260a10c2022-06-16 13:58:28 -0400183 if (ret == NULL) {
184 return NULL;
185 }
David Benjamin46350482022-06-16 14:03:12 -0400186 // Check for trailing data.
David Benjamin260a10c2022-06-16 13:58:28 -0400187 if (p != ext->value->data + ext->value->length) {
David Benjamin8abd1b52023-04-12 10:07:53 -0400188 ASN1_item_free(ret, ASN1_ITEM_ptr(method->it));
David Benjamin260a10c2022-06-16 13:58:28 -0400189 OPENSSL_PUT_ERROR(X509V3, X509V3_R_TRAILING_DATA_IN_EXTENSION);
190 return NULL;
191 }
192 return ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700193}
194
David Benjamin8591d532020-11-03 13:49:28 -0500195void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *extensions, int nid,
David Benjamin260a10c2022-06-16 13:58:28 -0400196 int *out_critical, int *out_idx) {
197 int lastpos;
David Benjamin260a10c2022-06-16 13:58:28 -0400198 X509_EXTENSION *ex, *found_ex = NULL;
199 if (!extensions) {
David Benjaminc0b87a02022-06-16 14:02:15 -0400200 if (out_idx) {
David Benjamin260a10c2022-06-16 13:58:28 -0400201 *out_idx = -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400202 }
203 if (out_critical) {
David Benjamin260a10c2022-06-16 13:58:28 -0400204 *out_critical = -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400205 }
Adam Langley57707c72016-01-14 11:25:12 -0800206 return NULL;
David Benjamin260a10c2022-06-16 13:58:28 -0400207 }
David Benjaminc0b87a02022-06-16 14:02:15 -0400208 if (out_idx) {
David Benjamin260a10c2022-06-16 13:58:28 -0400209 lastpos = *out_idx + 1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400210 } else {
David Benjamin260a10c2022-06-16 13:58:28 -0400211 lastpos = 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400212 }
213 if (lastpos < 0) {
David Benjamin260a10c2022-06-16 13:58:28 -0400214 lastpos = 0;
David Benjaminc0b87a02022-06-16 14:02:15 -0400215 }
David Benjamin89a62532023-05-23 11:45:10 -0400216 for (size_t i = lastpos; i < sk_X509_EXTENSION_num(extensions); i++) {
David Benjamin260a10c2022-06-16 13:58:28 -0400217 ex = sk_X509_EXTENSION_value(extensions, i);
218 if (OBJ_obj2nid(ex->object) == nid) {
219 if (out_idx) {
David Benjamin46350482022-06-16 14:03:12 -0400220 // TODO(https://crbug.com/boringssl/379): Consistently reject
221 // duplicate extensions.
David Benjamin89a62532023-05-23 11:45:10 -0400222 *out_idx = (int)i;
David Benjamin260a10c2022-06-16 13:58:28 -0400223 found_ex = ex;
224 break;
225 } else if (found_ex) {
David Benjamin46350482022-06-16 14:03:12 -0400226 // Found more than one
David Benjaminc0b87a02022-06-16 14:02:15 -0400227 if (out_critical) {
David Benjamin260a10c2022-06-16 13:58:28 -0400228 *out_critical = -2;
David Benjaminc0b87a02022-06-16 14:02:15 -0400229 }
David Benjamin260a10c2022-06-16 13:58:28 -0400230 return NULL;
231 }
232 found_ex = ex;
233 }
234 }
235 if (found_ex) {
David Benjamin46350482022-06-16 14:03:12 -0400236 // Found it
David Benjaminc0b87a02022-06-16 14:02:15 -0400237 if (out_critical) {
David Benjamin260a10c2022-06-16 13:58:28 -0400238 *out_critical = X509_EXTENSION_get_critical(found_ex);
David Benjaminc0b87a02022-06-16 14:02:15 -0400239 }
David Benjamin260a10c2022-06-16 13:58:28 -0400240 return X509V3_EXT_d2i(found_ex);
241 }
242
David Benjamin46350482022-06-16 14:03:12 -0400243 // Extension not found
David Benjaminc0b87a02022-06-16 14:02:15 -0400244 if (out_idx) {
David Benjamin260a10c2022-06-16 13:58:28 -0400245 *out_idx = -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400246 }
247 if (out_critical) {
David Benjamin260a10c2022-06-16 13:58:28 -0400248 *out_critical = -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400249 }
David Benjamin260a10c2022-06-16 13:58:28 -0400250 return NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700251}
252
David Benjamin46350482022-06-16 14:03:12 -0400253// 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.
Adam Langley95c29f32014-06-20 12:00:00 -0700256
257int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
David Benjamin260a10c2022-06-16 13:58:28 -0400258 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;
Adam Langley95c29f32014-06-20 12:00:00 -0700263
David Benjamin46350482022-06-16 14:03:12 -0400264 // If appending we don't care if it exists, otherwise look for existing
265 // extension.
David Benjaminc0b87a02022-06-16 14:02:15 -0400266 if (ext_op != X509V3_ADD_APPEND) {
David Benjamin260a10c2022-06-16 13:58:28 -0400267 extidx = X509v3_get_ext_by_NID(*x, nid, -1);
David Benjaminc0b87a02022-06-16 14:02:15 -0400268 }
David Benjamin260a10c2022-06-16 13:58:28 -0400269
David Benjamin46350482022-06-16 14:03:12 -0400270 // See if extension exists
David Benjamin260a10c2022-06-16 13:58:28 -0400271 if (extidx >= 0) {
David Benjamin46350482022-06-16 14:03:12 -0400272 // If keep existing, nothing to do
David Benjaminc0b87a02022-06-16 14:02:15 -0400273 if (ext_op == X509V3_ADD_KEEP_EXISTING) {
David Benjamin260a10c2022-06-16 13:58:28 -0400274 return 1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400275 }
David Benjamin46350482022-06-16 14:03:12 -0400276 // If default then its an error
David Benjamin260a10c2022-06-16 13:58:28 -0400277 if (ext_op == X509V3_ADD_DEFAULT) {
278 errcode = X509V3_R_EXTENSION_EXISTS;
279 goto err;
280 }
David Benjamin46350482022-06-16 14:03:12 -0400281 // If delete, just delete it
David Benjamin260a10c2022-06-16 13:58:28 -0400282 if (ext_op == X509V3_ADD_DELETE) {
David Benjamin7528f032022-07-08 15:43:46 -0400283 X509_EXTENSION *prev_ext = sk_X509_EXTENSION_delete(*x, extidx);
284 if (prev_ext == NULL) {
David Benjamin260a10c2022-06-16 13:58:28 -0400285 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400286 }
David Benjamin7528f032022-07-08 15:43:46 -0400287 X509_EXTENSION_free(prev_ext);
David Benjamin260a10c2022-06-16 13:58:28 -0400288 return 1;
289 }
290 } else {
David Benjamin46350482022-06-16 14:03:12 -0400291 // If replace existing or delete, error since extension must exist
David Benjamin260a10c2022-06-16 13:58:28 -0400292 if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
293 (ext_op == X509V3_ADD_DELETE)) {
294 errcode = X509V3_R_EXTENSION_NOT_FOUND;
295 goto err;
Adam Langley57707c72016-01-14 11:25:12 -0800296 }
David Benjamin260a10c2022-06-16 13:58:28 -0400297 }
Adam Langley95c29f32014-06-20 12:00:00 -0700298
David Benjamin46350482022-06-16 14:03:12 -0400299 // If we get this far then we have to create an extension: could have
300 // some flags for alternative encoding schemes...
Adam Langley95c29f32014-06-20 12:00:00 -0700301
David Benjamin260a10c2022-06-16 13:58:28 -0400302 ext = X509V3_EXT_i2d(nid, crit, value);
Adam Langley95c29f32014-06-20 12:00:00 -0700303
David Benjamin260a10c2022-06-16 13:58:28 -0400304 if (!ext) {
305 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
Adam Langley57707c72016-01-14 11:25:12 -0800306 return 0;
David Benjamin260a10c2022-06-16 13:58:28 -0400307 }
308
David Benjamin46350482022-06-16 14:03:12 -0400309 // If extension exists replace it..
David Benjamin260a10c2022-06-16 13:58:28 -0400310 if (extidx >= 0) {
311 extmp = sk_X509_EXTENSION_value(*x, extidx);
312 X509_EXTENSION_free(extmp);
David Benjaminc0b87a02022-06-16 14:02:15 -0400313 if (!sk_X509_EXTENSION_set(*x, extidx, ext)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400314 return -1;
David Benjaminc0b87a02022-06-16 14:02:15 -0400315 }
David Benjamin260a10c2022-06-16 13:58:28 -0400316 return 1;
317 }
318
David Benjaminc0b87a02022-06-16 14:02:15 -0400319 if ((ret = *x) == NULL && (ret = sk_X509_EXTENSION_new_null()) == NULL) {
David Benjamin260a10c2022-06-16 13:58:28 -0400320 goto m_fail;
David Benjaminc0b87a02022-06-16 14:02:15 -0400321 }
322 if (!sk_X509_EXTENSION_push(ret, ext)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400323 goto m_fail;
David Benjaminc0b87a02022-06-16 14:02:15 -0400324 }
David Benjamin260a10c2022-06-16 13:58:28 -0400325
326 *x = ret;
327 return 1;
328
329m_fail:
David Benjaminc0b87a02022-06-16 14:02:15 -0400330 if (ret != *x) {
David Benjamin260a10c2022-06-16 13:58:28 -0400331 sk_X509_EXTENSION_free(ret);
David Benjaminc0b87a02022-06-16 14:02:15 -0400332 }
David Benjamin260a10c2022-06-16 13:58:28 -0400333 X509_EXTENSION_free(ext);
334 return -1;
335
336err:
David Benjaminc0b87a02022-06-16 14:02:15 -0400337 if (!(flags & X509V3_ADD_SILENT)) {
David Benjamin260a10c2022-06-16 13:58:28 -0400338 OPENSSL_PUT_ERROR(X509V3, errcode);
David Benjaminc0b87a02022-06-16 14:02:15 -0400339 }
David Benjamin260a10c2022-06-16 13:58:28 -0400340 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700341}