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