blob: fa7c01b14dd20d4df21845abb05751e2fbfd592a [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
7 *
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14 *
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
51 *
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.] */
56
David Benjamin419144a2018-09-22 19:35:12 -050057#include <openssl/x509.h>
58
59#include <limits.h>
60
Adam Langley95c29f32014-06-20 12:00:00 -070061#include <openssl/asn1.h>
Adam Langley95c29f32014-06-20 12:00:00 -070062#include <openssl/digest.h>
63#include <openssl/dsa.h>
64#include <openssl/evp.h>
David Benjamin419144a2018-09-22 19:35:12 -050065#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070066#include <openssl/rsa.h>
67#include <openssl/stack.h>
Adam Langley95c29f32014-06-20 12:00:00 -070068
David Benjaminaaecb822021-05-14 12:15:18 -040069#include "internal.h"
70
71
David Benjamin260a10c2022-06-16 13:58:28 -040072int X509_verify(X509 *x509, EVP_PKEY *pkey) {
73 if (X509_ALGOR_cmp(x509->sig_alg, x509->cert_info->signature)) {
74 OPENSSL_PUT_ERROR(X509, X509_R_SIGNATURE_ALGORITHM_MISMATCH);
75 return 0;
76 }
77 return ASN1_item_verify(ASN1_ITEM_rptr(X509_CINF), x509->sig_alg,
78 x509->signature, x509->cert_info, pkey);
Adam Langley57707c72016-01-14 11:25:12 -080079}
Adam Langley95c29f32014-06-20 12:00:00 -070080
David Benjamin260a10c2022-06-16 13:58:28 -040081int X509_REQ_verify(X509_REQ *req, EVP_PKEY *pkey) {
82 return ASN1_item_verify(ASN1_ITEM_rptr(X509_REQ_INFO), req->sig_alg,
83 req->signature, req->req_info, pkey);
Adam Langley57707c72016-01-14 11:25:12 -080084}
Adam Langley95c29f32014-06-20 12:00:00 -070085
David Benjamin260a10c2022-06-16 13:58:28 -040086int X509_sign(X509 *x, EVP_PKEY *pkey, const EVP_MD *md) {
87 x->cert_info->enc.modified = 1;
88 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
89 x->sig_alg, x->signature, x->cert_info, pkey, md));
Adam Langley57707c72016-01-14 11:25:12 -080090}
Adam Langley95c29f32014-06-20 12:00:00 -070091
David Benjamin260a10c2022-06-16 13:58:28 -040092int X509_sign_ctx(X509 *x, EVP_MD_CTX *ctx) {
93 x->cert_info->enc.modified = 1;
94 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CINF), x->cert_info->signature,
95 x->sig_alg, x->signature, x->cert_info, ctx);
Adam Langley57707c72016-01-14 11:25:12 -080096}
Adam Langley95c29f32014-06-20 12:00:00 -070097
David Benjamin260a10c2022-06-16 13:58:28 -040098int X509_REQ_sign(X509_REQ *x, EVP_PKEY *pkey, const EVP_MD *md) {
99 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
100 x->signature, x->req_info, pkey, md));
Adam Langley57707c72016-01-14 11:25:12 -0800101}
Adam Langley95c29f32014-06-20 12:00:00 -0700102
David Benjamin260a10c2022-06-16 13:58:28 -0400103int X509_REQ_sign_ctx(X509_REQ *x, EVP_MD_CTX *ctx) {
104 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_REQ_INFO), x->sig_alg, NULL,
105 x->signature, x->req_info, ctx);
Adam Langley57707c72016-01-14 11:25:12 -0800106}
Adam Langley95c29f32014-06-20 12:00:00 -0700107
David Benjamin260a10c2022-06-16 13:58:28 -0400108int X509_CRL_sign(X509_CRL *x, EVP_PKEY *pkey, const EVP_MD *md) {
109 x->crl->enc.modified = 1;
110 return (ASN1_item_sign(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
111 x->sig_alg, x->signature, x->crl, pkey, md));
Adam Langley57707c72016-01-14 11:25:12 -0800112}
Adam Langley95c29f32014-06-20 12:00:00 -0700113
David Benjamin260a10c2022-06-16 13:58:28 -0400114int X509_CRL_sign_ctx(X509_CRL *x, EVP_MD_CTX *ctx) {
115 x->crl->enc.modified = 1;
116 return ASN1_item_sign_ctx(ASN1_ITEM_rptr(X509_CRL_INFO), x->crl->sig_alg,
117 x->sig_alg, x->signature, x->crl, ctx);
Adam Langley57707c72016-01-14 11:25:12 -0800118}
Adam Langley95c29f32014-06-20 12:00:00 -0700119
David Benjamin260a10c2022-06-16 13:58:28 -0400120int NETSCAPE_SPKI_sign(NETSCAPE_SPKI *x, EVP_PKEY *pkey, const EVP_MD *md) {
121 return (ASN1_item_sign(ASN1_ITEM_rptr(NETSCAPE_SPKAC), x->sig_algor, NULL,
122 x->signature, x->spkac, pkey, md));
Adam Langley57707c72016-01-14 11:25:12 -0800123}
Adam Langley95c29f32014-06-20 12:00:00 -0700124
David Benjamin260a10c2022-06-16 13:58:28 -0400125int NETSCAPE_SPKI_verify(NETSCAPE_SPKI *spki, EVP_PKEY *pkey) {
126 return (ASN1_item_verify(ASN1_ITEM_rptr(NETSCAPE_SPKAC), spki->sig_algor,
127 spki->signature, spki->spkac, pkey));
Adam Langley57707c72016-01-14 11:25:12 -0800128}
Matt Braithwaite7e8f2ba2015-06-17 15:55:25 -0700129
David Benjamin260a10c2022-06-16 13:58:28 -0400130X509 *d2i_X509_fp(FILE *fp, X509 **x509) {
131 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509), fp, x509);
Adam Langley57707c72016-01-14 11:25:12 -0800132}
Adam Langley95c29f32014-06-20 12:00:00 -0700133
David Benjamin260a10c2022-06-16 13:58:28 -0400134int i2d_X509_fp(FILE *fp, X509 *x509) {
135 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509), fp, x509);
Adam Langley57707c72016-01-14 11:25:12 -0800136}
Adam Langley95c29f32014-06-20 12:00:00 -0700137
David Benjamin260a10c2022-06-16 13:58:28 -0400138X509 *d2i_X509_bio(BIO *bp, X509 **x509) {
139 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509), bp, x509);
Adam Langley57707c72016-01-14 11:25:12 -0800140}
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjamin260a10c2022-06-16 13:58:28 -0400142int i2d_X509_bio(BIO *bp, X509 *x509) {
143 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509), bp, x509);
Adam Langley57707c72016-01-14 11:25:12 -0800144}
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin260a10c2022-06-16 13:58:28 -0400146X509_CRL *d2i_X509_CRL_fp(FILE *fp, X509_CRL **crl) {
147 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
Adam Langley57707c72016-01-14 11:25:12 -0800148}
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjamin260a10c2022-06-16 13:58:28 -0400150int i2d_X509_CRL_fp(FILE *fp, X509_CRL *crl) {
151 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_CRL), fp, crl);
Adam Langley57707c72016-01-14 11:25:12 -0800152}
Adam Langley95c29f32014-06-20 12:00:00 -0700153
David Benjamin260a10c2022-06-16 13:58:28 -0400154X509_CRL *d2i_X509_CRL_bio(BIO *bp, X509_CRL **crl) {
155 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
Adam Langley57707c72016-01-14 11:25:12 -0800156}
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjamin260a10c2022-06-16 13:58:28 -0400158int i2d_X509_CRL_bio(BIO *bp, X509_CRL *crl) {
159 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_CRL), bp, crl);
Adam Langley57707c72016-01-14 11:25:12 -0800160}
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjamin260a10c2022-06-16 13:58:28 -0400162X509_REQ *d2i_X509_REQ_fp(FILE *fp, X509_REQ **req) {
163 return ASN1_item_d2i_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
Adam Langley57707c72016-01-14 11:25:12 -0800164}
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjamin260a10c2022-06-16 13:58:28 -0400166int i2d_X509_REQ_fp(FILE *fp, X509_REQ *req) {
167 return ASN1_item_i2d_fp(ASN1_ITEM_rptr(X509_REQ), fp, req);
Adam Langley57707c72016-01-14 11:25:12 -0800168}
Adam Langley95c29f32014-06-20 12:00:00 -0700169
David Benjamin260a10c2022-06-16 13:58:28 -0400170X509_REQ *d2i_X509_REQ_bio(BIO *bp, X509_REQ **req) {
171 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
Adam Langley57707c72016-01-14 11:25:12 -0800172}
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin260a10c2022-06-16 13:58:28 -0400174int i2d_X509_REQ_bio(BIO *bp, X509_REQ *req) {
175 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(X509_REQ), bp, req);
Adam Langley57707c72016-01-14 11:25:12 -0800176}
Adam Langley95c29f32014-06-20 12:00:00 -0700177
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamin419144a2018-09-22 19:35:12 -0500179#define IMPLEMENT_D2I_FP(type, name, bio_func) \
180 type *name(FILE *fp, type **obj) { \
181 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
182 if (bio == NULL) { \
183 return NULL; \
184 } \
185 type *ret = bio_func(bio, obj); \
186 BIO_free(bio); \
187 return ret; \
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
David Benjamin419144a2018-09-22 19:35:12 -0500190#define IMPLEMENT_I2D_FP(type, name, bio_func) \
191 int name(FILE *fp, type *obj) { \
192 BIO *bio = BIO_new_fp(fp, BIO_NOCLOSE); \
193 if (bio == NULL) { \
194 return 0; \
195 } \
196 int ret = bio_func(bio, obj); \
197 BIO_free(bio); \
198 return ret; \
199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjamin419144a2018-09-22 19:35:12 -0500201IMPLEMENT_D2I_FP(RSA, d2i_RSAPrivateKey_fp, d2i_RSAPrivateKey_bio)
202IMPLEMENT_I2D_FP(RSA, i2d_RSAPrivateKey_fp, i2d_RSAPrivateKey_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin419144a2018-09-22 19:35:12 -0500204IMPLEMENT_D2I_FP(RSA, d2i_RSAPublicKey_fp, d2i_RSAPublicKey_bio)
205IMPLEMENT_I2D_FP(RSA, i2d_RSAPublicKey_fp, i2d_RSAPublicKey_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjamin419144a2018-09-22 19:35:12 -0500207IMPLEMENT_D2I_FP(RSA, d2i_RSA_PUBKEY_fp, d2i_RSA_PUBKEY_bio)
208IMPLEMENT_I2D_FP(RSA, i2d_RSA_PUBKEY_fp, i2d_RSA_PUBKEY_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700209
David Benjamin419144a2018-09-22 19:35:12 -0500210#define IMPLEMENT_D2I_BIO(type, name, d2i_func) \
211 type *name(BIO *bio, type **obj) { \
212 uint8_t *data; \
213 size_t len; \
214 if (!BIO_read_asn1(bio, &data, &len, 100 * 1024)) { \
215 return NULL; \
216 } \
217 const uint8_t *ptr = data; \
218 type *ret = d2i_func(obj, &ptr, (long)len); \
219 OPENSSL_free(data); \
220 return ret; \
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin419144a2018-09-22 19:35:12 -0500223#define IMPLEMENT_I2D_BIO(type, name, i2d_func) \
224 int name(BIO *bio, type *obj) { \
225 uint8_t *data = NULL; \
226 int len = i2d_func(obj, &data); \
227 if (len < 0) { \
228 return 0; \
229 } \
230 int ret = BIO_write_all(bio, data, len); \
231 OPENSSL_free(data); \
232 return ret; \
233 }
Adam Langley95c29f32014-06-20 12:00:00 -0700234
David Benjamin419144a2018-09-22 19:35:12 -0500235IMPLEMENT_D2I_BIO(RSA, d2i_RSAPrivateKey_bio, d2i_RSAPrivateKey)
236IMPLEMENT_I2D_BIO(RSA, i2d_RSAPrivateKey_bio, i2d_RSAPrivateKey)
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamin419144a2018-09-22 19:35:12 -0500238IMPLEMENT_D2I_BIO(RSA, d2i_RSAPublicKey_bio, d2i_RSAPublicKey)
239IMPLEMENT_I2D_BIO(RSA, i2d_RSAPublicKey_bio, i2d_RSAPublicKey)
Adam Langley95c29f32014-06-20 12:00:00 -0700240
David Benjamin419144a2018-09-22 19:35:12 -0500241IMPLEMENT_D2I_BIO(RSA, d2i_RSA_PUBKEY_bio, d2i_RSA_PUBKEY)
242IMPLEMENT_I2D_BIO(RSA, i2d_RSA_PUBKEY_bio, i2d_RSA_PUBKEY)
Adam Langley95c29f32014-06-20 12:00:00 -0700243
244#ifndef OPENSSL_NO_DSA
David Benjamin419144a2018-09-22 19:35:12 -0500245IMPLEMENT_D2I_FP(DSA, d2i_DSAPrivateKey_fp, d2i_DSAPrivateKey_bio)
246IMPLEMENT_I2D_FP(DSA, i2d_DSAPrivateKey_fp, i2d_DSAPrivateKey_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700247
David Benjamin419144a2018-09-22 19:35:12 -0500248IMPLEMENT_D2I_FP(DSA, d2i_DSA_PUBKEY_fp, d2i_DSA_PUBKEY_bio)
249IMPLEMENT_I2D_FP(DSA, i2d_DSA_PUBKEY_fp, i2d_DSA_PUBKEY_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700250
David Benjamin419144a2018-09-22 19:35:12 -0500251IMPLEMENT_D2I_BIO(DSA, d2i_DSAPrivateKey_bio, d2i_DSAPrivateKey)
252IMPLEMENT_I2D_BIO(DSA, i2d_DSAPrivateKey_bio, i2d_DSAPrivateKey)
Adam Langley95c29f32014-06-20 12:00:00 -0700253
David Benjamin419144a2018-09-22 19:35:12 -0500254IMPLEMENT_D2I_BIO(DSA, d2i_DSA_PUBKEY_bio, d2i_DSA_PUBKEY)
255IMPLEMENT_I2D_BIO(DSA, i2d_DSA_PUBKEY_bio, i2d_DSA_PUBKEY)
Adam Langley95c29f32014-06-20 12:00:00 -0700256#endif
257
David Benjamin419144a2018-09-22 19:35:12 -0500258IMPLEMENT_D2I_FP(EC_KEY, d2i_ECPrivateKey_fp, d2i_ECPrivateKey_bio)
259IMPLEMENT_I2D_FP(EC_KEY, i2d_ECPrivateKey_fp, i2d_ECPrivateKey_bio)
Adam Langley57707c72016-01-14 11:25:12 -0800260
David Benjamin419144a2018-09-22 19:35:12 -0500261IMPLEMENT_D2I_FP(EC_KEY, d2i_EC_PUBKEY_fp, d2i_EC_PUBKEY_bio)
262IMPLEMENT_I2D_FP(EC_KEY, i2d_EC_PUBKEY_fp, i2d_EC_PUBKEY_bio)
Adam Langley57707c72016-01-14 11:25:12 -0800263
David Benjamin419144a2018-09-22 19:35:12 -0500264IMPLEMENT_D2I_BIO(EC_KEY, d2i_ECPrivateKey_bio, d2i_ECPrivateKey)
265IMPLEMENT_I2D_BIO(EC_KEY, i2d_ECPrivateKey_bio, i2d_ECPrivateKey)
Adam Langley95c29f32014-06-20 12:00:00 -0700266
David Benjamin419144a2018-09-22 19:35:12 -0500267IMPLEMENT_D2I_BIO(EC_KEY, d2i_EC_PUBKEY_bio, d2i_EC_PUBKEY)
268IMPLEMENT_I2D_BIO(EC_KEY, i2d_EC_PUBKEY_bio, i2d_EC_PUBKEY)
Adam Langley95c29f32014-06-20 12:00:00 -0700269
David Benjamin260a10c2022-06-16 13:58:28 -0400270int X509_pubkey_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
271 unsigned int *len) {
272 ASN1_BIT_STRING *key;
273 key = X509_get0_pubkey_bitstr(data);
274 if (!key)
275 return 0;
276 return EVP_Digest(key->data, key->length, md, len, type, NULL);
Adam Langley57707c72016-01-14 11:25:12 -0800277}
Adam Langley95c29f32014-06-20 12:00:00 -0700278
279int X509_digest(const X509 *data, const EVP_MD *type, unsigned char *md,
David Benjamin260a10c2022-06-16 13:58:28 -0400280 unsigned int *len) {
281 return (ASN1_item_digest(ASN1_ITEM_rptr(X509), type, (char *)data, md, len));
Adam Langley57707c72016-01-14 11:25:12 -0800282}
Adam Langley95c29f32014-06-20 12:00:00 -0700283
David Benjamin260a10c2022-06-16 13:58:28 -0400284int X509_CRL_digest(const X509_CRL *data, const EVP_MD *type, unsigned char *md,
285 unsigned int *len) {
286 return (
287 ASN1_item_digest(ASN1_ITEM_rptr(X509_CRL), type, (char *)data, md, len));
Adam Langley57707c72016-01-14 11:25:12 -0800288}
Adam Langley95c29f32014-06-20 12:00:00 -0700289
David Benjamin260a10c2022-06-16 13:58:28 -0400290int X509_REQ_digest(const X509_REQ *data, const EVP_MD *type, unsigned char *md,
291 unsigned int *len) {
292 return (
293 ASN1_item_digest(ASN1_ITEM_rptr(X509_REQ), type, (char *)data, md, len));
Adam Langley57707c72016-01-14 11:25:12 -0800294}
Adam Langley95c29f32014-06-20 12:00:00 -0700295
Adam Langley57707c72016-01-14 11:25:12 -0800296int X509_NAME_digest(const X509_NAME *data, const EVP_MD *type,
David Benjamin260a10c2022-06-16 13:58:28 -0400297 unsigned char *md, unsigned int *len) {
298 return (
299 ASN1_item_digest(ASN1_ITEM_rptr(X509_NAME), type, (char *)data, md, len));
Adam Langley57707c72016-01-14 11:25:12 -0800300}
Adam Langley95c29f32014-06-20 12:00:00 -0700301
David Benjamin419144a2018-09-22 19:35:12 -0500302IMPLEMENT_D2I_FP(X509_SIG, d2i_PKCS8_fp, d2i_PKCS8_bio)
303IMPLEMENT_I2D_FP(X509_SIG, i2d_PKCS8_fp, i2d_PKCS8_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700304
David Benjamin419144a2018-09-22 19:35:12 -0500305IMPLEMENT_D2I_BIO(X509_SIG, d2i_PKCS8_bio, d2i_X509_SIG)
306IMPLEMENT_I2D_BIO(X509_SIG, i2d_PKCS8_bio, i2d_X509_SIG)
Adam Langley95c29f32014-06-20 12:00:00 -0700307
David Benjamin419144a2018-09-22 19:35:12 -0500308IMPLEMENT_D2I_FP(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_fp,
309 d2i_PKCS8_PRIV_KEY_INFO_bio)
310IMPLEMENT_I2D_FP(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_fp,
311 i2d_PKCS8_PRIV_KEY_INFO_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjamin260a10c2022-06-16 13:58:28 -0400313int i2d_PKCS8PrivateKeyInfo_fp(FILE *fp, EVP_PKEY *key) {
314 PKCS8_PRIV_KEY_INFO *p8inf;
315 int ret;
316 p8inf = EVP_PKEY2PKCS8(key);
317 if (!p8inf)
318 return 0;
319 ret = i2d_PKCS8_PRIV_KEY_INFO_fp(fp, p8inf);
320 PKCS8_PRIV_KEY_INFO_free(p8inf);
321 return ret;
Adam Langley57707c72016-01-14 11:25:12 -0800322}
Adam Langley95c29f32014-06-20 12:00:00 -0700323
David Benjamin419144a2018-09-22 19:35:12 -0500324IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PrivateKey_fp, d2i_PrivateKey_bio)
325IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PrivateKey_fp, i2d_PrivateKey_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700326
David Benjamin419144a2018-09-22 19:35:12 -0500327IMPLEMENT_D2I_FP(EVP_PKEY, d2i_PUBKEY_fp, d2i_PUBKEY_bio)
328IMPLEMENT_I2D_FP(EVP_PKEY, i2d_PUBKEY_fp, i2d_PUBKEY_bio)
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamin419144a2018-09-22 19:35:12 -0500330IMPLEMENT_D2I_BIO(PKCS8_PRIV_KEY_INFO, d2i_PKCS8_PRIV_KEY_INFO_bio,
331 d2i_PKCS8_PRIV_KEY_INFO)
332IMPLEMENT_I2D_BIO(PKCS8_PRIV_KEY_INFO, i2d_PKCS8_PRIV_KEY_INFO_bio,
333 i2d_PKCS8_PRIV_KEY_INFO)
Adam Langley95c29f32014-06-20 12:00:00 -0700334
David Benjamin260a10c2022-06-16 13:58:28 -0400335int i2d_PKCS8PrivateKeyInfo_bio(BIO *bp, EVP_PKEY *key) {
336 PKCS8_PRIV_KEY_INFO *p8inf;
337 int ret;
338 p8inf = EVP_PKEY2PKCS8(key);
339 if (!p8inf)
340 return 0;
341 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(bp, p8inf);
342 PKCS8_PRIV_KEY_INFO_free(p8inf);
343 return ret;
Adam Langley57707c72016-01-14 11:25:12 -0800344}
Adam Langley95c29f32014-06-20 12:00:00 -0700345
David Benjamin419144a2018-09-22 19:35:12 -0500346IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PrivateKey_bio, d2i_AutoPrivateKey)
347IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PrivateKey_bio, i2d_PrivateKey)
Adam Langley95c29f32014-06-20 12:00:00 -0700348
David Benjamin419144a2018-09-22 19:35:12 -0500349IMPLEMENT_D2I_BIO(EVP_PKEY, d2i_PUBKEY_bio, d2i_PUBKEY)
350IMPLEMENT_I2D_BIO(EVP_PKEY, i2d_PUBKEY_bio, i2d_PUBKEY)
Adam Langley95c29f32014-06-20 12:00:00 -0700351
David Benjamin419144a2018-09-22 19:35:12 -0500352IMPLEMENT_D2I_BIO(DH, d2i_DHparams_bio, d2i_DHparams)
353IMPLEMENT_I2D_BIO(const DH, i2d_DHparams_bio, i2d_DHparams)