Allow strings in params to be of zero length
[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 "crypto/store.h"
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25
26 int pem_check_suffix(const char *pem_str, const char *suffix);
27
28 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
29                                   void *u)
30 {
31     EVP_PKEY *ret = NULL;
32     OSSL_STORE_CTX *ctx = NULL;
33     OSSL_STORE_INFO *info = NULL;
34     UI_METHOD *ui_method = NULL;
35
36     if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
37         return NULL;
38
39     if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
40         goto err;
41 #ifndef OPENSSL_NO_SECURE_HEAP
42     {
43         int on = 1;
44         if (!OSSL_STORE_ctrl(ctx, OSSL_STORE_C_USE_SECMEM, &on))
45             goto err;
46     }
47 #endif
48
49     while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
50         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
51             ret = OSSL_STORE_INFO_get1_PKEY(info);
52             break;
53         }
54         OSSL_STORE_INFO_free(info);
55     }
56
57     if (ret != NULL && x != NULL)
58         *x = ret;
59
60  err:
61     ossl_store_detach_pem_bio(ctx);
62     UI_destroy_method(ui_method);
63     OSSL_STORE_INFO_free(info);
64     return ret;
65 }
66
67 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
68                              unsigned char *kstr, int klen,
69                              pem_password_cb *cb, void *u)
70 {
71     if (x->ameth == NULL || x->ameth->priv_encode != NULL)
72         return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
73                                              (char *)kstr, klen, cb, u);
74     return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
75 }
76
77 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
78                                          const EVP_CIPHER *enc,
79                                          unsigned char *kstr, int klen,
80                                          pem_password_cb *cb, void *u)
81 {
82     char pem_str[80];
83     BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
84     return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
85                               pem_str, bp, x, enc, kstr, klen, cb, u);
86 }
87
88 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
89 {
90     EVP_PKEY *ret = NULL;
91     OSSL_STORE_CTX *ctx = NULL;
92     OSSL_STORE_INFO *info = NULL;
93
94     if ((ctx = ossl_store_attach_pem_bio(bp, UI_null(), NULL)) == NULL)
95         goto err;
96
97     while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
98         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
99             ret = OSSL_STORE_INFO_get1_PARAMS(info);
100             break;
101         }
102         OSSL_STORE_INFO_free(info);
103     }
104
105     if (ret != NULL && x != NULL)
106         *x = ret;
107
108  err:
109     ossl_store_detach_pem_bio(ctx);
110     OSSL_STORE_INFO_free(info);
111     return ret;
112 }
113
114 int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
115 {
116     char pem_str[80];
117     if (!x->ameth || !x->ameth->param_encode)
118         return 0;
119
120     BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
121     return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
122                               pem_str, bp, x, NULL, NULL, 0, 0, NULL);
123 }
124
125 #ifndef OPENSSL_NO_STDIO
126 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
127                               void *u)
128 {
129     BIO *b;
130     EVP_PKEY *ret;
131
132     if ((b = BIO_new(BIO_s_file())) == NULL) {
133         PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
134         return 0;
135     }
136     BIO_set_fp(b, fp, BIO_NOCLOSE);
137     ret = PEM_read_bio_PrivateKey(b, x, cb, u);
138     BIO_free(b);
139     return ret;
140 }
141
142 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
143                          unsigned char *kstr, int klen,
144                          pem_password_cb *cb, void *u)
145 {
146     BIO *b;
147     int ret;
148
149     if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
150         PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
151         return 0;
152     }
153     ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
154     BIO_free(b);
155     return ret;
156 }
157
158 #endif
159
160 #ifndef OPENSSL_NO_DH
161
162 /* Transparently read in PKCS#3 or X9.42 DH parameters */
163
164 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
165 {
166     DH *ret = NULL;
167     EVP_PKEY *pkey = NULL;
168     OSSL_STORE_CTX *ctx = NULL;
169     OSSL_STORE_INFO *info = NULL;
170     UI_METHOD *ui_method = NULL;
171
172     if ((ui_method = UI_UTIL_wrap_read_pem_callback(cb, 0)) == NULL)
173         return NULL;
174
175     if ((ctx = ossl_store_attach_pem_bio(bp, ui_method, u)) == NULL)
176         goto err;
177
178     while (!OSSL_STORE_eof(ctx) && (info = OSSL_STORE_load(ctx)) != NULL) {
179         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
180             pkey = OSSL_STORE_INFO_get0_PARAMS(info);
181             if (EVP_PKEY_id(pkey) == EVP_PKEY_DHX
182                 || EVP_PKEY_id(pkey) == EVP_PKEY_DH) {
183                 ret = EVP_PKEY_get1_DH(pkey);
184                 break;
185             }
186         }
187         OSSL_STORE_INFO_free(info);
188     }
189
190     if (ret != NULL && x != NULL)
191         *x = ret;
192
193  err:
194     ossl_store_detach_pem_bio(ctx);
195     UI_destroy_method(ui_method);
196     OSSL_STORE_INFO_free(info);
197     return ret;
198 }
199
200 # ifndef OPENSSL_NO_STDIO
201 DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
202 {
203     BIO *b;
204     DH *ret;
205
206     if ((b = BIO_new(BIO_s_file())) == NULL) {
207         PEMerr(PEM_F_PEM_READ_DHPARAMS, ERR_R_BUF_LIB);
208         return 0;
209     }
210     BIO_set_fp(b, fp, BIO_NOCLOSE);
211     ret = PEM_read_bio_DHparams(b, x, cb, u);
212     BIO_free(b);
213     return ret;
214 }
215 # endif
216
217 #endif