xref: /openssl/crypto/x509/x_all.c (revision cdbe47bf)
1 /*
2  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * Low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18 #include <openssl/buffer.h>
19 #include <openssl/asn1.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509.h>
22 #include <openssl/x509_acert.h>
23 #include <openssl/http.h>
24 #include <openssl/rsa.h>
25 #include <openssl/dsa.h>
26 #include <openssl/x509v3.h>
27 #include "internal/asn1.h"
28 #include "crypto/pkcs7.h"
29 #include "crypto/x509.h"
30 #include "crypto/x509_acert.h"
31 #include "crypto/rsa.h"
32 
X509_verify(X509 * a,EVP_PKEY * r)33 int X509_verify(X509 *a, EVP_PKEY *r)
34 {
35     if (X509_ALGOR_cmp(&a->sig_alg, &a->cert_info.signature) != 0)
36         return 0;
37 
38     return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_CINF), &a->sig_alg,
39                                &a->signature, &a->cert_info,
40                                a->distinguishing_id, r, a->libctx, a->propq);
41 }
42 
X509_REQ_verify_ex(X509_REQ * a,EVP_PKEY * r,OSSL_LIB_CTX * libctx,const char * propq)43 int X509_REQ_verify_ex(X509_REQ *a, EVP_PKEY *r, OSSL_LIB_CTX *libctx,
44                        const char *propq)
45 {
46     if (X509_REQ_get_version(a) != X509_REQ_VERSION_1) {
47         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_VERSION);
48         return -1;
49     }
50 
51     return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_REQ_INFO), &a->sig_alg,
52                                a->signature, &a->req_info, a->distinguishing_id,
53                                r, libctx, propq);
54 }
55 
X509_REQ_verify(X509_REQ * a,EVP_PKEY * r)56 int X509_REQ_verify(X509_REQ *a, EVP_PKEY *r)
57 {
58     return X509_REQ_verify_ex(a, r, NULL, NULL);
59 }
60 
X509_ACERT_verify(X509_ACERT * a,EVP_PKEY * r)61 int X509_ACERT_verify(X509_ACERT *a, EVP_PKEY *r)
62 {
63     if (X509_ALGOR_cmp(&a->sig_alg, &a->acinfo->signature) != 0)
64         return 0;
65 
66     return ASN1_item_verify_ex(ASN1_ITEM_rptr(X509_ACERT_INFO), &a->sig_alg,
67                                &a->signature, a->acinfo,
68                                NULL, r, NULL, NULL);
69 }
70 
NETSCAPE_SPKI_verify(NETSCAPE_SPKI * a,EVP_PKEY * r)71 int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *a, EVP_PKEY *r)
72 {
73     return ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC),
74                             &a->sig_algor, a->signature, a->spkac, r);
75 }
76 
X509_sign(X509 * x,EVP_PKEY * pkey,const EVP_MD * md)77 int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md)
78 {
79     if (x == NULL) {
80         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
81         return 0;
82     }
83     if (sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0
84             && !X509_set_version(x, X509_VERSION_3))
85         return 0;
86 
87     /*
88      * Setting the modified flag before signing it. This makes the cached
89      * encoding to be ignored, so even if the certificate fields have changed,
90      * they are signed correctly.
91      * The X509_sign_ctx, X509_REQ_sign{,_ctx}, X509_CRL_sign{,_ctx} functions
92      * which exist below are the same.
93      */
94     x->cert_info.enc.modified = 1;
95     return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_CINF), &x->cert_info.signature,
96                              &x->sig_alg, &x->signature, &x->cert_info, NULL,
97                              pkey, md, x->libctx, x->propq);
98 }
99 
X509_sign_ctx(X509 * x,EVP_MD_CTX * ctx)100 int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx)
101 {
102     if (x == NULL) {
103         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
104         return 0;
105     }
106     if (sk_X509_EXTENSION_num(X509_get0_extensions(x)) > 0
107             && !X509_set_version(x, X509_VERSION_3))
108         return 0;
109     x->cert_info.enc.modified = 1;
110     return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF),
111                               &x->cert_info.signature,
112                               &x->sig_alg, &x->signature, &x->cert_info, ctx);
113 }
114 
simple_get_asn1(const char * url,BIO * bio,BIO * rbio,int timeout,const ASN1_ITEM * it)115 static ASN1_VALUE *simple_get_asn1(const char *url, BIO *bio, BIO *rbio,
116                                    int timeout, const ASN1_ITEM *it)
117 {
118 #ifndef OPENSSL_NO_HTTP
119     size_t max_resp_len = (it == ASN1_ITEM_rptr(X509_CRL)) ?
120         OSSL_HTTP_DEFAULT_MAX_CRL_LEN : OSSL_HTTP_DEFAULT_MAX_RESP_LEN;
121     BIO *mem = OSSL_HTTP_get(url, NULL /* proxy */, NULL /* no_proxy */,
122                              bio, rbio, NULL /* cb */, NULL /* arg */,
123                              1024 /* buf_size */, NULL /* headers */,
124                              NULL /* expected_ct */, 1 /* expect_asn1 */,
125                              max_resp_len, timeout);
126     ASN1_VALUE *res = ASN1_item_d2i_bio(it, mem, NULL);
127 
128     BIO_free(mem);
129     return res;
130 #else
131     return 0;
132 #endif
133 }
134 
X509_load_http(const char * url,BIO * bio,BIO * rbio,int timeout)135 X509 *X509_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
136 {
137     return (X509 *)simple_get_asn1(url, bio, rbio, timeout,
138                                    ASN1_ITEM_rptr(X509));
139 }
140 
X509_REQ_sign(X509_REQ * x,EVP_PKEY * pkey,const EVP_MD * md)141 int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md)
142 {
143     if (x == NULL) {
144         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
145         return 0;
146     }
147     x->req_info.enc.modified = 1;
148     return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_REQ_INFO), &x->sig_alg, NULL,
149                              x->signature, &x->req_info, NULL,
150                              pkey, md, x->libctx, x->propq);
151 }
152 
X509_REQ_sign_ctx(X509_REQ * x,EVP_MD_CTX * ctx)153 int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx)
154 {
155     if (x == NULL) {
156         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
157         return 0;
158     }
159     x->req_info.enc.modified = 1;
160     return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO),
161                               &x->sig_alg, NULL, x->signature, &x->req_info,
162                               ctx);
163 }
164 
X509_CRL_sign(X509_CRL * x,EVP_PKEY * pkey,const EVP_MD * md)165 int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md)
166 {
167     if (x == NULL) {
168         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
169         return 0;
170     }
171     x->crl.enc.modified = 1;
172     return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_CRL_INFO), &x->crl.sig_alg,
173                              &x->sig_alg, &x->signature, &x->crl, NULL,
174                              pkey, md, x->libctx, x->propq);
175 }
176 
X509_CRL_sign_ctx(X509_CRL * x,EVP_MD_CTX * ctx)177 int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx)
178 {
179     if (x == NULL) {
180         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
181         return 0;
182     }
183     x->crl.enc.modified = 1;
184     return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO),
185                               &x->crl.sig_alg, &x->sig_alg, &x->signature,
186                               &x->crl, ctx);
187 }
188 
X509_CRL_load_http(const char * url,BIO * bio,BIO * rbio,int timeout)189 X509_CRL *X509_CRL_load_http(const char *url, BIO *bio, BIO *rbio, int timeout)
190 {
191     return (X509_CRL *)simple_get_asn1(url, bio, rbio, timeout,
192                                        ASN1_ITEM_rptr(X509_CRL));
193 }
194 
X509_ACERT_sign(X509_ACERT * x,EVP_PKEY * pkey,const EVP_MD * md)195 int X509_ACERT_sign(X509_ACERT *x, EVP_PKEY *pkey, const EVP_MD *md)
196 {
197     return ASN1_item_sign_ex(ASN1_ITEM_rptr(X509_ACERT_INFO), &x->sig_alg,
198                              &x->acinfo->signature,
199                              &x->signature, x->acinfo, NULL,
200                              pkey, md, NULL, NULL);
201 }
202 
X509_ACERT_sign_ctx(X509_ACERT * x,EVP_MD_CTX * ctx)203 int X509_ACERT_sign_ctx(X509_ACERT *x, EVP_MD_CTX *ctx)
204 {
205     return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_ACERT_INFO),
206                               &x->sig_alg, &x->acinfo->signature, &x->signature,
207                               &x->acinfo, ctx);
208 }
209 
NETSCAPE_SPKI_sign(NETSCAPE_SPKI * x,EVP_PKEY * pkey,const EVP_MD * md)210 int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md)
211 {
212     return
213         ASN1_item_sign_ex(ASN1_ITEM_rptr(NETSCAPE_SPKAC), &x->sig_algor, NULL,
214                           x->signature, x->spkac, NULL, pkey, md, NULL, NULL);
215 }
216 
217 #ifndef OPENSSL_NO_STDIO
d2i_X509_fp(FILE * fp,X509 ** x509)218 X509 *d2i_X509_fp(FILE *fp, X509 **x509)
219 {
220     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
221 }
222 
i2d_X509_fp(FILE * fp,const X509 * x509)223 int i2d_X509_fp(FILE *fp, const X509 *x509)
224 {
225     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
226 }
227 #endif
228 
d2i_X509_bio(BIO * bp,X509 ** x509)229 X509 *d2i_X509_bio(BIO *bp, X509 **x509)
230 {
231     return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
232 }
233 
i2d_X509_bio(BIO * bp,const X509 * x509)234 int i2d_X509_bio(BIO *bp, const X509 *x509)
235 {
236     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
237 }
238 
239 #ifndef OPENSSL_NO_STDIO
d2i_X509_CRL_fp(FILE * fp,X509_CRL ** crl)240 X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl)
241 {
242     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
243 }
244 
i2d_X509_CRL_fp(FILE * fp,const X509_CRL * crl)245 int i2d_X509_CRL_fp(FILE *fp, const X509_CRL *crl)
246 {
247     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
248 }
249 #endif
250 
d2i_X509_CRL_bio(BIO * bp,X509_CRL ** crl)251 X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl)
252 {
253     return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
254 }
255 
i2d_X509_CRL_bio(BIO * bp,const X509_CRL * crl)256 int i2d_X509_CRL_bio(BIO *bp, const X509_CRL *crl)
257 {
258     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
259 }
260 
261 #ifndef OPENSSL_NO_STDIO
d2i_PKCS7_fp(FILE * fp,PKCS7 ** p7)262 PKCS7 *d2i_PKCS7_fp(FILE *fp, PKCS7 **p7)
263 {
264     PKCS7 *ret;
265     OSSL_LIB_CTX *libctx = NULL;
266     const char *propq = NULL;
267 
268     if (p7 != NULL && *p7 != NULL) {
269         libctx = (*p7)->ctx.libctx;
270         propq = (*p7)->ctx.propq;
271     }
272 
273     ret = ASN1_item_d2i_fp_ex(ASN1_ITEM_rptr(PKCS7), fp, p7, libctx, propq);
274     if (ret != NULL)
275         ossl_pkcs7_resolve_libctx(ret);
276     return ret;
277 }
278 
i2d_PKCS7_fp(FILE * fp,const PKCS7 * p7)279 int i2d_PKCS7_fp(FILE *fp, const PKCS7 *p7)
280 {
281     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS7), fp, p7);
282 }
283 #endif
284 
d2i_PKCS7_bio(BIO * bp,PKCS7 ** p7)285 PKCS7 *d2i_PKCS7_bio(BIO *bp, PKCS7 **p7)
286 {
287     PKCS7 *ret;
288     OSSL_LIB_CTX *libctx = NULL;
289     const char *propq = NULL;
290 
291     if (p7 != NULL && *p7 != NULL) {
292         libctx = (*p7)->ctx.libctx;
293         propq = (*p7)->ctx.propq;
294     }
295 
296     ret = ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(PKCS7), bp, p7, libctx, propq);
297     if (ret != NULL)
298         ossl_pkcs7_resolve_libctx(ret);
299     return ret;
300 }
301 
i2d_PKCS7_bio(BIO * bp,const PKCS7 * p7)302 int i2d_PKCS7_bio(BIO *bp, const PKCS7 *p7)
303 {
304     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS7), bp, p7);
305 }
306 
307 #ifndef OPENSSL_NO_STDIO
d2i_X509_REQ_fp(FILE * fp,X509_REQ ** req)308 X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req)
309 {
310     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
311 }
312 
i2d_X509_REQ_fp(FILE * fp,const X509_REQ * req)313 int i2d_X509_REQ_fp(FILE *fp, const X509_REQ *req)
314 {
315     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
316 }
317 #endif
318 
d2i_X509_REQ_bio(BIO * bp,X509_REQ ** req)319 X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req)
320 {
321     OSSL_LIB_CTX *libctx = NULL;
322     const char *propq = NULL;
323 
324     if (req != NULL && *req != NULL) {
325         libctx = (*req)->libctx;
326         propq = (*req)->propq;
327     }
328 
329     return
330         ASN1_item_d2i_bio_ex(ASN1_ITEM_rptr(X509_REQ), bp, req, libctx, propq);
331 }
332 
i2d_X509_REQ_bio(BIO * bp,const X509_REQ * req)333 int i2d_X509_REQ_bio(BIO *bp, const X509_REQ *req)
334 {
335     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
336 }
337 
338 #ifndef OPENSSL_NO_STDIO
d2i_RSAPrivateKey_fp(FILE * fp,RSA ** rsa)339 RSA *d2i_RSAPrivateKey_fp(FILE *fp, RSA **rsa)
340 {
341     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
342 }
343 
i2d_RSAPrivateKey_fp(FILE * fp,const RSA * rsa)344 int i2d_RSAPrivateKey_fp(FILE *fp, const RSA *rsa)
345 {
346     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPrivateKey), fp, rsa);
347 }
348 
d2i_RSAPublicKey_fp(FILE * fp,RSA ** rsa)349 RSA *d2i_RSAPublicKey_fp(FILE *fp, RSA **rsa)
350 {
351     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
352 }
353 
d2i_RSA_PUBKEY_fp(FILE * fp,RSA ** rsa)354 RSA *d2i_RSA_PUBKEY_fp(FILE *fp, RSA **rsa)
355 {
356     return ASN1_d2i_fp((void *(*)(void))
357                        RSA_new, (D2I_OF(void)) d2i_RSA_PUBKEY, fp,
358                        (void **)rsa);
359 }
360 
i2d_RSAPublicKey_fp(FILE * fp,const RSA * rsa)361 int i2d_RSAPublicKey_fp(FILE *fp, const RSA *rsa)
362 {
363     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(RSAPublicKey), fp, rsa);
364 }
365 
i2d_RSA_PUBKEY_fp(FILE * fp,const RSA * rsa)366 int i2d_RSA_PUBKEY_fp(FILE *fp, const RSA *rsa)
367 {
368     return ASN1_i2d_fp((I2D_OF(void))i2d_RSA_PUBKEY, fp, rsa);
369 }
370 #endif
371 
d2i_RSAPrivateKey_bio(BIO * bp,RSA ** rsa)372 RSA *d2i_RSAPrivateKey_bio(BIO *bp, RSA **rsa)
373 {
374     return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
375 }
376 
i2d_RSAPrivateKey_bio(BIO * bp,const RSA * rsa)377 int i2d_RSAPrivateKey_bio(BIO *bp, const RSA *rsa)
378 {
379     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPrivateKey), bp, rsa);
380 }
381 
d2i_RSAPublicKey_bio(BIO * bp,RSA ** rsa)382 RSA *d2i_RSAPublicKey_bio(BIO *bp, RSA **rsa)
383 {
384     return ASN1_item_d2i_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
385 }
386 
d2i_RSA_PUBKEY_bio(BIO * bp,RSA ** rsa)387 RSA *d2i_RSA_PUBKEY_bio(BIO *bp, RSA **rsa)
388 {
389     return ASN1_d2i_bio_of(RSA, RSA_new, d2i_RSA_PUBKEY, bp, rsa);
390 }
391 
i2d_RSAPublicKey_bio(BIO * bp,const RSA * rsa)392 int i2d_RSAPublicKey_bio(BIO *bp, const RSA *rsa)
393 {
394     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(RSAPublicKey), bp, rsa);
395 }
396 
i2d_RSA_PUBKEY_bio(BIO * bp,const RSA * rsa)397 int i2d_RSA_PUBKEY_bio(BIO *bp, const RSA *rsa)
398 {
399     return ASN1_i2d_bio_of(RSA, i2d_RSA_PUBKEY, bp, rsa);
400 }
401 
402 #ifndef OPENSSL_NO_DSA
403 # ifndef OPENSSL_NO_STDIO
d2i_DSAPrivateKey_fp(FILE * fp,DSA ** dsa)404 DSA *d2i_DSAPrivateKey_fp(FILE *fp, DSA **dsa)
405 {
406     return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSAPrivateKey, fp, dsa);
407 }
408 
i2d_DSAPrivateKey_fp(FILE * fp,const DSA * dsa)409 int i2d_DSAPrivateKey_fp(FILE *fp, const DSA *dsa)
410 {
411     return ASN1_i2d_fp_of(DSA, i2d_DSAPrivateKey, fp, dsa);
412 }
413 
d2i_DSA_PUBKEY_fp(FILE * fp,DSA ** dsa)414 DSA *d2i_DSA_PUBKEY_fp(FILE *fp, DSA **dsa)
415 {
416     return ASN1_d2i_fp_of(DSA, DSA_new, d2i_DSA_PUBKEY, fp, dsa);
417 }
418 
i2d_DSA_PUBKEY_fp(FILE * fp,const DSA * dsa)419 int i2d_DSA_PUBKEY_fp(FILE *fp, const DSA *dsa)
420 {
421     return ASN1_i2d_fp_of(DSA, i2d_DSA_PUBKEY, fp, dsa);
422 }
423 # endif
424 
d2i_DSAPrivateKey_bio(BIO * bp,DSA ** dsa)425 DSA *d2i_DSAPrivateKey_bio(BIO *bp, DSA **dsa)
426 {
427     return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSAPrivateKey, bp, dsa);
428 }
429 
i2d_DSAPrivateKey_bio(BIO * bp,const DSA * dsa)430 int i2d_DSAPrivateKey_bio(BIO *bp, const DSA *dsa)
431 {
432     return ASN1_i2d_bio_of(DSA, i2d_DSAPrivateKey, bp, dsa);
433 }
434 
d2i_DSA_PUBKEY_bio(BIO * bp,DSA ** dsa)435 DSA *d2i_DSA_PUBKEY_bio(BIO *bp, DSA **dsa)
436 {
437     return ASN1_d2i_bio_of(DSA, DSA_new, d2i_DSA_PUBKEY, bp, dsa);
438 }
439 
i2d_DSA_PUBKEY_bio(BIO * bp,const DSA * dsa)440 int i2d_DSA_PUBKEY_bio(BIO *bp, const DSA *dsa)
441 {
442     return ASN1_i2d_bio_of(DSA, i2d_DSA_PUBKEY, bp, dsa);
443 }
444 
445 #endif
446 
447 #ifndef OPENSSL_NO_EC
448 # ifndef OPENSSL_NO_STDIO
d2i_EC_PUBKEY_fp(FILE * fp,EC_KEY ** eckey)449 EC_KEY *d2i_EC_PUBKEY_fp(FILE *fp, EC_KEY **eckey)
450 {
451     return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, fp, eckey);
452 }
453 
i2d_EC_PUBKEY_fp(FILE * fp,const EC_KEY * eckey)454 int i2d_EC_PUBKEY_fp(FILE *fp, const EC_KEY *eckey)
455 {
456     return ASN1_i2d_fp_of(EC_KEY, i2d_EC_PUBKEY, fp, eckey);
457 }
458 
d2i_ECPrivateKey_fp(FILE * fp,EC_KEY ** eckey)459 EC_KEY *d2i_ECPrivateKey_fp(FILE *fp, EC_KEY **eckey)
460 {
461     return ASN1_d2i_fp_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, fp, eckey);
462 }
463 
i2d_ECPrivateKey_fp(FILE * fp,const EC_KEY * eckey)464 int i2d_ECPrivateKey_fp(FILE *fp, const EC_KEY *eckey)
465 {
466     return ASN1_i2d_fp_of(EC_KEY, i2d_ECPrivateKey, fp, eckey);
467 }
468 # endif
d2i_EC_PUBKEY_bio(BIO * bp,EC_KEY ** eckey)469 EC_KEY *d2i_EC_PUBKEY_bio(BIO *bp, EC_KEY **eckey)
470 {
471     return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_EC_PUBKEY, bp, eckey);
472 }
473 
i2d_EC_PUBKEY_bio(BIO * bp,const EC_KEY * ecdsa)474 int i2d_EC_PUBKEY_bio(BIO *bp, const EC_KEY *ecdsa)
475 {
476     return ASN1_i2d_bio_of(EC_KEY, i2d_EC_PUBKEY, bp, ecdsa);
477 }
478 
d2i_ECPrivateKey_bio(BIO * bp,EC_KEY ** eckey)479 EC_KEY *d2i_ECPrivateKey_bio(BIO *bp, EC_KEY **eckey)
480 {
481     return ASN1_d2i_bio_of(EC_KEY, EC_KEY_new, d2i_ECPrivateKey, bp, eckey);
482 }
483 
i2d_ECPrivateKey_bio(BIO * bp,const EC_KEY * eckey)484 int i2d_ECPrivateKey_bio(BIO *bp, const EC_KEY *eckey)
485 {
486     return ASN1_i2d_bio_of(EC_KEY, i2d_ECPrivateKey, bp, eckey);
487 }
488 #endif
489 
X509_pubkey_digest(const X509 * data,const EVP_MD * type,unsigned char * md,unsigned int * len)490 int X509_pubkey_digest(const X509 *data, const EVP_MD *type,
491                        unsigned char *md, unsigned int *len)
492 {
493     ASN1_BIT_STRING *key = X509_get0_pubkey_bitstr(data);
494 
495     if (key == NULL)
496         return 0;
497     return EVP_Digest(key->data, key->length, md, len, type, NULL);
498 }
499 
X509_digest(const X509 * cert,const EVP_MD * md,unsigned char * data,unsigned int * len)500 int X509_digest(const X509 *cert, const EVP_MD *md, unsigned char *data,
501                 unsigned int *len)
502 {
503     if (EVP_MD_is_a(md, SN_sha1) && (cert->ex_flags & EXFLAG_SET) != 0
504             && (cert->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) {
505         /* Asking for SHA1 and we already computed it. */
506         if (len != NULL)
507             *len = sizeof(cert->sha1_hash);
508         memcpy(data, cert->sha1_hash, sizeof(cert->sha1_hash));
509         return 1;
510     }
511     return ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509), md, (char *)cert,
512                                     data, len, cert->libctx, cert->propq);
513 }
514 
515 /* calculate cert digest using the same hash algorithm as in its signature */
X509_digest_sig(const X509 * cert,EVP_MD ** md_used,int * md_is_fallback)516 ASN1_OCTET_STRING *X509_digest_sig(const X509 *cert,
517                                    EVP_MD **md_used, int *md_is_fallback)
518 {
519     unsigned int len;
520     unsigned char hash[EVP_MAX_MD_SIZE];
521     int mdnid, pknid;
522     EVP_MD *md = NULL;
523     const char *md_name;
524     ASN1_OCTET_STRING *new;
525 
526     if (md_used != NULL)
527         *md_used = NULL;
528     if (md_is_fallback != NULL)
529         *md_is_fallback = 0;
530 
531     if (cert == NULL) {
532         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
533         return NULL;
534     }
535 
536     if (!OBJ_find_sigid_algs(X509_get_signature_nid(cert), &mdnid, &pknid)) {
537         ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_SIGID_ALGS);
538         return NULL;
539     }
540 
541     if (mdnid == NID_undef) {
542         if (pknid == EVP_PKEY_RSA_PSS) {
543             RSA_PSS_PARAMS *pss = ossl_rsa_pss_decode(&cert->sig_alg);
544             const EVP_MD *mgf1md, *mmd = NULL;
545             int saltlen, trailerfield;
546 
547             if (pss == NULL
548                 || !ossl_rsa_pss_get_param_unverified(pss, &mmd, &mgf1md,
549                                                       &saltlen,
550                                                       &trailerfield)
551                 || mmd == NULL) {
552                 RSA_PSS_PARAMS_free(pss);
553                 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
554                 return NULL;
555             }
556             RSA_PSS_PARAMS_free(pss);
557             /* Fetch explicitly and do not fallback */
558             if ((md = EVP_MD_fetch(cert->libctx, EVP_MD_get0_name(mmd),
559                                    cert->propq)) == NULL)
560                 /* Error code from fetch is sufficient */
561                 return NULL;
562         } else if (pknid != NID_undef) {
563             /* A known algorithm, but without a digest */
564             switch (pknid) {
565             case NID_ED25519: /* Follow CMS default given in RFC8419 */
566                 md_name = "SHA512";
567                 break;
568             case NID_ED448: /* Follow CMS default given in RFC8419 */
569                 md_name = "SHAKE256";
570                 break;
571             default: /* Fall back to SHA-256 */
572                 md_name = "SHA256";
573                 break;
574             }
575             if ((md = EVP_MD_fetch(cert->libctx, md_name,
576                                    cert->propq)) == NULL)
577                 return NULL;
578             if (md_is_fallback != NULL)
579                 *md_is_fallback = 1;
580         } else {
581             /* A completely unknown algorithm */
582             ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
583             return NULL;
584         }
585     } else if ((md = EVP_MD_fetch(cert->libctx, OBJ_nid2sn(mdnid),
586                                   cert->propq)) == NULL
587                && (md = (EVP_MD *)EVP_get_digestbynid(mdnid)) == NULL) {
588         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
589         return NULL;
590     }
591     if (!X509_digest(cert, md, hash, &len)
592             || (new = ASN1_OCTET_STRING_new()) == NULL)
593         goto err;
594     if (ASN1_OCTET_STRING_set(new, hash, len)) {
595         if (md_used != NULL)
596             *md_used = md;
597         else
598             EVP_MD_free(md);
599         return new;
600     }
601     ASN1_OCTET_STRING_free(new);
602  err:
603     EVP_MD_free(md);
604     return NULL;
605 }
606 
X509_CRL_digest(const X509_CRL * data,const EVP_MD * type,unsigned char * md,unsigned int * len)607 int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type,
608                     unsigned char *md, unsigned int *len)
609 {
610     if (type == NULL) {
611         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
612         return 0;
613     }
614     if (EVP_MD_is_a(type, SN_sha1)
615             && (data->flags & EXFLAG_SET) != 0
616             && (data->flags & EXFLAG_NO_FINGERPRINT) == 0) {
617         /* Asking for SHA1; always computed in CRL d2i. */
618         if (len != NULL)
619             *len = sizeof(data->sha1_hash);
620         memcpy(md, data->sha1_hash, sizeof(data->sha1_hash));
621         return 1;
622     }
623     return
624         ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509_CRL), type, (char *)data,
625                                  md, len, data->libctx, data->propq);
626 }
627 
X509_REQ_digest(const X509_REQ * data,const EVP_MD * type,unsigned char * md,unsigned int * len)628 int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type,
629                     unsigned char *md, unsigned int *len)
630 {
631     return
632         ossl_asn1_item_digest_ex(ASN1_ITEM_rptr(X509_REQ), type, (char *)data,
633                                  md, len, data->libctx, data->propq);
634 }
635 
X509_NAME_digest(const X509_NAME * data,const EVP_MD * type,unsigned char * md,unsigned int * len)636 int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
637                      unsigned char *md, unsigned int *len)
638 {
639     return ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data,
640                             md, len);
641 }
642 
PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL * data,const EVP_MD * type,unsigned char * md,unsigned int * len)643 int PKCS7_ISSUER_AND_SERIAL_digest(PKCS7_ISSUER_AND_SERIAL *data,
644                                    const EVP_MD *type, unsigned char *md,
645                                    unsigned int *len)
646 {
647     return ASN1_item_digest(ASN1_ITEM_rptr(PKCS7_ISSUER_AND_SERIAL), type,
648                             (char *)data, md, len);
649 }
650 
651 #ifndef OPENSSL_NO_STDIO
d2i_PKCS8_fp(FILE * fp,X509_SIG ** p8)652 X509_SIG *d2i_PKCS8_fp(FILE *fp, X509_SIG **p8)
653 {
654     return ASN1_d2i_fp_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, fp, p8);
655 }
656 
i2d_PKCS8_fp(FILE * fp,const X509_SIG * p8)657 int i2d_PKCS8_fp(FILE *fp, const X509_SIG *p8)
658 {
659     return ASN1_i2d_fp_of(X509_SIG, i2d_X509_SIG, fp, p8);
660 }
661 #endif
662 
d2i_PKCS8_bio(BIO * bp,X509_SIG ** p8)663 X509_SIG *d2i_PKCS8_bio(BIO *bp, X509_SIG **p8)
664 {
665     return ASN1_d2i_bio_of(X509_SIG, X509_SIG_new, d2i_X509_SIG, bp, p8);
666 }
667 
i2d_PKCS8_bio(BIO * bp,const X509_SIG * p8)668 int i2d_PKCS8_bio(BIO *bp, const X509_SIG *p8)
669 {
670     return ASN1_i2d_bio_of(X509_SIG, i2d_X509_SIG, bp, p8);
671 }
672 
673 #ifndef OPENSSL_NO_STDIO
d2i_X509_PUBKEY_fp(FILE * fp,X509_PUBKEY ** xpk)674 X509_PUBKEY *d2i_X509_PUBKEY_fp(FILE *fp, X509_PUBKEY **xpk)
675 {
676     return ASN1_d2i_fp_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
677                           fp, xpk);
678 }
679 
i2d_X509_PUBKEY_fp(FILE * fp,const X509_PUBKEY * xpk)680 int i2d_X509_PUBKEY_fp(FILE *fp, const X509_PUBKEY *xpk)
681 {
682     return ASN1_i2d_fp_of(X509_PUBKEY, i2d_X509_PUBKEY, fp, xpk);
683 }
684 #endif
685 
d2i_X509_PUBKEY_bio(BIO * bp,X509_PUBKEY ** xpk)686 X509_PUBKEY *d2i_X509_PUBKEY_bio(BIO *bp, X509_PUBKEY **xpk)
687 {
688     return ASN1_d2i_bio_of(X509_PUBKEY, X509_PUBKEY_new, d2i_X509_PUBKEY,
689                            bp, xpk);
690 }
691 
i2d_X509_PUBKEY_bio(BIO * bp,const X509_PUBKEY * xpk)692 int i2d_X509_PUBKEY_bio(BIO *bp, const X509_PUBKEY *xpk)
693 {
694     return ASN1_i2d_bio_of(X509_PUBKEY, i2d_X509_PUBKEY, bp, xpk);
695 }
696 
697 #ifndef OPENSSL_NO_STDIO
d2i_PKCS8_PRIV_KEY_INFO_fp(FILE * fp,PKCS8_PRIV_KEY_INFO ** p8inf)698 PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_fp(FILE *fp,
699                                                 PKCS8_PRIV_KEY_INFO **p8inf)
700 {
701     return ASN1_d2i_fp_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
702                           d2i_PKCS8_PRIV_KEY_INFO, fp, p8inf);
703 }
704 
i2d_PKCS8_PRIV_KEY_INFO_fp(FILE * fp,const PKCS8_PRIV_KEY_INFO * p8inf)705 int i2d_PKCS8_PRIV_KEY_INFO_fp(FILE *fp, const PKCS8_PRIV_KEY_INFO *p8inf)
706 {
707     return ASN1_i2d_fp_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, fp,
708                           p8inf);
709 }
710 
i2d_PKCS8PrivateKeyInfo_fp(FILE * fp,const EVP_PKEY * key)711 int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, const EVP_PKEY *key)
712 {
713     PKCS8_PRIV_KEY_INFO *p8inf;
714     int ret;
715 
716     p8inf = EVP_PKEY2PKCS8(key);
717     if (p8inf == NULL)
718         return 0;
719     ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
720     PKCS8_PRIV_KEY_INFO_free(p8inf);
721     return ret;
722 }
723 
i2d_PrivateKey_fp(FILE * fp,const EVP_PKEY * pkey)724 int i2d_PrivateKey_fp(FILE *fp, const EVP_PKEY *pkey)
725 {
726     return ASN1_i2d_fp_of(EVP_PKEY, i2d_PrivateKey, fp, pkey);
727 }
728 
d2i_PrivateKey_fp(FILE * fp,EVP_PKEY ** a)729 EVP_PKEY *d2i_PrivateKey_fp(FILE *fp, EVP_PKEY **a)
730 {
731     return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, fp, a);
732 }
733 
d2i_PrivateKey_ex_fp(FILE * fp,EVP_PKEY ** a,OSSL_LIB_CTX * libctx,const char * propq)734 EVP_PKEY *d2i_PrivateKey_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
735                                const char *propq)
736 {
737     BIO *b;
738     void *ret;
739 
740     if ((b = BIO_new(BIO_s_file())) == NULL) {
741         ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
742         return NULL;
743     }
744     BIO_set_fp(b, fp, BIO_NOCLOSE);
745     ret = d2i_PrivateKey_ex_bio(b, a, libctx, propq);
746     BIO_free(b);
747     return ret;
748 }
749 
i2d_PUBKEY_fp(FILE * fp,const EVP_PKEY * pkey)750 int i2d_PUBKEY_fp(FILE *fp, const EVP_PKEY *pkey)
751 {
752     return ASN1_i2d_fp_of(EVP_PKEY, i2d_PUBKEY, fp, pkey);
753 }
754 
d2i_PUBKEY_ex_fp(FILE * fp,EVP_PKEY ** a,OSSL_LIB_CTX * libctx,const char * propq)755 EVP_PKEY *d2i_PUBKEY_ex_fp(FILE *fp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
756                            const char *propq)
757 {
758     BIO *b;
759     void *ret;
760 
761     if ((b = BIO_new(BIO_s_file())) == NULL) {
762         ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB);
763         return NULL;
764     }
765     BIO_set_fp(b, fp, BIO_NOCLOSE);
766     ret = d2i_PUBKEY_ex_bio(b, a, libctx, propq);
767     BIO_free(b);
768     return ret;
769 }
770 
d2i_PUBKEY_fp(FILE * fp,EVP_PKEY ** a)771 EVP_PKEY *d2i_PUBKEY_fp(FILE *fp, EVP_PKEY **a)
772 {
773     return ASN1_d2i_fp_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, fp, a);
774 }
775 
776 #endif
777 
d2i_PKCS8_PRIV_KEY_INFO_bio(BIO * bp,PKCS8_PRIV_KEY_INFO ** p8inf)778 PKCS8_PRIV_KEY_INFO *d2i_PKCS8_PRIV_KEY_INFO_bio(BIO *bp,
779                                                  PKCS8_PRIV_KEY_INFO **p8inf)
780 {
781     return ASN1_d2i_bio_of(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_new,
782                            d2i_PKCS8_PRIV_KEY_INFO, bp, p8inf);
783 }
784 
i2d_PKCS8_PRIV_KEY_INFO_bio(BIO * bp,const PKCS8_PRIV_KEY_INFO * p8inf)785 int i2d_PKCS8_PRIV_KEY_INFO_bio(BIO *bp, const PKCS8_PRIV_KEY_INFO *p8inf)
786 {
787     return ASN1_i2d_bio_of(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO, bp,
788                            p8inf);
789 }
790 
i2d_PKCS8PrivateKeyInfo_bio(BIO * bp,const EVP_PKEY * key)791 int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, const EVP_PKEY *key)
792 {
793     PKCS8_PRIV_KEY_INFO *p8inf;
794     int ret;
795 
796     p8inf = EVP_PKEY2PKCS8(key);
797     if (p8inf == NULL)
798         return 0;
799     ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
800     PKCS8_PRIV_KEY_INFO_free(p8inf);
801     return ret;
802 }
803 
i2d_PrivateKey_bio(BIO * bp,const EVP_PKEY * pkey)804 int i2d_PrivateKey_bio(BIO *bp, const EVP_PKEY *pkey)
805 {
806     return ASN1_i2d_bio_of(EVP_PKEY, i2d_PrivateKey, bp, pkey);
807 }
808 
d2i_PrivateKey_bio(BIO * bp,EVP_PKEY ** a)809 EVP_PKEY *d2i_PrivateKey_bio(BIO *bp, EVP_PKEY **a)
810 {
811     return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_AutoPrivateKey, bp, a);
812 }
813 
d2i_PrivateKey_ex_bio(BIO * bp,EVP_PKEY ** a,OSSL_LIB_CTX * libctx,const char * propq)814 EVP_PKEY *d2i_PrivateKey_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
815                                 const char *propq)
816 {
817     BUF_MEM *b = NULL;
818     const unsigned char *p;
819     void *ret = NULL;
820     int len;
821 
822     len = asn1_d2i_read_bio(bp, &b);
823     if (len < 0)
824         goto err;
825 
826     p = (unsigned char *)b->data;
827     ret = d2i_AutoPrivateKey_ex(a, &p, len, libctx, propq);
828  err:
829     BUF_MEM_free(b);
830     return ret;
831 }
832 
i2d_PUBKEY_bio(BIO * bp,const EVP_PKEY * pkey)833 int i2d_PUBKEY_bio(BIO *bp, const EVP_PKEY *pkey)
834 {
835     return ASN1_i2d_bio_of(EVP_PKEY, i2d_PUBKEY, bp, pkey);
836 }
837 
d2i_PUBKEY_ex_bio(BIO * bp,EVP_PKEY ** a,OSSL_LIB_CTX * libctx,const char * propq)838 EVP_PKEY *d2i_PUBKEY_ex_bio(BIO *bp, EVP_PKEY **a, OSSL_LIB_CTX *libctx,
839                             const char *propq)
840 {
841     BUF_MEM *b = NULL;
842     const unsigned char *p;
843     void *ret = NULL;
844     int len;
845 
846     len = asn1_d2i_read_bio(bp, &b);
847     if (len < 0)
848         goto err;
849 
850     p = (unsigned char *)b->data;
851     ret = d2i_PUBKEY_ex(a, &p, len, libctx, propq);
852  err:
853     BUF_MEM_free(b);
854     return ret;
855 }
856 
d2i_PUBKEY_bio(BIO * bp,EVP_PKEY ** a)857 EVP_PKEY *d2i_PUBKEY_bio(BIO *bp, EVP_PKEY **a)
858 {
859     return ASN1_d2i_bio_of(EVP_PKEY, EVP_PKEY_new, d2i_PUBKEY, bp, a);
860 }
861 
862 #ifndef OPENSSL_NO_STDIO
d2i_X509_ACERT_fp(FILE * fp,X509_ACERT ** acert)863 X509_ACERT *d2i_X509_ACERT_fp(FILE *fp, X509_ACERT **acert)
864 {
865     return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_ACERT), fp, acert);
866 }
867 
i2d_X509_ACERT_fp(FILE * fp,const X509_ACERT * acert)868 int i2d_X509_ACERT_fp(FILE *fp, const X509_ACERT *acert)
869 {
870     return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_ACERT), fp, acert);
871 }
872 #endif
873 
d2i_X509_ACERT_bio(BIO * bp,X509_ACERT ** acert)874 X509_ACERT *d2i_X509_ACERT_bio(BIO *bp, X509_ACERT **acert)
875 {
876     return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_ACERT), bp, acert);
877 }
878 
i2d_X509_ACERT_bio(BIO * bp,const X509_ACERT * acert)879 int i2d_X509_ACERT_bio(BIO *bp, const X509_ACERT *acert)
880 {
881     return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_ACERT), bp, acert);
882 }
883