diff options
Diffstat (limited to 'src/lib/libcrypto/pem/pem_info.c')
-rw-r--r-- | src/lib/libcrypto/pem/pem_info.c | 99 |
1 files changed, 49 insertions, 50 deletions
diff --git a/src/lib/libcrypto/pem/pem_info.c b/src/lib/libcrypto/pem/pem_info.c index 4b69833b62..9a6dffb45c 100644 --- a/src/lib/libcrypto/pem/pem_info.c +++ b/src/lib/libcrypto/pem/pem_info.c | |||
@@ -58,20 +58,17 @@ | |||
58 | 58 | ||
59 | #include <stdio.h> | 59 | #include <stdio.h> |
60 | #include "cryptlib.h" | 60 | #include "cryptlib.h" |
61 | #include "buffer.h" | 61 | #include <openssl/buffer.h> |
62 | #include "objects.h" | 62 | #include <openssl/objects.h> |
63 | #include "evp.h" | 63 | #include <openssl/evp.h> |
64 | #include "x509.h" | 64 | #include <openssl/x509.h> |
65 | #include "pem.h" | 65 | #include <openssl/pem.h> |
66 | 66 | ||
67 | #ifndef NO_FP_API | 67 | #ifndef OPENSSL_NO_FP_API |
68 | STACK *PEM_X509_INFO_read(fp,sk,cb) | 68 | STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u) |
69 | FILE *fp; | ||
70 | STACK *sk; | ||
71 | int (*cb)(); | ||
72 | { | 69 | { |
73 | BIO *b; | 70 | BIO *b; |
74 | STACK *ret; | 71 | STACK_OF(X509_INFO) *ret; |
75 | 72 | ||
76 | if ((b=BIO_new(BIO_s_file())) == NULL) | 73 | if ((b=BIO_new(BIO_s_file())) == NULL) |
77 | { | 74 | { |
@@ -79,29 +76,26 @@ int (*cb)(); | |||
79 | return(0); | 76 | return(0); |
80 | } | 77 | } |
81 | BIO_set_fp(b,fp,BIO_NOCLOSE); | 78 | BIO_set_fp(b,fp,BIO_NOCLOSE); |
82 | ret=PEM_X509_INFO_read_bio(b,sk,cb); | 79 | ret=PEM_X509_INFO_read_bio(b,sk,cb,u); |
83 | BIO_free(b); | 80 | BIO_free(b); |
84 | return(ret); | 81 | return(ret); |
85 | } | 82 | } |
86 | #endif | 83 | #endif |
87 | 84 | ||
88 | STACK *PEM_X509_INFO_read_bio(bp,sk,cb) | 85 | STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u) |
89 | BIO *bp; | ||
90 | STACK *sk; | ||
91 | int (*cb)(); | ||
92 | { | 86 | { |
93 | X509_INFO *xi=NULL; | 87 | X509_INFO *xi=NULL; |
94 | char *name=NULL,*header=NULL,**pp; | 88 | char *name=NULL,*header=NULL,**pp; |
95 | unsigned char *data=NULL,*p; | 89 | unsigned char *data=NULL,*p; |
96 | long len,error=0; | 90 | long len,error=0; |
97 | int ok=0; | 91 | int ok=0; |
98 | STACK *ret=NULL; | 92 | STACK_OF(X509_INFO) *ret=NULL; |
99 | unsigned int i,raw; | 93 | unsigned int i,raw; |
100 | char *(*d2i)(); | 94 | char *(*d2i)(); |
101 | 95 | ||
102 | if (sk == NULL) | 96 | if (sk == NULL) |
103 | { | 97 | { |
104 | if ((ret=sk_new_null()) == NULL) | 98 | if ((ret=sk_X509_INFO_new_null()) == NULL) |
105 | { | 99 | { |
106 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE); | 100 | PEMerr(PEM_F_PEM_X509_INFO_READ_BIO,ERR_R_MALLOC_FAILURE); |
107 | goto err; | 101 | goto err; |
@@ -117,7 +111,7 @@ int (*cb)(); | |||
117 | i=PEM_read_bio(bp,&name,&header,&data,&len); | 111 | i=PEM_read_bio(bp,&name,&header,&data,&len); |
118 | if (i == 0) | 112 | if (i == 0) |
119 | { | 113 | { |
120 | error=ERR_GET_REASON(ERR_peek_error()); | 114 | error=ERR_GET_REASON(ERR_peek_last_error()); |
121 | if (error == PEM_R_NO_START_LINE) | 115 | if (error == PEM_R_NO_START_LINE) |
122 | { | 116 | { |
123 | ERR_clear_error(); | 117 | ERR_clear_error(); |
@@ -132,7 +126,18 @@ start: | |||
132 | d2i=(char *(*)())d2i_X509; | 126 | d2i=(char *(*)())d2i_X509; |
133 | if (xi->x509 != NULL) | 127 | if (xi->x509 != NULL) |
134 | { | 128 | { |
135 | if (!sk_push(ret,(char *)xi)) goto err; | 129 | if (!sk_X509_INFO_push(ret,xi)) goto err; |
130 | if ((xi=X509_INFO_new()) == NULL) goto err; | ||
131 | goto start; | ||
132 | } | ||
133 | pp=(char **)&(xi->x509); | ||
134 | } | ||
135 | else if ((strcmp(name,PEM_STRING_X509_TRUSTED) == 0)) | ||
136 | { | ||
137 | d2i=(char *(*)())d2i_X509_AUX; | ||
138 | if (xi->x509 != NULL) | ||
139 | { | ||
140 | if (!sk_X509_INFO_push(ret,xi)) goto err; | ||
136 | if ((xi=X509_INFO_new()) == NULL) goto err; | 141 | if ((xi=X509_INFO_new()) == NULL) goto err; |
137 | goto start; | 142 | goto start; |
138 | } | 143 | } |
@@ -143,20 +148,20 @@ start: | |||
143 | d2i=(char *(*)())d2i_X509_CRL; | 148 | d2i=(char *(*)())d2i_X509_CRL; |
144 | if (xi->crl != NULL) | 149 | if (xi->crl != NULL) |
145 | { | 150 | { |
146 | if (!sk_push(ret,(char *)xi)) goto err; | 151 | if (!sk_X509_INFO_push(ret,xi)) goto err; |
147 | if ((xi=X509_INFO_new()) == NULL) goto err; | 152 | if ((xi=X509_INFO_new()) == NULL) goto err; |
148 | goto start; | 153 | goto start; |
149 | } | 154 | } |
150 | pp=(char **)&(xi->crl); | 155 | pp=(char **)&(xi->crl); |
151 | } | 156 | } |
152 | else | 157 | else |
153 | #ifndef NO_RSA | 158 | #ifndef OPENSSL_NO_RSA |
154 | if (strcmp(name,PEM_STRING_RSA) == 0) | 159 | if (strcmp(name,PEM_STRING_RSA) == 0) |
155 | { | 160 | { |
156 | d2i=(char *(*)())d2i_RSAPrivateKey; | 161 | d2i=(char *(*)())d2i_RSAPrivateKey; |
157 | if (xi->x_pkey != NULL) | 162 | if (xi->x_pkey != NULL) |
158 | { | 163 | { |
159 | if (!sk_push(ret,(char *)xi)) goto err; | 164 | if (!sk_X509_INFO_push(ret,xi)) goto err; |
160 | if ((xi=X509_INFO_new()) == NULL) goto err; | 165 | if ((xi=X509_INFO_new()) == NULL) goto err; |
161 | goto start; | 166 | goto start; |
162 | } | 167 | } |
@@ -174,13 +179,13 @@ start: | |||
174 | } | 179 | } |
175 | else | 180 | else |
176 | #endif | 181 | #endif |
177 | #ifndef NO_DSA | 182 | #ifndef OPENSSL_NO_DSA |
178 | if (strcmp(name,PEM_STRING_DSA) == 0) | 183 | if (strcmp(name,PEM_STRING_DSA) == 0) |
179 | { | 184 | { |
180 | d2i=(char *(*)())d2i_DSAPrivateKey; | 185 | d2i=(char *(*)())d2i_DSAPrivateKey; |
181 | if (xi->x_pkey != NULL) | 186 | if (xi->x_pkey != NULL) |
182 | { | 187 | { |
183 | if (!sk_push(ret,(char *)xi)) goto err; | 188 | if (!sk_X509_INFO_push(ret,xi)) goto err; |
184 | if ((xi=X509_INFO_new()) == NULL) goto err; | 189 | if ((xi=X509_INFO_new()) == NULL) goto err; |
185 | goto start; | 190 | goto start; |
186 | } | 191 | } |
@@ -211,7 +216,7 @@ start: | |||
211 | 216 | ||
212 | if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) | 217 | if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) |
213 | goto err; | 218 | goto err; |
214 | if (!PEM_do_header(&cipher,data,&len,cb)) | 219 | if (!PEM_do_header(&cipher,data,&len,cb,u)) |
215 | goto err; | 220 | goto err; |
216 | p=data; | 221 | p=data; |
217 | if (d2i(pp,&p,len) == NULL) | 222 | if (d2i(pp,&p,len) == NULL) |
@@ -232,9 +237,9 @@ start: | |||
232 | else { | 237 | else { |
233 | /* unknown */ | 238 | /* unknown */ |
234 | } | 239 | } |
235 | if (name != NULL) Free(name); | 240 | if (name != NULL) OPENSSL_free(name); |
236 | if (header != NULL) Free(header); | 241 | if (header != NULL) OPENSSL_free(header); |
237 | if (data != NULL) Free(data); | 242 | if (data != NULL) OPENSSL_free(data); |
238 | name=NULL; | 243 | name=NULL; |
239 | header=NULL; | 244 | header=NULL; |
240 | data=NULL; | 245 | data=NULL; |
@@ -246,7 +251,7 @@ start: | |||
246 | if ((xi->x509 != NULL) || (xi->crl != NULL) || | 251 | if ((xi->x509 != NULL) || (xi->crl != NULL) || |
247 | (xi->x_pkey != NULL) || (xi->enc_data != NULL)) | 252 | (xi->x_pkey != NULL) || (xi->enc_data != NULL)) |
248 | { | 253 | { |
249 | if (!sk_push(ret,(char *)xi)) goto err; | 254 | if (!sk_X509_INFO_push(ret,xi)) goto err; |
250 | xi=NULL; | 255 | xi=NULL; |
251 | } | 256 | } |
252 | ok=1; | 257 | ok=1; |
@@ -254,36 +259,30 @@ err: | |||
254 | if (xi != NULL) X509_INFO_free(xi); | 259 | if (xi != NULL) X509_INFO_free(xi); |
255 | if (!ok) | 260 | if (!ok) |
256 | { | 261 | { |
257 | for (i=0; ((int)i)<sk_num(ret); i++) | 262 | for (i=0; ((int)i)<sk_X509_INFO_num(ret); i++) |
258 | { | 263 | { |
259 | xi=(X509_INFO *)sk_value(ret,i); | 264 | xi=sk_X509_INFO_value(ret,i); |
260 | X509_INFO_free(xi); | 265 | X509_INFO_free(xi); |
261 | } | 266 | } |
262 | if (ret != sk) sk_free(ret); | 267 | if (ret != sk) sk_X509_INFO_free(ret); |
263 | ret=NULL; | 268 | ret=NULL; |
264 | } | 269 | } |
265 | 270 | ||
266 | if (name != NULL) Free(name); | 271 | if (name != NULL) OPENSSL_free(name); |
267 | if (header != NULL) Free(header); | 272 | if (header != NULL) OPENSSL_free(header); |
268 | if (data != NULL) Free(data); | 273 | if (data != NULL) OPENSSL_free(data); |
269 | return(ret); | 274 | return(ret); |
270 | } | 275 | } |
271 | 276 | ||
272 | 277 | ||
273 | /* A TJH addition */ | 278 | /* A TJH addition */ |
274 | int PEM_X509_INFO_write_bio(bp,xi,enc,kstr,klen,cb) | 279 | int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc, |
275 | BIO *bp; | 280 | unsigned char *kstr, int klen, pem_password_cb *cb, void *u) |
276 | X509_INFO *xi; | ||
277 | EVP_CIPHER *enc; | ||
278 | unsigned char *kstr; | ||
279 | int klen; | ||
280 | int (*cb)(); | ||
281 | { | 281 | { |
282 | EVP_CIPHER_CTX ctx; | 282 | EVP_CIPHER_CTX ctx; |
283 | int i,ret=0; | 283 | int i,ret=0; |
284 | unsigned char *data=NULL; | 284 | unsigned char *data=NULL; |
285 | char *objstr=NULL; | 285 | const char *objstr=NULL; |
286 | #define PEM_BUFSIZE 1024 | ||
287 | char buf[PEM_BUFSIZE]; | 286 | char buf[PEM_BUFSIZE]; |
288 | unsigned char *iv=NULL; | 287 | unsigned char *iv=NULL; |
289 | 288 | ||
@@ -306,7 +305,7 @@ int (*cb)(); | |||
306 | { | 305 | { |
307 | if ( (xi->enc_data!=NULL) && (xi->enc_len>0) ) | 306 | if ( (xi->enc_data!=NULL) && (xi->enc_len>0) ) |
308 | { | 307 | { |
309 | /* copy from wierdo names into more normal things */ | 308 | /* copy from weirdo names into more normal things */ |
310 | iv=xi->enc_cipher.iv; | 309 | iv=xi->enc_cipher.iv; |
311 | data=(unsigned char *)xi->enc_data; | 310 | data=(unsigned char *)xi->enc_data; |
312 | i=xi->enc_len; | 311 | i=xi->enc_len; |
@@ -327,7 +326,7 @@ int (*cb)(); | |||
327 | /* create the right magic header stuff */ | 326 | /* create the right magic header stuff */ |
328 | buf[0]='\0'; | 327 | buf[0]='\0'; |
329 | PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); | 328 | PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); |
330 | PEM_dek_info(buf,objstr,8,(char *)iv); | 329 | PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv); |
331 | 330 | ||
332 | /* use the normal code to write things out */ | 331 | /* use the normal code to write things out */ |
333 | i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i); | 332 | i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i); |
@@ -336,18 +335,18 @@ int (*cb)(); | |||
336 | else | 335 | else |
337 | { | 336 | { |
338 | /* Add DSA/DH */ | 337 | /* Add DSA/DH */ |
339 | #ifndef NO_RSA | 338 | #ifndef OPENSSL_NO_RSA |
340 | /* normal optionally encrypted stuff */ | 339 | /* normal optionally encrypted stuff */ |
341 | if (PEM_write_bio_RSAPrivateKey(bp, | 340 | if (PEM_write_bio_RSAPrivateKey(bp, |
342 | xi->x_pkey->dec_pkey->pkey.rsa, | 341 | xi->x_pkey->dec_pkey->pkey.rsa, |
343 | enc,kstr,klen,cb)<=0) | 342 | enc,kstr,klen,cb,u)<=0) |
344 | goto err; | 343 | goto err; |
345 | #endif | 344 | #endif |
346 | } | 345 | } |
347 | } | 346 | } |
348 | 347 | ||
349 | /* if we have a certificate then write it out now */ | 348 | /* if we have a certificate then write it out now */ |
350 | if ((xi->x509 != NULL) || (PEM_write_bio_X509(bp,xi->x509) <= 0)) | 349 | if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp,xi->x509) <= 0)) |
351 | goto err; | 350 | goto err; |
352 | 351 | ||
353 | /* we are ignoring anything else that is loaded into the X509_INFO | 352 | /* we are ignoring anything else that is loaded into the X509_INFO |