diff options
Diffstat (limited to 'src/lib/libcrypto/evp/bio_enc.c')
-rw-r--r-- | src/lib/libcrypto/evp/bio_enc.c | 412 |
1 files changed, 206 insertions, 206 deletions
diff --git a/src/lib/libcrypto/evp/bio_enc.c b/src/lib/libcrypto/evp/bio_enc.c index 3362c25768..46cd2a6a60 100644 --- a/src/lib/libcrypto/evp/bio_enc.c +++ b/src/lib/libcrypto/evp/bio_enc.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 |
@@ -73,8 +73,7 @@ static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps); | |||
73 | #define ENC_BLOCK_SIZE (1024*4) | 73 | #define ENC_BLOCK_SIZE (1024*4) |
74 | #define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2) | 74 | #define BUF_OFFSET (EVP_MAX_BLOCK_LENGTH*2) |
75 | 75 | ||
76 | typedef struct enc_struct | 76 | typedef struct enc_struct { |
77 | { | ||
78 | int buf_len; | 77 | int buf_len; |
79 | int buf_off; | 78 | int buf_off; |
80 | int cont; /* <= 0 when finished */ | 79 | int cont; /* <= 0 when finished */ |
@@ -84,10 +83,10 @@ typedef struct enc_struct | |||
84 | /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate | 83 | /* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate |
85 | * can return up to a block more data than is presented to it | 84 | * can return up to a block more data than is presented to it |
86 | */ | 85 | */ |
87 | char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2]; | 86 | char buf[ENC_BLOCK_SIZE + BUF_OFFSET + 2]; |
88 | } BIO_ENC_CTX; | 87 | } BIO_ENC_CTX; |
89 | 88 | ||
90 | static BIO_METHOD methods_enc= { | 89 | static BIO_METHOD methods_enc = { |
91 | .type = BIO_TYPE_CIPHER, | 90 | .type = BIO_TYPE_CIPHER, |
92 | .name = "cipher", | 91 | .name = "cipher", |
93 | .bwrite = enc_write, | 92 | .bwrite = enc_write, |
@@ -98,292 +97,292 @@ static BIO_METHOD methods_enc= { | |||
98 | .callback_ctrl = enc_callback_ctrl | 97 | .callback_ctrl = enc_callback_ctrl |
99 | }; | 98 | }; |
100 | 99 | ||
101 | BIO_METHOD *BIO_f_cipher(void) | 100 | BIO_METHOD * |
102 | { | 101 | BIO_f_cipher(void) |
103 | return(&methods_enc); | 102 | { |
104 | } | 103 | return (&methods_enc); |
104 | } | ||
105 | 105 | ||
106 | static int enc_new(BIO *bi) | 106 | static int |
107 | { | 107 | enc_new(BIO *bi) |
108 | { | ||
108 | BIO_ENC_CTX *ctx; | 109 | BIO_ENC_CTX *ctx; |
109 | 110 | ||
110 | ctx=(BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX)); | 111 | ctx = (BIO_ENC_CTX *)malloc(sizeof(BIO_ENC_CTX)); |
111 | if (ctx == NULL) return(0); | 112 | if (ctx == NULL) |
113 | return (0); | ||
112 | EVP_CIPHER_CTX_init(&ctx->cipher); | 114 | EVP_CIPHER_CTX_init(&ctx->cipher); |
113 | 115 | ||
114 | ctx->buf_len=0; | 116 | ctx->buf_len = 0; |
115 | ctx->buf_off=0; | 117 | ctx->buf_off = 0; |
116 | ctx->cont=1; | 118 | ctx->cont = 1; |
117 | ctx->finished=0; | 119 | ctx->finished = 0; |
118 | ctx->ok=1; | 120 | ctx->ok = 1; |
119 | 121 | ||
120 | bi->init=0; | 122 | bi->init = 0; |
121 | bi->ptr=(char *)ctx; | 123 | bi->ptr = (char *)ctx; |
122 | bi->flags=0; | 124 | bi->flags = 0; |
123 | return(1); | 125 | return (1); |
124 | } | 126 | } |
125 | 127 | ||
126 | static int enc_free(BIO *a) | 128 | static int |
127 | { | 129 | enc_free(BIO *a) |
130 | { | ||
128 | BIO_ENC_CTX *b; | 131 | BIO_ENC_CTX *b; |
129 | 132 | ||
130 | if (a == NULL) return(0); | 133 | if (a == NULL) |
131 | b=(BIO_ENC_CTX *)a->ptr; | 134 | return (0); |
135 | b = (BIO_ENC_CTX *)a->ptr; | ||
132 | EVP_CIPHER_CTX_cleanup(&(b->cipher)); | 136 | EVP_CIPHER_CTX_cleanup(&(b->cipher)); |
133 | OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX)); | 137 | OPENSSL_cleanse(a->ptr, sizeof(BIO_ENC_CTX)); |
134 | free(a->ptr); | 138 | free(a->ptr); |
135 | a->ptr=NULL; | 139 | a->ptr = NULL; |
136 | a->init=0; | 140 | a->init = 0; |
137 | a->flags=0; | 141 | a->flags = 0; |
138 | return(1); | 142 | return (1); |
139 | } | 143 | } |
140 | 144 | ||
141 | static int enc_read(BIO *b, char *out, int outl) | 145 | static int |
142 | { | 146 | enc_read(BIO *b, char *out, int outl) |
143 | int ret=0,i; | 147 | { |
148 | int ret = 0, i; | ||
144 | BIO_ENC_CTX *ctx; | 149 | BIO_ENC_CTX *ctx; |
145 | 150 | ||
146 | if (out == NULL) return(0); | 151 | if (out == NULL) |
147 | ctx=(BIO_ENC_CTX *)b->ptr; | 152 | return (0); |
153 | ctx = (BIO_ENC_CTX *)b->ptr; | ||
148 | 154 | ||
149 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | 155 | if ((ctx == NULL) || (b->next_bio == NULL)) |
156 | return (0); | ||
150 | 157 | ||
151 | /* First check if there are bytes decoded/encoded */ | 158 | /* First check if there are bytes decoded/encoded */ |
152 | if (ctx->buf_len > 0) | 159 | if (ctx->buf_len > 0) { |
153 | { | 160 | i = ctx->buf_len - ctx->buf_off; |
154 | i=ctx->buf_len-ctx->buf_off; | 161 | if (i > outl) |
155 | if (i > outl) i=outl; | 162 | i = outl; |
156 | memcpy(out,&(ctx->buf[ctx->buf_off]),i); | 163 | memcpy(out, &(ctx->buf[ctx->buf_off]), i); |
157 | ret=i; | 164 | ret = i; |
158 | out+=i; | 165 | out += i; |
159 | outl-=i; | 166 | outl -= i; |
160 | ctx->buf_off+=i; | 167 | ctx->buf_off += i; |
161 | if (ctx->buf_len == ctx->buf_off) | 168 | if (ctx->buf_len == ctx->buf_off) { |
162 | { | 169 | ctx->buf_len = 0; |
163 | ctx->buf_len=0; | 170 | ctx->buf_off = 0; |
164 | ctx->buf_off=0; | ||
165 | } | ||
166 | } | 171 | } |
172 | } | ||
167 | 173 | ||
168 | /* At this point, we have room of outl bytes and an empty | 174 | /* At this point, we have room of outl bytes and an empty |
169 | * buffer, so we should read in some more. */ | 175 | * buffer, so we should read in some more. */ |
170 | 176 | ||
171 | while (outl > 0) | 177 | while (outl > 0) { |
172 | { | 178 | if (ctx->cont <= 0) |
173 | if (ctx->cont <= 0) break; | 179 | break; |
174 | 180 | ||
175 | /* read in at IV offset, read the EVP_Cipher | 181 | /* read in at IV offset, read the EVP_Cipher |
176 | * documentation about why */ | 182 | * documentation about why */ |
177 | i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE); | 183 | i = BIO_read(b->next_bio, &(ctx->buf[BUF_OFFSET]), ENC_BLOCK_SIZE); |
178 | 184 | ||
179 | if (i <= 0) | 185 | if (i <= 0) { |
180 | { | ||
181 | /* Should be continue next time we are called? */ | 186 | /* Should be continue next time we are called? */ |
182 | if (!BIO_should_retry(b->next_bio)) | 187 | if (!BIO_should_retry(b->next_bio)) { |
183 | { | 188 | ctx->cont = i; |
184 | ctx->cont=i; | 189 | i = EVP_CipherFinal_ex(&(ctx->cipher), |
185 | i=EVP_CipherFinal_ex(&(ctx->cipher), | 190 | (unsigned char *)ctx->buf, |
186 | (unsigned char *)ctx->buf, | 191 | &(ctx->buf_len)); |
187 | &(ctx->buf_len)); | 192 | ctx->ok = i; |
188 | ctx->ok=i; | 193 | ctx->buf_off = 0; |
189 | ctx->buf_off=0; | 194 | } else { |
190 | } | 195 | ret = (ret == 0) ? i : ret; |
191 | else | ||
192 | { | ||
193 | ret=(ret == 0)?i:ret; | ||
194 | break; | 196 | break; |
195 | } | ||
196 | } | 197 | } |
197 | else | 198 | } else { |
198 | { | ||
199 | EVP_CipherUpdate(&(ctx->cipher), | 199 | EVP_CipherUpdate(&(ctx->cipher), |
200 | (unsigned char *)ctx->buf,&ctx->buf_len, | 200 | (unsigned char *)ctx->buf, &ctx->buf_len, |
201 | (unsigned char *)&(ctx->buf[BUF_OFFSET]),i); | 201 | (unsigned char *)&(ctx->buf[BUF_OFFSET]), i); |
202 | ctx->cont=1; | 202 | ctx->cont = 1; |
203 | /* Note: it is possible for EVP_CipherUpdate to | 203 | /* Note: it is possible for EVP_CipherUpdate to |
204 | * decrypt zero bytes because this is or looks like | 204 | * decrypt zero bytes because this is or looks like |
205 | * the final block: if this happens we should retry | 205 | * the final block: if this happens we should retry |
206 | * and either read more data or decrypt the final | 206 | * and either read more data or decrypt the final |
207 | * block | 207 | * block |
208 | */ | 208 | */ |
209 | if(ctx->buf_len == 0) continue; | 209 | if (ctx->buf_len == 0) |
210 | } | 210 | continue; |
211 | } | ||
211 | 212 | ||
212 | if (ctx->buf_len <= outl) | 213 | if (ctx->buf_len <= outl) |
213 | i=ctx->buf_len; | 214 | i = ctx->buf_len; |
214 | else | 215 | else |
215 | i=outl; | 216 | i = outl; |
216 | if (i <= 0) break; | 217 | if (i <= 0) |
217 | memcpy(out,ctx->buf,i); | 218 | break; |
218 | ret+=i; | 219 | memcpy(out, ctx->buf, i); |
219 | ctx->buf_off=i; | 220 | ret += i; |
220 | outl-=i; | 221 | ctx->buf_off = i; |
221 | out+=i; | 222 | outl -= i; |
222 | } | 223 | out += i; |
224 | } | ||
223 | 225 | ||
224 | BIO_clear_retry_flags(b); | 226 | BIO_clear_retry_flags(b); |
225 | BIO_copy_next_retry(b); | 227 | BIO_copy_next_retry(b); |
226 | return((ret == 0)?ctx->cont:ret); | 228 | return ((ret == 0) ? ctx->cont : ret); |
227 | } | 229 | } |
228 | 230 | ||
229 | static int enc_write(BIO *b, const char *in, int inl) | 231 | static int |
230 | { | 232 | enc_write(BIO *b, const char *in, int inl) |
231 | int ret=0,n,i; | 233 | { |
234 | int ret = 0, n, i; | ||
232 | BIO_ENC_CTX *ctx; | 235 | BIO_ENC_CTX *ctx; |
233 | 236 | ||
234 | ctx=(BIO_ENC_CTX *)b->ptr; | 237 | ctx = (BIO_ENC_CTX *)b->ptr; |
235 | ret=inl; | 238 | ret = inl; |
236 | 239 | ||
237 | BIO_clear_retry_flags(b); | 240 | BIO_clear_retry_flags(b); |
238 | n=ctx->buf_len-ctx->buf_off; | 241 | n = ctx->buf_len - ctx->buf_off; |
239 | while (n > 0) | 242 | while (n > 0) { |
240 | { | 243 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); |
241 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | 244 | if (i <= 0) { |
242 | if (i <= 0) | ||
243 | { | ||
244 | BIO_copy_next_retry(b); | 245 | BIO_copy_next_retry(b); |
245 | return(i); | 246 | return (i); |
246 | } | ||
247 | ctx->buf_off+=i; | ||
248 | n-=i; | ||
249 | } | 247 | } |
248 | ctx->buf_off += i; | ||
249 | n -= i; | ||
250 | } | ||
250 | /* at this point all pending data has been written */ | 251 | /* at this point all pending data has been written */ |
251 | 252 | ||
252 | if ((in == NULL) || (inl <= 0)) return(0); | 253 | if ((in == NULL) || (inl <= 0)) |
254 | return (0); | ||
253 | 255 | ||
254 | ctx->buf_off=0; | 256 | ctx->buf_off = 0; |
255 | while (inl > 0) | 257 | while (inl > 0) { |
256 | { | 258 | n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl; |
257 | n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl; | ||
258 | EVP_CipherUpdate(&(ctx->cipher), | 259 | EVP_CipherUpdate(&(ctx->cipher), |
259 | (unsigned char *)ctx->buf,&ctx->buf_len, | 260 | (unsigned char *)ctx->buf, &ctx->buf_len, |
260 | (unsigned char *)in,n); | 261 | (unsigned char *)in, n); |
261 | inl-=n; | 262 | inl -= n; |
262 | in+=n; | 263 | in += n; |
263 | 264 | ||
264 | ctx->buf_off=0; | 265 | ctx->buf_off = 0; |
265 | n=ctx->buf_len; | 266 | n = ctx->buf_len; |
266 | while (n > 0) | 267 | while (n > 0) { |
267 | { | 268 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); |
268 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | 269 | if (i <= 0) { |
269 | if (i <= 0) | ||
270 | { | ||
271 | BIO_copy_next_retry(b); | 270 | BIO_copy_next_retry(b); |
272 | return (ret == inl) ? i : ret - inl; | 271 | return (ret == inl) ? i : ret - inl; |
273 | } | ||
274 | n-=i; | ||
275 | ctx->buf_off+=i; | ||
276 | } | 272 | } |
277 | ctx->buf_len=0; | 273 | n -= i; |
278 | ctx->buf_off=0; | 274 | ctx->buf_off += i; |
279 | } | 275 | } |
280 | BIO_copy_next_retry(b); | 276 | ctx->buf_len = 0; |
281 | return(ret); | 277 | ctx->buf_off = 0; |
282 | } | 278 | } |
279 | BIO_copy_next_retry(b); | ||
280 | return (ret); | ||
281 | } | ||
283 | 282 | ||
284 | static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) | 283 | static long |
285 | { | 284 | enc_ctrl(BIO *b, int cmd, long num, void *ptr) |
285 | { | ||
286 | BIO *dbio; | 286 | BIO *dbio; |
287 | BIO_ENC_CTX *ctx,*dctx; | 287 | BIO_ENC_CTX *ctx, *dctx; |
288 | long ret=1; | 288 | long ret = 1; |
289 | int i; | 289 | int i; |
290 | EVP_CIPHER_CTX **c_ctx; | 290 | EVP_CIPHER_CTX **c_ctx; |
291 | 291 | ||
292 | ctx=(BIO_ENC_CTX *)b->ptr; | 292 | ctx = (BIO_ENC_CTX *)b->ptr; |
293 | 293 | ||
294 | switch (cmd) | 294 | switch (cmd) { |
295 | { | ||
296 | case BIO_CTRL_RESET: | 295 | case BIO_CTRL_RESET: |
297 | ctx->ok=1; | 296 | ctx->ok = 1; |
298 | ctx->finished=0; | 297 | ctx->finished = 0; |
299 | EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, | 298 | EVP_CipherInit_ex(&(ctx->cipher), NULL, NULL, NULL, NULL, |
300 | ctx->cipher.encrypt); | 299 | ctx->cipher.encrypt); |
301 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 300 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
302 | break; | 301 | break; |
303 | case BIO_CTRL_EOF: /* More to read */ | 302 | case BIO_CTRL_EOF: /* More to read */ |
304 | if (ctx->cont <= 0) | 303 | if (ctx->cont <= 0) |
305 | ret=1; | 304 | ret = 1; |
306 | else | 305 | else |
307 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 306 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
308 | break; | 307 | break; |
309 | case BIO_CTRL_WPENDING: | 308 | case BIO_CTRL_WPENDING: |
310 | ret=ctx->buf_len-ctx->buf_off; | 309 | ret = ctx->buf_len - ctx->buf_off; |
311 | if (ret <= 0) | 310 | if (ret <= 0) |
312 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 311 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
313 | break; | 312 | break; |
314 | case BIO_CTRL_PENDING: /* More to read in buffer */ | 313 | case BIO_CTRL_PENDING: /* More to read in buffer */ |
315 | ret=ctx->buf_len-ctx->buf_off; | 314 | ret = ctx->buf_len - ctx->buf_off; |
316 | if (ret <= 0) | 315 | if (ret <= 0) |
317 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 316 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
318 | break; | 317 | break; |
319 | case BIO_CTRL_FLUSH: | 318 | case BIO_CTRL_FLUSH: |
320 | /* do a final write */ | 319 | /* do a final write */ |
321 | again: | 320 | again: |
322 | while (ctx->buf_len != ctx->buf_off) | 321 | while (ctx->buf_len != ctx->buf_off) { |
323 | { | 322 | i = enc_write(b, NULL, 0); |
324 | i=enc_write(b,NULL,0); | ||
325 | if (i < 0) | 323 | if (i < 0) |
326 | return i; | 324 | return i; |
327 | } | 325 | } |
328 | 326 | ||
329 | if (!ctx->finished) | 327 | if (!ctx->finished) { |
330 | { | 328 | ctx->finished = 1; |
331 | ctx->finished=1; | 329 | ctx->buf_off = 0; |
332 | ctx->buf_off=0; | 330 | ret = EVP_CipherFinal_ex(&(ctx->cipher), |
333 | ret=EVP_CipherFinal_ex(&(ctx->cipher), | 331 | (unsigned char *)ctx->buf, |
334 | (unsigned char *)ctx->buf, | 332 | &(ctx->buf_len)); |
335 | &(ctx->buf_len)); | 333 | ctx->ok = (int)ret; |
336 | ctx->ok=(int)ret; | 334 | if (ret <= 0) |
337 | if (ret <= 0) break; | 335 | break; |
338 | 336 | ||
339 | /* push out the bytes */ | 337 | /* push out the bytes */ |
340 | goto again; | 338 | goto again; |
341 | } | 339 | } |
342 | 340 | ||
343 | /* Finally flush the underlying BIO */ | 341 | /* Finally flush the underlying BIO */ |
344 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 342 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
345 | break; | 343 | break; |
346 | case BIO_C_GET_CIPHER_STATUS: | 344 | case BIO_C_GET_CIPHER_STATUS: |
347 | ret=(long)ctx->ok; | 345 | ret = (long)ctx->ok; |
348 | break; | 346 | break; |
349 | case BIO_C_DO_STATE_MACHINE: | 347 | case BIO_C_DO_STATE_MACHINE: |
350 | BIO_clear_retry_flags(b); | 348 | BIO_clear_retry_flags(b); |
351 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 349 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
352 | BIO_copy_next_retry(b); | 350 | BIO_copy_next_retry(b); |
353 | break; | 351 | break; |
354 | case BIO_C_GET_CIPHER_CTX: | 352 | case BIO_C_GET_CIPHER_CTX: |
355 | c_ctx=(EVP_CIPHER_CTX **)ptr; | 353 | c_ctx = (EVP_CIPHER_CTX **)ptr; |
356 | (*c_ctx)= &(ctx->cipher); | 354 | (*c_ctx) = &(ctx->cipher); |
357 | b->init=1; | 355 | b->init = 1; |
358 | break; | 356 | break; |
359 | case BIO_CTRL_DUP: | 357 | case BIO_CTRL_DUP: |
360 | dbio=(BIO *)ptr; | 358 | dbio = (BIO *)ptr; |
361 | dctx=(BIO_ENC_CTX *)dbio->ptr; | 359 | dctx = (BIO_ENC_CTX *)dbio->ptr; |
362 | EVP_CIPHER_CTX_init(&dctx->cipher); | 360 | EVP_CIPHER_CTX_init(&dctx->cipher); |
363 | ret = EVP_CIPHER_CTX_copy(&dctx->cipher,&ctx->cipher); | 361 | ret = EVP_CIPHER_CTX_copy(&dctx->cipher, &ctx->cipher); |
364 | if (ret) | 362 | if (ret) |
365 | dbio->init=1; | 363 | dbio->init = 1; |
366 | break; | 364 | break; |
367 | default: | 365 | default: |
368 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 366 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
369 | break; | 367 | break; |
370 | } | ||
371 | return(ret); | ||
372 | } | 368 | } |
369 | return (ret); | ||
370 | } | ||
373 | 371 | ||
374 | static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | 372 | static long |
375 | { | 373 | enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
376 | long ret=1; | 374 | { |
375 | long ret = 1; | ||
377 | 376 | ||
378 | if (b->next_bio == NULL) return(0); | 377 | if (b->next_bio == NULL) |
379 | switch (cmd) | 378 | return (0); |
380 | { | 379 | switch (cmd) { |
381 | default: | 380 | default: |
382 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | 381 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); |
383 | break; | 382 | break; |
384 | } | ||
385 | return(ret); | ||
386 | } | 383 | } |
384 | return (ret); | ||
385 | } | ||
387 | 386 | ||
388 | /* | 387 | /* |
389 | void BIO_set_cipher_ctx(b,c) | 388 | void BIO_set_cipher_ctx(b,c) |
@@ -399,28 +398,29 @@ EVP_CIPHER_ctx *c; | |||
399 | b->init=1; | 398 | b->init=1; |
400 | ctx=(BIO_ENC_CTX *)b->ptr; | 399 | ctx=(BIO_ENC_CTX *)b->ptr; |
401 | memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); | 400 | memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); |
402 | 401 | ||
403 | if (b->callback != NULL) | 402 | if (b->callback != NULL) |
404 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); | 403 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); |
405 | } | 404 | } |
406 | */ | 405 | */ |
407 | 406 | ||
408 | void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, | 407 | void |
409 | const unsigned char *i, int e) | 408 | BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, |
410 | { | 409 | const unsigned char *i, int e) |
410 | { | ||
411 | BIO_ENC_CTX *ctx; | 411 | BIO_ENC_CTX *ctx; |
412 | 412 | ||
413 | if (b == NULL) return; | 413 | if (b == NULL) |
414 | return; | ||
414 | 415 | ||
415 | if ((b->callback != NULL) && | 416 | if ((b->callback != NULL) && |
416 | (b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0)) | 417 | (b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 0L) <= 0)) |
417 | return; | 418 | return; |
418 | 419 | ||
419 | b->init=1; | 420 | b->init = 1; |
420 | ctx=(BIO_ENC_CTX *)b->ptr; | 421 | ctx = (BIO_ENC_CTX *)b->ptr; |
421 | EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e); | 422 | EVP_CipherInit_ex(&(ctx->cipher), c,NULL, k, i, e); |
422 | |||
423 | if (b->callback != NULL) | ||
424 | b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L); | ||
425 | } | ||
426 | 423 | ||
424 | if (b->callback != NULL) | ||
425 | b->callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L); | ||
426 | } | ||