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