Useless header include of openssl/rand.h
[openssl.git] / crypto / pem / pem_pkey.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
21 #include "internal/evp_int.h"
22
23 int pem_check_suffix(const char *pem_str, const char *suffix);
24
25 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
26                                   void *u)
27 {
28     char *nm = NULL;
29     const unsigned char *p = NULL;
30     unsigned char *data = NULL;
31     long len;
32     int slen;
33     EVP_PKEY *ret = NULL;
34
35     if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
36         return NULL;
37     p = data;
38
39     if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
40         PKCS8_PRIV_KEY_INFO *p8inf;
41         p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
42         if (!p8inf)
43             goto p8err;
44         ret = EVP_PKCS82PKEY(p8inf);
45         if (x) {
46             EVP_PKEY_free((EVP_PKEY *)*x);
47             *x = ret;
48         }
49         PKCS8_PRIV_KEY_INFO_free(p8inf);
50     } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
51         PKCS8_PRIV_KEY_INFO *p8inf;
52         X509_SIG *p8;
53         int klen;
54         char psbuf[PEM_BUFSIZE];
55         p8 = d2i_X509_SIG(NULL, &p, len);
56         if (!p8)
57             goto p8err;
58         if (cb)
59             klen = cb(psbuf, PEM_BUFSIZE, 0, u);
60         else
61             klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u);
62         if (klen <= 0) {
63             PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, PEM_R_BAD_PASSWORD_READ);
64             X509_SIG_free(p8);
65             goto err;
66         }
67         p8inf = PKCS8_decrypt(p8, psbuf, klen);
68         X509_SIG_free(p8);
69         if (!p8inf)
70             goto p8err;
71         ret = EVP_PKCS82PKEY(p8inf);
72         if (x) {
73             EVP_PKEY_free((EVP_PKEY *)*x);
74             *x = ret;
75         }
76         PKCS8_PRIV_KEY_INFO_free(p8inf);
77     } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) {
78         const EVP_PKEY_ASN1_METHOD *ameth;
79         ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
80         if (!ameth || !ameth->old_priv_decode)
81             goto p8err;
82         ret = d2i_PrivateKey(ameth->pkey_id, x, &p, len);
83     }
84  p8err:
85     if (ret == NULL)
86         PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB);
87  err:
88     OPENSSL_free(nm);
89     OPENSSL_clear_free(data, len);
90     return (ret);
91 }
92
93 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
94                              unsigned char *kstr, int klen,
95                              pem_password_cb *cb, void *u)
96 {
97     if (x->ameth == NULL || x->ameth->priv_encode != NULL)
98         return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
99                                              (char *)kstr, klen, cb, u);
100     return PEM_write_bio_PrivateKey_traditional(bp, x, enc, kstr, klen, cb, u);
101 }
102
103 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
104                                          const EVP_CIPHER *enc,
105                                          unsigned char *kstr, int klen,
106                                          pem_password_cb *cb, void *u)
107 {
108     char pem_str[80];
109     BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
110     return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
111                               pem_str, bp, x, enc, kstr, klen, cb, u);
112 }
113
114 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
115 {
116     char *nm = NULL;
117     const unsigned char *p = NULL;
118     unsigned char *data = NULL;
119     long len;
120     int slen;
121     EVP_PKEY *ret = NULL;
122
123     if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
124                             bp, 0, NULL))
125         return NULL;
126     p = data;
127
128     if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) {
129         ret = EVP_PKEY_new();
130         if (ret == NULL)
131             goto err;
132         if (!EVP_PKEY_set_type_str(ret, nm, slen)
133             || !ret->ameth->param_decode
134             || !ret->ameth->param_decode(ret, &p, len)) {
135             EVP_PKEY_free(ret);
136             ret = NULL;
137             goto err;
138         }
139         if (x) {
140             EVP_PKEY_free((EVP_PKEY *)*x);
141             *x = ret;
142         }
143     }
144  err:
145     if (ret == NULL)
146         PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB);
147     OPENSSL_free(nm);
148     OPENSSL_free(data);
149     return (ret);
150 }
151
152 int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
153 {
154     char pem_str[80];
155     if (!x->ameth || !x->ameth->param_encode)
156         return 0;
157
158     BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
159     return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode,
160                               pem_str, bp, x, NULL, NULL, 0, 0, NULL);
161 }
162
163 #ifndef OPENSSL_NO_STDIO
164 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
165                               void *u)
166 {
167     BIO *b;
168     EVP_PKEY *ret;
169
170     if ((b = BIO_new(BIO_s_file())) == NULL) {
171         PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB);
172         return (0);
173     }
174     BIO_set_fp(b, fp, BIO_NOCLOSE);
175     ret = PEM_read_bio_PrivateKey(b, x, cb, u);
176     BIO_free(b);
177     return (ret);
178 }
179
180 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
181                          unsigned char *kstr, int klen,
182                          pem_password_cb *cb, void *u)
183 {
184     BIO *b;
185     int ret;
186
187     if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
188         PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB);
189         return 0;
190     }
191     ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
192     BIO_free(b);
193     return ret;
194 }
195
196 #endif
197
198 #ifndef OPENSSL_NO_DH
199
200 /* Transparently read in PKCS#3 or X9.42 DH parameters */
201
202 DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
203 {
204     char *nm = NULL;
205     const unsigned char *p = NULL;
206     unsigned char *data = NULL;
207     long len;
208     DH *ret = NULL;
209
210     if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS, bp, cb, u))
211         return NULL;
212     p = data;
213
214     if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0)
215         ret = d2i_DHxparams(x, &p, len);
216     else
217         ret = d2i_DHparams(x, &p, len);
218
219     if (ret == NULL)
220         PEMerr(PEM_F_PEM_READ_BIO_DHPARAMS, ERR_R_ASN1_LIB);
221     OPENSSL_free(nm);
222     OPENSSL_free(data);
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