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