xref: /openbsd/lib/libcrypto/asn1/x_x509.c (revision f0d701e2)
1 /* $OpenBSD: x_x509.c,v 1.39 2024/07/08 14:48:49 beck Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 
61 #include <openssl/opensslconf.h>
62 
63 #include <openssl/asn1t.h>
64 #include <openssl/evp.h>
65 #include <openssl/x509.h>
66 #include <openssl/x509v3.h>
67 
68 #include "x509_local.h"
69 
70 static const ASN1_AUX X509_CINF_aux = {
71 	.flags = ASN1_AFLG_ENCODING,
72 	.enc_offset = offsetof(X509_CINF, enc),
73 };
74 static const ASN1_TEMPLATE X509_CINF_seq_tt[] = {
75 	{
76 		.flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
77 		.offset = offsetof(X509_CINF, version),
78 		.field_name = "version",
79 		.item = &ASN1_INTEGER_it,
80 	},
81 	{
82 		.offset = offsetof(X509_CINF, serialNumber),
83 		.field_name = "serialNumber",
84 		.item = &ASN1_INTEGER_it,
85 	},
86 	{
87 		.offset = offsetof(X509_CINF, signature),
88 		.field_name = "signature",
89 		.item = &X509_ALGOR_it,
90 	},
91 	{
92 		.offset = offsetof(X509_CINF, issuer),
93 		.field_name = "issuer",
94 		.item = &X509_NAME_it,
95 	},
96 	{
97 		.offset = offsetof(X509_CINF, validity),
98 		.field_name = "validity",
99 		.item = &X509_VAL_it,
100 	},
101 	{
102 		.offset = offsetof(X509_CINF, subject),
103 		.field_name = "subject",
104 		.item = &X509_NAME_it,
105 	},
106 	{
107 		.offset = offsetof(X509_CINF, key),
108 		.field_name = "key",
109 		.item = &X509_PUBKEY_it,
110 	},
111 	{
112 		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
113 		.tag = 1,
114 		.offset = offsetof(X509_CINF, issuerUID),
115 		.field_name = "issuerUID",
116 		.item = &ASN1_BIT_STRING_it,
117 	},
118 	{
119 		.flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
120 		.tag = 2,
121 		.offset = offsetof(X509_CINF, subjectUID),
122 		.field_name = "subjectUID",
123 		.item = &ASN1_BIT_STRING_it,
124 	},
125 	{
126 		.flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_SEQUENCE_OF |
127 		    ASN1_TFLG_OPTIONAL,
128 		.tag = 3,
129 		.offset = offsetof(X509_CINF, extensions),
130 		.field_name = "extensions",
131 		.item = &X509_EXTENSION_it,
132 	},
133 };
134 
135 const ASN1_ITEM X509_CINF_it = {
136 	.itype = ASN1_ITYPE_SEQUENCE,
137 	.utype = V_ASN1_SEQUENCE,
138 	.templates = X509_CINF_seq_tt,
139 	.tcount = sizeof(X509_CINF_seq_tt) / sizeof(ASN1_TEMPLATE),
140 	.funcs = &X509_CINF_aux,
141 	.size = sizeof(X509_CINF),
142 	.sname = "X509_CINF",
143 };
144 LCRYPTO_ALIAS(X509_CINF_it);
145 
146 
147 X509_CINF *
d2i_X509_CINF(X509_CINF ** a,const unsigned char ** in,long len)148 d2i_X509_CINF(X509_CINF **a, const unsigned char **in, long len)
149 {
150 	return (X509_CINF *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
151 	    &X509_CINF_it);
152 }
153 LCRYPTO_ALIAS(d2i_X509_CINF);
154 
155 int
i2d_X509_CINF(X509_CINF * a,unsigned char ** out)156 i2d_X509_CINF(X509_CINF *a, unsigned char **out)
157 {
158 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CINF_it);
159 }
160 LCRYPTO_ALIAS(i2d_X509_CINF);
161 
162 X509_CINF *
X509_CINF_new(void)163 X509_CINF_new(void)
164 {
165 	return (X509_CINF *)ASN1_item_new(&X509_CINF_it);
166 }
167 LCRYPTO_ALIAS(X509_CINF_new);
168 
169 void
X509_CINF_free(X509_CINF * a)170 X509_CINF_free(X509_CINF *a)
171 {
172 	ASN1_item_free((ASN1_VALUE *)a, &X509_CINF_it);
173 }
174 LCRYPTO_ALIAS(X509_CINF_free);
175 /* X509 top level structure needs a bit of customisation */
176 
177 static int
x509_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)178 x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
179 {
180 	X509 *ret = (X509 *)*pval;
181 
182 	switch (operation) {
183 
184 	case ASN1_OP_NEW_POST:
185 		ret->valid = 0;
186 		ret->name = NULL;
187 		ret->ex_flags = 0;
188 		ret->ex_pathlen = -1;
189 		ret->skid = NULL;
190 		ret->akid = NULL;
191 		ret->aux = NULL;
192 		ret->crldp = NULL;
193 #ifndef OPENSSL_NO_RFC3779
194 		ret->rfc3779_addr = NULL;
195 		ret->rfc3779_asid = NULL;
196 #endif
197 		CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
198 		break;
199 
200 	case ASN1_OP_D2I_POST:
201 		free(ret->name);
202 		ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
203 		break;
204 
205 	case ASN1_OP_FREE_POST:
206 		CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
207 		X509_CERT_AUX_free(ret->aux);
208 		ASN1_OCTET_STRING_free(ret->skid);
209 		AUTHORITY_KEYID_free(ret->akid);
210 		CRL_DIST_POINTS_free(ret->crldp);
211 		GENERAL_NAMES_free(ret->altname);
212 		NAME_CONSTRAINTS_free(ret->nc);
213 #ifndef OPENSSL_NO_RFC3779
214 		sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
215 		ASIdentifiers_free(ret->rfc3779_asid);
216 #endif
217 		free(ret->name);
218 		ret->name = NULL;
219 		break;
220 	}
221 
222 	return 1;
223 }
224 LCRYPTO_ALIAS(d2i_X509_CINF);
225 
226 static const ASN1_AUX X509_aux = {
227 	.app_data = NULL,
228 	.flags = ASN1_AFLG_REFCOUNT,
229 	.ref_offset = offsetof(X509, references),
230 	.ref_lock = CRYPTO_LOCK_X509,
231 	.asn1_cb = x509_cb,
232 };
233 static const ASN1_TEMPLATE X509_seq_tt[] = {
234 	{
235 		.offset = offsetof(X509, cert_info),
236 		.field_name = "cert_info",
237 		.item = &X509_CINF_it,
238 	},
239 	{
240 		.offset = offsetof(X509, sig_alg),
241 		.field_name = "sig_alg",
242 		.item = &X509_ALGOR_it,
243 	},
244 	{
245 		.offset = offsetof(X509, signature),
246 		.field_name = "signature",
247 		.item = &ASN1_BIT_STRING_it,
248 	},
249 };
250 
251 const ASN1_ITEM X509_it = {
252 	.itype = ASN1_ITYPE_SEQUENCE,
253 	.utype = V_ASN1_SEQUENCE,
254 	.templates = X509_seq_tt,
255 	.tcount = sizeof(X509_seq_tt) / sizeof(ASN1_TEMPLATE),
256 	.funcs = &X509_aux,
257 	.size = sizeof(X509),
258 	.sname = "X509",
259 };
260 LCRYPTO_ALIAS(X509_it);
261 
262 
263 X509 *
d2i_X509(X509 ** a,const unsigned char ** in,long len)264 d2i_X509(X509 **a, const unsigned char **in, long len)
265 {
266 	return (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
267 	    &X509_it);
268 }
269 LCRYPTO_ALIAS(d2i_X509);
270 
271 int
i2d_X509(X509 * a,unsigned char ** out)272 i2d_X509(X509 *a, unsigned char **out)
273 {
274 	return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_it);
275 }
276 LCRYPTO_ALIAS(i2d_X509);
277 
278 X509 *
X509_new(void)279 X509_new(void)
280 {
281 	return (X509 *)ASN1_item_new(&X509_it);
282 }
283 LCRYPTO_ALIAS(X509_new);
284 
285 void
X509_free(X509 * a)286 X509_free(X509 *a)
287 {
288 	ASN1_item_free((ASN1_VALUE *)a, &X509_it);
289 }
290 LCRYPTO_ALIAS(X509_free);
291 
292 X509 *
X509_dup(X509 * x)293 X509_dup(X509 *x)
294 {
295 	return ASN1_item_dup(&X509_it, x);
296 }
297 LCRYPTO_ALIAS(X509_dup);
298 
299 int
X509_get_ex_new_index(long argl,void * argp,CRYPTO_EX_new * new_func,CRYPTO_EX_dup * dup_func,CRYPTO_EX_free * free_func)300 X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
301     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
302 {
303 	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp,
304 	    new_func, dup_func, free_func);
305 }
306 LCRYPTO_ALIAS(X509_get_ex_new_index);
307 
308 int
X509_set_ex_data(X509 * r,int idx,void * arg)309 X509_set_ex_data(X509 *r, int idx, void *arg)
310 {
311 	return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
312 }
313 LCRYPTO_ALIAS(X509_set_ex_data);
314 
315 void *
X509_get_ex_data(X509 * r,int idx)316 X509_get_ex_data(X509 *r, int idx)
317 {
318 	return (CRYPTO_get_ex_data(&r->ex_data, idx));
319 }
320 LCRYPTO_ALIAS(X509_get_ex_data);
321 
322 /* X509_AUX ASN1 routines. X509_AUX is the name given to
323  * a certificate with extra info tagged on the end. Since these
324  * functions set how a certificate is trusted they should only
325  * be used when the certificate comes from a reliable source
326  * such as local storage.
327  *
328  */
329 
330 X509 *
d2i_X509_AUX(X509 ** a,const unsigned char ** pp,long length)331 d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
332 {
333 	const unsigned char *q;
334 	X509 *ret;
335 
336 	/* Save start position */
337 	q = *pp;
338 	ret = d2i_X509(NULL, pp, length);
339 	/* If certificate unreadable then forget it */
340 	if (!ret)
341 		return NULL;
342 	/* update length */
343 	length -= *pp - q;
344 	if (length > 0) {
345 		if (!d2i_X509_CERT_AUX(&ret->aux, pp, length))
346 			goto err;
347 	}
348 	if (a != NULL) {
349 		X509_free(*a);
350 		*a = ret;
351 	}
352 	return ret;
353 
354  err:
355 	X509_free(ret);
356 	return NULL;
357 }
358 LCRYPTO_ALIAS(d2i_X509_AUX);
359 
360 int
i2d_X509_AUX(X509 * a,unsigned char ** pp)361 i2d_X509_AUX(X509 *a, unsigned char **pp)
362 {
363 	int length;
364 
365 	length = i2d_X509(a, pp);
366 	if (a)
367 		length += i2d_X509_CERT_AUX(a->aux, pp);
368 	return length;
369 }
370 LCRYPTO_ALIAS(i2d_X509_AUX);
371 
372 int
i2d_re_X509_tbs(X509 * x,unsigned char ** pp)373 i2d_re_X509_tbs(X509 *x, unsigned char **pp)
374 {
375 	x->cert_info->enc.modified = 1;
376 	return i2d_X509_CINF(x->cert_info, pp);
377 }
378 LCRYPTO_ALIAS(i2d_re_X509_tbs);
379 
380 void
X509_get0_signature(const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg,const X509 * x)381 X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg,
382     const X509 *x)
383 {
384 	if (psig != NULL)
385 		*psig = x->signature;
386 	if (palg != NULL)
387 		*palg = x->sig_alg;
388 }
389 LCRYPTO_ALIAS(X509_get0_signature);
390 
391 int
X509_get_signature_nid(const X509 * x)392 X509_get_signature_nid(const X509 *x)
393 {
394 	return OBJ_obj2nid(x->sig_alg->algorithm);
395 }
396 LCRYPTO_ALIAS(X509_get_signature_nid);
397