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