]>
Commit | Line | Data |
---|---|---|
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> | |
f864a939 | 22 | #include <openssl/serializer.h> |
1427d33c | 23 | #include "crypto/store.h" |
25f2138b DMSP |
24 | #include "crypto/asn1.h" |
25 | #include "crypto/evp.h" | |
f864a939 | 26 | #include "pem_local.h" |
1241126a | 27 | |
e4263314 | 28 | int pem_check_suffix(const char *pem_str, const char *suffix); |
1241126a | 29 | |
1531241c MC |
30 | EVP_PKEY *PEM_read_bio_PrivateKey_ex(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, |
31 | void *u, OPENSSL_CTX *libctx, | |
32 | const char *propq) | |
0f113f3e | 33 | { |
0f113f3e | 34 | EVP_PKEY *ret = NULL; |
1427d33c RL |
35 | OSSL_STORE_CTX *ctx = NULL; |
36 | OSSL_STORE_INFO *info = NULL; | |
37 | UI_METHOD *ui_method = NULL; | |
1241126a | 38 | |
1427d33c | 39 | if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL) |
0f113f3e | 40 | return NULL; |
1427d33c | 41 | |
6ab6ecfd RL |
42 | if ((ctx = OSSL_STORE_attach(bp, libctx, "file", propq, ui_method, u, |
43 | NULL, NULL)) == NULL) | |
1427d33c RL |
44 | goto err; |
45 | #ifndef OPENSSL_NO_SECURE_HEAP | |
46 | { | |
47 | int on = 1; | |
48 | if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on)) | |
0f113f3e | 49 | goto err; |
1427d33c RL |
50 | } |
51 | #endif | |
52 | ||
53 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { | |
54 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) { | |
55 | ret = OSSL_STORE_INFO_get1_PKEY(info); | |
56 | break; | |
0f113f3e | 57 | } |
1427d33c | 58 | OSSL_STORE_INFO_free(info); |
6ab6ecfd | 59 | info = NULL; |
0f113f3e | 60 | } |
1427d33c RL |
61 | |
62 | if (ret != NULL && x != NULL) | |
63 | *x = ret; | |
64 | ||
0f113f3e | 65 | err: |
6ab6ecfd | 66 | OSSL_STORE_close(ctx); |
1427d33c RL |
67 | UI_destroy_method(ui_method); |
68 | OSSL_STORE_INFO_free(info); | |
26a7d938 | 69 | return ret; |
0f113f3e | 70 | } |
1241126a | 71 | |
1531241c MC |
72 | EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, |
73 | void *u) | |
74 | { | |
75 | return PEM_read_bio_PrivateKey_ex(bp, x, cb, u, NULL, NULL); | |
76 | } | |
77 | ||
f864a939 | 78 | PEM_write_cb_fnsig(PrivateKey, EVP_PKEY, BIO, write_bio) |
0f113f3e | 79 | { |
f864a939 RL |
80 | IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, PrivateKey); |
81 | ||
82 | IMPLEMENT_PEM_provided_write_body_pass(); | |
83 | IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio); | |
84 | ||
85 | legacy: | |
05dba815 | 86 | if (x->ameth == NULL || x->ameth->priv_encode != NULL) |
f864a939 | 87 | return PEM_write_bio_PKCS8PrivateKey(out, x, enc, |
de0799b0 | 88 | (const char *)kstr, klen, cb, u); |
f864a939 | 89 | return PEM_write_bio_PrivateKey_traditional(out, x, enc, kstr, klen, cb, u); |
05dba815 | 90 | } |
e4263314 | 91 | |
de0799b0 | 92 | int PEM_write_bio_PrivateKey_traditional(BIO *bp, const EVP_PKEY *x, |
05dba815 | 93 | const EVP_CIPHER *enc, |
de0799b0 | 94 | const unsigned char *kstr, int klen, |
05dba815 DSH |
95 | pem_password_cb *cb, void *u) |
96 | { | |
97 | char pem_str[80]; | |
0f113f3e MC |
98 | BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str); |
99 | return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, | |
100 | pem_str, bp, x, enc, kstr, klen, cb, u); | |
101 | } | |
e4263314 | 102 | |
3e4585c8 | 103 | EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) |
0f113f3e | 104 | { |
0f113f3e | 105 | EVP_PKEY *ret = NULL; |
1427d33c RL |
106 | OSSL_STORE_CTX *ctx = NULL; |
107 | OSSL_STORE_INFO *info = NULL; | |
db98bbc1 | 108 | |
6ab6ecfd RL |
109 | if ((ctx = OSSL_STORE_attach(bp, NULL, "file", NULL, UI_null(), NULL, |
110 | NULL, NULL)) == NULL) | |
1427d33c | 111 | goto err; |
db98bbc1 | 112 | |
1427d33c RL |
113 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { |
114 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { | |
115 | ret = OSSL_STORE_INFO_get1_PARAMS(info); | |
116 | break; | |
0f113f3e | 117 | } |
1427d33c | 118 | OSSL_STORE_INFO_free(info); |
6ab6ecfd | 119 | info = NULL; |
0f113f3e | 120 | } |
1427d33c RL |
121 | |
122 | if (ret != NULL && x != NULL) | |
123 | *x = ret; | |
124 | ||
0f113f3e | 125 | err: |
6ab6ecfd | 126 | OSSL_STORE_close(ctx); |
1427d33c | 127 | OSSL_STORE_INFO_free(info); |
26a7d938 | 128 | return ret; |
0f113f3e | 129 | } |
db98bbc1 | 130 | |
f864a939 | 131 | PEM_write_fnsig(Parameters, EVP_PKEY, BIO, write_bio) |
0f113f3e MC |
132 | { |
133 | char pem_str[80]; | |
f864a939 RL |
134 | IMPLEMENT_PEM_provided_write_body_vars(EVP_PKEY, Parameters); |
135 | ||
136 | IMPLEMENT_PEM_provided_write_body_main(EVP_PKEY, bio); | |
137 | ||
138 | legacy: | |
0f113f3e MC |
139 | if (!x->ameth || !x->ameth->param_encode) |
140 | return 0; | |
db98bbc1 | 141 | |
0f113f3e MC |
142 | BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str); |
143 | return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode, | |
f864a939 | 144 | pem_str, out, x, NULL, NULL, 0, 0, NULL); |
0f113f3e | 145 | } |
e4263314 | 146 | |
4b618848 | 147 | #ifndef OPENSSL_NO_STDIO |
1531241c MC |
148 | EVP_PKEY *PEM_read_PrivateKey_ex(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, |
149 | void *u, OPENSSL_CTX *libctx, | |
150 | const char *propq) | |
0f113f3e MC |
151 | { |
152 | BIO *b; | |
153 | EVP_PKEY *ret; | |
1241126a | 154 | |
0f113f3e | 155 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
1531241c | 156 | PEMerr(0, ERR_R_BUF_LIB); |
26a7d938 | 157 | return 0; |
0f113f3e MC |
158 | } |
159 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
1531241c | 160 | ret = PEM_read_bio_PrivateKey_ex(b, x, cb, u, libctx, propq); |
0f113f3e | 161 | BIO_free(b); |
26a7d938 | 162 | return ret; |
0f113f3e | 163 | } |
e4263314 | 164 | |
1531241c MC |
165 | EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, |
166 | void *u) | |
167 | { | |
168 | return PEM_read_PrivateKey_ex(fp, x, cb, u, NULL, NULL); | |
169 | } | |
170 | ||
de0799b0 RL |
171 | int PEM_write_PrivateKey(FILE *fp, const EVP_PKEY *x, const EVP_CIPHER *enc, |
172 | const unsigned char *kstr, int klen, | |
0f113f3e MC |
173 | pem_password_cb *cb, void *u) |
174 | { | |
175 | BIO *b; | |
176 | int ret; | |
e4263314 | 177 | |
0f113f3e MC |
178 | if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { |
179 | PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB); | |
180 | return 0; | |
181 | } | |
182 | ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u); | |
183 | BIO_free(b); | |
184 | return ret; | |
185 | } | |
e4263314 | 186 | |
1241126a | 187 | #endif |
2ca873e8 DSH |
188 | |
189 | #ifndef OPENSSL_NO_DH | |
190 | ||
191 | /* Transparently read in PKCS#3 or X9.42 DH parameters */ | |
192 | ||
193 | DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u) | |
0f113f3e | 194 | { |
0f113f3e | 195 | DH *ret = NULL; |
1427d33c RL |
196 | EVP_PKEY *pkey = NULL; |
197 | OSSL_STORE_CTX *ctx = NULL; | |
198 | OSSL_STORE_INFO *info = NULL; | |
199 | UI_METHOD *ui_method = NULL; | |
2ca873e8 | 200 | |
1427d33c | 201 | if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL) |
0f113f3e | 202 | return NULL; |
2ca873e8 | 203 | |
6ab6ecfd RL |
204 | if ((ctx = OSSL_STORE_attach(bp, NULL, "file", NULL, ui_method, u, |
205 | NULL, NULL)) == NULL) | |
1427d33c RL |
206 | goto err; |
207 | ||
208 | while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) { | |
209 | if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) { | |
210 | pkey = OSSL_STORE_INFO_get0_PARAMS(info); | |
211 | if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX | |
212 | || EVP_PKEY_id(pkey) == EVP_PKEY_DH) { | |
213 | ret = EVP_PKEY_get1_DH(pkey); | |
214 | break; | |
215 | } | |
216 | } | |
217 | OSSL_STORE_INFO_free(info); | |
6ab6ecfd | 218 | info = NULL; |
1427d33c | 219 | } |
2ca873e8 | 220 | |
1427d33c RL |
221 | if (ret != NULL && x != NULL) |
222 | *x = ret; | |
223 | ||
224 | err: | |
6ab6ecfd | 225 | OSSL_STORE_close(ctx); |
1427d33c RL |
226 | UI_destroy_method(ui_method); |
227 | OSSL_STORE_INFO_free(info); | |
0f113f3e MC |
228 | return ret; |
229 | } | |
2ca873e8 | 230 | |
0f113f3e | 231 | # ifndef OPENSSL_NO_STDIO |
2ca873e8 | 232 | DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u) |
0f113f3e MC |
233 | { |
234 | BIO *b; | |
235 | DH *ret; | |
2ca873e8 | 236 | |
0f113f3e MC |
237 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
238 | PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB); | |
26a7d938 | 239 | return 0; |
0f113f3e MC |
240 | } |
241 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
242 | ret = PEM_read_bio_DHparams(b, x, cb, u); | |
243 | BIO_free(b); | |
26a7d938 | 244 | return ret; |
0f113f3e MC |
245 | } |
246 | # endif | |
2ca873e8 DSH |
247 | |
248 | #endif |