xref: /freebsd/crypto/openssl/crypto/x509/v3_lib.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /* X509 v3 extension utilities */
11 
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
16 
17 #include "ext_dat.h"
18 
19 static STACK_OF(X509V3_EXT_METHOD) *ext_list = NULL;
20 
21 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
22                    const X509V3_EXT_METHOD *const *b);
23 static void ext_list_free(X509V3_EXT_METHOD *ext);
24 
X509V3_EXT_add(X509V3_EXT_METHOD * ext)25 int X509V3_EXT_add(X509V3_EXT_METHOD *ext)
26 {
27     if (ext_list == NULL
28         && (ext_list = sk_X509V3_EXT_METHOD_new(ext_cmp)) == NULL) {
29         ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
30         return 0;
31     }
32     if (!sk_X509V3_EXT_METHOD_push(ext_list, ext)) {
33         ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
34         return 0;
35     }
36     return 1;
37 }
38 
ext_cmp(const X509V3_EXT_METHOD * const * a,const X509V3_EXT_METHOD * const * b)39 static int ext_cmp(const X509V3_EXT_METHOD *const *a,
40                    const X509V3_EXT_METHOD *const *b)
41 {
42     return ((*a)->ext_nid - (*b)->ext_nid);
43 }
44 
45 DECLARE_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
46                            const X509V3_EXT_METHOD *, ext);
47 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const X509V3_EXT_METHOD *,
48                              const X509V3_EXT_METHOD *, ext);
49 
50 #include "standard_exts.h"
51 
X509V3_EXT_get_nid(int nid)52 const X509V3_EXT_METHOD *X509V3_EXT_get_nid(int nid)
53 {
54     X509V3_EXT_METHOD tmp;
55     const X509V3_EXT_METHOD *t = &tmp, *const *ret;
56     int idx;
57 
58     if (nid < 0)
59         return NULL;
60     tmp.ext_nid = nid;
61     ret = OBJ_bsearch_ext(&t, standard_exts, STANDARD_EXTENSION_COUNT);
62     if (ret)
63         return *ret;
64     if (!ext_list)
65         return NULL;
66     /* Ideally, this would be done under a lock */
67     sk_X509V3_EXT_METHOD_sort(ext_list);
68     idx = sk_X509V3_EXT_METHOD_find(ext_list, &tmp);
69     /* A failure to locate the item is handled by the value method */
70     return sk_X509V3_EXT_METHOD_value(ext_list, idx);
71 }
72 
X509V3_EXT_get(X509_EXTENSION * ext)73 const X509V3_EXT_METHOD *X509V3_EXT_get(X509_EXTENSION *ext)
74 {
75     int nid;
76     if ((nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext))) == NID_undef)
77         return NULL;
78     return X509V3_EXT_get_nid(nid);
79 }
80 
X509V3_EXT_add_list(X509V3_EXT_METHOD * extlist)81 int X509V3_EXT_add_list(X509V3_EXT_METHOD *extlist)
82 {
83     for (; extlist->ext_nid != -1; extlist++)
84         if (!X509V3_EXT_add(extlist))
85             return 0;
86     return 1;
87 }
88 
X509V3_EXT_add_alias(int nid_to,int nid_from)89 int X509V3_EXT_add_alias(int nid_to, int nid_from)
90 {
91     const X509V3_EXT_METHOD *ext;
92     X509V3_EXT_METHOD *tmpext;
93 
94     if ((ext = X509V3_EXT_get_nid(nid_from)) == NULL) {
95         ERR_raise(ERR_LIB_X509V3, X509V3_R_EXTENSION_NOT_FOUND);
96         return 0;
97     }
98     if ((tmpext = OPENSSL_malloc(sizeof(*tmpext))) == NULL)
99         return 0;
100     *tmpext = *ext;
101     tmpext->ext_nid = nid_to;
102     tmpext->ext_flags |= X509V3_EXT_DYNAMIC;
103     if (!X509V3_EXT_add(tmpext)) {
104         OPENSSL_free(tmpext);
105         return 0;
106     }
107     return 1;
108 }
109 
X509V3_EXT_cleanup(void)110 void X509V3_EXT_cleanup(void)
111 {
112     sk_X509V3_EXT_METHOD_pop_free(ext_list, ext_list_free);
113     ext_list = NULL;
114 }
115 
ext_list_free(X509V3_EXT_METHOD * ext)116 static void ext_list_free(X509V3_EXT_METHOD *ext)
117 {
118     if (ext->ext_flags & X509V3_EXT_DYNAMIC)
119         OPENSSL_free(ext);
120 }
121 
122 /*
123  * Legacy function: we don't need to add standard extensions any more because
124  * they are now kept in ext_dat.h.
125  */
126 
X509V3_add_standard_extensions(void)127 int X509V3_add_standard_extensions(void)
128 {
129     return 1;
130 }
131 
132 /* Return an extension internal structure */
133 
X509V3_EXT_d2i(X509_EXTENSION * ext)134 void *X509V3_EXT_d2i(X509_EXTENSION *ext)
135 {
136     const X509V3_EXT_METHOD *method;
137     const unsigned char *p;
138     ASN1_STRING *extvalue;
139     int extlen;
140 
141     if ((method = X509V3_EXT_get(ext)) == NULL)
142         return NULL;
143     extvalue = X509_EXTENSION_get_data(ext);
144     p = ASN1_STRING_get0_data(extvalue);
145     extlen = ASN1_STRING_length(extvalue);
146     if (method->it)
147         return ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
148     return method->d2i(NULL, &p, extlen);
149 }
150 
151 /*-
152  * Get critical flag and decoded version of extension from a NID.
153  * The "idx" variable returns the last found extension and can
154  * be used to retrieve multiple extensions of the same NID.
155  * However multiple extensions with the same NID is usually
156  * due to a badly encoded certificate so if idx is NULL we
157  * choke if multiple extensions exist.
158  * The "crit" variable is set to the critical value.
159  * The return value is the decoded extension or NULL on
160  * error. The actual error can have several different causes,
161  * the value of *crit reflects the cause:
162  * >= 0, extension found but not decoded (reflects critical value).
163  * -1 extension not found.
164  * -2 extension occurs more than once.
165  */
166 
X509V3_get_d2i(const STACK_OF (X509_EXTENSION)* x,int nid,int * crit,int * idx)167 void *X509V3_get_d2i(const STACK_OF(X509_EXTENSION) *x, int nid, int *crit,
168                      int *idx)
169 {
170     int lastpos, i;
171     X509_EXTENSION *ex, *found_ex = NULL;
172 
173     if (!x) {
174         if (idx)
175             *idx = -1;
176         if (crit)
177             *crit = -1;
178         return NULL;
179     }
180     if (idx)
181         lastpos = *idx + 1;
182     else
183         lastpos = 0;
184     if (lastpos < 0)
185         lastpos = 0;
186     for (i = lastpos; i < sk_X509_EXTENSION_num(x); i++) {
187         ex = sk_X509_EXTENSION_value(x, i);
188         if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == nid) {
189             if (idx) {
190                 *idx = i;
191                 found_ex = ex;
192                 break;
193             } else if (found_ex) {
194                 /* Found more than one */
195                 if (crit)
196                     *crit = -2;
197                 return NULL;
198             }
199             found_ex = ex;
200         }
201     }
202     if (found_ex) {
203         /* Found it */
204         if (crit)
205             *crit = X509_EXTENSION_get_critical(found_ex);
206         return X509V3_EXT_d2i(found_ex);
207     }
208 
209     /* Extension not found */
210     if (idx)
211         *idx = -1;
212     if (crit)
213         *crit = -1;
214     return NULL;
215 }
216 
217 /*
218  * This function is a general extension append, replace and delete utility.
219  * The precise operation is governed by the 'flags' value. The 'crit' and
220  * 'value' arguments (if relevant) are the extensions internal structure.
221  */
222 
X509V3_add1_i2d(STACK_OF (X509_EXTENSION)** x,int nid,void * value,int crit,unsigned long flags)223 int X509V3_add1_i2d(STACK_OF(X509_EXTENSION) **x, int nid, void *value,
224                     int crit, unsigned long flags)
225 {
226     int errcode, extidx = -1;
227     X509_EXTENSION *ext = NULL, *extmp;
228     STACK_OF(X509_EXTENSION) *ret = NULL;
229     unsigned long ext_op = flags & X509V3_ADD_OP_MASK;
230 
231     /*
232      * If appending we don't care if it exists, otherwise look for existing
233      * extension.
234      */
235     if (ext_op != X509V3_ADD_APPEND)
236         extidx = X509v3_get_ext_by_NID(*x, nid, -1);
237 
238     /* See if extension exists */
239     if (extidx >= 0) {
240         /* If keep existing, nothing to do */
241         if (ext_op == X509V3_ADD_KEEP_EXISTING)
242             return 1;
243         /* If default then its an error */
244         if (ext_op == X509V3_ADD_DEFAULT) {
245             errcode = X509V3_R_EXTENSION_EXISTS;
246             goto err;
247         }
248         /* If delete, just delete it */
249         if (ext_op == X509V3_ADD_DELETE) {
250             extmp = sk_X509_EXTENSION_delete(*x, extidx);
251             if (extmp == NULL)
252                 return -1;
253             X509_EXTENSION_free(extmp);
254             return 1;
255         }
256     } else {
257         /*
258          * If replace existing or delete, error since extension must exist
259          */
260         if ((ext_op == X509V3_ADD_REPLACE_EXISTING) ||
261             (ext_op == X509V3_ADD_DELETE)) {
262             errcode = X509V3_R_EXTENSION_NOT_FOUND;
263             goto err;
264         }
265     }
266 
267     /*
268      * If we get this far then we have to create an extension: could have
269      * some flags for alternative encoding schemes...
270      */
271 
272     ext = X509V3_EXT_i2d(nid, crit, value);
273 
274     if (!ext) {
275         ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CREATING_EXTENSION);
276         return 0;
277     }
278 
279     /* If extension exists replace it.. */
280     if (extidx >= 0) {
281         extmp = sk_X509_EXTENSION_value(*x, extidx);
282         X509_EXTENSION_free(extmp);
283         if (!sk_X509_EXTENSION_set(*x, extidx, ext))
284             return -1;
285         return 1;
286     }
287 
288     ret = *x;
289     if (*x == NULL
290         && (ret = sk_X509_EXTENSION_new_null()) == NULL)
291         goto m_fail;
292     if (!sk_X509_EXTENSION_push(ret, ext))
293         goto m_fail;
294 
295     *x = ret;
296     return 1;
297 
298  m_fail:
299     /* ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); */
300     if (ret != *x)
301         sk_X509_EXTENSION_free(ret);
302     X509_EXTENSION_free(ext);
303     return -1;
304 
305  err:
306     if (!(flags & X509V3_ADD_SILENT))
307         ERR_raise(ERR_LIB_X509V3, errcode);
308     return 0;
309 }
310