diff options
Diffstat (limited to 'src/lib/libcrypto/pem/pem_lib.c')
-rw-r--r-- | src/lib/libcrypto/pem/pem_lib.c | 1036 |
1 files changed, 532 insertions, 504 deletions
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c index 93736455fa..9d5d8e714c 100644 --- a/src/lib/libcrypto/pem/pem_lib.c +++ b/src/lib/libcrypto/pem/pem_lib.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 |
@@ -74,132 +74,132 @@ | |||
74 | #include <openssl/engine.h> | 74 | #include <openssl/engine.h> |
75 | #endif | 75 | #endif |
76 | 76 | ||
77 | const char PEM_version[]="PEM" OPENSSL_VERSION_PTEXT; | 77 | const char PEM_version[] = "PEM" OPENSSL_VERSION_PTEXT; |
78 | 78 | ||
79 | #define MIN_LENGTH 4 | 79 | #define MIN_LENGTH 4 |
80 | 80 | ||
81 | static int load_iv(char **fromp,unsigned char *to, int num); | 81 | static int load_iv(char **fromp, unsigned char *to, int num); |
82 | static int check_pem(const char *nm, const char *name); | 82 | static int check_pem(const char *nm, const char *name); |
83 | int pem_check_suffix(const char *pem_str, const char *suffix); | 83 | int pem_check_suffix(const char *pem_str, const char *suffix); |
84 | 84 | ||
85 | int PEM_def_callback(char *buf, int num, int w, void *key) | 85 | int |
86 | { | 86 | PEM_def_callback(char *buf, int num, int w, void *key) |
87 | { | ||
87 | #ifdef OPENSSL_NO_FP_API | 88 | #ifdef OPENSSL_NO_FP_API |
88 | /* We should not ever call the default callback routine from | 89 | /* We should not ever call the default callback routine from |
89 | * windows. */ | 90 | * windows. */ |
90 | PEMerr(PEM_F_PEM_DEF_CALLBACK,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 91 | PEMerr(PEM_F_PEM_DEF_CALLBACK, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
91 | return(-1); | 92 | return (-1); |
92 | #else | 93 | #else |
93 | int i,j; | 94 | int i, j; |
94 | const char *prompt; | 95 | const char *prompt; |
95 | if(key) { | 96 | |
96 | i=strlen(key); | 97 | if (key) { |
97 | i=(i > num)?num:i; | 98 | i = strlen(key); |
98 | memcpy(buf,key,i); | 99 | i = (i > num) ? num : i; |
99 | return(i); | 100 | memcpy(buf, key, i); |
101 | return (i); | ||
100 | } | 102 | } |
101 | 103 | ||
102 | prompt=EVP_get_pw_prompt(); | 104 | prompt = EVP_get_pw_prompt(); |
103 | if (prompt == NULL) | 105 | if (prompt == NULL) |
104 | prompt="Enter PEM pass phrase:"; | 106 | prompt = "Enter PEM pass phrase:"; |
105 | 107 | ||
106 | for (;;) | 108 | for (;;) { |
107 | { | 109 | i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w); |
108 | i=EVP_read_pw_string_min(buf,MIN_LENGTH,num,prompt,w); | 110 | if (i != 0) { |
109 | if (i != 0) | 111 | PEMerr(PEM_F_PEM_DEF_CALLBACK, |
110 | { | 112 | PEM_R_PROBLEMS_GETTING_PASSWORD); |
111 | PEMerr(PEM_F_PEM_DEF_CALLBACK,PEM_R_PROBLEMS_GETTING_PASSWORD); | 113 | memset(buf, 0, (unsigned int)num); |
112 | memset(buf,0,(unsigned int)num); | 114 | return (-1); |
113 | return(-1); | 115 | } |
114 | } | 116 | j = strlen(buf); |
115 | j=strlen(buf); | 117 | if (j < MIN_LENGTH) { |
116 | if (j < MIN_LENGTH) | 118 | fprintf(stderr, "phrase is too short, needs to be at least %d chars\n", MIN_LENGTH); |
117 | { | 119 | } else |
118 | fprintf(stderr,"phrase is too short, needs to be at least %d chars\n",MIN_LENGTH); | ||
119 | } | ||
120 | else | ||
121 | break; | 120 | break; |
122 | } | ||
123 | return(j); | ||
124 | #endif | ||
125 | } | 121 | } |
122 | return (j); | ||
123 | #endif | ||
124 | } | ||
126 | 125 | ||
127 | void PEM_proc_type(char *buf, int type) | 126 | void |
128 | { | 127 | PEM_proc_type(char *buf, int type) |
128 | { | ||
129 | const char *str; | 129 | const char *str; |
130 | 130 | ||
131 | if (type == PEM_TYPE_ENCRYPTED) | 131 | if (type == PEM_TYPE_ENCRYPTED) |
132 | str="ENCRYPTED"; | 132 | str = "ENCRYPTED"; |
133 | else if (type == PEM_TYPE_MIC_CLEAR) | 133 | else if (type == PEM_TYPE_MIC_CLEAR) |
134 | str="MIC-CLEAR"; | 134 | str = "MIC-CLEAR"; |
135 | else if (type == PEM_TYPE_MIC_ONLY) | 135 | else if (type == PEM_TYPE_MIC_ONLY) |
136 | str="MIC-ONLY"; | 136 | str = "MIC-ONLY"; |
137 | else | 137 | else |
138 | str="BAD-TYPE"; | 138 | str = "BAD-TYPE"; |
139 | 139 | ||
140 | strlcat(buf,"Proc-Type: 4,",PEM_BUFSIZE); | 140 | strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE); |
141 | strlcat(buf,str,PEM_BUFSIZE); | 141 | strlcat(buf, str, PEM_BUFSIZE); |
142 | strlcat(buf,"\n",PEM_BUFSIZE); | 142 | strlcat(buf, "\n", PEM_BUFSIZE); |
143 | } | 143 | } |
144 | 144 | ||
145 | void PEM_dek_info(char *buf, const char *type, int len, char *str) | 145 | void |
146 | { | 146 | PEM_dek_info(char *buf, const char *type, int len, char *str) |
147 | static const unsigned char map[17]="0123456789ABCDEF"; | 147 | { |
148 | static const unsigned char map[17] = "0123456789ABCDEF"; | ||
148 | long i; | 149 | long i; |
149 | int j; | 150 | int j; |
150 | 151 | ||
151 | strlcat(buf,"DEK-Info: ",PEM_BUFSIZE); | 152 | strlcat(buf, "DEK-Info: ", PEM_BUFSIZE); |
152 | strlcat(buf,type,PEM_BUFSIZE); | 153 | strlcat(buf, type, PEM_BUFSIZE); |
153 | strlcat(buf,",",PEM_BUFSIZE); | 154 | strlcat(buf, ",", PEM_BUFSIZE); |
154 | j=strlen(buf); | 155 | j = strlen(buf); |
155 | if (j + (len * 2) + 1 > PEM_BUFSIZE) | 156 | if (j + (len * 2) + 1 > PEM_BUFSIZE) |
156 | return; | 157 | return; |
157 | for (i=0; i<len; i++) | 158 | for (i = 0; i < len; i++) { |
158 | { | 159 | buf[j + i * 2] = map[(str[i] >> 4) & 0x0f]; |
159 | buf[j+i*2] =map[(str[i]>>4)&0x0f]; | 160 | buf[j + i * 2 + 1] = map[(str[i]) & 0x0f]; |
160 | buf[j+i*2+1]=map[(str[i] )&0x0f]; | ||
161 | } | ||
162 | buf[j+i*2]='\n'; | ||
163 | buf[j+i*2+1]='\0'; | ||
164 | } | 161 | } |
162 | buf[j + i * 2] = '\n'; | ||
163 | buf[j + i * 2 + 1] = '\0'; | ||
164 | } | ||
165 | 165 | ||
166 | #ifndef OPENSSL_NO_FP_API | 166 | #ifndef OPENSSL_NO_FP_API |
167 | void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, | 167 | void * |
168 | pem_password_cb *cb, void *u) | 168 | PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, |
169 | { | 169 | pem_password_cb *cb, void *u) |
170 | BIO *b; | 170 | { |
171 | void *ret; | 171 | BIO *b; |
172 | 172 | void *ret; | |
173 | if ((b=BIO_new(BIO_s_file())) == NULL) | 173 | |
174 | { | 174 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
175 | PEMerr(PEM_F_PEM_ASN1_READ,ERR_R_BUF_LIB); | 175 | PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB); |
176 | return(0); | 176 | return (0); |
177 | } | ||
178 | BIO_set_fp(b,fp,BIO_NOCLOSE); | ||
179 | ret=PEM_ASN1_read_bio(d2i,name,b,x,cb,u); | ||
180 | BIO_free(b); | ||
181 | return(ret); | ||
182 | } | 177 | } |
178 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
179 | ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); | ||
180 | BIO_free(b); | ||
181 | return (ret); | ||
182 | } | ||
183 | #endif | 183 | #endif |
184 | 184 | ||
185 | static int check_pem(const char *nm, const char *name) | 185 | static int |
186 | check_pem(const char *nm, const char *name) | ||
186 | { | 187 | { |
187 | /* Normal matching nm and name */ | 188 | /* Normal matching nm and name */ |
188 | if (!strcmp(nm,name)) return 1; | 189 | if (!strcmp(nm, name)) |
190 | return 1; | ||
189 | 191 | ||
190 | /* Make PEM_STRING_EVP_PKEY match any private key */ | 192 | /* Make PEM_STRING_EVP_PKEY match any private key */ |
191 | 193 | ||
192 | if(!strcmp(name,PEM_STRING_EVP_PKEY)) | 194 | if (!strcmp(name, PEM_STRING_EVP_PKEY)) { |
193 | { | ||
194 | int slen; | 195 | int slen; |
195 | const EVP_PKEY_ASN1_METHOD *ameth; | 196 | const EVP_PKEY_ASN1_METHOD *ameth; |
196 | if(!strcmp(nm,PEM_STRING_PKCS8)) | 197 | if (!strcmp(nm, PEM_STRING_PKCS8)) |
197 | return 1; | 198 | return 1; |
198 | if(!strcmp(nm,PEM_STRING_PKCS8INF)) | 199 | if (!strcmp(nm, PEM_STRING_PKCS8INF)) |
199 | return 1; | 200 | return 1; |
200 | slen = pem_check_suffix(nm, "PRIVATE KEY"); | 201 | slen = pem_check_suffix(nm, "PRIVATE KEY"); |
201 | if (slen > 0) | 202 | if (slen > 0) { |
202 | { | ||
203 | /* NB: ENGINE implementations wont contain | 203 | /* NB: ENGINE implementations wont contain |
204 | * a deprecated old private key decode function | 204 | * a deprecated old private key decode function |
205 | * so don't look for them. | 205 | * so don't look for them. |
@@ -207,21 +207,18 @@ static int check_pem(const char *nm, const char *name) | |||
207 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); | 207 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); |
208 | if (ameth && ameth->old_priv_decode) | 208 | if (ameth && ameth->old_priv_decode) |
209 | return 1; | 209 | return 1; |
210 | } | ||
211 | return 0; | ||
212 | } | 210 | } |
211 | return 0; | ||
212 | } | ||
213 | 213 | ||
214 | if(!strcmp(name,PEM_STRING_PARAMETERS)) | 214 | if (!strcmp(name, PEM_STRING_PARAMETERS)) { |
215 | { | ||
216 | int slen; | 215 | int slen; |
217 | const EVP_PKEY_ASN1_METHOD *ameth; | 216 | const EVP_PKEY_ASN1_METHOD *ameth; |
218 | slen = pem_check_suffix(nm, "PARAMETERS"); | 217 | slen = pem_check_suffix(nm, "PARAMETERS"); |
219 | if (slen > 0) | 218 | if (slen > 0) { |
220 | { | ||
221 | ENGINE *e; | 219 | ENGINE *e; |
222 | ameth = EVP_PKEY_asn1_find_str(&e, nm, slen); | 220 | ameth = EVP_PKEY_asn1_find_str(&e, nm, slen); |
223 | if (ameth) | 221 | if (ameth) { |
224 | { | ||
225 | int r; | 222 | int r; |
226 | if (ameth->param_decode) | 223 | if (ameth->param_decode) |
227 | r = 1; | 224 | r = 1; |
@@ -232,68 +229,79 @@ static int check_pem(const char *nm, const char *name) | |||
232 | ENGINE_finish(e); | 229 | ENGINE_finish(e); |
233 | #endif | 230 | #endif |
234 | return r; | 231 | return r; |
235 | } | ||
236 | } | 232 | } |
237 | return 0; | ||
238 | } | 233 | } |
234 | return 0; | ||
235 | } | ||
239 | 236 | ||
240 | /* Permit older strings */ | 237 | /* Permit older strings */ |
241 | 238 | ||
242 | if(!strcmp(nm,PEM_STRING_X509_OLD) && | 239 | if (!strcmp(nm, PEM_STRING_X509_OLD) && |
243 | !strcmp(name,PEM_STRING_X509)) return 1; | 240 | !strcmp(name, PEM_STRING_X509)) |
241 | return 1; | ||
244 | 242 | ||
245 | if(!strcmp(nm,PEM_STRING_X509_REQ_OLD) && | 243 | if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) && |
246 | !strcmp(name,PEM_STRING_X509_REQ)) return 1; | 244 | !strcmp(name, PEM_STRING_X509_REQ)) |
245 | return 1; | ||
247 | 246 | ||
248 | /* Allow normal certs to be read as trusted certs */ | 247 | /* Allow normal certs to be read as trusted certs */ |
249 | if(!strcmp(nm,PEM_STRING_X509) && | 248 | if (!strcmp(nm, PEM_STRING_X509) && |
250 | !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1; | 249 | !strcmp(name, PEM_STRING_X509_TRUSTED)) |
250 | return 1; | ||
251 | 251 | ||
252 | if(!strcmp(nm,PEM_STRING_X509_OLD) && | 252 | if (!strcmp(nm, PEM_STRING_X509_OLD) && |
253 | !strcmp(name,PEM_STRING_X509_TRUSTED)) return 1; | 253 | !strcmp(name, PEM_STRING_X509_TRUSTED)) |
254 | return 1; | ||
254 | 255 | ||
255 | /* Some CAs use PKCS#7 with CERTIFICATE headers */ | 256 | /* Some CAs use PKCS#7 with CERTIFICATE headers */ |
256 | if(!strcmp(nm, PEM_STRING_X509) && | 257 | if (!strcmp(nm, PEM_STRING_X509) && |
257 | !strcmp(name, PEM_STRING_PKCS7)) return 1; | 258 | !strcmp(name, PEM_STRING_PKCS7)) |
259 | return 1; | ||
258 | 260 | ||
259 | if(!strcmp(nm, PEM_STRING_PKCS7_SIGNED) && | 261 | if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) && |
260 | !strcmp(name, PEM_STRING_PKCS7)) return 1; | 262 | !strcmp(name, PEM_STRING_PKCS7)) |
263 | return 1; | ||
261 | 264 | ||
262 | #ifndef OPENSSL_NO_CMS | 265 | #ifndef OPENSSL_NO_CMS |
263 | if(!strcmp(nm, PEM_STRING_X509) && | 266 | if (!strcmp(nm, PEM_STRING_X509) && |
264 | !strcmp(name, PEM_STRING_CMS)) return 1; | 267 | !strcmp(name, PEM_STRING_CMS)) |
268 | return 1; | ||
265 | /* Allow CMS to be read from PKCS#7 headers */ | 269 | /* Allow CMS to be read from PKCS#7 headers */ |
266 | if(!strcmp(nm, PEM_STRING_PKCS7) && | 270 | if (!strcmp(nm, PEM_STRING_PKCS7) && |
267 | !strcmp(name, PEM_STRING_CMS)) return 1; | 271 | !strcmp(name, PEM_STRING_CMS)) |
272 | return 1; | ||
268 | #endif | 273 | #endif |
269 | 274 | ||
270 | return 0; | 275 | return 0; |
271 | } | 276 | } |
272 | 277 | ||
273 | int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char *name, BIO *bp, | 278 | int |
274 | pem_password_cb *cb, void *u) | 279 | PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, |
275 | { | 280 | const char *name, BIO *bp, pem_password_cb *cb, void *u) |
281 | { | ||
276 | EVP_CIPHER_INFO cipher; | 282 | EVP_CIPHER_INFO cipher; |
277 | char *nm=NULL,*header=NULL; | 283 | char *nm = NULL, *header = NULL; |
278 | unsigned char *data=NULL; | 284 | unsigned char *data = NULL; |
279 | long len; | 285 | long len; |
280 | int ret = 0; | 286 | int ret = 0; |
281 | 287 | ||
282 | for (;;) | 288 | for (;;) { |
283 | { | 289 | if (!PEM_read_bio(bp, &nm, &header, &data, &len)) { |
284 | if (!PEM_read_bio(bp,&nm,&header,&data,&len)) { | 290 | if (ERR_GET_REASON(ERR_peek_error()) == |
285 | if(ERR_GET_REASON(ERR_peek_error()) == | 291 | PEM_R_NO_START_LINE) |
286 | PEM_R_NO_START_LINE) | ||
287 | ERR_add_error_data(2, "Expecting: ", name); | 292 | ERR_add_error_data(2, "Expecting: ", name); |
288 | return 0; | 293 | return 0; |
289 | } | 294 | } |
290 | if(check_pem(nm, name)) break; | 295 | if (check_pem(nm, name)) |
296 | break; | ||
291 | free(nm); | 297 | free(nm); |
292 | free(header); | 298 | free(header); |
293 | free(data); | 299 | free(data); |
294 | } | 300 | } |
295 | if (!PEM_get_EVP_CIPHER_INFO(header,&cipher)) goto err; | 301 | if (!PEM_get_EVP_CIPHER_INFO(header, &cipher)) |
296 | if (!PEM_do_header(&cipher,data,&len,cb,u)) goto err; | 302 | goto err; |
303 | if (!PEM_do_header(&cipher, data, &len, cb, u)) | ||
304 | goto err; | ||
297 | 305 | ||
298 | *pdata = data; | 306 | *pdata = data; |
299 | *plen = len; | 307 | *plen = len; |
@@ -304,532 +312,553 @@ int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, const char | |||
304 | ret = 1; | 312 | ret = 1; |
305 | 313 | ||
306 | err: | 314 | err: |
307 | if (!ret || !pnm) free(nm); | 315 | if (!ret || !pnm) |
316 | free(nm); | ||
308 | free(header); | 317 | free(header); |
309 | if (!ret) free(data); | 318 | if (!ret) |
319 | free(data); | ||
310 | return ret; | 320 | return ret; |
311 | } | 321 | } |
312 | 322 | ||
313 | #ifndef OPENSSL_NO_FP_API | 323 | #ifndef OPENSSL_NO_FP_API |
314 | int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, | 324 | int |
315 | void *x, const EVP_CIPHER *enc, unsigned char *kstr, | 325 | PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, |
316 | int klen, pem_password_cb *callback, void *u) | 326 | const EVP_CIPHER *enc, unsigned char *kstr, int klen, |
317 | { | 327 | pem_password_cb *callback, void *u) |
318 | BIO *b; | 328 | { |
319 | int ret; | 329 | BIO *b; |
320 | 330 | int ret; | |
321 | if ((b=BIO_new(BIO_s_file())) == NULL) | 331 | |
322 | { | 332 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
323 | PEMerr(PEM_F_PEM_ASN1_WRITE,ERR_R_BUF_LIB); | 333 | PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB); |
324 | return(0); | 334 | return (0); |
325 | } | 335 | } |
326 | BIO_set_fp(b,fp,BIO_NOCLOSE); | 336 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
327 | ret=PEM_ASN1_write_bio(i2d,name,b,x,enc,kstr,klen,callback,u); | 337 | ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u); |
328 | BIO_free(b); | 338 | BIO_free(b); |
329 | return(ret); | 339 | return (ret); |
330 | } | 340 | } |
331 | #endif | 341 | #endif |
332 | 342 | ||
333 | int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, | 343 | int |
334 | void *x, const EVP_CIPHER *enc, unsigned char *kstr, | 344 | PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, |
335 | int klen, pem_password_cb *callback, void *u) | 345 | const EVP_CIPHER *enc, unsigned char *kstr, int klen, |
336 | { | 346 | pem_password_cb *callback, void *u) |
347 | { | ||
337 | EVP_CIPHER_CTX ctx; | 348 | EVP_CIPHER_CTX ctx; |
338 | int dsize=0,i,j,ret=0; | 349 | int dsize = 0, i, j, ret = 0; |
339 | unsigned char *p,*data=NULL; | 350 | unsigned char *p, *data = NULL; |
340 | const char *objstr=NULL; | 351 | const char *objstr = NULL; |
341 | char buf[PEM_BUFSIZE]; | 352 | char buf[PEM_BUFSIZE]; |
342 | unsigned char key[EVP_MAX_KEY_LENGTH]; | 353 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
343 | unsigned char iv[EVP_MAX_IV_LENGTH]; | 354 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
344 | 355 | ||
345 | if (enc != NULL) | 356 | if (enc != NULL) { |
346 | { | 357 | objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); |
347 | objstr=OBJ_nid2sn(EVP_CIPHER_nid(enc)); | 358 | if (objstr == NULL) { |
348 | if (objstr == NULL) | 359 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, |
349 | { | 360 | PEM_R_UNSUPPORTED_CIPHER); |
350 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_UNSUPPORTED_CIPHER); | ||
351 | goto err; | 361 | goto err; |
352 | } | ||
353 | } | 362 | } |
363 | } | ||
354 | 364 | ||
355 | if ((dsize=i2d(x,NULL)) < 0) | 365 | if ((dsize = i2d(x, NULL)) < 0) { |
356 | { | 366 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB); |
357 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_ASN1_LIB); | 367 | dsize = 0; |
358 | dsize=0; | ||
359 | goto err; | 368 | goto err; |
360 | } | 369 | } |
361 | /* dzise + 8 bytes are needed */ | 370 | /* dzise + 8 bytes are needed */ |
362 | /* actually it needs the cipher block size extra... */ | 371 | /* actually it needs the cipher block size extra... */ |
363 | data=(unsigned char *)malloc((unsigned int)dsize+20); | 372 | data = (unsigned char *)malloc((unsigned int)dsize + 20); |
364 | if (data == NULL) | 373 | if (data == NULL) { |
365 | { | 374 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE); |
366 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,ERR_R_MALLOC_FAILURE); | ||
367 | goto err; | 375 | goto err; |
368 | } | 376 | } |
369 | p=data; | 377 | p = data; |
370 | i=i2d(x,&p); | 378 | i = i2d(x, &p); |
371 | 379 | ||
372 | if (enc != NULL) | 380 | if (enc != NULL) { |
373 | { | 381 | if (kstr == NULL) { |
374 | if (kstr == NULL) | ||
375 | { | ||
376 | if (callback == NULL) | 382 | if (callback == NULL) |
377 | klen=PEM_def_callback(buf,PEM_BUFSIZE,1,u); | 383 | klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u); |
378 | else | 384 | else |
379 | klen=(*callback)(buf,PEM_BUFSIZE,1,u); | 385 | klen = (*callback)(buf, PEM_BUFSIZE, 1, u); |
380 | if (klen <= 0) | 386 | if (klen <= 0) { |
381 | { | 387 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, |
382 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO,PEM_R_READ_KEY); | 388 | PEM_R_READ_KEY); |
383 | goto err; | 389 | goto err; |
384 | } | ||
385 | kstr=(unsigned char *)buf; | ||
386 | } | 390 | } |
391 | kstr = (unsigned char *)buf; | ||
392 | } | ||
387 | OPENSSL_assert(enc->iv_len <= (int)sizeof(iv)); | 393 | OPENSSL_assert(enc->iv_len <= (int)sizeof(iv)); |
388 | if (RAND_pseudo_bytes(iv,enc->iv_len) < 0) /* Generate a salt */ | 394 | if (RAND_pseudo_bytes(iv, enc->iv_len) < 0) /* Generate a salt */ |
389 | goto err; | 395 | goto err; |
390 | /* The 'iv' is used as the iv and as a salt. It is | 396 | /* The 'iv' is used as the iv and as a salt. It is |
391 | * NOT taken from the BytesToKey function */ | 397 | * NOT taken from the BytesToKey function */ |
392 | if (!EVP_BytesToKey(enc,EVP_md5(),iv,kstr,klen,1,key,NULL)) | 398 | if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, |
399 | key, NULL)) | ||
393 | goto err; | 400 | goto err; |
394 | 401 | ||
395 | if (kstr == (unsigned char *)buf) OPENSSL_cleanse(buf,PEM_BUFSIZE); | 402 | if (kstr == (unsigned char *)buf) |
403 | OPENSSL_cleanse(buf, PEM_BUFSIZE); | ||
396 | 404 | ||
397 | OPENSSL_assert(strlen(objstr)+23+2*enc->iv_len+13 <= sizeof buf); | 405 | OPENSSL_assert(strlen(objstr) + 23 + |
406 | 2 * enc->iv_len + 13 <= sizeof buf); | ||
398 | 407 | ||
399 | buf[0]='\0'; | 408 | buf[0] = '\0'; |
400 | PEM_proc_type(buf,PEM_TYPE_ENCRYPTED); | 409 | PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); |
401 | PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv); | 410 | PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv); |
402 | /* k=strlen(buf); */ | 411 | /* k=strlen(buf); */ |
403 | 412 | ||
404 | EVP_CIPHER_CTX_init(&ctx); | 413 | EVP_CIPHER_CTX_init(&ctx); |
405 | ret = 1; | 414 | ret = 1; |
406 | if (!EVP_EncryptInit_ex(&ctx,enc,NULL,key,iv) | 415 | if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv) || |
407 | || !EVP_EncryptUpdate(&ctx,data,&j,data,i) | 416 | !EVP_EncryptUpdate(&ctx, data, &j, data, i) || |
408 | || !EVP_EncryptFinal_ex(&ctx,&(data[j]),&i)) | 417 | !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i)) |
409 | ret = 0; | 418 | ret = 0; |
410 | EVP_CIPHER_CTX_cleanup(&ctx); | 419 | EVP_CIPHER_CTX_cleanup(&ctx); |
411 | if (ret == 0) | 420 | if (ret == 0) |
412 | goto err; | 421 | goto err; |
413 | i+=j; | 422 | i += j; |
414 | } | 423 | } else { |
415 | else | 424 | ret = 1; |
416 | { | 425 | buf[0] = '\0'; |
417 | ret=1; | 426 | } |
418 | buf[0]='\0'; | 427 | i = PEM_write_bio(bp, name, buf, data, i); |
419 | } | 428 | if (i <= 0) |
420 | i=PEM_write_bio(bp,name,buf,data,i); | 429 | ret = 0; |
421 | if (i <= 0) ret=0; | ||
422 | err: | 430 | err: |
423 | OPENSSL_cleanse(key,sizeof(key)); | 431 | OPENSSL_cleanse(key, sizeof(key)); |
424 | OPENSSL_cleanse(iv,sizeof(iv)); | 432 | OPENSSL_cleanse(iv, sizeof(iv)); |
425 | OPENSSL_cleanse((char *)&ctx,sizeof(ctx)); | 433 | OPENSSL_cleanse((char *)&ctx, sizeof(ctx)); |
426 | OPENSSL_cleanse(buf,PEM_BUFSIZE); | 434 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
427 | if (data != NULL) | 435 | if (data != NULL) { |
428 | { | 436 | OPENSSL_cleanse(data, (unsigned int)dsize); |
429 | OPENSSL_cleanse(data,(unsigned int)dsize); | ||
430 | free(data); | 437 | free(data); |
431 | } | ||
432 | return(ret); | ||
433 | } | 438 | } |
439 | return (ret); | ||
440 | } | ||
434 | 441 | ||
435 | int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, | 442 | int |
436 | pem_password_cb *callback,void *u) | 443 | PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, |
437 | { | 444 | pem_password_cb *callback, void *u) |
438 | int i,j,o,klen; | 445 | { |
446 | int i, j, o, klen; | ||
439 | long len; | 447 | long len; |
440 | EVP_CIPHER_CTX ctx; | 448 | EVP_CIPHER_CTX ctx; |
441 | unsigned char key[EVP_MAX_KEY_LENGTH]; | 449 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
442 | char buf[PEM_BUFSIZE]; | 450 | char buf[PEM_BUFSIZE]; |
443 | 451 | ||
444 | len= *plen; | 452 | len = *plen; |
445 | 453 | ||
446 | if (cipher->cipher == NULL) return(1); | 454 | if (cipher->cipher == NULL) |
455 | return (1); | ||
447 | if (callback == NULL) | 456 | if (callback == NULL) |
448 | klen=PEM_def_callback(buf,PEM_BUFSIZE,0,u); | 457 | klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u); |
449 | else | 458 | else |
450 | klen=callback(buf,PEM_BUFSIZE,0,u); | 459 | klen = callback(buf, PEM_BUFSIZE, 0, u); |
451 | if (klen <= 0) | 460 | if (klen <= 0) { |
452 | { | 461 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ); |
453 | PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_PASSWORD_READ); | 462 | return (0); |
454 | return(0); | 463 | } |
455 | } | 464 | if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]), |
456 | if (!EVP_BytesToKey(cipher->cipher,EVP_md5(),&(cipher->iv[0]), | 465 | (unsigned char *)buf, klen, 1, key, NULL)) |
457 | (unsigned char *)buf,klen,1,key,NULL)) | ||
458 | return 0; | 466 | return 0; |
459 | 467 | ||
460 | j=(int)len; | 468 | j = (int)len; |
461 | EVP_CIPHER_CTX_init(&ctx); | 469 | EVP_CIPHER_CTX_init(&ctx); |
462 | o = EVP_DecryptInit_ex(&ctx,cipher->cipher,NULL, key,&(cipher->iv[0])); | 470 | o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key, |
471 | &(cipher->iv[0])); | ||
463 | if (o) | 472 | if (o) |
464 | o = EVP_DecryptUpdate(&ctx,data,&i,data,j); | 473 | o = EVP_DecryptUpdate(&ctx, data, &i, data, j); |
465 | if (o) | 474 | if (o) |
466 | o = EVP_DecryptFinal_ex(&ctx,&(data[i]),&j); | 475 | o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j); |
467 | EVP_CIPHER_CTX_cleanup(&ctx); | 476 | EVP_CIPHER_CTX_cleanup(&ctx); |
468 | OPENSSL_cleanse((char *)buf,sizeof(buf)); | 477 | OPENSSL_cleanse((char *)buf, sizeof(buf)); |
469 | OPENSSL_cleanse((char *)key,sizeof(key)); | 478 | OPENSSL_cleanse((char *)key, sizeof(key)); |
470 | j+=i; | 479 | j += i; |
471 | if (!o) | 480 | if (!o) { |
472 | { | 481 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT); |
473 | PEMerr(PEM_F_PEM_DO_HEADER,PEM_R_BAD_DECRYPT); | 482 | return (0); |
474 | return(0); | ||
475 | } | ||
476 | *plen=j; | ||
477 | return(1); | ||
478 | } | 483 | } |
484 | *plen = j; | ||
485 | return (1); | ||
486 | } | ||
479 | 487 | ||
480 | int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) | 488 | int |
481 | { | 489 | PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) |
482 | const EVP_CIPHER *enc=NULL; | 490 | { |
483 | char *p,c; | 491 | const EVP_CIPHER *enc = NULL; |
492 | char *p, c; | ||
484 | char **header_pp = &header; | 493 | char **header_pp = &header; |
485 | 494 | ||
486 | cipher->cipher=NULL; | 495 | cipher->cipher = NULL; |
487 | if ((header == NULL) || (*header == '\0') || (*header == '\n')) | 496 | if ((header == NULL) || (*header == '\0') || (*header == '\n')) |
488 | return(1); | 497 | return (1); |
489 | if (strncmp(header,"Proc-Type: ",11) != 0) | 498 | if (strncmp(header, "Proc-Type: ", 11) != 0) { |
490 | { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_PROC_TYPE); return(0); } | 499 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE); |
491 | header+=11; | 500 | return (0); |
492 | if (*header != '4') return(0); header++; | 501 | } |
493 | if (*header != ',') return(0); header++; | 502 | header += 11; |
494 | if (strncmp(header,"ENCRYPTED",9) != 0) | 503 | if (*header != '4') |
495 | { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_ENCRYPTED); return(0); } | 504 | return(0); |
505 | header++; | ||
506 | if (*header != ',') | ||
507 | return(0); | ||
508 | header++; | ||
509 | if (strncmp(header, "ENCRYPTED", 9) != 0) { | ||
510 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED); | ||
511 | return (0); | ||
512 | } | ||
496 | for (; (*header != '\n') && (*header != '\0'); header++) | 513 | for (; (*header != '\n') && (*header != '\0'); header++) |
497 | ; | 514 | ; |
498 | if (*header == '\0') | 515 | if (*header == '\0') { |
499 | { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_SHORT_HEADER); return(0); } | 516 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER); |
517 | return (0); | ||
518 | } | ||
500 | header++; | 519 | header++; |
501 | if (strncmp(header,"DEK-Info: ",10) != 0) | 520 | if (strncmp(header, "DEK-Info: ", 10) != 0) { |
502 | { PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_NOT_DEK_INFO); return(0); } | 521 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO); |
503 | header+=10; | 522 | return (0); |
523 | } | ||
524 | header += 10; | ||
504 | 525 | ||
505 | p=header; | 526 | p = header; |
506 | for (;;) | 527 | for (;;) { |
507 | { | ||
508 | c= *header; | 528 | c= *header; |
509 | if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || | 529 | if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || |
510 | ((c >= '0') && (c <= '9')))) | 530 | ((c >= '0') && (c <= '9')))) |
511 | break; | 531 | break; |
512 | header++; | 532 | header++; |
513 | } | 533 | } |
514 | *header='\0'; | 534 | *header = '\0'; |
515 | cipher->cipher=enc=EVP_get_cipherbyname(p); | 535 | cipher->cipher = enc = EVP_get_cipherbyname(p); |
516 | *header=c; | 536 | *header = c; |
517 | header++; | 537 | header++; |
518 | 538 | ||
519 | if (enc == NULL) | 539 | if (enc == NULL) { |
520 | { | 540 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, |
521 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO,PEM_R_UNSUPPORTED_ENCRYPTION); | 541 | PEM_R_UNSUPPORTED_ENCRYPTION); |
522 | return(0); | 542 | return (0); |
523 | } | ||
524 | if (!load_iv(header_pp,&(cipher->iv[0]),enc->iv_len)) | ||
525 | return(0); | ||
526 | |||
527 | return(1); | ||
528 | } | 543 | } |
544 | if (!load_iv(header_pp, &(cipher->iv[0]), enc->iv_len)) | ||
545 | return (0); | ||
546 | |||
547 | return (1); | ||
548 | } | ||
529 | 549 | ||
530 | static int load_iv(char **fromp, unsigned char *to, int num) | 550 | static int |
531 | { | 551 | load_iv(char **fromp, unsigned char *to, int num) |
532 | int v,i; | 552 | { |
553 | int v, i; | ||
533 | char *from; | 554 | char *from; |
534 | 555 | ||
535 | from= *fromp; | 556 | from= *fromp; |
536 | for (i=0; i<num; i++) to[i]=0; | 557 | for (i = 0; i < num; i++) |
537 | num*=2; | 558 | to[i] = 0; |
538 | for (i=0; i<num; i++) | 559 | num *= 2; |
539 | { | 560 | for (i = 0; i < num; i++) { |
540 | if ((*from >= '0') && (*from <= '9')) | 561 | if ((*from >= '0') && (*from <= '9')) |
541 | v= *from-'0'; | 562 | v = *from - '0'; |
542 | else if ((*from >= 'A') && (*from <= 'F')) | 563 | else if ((*from >= 'A') && (*from <= 'F')) |
543 | v= *from-'A'+10; | 564 | v = *from - 'A' + 10; |
544 | else if ((*from >= 'a') && (*from <= 'f')) | 565 | else if ((*from >= 'a') && (*from <= 'f')) |
545 | v= *from-'a'+10; | 566 | v = *from - 'a' + 10; |
546 | else | 567 | else { |
547 | { | 568 | PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS); |
548 | PEMerr(PEM_F_LOAD_IV,PEM_R_BAD_IV_CHARS); | 569 | return (0); |
549 | return(0); | ||
550 | } | ||
551 | from++; | ||
552 | to[i/2]|=v<<(long)((!(i&1))*4); | ||
553 | } | 570 | } |
554 | 571 | from++; | |
555 | *fromp=from; | 572 | to[i / 2] |= v << (long)((!(i & 1)) * 4); |
556 | return(1); | ||
557 | } | 573 | } |
558 | 574 | ||
575 | *fromp = from; | ||
576 | return (1); | ||
577 | } | ||
578 | |||
559 | #ifndef OPENSSL_NO_FP_API | 579 | #ifndef OPENSSL_NO_FP_API |
560 | int PEM_write(FILE *fp, char *name, char *header, unsigned char *data, | 580 | int |
561 | long len) | 581 | PEM_write(FILE *fp, char *name, char *header, unsigned char *data, long len) |
562 | { | 582 | { |
563 | BIO *b; | 583 | BIO *b; |
564 | int ret; | 584 | int ret; |
565 | 585 | ||
566 | if ((b=BIO_new(BIO_s_file())) == NULL) | 586 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
567 | { | 587 | PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB); |
568 | PEMerr(PEM_F_PEM_WRITE,ERR_R_BUF_LIB); | 588 | return (0); |
569 | return(0); | 589 | } |
570 | } | 590 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
571 | BIO_set_fp(b,fp,BIO_NOCLOSE); | 591 | ret = PEM_write_bio(b, name, header, data, len); |
572 | ret=PEM_write_bio(b, name, header, data,len); | 592 | BIO_free(b); |
573 | BIO_free(b); | 593 | return (ret); |
574 | return(ret); | 594 | } |
575 | } | ||
576 | #endif | 595 | #endif |
577 | 596 | ||
578 | int PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data, | 597 | int |
579 | long len) | 598 | PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data, |
580 | { | 599 | long len) |
581 | int nlen,n,i,j,outl; | 600 | { |
601 | int nlen, n, i, j, outl; | ||
582 | unsigned char *buf = NULL; | 602 | unsigned char *buf = NULL; |
583 | EVP_ENCODE_CTX ctx; | 603 | EVP_ENCODE_CTX ctx; |
584 | int reason=ERR_R_BUF_LIB; | 604 | int reason = ERR_R_BUF_LIB; |
585 | 605 | ||
586 | EVP_EncodeInit(&ctx); | 606 | EVP_EncodeInit(&ctx); |
587 | nlen=strlen(name); | 607 | nlen = strlen(name); |
588 | 608 | ||
589 | if ( (BIO_write(bp,"-----BEGIN ",11) != 11) || | 609 | if ((BIO_write(bp, "-----BEGIN ", 11) != 11) || |
590 | (BIO_write(bp,name,nlen) != nlen) || | 610 | (BIO_write(bp, name, nlen) != nlen) || |
591 | (BIO_write(bp,"-----\n",6) != 6)) | 611 | (BIO_write(bp, "-----\n", 6) != 6)) |
592 | goto err; | 612 | goto err; |
593 | 613 | ||
594 | i=strlen(header); | 614 | i = strlen(header); |
595 | if (i > 0) | 615 | if (i > 0) { |
596 | { | 616 | if ((BIO_write(bp, header, i) != i) || |
597 | if ( (BIO_write(bp,header,i) != i) || | 617 | (BIO_write(bp, "\n", 1) != 1)) |
598 | (BIO_write(bp,"\n",1) != 1)) | ||
599 | goto err; | 618 | goto err; |
600 | } | 619 | } |
601 | 620 | ||
602 | buf = malloc(PEM_BUFSIZE*8); | 621 | buf = malloc(PEM_BUFSIZE * 8); |
603 | if (buf == NULL) | 622 | if (buf == NULL) { |
604 | { | 623 | reason = ERR_R_MALLOC_FAILURE; |
605 | reason=ERR_R_MALLOC_FAILURE; | ||
606 | goto err; | 624 | goto err; |
607 | } | 625 | } |
608 | 626 | ||
609 | i=j=0; | 627 | i = j = 0; |
610 | while (len > 0) | 628 | while (len > 0) { |
611 | { | 629 | n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len); |
612 | n=(int)((len>(PEM_BUFSIZE*5))?(PEM_BUFSIZE*5):len); | 630 | EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n); |
613 | EVP_EncodeUpdate(&ctx,buf,&outl,&(data[j]),n); | 631 | if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) |
614 | if ((outl) && (BIO_write(bp,(char *)buf,outl) != outl)) | ||
615 | goto err; | 632 | goto err; |
616 | i+=outl; | 633 | i += outl; |
617 | len-=n; | 634 | len -= n; |
618 | j+=n; | 635 | j += n; |
619 | } | 636 | } |
620 | EVP_EncodeFinal(&ctx,buf,&outl); | 637 | EVP_EncodeFinal(&ctx, buf, &outl); |
621 | if ((outl > 0) && (BIO_write(bp,(char *)buf,outl) != outl)) goto err; | 638 | if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) |
622 | OPENSSL_cleanse(buf, PEM_BUFSIZE*8); | 639 | goto err; |
640 | OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); | ||
623 | free(buf); | 641 | free(buf); |
624 | buf = NULL; | 642 | buf = NULL; |
625 | if ( (BIO_write(bp,"-----END ",9) != 9) || | 643 | if ((BIO_write(bp, "-----END ", 9) != 9) || |
626 | (BIO_write(bp,name,nlen) != nlen) || | 644 | (BIO_write(bp, name, nlen) != nlen) || |
627 | (BIO_write(bp,"-----\n",6) != 6)) | 645 | (BIO_write(bp, "-----\n", 6) != 6)) |
628 | goto err; | 646 | goto err; |
629 | return(i+outl); | 647 | return (i + outl); |
648 | |||
630 | err: | 649 | err: |
631 | if (buf) { | 650 | if (buf) { |
632 | OPENSSL_cleanse(buf, PEM_BUFSIZE*8); | 651 | OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); |
633 | free(buf); | 652 | free(buf); |
634 | } | 653 | } |
635 | PEMerr(PEM_F_PEM_WRITE_BIO,reason); | 654 | PEMerr(PEM_F_PEM_WRITE_BIO, reason); |
636 | return(0); | 655 | return (0); |
637 | } | 656 | } |
638 | 657 | ||
639 | #ifndef OPENSSL_NO_FP_API | 658 | #ifndef OPENSSL_NO_FP_API |
640 | int PEM_read(FILE *fp, char **name, char **header, unsigned char **data, | 659 | int |
641 | long *len) | 660 | PEM_read(FILE *fp, char **name, char **header, unsigned char **data, long *len) |
642 | { | 661 | { |
643 | BIO *b; | 662 | BIO *b; |
644 | int ret; | 663 | int ret; |
645 | 664 | ||
646 | if ((b=BIO_new(BIO_s_file())) == NULL) | 665 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
647 | { | 666 | PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB); |
648 | PEMerr(PEM_F_PEM_READ,ERR_R_BUF_LIB); | 667 | return (0); |
649 | return(0); | 668 | } |
650 | } | 669 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
651 | BIO_set_fp(b,fp,BIO_NOCLOSE); | 670 | ret = PEM_read_bio(b, name, header, data, len); |
652 | ret=PEM_read_bio(b, name, header, data,len); | 671 | BIO_free(b); |
653 | BIO_free(b); | 672 | return (ret); |
654 | return(ret); | 673 | } |
655 | } | ||
656 | #endif | 674 | #endif |
657 | 675 | ||
658 | int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, | 676 | int |
659 | long *len) | 677 | PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, |
660 | { | 678 | long *len) |
679 | { | ||
661 | EVP_ENCODE_CTX ctx; | 680 | EVP_ENCODE_CTX ctx; |
662 | int end=0,i,k,bl=0,hl=0,nohead=0; | 681 | int end = 0, i, k, bl = 0, hl = 0, nohead = 0; |
663 | char buf[256]; | 682 | char buf[256]; |
664 | BUF_MEM *nameB; | 683 | BUF_MEM *nameB; |
665 | BUF_MEM *headerB; | 684 | BUF_MEM *headerB; |
666 | BUF_MEM *dataB,*tmpB; | 685 | BUF_MEM *dataB, *tmpB; |
667 | 686 | ||
668 | nameB=BUF_MEM_new(); | 687 | nameB = BUF_MEM_new(); |
669 | headerB=BUF_MEM_new(); | 688 | headerB = BUF_MEM_new(); |
670 | dataB=BUF_MEM_new(); | 689 | dataB = BUF_MEM_new(); |
671 | if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) | 690 | if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) { |
672 | { | ||
673 | BUF_MEM_free(nameB); | 691 | BUF_MEM_free(nameB); |
674 | BUF_MEM_free(headerB); | 692 | BUF_MEM_free(headerB); |
675 | BUF_MEM_free(dataB); | 693 | BUF_MEM_free(dataB); |
676 | PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); | 694 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); |
677 | return(0); | 695 | return (0); |
678 | } | 696 | } |
679 | 697 | ||
680 | buf[254]='\0'; | 698 | buf[254] = '\0'; |
681 | for (;;) | 699 | for (;;) { |
682 | { | 700 | i = BIO_gets(bp, buf, 254); |
683 | i=BIO_gets(bp,buf,254); | ||
684 | 701 | ||
685 | if (i <= 0) | 702 | if (i <= 0) { |
686 | { | 703 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE); |
687 | PEMerr(PEM_F_PEM_READ_BIO,PEM_R_NO_START_LINE); | ||
688 | goto err; | 704 | goto err; |
689 | } | 705 | } |
690 | 706 | ||
691 | while ((i >= 0) && (buf[i] <= ' ')) i--; | 707 | while ((i >= 0) && (buf[i] <= ' ')) |
692 | buf[++i]='\n'; buf[++i]='\0'; | 708 | i--; |
709 | buf[++i] = '\n'; | ||
710 | buf[++i] = '\0'; | ||
693 | 711 | ||
694 | if (strncmp(buf,"-----BEGIN ",11) == 0) | 712 | if (strncmp(buf, "-----BEGIN ", 11) == 0) { |
695 | { | 713 | i = strlen(&(buf[11])); |
696 | i=strlen(&(buf[11])); | ||
697 | 714 | ||
698 | if (strncmp(&(buf[11+i-6]),"-----\n",6) != 0) | 715 | if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0) |
699 | continue; | 716 | continue; |
700 | if (!BUF_MEM_grow(nameB,i+9)) | 717 | if (!BUF_MEM_grow(nameB, i + 9)) { |
701 | { | 718 | PEMerr(PEM_F_PEM_READ_BIO, |
702 | PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); | 719 | ERR_R_MALLOC_FAILURE); |
703 | goto err; | 720 | goto err; |
704 | } | ||
705 | memcpy(nameB->data,&(buf[11]),i-6); | ||
706 | nameB->data[i-6]='\0'; | ||
707 | break; | ||
708 | } | 721 | } |
722 | memcpy(nameB->data, &(buf[11]), i - 6); | ||
723 | nameB->data[i - 6] = '\0'; | ||
724 | break; | ||
709 | } | 725 | } |
710 | hl=0; | 726 | } |
711 | if (!BUF_MEM_grow(headerB,256)) | 727 | hl = 0; |
712 | { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } | 728 | if (!BUF_MEM_grow(headerB, 256)) { |
713 | headerB->data[0]='\0'; | 729 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); |
714 | for (;;) | 730 | goto err; |
715 | { | 731 | } |
716 | i=BIO_gets(bp,buf,254); | 732 | headerB->data[0] = '\0'; |
717 | if (i <= 0) break; | 733 | for (;;) { |
718 | 734 | i = BIO_gets(bp, buf, 254); | |
719 | while ((i >= 0) && (buf[i] <= ' ')) i--; | 735 | if (i <= 0) |
720 | buf[++i]='\n'; buf[++i]='\0'; | ||
721 | |||
722 | if (buf[0] == '\n') break; | ||
723 | if (!BUF_MEM_grow(headerB,hl+i+9)) | ||
724 | { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } | ||
725 | if (strncmp(buf,"-----END ",9) == 0) | ||
726 | { | ||
727 | nohead=1; | ||
728 | break; | 736 | break; |
729 | } | 737 | |
730 | memcpy(&(headerB->data[hl]),buf,i); | 738 | while ((i >= 0) && (buf[i] <= ' ')) |
731 | headerB->data[hl+i]='\0'; | 739 | i--; |
732 | hl+=i; | 740 | buf[++i] = '\n'; |
741 | buf[++i] = '\0'; | ||
742 | |||
743 | if (buf[0] == '\n') | ||
744 | break; | ||
745 | if (!BUF_MEM_grow(headerB, hl + i + 9)) { | ||
746 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
747 | goto err; | ||
733 | } | 748 | } |
749 | if (strncmp(buf, "-----END ", 9) == 0) { | ||
750 | nohead = 1; | ||
751 | break; | ||
752 | } | ||
753 | memcpy(&(headerB->data[hl]), buf, i); | ||
754 | headerB->data[hl + i] = '\0'; | ||
755 | hl += i; | ||
756 | } | ||
757 | |||
758 | bl = 0; | ||
759 | if (!BUF_MEM_grow(dataB, 1024)) { | ||
760 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
761 | goto err; | ||
762 | } | ||
763 | dataB->data[0] = '\0'; | ||
764 | if (!nohead) { | ||
765 | for (;;) { | ||
766 | i = BIO_gets(bp, buf, 254); | ||
767 | if (i <= 0) | ||
768 | break; | ||
769 | |||
770 | while ((i >= 0) && (buf[i] <= ' ')) | ||
771 | i--; | ||
772 | buf[++i] = '\n'; | ||
773 | buf[++i] = '\0'; | ||
734 | 774 | ||
735 | bl=0; | 775 | if (i != 65) |
736 | if (!BUF_MEM_grow(dataB,1024)) | 776 | end = 1; |
737 | { PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); goto err; } | 777 | if (strncmp(buf, "-----END ", 9) == 0) |
738 | dataB->data[0]='\0'; | ||
739 | if (!nohead) | ||
740 | { | ||
741 | for (;;) | ||
742 | { | ||
743 | i=BIO_gets(bp,buf,254); | ||
744 | if (i <= 0) break; | ||
745 | |||
746 | while ((i >= 0) && (buf[i] <= ' ')) i--; | ||
747 | buf[++i]='\n'; buf[++i]='\0'; | ||
748 | |||
749 | if (i != 65) end=1; | ||
750 | if (strncmp(buf,"-----END ",9) == 0) | ||
751 | break; | 778 | break; |
752 | if (i > 65) break; | 779 | if (i > 65) |
753 | if (!BUF_MEM_grow_clean(dataB,i+bl+9)) | 780 | break; |
754 | { | 781 | if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) { |
755 | PEMerr(PEM_F_PEM_READ_BIO,ERR_R_MALLOC_FAILURE); | 782 | PEMerr(PEM_F_PEM_READ_BIO, |
783 | ERR_R_MALLOC_FAILURE); | ||
756 | goto err; | 784 | goto err; |
757 | } | 785 | } |
758 | memcpy(&(dataB->data[bl]),buf,i); | 786 | memcpy(&(dataB->data[bl]), buf, i); |
759 | dataB->data[bl+i]='\0'; | 787 | dataB->data[bl + i] = '\0'; |
760 | bl+=i; | 788 | bl += i; |
761 | if (end) | 789 | if (end) { |
762 | { | 790 | buf[0] = '\0'; |
763 | buf[0]='\0'; | 791 | i = BIO_gets(bp, buf, 254); |
764 | i=BIO_gets(bp,buf,254); | 792 | if (i <= 0) |
765 | if (i <= 0) break; | 793 | break; |
766 | 794 | ||
767 | while ((i >= 0) && (buf[i] <= ' ')) i--; | 795 | while ((i >= 0) && (buf[i] <= ' ')) |
768 | buf[++i]='\n'; buf[++i]='\0'; | 796 | i--; |
797 | buf[++i] = '\n'; | ||
798 | buf[++i] = '\0'; | ||
769 | 799 | ||
770 | break; | 800 | break; |
771 | } | ||
772 | } | 801 | } |
773 | } | 802 | } |
774 | else | 803 | } else { |
775 | { | 804 | tmpB = headerB; |
776 | tmpB=headerB; | 805 | headerB = dataB; |
777 | headerB=dataB; | 806 | dataB = tmpB; |
778 | dataB=tmpB; | 807 | bl = hl; |
779 | bl=hl; | 808 | } |
780 | } | 809 | i = strlen(nameB->data); |
781 | i=strlen(nameB->data); | 810 | if ((strncmp(buf, "-----END ", 9) != 0) || |
782 | if ( (strncmp(buf,"-----END ",9) != 0) || | 811 | (strncmp(nameB->data, &(buf[9]), i) != 0) || |
783 | (strncmp(nameB->data,&(buf[9]),i) != 0) || | 812 | (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) { |
784 | (strncmp(&(buf[9+i]),"-----\n",6) != 0)) | 813 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE); |
785 | { | ||
786 | PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_END_LINE); | ||
787 | goto err; | 814 | goto err; |
788 | } | 815 | } |
789 | 816 | ||
790 | EVP_DecodeInit(&ctx); | 817 | EVP_DecodeInit(&ctx); |
791 | i=EVP_DecodeUpdate(&ctx, | 818 | i = EVP_DecodeUpdate(&ctx, |
792 | (unsigned char *)dataB->data,&bl, | 819 | (unsigned char *)dataB->data, &bl, |
793 | (unsigned char *)dataB->data,bl); | 820 | (unsigned char *)dataB->data, bl); |
794 | if (i < 0) | 821 | if (i < 0) { |
795 | { | 822 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); |
796 | PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE); | ||
797 | goto err; | 823 | goto err; |
798 | } | 824 | } |
799 | i=EVP_DecodeFinal(&ctx,(unsigned char *)&(dataB->data[bl]),&k); | 825 | i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k); |
800 | if (i < 0) | 826 | if (i < 0) { |
801 | { | 827 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); |
802 | PEMerr(PEM_F_PEM_READ_BIO,PEM_R_BAD_BASE64_DECODE); | ||
803 | goto err; | 828 | goto err; |
804 | } | 829 | } |
805 | bl+=k; | 830 | bl += k; |
806 | 831 | ||
807 | if (bl == 0) goto err; | 832 | if (bl == 0) |
808 | *name=nameB->data; | 833 | goto err; |
809 | *header=headerB->data; | 834 | *name = nameB->data; |
810 | *data=(unsigned char *)dataB->data; | 835 | *header = headerB->data; |
811 | *len=bl; | 836 | *data = (unsigned char *)dataB->data; |
837 | *len = bl; | ||
812 | free(nameB); | 838 | free(nameB); |
813 | free(headerB); | 839 | free(headerB); |
814 | free(dataB); | 840 | free(dataB); |
815 | return(1); | 841 | return (1); |
842 | |||
816 | err: | 843 | err: |
817 | BUF_MEM_free(nameB); | 844 | BUF_MEM_free(nameB); |
818 | BUF_MEM_free(headerB); | 845 | BUF_MEM_free(headerB); |
819 | BUF_MEM_free(dataB); | 846 | BUF_MEM_free(dataB); |
820 | return(0); | 847 | return (0); |
821 | } | 848 | } |
822 | 849 | ||
823 | /* Check pem string and return prefix length. | 850 | /* Check pem string and return prefix length. |
824 | * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" | 851 | * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" |
825 | * the return value is 3 for the string "RSA". | 852 | * the return value is 3 for the string "RSA". |
826 | */ | 853 | */ |
827 | 854 | ||
828 | int pem_check_suffix(const char *pem_str, const char *suffix) | 855 | int |
829 | { | 856 | pem_check_suffix(const char *pem_str, const char *suffix) |
857 | { | ||
830 | int pem_len = strlen(pem_str); | 858 | int pem_len = strlen(pem_str); |
831 | int suffix_len = strlen(suffix); | 859 | int suffix_len = strlen(suffix); |
832 | const char *p; | 860 | const char *p; |
861 | |||
833 | if (suffix_len + 1 >= pem_len) | 862 | if (suffix_len + 1 >= pem_len) |
834 | return 0; | 863 | return 0; |
835 | p = pem_str + pem_len - suffix_len; | 864 | p = pem_str + pem_len - suffix_len; |
@@ -839,5 +868,4 @@ int pem_check_suffix(const char *pem_str, const char *suffix) | |||
839 | if (*p != ' ') | 868 | if (*p != ' ') |
840 | return 0; | 869 | return 0; |
841 | return p - pem_str; | 870 | return p - pem_str; |
842 | } | 871 | } |
843 | |||