2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/buffer.h>
13 #include <openssl/objects.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509.h>
16 #include <openssl/pkcs12.h>
17 #include <openssl/pem.h>
18 #include <openssl/engine.h>
19 #include <openssl/dh.h>
20 #include "internal/asn1_int.h"
21 #include "internal/evp_int.h"
23 int pem_check_suffix(const char *pem_str
, const char *suffix
);
25 EVP_PKEY
*PEM_read_bio_PrivateKey(BIO
*bp
, EVP_PKEY
**x
, pem_password_cb
*cb
,
29 const unsigned char *p
= NULL
;
30 unsigned char *data
= NULL
;
35 if (!PEM_bytes_read_bio_secmem(&data
, &len
, &nm
, PEM_STRING_EVP_PKEY
, bp
,
40 if (strcmp(nm
, PEM_STRING_PKCS8INF
) == 0) {
41 PKCS8_PRIV_KEY_INFO
*p8inf
;
42 p8inf
= d2i_PKCS8_PRIV_KEY_INFO(NULL
, &p
, len
);
45 ret
= EVP_PKCS82PKEY(p8inf
);
47 EVP_PKEY_free((EVP_PKEY
*)*x
);
50 PKCS8_PRIV_KEY_INFO_free(p8inf
);
51 } else if (strcmp(nm
, PEM_STRING_PKCS8
) == 0) {
52 PKCS8_PRIV_KEY_INFO
*p8inf
;
55 char psbuf
[PEM_BUFSIZE
];
56 p8
= d2i_X509_SIG(NULL
, &p
, len
);
60 klen
= cb(psbuf
, PEM_BUFSIZE
, 0, u
);
62 klen
= PEM_def_callback(psbuf
, PEM_BUFSIZE
, 0, u
);
64 PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY
, PEM_R_BAD_PASSWORD_READ
);
68 p8inf
= PKCS8_decrypt(p8
, psbuf
, klen
);
70 OPENSSL_cleanse(psbuf
, klen
);
73 ret
= EVP_PKCS82PKEY(p8inf
);
75 EVP_PKEY_free((EVP_PKEY
*)*x
);
78 PKCS8_PRIV_KEY_INFO_free(p8inf
);
79 } else if ((slen
= pem_check_suffix(nm
, "PRIVATE KEY")) > 0) {
80 const EVP_PKEY_ASN1_METHOD
*ameth
;
81 ameth
= EVP_PKEY_asn1_find_str(NULL
, nm
, slen
);
82 if (!ameth
|| !ameth
->old_priv_decode
)
84 ret
= d2i_PrivateKey(ameth
->pkey_id
, x
, &p
, len
);
88 PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY
, ERR_R_ASN1_LIB
);
90 OPENSSL_secure_free(nm
);
91 OPENSSL_secure_clear_free(data
, len
);
95 int PEM_write_bio_PrivateKey(BIO
*bp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
96 unsigned char *kstr
, int klen
,
97 pem_password_cb
*cb
, void *u
)
99 if (x
->ameth
== NULL
|| x
->ameth
->priv_encode
!= NULL
)
100 return PEM_write_bio_PKCS8PrivateKey(bp
, x
, enc
,
101 (char *)kstr
, klen
, cb
, u
);
102 return PEM_write_bio_PrivateKey_traditional(bp
, x
, enc
, kstr
, klen
, cb
, u
);
105 int PEM_write_bio_PrivateKey_traditional(BIO
*bp
, EVP_PKEY
*x
,
106 const EVP_CIPHER
*enc
,
107 unsigned char *kstr
, int klen
,
108 pem_password_cb
*cb
, void *u
)
111 BIO_snprintf(pem_str
, 80, "%s PRIVATE KEY", x
->ameth
->pem_str
);
112 return PEM_ASN1_write_bio((i2d_of_void
*)i2d_PrivateKey
,
113 pem_str
, bp
, x
, enc
, kstr
, klen
, cb
, u
);
116 EVP_PKEY
*PEM_read_bio_Parameters(BIO
*bp
, EVP_PKEY
**x
)
119 const unsigned char *p
= NULL
;
120 unsigned char *data
= NULL
;
123 EVP_PKEY
*ret
= NULL
;
125 if (!PEM_bytes_read_bio(&data
, &len
, &nm
, PEM_STRING_PARAMETERS
,
130 if ((slen
= pem_check_suffix(nm
, "PARAMETERS")) > 0) {
131 ret
= EVP_PKEY_new();
134 if (!EVP_PKEY_set_type_str(ret
, nm
, slen
)
135 || !ret
->ameth
->param_decode
136 || !ret
->ameth
->param_decode(ret
, &p
, len
)) {
142 EVP_PKEY_free((EVP_PKEY
*)*x
);
148 PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS
, ERR_R_ASN1_LIB
);
154 int PEM_write_bio_Parameters(BIO
*bp
, EVP_PKEY
*x
)
157 if (!x
->ameth
|| !x
->ameth
->param_encode
)
160 BIO_snprintf(pem_str
, 80, "%s PARAMETERS", x
->ameth
->pem_str
);
161 return PEM_ASN1_write_bio((i2d_of_void
*)x
->ameth
->param_encode
,
162 pem_str
, bp
, x
, NULL
, NULL
, 0, 0, NULL
);
165 #ifndef OPENSSL_NO_STDIO
166 EVP_PKEY
*PEM_read_PrivateKey(FILE *fp
, EVP_PKEY
**x
, pem_password_cb
*cb
,
172 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
173 PEMerr(PEM_F_PEM_READ_PRIVATEKEY
, ERR_R_BUF_LIB
);
176 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
177 ret
= PEM_read_bio_PrivateKey(b
, x
, cb
, u
);
182 int PEM_write_PrivateKey(FILE *fp
, EVP_PKEY
*x
, const EVP_CIPHER
*enc
,
183 unsigned char *kstr
, int klen
,
184 pem_password_cb
*cb
, void *u
)
189 if ((b
= BIO_new_fp(fp
, BIO_NOCLOSE
)) == NULL
) {
190 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY
, ERR_R_BUF_LIB
);
193 ret
= PEM_write_bio_PrivateKey(b
, x
, enc
, kstr
, klen
, cb
, u
);
200 #ifndef OPENSSL_NO_DH
202 /* Transparently read in PKCS#3 or X9.42 DH parameters */
204 DH
*PEM_read_bio_DHparams(BIO
*bp
, DH
**x
, pem_password_cb
*cb
, void *u
)
207 const unsigned char *p
= NULL
;
208 unsigned char *data
= NULL
;
212 if (!PEM_bytes_read_bio(&data
, &len
, &nm
, PEM_STRING_DHPARAMS
, bp
, cb
, u
))
216 if (strcmp(nm
, PEM_STRING_DHXPARAMS
) == 0)
217 ret
= d2i_DHxparams(x
, &p
, len
);
219 ret
= d2i_DHparams(x
, &p
, len
);
222 PEMerr(PEM_F_PEM_READ_BIO_DHPARAMS
, ERR_R_ASN1_LIB
);
228 # ifndef OPENSSL_NO_STDIO
229 DH
*PEM_read_DHparams(FILE *fp
, DH
**x
, pem_password_cb
*cb
, void *u
)
234 if ((b
= BIO_new(BIO_s_file())) == NULL
) {
235 PEMerr(PEM_F_PEM_READ_DHPARAMS
, ERR_R_BUF_LIB
);
238 BIO_set_fp(b
, fp
, BIO_NOCLOSE
);
239 ret
= PEM_read_bio_DHparams(b
, x
, cb
, u
);