]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pem/pem_pkey.c
Rename OSSL_SERIALIZER / OSSL_DESERIALIZER to OSSL_ENCODE / OSSL_DECODE
[thirdparty/openssl.git] / crypto / pem / pem_pkey.c
CommitLineData
62867571 1/*
33388b44 2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
1241126a 3 *
16742672 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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
1241126a
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
1241126a
DSH
12#include <openssl/buffer.h>
13#include <openssl/objects.h>
14#include <openssl/evp.h>
1241126a
DSH
15#include <openssl/x509.h>
16#include <openssl/pkcs12.h>
17#include <openssl/pem.h>
3c27208f
RS
18#include <openssl/engine.h>
19#include <openssl/dh.h>
1427d33c
RL
20#include <openssl/store.h>
21#include <openssl/ui.h>
22#include "crypto/store.h"
25f2138b
DMSP
23#include "crypto/asn1.h"
24#include "crypto/evp.h"
f864a939 25#include "pem_local.h"
1241126a 26
e4263314 27int pem_check_suffix(const char *pem_str, const char *suffix);
1241126a 28
6e5ccd58
RL
29static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
30 pem_password_cb *cb, void *u,
31 OPENSSL_CTX *libctx, const char *propq,
32 int expected_store_info_type,
33 int try_secure)
0f113f3e 34{
0f113f3e 35 EVP_PKEY *ret = NULL;
1427d33c
RL
36 OSSL_STORE_CTX *ctx = NULL;
37 OSSL_STORE_INFO *info = NULL;
6e5ccd58
RL
38 const UI_METHOD *ui_method = NULL;
39 UI_METHOD *allocated_ui_method = NULL;
1241126a 40
6e5ccd58
RL
41 if (expected_store_info_type != OSSL_STORE_INFO_PKEY
42 && expected_store_info_type != OSSL_STORE_INFO_PUBKEY
43 && expected_store_info_type != OSSL_STORE_INFO_PARAMS) {
44 ERR_raise(ERR_LIB_PEM, ERR_R_PASSED_INVALID_ARGUMENT);
45 return NULL;
46 }
47
48 if (u != NULL && cb == NULL)
49 cb = PEM_def_callback;
50 if (cb == NULL)
51 ui_method = UI_null();
52 else
53 ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
54 if (ui_method == NULL)
0f113f3e 55 return NULL;
1427d33c 56
6725682d 57 if ((ctx = OSSL_STORE_attach(bp, "file", libctx, propq, ui_method, u,
6ab6ecfd 58 NULL, NULL)) == NULL)
1427d33c
RL
59 goto err;
60#ifndef OPENSSL_NO_SECURE_HEAP
6e5ccd58 61 if (try_secure) {
1427d33c
RL
62 int on = 1;
63 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
0f113f3e 64 goto err;
1427d33c
RL
65 }
66#endif
67
6725682d
SL
68 while (!OSSL_STORE_eof(ctx)
69 && (info = OSSL_STORE_load(ctx)) != NULL) {
6e5ccd58
RL
70 if (OSSL_STORE_INFO_get_type(info) == expected_store_info_type) {
71 switch (expected_store_info_type) {
72 case OSSL_STORE_INFO_PKEY:
73 ret = OSSL_STORE_INFO_get1_PKEY(info);
74 break;
75 case OSSL_STORE_INFO_PUBKEY:
76 ret = OSSL_STORE_INFO_get1_PUBKEY(info);
77 break;
78 case OSSL_STORE_INFO_PARAMS:
79 ret = OSSL_STORE_INFO_get1_PARAMS(info);
80 break;
81 }
0f113f3e 82 }
1427d33c 83 OSSL_STORE_INFO_free(info);
6ab6ecfd 84 info = NULL;
0f113f3e 85 }
1427d33c
RL
86
87 if (ret != NULL && x != NULL)
88 *x = ret;
89
0f113f3e 90 err:
6ab6ecfd 91 OSSL_STORE_close(ctx);
6e5ccd58 92 UI_destroy_method(allocated_ui_method);
1427d33c 93 OSSL_STORE_INFO_free(info);
26a7d938 94 return ret;
0f113f3e 95}
1241126a 96
6e5ccd58
RL
97EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
98 pem_password_cb *cb, void *u,
99 OPENSSL_CTX *libctx, const char *propq)
100{
101 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
102 OSSL_STORE_INFO_PUBKEY, 0);
103}
104
105EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
106 void *u)
107{
108 return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
109}
110
111#ifndef OPENSSL_NO_STDIO
112EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
113 pem_password_cb *cb, void *u,
114 OPENSSL_CTX *libctx, const char *propq)
115{
116 BIO *b;
117 EVP_PKEY *ret;
118
119 if ((b = BIO_new(BIO_s_file())) == NULL) {
120 PEMerr(0, ERR_R_BUF_LIB);
121 return 0;
122 }
123 BIO_set_fp(b, fp, BIO_NOCLOSE);
124 ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
125 BIO_free(b);
126 return ret;
127}
128
129EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
130{
131 return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
132}
133#endif
134
135EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
136 pem_password_cb *cb, void *u,
137 OPENSSL_CTX *libctx, const char *propq)
138{
139 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
140 OSSL_STORE_INFO_PKEY, 1);
141}
142
1531241c
MC
143EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
144 void *u)
145{
146 return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
147}
148
f864a939 149PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
0f113f3e 150{
f864a939
RL
151 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey);
152
153 IMPLEMENT_PEM_provided_write_body_pass();
154 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
155
156 legacy:
05dba815 157 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
f864a939 158 return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
de0799b0 159 (const char *)kstr, klen, cb, u);
f864a939 160 return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
05dba815 161}
e4263314 162
de0799b0 163int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
05dba815 164 const EVP_CIPHER *enc,
de0799b0 165 const unsigned char *kstr, int klen,
05dba815
DSH
166 pem_password_cb *cb, void *u)
167{
168 char pem_str[80];
0f113f3e
MC
169 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
170 return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
171 pem_str, bp, x, enc, kstr, klen, cb, u);
172}
e4263314 173
6e5ccd58
RL
174EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
175 OPENSSL_CTX *libctx, const char *propq)
0f113f3e 176{
6e5ccd58
RL
177 return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
178 OSSL_STORE_INFO_PARAMS, 0);
179}
1427d33c 180
6e5ccd58
RL
181EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
182{
183 return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
0f113f3e 184}
db98bbc1 185
f864a939 186PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
0f113f3e
MC
187{
188 char pem_str[80];
f864a939
RL
189 IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters);
190
191 IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio);
192
193 legacy:
0f113f3e
MC
194 if (!x->ameth || !x->ameth->param_encode)
195 return 0;
db98bbc1 196
0f113f3e
MC
197 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
198 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
f864a939 199 pem_str, out, x, NULL, NULL, 0, 0, NULL);
0f113f3e 200}
e4263314 201
4b618848 202#ifndef OPENSSL_NO_STDIO
1531241c
MC
203EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
204 void *u, OPENSSL_CTX *libctx,
205 const char *propq)
0f113f3e
MC
206{
207 BIO *b;
208 EVP_PKEY *ret;
1241126a 209
0f113f3e 210 if ((b = BIO_new(BIO_s_file())) == NULL) {
1531241c 211 PEMerr(0, ERR_R_BUF_LIB);
26a7d938 212 return 0;
0f113f3e
MC
213 }
214 BIO_set_fp(b, fp, BIO_NOCLOSE);
1531241c 215 ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
0f113f3e 216 BIO_free(b);
26a7d938 217 return ret;
0f113f3e 218}
e4263314 219
1531241c
MC
220EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
221 void *u)
222{
223 return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
224}
225
de0799b0
RL
226int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc,
227 const unsigned char *kstr, int klen,
0f113f3e
MC
228 pem_password_cb *cb, void *u)
229{
230 BIO *b;
231 int ret;
e4263314 232
0f113f3e
MC
233 if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
234 PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
235 return 0;
236 }
237 ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
238 BIO_free(b);
239 return ret;
240}
e4263314 241
1241126a 242#endif
2ca873e8
DSH
243
244#ifndef OPENSSL_NO_DH
245
246/* Transparently read in PKCS#3 or X9.42 DH parameters */
247
248DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
0f113f3e 249{
0f113f3e 250 DH *ret = NULL;
1427d33c
RL
251 EVP_PKEY *pkey = NULL;
252 OSSL_STORE_CTX *ctx = NULL;
253 OSSL_STORE_INFO *info = NULL;
254 UI_METHOD *ui_method = NULL;
2ca873e8 255
1427d33c 256 if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
0f113f3e 257 return NULL;
2ca873e8 258
6725682d 259 if ((ctx = OSSL_STORE_attach(bp, "file", NULL, NULL, ui_method, u,
6ab6ecfd 260 NULL, NULL)) == NULL)
1427d33c
RL
261 goto err;
262
263 while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
264 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
265 pkey = OSSL_STORE_INFO_get0_PARAMS(info);
266 if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
267 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
268 ret = EVP_PKEY_get1_DH(pkey);
269 break;
270 }
271 }
272 OSSL_STORE_INFO_free(info);
6ab6ecfd 273 info = NULL;
1427d33c 274 }
2ca873e8 275
1427d33c
RL
276 if (ret != NULL && x != NULL)
277 *x = ret;
278
279 err:
6ab6ecfd 280 OSSL_STORE_close(ctx);
1427d33c
RL
281 UI_destroy_method(ui_method);
282 OSSL_STORE_INFO_free(info);
0f113f3e
MC
283 return ret;
284}
2ca873e8 285
0f113f3e 286# ifndef OPENSSL_NO_STDIO
2ca873e8 287DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
0f113f3e
MC
288{
289 BIO *b;
290 DH *ret;
2ca873e8 291
0f113f3e
MC
292 if ((b = BIO_new(BIO_s_file())) == NULL) {
293 PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
26a7d938 294 return 0;
0f113f3e
MC
295 }
296 BIO_set_fp(b, fp, BIO_NOCLOSE);
297 ret = PEM_read_bio_DHparams(b, x, cb, u);
298 BIO_free(b);
26a7d938 299 return ret;
0f113f3e
MC
300}
301# endif
2ca873e8
DSH
302
303#endif