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