diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/pem/pem_pkey.c | 207 |
1 files changed, 108 insertions, 99 deletions
diff --git a/src/lib/libcrypto/pem/pem_pkey.c b/src/lib/libcrypto/pem/pem_pkey.c index 5274447b24..9aaff6e514 100644 --- a/src/lib/libcrypto/pem/pem_pkey.c +++ b/src/lib/libcrypto/pem/pem_pkey.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -72,173 +72,182 @@ | |||
72 | 72 | ||
73 | int pem_check_suffix(const char *pem_str, const char *suffix); | 73 | int pem_check_suffix(const char *pem_str, const char *suffix); |
74 | 74 | ||
75 | EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) | 75 | EVP_PKEY * |
76 | { | 76 | PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u) |
77 | char *nm=NULL; | 77 | { |
78 | const unsigned char *p=NULL; | 78 | char *nm = NULL; |
79 | unsigned char *data=NULL; | 79 | const unsigned char *p = NULL; |
80 | unsigned char *data = NULL; | ||
80 | long len; | 81 | long len; |
81 | int slen; | 82 | int slen; |
82 | EVP_PKEY *ret=NULL; | 83 | EVP_PKEY *ret = NULL; |
83 | 84 | ||
84 | if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u)) | 85 | if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, |
86 | bp, cb, u)) | ||
85 | return NULL; | 87 | return NULL; |
86 | p = data; | 88 | p = data; |
87 | 89 | ||
88 | if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) { | 90 | if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) { |
89 | PKCS8_PRIV_KEY_INFO *p8inf; | 91 | PKCS8_PRIV_KEY_INFO *p8inf; |
90 | p8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len); | 92 | p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len); |
91 | if(!p8inf) goto p8err; | 93 | if (!p8inf) |
94 | goto p8err; | ||
92 | ret = EVP_PKCS82PKEY(p8inf); | 95 | ret = EVP_PKCS82PKEY(p8inf); |
93 | if(x) { | 96 | if (x) { |
94 | if(*x) EVP_PKEY_free((EVP_PKEY *)*x); | 97 | if (*x) |
98 | EVP_PKEY_free((EVP_PKEY *)*x); | ||
95 | *x = ret; | 99 | *x = ret; |
96 | } | 100 | } |
97 | PKCS8_PRIV_KEY_INFO_free(p8inf); | 101 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
98 | } else if (strcmp(nm,PEM_STRING_PKCS8) == 0) { | 102 | } else if (strcmp(nm, PEM_STRING_PKCS8) == 0) { |
99 | PKCS8_PRIV_KEY_INFO *p8inf; | 103 | PKCS8_PRIV_KEY_INFO *p8inf; |
100 | X509_SIG *p8; | 104 | X509_SIG *p8; |
101 | int klen; | 105 | int klen; |
102 | char psbuf[PEM_BUFSIZE]; | 106 | char psbuf[PEM_BUFSIZE]; |
103 | p8 = d2i_X509_SIG(NULL, &p, len); | 107 | p8 = d2i_X509_SIG(NULL, &p, len); |
104 | if(!p8) goto p8err; | 108 | if (!p8) |
105 | if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u); | 109 | goto p8err; |
106 | else klen=PEM_def_callback(psbuf,PEM_BUFSIZE,0,u); | 110 | if (cb) |
111 | klen = cb(psbuf, PEM_BUFSIZE, 0, u); | ||
112 | else | ||
113 | klen = PEM_def_callback(psbuf, PEM_BUFSIZE, 0, u); | ||
107 | if (klen <= 0) { | 114 | if (klen <= 0) { |
108 | PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, | 115 | PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, |
109 | PEM_R_BAD_PASSWORD_READ); | 116 | PEM_R_BAD_PASSWORD_READ); |
110 | X509_SIG_free(p8); | 117 | X509_SIG_free(p8); |
111 | goto err; | 118 | goto err; |
112 | } | 119 | } |
113 | p8inf = PKCS8_decrypt(p8, psbuf, klen); | 120 | p8inf = PKCS8_decrypt(p8, psbuf, klen); |
114 | X509_SIG_free(p8); | 121 | X509_SIG_free(p8); |
115 | if(!p8inf) goto p8err; | 122 | if (!p8inf) |
123 | goto p8err; | ||
116 | ret = EVP_PKCS82PKEY(p8inf); | 124 | ret = EVP_PKCS82PKEY(p8inf); |
117 | if(x) { | 125 | if (x) { |
118 | if(*x) EVP_PKEY_free((EVP_PKEY *)*x); | 126 | if (*x) |
127 | EVP_PKEY_free((EVP_PKEY *)*x); | ||
119 | *x = ret; | 128 | *x = ret; |
120 | } | 129 | } |
121 | PKCS8_PRIV_KEY_INFO_free(p8inf); | 130 | PKCS8_PRIV_KEY_INFO_free(p8inf); |
122 | } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) | 131 | } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0) { |
123 | { | ||
124 | const EVP_PKEY_ASN1_METHOD *ameth; | 132 | const EVP_PKEY_ASN1_METHOD *ameth; |
125 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); | 133 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); |
126 | if (!ameth || !ameth->old_priv_decode) | 134 | if (!ameth || !ameth->old_priv_decode) |
127 | goto p8err; | 135 | goto p8err; |
128 | ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len); | 136 | ret = d2i_PrivateKey(ameth->pkey_id, x,&p, len); |
129 | } | 137 | } |
138 | |||
130 | p8err: | 139 | p8err: |
131 | if (ret == NULL) | 140 | if (ret == NULL) |
132 | PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,ERR_R_ASN1_LIB); | 141 | PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY, ERR_R_ASN1_LIB); |
133 | err: | 142 | err: |
134 | free(nm); | 143 | free(nm); |
135 | OPENSSL_cleanse(data, len); | 144 | OPENSSL_cleanse(data, len); |
136 | free(data); | 145 | free(data); |
137 | return(ret); | 146 | return (ret); |
138 | } | 147 | } |
139 | 148 | ||
140 | int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, | 149 | int |
141 | unsigned char *kstr, int klen, | 150 | PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc, |
142 | pem_password_cb *cb, void *u) | 151 | unsigned char *kstr, int klen, pem_password_cb *cb, void *u) |
143 | { | 152 | { |
144 | char pem_str[80]; | 153 | char pem_str[80]; |
154 | |||
145 | if (!x->ameth || x->ameth->priv_encode) | 155 | if (!x->ameth || x->ameth->priv_encode) |
146 | return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, | 156 | return PEM_write_bio_PKCS8PrivateKey(bp, x, enc, |
147 | (char *)kstr, klen, | 157 | (char *)kstr, klen, cb, u); |
148 | cb, u); | ||
149 | 158 | ||
150 | (void) snprintf(pem_str, sizeof(pem_str), "%s PRIVATE KEY", | 159 | (void) snprintf(pem_str, sizeof(pem_str), "%s PRIVATE KEY", |
151 | x->ameth->pem_str); | 160 | x->ameth->pem_str); |
152 | return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, | 161 | return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey, |
153 | pem_str,bp,x,enc,kstr,klen,cb,u); | 162 | pem_str, bp, x,enc, kstr, klen, cb, u); |
154 | } | 163 | } |
155 | 164 | ||
156 | EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) | 165 | EVP_PKEY * |
157 | { | 166 | PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x) |
158 | char *nm=NULL; | 167 | { |
159 | const unsigned char *p=NULL; | 168 | char *nm = NULL; |
160 | unsigned char *data=NULL; | 169 | const unsigned char *p = NULL; |
170 | unsigned char *data = NULL; | ||
161 | long len; | 171 | long len; |
162 | int slen; | 172 | int slen; |
163 | EVP_PKEY *ret=NULL; | 173 | EVP_PKEY *ret = NULL; |
164 | 174 | ||
165 | if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS, | 175 | if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS, |
166 | bp, 0, NULL)) | 176 | bp, 0, NULL)) |
167 | return NULL; | 177 | return NULL; |
168 | p = data; | 178 | p = data; |
169 | 179 | ||
170 | if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) | 180 | if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0) { |
171 | { | ||
172 | ret = EVP_PKEY_new(); | 181 | ret = EVP_PKEY_new(); |
173 | if (!ret) | 182 | if (!ret) |
174 | goto err; | 183 | goto err; |
175 | if (!EVP_PKEY_set_type_str(ret, nm, slen) | 184 | if (!EVP_PKEY_set_type_str(ret, nm, slen) || |
176 | || !ret->ameth->param_decode | 185 | !ret->ameth->param_decode || |
177 | || !ret->ameth->param_decode(ret, &p, len)) | 186 | !ret->ameth->param_decode(ret, &p, len)) { |
178 | { | ||
179 | EVP_PKEY_free(ret); | 187 | EVP_PKEY_free(ret); |
180 | ret = NULL; | 188 | ret = NULL; |
181 | goto err; | 189 | goto err; |
182 | } | 190 | } |
183 | if(x) | 191 | if (x) { |
184 | { | 192 | if (*x) |
185 | if(*x) EVP_PKEY_free((EVP_PKEY *)*x); | 193 | EVP_PKEY_free((EVP_PKEY *)*x); |
186 | *x = ret; | 194 | *x = ret; |
187 | } | ||
188 | } | 195 | } |
196 | } | ||
197 | |||
189 | err: | 198 | err: |
190 | if (ret == NULL) | 199 | if (ret == NULL) |
191 | PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS,ERR_R_ASN1_LIB); | 200 | PEMerr(PEM_F_PEM_READ_BIO_PARAMETERS, ERR_R_ASN1_LIB); |
192 | free(nm); | 201 | free(nm); |
193 | free(data); | 202 | free(data); |
194 | return(ret); | 203 | return (ret); |
195 | } | 204 | } |
196 | 205 | ||
197 | int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x) | 206 | int |
198 | { | 207 | PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x) |
208 | { | ||
199 | char pem_str[80]; | 209 | char pem_str[80]; |
210 | |||
200 | if (!x->ameth || !x->ameth->param_encode) | 211 | if (!x->ameth || !x->ameth->param_encode) |
201 | return 0; | 212 | return 0; |
202 | 213 | ||
203 | (void) snprintf(pem_str, sizeof(pem_str), "%s PARAMETERS", | 214 | (void) snprintf(pem_str, sizeof(pem_str), "%s PARAMETERS", |
204 | x->ameth->pem_str); | 215 | x->ameth->pem_str); |
205 | return PEM_ASN1_write_bio( | 216 | return PEM_ASN1_write_bio((i2d_of_void *)x->ameth->param_encode, |
206 | (i2d_of_void *)x->ameth->param_encode, | 217 | pem_str, bp, x,NULL, NULL, 0,0, NULL); |
207 | pem_str,bp,x,NULL,NULL,0,0,NULL); | 218 | } |
208 | } | ||
209 | 219 | ||
210 | #ifndef OPENSSL_NO_FP_API | 220 | #ifndef OPENSSL_NO_FP_API |
211 | EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u) | 221 | EVP_PKEY * |
212 | { | 222 | PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u) |
213 | BIO *b; | 223 | { |
214 | EVP_PKEY *ret; | 224 | BIO *b; |
215 | 225 | EVP_PKEY *ret; | |
216 | if ((b=BIO_new(BIO_s_file())) == NULL) | 226 | |
217 | { | 227 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
218 | PEMerr(PEM_F_PEM_READ_PRIVATEKEY,ERR_R_BUF_LIB); | 228 | PEMerr(PEM_F_PEM_READ_PRIVATEKEY, ERR_R_BUF_LIB); |
219 | return(0); | 229 | return (0); |
220 | } | ||
221 | BIO_set_fp(b,fp,BIO_NOCLOSE); | ||
222 | ret=PEM_read_bio_PrivateKey(b,x,cb,u); | ||
223 | BIO_free(b); | ||
224 | return(ret); | ||
225 | } | 230 | } |
226 | 231 | BIO_set_fp(b, fp, BIO_NOCLOSE); | |
227 | int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, | 232 | ret = PEM_read_bio_PrivateKey(b, x,cb, u); |
228 | unsigned char *kstr, int klen, | 233 | BIO_free(b); |
229 | pem_password_cb *cb, void *u) | 234 | return (ret); |
230 | { | 235 | } |
231 | BIO *b; | 236 | |
232 | int ret; | 237 | int |
233 | 238 | PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc, | |
234 | if ((b=BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) | 239 | unsigned char *kstr, int klen, pem_password_cb *cb, void *u) |
235 | { | 240 | { |
236 | PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY,ERR_R_BUF_LIB); | 241 | BIO *b; |
237 | return 0; | 242 | int ret; |
238 | } | 243 | |
239 | ret=PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u); | 244 | if ((b = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) { |
240 | BIO_free(b); | 245 | PEMerr(PEM_F_PEM_WRITE_PRIVATEKEY, ERR_R_BUF_LIB); |
241 | return ret; | 246 | return 0; |
242 | } | 247 | } |
248 | ret = PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u); | ||
249 | BIO_free(b); | ||
250 | return ret; | ||
251 | } | ||
243 | 252 | ||
244 | #endif | 253 | #endif |