xref: /dragonfly/crypto/libressl/crypto/x509/x_all.c (revision 6f5ec8b5)
1 /* $OpenBSD: x_all.c,v 1.26 2022/06/26 04:14:43 tb 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/asn1.h>
64 #include <openssl/buffer.h>
65 #include <openssl/evp.h>
66 #include <openssl/stack.h>
67 #include <openssl/x509.h>
68 
69 #ifndef OPENSSL_NO_DSA
70 #include <openssl/dsa.h>
71 #endif
72 #ifndef OPENSSL_NO_RSA
73 #include <openssl/rsa.h>
74 #endif
75 
76 #include "x509_lcl.h"
77 
78 X509 *
79 d2i_X509_bio(BIO *bp, X509 **x509)
80 {
81 	return ASN1_item_d2i_bio(&X509_it, bp, x509);
82 }
83 
84 int
85 i2d_X509_bio(BIO *bp, X509 *x509)
86 {
87 	return ASN1_item_i2d_bio(&X509_it, bp, x509);
88 }
89 
90 X509 *
91 d2i_X509_fp(FILE *fp, X509 **x509)
92 {
93 	return ASN1_item_d2i_fp(&X509_it, fp, x509);
94 }
95 
96 int
97 i2d_X509_fp(FILE *fp, X509 *x509)
98 {
99 	return ASN1_item_i2d_fp(&X509_it, fp, x509);
100 }
101 
102 X509_CRL *
103 d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
104 {
105 	return ASN1_item_d2i_bio(&X509_CRL_it, bp, crl);
106 }
107 
108 int
109 i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl)
110 {
111 	return ASN1_item_i2d_bio(&X509_CRL_it, bp, crl);
112 }
113 
114 X509_CRL *
115 d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
116 {
117 	return ASN1_item_d2i_fp(&X509_CRL_it, fp, crl);
118 }
119 
120 int
121 i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl)
122 {
123 	return ASN1_item_i2d_fp(&X509_CRL_it, fp, crl);
124 }
125 
126 PKCS7 *
127 d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
128 {
129 	return ASN1_item_d2i_bio(&PKCS7_it, bp, p7);
130 }
131 
132 int
133 i2d_PKCS7_bio(BIO *bp, PKCS7 *p7)
134 {
135 	return ASN1_item_i2d_bio(&PKCS7_it, bp, p7);
136 }
137 
138 PKCS7 *
139 d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
140 {
141 	return ASN1_item_d2i_fp(&PKCS7_it, fp, p7);
142 }
143 
144 int
145 i2d_PKCS7_fp(FILE *fp, PKCS7 *p7)
146 {
147 	return ASN1_item_i2d_fp(&PKCS7_it, fp, p7);
148 }
149 
150 X509_REQ *
151 d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
152 {
153 	return ASN1_item_d2i_bio(&X509_REQ_it, bp, req);
154 }
155 
156 int
157 i2d_X509_REQ_bio(BIO *bp, X509_REQ *req)
158 {
159 	return ASN1_item_i2d_bio(&X509_REQ_it, bp, req);
160 }
161 
162 X509_REQ *
163 d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
164 {
165 	return ASN1_item_d2i_fp(&X509_REQ_it, fp, req);
166 }
167 
168 int
169 i2d_X509_REQ_fp(FILE *fp, X509_REQ *req)
170 {
171 	return ASN1_item_i2d_fp(&X509_REQ_it, fp, req);
172 }
173 
174 #ifndef OPENSSL_NO_RSA
175 RSA *
176 d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
177 {
178 	return ASN1_item_d2i_bio(&RSAPrivateKey_it, bp, rsa);
179 }
180 
181 int
182 i2d_RSAPrivateKey_bio(BIO *bp, RSA *rsa)
183 {
184 	return ASN1_item_i2d_bio(&RSAPrivateKey_it, bp, rsa);
185 }
186 
187 RSA *
188 d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
189 {
190 	return ASN1_item_d2i_fp(&RSAPrivateKey_it, fp, rsa);
191 }
192 
193 int
194 i2d_RSAPrivateKey_fp(FILE *fp, RSA *rsa)
195 {
196 	return ASN1_item_i2d_fp(&RSAPrivateKey_it, fp, rsa);
197 }
198 
199 RSA *
200 d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
201 {
202 	return ASN1_item_d2i_bio(&RSAPublicKey_it, bp, rsa);
203 }
204 
205 int
206 i2d_RSAPublicKey_bio(BIO *bp, RSA *rsa)
207 {
208 	return ASN1_item_i2d_bio(&RSAPublicKey_it, bp, rsa);
209 }
210 
211 RSA *
212 d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
213 {
214 	return ASN1_item_d2i_fp(&RSAPublicKey_it, fp, rsa);
215 }
216 
217 int
218 i2d_RSAPublicKey_fp(FILE *fp, RSA *rsa)
219 {
220 	return ASN1_item_i2d_fp(&RSAPublicKey_it, fp, rsa);
221 }
222 #endif
223 
224 #ifndef OPENSSL_NO_DSA
225 DSA *
226 d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
227 {
228 	return ASN1_item_d2i_bio(&DSAPrivateKey_it, bp, dsa);
229 }
230 
231 int
232 i2d_DSAPrivateKey_bio(BIO *bp, DSA *dsa)
233 {
234 	return ASN1_item_i2d_bio(&DSAPrivateKey_it, bp, dsa);
235 }
236 
237 DSA *
238 d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
239 {
240 	return ASN1_item_d2i_fp(&DSAPrivateKey_it, fp, dsa);
241 }
242 
243 int
244 i2d_DSAPrivateKey_fp(FILE *fp, DSA *dsa)
245 {
246 	return ASN1_item_i2d_fp(&DSAPrivateKey_it, fp, dsa);
247 }
248 #endif
249 
250 #ifndef OPENSSL_NO_EC
251 EC_KEY *
252 d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey)
253 {
254 	return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey);
255 }
256 
257 int
258 i2d_ECPrivateKey_bio(BIO *bp, EC_KEY *eckey)
259 {
260 	return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey);
261 }
262 
263 EC_KEY *
264 d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey)
265 {
266 	return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey);
267 }
268 
269 int
270 i2d_ECPrivateKey_fp(FILE *fp, EC_KEY *eckey)
271 {
272 	return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey);
273 }
274 #endif
275 
276 X509_SIG *
277 d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
278 {
279 	return ASN1_item_d2i_bio(&X509_SIG_it, bp, p8);
280 }
281 
282 int
283 i2d_PKCS8_bio(BIO *bp, X509_SIG *p8)
284 {
285 	return ASN1_item_i2d_bio(&X509_SIG_it, bp, p8);
286 }
287 
288 X509_SIG *
289 d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
290 {
291 	return ASN1_item_d2i_fp(&X509_SIG_it, fp, p8);
292 }
293 
294 int
295 i2d_PKCS8_fp(FILE *fp, X509_SIG *p8)
296 {
297 	return ASN1_item_i2d_fp(&X509_SIG_it, fp, p8);
298 }
299 
300 PKCS8_PRIV_KEY_INFO *
301 d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO **p8inf)
302 {
303 	return ASN1_item_d2i_bio(&PKCS8_PRIV_KEY_INFO_it, bp,
304 	    p8inf);
305 }
306 
307 int
308 i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, PKCS8_PRIV_KEY_INFO *p8inf)
309 {
310 	return ASN1_item_i2d_bio(&PKCS8_PRIV_KEY_INFO_it, bp,
311 	    p8inf);
312 }
313 
314 PKCS8_PRIV_KEY_INFO *
315 d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO **p8inf)
316 {
317 	return ASN1_item_d2i_fp(&PKCS8_PRIV_KEY_INFO_it, fp,
318 	    p8inf);
319 }
320 
321 int
322 i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, PKCS8_PRIV_KEY_INFO *p8inf)
323 {
324 	return ASN1_item_i2d_fp(&PKCS8_PRIV_KEY_INFO_it, fp,
325 	    p8inf);
326 }
327 
328 EVP_PKEY *
329 d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
330 {
331 	return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey,
332 	    bp, a);
333 }
334 
335 int
336 i2d_PrivateKey_bio(BIO *bp, EVP_PKEY *pkey)
337 {
338 	return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey);
339 }
340 
341 EVP_PKEY *
342 d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
343 {
344 	return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey,
345 	    fp, a);
346 }
347 
348 int
349 i2d_PrivateKey_fp(FILE *fp, EVP_PKEY *pkey)
350 {
351 	return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey);
352 }
353 
354 int
355 i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key)
356 {
357 	PKCS8_PRIV_KEY_INFO *p8inf;
358 	int ret;
359 
360 	p8inf = EVP_PKEY2PKCS8(key);
361 	if (!p8inf)
362 		return 0;
363 	ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
364 	PKCS8_PRIV_KEY_INFO_free(p8inf);
365 	return ret;
366 }
367 
368 int
369 i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key)
370 {
371 	PKCS8_PRIV_KEY_INFO *p8inf;
372 	int ret;
373 	p8inf = EVP_PKEY2PKCS8(key);
374 	if (!p8inf)
375 		return 0;
376 	ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
377 	PKCS8_PRIV_KEY_INFO_free(p8inf);
378 	return ret;
379 }
380 
381 int
382 X509_verify(X509 *a, EVP_PKEY *r)
383 {
384 	if (X509_ALGOR_cmp(a->sig_alg, a->cert_info->signature))
385 		return 0;
386 	return (ASN1_item_verify(&X509_CINF_it, a->sig_alg,
387 	    a->signature, a->cert_info, r));
388 }
389 
390 int
391 X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
392 {
393 	return (ASN1_item_verify(&X509_REQ_INFO_it,
394 	    a->sig_alg, a->signature, a->req_info, r));
395 }
396 
397 int
398 NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
399 {
400 	return (ASN1_item_verify(&NETSCAPE_SPKAC_it,
401 	    a->sig_algor, a->signature, a->spkac, r));
402 }
403 
404 int
405 X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
406 {
407 	x->cert_info->enc.modified = 1;
408 	return (ASN1_item_sign(&X509_CINF_it,
409 	    x->cert_info->signature, x->sig_alg, x->signature,
410 	    x->cert_info, pkey, md));
411 }
412 
413 int
414 X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
415 {
416 	x->cert_info->enc.modified = 1;
417 	return ASN1_item_sign_ctx(&X509_CINF_it,
418 	    x->cert_info->signature, x->sig_alg, x->signature,
419 	    x->cert_info, ctx);
420 }
421 
422 int
423 X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
424 {
425 	return (ASN1_item_sign(&X509_REQ_INFO_it,
426 	    x->sig_alg, NULL, x->signature, x->req_info, pkey, md));
427 }
428 
429 int
430 X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
431 {
432 	return ASN1_item_sign_ctx(&X509_REQ_INFO_it,
433 	    x->sig_alg, NULL, x->signature, x->req_info, ctx);
434 }
435 
436 int
437 X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
438 {
439 	x->crl->enc.modified = 1;
440 	return(ASN1_item_sign(&X509_CRL_INFO_it, x->crl->sig_alg,
441 	    x->sig_alg, x->signature, x->crl, pkey, md));
442 }
443 
444 int
445 X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
446 {
447 	x->crl->enc.modified = 1;
448 	return ASN1_item_sign_ctx(&X509_CRL_INFO_it,
449 	    x->crl->sig_alg, x->sig_alg, x->signature, x->crl, ctx);
450 }
451 
452 int
453 NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
454 {
455 	return (ASN1_item_sign(&NETSCAPE_SPKAC_it,
456 	    x->sig_algor, NULL, x->signature, x->spkac, pkey, md));
457 }
458 
459 int
460 X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
461     unsigned int *len)
462 {
463 	ASN1_BIT_STRING *key;
464 	key = X509_get0_pubkey_bitstr(data);
465 	if (!key)
466 		return 0;
467 	return EVP_Digest(key->data, key->length, md, len, type, NULL);
468 }
469 
470 int
471 X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
472     unsigned int *len)
473 {
474 	return (ASN1_item_digest(&X509_it, type, (char *)data,
475 	    md, len));
476 }
477 
478 int
479 X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md,
480     unsigned int *len)
481 {
482 	return (ASN1_item_digest(&X509_CRL_it, type, (char *)data,
483 	    md, len));
484 }
485 
486 int
487 X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md,
488     unsigned int *len)
489 {
490 	return (ASN1_item_digest(&X509_REQ_it, type, (char *)data,
491 	    md, len));
492 }
493 
494 int
495 X509_NAME_digest(const X509_NAME *data, const EVP_MD *type, unsigned char *md,
496     unsigned int *len)
497 {
498 	return (ASN1_item_digest(&X509_NAME_it, type, (char *)data,
499 	    md, len));
500 }
501 
502 int
503 PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
504     const EVP_MD *type, unsigned char *md, unsigned int *len)
505 {
506 	return(ASN1_item_digest(&PKCS7_ISSUER_AND_SERIAL_it, type,
507 	    (char *)data, md, len));
508 }
509 
510 int
511 X509_up_ref(X509 *x)
512 {
513 	int i = CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
514 	return i > 1 ? 1 : 0;
515 }
516