Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/pem/pem_pkey.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/pem.h>
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <openssl/dh.h>
10 #include <openssl/err.h>
11 #include <openssl/evp.h>
12 #include <openssl/mem.h>
13 #include <openssl/obj.h>
14 #include <openssl/pkcs8.h>
15 #include <openssl/rand.h>
16 #include <openssl/x509.h>
17 #include "../fipsmodule/evp/internal.h"
18
19 EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb,
20 void *u) {
21 char *nm = NULL;
22 const unsigned char *p = NULL;
23 unsigned char *data = NULL;
24 long len;
25 EVP_PKEY *ret = NULL;
26
27 if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u)) {
28 return NULL;
29 }
30 p = data;
31
32 if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) {
33 PKCS8_PRIV_KEY_INFO *p8inf;
34 p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
35 if (!p8inf) {
36 goto p8err;
37 }
38 ret = EVP_PKCS82PKEY(p8inf);
39 if (x) {
40 if (*x) {
41 EVP_PKEY_free((EVP_PKEY *)*x);
42 }
43 *x = ret;
44 }
45 PKCS8_PRIV_KEY_INFO_free(p8inf);
46 } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) {
47 PKCS8_PRIV_KEY_INFO *p8inf;
48 X509_SIG *p8;
49 char psbuf[PEM_BUFSIZE];
50 p8 = d2i_X509_SIG(NULL, &p, len);
51 if (!p8) {
52 goto p8err;
53 }
54
55 if (!cb) {
56 cb = PEM_def_callback;
57 }
58 int pass_len = cb(psbuf, PEM_BUFSIZE, 0, u);
59 if (pass_len < 0) {
60 OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ);
61 X509_SIG_free(p8);
62 goto err;
63 }
64 p8inf = PKCS8_decrypt(p8, psbuf, pass_len);
65 X509_SIG_free(p8);
66 OPENSSL_cleanse(psbuf, pass_len);
67 if (!p8inf) {
68 goto p8err;
69 }
70 ret = EVP_PKCS82PKEY(p8inf);
71 if (x) {
72 if (*x) {
73 EVP_PKEY_free((EVP_PKEY *)*x);
74 }
75 *x = ret;
76 }
77 PKCS8_PRIV_KEY_INFO_free(p8inf);
78 } else if (strcmp(nm, PEM_STRING_RSA) == 0) {
79 // TODO(davidben): d2i_PrivateKey parses PKCS#8 along with the
80 // standalone format. This and the cases below probably should not
81 // accept PKCS#8.
82 ret = d2i_PrivateKey(EVP_PKEY_RSA, x, &p, len);
83 } else if (strcmp(nm, PEM_STRING_EC) == 0) {
84 ret = d2i_PrivateKey(EVP_PKEY_EC, x, &p, len);
85 } else if (strcmp(nm, PEM_STRING_DSA) == 0) {
86 ret = d2i_PrivateKey(EVP_PKEY_DSA, x, &p, len);
87 }
88 p8err:
89 if (ret == NULL) {
90 OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
91 }
92
93 err:
94 OPENSSL_free(nm);
95 OPENSSL_free(data);
96 return ret;
97 }
98
99 int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
100 const unsigned char *pass, int pass_len,
101 pem_password_cb *cb, void *u) {
102 return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, (const char *)pass, pass_len,
103 cb, u);
104 }
105
106 EVP_PKEY *PEM_read_bio_Parameters(BIO *bio, EVP_PKEY **pkey) {
107 if (bio == NULL) {
108 OPENSSL_PUT_ERROR(PEM, ERR_R_PASSED_NULL_PARAMETER);
109 return NULL;
110 }
111
112 char *nm = NULL;
113 unsigned char *data = NULL;
114 long len;
115 if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS, bio, 0,
116 NULL)) {
117 return NULL;
118 }
119 const unsigned char *data_const = data;
120
121 // Implementing the ASN1 logic here allows us to decouple the pem logic for
122 // |EVP_PKEY|. These correspond to the historical |param_decode|
123 // |EVP_PKEY_ASN1_METHOD| hooks in OpenSSL.
124 EVP_PKEY *ret = EVP_PKEY_new();
125 if (ret == NULL) {
126 goto err;
127 }
128 if (strcmp(nm, PEM_STRING_ECPARAMETERS) == 0) {
129 EC_KEY *ec_key = d2i_ECParameters(NULL, &data_const, len);
130 if (ec_key == NULL || !EVP_PKEY_assign_EC_KEY(ret, ec_key)) {
131 OPENSSL_PUT_ERROR(EVP, ERR_R_EC_LIB);
132 EC_KEY_free(ec_key);
133 goto err;
134 }
135 } else if (strcmp(nm, PEM_STRING_DSAPARAMS) == 0) {
136 DSA *dsa = d2i_DSAparams(NULL, &data_const, len);
137 if (dsa == NULL || !EVP_PKEY_assign_DSA(ret, dsa)) {
138 OPENSSL_PUT_ERROR(EVP, ERR_R_DSA_LIB);
139 DSA_free(dsa);
140 goto err;
141 }
142 } else if (strcmp(nm, PEM_STRING_DHPARAMS) == 0) {
143 DH *dh = d2i_DHparams(NULL, &data_const, len);
144 if (dh == NULL || !EVP_PKEY_assign_DH(ret, dh)) {
145 OPENSSL_PUT_ERROR(EVP, ERR_R_DH_LIB);
146 DH_free(dh);
147 goto err;
148 }
149 } else {
150 goto err;
151 }
152
153 if (pkey != NULL) {
154 EVP_PKEY_free(*pkey);
155 *pkey = ret;
156 }
157
158 OPENSSL_free(nm);
159 OPENSSL_free(data);
160 return ret;
161
162 err:
163 EVP_PKEY_free(ret);
164 OPENSSL_free(nm);
165 OPENSSL_free(data);
166 return NULL;
167 }
168
169 static int i2d_ECParameters_void(const void *key, uint8_t **out) {
170 return i2d_ECParameters((EC_KEY *)key, out);
171 }
172
173 static int i2d_DSAparams_void(const void *key, uint8_t **out) {
174 return i2d_DSAparams((DSA *)key, out);
175 }
176
177 static int i2d_DHparams_void(const void *key, uint8_t **out) {
178 return i2d_DHparams((DH *)key, out);
179 }
180
181 int PEM_write_bio_Parameters(BIO *bio, EVP_PKEY *pkey) {
182 if (bio == NULL || pkey == NULL) {
183 OPENSSL_PUT_ERROR(PEM, ERR_R_PASSED_NULL_PARAMETER);
184 return 0;
185 }
186
187 // Implementing the ASN1 logic here allows us to decouple the pem logic for
188 // |EVP_PKEY|. These correspond to the historical |param_encode|
189 // |EVP_PKEY_ASN1_METHOD| hooks in OpenSSL.
190 char pem_str[80];
191 switch (pkey->type) {
192 case EVP_PKEY_EC:
193 BIO_snprintf(pem_str, 80, PEM_STRING_ECPARAMETERS);
194 return PEM_ASN1_write_bio(i2d_ECParameters_void, pem_str, bio,
195 pkey->pkey.ec, NULL, NULL, 0, 0, NULL);
196 case EVP_PKEY_DSA:
197 BIO_snprintf(pem_str, 80, PEM_STRING_DSAPARAMS);
198 return PEM_ASN1_write_bio(i2d_DSAparams_void, pem_str, bio,
199 pkey->pkey.dsa, NULL, NULL, 0, 0, NULL);
200 case EVP_PKEY_DH:
201 BIO_snprintf(pem_str, 80, PEM_STRING_DHPARAMS);
202 return PEM_ASN1_write_bio(i2d_DHparams_void, pem_str, bio, pkey->pkey.dh,
203 NULL, NULL, 0, 0, NULL);
204 default:
205 return 0;
206 }
207 }
208
209 static int i2d_PrivateKey_void(const void *key, uint8_t **out) {
210 return i2d_PrivateKey((const EVP_PKEY *)key, out);
211 }
212
213 int PEM_write_bio_PrivateKey_traditional(BIO *bp, EVP_PKEY *x,
214 const EVP_CIPHER *enc,
215 unsigned char *kstr, int klen,
216 pem_password_cb *cb, void *u) {
217 if (bp == NULL || x == NULL || x->ameth == NULL ||
218 x->ameth->pem_str == NULL) {
219 OPENSSL_PUT_ERROR(PEM, ERR_R_PASSED_NULL_PARAMETER);
220 return 0;
221 }
222 char pem_str[80];
223 BIO_snprintf(pem_str, sizeof(pem_str), "%s PRIVATE KEY", x->ameth->pem_str);
224 return PEM_ASN1_write_bio(i2d_PrivateKey_void, pem_str, bp, x, enc, kstr,
225 klen, cb, u);
226 }
227
228 EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb,
229 void *u) {
230 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
231 if (b == NULL) {
232 OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
233 return NULL;
234 }
235 EVP_PKEY *ret = PEM_read_bio_PrivateKey(b, x, cb, u);
236 BIO_free(b);
237 return ret;
238 }
239
240 int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
241 const unsigned char *pass, int pass_len,
242 pem_password_cb *cb, void *u) {
243 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
244 if (b == NULL) {
245 OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
246 return 0;
247 }
248 int ret = PEM_write_bio_PrivateKey(b, x, enc, pass, pass_len, cb, u);
249 BIO_free(b);
250 return ret;
251 }