1 /* $OpenBSD: x_x509.c,v 1.27 2021/09/02 12:41:44 job 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 static const ASN1_AUX X509_CINF_aux = {
69 .flags = ASN1_AFLG_ENCODING,
70 .enc_offset = offsetof(X509_CINF, enc),
71 };
72 static const ASN1_TEMPLATE X509_CINF_seq_tt[] = {
73 {
74 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
75 .offset = offsetof(X509_CINF, version),
76 .field_name = "version",
77 .item = &ASN1_INTEGER_it,
78 },
79 {
80 .offset = offsetof(X509_CINF, serialNumber),
81 .field_name = "serialNumber",
82 .item = &ASN1_INTEGER_it,
83 },
84 {
85 .offset = offsetof(X509_CINF, signature),
86 .field_name = "signature",
87 .item = &X509_ALGOR_it,
88 },
89 {
90 .offset = offsetof(X509_CINF, issuer),
91 .field_name = "issuer",
92 .item = &X509_NAME_it,
93 },
94 {
95 .offset = offsetof(X509_CINF, validity),
96 .field_name = "validity",
97 .item = &X509_VAL_it,
98 },
99 {
100 .offset = offsetof(X509_CINF, subject),
101 .field_name = "subject",
102 .item = &X509_NAME_it,
103 },
104 {
105 .offset = offsetof(X509_CINF, key),
106 .field_name = "key",
107 .item = &X509_PUBKEY_it,
108 },
109 {
110 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
111 .tag = 1,
112 .offset = offsetof(X509_CINF, issuerUID),
113 .field_name = "issuerUID",
114 .item = &ASN1_BIT_STRING_it,
115 },
116 {
117 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_OPTIONAL,
118 .tag = 2,
119 .offset = offsetof(X509_CINF, subjectUID),
120 .field_name = "subjectUID",
121 .item = &ASN1_BIT_STRING_it,
122 },
123 {
124 .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_SEQUENCE_OF |
125 ASN1_TFLG_OPTIONAL,
126 .tag = 3,
127 .offset = offsetof(X509_CINF, extensions),
128 .field_name = "extensions",
129 .item = &X509_EXTENSION_it,
130 },
131 };
132
133 const ASN1_ITEM X509_CINF_it = {
134 .itype = ASN1_ITYPE_SEQUENCE,
135 .utype = V_ASN1_SEQUENCE,
136 .templates = X509_CINF_seq_tt,
137 .tcount = sizeof(X509_CINF_seq_tt) / sizeof(ASN1_TEMPLATE),
138 .funcs = &X509_CINF_aux,
139 .size = sizeof(X509_CINF),
140 .sname = "X509_CINF",
141 };
142
143
144 X509_CINF *
d2i_X509_CINF(X509_CINF ** a,const unsigned char ** in,long len)145 d2i_X509_CINF(X509_CINF **a, const unsigned char **in, long len)
146 {
147 return (X509_CINF *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
148 &X509_CINF_it);
149 }
150
151 int
i2d_X509_CINF(X509_CINF * a,unsigned char ** out)152 i2d_X509_CINF(X509_CINF *a, unsigned char **out)
153 {
154 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_CINF_it);
155 }
156
157 X509_CINF *
X509_CINF_new(void)158 X509_CINF_new(void)
159 {
160 return (X509_CINF *)ASN1_item_new(&X509_CINF_it);
161 }
162
163 void
X509_CINF_free(X509_CINF * a)164 X509_CINF_free(X509_CINF *a)
165 {
166 ASN1_item_free((ASN1_VALUE *)a, &X509_CINF_it);
167 }
168 /* X509 top level structure needs a bit of customisation */
169
170 extern void policy_cache_free(X509_POLICY_CACHE *cache);
171
172 static int
x509_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)173 x509_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
174 {
175 X509 *ret = (X509 *)*pval;
176
177 switch (operation) {
178
179 case ASN1_OP_NEW_POST:
180 ret->valid = 0;
181 ret->name = NULL;
182 ret->ex_flags = 0;
183 ret->ex_pathlen = -1;
184 ret->skid = NULL;
185 ret->akid = NULL;
186 ret->aux = NULL;
187 ret->crldp = NULL;
188 #ifndef OPENSSL_NO_RFC3779
189 ret->rfc3779_addr = NULL;
190 ret->rfc3779_asid = NULL;
191 #endif
192 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
193 break;
194
195 case ASN1_OP_D2I_POST:
196 free(ret->name);
197 ret->name = X509_NAME_oneline(ret->cert_info->subject, NULL, 0);
198 break;
199
200 case ASN1_OP_FREE_POST:
201 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509, ret, &ret->ex_data);
202 X509_CERT_AUX_free(ret->aux);
203 ASN1_OCTET_STRING_free(ret->skid);
204 AUTHORITY_KEYID_free(ret->akid);
205 CRL_DIST_POINTS_free(ret->crldp);
206 policy_cache_free(ret->policy_cache);
207 GENERAL_NAMES_free(ret->altname);
208 NAME_CONSTRAINTS_free(ret->nc);
209 #ifndef OPENSSL_NO_RFC3779
210 sk_IPAddressFamily_pop_free(ret->rfc3779_addr, IPAddressFamily_free);
211 ASIdentifiers_free(ret->rfc3779_asid);
212 #endif
213 free(ret->name);
214 ret->name = NULL;
215 break;
216 }
217
218 return 1;
219 }
220
221 static const ASN1_AUX X509_aux = {
222 .app_data = NULL,
223 .flags = ASN1_AFLG_REFCOUNT,
224 .ref_offset = offsetof(X509, references),
225 .ref_lock = CRYPTO_LOCK_X509,
226 .asn1_cb = x509_cb,
227 };
228 static const ASN1_TEMPLATE X509_seq_tt[] = {
229 {
230 .offset = offsetof(X509, cert_info),
231 .field_name = "cert_info",
232 .item = &X509_CINF_it,
233 },
234 {
235 .offset = offsetof(X509, sig_alg),
236 .field_name = "sig_alg",
237 .item = &X509_ALGOR_it,
238 },
239 {
240 .offset = offsetof(X509, signature),
241 .field_name = "signature",
242 .item = &ASN1_BIT_STRING_it,
243 },
244 };
245
246 const ASN1_ITEM X509_it = {
247 .itype = ASN1_ITYPE_SEQUENCE,
248 .utype = V_ASN1_SEQUENCE,
249 .templates = X509_seq_tt,
250 .tcount = sizeof(X509_seq_tt) / sizeof(ASN1_TEMPLATE),
251 .funcs = &X509_aux,
252 .size = sizeof(X509),
253 .sname = "X509",
254 };
255
256
257 X509 *
d2i_X509(X509 ** a,const unsigned char ** in,long len)258 d2i_X509(X509 **a, const unsigned char **in, long len)
259 {
260 return (X509 *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
261 &X509_it);
262 }
263
264 int
i2d_X509(X509 * a,unsigned char ** out)265 i2d_X509(X509 *a, unsigned char **out)
266 {
267 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_it);
268 }
269
270 X509 *
X509_new(void)271 X509_new(void)
272 {
273 return (X509 *)ASN1_item_new(&X509_it);
274 }
275
276 void
X509_free(X509 * a)277 X509_free(X509 *a)
278 {
279 ASN1_item_free((ASN1_VALUE *)a, &X509_it);
280 }
281
282 X509 *
X509_dup(X509 * x)283 X509_dup(X509 *x)
284 {
285 return ASN1_item_dup(&X509_it, x);
286 }
287
288 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)289 X509_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
290 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
291 {
292 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509, argl, argp,
293 new_func, dup_func, free_func);
294 }
295
296 int
X509_set_ex_data(X509 * r,int idx,void * arg)297 X509_set_ex_data(X509 *r, int idx, void *arg)
298 {
299 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
300 }
301
302 void *
X509_get_ex_data(X509 * r,int idx)303 X509_get_ex_data(X509 *r, int idx)
304 {
305 return (CRYPTO_get_ex_data(&r->ex_data, idx));
306 }
307
308 /* X509_AUX ASN1 routines. X509_AUX is the name given to
309 * a certificate with extra info tagged on the end. Since these
310 * functions set how a certificate is trusted they should only
311 * be used when the certificate comes from a reliable source
312 * such as local storage.
313 *
314 */
315
316 X509 *
d2i_X509_AUX(X509 ** a,const unsigned char ** pp,long length)317 d2i_X509_AUX(X509 **a, const unsigned char **pp, long length)
318 {
319 const unsigned char *q;
320 X509 *ret;
321
322 /* Save start position */
323 q = *pp;
324 ret = d2i_X509(NULL, pp, length);
325 /* If certificate unreadable then forget it */
326 if (!ret)
327 return NULL;
328 /* update length */
329 length -= *pp - q;
330 if (length > 0) {
331 if (!d2i_X509_CERT_AUX(&ret->aux, pp, length))
332 goto err;
333 }
334 if (a != NULL) {
335 X509_free(*a);
336 *a = ret;
337 }
338 return ret;
339
340 err:
341 X509_free(ret);
342 return NULL;
343 }
344
345 int
i2d_X509_AUX(X509 * a,unsigned char ** pp)346 i2d_X509_AUX(X509 *a, unsigned char **pp)
347 {
348 int length;
349
350 length = i2d_X509(a, pp);
351 if (a)
352 length += i2d_X509_CERT_AUX(a->aux, pp);
353 return length;
354 }
355
356 void
X509_get0_signature(const ASN1_BIT_STRING ** psig,const X509_ALGOR ** palg,const X509 * x)357 X509_get0_signature(const ASN1_BIT_STRING **psig, const X509_ALGOR **palg,
358 const X509 *x)
359 {
360 if (psig != NULL)
361 *psig = x->signature;
362 if (palg != NULL)
363 *palg = x->sig_alg;
364 }
365
366 int
X509_get_signature_nid(const X509 * x)367 X509_get_signature_nid(const X509 *x)
368 {
369 return OBJ_obj2nid(x->sig_alg->algorithm);
370 }
371