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