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