xref: /dragonfly/crypto/libressl/crypto/asn1/x_x509.c (revision 961e30ea7dc61d1112b778ea4981eac68129fb86)
1 /* $OpenBSD: x_x509.c,v 1.30 2021/12/25 13:17:48 jsing 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_lcl.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 
145 
146 X509_CINF *
d2i_X509_CINF(X509_CINF ** a,const unsigned char ** in,long len)147 d2i_X509_CINF(X509_CINF **a, const unsigned char **in, long len)
148 {
149           return (X509_CINF *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
150               &X509_CINF_it);
151 }
152 
153 int
i2d_X509_CINF(X509_CINF * a,unsigned char ** out)154 i2d_X509_CINF(X509_CINF *a, unsigned char **out)
155 {
156           return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CINF_it);
157 }
158 
159 X509_CINF *
X509_CINF_new(void)160 X509_CINF_new(void)
161 {
162           return (X509_CINF *)ASN1_item_new(&X509_CINF_it);
163 }
164 
165 void
X509_CINF_free(X509_CINF * a)166 X509_CINF_free(X509_CINF *a)
167 {
168           ASN1_item_free((ASN1_VALUE *)a, &X509_CINF_it);
169 }
170 /* X509 top level structure needs a bit of customisation */
171 
172 extern void policy_cache_free(X509_POLICY_CACHE *cache);
173 
174 static int
x509_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)175 x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
176 {
177           X509 *ret = (X509 *)*pval;
178 
179           switch (operation) {
180 
181           case ASN1_OP_NEW_POST:
182                     ret->valid = 0;
183                     ret->name = NULL;
184                     ret->ex_flags = 0;
185                     ret->ex_pathlen = -1;
186                     ret->skid = NULL;
187                     ret->akid = NULL;
188                     ret->aux = NULL;
189                     ret->crldp = NULL;
190 #ifndef OPENSSL_NO_RFC3779
191                     ret->rfc3779_addr = NULL;
192                     ret->rfc3779_asid = NULL;
193 #endif
194                     CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
195                     break;
196 
197           case ASN1_OP_D2I_POST:
198                     free(ret->name);
199                     ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
200                     break;
201 
202           case ASN1_OP_FREE_POST:
203                     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
204                     X509_CERT_AUX_free(ret->aux);
205                     ASN1_OCTET_STRING_free(ret->skid);
206                     AUTHORITY_KEYID_free(ret->akid);
207                     CRL_DIST_POINTS_free(ret->crldp);
208                     policy_cache_free(ret->policy_cache);
209                     GENERAL_NAMES_free(ret->altname);
210                     NAME_CONSTRAINTS_free(ret->nc);
211 #ifndef OPENSSL_NO_RFC3779
212                     sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
213                     ASIdentifiers_free(ret->rfc3779_asid);
214 #endif
215                     free(ret->name);
216                     ret->name = NULL;
217                     break;
218           }
219 
220           return 1;
221 }
222 
223 static const ASN1_AUX X509_aux = {
224           .app_data = NULL,
225           .flags = ASN1_AFLG_REFCOUNT,
226           .ref_offset = offsetof(X509, references),
227           .ref_lock = CRYPTO_LOCK_X509,
228           .asn1_cb = x509_cb,
229 };
230 static const ASN1_TEMPLATE X509_seq_tt[] = {
231           {
232                     .offset = offsetof(X509, cert_info),
233                     .field_name = "cert_info",
234                     .item = &X509_CINF_it,
235           },
236           {
237                     .offset = offsetof(X509, sig_alg),
238                     .field_name = "sig_alg",
239                     .item = &X509_ALGOR_it,
240           },
241           {
242                     .offset = offsetof(X509, signature),
243                     .field_name = "signature",
244                     .item = &ASN1_BIT_STRING_it,
245           },
246 };
247 
248 const ASN1_ITEM X509_it = {
249           .itype = ASN1_ITYPE_SEQUENCE,
250           .utype = V_ASN1_SEQUENCE,
251           .templates = X509_seq_tt,
252           .tcount = sizeof(X509_seq_tt) / sizeof(ASN1_TEMPLATE),
253           .funcs = &X509_aux,
254           .size = sizeof(X509),
255           .sname = "X509",
256 };
257 
258 
259 X509 *
d2i_X509(X509 ** a,const unsigned char ** in,long len)260 d2i_X509(X509 **a, const unsigned char **in, long len)
261 {
262           return (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
263               &X509_it);
264 }
265 
266 int
i2d_X509(X509 * a,unsigned char ** out)267 i2d_X509(X509 *a, unsigned char **out)
268 {
269           return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_it);
270 }
271 
272 X509 *
X509_new(void)273 X509_new(void)
274 {
275           return (X509 *)ASN1_item_new(&X509_it);
276 }
277 
278 void
X509_free(X509 * a)279 X509_free(X509 *a)
280 {
281           ASN1_item_free((ASN1_VALUE *)a, &X509_it);
282 }
283 
284 X509 *
X509_dup(X509 * x)285 X509_dup(X509 *x)
286 {
287           return ASN1_item_dup(&X509_it, x);
288 }
289 
290 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)291 X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
292     CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
293 {
294           return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp,
295               new_func, dup_func, free_func);
296 }
297 
298 int
X509_set_ex_data(X509 * r,int idx,void * arg)299 X509_set_ex_data(X509 *r, int idx, void *arg)
300 {
301           return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
302 }
303 
304 void *
X509_get_ex_data(X509 * r,int idx)305 X509_get_ex_data(X509 *r, int idx)
306 {
307           return (CRYPTO_get_ex_data(&r->ex_data, idx));
308 }
309 
310 /* X509_AUX ASN1 routines. X509_AUX is the name given to
311  * a certificate with extra info tagged on the end. Since these
312  * functions set how a certificate is trusted they should only
313  * be used when the certificate comes from a reliable source
314  * such as local storage.
315  *
316  */
317 
318 X509 *
d2i_X509_AUX(X509 ** a,const unsigned char ** pp,long length)319 d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
320 {
321           const unsigned char *q;
322           X509 *ret;
323 
324           /* Save start position */
325           q = *pp;
326           ret = d2i_X509(NULL, pp, length);
327           /* If certificate unreadable then forget it */
328           if (!ret)
329                     return NULL;
330           /* update length */
331           length -= *pp - q;
332           if (length > 0) {
333                     if (!d2i_X509_CERT_AUX(&ret->aux, pp, length))
334                               goto err;
335           }
336           if (a != NULL) {
337                     X509_free(*a);
338                     *a = ret;
339           }
340           return ret;
341 
342  err:
343           X509_free(ret);
344           return NULL;
345 }
346 
347 int
i2d_X509_AUX(X509 * a,unsigned char ** pp)348 i2d_X509_AUX(X509 *a, unsigned char **pp)
349 {
350           int length;
351 
352           length = i2d_X509(a, pp);
353           if (a)
354                     length += i2d_X509_CERT_AUX(a->aux, pp);
355           return length;
356 }
357 
358 int
i2d_re_X509_tbs(X509 * x,unsigned char ** pp)359 i2d_re_X509_tbs(X509 *x, unsigned char **pp)
360 {
361           x->cert_info->enc.modified = 1;
362           return i2d_X509_CINF(x->cert_info, pp);
363 }
364 
365 void
X509_get0_signature(const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg,const X509 * x)366 X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg,
367     const X509 *x)
368 {
369           if (psig != NULL)
370                     *psig = x->signature;
371           if (palg != NULL)
372                     *palg = x->sig_alg;
373 }
374 
375 int
X509_get_signature_nid(const X509 * x)376 X509_get_signature_nid(const X509 *x)
377 {
378           return OBJ_obj2nid(x->sig_alg->algorithm);
379 }
380