diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/pem/pem_info.c | 463 |
1 files changed, 227 insertions, 236 deletions
diff --git a/src/lib/libcrypto/pem/pem_info.c b/src/lib/libcrypto/pem/pem_info.c index 4351260dfb..967d6d2d41 100644 --- a/src/lib/libcrypto/pem/pem_info.c +++ b/src/lib/libcrypto/pem/pem_info.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 |
@@ -71,324 +71,315 @@ | |||
71 | #endif | 71 | #endif |
72 | 72 | ||
73 | #ifndef OPENSSL_NO_FP_API | 73 | #ifndef OPENSSL_NO_FP_API |
74 | STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u) | 74 | STACK_OF(X509_INFO) * |
75 | { | 75 | PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, |
76 | BIO *b; | 76 | void *u) |
77 | STACK_OF(X509_INFO) *ret; | 77 | { |
78 | BIO *b; | ||
79 | STACK_OF(X509_INFO) *ret; | ||
78 | 80 | ||
79 | if ((b=BIO_new(BIO_s_file())) == NULL) | 81 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
80 | { | 82 | PEMerr(PEM_F_PEM_X509_INFO_READ, ERR_R_BUF_LIB); |
81 | PEMerr(PEM_F_PEM_X509_INFO_READ,ERR_R_BUF_LIB); | 83 | return (0); |
82 | return(0); | ||
83 | } | ||
84 | BIO_set_fp(b,fp,BIO_NOCLOSE); | ||
85 | ret=PEM_X509_INFO_read_bio(b,sk,cb,u); | ||
86 | BIO_free(b); | ||
87 | return(ret); | ||
88 | } | 84 | } |
85 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
86 | ret = PEM_X509_INFO_read_bio(b, sk, cb, u); | ||
87 | BIO_free(b); | ||
88 | return (ret); | ||
89 | } | ||
89 | #endif | 90 | #endif |
90 | 91 | ||
91 | STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u) | 92 | STACK_OF(X509_INFO) * |
92 | { | 93 | PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, |
93 | X509_INFO *xi=NULL; | 94 | void *u) |
94 | char *name=NULL,*header=NULL; | 95 | { |
96 | X509_INFO *xi = NULL; | ||
97 | char *name = NULL, *header = NULL; | ||
95 | void *pp; | 98 | void *pp; |
96 | unsigned char *data=NULL; | 99 | unsigned char *data = NULL; |
97 | const unsigned char *p; | 100 | const unsigned char *p; |
98 | long len,error=0; | 101 | long len, error = 0; |
99 | int ok=0; | 102 | int ok = 0; |
100 | STACK_OF(X509_INFO) *ret=NULL; | 103 | STACK_OF(X509_INFO) *ret = NULL; |
101 | unsigned int i,raw,ptype; | 104 | unsigned int i, raw, ptype; |
102 | d2i_of_void *d2i = 0; | 105 | d2i_of_void *d2i = 0; |
103 | 106 | ||
104 | if (sk == NULL) | 107 | if (sk == NULL) { |
105 | { | 108 | if ((ret = sk_X509_INFO_new_null()) == NULL) { |
106 | if ((ret=sk_X509_INFO_new_null()) == NULL) | 109 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, |
107 | { | 110 | ERR_R_MALLOC_FAILURE); |
108 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE); | ||
109 | goto err; | 111 | goto err; |
110 | } | ||
111 | } | 112 | } |
112 | else | 113 | } else |
113 | ret=sk; | 114 | ret = sk; |
114 | 115 | ||
115 | if ((xi=X509_INFO_new()) == NULL) goto err; | 116 | if ((xi = X509_INFO_new()) == NULL) |
116 | for (;;) | 117 | goto err; |
117 | { | 118 | for (;;) { |
118 | raw=0; | 119 | raw = 0; |
119 | ptype = 0; | 120 | ptype = 0; |
120 | i=PEM_read_bio(bp,&name,&header,&data,&len); | 121 | i = PEM_read_bio(bp, &name, &header, &data, &len); |
121 | if (i == 0) | 122 | if (i == 0) { |
122 | { | 123 | error = ERR_GET_REASON(ERR_peek_last_error()); |
123 | error=ERR_GET_REASON(ERR_peek_last_error()); | 124 | if (error == PEM_R_NO_START_LINE) { |
124 | if (error == PEM_R_NO_START_LINE) | ||
125 | { | ||
126 | ERR_clear_error(); | 125 | ERR_clear_error(); |
127 | break; | 126 | break; |
128 | } | ||
129 | goto err; | ||
130 | } | 127 | } |
128 | goto err; | ||
129 | } | ||
131 | start: | 130 | start: |
132 | if ( (strcmp(name,PEM_STRING_X509) == 0) || | 131 | if ((strcmp(name, PEM_STRING_X509) == 0) || |
133 | (strcmp(name,PEM_STRING_X509_OLD) == 0)) | 132 | (strcmp(name, PEM_STRING_X509_OLD) == 0)) { |
134 | { | 133 | d2i = (D2I_OF(void))d2i_X509; |
135 | d2i=(D2I_OF(void))d2i_X509; | 134 | if (xi->x509 != NULL) { |
136 | if (xi->x509 != NULL) | 135 | if (!sk_X509_INFO_push(ret, xi)) |
137 | { | 136 | goto err; |
138 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 137 | if ((xi = X509_INFO_new()) == NULL) |
139 | if ((xi=X509_INFO_new()) == NULL) goto err; | 138 | goto err; |
140 | goto start; | 139 | goto start; |
141 | } | ||
142 | pp=&(xi->x509); | ||
143 | } | 140 | } |
144 | else if ((strcmp(name,PEM_STRING_X509_TRUSTED) == 0)) | 141 | pp = &(xi->x509); |
145 | { | 142 | } else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) { |
146 | d2i=(D2I_OF(void))d2i_X509_AUX; | 143 | d2i = (D2I_OF(void))d2i_X509_AUX; |
147 | if (xi->x509 != NULL) | 144 | if (xi->x509 != NULL) { |
148 | { | 145 | if (!sk_X509_INFO_push(ret, xi)) |
149 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 146 | goto err; |
150 | if ((xi=X509_INFO_new()) == NULL) goto err; | 147 | if ((xi = X509_INFO_new()) == NULL) |
148 | goto err; | ||
151 | goto start; | 149 | goto start; |
152 | } | ||
153 | pp=&(xi->x509); | ||
154 | } | 150 | } |
155 | else if (strcmp(name,PEM_STRING_X509_CRL) == 0) | 151 | pp = &(xi->x509); |
156 | { | 152 | } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) { |
157 | d2i=(D2I_OF(void))d2i_X509_CRL; | 153 | d2i = (D2I_OF(void))d2i_X509_CRL; |
158 | if (xi->crl != NULL) | 154 | if (xi->crl != NULL) { |
159 | { | 155 | if (!sk_X509_INFO_push(ret, xi)) |
160 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 156 | goto err; |
161 | if ((xi=X509_INFO_new()) == NULL) goto err; | 157 | if ((xi = X509_INFO_new()) == NULL) |
158 | goto err; | ||
162 | goto start; | 159 | goto start; |
163 | } | ||
164 | pp=&(xi->crl); | ||
165 | } | 160 | } |
166 | else | 161 | pp = &(xi->crl); |
162 | } else | ||
167 | #ifndef OPENSSL_NO_RSA | 163 | #ifndef OPENSSL_NO_RSA |
168 | if (strcmp(name,PEM_STRING_RSA) == 0) | 164 | if (strcmp(name, PEM_STRING_RSA) == 0) { |
169 | { | 165 | d2i = (D2I_OF(void))d2i_RSAPrivateKey; |
170 | d2i=(D2I_OF(void))d2i_RSAPrivateKey; | 166 | if (xi->x_pkey != NULL) { |
171 | if (xi->x_pkey != NULL) | 167 | if (!sk_X509_INFO_push(ret, xi)) |
172 | { | 168 | goto err; |
173 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 169 | if ((xi = X509_INFO_new()) == NULL) |
174 | if ((xi=X509_INFO_new()) == NULL) goto err; | 170 | goto err; |
175 | goto start; | 171 | goto start; |
176 | } | 172 | } |
177 | 173 | ||
178 | xi->enc_data=NULL; | 174 | xi->enc_data = NULL; |
179 | xi->enc_len=0; | 175 | xi->enc_len = 0; |
180 | 176 | ||
181 | xi->x_pkey=X509_PKEY_new(); | 177 | xi->x_pkey = X509_PKEY_new(); |
182 | ptype=EVP_PKEY_RSA; | 178 | ptype = EVP_PKEY_RSA; |
183 | pp=&xi->x_pkey->dec_pkey; | 179 | pp = &xi->x_pkey->dec_pkey; |
184 | if ((int)strlen(header) > 10) /* assume encrypted */ | 180 | if ((int)strlen(header) > 10) /* assume encrypted */ |
185 | raw=1; | 181 | raw = 1; |
186 | } | 182 | } else |
187 | else | ||
188 | #endif | 183 | #endif |
189 | #ifndef OPENSSL_NO_DSA | 184 | #ifndef OPENSSL_NO_DSA |
190 | if (strcmp(name,PEM_STRING_DSA) == 0) | 185 | if (strcmp(name, PEM_STRING_DSA) == 0) { |
191 | { | 186 | d2i = (D2I_OF(void))d2i_DSAPrivateKey; |
192 | d2i=(D2I_OF(void))d2i_DSAPrivateKey; | 187 | if (xi->x_pkey != NULL) { |
193 | if (xi->x_pkey != NULL) | 188 | if (!sk_X509_INFO_push(ret, xi)) |
194 | { | 189 | goto err; |
195 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 190 | if ((xi = X509_INFO_new()) == NULL) |
196 | if ((xi=X509_INFO_new()) == NULL) goto err; | 191 | goto err; |
197 | goto start; | 192 | goto start; |
198 | } | 193 | } |
199 | 194 | ||
200 | xi->enc_data=NULL; | 195 | xi->enc_data = NULL; |
201 | xi->enc_len=0; | 196 | xi->enc_len = 0; |
202 | 197 | ||
203 | xi->x_pkey=X509_PKEY_new(); | 198 | xi->x_pkey = X509_PKEY_new(); |
204 | ptype = EVP_PKEY_DSA; | 199 | ptype = EVP_PKEY_DSA; |
205 | pp=&xi->x_pkey->dec_pkey; | 200 | pp = &xi->x_pkey->dec_pkey; |
206 | if ((int)strlen(header) > 10) /* assume encrypted */ | 201 | if ((int)strlen(header) > 10) /* assume encrypted */ |
207 | raw=1; | 202 | raw = 1; |
208 | } | 203 | } else |
209 | else | ||
210 | #endif | 204 | #endif |
211 | #ifndef OPENSSL_NO_EC | 205 | #ifndef OPENSSL_NO_EC |
212 | if (strcmp(name,PEM_STRING_ECPRIVATEKEY) == 0) | 206 | if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) { |
213 | { | 207 | d2i = (D2I_OF(void))d2i_ECPrivateKey; |
214 | d2i=(D2I_OF(void))d2i_ECPrivateKey; | 208 | if (xi->x_pkey != NULL) { |
215 | if (xi->x_pkey != NULL) | 209 | if (!sk_X509_INFO_push(ret, xi)) |
216 | { | 210 | goto err; |
217 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 211 | if ((xi = X509_INFO_new()) == NULL) |
218 | if ((xi=X509_INFO_new()) == NULL) goto err; | 212 | goto err; |
219 | goto start; | 213 | goto start; |
220 | } | ||
221 | |||
222 | xi->enc_data=NULL; | ||
223 | xi->enc_len=0; | ||
224 | |||
225 | xi->x_pkey=X509_PKEY_new(); | ||
226 | ptype = EVP_PKEY_EC; | ||
227 | pp=&xi->x_pkey->dec_pkey; | ||
228 | if ((int)strlen(header) > 10) /* assume encrypted */ | ||
229 | raw=1; | ||
230 | } | 214 | } |
231 | else | 215 | |
216 | xi->enc_data = NULL; | ||
217 | xi->enc_len = 0; | ||
218 | |||
219 | xi->x_pkey = X509_PKEY_new(); | ||
220 | ptype = EVP_PKEY_EC; | ||
221 | pp = &xi->x_pkey->dec_pkey; | ||
222 | if ((int)strlen(header) > 10) /* assume encrypted */ | ||
223 | raw = 1; | ||
224 | } else | ||
232 | #endif | 225 | #endif |
233 | { | 226 | { |
234 | d2i=NULL; | 227 | d2i = NULL; |
235 | pp=NULL; | 228 | pp = NULL; |
236 | } | 229 | } |
237 | 230 | ||
238 | if (d2i != NULL) | 231 | if (d2i != NULL) { |
239 | { | 232 | if (!raw) { |
240 | if (!raw) | ||
241 | { | ||
242 | EVP_CIPHER_INFO cipher; | 233 | EVP_CIPHER_INFO cipher; |
243 | 234 | ||
244 | if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) | 235 | if (!PEM_get_EVP_CIPHER_INFO(header, &cipher)) |
245 | goto err; | 236 | goto err; |
246 | if (!PEM_do_header(&cipher,data,&len,cb,u)) | 237 | if (!PEM_do_header(&cipher, data, &len, cb, u)) |
247 | goto err; | 238 | goto err; |
248 | p=data; | 239 | p = data; |
249 | if (ptype) | 240 | if (ptype) { |
250 | { | 241 | if (!d2i_PrivateKey(ptype, pp, &p, |
251 | if (!d2i_PrivateKey(ptype, pp, &p, len)) | 242 | len)) { |
252 | { | 243 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB); |
253 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_ASN1_LIB); | ||
254 | goto err; | 244 | goto err; |
255 | } | ||
256 | } | 245 | } |
257 | else if (d2i(pp,&p,len) == NULL) | 246 | } else if (d2i(pp, &p, len) == NULL) { |
258 | { | 247 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO, ERR_R_ASN1_LIB); |
259 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_ASN1_LIB); | ||
260 | goto err; | 248 | goto err; |
261 | } | ||
262 | } | 249 | } |
263 | else | 250 | } else { /* encrypted RSA data */ |
264 | { /* encrypted RSA data */ | ||
265 | if (!PEM_get_EVP_CIPHER_INFO(header, | 251 | if (!PEM_get_EVP_CIPHER_INFO(header, |
266 | &xi->enc_cipher)) goto err; | 252 | &xi->enc_cipher)) |
267 | xi->enc_data=(char *)data; | 253 | goto err; |
268 | xi->enc_len=(int)len; | 254 | xi->enc_data = (char *)data; |
269 | data=NULL; | 255 | xi->enc_len = (int)len; |
270 | } | 256 | data = NULL; |
271 | } | 257 | } |
272 | else { | 258 | } else { |
273 | /* unknown */ | 259 | /* unknown */ |
274 | } | ||
275 | if (name != NULL) free(name); | ||
276 | if (header != NULL) free(header); | ||
277 | if (data != NULL) free(data); | ||
278 | name=NULL; | ||
279 | header=NULL; | ||
280 | data=NULL; | ||
281 | } | 260 | } |
261 | if (name != NULL) | ||
262 | free(name); | ||
263 | if (header != NULL) | ||
264 | free(header); | ||
265 | if (data != NULL) | ||
266 | free(data); | ||
267 | name = NULL; | ||
268 | header = NULL; | ||
269 | data = NULL; | ||
270 | } | ||
282 | 271 | ||
283 | /* if the last one hasn't been pushed yet and there is anything | 272 | /* if the last one hasn't been pushed yet and there is anything |
284 | * in it then add it to the stack ... | 273 | * in it then add it to the stack ... |
285 | */ | 274 | */ |
286 | if ((xi->x509 != NULL) || (xi->crl != NULL) || | 275 | if ((xi->x509 != NULL) || (xi->crl != NULL) || |
287 | (xi->x_pkey != NULL) || (xi->enc_data != NULL)) | 276 | (xi->x_pkey != NULL) || (xi->enc_data != NULL)) { |
288 | { | 277 | if (!sk_X509_INFO_push(ret, xi)) |
289 | if (!sk_X509_INFO_push(ret,xi)) goto err; | 278 | goto err; |
290 | xi=NULL; | 279 | xi = NULL; |
291 | } | 280 | } |
292 | ok=1; | 281 | ok = 1; |
282 | |||
293 | err: | 283 | err: |
294 | if (xi != NULL) X509_INFO_free(xi); | 284 | if (xi != NULL) |
295 | if (!ok) | 285 | X509_INFO_free(xi); |
296 | { | 286 | if (!ok) { |
297 | for (i=0; ((int)i)<sk_X509_INFO_num(ret); i++) | 287 | for (i = 0; ((int)i) < sk_X509_INFO_num(ret); i++) { |
298 | { | 288 | xi = sk_X509_INFO_value(ret, i); |
299 | xi=sk_X509_INFO_value(ret,i); | ||
300 | X509_INFO_free(xi); | 289 | X509_INFO_free(xi); |
301 | } | ||
302 | if (ret != sk) sk_X509_INFO_free(ret); | ||
303 | ret=NULL; | ||
304 | } | 290 | } |
305 | 291 | if (ret != sk) | |
306 | if (name != NULL) free(name); | 292 | sk_X509_INFO_free(ret); |
307 | if (header != NULL) free(header); | 293 | ret = NULL; |
308 | if (data != NULL) free(data); | ||
309 | return(ret); | ||
310 | } | 294 | } |
311 | 295 | ||
296 | if (name != NULL) | ||
297 | free(name); | ||
298 | if (header != NULL) | ||
299 | free(header); | ||
300 | if (data != NULL) | ||
301 | free(data); | ||
302 | return (ret); | ||
303 | } | ||
304 | |||
312 | 305 | ||
313 | /* A TJH addition */ | 306 | /* A TJH addition */ |
314 | int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc, | 307 | int |
315 | unsigned char *kstr, int klen, pem_password_cb *cb, void *u) | 308 | PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc, |
316 | { | 309 | unsigned char *kstr, int klen, pem_password_cb *cb, void *u) |
310 | { | ||
317 | EVP_CIPHER_CTX ctx; | 311 | EVP_CIPHER_CTX ctx; |
318 | int i,ret=0; | 312 | int i, ret = 0; |
319 | unsigned char *data=NULL; | 313 | unsigned char *data = NULL; |
320 | const char *objstr=NULL; | 314 | const char *objstr = NULL; |
321 | char buf[PEM_BUFSIZE]; | 315 | char buf[PEM_BUFSIZE]; |
322 | unsigned char *iv=NULL; | 316 | unsigned char *iv = NULL; |
323 | 317 | ||
324 | if (enc != NULL) | 318 | if (enc != NULL) { |
325 | { | 319 | objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); |
326 | objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc)); | 320 | if (objstr == NULL) { |
327 | if (objstr == NULL) | 321 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, |
328 | { | 322 | PEM_R_UNSUPPORTED_CIPHER); |
329 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER); | ||
330 | goto err; | 323 | goto err; |
331 | } | ||
332 | } | 324 | } |
325 | } | ||
333 | 326 | ||
334 | /* now for the fun part ... if we have a private key then | 327 | /* now for the fun part ... if we have a private key then |
335 | * we have to be able to handle a not-yet-decrypted key | 328 | * we have to be able to handle a not-yet-decrypted key |
336 | * being written out correctly ... if it is decrypted or | 329 | * being written out correctly ... if it is decrypted or |
337 | * it is non-encrypted then we use the base code | 330 | * it is non-encrypted then we use the base code |
338 | */ | 331 | */ |
339 | if (xi->x_pkey!=NULL) | 332 | if (xi->x_pkey != NULL) { |
340 | { | 333 | if ((xi->enc_data != NULL) && (xi->enc_len > 0) ) { |
341 | if ( (xi->enc_data!=NULL) && (xi->enc_len>0) ) | 334 | if (enc == NULL) { |
342 | { | 335 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, |
343 | if (enc == NULL) | 336 | PEM_R_CIPHER_IS_NULL); |
344 | { | ||
345 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_CIPHER_IS_NULL); | ||
346 | goto err; | 337 | goto err; |
347 | } | 338 | } |
348 | 339 | ||
349 | /* copy from weirdo names into more normal things */ | 340 | /* copy from weirdo names into more normal things */ |
350 | iv=xi->enc_cipher.iv; | 341 | iv = xi->enc_cipher.iv; |
351 | data=(unsigned char *)xi->enc_data; | 342 | data = (unsigned char *)xi->enc_data; |
352 | i=xi->enc_len; | 343 | i = xi->enc_len; |
353 | 344 | ||
354 | /* we take the encryption data from the | 345 | /* we take the encryption data from the |
355 | * internal stuff rather than what the | 346 | * internal stuff rather than what the |
356 | * user has passed us ... as we have to | 347 | * user has passed us ... as we have to |
357 | * match exactly for some strange reason | 348 | * match exactly for some strange reason |
358 | */ | 349 | */ |
359 | objstr=OBJ_nid2sn( | 350 | objstr = OBJ_nid2sn( |
360 | EVP_CIPHER_nid(xi->enc_cipher.cipher)); | 351 | EVP_CIPHER_nid(xi->enc_cipher.cipher)); |
361 | if (objstr == NULL) | 352 | if (objstr == NULL) { |
362 | { | 353 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO, |
363 | PEMerr(PEM_F_PEM_X509_INFO_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER); | 354 | PEM_R_UNSUPPORTED_CIPHER); |
364 | goto err; | 355 | goto err; |
365 | } | 356 | } |
366 | 357 | ||
367 | /* create the right magic header stuff */ | 358 | /* create the right magic header stuff */ |
368 | OPENSSL_assert(strlen(objstr)+23+2*enc->iv_len+13 <= sizeof buf); | 359 | OPENSSL_assert(strlen(objstr) + 23 + |
369 | buf[0]='\0'; | 360 | 2 * enc->iv_len + 13 <= sizeof buf); |
370 | PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); | 361 | buf[0] = '\0'; |
371 | PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv); | 362 | PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); |
363 | PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv); | ||
372 | 364 | ||
373 | /* use the normal code to write things out */ | 365 | /* use the normal code to write things out */ |
374 | i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i); | 366 | i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i); |
375 | if (i <= 0) goto err; | 367 | if (i <= 0) |
376 | } | 368 | goto err; |
377 | else | 369 | } else { |
378 | { | ||
379 | /* Add DSA/DH */ | 370 | /* Add DSA/DH */ |
380 | #ifndef OPENSSL_NO_RSA | 371 | #ifndef OPENSSL_NO_RSA |
381 | /* normal optionally encrypted stuff */ | 372 | /* normal optionally encrypted stuff */ |
382 | if (PEM_write_bio_RSAPrivateKey(bp, | 373 | if (PEM_write_bio_RSAPrivateKey(bp, |
383 | xi->x_pkey->dec_pkey->pkey.rsa, | 374 | xi->x_pkey->dec_pkey->pkey.rsa, |
384 | enc,kstr,klen,cb,u)<=0) | 375 | enc, kstr, klen, cb, u) <= 0) |
385 | goto err; | 376 | goto err; |
386 | #endif | 377 | #endif |
387 | } | ||
388 | } | 378 | } |
379 | } | ||
389 | 380 | ||
390 | /* if we have a certificate then write it out now */ | 381 | /* if we have a certificate then write it out now */ |
391 | if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp,xi->x509) <= 0)) | 382 | if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0)) |
392 | goto err; | 383 | goto err; |
393 | 384 | ||
394 | /* we are ignoring anything else that is loaded into the X509_INFO | 385 | /* we are ignoring anything else that is loaded into the X509_INFO |
@@ -397,10 +388,10 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc, | |||
397 | * base library --tjh | 388 | * base library --tjh |
398 | */ | 389 | */ |
399 | 390 | ||
400 | ret=1; | 391 | ret = 1; |
401 | 392 | ||
402 | err: | 393 | err: |
403 | OPENSSL_cleanse((char *)&ctx,sizeof(ctx)); | 394 | OPENSSL_cleanse((char *)&ctx, sizeof(ctx)); |
404 | OPENSSL_cleanse(buf,PEM_BUFSIZE); | 395 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
405 | return(ret); | 396 | return (ret); |
406 | } | 397 | } |