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