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