2 * Copyright 1995-2020 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 <openssl/store.h>
21 #include <openssl/ui.h>
22 #include <openssl/serializer.h>
23 #include "crypto/store.h"
24 #include "crypto/asn1.h"
25 #include "crypto/evp.h"
26 #include "pem_local.h"
28 int pem_check_suffix(const char *pem_str, const char *suffix);
30 static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
31 pem_password_cb *cb, void *u,
32 OPENSSL_CTX *libctx, const char *propq,
33 int expected_store_info_type,
37 OSSL_STORE_CTX *ctx = NULL;
38 OSSL_STORE_INFO *info = NULL;
39 const UI_METHOD *ui_method = NULL;
40 UI_METHOD *allocated_ui_method = NULL;
42 if (expected_store_info_type != OSSL_STORE_INFO_PKEY
43 && expected_store_info_type != OSSL_STORE_INFO_PUBKEY
44 && expected_store_info_type != OSSL_STORE_INFO_PARAMS) {
45 ERR_raise(ERR_LIB_PEM, ERR_R_PASSED_INVALID_ARGUMENT);
49 if (u != NULL && cb == NULL)
50 cb = PEM_def_callback;
52 ui_method = UI_null();
54 ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
55 if (ui_method == NULL)
58 if ((ctx = OSSL_STORE_attach(bp, "file", libctx, propq, ui_method, u,
61 #ifndef OPENSSL_NO_SECURE_HEAP
64 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
69 while (!OSSL_STORE_eof(ctx)
70 && (info = OSSL_STORE_load(ctx)) != NULL) {
71 if (OSSL_STORE_INFO_get_type(info) == expected_store_info_type) {
72 switch (expected_store_info_type) {
73 case OSSL_STORE_INFO_PKEY:
74 ret = OSSL_STORE_INFO_get1_PKEY(info);
76 case OSSL_STORE_INFO_PUBKEY:
77 ret = OSSL_STORE_INFO_get1_PUBKEY(info);
79 case OSSL_STORE_INFO_PARAMS:
80 ret = OSSL_STORE_INFO_get1_PARAMS(info);
84 OSSL_STORE_INFO_free(info);
88 if (ret != NULL && x != NULL)
92 OSSL_STORE_close(ctx);
93 UI_destroy_method(allocated_ui_method);
94 OSSL_STORE_INFO_free(info);
98 EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
99 pem_password_cb *cb, void *u,
100 OPENSSL_CTX *libctx, const char *propq)
102 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
103 OSSL_STORE_INFO_PUBKEY, 0);
106 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
109 return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
112 #ifndef OPENSSL_NO_STDIO
113 EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
114 pem_password_cb *cb, void *u,
115 OPENSSL_CTX *libctx, const char *propq)
120 if ((b = BIO_new(BIO_s_file())) == NULL) {
121 PEMerr(0, ERR_R_BUF_LIB);
124 BIO_set_fp(b, fp, BIO_NOCLOSE);
125 ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
130 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
132 return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
136 EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
137 pem_password_cb *cb, void *u,
138 OPENSSL_CTX *libctx, const char *propq)
140 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
141 OSSL_STORE_INFO_PKEY, 1);
144 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
147 return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
150 PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
152 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
154 IMPLEMENT_PEM_provided_write_body_pass();
155 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
158 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
159 return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
160 (const char *)kstr, klen, cb, u);
161 return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
164 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
165 const EVP_CIPHER *enc,
166 const unsigned char *kstr, int klen,
167 pem_password_cb *cb, void *u)
170 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
171 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
172 pem_str, bp, x, enc, kstr, klen, cb, u);
175 EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
176 OPENSSL_CTX *libctx, const char *propq)
178 return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
179 OSSL_STORE_INFO_PARAMS, 0);
182 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
184 return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
187 PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
190 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
192 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
195 if (!x->ameth || !x->ameth->param_encode)
198 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
199 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
200 pem_str, out, x, NULL, NULL, 0, 0, NULL);
203 #ifndef OPENSSL_NO_STDIO
204 EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
205 void *u, OPENSSL_CTX *libctx,
211 if ((b = BIO_new(BIO_s_file())) == NULL) {
212 PEMerr(0, ERR_R_BUF_LIB);
215 BIO_set_fp(b, fp, BIO_NOCLOSE);
216 ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
221 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
224 return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
227 int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
228 const unsigned char *kstr, int klen,
229 pem_password_cb *cb, void *u)
234 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
235 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
238 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
245 #ifndef OPENSSL_NO_DH
247 /* Transparently read in PKCS#3 or X9.42 DH parameters */
249 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
252 EVP_PKEY *pkey = NULL;
253 OSSL_STORE_CTX *ctx = NULL;
254 OSSL_STORE_INFO *info = NULL;
255 UI_METHOD *ui_method = NULL;
257 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
260 if ((ctx = OSSL_STORE_attach(bp, "file", NULL, NULL, ui_method, u,
261 NULL, NULL)) == NULL)
264 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
265 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
266 pkey = OSSL_STORE_INFO_get0_PARAMS(info);
267 if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
268 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
269 ret = EVP_PKEY_get1_DH(pkey);
273 OSSL_STORE_INFO_free(info);
277 if (ret != NULL && x != NULL)
281 OSSL_STORE_close(ctx);
282 UI_destroy_method(ui_method);
283 OSSL_STORE_INFO_free(info);
287 # ifndef OPENSSL_NO_STDIO
288 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
293 if ((b = BIO_new(BIO_s_file())) == NULL) {
294 PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
297 BIO_set_fp(b, fp, BIO_NOCLOSE);
298 ret = PEM_read_bio_DHparams(b, x, cb, u);