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
10 /* We need to use some STORE deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
14 #include "internal/cryptlib.h"
15 #include <openssl/buffer.h>
16 #include <openssl/objects.h>
17 #include <openssl/evp.h>
18 #include <openssl/x509.h>
19 #include <openssl/pkcs12.h>
20 #include <openssl/pem.h>
21 #include <openssl/engine.h>
22 #include <openssl/dh.h>
23 #include <openssl/store.h>
24 #include <openssl/ui.h>
25 #include "crypto/store.h"
26 #include "crypto/asn1.h"
27 #include "crypto/evp.h"
28 #include "pem_local.h"
30 int pem_check_suffix(const char *pem_str, const char *suffix);
32 static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
33 pem_password_cb *cb, void *u,
34 OPENSSL_CTX *libctx, const char *propq,
35 int expected_store_info_type,
39 OSSL_STORE_CTX *ctx = NULL;
40 OSSL_STORE_INFO *info = NULL;
41 const UI_METHOD *ui_method = NULL;
42 UI_METHOD *allocated_ui_method = NULL;
44 if (expected_store_info_type != OSSL_STORE_INFO_PKEY
45 && expected_store_info_type != OSSL_STORE_INFO_PUBKEY
46 && expected_store_info_type != OSSL_STORE_INFO_PARAMS) {
47 ERR_raise(ERR_LIB_PEM, ERR_R_PASSED_INVALID_ARGUMENT);
51 if (u != NULL && cb == NULL)
52 cb = PEM_def_callback;
54 ui_method = UI_null();
56 ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
57 if (ui_method == NULL)
60 if ((ctx = OSSL_STORE_attach(bp, "file", libctx, propq, ui_method, u,
63 #ifndef OPENSSL_NO_SECURE_HEAP
64 # ifndef OPENSSL_NO_DEPRECATED_3_0
67 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
73 while (!OSSL_STORE_eof(ctx)
74 && (info = OSSL_STORE_load(ctx)) != NULL) {
75 if (OSSL_STORE_INFO_get_type(info) == expected_store_info_type) {
76 switch (expected_store_info_type) {
77 case OSSL_STORE_INFO_PKEY:
78 ret = OSSL_STORE_INFO_get1_PKEY(info);
80 case OSSL_STORE_INFO_PUBKEY:
81 ret = OSSL_STORE_INFO_get1_PUBKEY(info);
83 case OSSL_STORE_INFO_PARAMS:
84 ret = OSSL_STORE_INFO_get1_PARAMS(info);
88 OSSL_STORE_INFO_free(info);
92 if (ret != NULL && x != NULL)
96 OSSL_STORE_close(ctx);
97 UI_destroy_method(allocated_ui_method);
98 OSSL_STORE_INFO_free(info);
102 EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
103 pem_password_cb *cb, void *u,
104 OPENSSL_CTX *libctx, const char *propq)
106 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
107 OSSL_STORE_INFO_PUBKEY, 0);
110 EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
113 return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
116 #ifndef OPENSSL_NO_STDIO
117 EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
118 pem_password_cb *cb, void *u,
119 OPENSSL_CTX *libctx, const char *propq)
124 if ((b = BIO_new(BIO_s_file())) == NULL) {
125 PEMerr(0, ERR_R_BUF_LIB);
128 BIO_set_fp(b, fp, BIO_NOCLOSE);
129 ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
134 EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
136 return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
140 EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
141 pem_password_cb *cb, void *u,
142 OPENSSL_CTX *libctx, const char *propq)
144 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
145 OSSL_STORE_INFO_PKEY, 1);
148 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
151 return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
154 PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
156 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
158 IMPLEMENT_PEM_provided_write_body_pass();
159 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
162 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
163 return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
164 (const char *)kstr, klen, cb, u);
165 return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
168 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
169 const EVP_CIPHER *enc,
170 const unsigned char *kstr, int klen,
171 pem_password_cb *cb, void *u)
175 if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
176 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
179 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
180 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
181 pem_str, bp, x, enc, kstr, klen, cb, u);
184 EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
185 OPENSSL_CTX *libctx, const char *propq)
187 return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
188 OSSL_STORE_INFO_PARAMS, 0);
191 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
193 return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
196 PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
199 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
201 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
204 if (!x->ameth || !x->ameth->param_encode)
207 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
208 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
209 pem_str, out, x, NULL, NULL, 0, 0, NULL);
212 #ifndef OPENSSL_NO_STDIO
213 EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
214 void *u, OPENSSL_CTX *libctx,
220 if ((b = BIO_new(BIO_s_file())) == NULL) {
221 PEMerr(0, ERR_R_BUF_LIB);
224 BIO_set_fp(b, fp, BIO_NOCLOSE);
225 ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
230 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
233 return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
236 int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
237 const unsigned char *kstr, int klen,
238 pem_password_cb *cb, void *u)
243 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
244 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
247 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
254 #ifndef OPENSSL_NO_DH
256 /* Transparently read in PKCS#3 or X9.42 DH parameters */
258 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
261 EVP_PKEY *pkey = NULL;
262 OSSL_STORE_CTX *ctx = NULL;
263 OSSL_STORE_INFO *info = NULL;
264 UI_METHOD *ui_method = NULL;
266 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
269 if ((ctx = OSSL_STORE_attach(bp, "file", NULL, NULL, ui_method, u,
270 NULL, NULL)) == NULL)
273 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
274 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
275 pkey = OSSL_STORE_INFO_get0_PARAMS(info);
276 if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
277 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
278 ret = EVP_PKEY_get1_DH(pkey);
282 OSSL_STORE_INFO_free(info);
286 if (ret != NULL && x != NULL)
290 OSSL_STORE_close(ctx);
291 UI_destroy_method(ui_method);
292 OSSL_STORE_INFO_free(info);
296 # ifndef OPENSSL_NO_STDIO
297 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
302 if ((b = BIO_new(BIO_s_file())) == NULL) {
303 PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
306 BIO_set_fp(b, fp, BIO_NOCLOSE);
307 ret = PEM_read_bio_DHparams(b, x, cb, u);