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 <openssl/store.h>
21 #include <openssl/ui.h>
22 #include "crypto/store.h"
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
26 int pem_check_suffix(const char *pem_str, const char *suffix);
28 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
32 OSSL_STORE_CTX *ctx = NULL;
33 OSSL_STORE_INFO *info = NULL;
34 UI_METHOD *ui_method = NULL;
36 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
39 if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
41 #ifndef OPENSSL_NO_SECURE_HEAP
44 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
49 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
50 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
51 ret = OSSL_STORE_INFO_get1_PKEY(info);
54 OSSL_STORE_INFO_free(info);
57 if (ret != NULL && x != NULL)
61 ossl_store_detach_pem_bio(ctx);
62 UI_destroy_method(ui_method);
63 OSSL_STORE_INFO_free(info);
67 int PEM_write_bio_PrivateKey(BIO *bp, const EVP_PKEY *x,
68 const EVP_CIPHER *enc,
69 const unsigned char *kstr, int klen,
70 pem_password_cb *cb, void *u)
72 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
73 return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
74 (const char *)kstr, klen, cb, u);
75 return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
78 int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
79 const EVP_CIPHER *enc,
80 const unsigned char *kstr, int klen,
81 pem_password_cb *cb, void *u)
84 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
85 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
86 pem_str, bp, x, enc, kstr, klen, cb, u);
89 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
92 OSSL_STORE_CTX *ctx = NULL;
93 OSSL_STORE_INFO *info = NULL;
95 if ((ctx = ossl_store_attach_pem_bio(bp, UI_null(), NULL)) == NULL)
98 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
99 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
100 ret = OSSL_STORE_INFO_get1_PARAMS(info);
103 OSSL_STORE_INFO_free(info);
106 if (ret != NULL && x != NULL)
110 ossl_store_detach_pem_bio(ctx);
111 OSSL_STORE_INFO_free(info);
115 int PEM_write_bio_Parameters(BIO *bp, const EVP_PKEY *x)
118 if (!x->ameth || !x->ameth->param_encode)
121 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
122 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
123 pem_str, bp, x, NULL, NULL, 0, 0, NULL);
126 #ifndef OPENSSL_NO_STDIO
127 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
133 if ((b = BIO_new(BIO_s_file())) == NULL) {
134 PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
137 BIO_set_fp(b, fp, BIO_NOCLOSE);
138 ret = PEM_read_bio_PrivateKey(b, x, cb, u);
143 int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
144 const unsigned char *kstr, int klen,
145 pem_password_cb *cb, void *u)
150 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
151 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
154 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
161 #ifndef OPENSSL_NO_DH
163 /* Transparently read in PKCS#3 or X9.42 DH parameters */
165 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
168 EVP_PKEY *pkey = NULL;
169 OSSL_STORE_CTX *ctx = NULL;
170 OSSL_STORE_INFO *info = NULL;
171 UI_METHOD *ui_method = NULL;
173 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
176 if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
179 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
180 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
181 pkey = OSSL_STORE_INFO_get0_PARAMS(info);
182 if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
183 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
184 ret = EVP_PKEY_get1_DH(pkey);
188 OSSL_STORE_INFO_free(info);
191 if (ret != NULL && x != NULL)
195 ossl_store_detach_pem_bio(ctx);
196 UI_destroy_method(ui_method);
197 OSSL_STORE_INFO_free(info);
201 # ifndef OPENSSL_NO_STDIO
202 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
207 if ((b = BIO_new(BIO_s_file())) == NULL) {
208 PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
211 BIO_set_fp(b, fp, BIO_NOCLOSE);
212 ret = PEM_read_bio_DHparams(b, x, cb, u);