]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pem/pem_pkey.c
Update copyright year
[thirdparty/openssl.git] / crypto / pem / pem_pkey.c
CommitLineData
62867571 1/*
a28d06f3 2 * Copyright 1995-2021 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
a1447076
RL
10/* We need to use some STORE deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
1241126a 13#include <stdio.h>
b39fc560 14#include "internal/cryptlib.h"
1241126a
DSH
15#include <openssl/buffer.h>
16#include <openssl/objects.h>
17#include <openssl/evp.h>
1241126a
DSH
18#include <openssl/x509.h>
19#include <openssl/pkcs12.h>
20#include <openssl/pem.h>
3c27208f
RS
21#include <openssl/engine.h>
22#include <openssl/dh.h>
1427d33c
RL
23#include <openssl/store.h>
24#include <openssl/ui.h>
25#include "crypto/store.h"
25f2138b
DMSP
26#include "crypto/asn1.h"
27#include "crypto/evp.h"
f864a939 28#include "pem_local.h"
1241126a 29
e4263314 30int pem_check_suffix(const char *pem_str, const char *suffix);
1241126a 31
6e5ccd58
RL
32static EVP_PKEY *pem_read_bio_key(BIO *bp, EVP_PKEY **x,
33 pem_password_cb *cb, void *u,
b4250010 34 OSSL_LIB_CTX *libctx, const char *propq,
6e5ccd58
RL
35 int expected_store_info_type,
36 int try_secure)
0f113f3e 37{
0f113f3e 38 EVP_PKEY *ret = NULL;
1427d33c
RL
39 OSSL_STORE_CTX *ctx = NULL;
40 OSSL_STORE_INFO *info = NULL;
6e5ccd58
RL
41 const UI_METHOD *ui_method = NULL;
42 UI_METHOD *allocated_ui_method = NULL;
1241126a 43
6e5ccd58
RL
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);
48 return NULL;
49 }
50
6e5ccd58 51 if (cb == NULL)
00eae3f9
RL
52 cb = PEM_def_callback;
53 ui_method = allocated_ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0);
6e5ccd58 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
a1447076
RL
61# ifndef OPENSSL_NO_DEPRECATED_3_0
62 if (try_secure) {
1427d33c
RL
63 int on = 1;
64 if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
0f113f3e 65 goto err;
1427d33c 66 }
a1447076 67# endif
1427d33c
RL
68#endif
69
8d8fee64
RL
70 if (!OSSL_STORE_expect(ctx, expected_store_info_type))
71 goto err;
72
6725682d
SL
73 while (!OSSL_STORE_eof(ctx)
74 && (info = OSSL_STORE_load(ctx)) != NULL) {
6e5ccd58
RL
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);
79 break;
80 case OSSL_STORE_INFO_PUBKEY:
81 ret = OSSL_STORE_INFO_get1_PUBKEY(info);
82 break;
83 case OSSL_STORE_INFO_PARAMS:
84 ret = OSSL_STORE_INFO_get1_PARAMS(info);
85 break;
86 }
0f113f3e 87 }
1427d33c 88 OSSL_STORE_INFO_free(info);
6ab6ecfd 89 info = NULL;
0f113f3e 90 }
1427d33c
RL
91
92 if (ret != NULL && x != NULL)
93 *x = ret;
94
0f113f3e 95 err:
6ab6ecfd 96 OSSL_STORE_close(ctx);
6e5ccd58 97 UI_destroy_method(allocated_ui_method);
1427d33c 98 OSSL_STORE_INFO_free(info);
26a7d938 99 return ret;
0f113f3e 100}
1241126a 101
6e5ccd58
RL
102EVP_PKEY *PEM_read_bio_PUBKEY_ex(BIO *bp, EVP_PKEY **x,
103 pem_password_cb *cb, void *u,
b4250010 104 OSSL_LIB_CTX *libctx, const char *propq)
6e5ccd58
RL
105{
106 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
107 OSSL_STORE_INFO_PUBKEY, 0);
108}
109
110EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
111 void *u)
112{
113 return PEM_read_bio_PUBKEY_ex(bp, x, cb, u, NULL, NULL);
114}
115
116#ifndef OPENSSL_NO_STDIO
117EVP_PKEY *PEM_read_PUBKEY_ex(FILE *fp, EVP_PKEY **x,
118 pem_password_cb *cb, void *u,
b4250010 119 OSSL_LIB_CTX *libctx, const char *propq)
6e5ccd58
RL
120{
121 BIO *b;
122 EVP_PKEY *ret;
123
124 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 125 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
6e5ccd58
RL
126 return 0;
127 }
128 BIO_set_fp(b, fp, BIO_NOCLOSE);
129 ret = PEM_read_bio_PUBKEY_ex(b, x, cb, u, libctx, propq);
130 BIO_free(b);
131 return ret;
132}
133
134EVP_PKEY *PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
135{
136 return PEM_read_PUBKEY_ex(fp, x, cb, u, NULL, NULL);
137}
138#endif
139
140EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x,
141 pem_password_cb *cb, void *u,
b4250010 142 OSSL_LIB_CTX *libctx, const char *propq)
6e5ccd58
RL
143{
144 return pem_read_bio_key(bp, x, cb, u, libctx, propq,
145 OSSL_STORE_INFO_PKEY, 1);
146}
147
1531241c
MC
148EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
149 void *u)
150{
151 return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL);
152}
153
9256e8a2 154PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
0f113f3e 155{
fe75766c 156 IMPLEMENT_PEM_provided_write_body_vars(pkey, PrivateKey, propq);
f864a939
RL
157
158 IMPLEMENT_PEM_provided_write_body_pass();
fe75766c 159 IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
f864a939
RL
160
161 legacy:
05dba815 162 if (x->ameth == NULL || x->ameth->priv_encode != NULL)
f864a939 163 return PEM_write_bio_PKCS8PrivateKey(out, x, enc,
de0799b0 164 (const char *)kstr, klen, cb, u);
f864a939 165 return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u);
05dba815 166}
e4263314 167
9256e8a2
RL
168PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio)
169{
170 return PEM_write_bio_PrivateKey_ex(out, x, enc, kstr, klen, cb, u,
171 NULL, NULL);
172}
173
4ce1025a
RL
174/*
175 * Note: there is no way to tell a provided pkey encoder to use "traditional"
176 * encoding. Therefore, if the pkey is provided, we try to take a copy
177 * TODO: when #legacy keys are gone, this function will not be possible any
178 * more and should be removed.
179 */
de0799b0 180int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x,
05dba815 181 const EVP_CIPHER *enc,
de0799b0 182 const unsigned char *kstr, int klen,
05dba815
DSH
183 pem_password_cb *cb, void *u)
184{
185 char pem_str[80];
4ce1025a
RL
186 EVP_PKEY *copy = NULL;
187 int ret;
188
189 if (evp_pkey_is_assigned(x)
190 && evp_pkey_is_provided(x)
191 && evp_pkey_copy_downgraded(&copy, x))
192 x = copy;
87d91d22
RL
193
194 if (x->ameth == NULL || x->ameth->old_priv_encode == NULL) {
195 ERR_raise(ERR_LIB_PEM, PEM_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
196 return 0;
197 }
0f113f3e 198 BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
4ce1025a
RL
199 ret = PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
200 pem_str, bp, x, enc, kstr, klen, cb, u);
201
202 EVP_PKEY_free(copy);
203 return ret;
0f113f3e 204}
e4263314 205
6e5ccd58 206EVP_PKEY *PEM_read_bio_Parameters_ex(BIO *bp, EVP_PKEY **x,
b4250010 207 OSSL_LIB_CTX *libctx, const char *propq)
0f113f3e 208{
6e5ccd58
RL
209 return pem_read_bio_key(bp, x, NULL, NULL, libctx, propq,
210 OSSL_STORE_INFO_PARAMS, 0);
211}
1427d33c 212
6e5ccd58
RL
213EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
214{
215 return PEM_read_bio_Parameters_ex(bp, x, NULL, NULL);
0f113f3e 216}
db98bbc1 217
f864a939 218PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio)
0f113f3e
MC
219{
220 char pem_str[80];
fe75766c 221 IMPLEMENT_PEM_provided_write_body_vars(pkey, Parameters, NULL);
f864a939 222
fe75766c 223 IMPLEMENT_PEM_provided_write_body_main(pkey, bio);
f864a939
RL
224
225 legacy:
0f113f3e
MC
226 if (!x->ameth || !x->ameth->param_encode)
227 return 0;
db98bbc1 228
0f113f3e
MC
229 BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
230 return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
f864a939 231 pem_str, out, x, NULL, NULL, 0, 0, NULL);
0f113f3e 232}
e4263314 233
4b618848 234#ifndef OPENSSL_NO_STDIO
1531241c 235EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
b4250010 236 void *u, OSSL_LIB_CTX *libctx,
1531241c 237 const char *propq)
0f113f3e
MC
238{
239 BIO *b;
240 EVP_PKEY *ret;
1241126a 241
0f113f3e 242 if ((b = BIO_new(BIO_s_file())) == NULL) {
9311d0c4 243 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
26a7d938 244 return 0;
0f113f3e
MC
245 }
246 BIO_set_fp(b, fp, BIO_NOCLOSE);
1531241c 247 ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq);
0f113f3e 248 BIO_free(b);
26a7d938 249 return ret;
0f113f3e 250}
e4263314 251
1531241c
MC
252EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
253 void *u)
254{
255 return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL);
256}
257
9256e8a2 258PEM_write_cb_ex_fnsig(PrivateKey, EVP_PKEY, FILE, write)
0f113f3e
MC
259{
260 BIO *b;
261 int ret;
e4263314 262
9256e8a2 263 if ((b = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
9311d0c4 264 ERR_raise(ERR_LIB_PEM, ERR_R_BUF_LIB);
0f113f3e
MC
265 return 0;
266 }
9256e8a2
RL
267 ret = PEM_write_bio_PrivateKey_ex(b, x, enc, kstr, klen, cb, u,
268 libctx, propq);
0f113f3e
MC
269 BIO_free(b);
270 return ret;
271}
e4263314 272
9256e8a2
RL
273PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, FILE, write)
274{
275 return PEM_write_PrivateKey_ex(out, x, enc, kstr, klen, cb, u, NULL, NULL);
276}
1241126a 277#endif