diff options
author | jsing <> | 2014-06-29 16:34:12 +0000 |
---|---|---|
committer | jsing <> | 2014-06-29 16:34:12 +0000 |
commit | 9d44d3784f281c21a4fc03e18b10241744da5af5 (patch) | |
tree | 0e5c626dfdf9c8eb698519bab4f7fc0a767935e6 | |
parent | 3f8b4bfb24a5e3eb0a466fe1a53b3aa428027b78 (diff) | |
download | openbsd-9d44d3784f281c21a4fc03e18b10241744da5af5.tar.gz openbsd-9d44d3784f281c21a4fc03e18b10241744da5af5.tar.bz2 openbsd-9d44d3784f281c21a4fc03e18b10241744da5af5.zip |
Remove another unused source file - I got suspicious when I found a
function that ended with:
if (ret & 0x01)
if (ret & V_ASN1_CONSTRUCTED)
}
-rw-r--r-- | src/lib/libcrypto/pkcs7/bio_ber.c | 460 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/pkcs7/bio_ber.c | 460 |
2 files changed, 0 insertions, 920 deletions
diff --git a/src/lib/libcrypto/pkcs7/bio_ber.c b/src/lib/libcrypto/pkcs7/bio_ber.c deleted file mode 100644 index 0f0a5ba7dc..0000000000 --- a/src/lib/libcrypto/pkcs7/bio_ber.c +++ /dev/null | |||
@@ -1,460 +0,0 @@ | |||
1 | /* $OpenBSD: bio_ber.c,v 1.11 2014/06/12 15:49:30 deraadt Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
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 | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
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. | ||
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. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
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: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
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 | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
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 | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <errno.h> | ||
61 | #include "cryptlib.h" | ||
62 | #include <openssl/buffer.h> | ||
63 | #include <openssl/evp.h> | ||
64 | |||
65 | static int ber_write(BIO *h,char *buf,int num); | ||
66 | static int ber_read(BIO *h,char *buf,int size); | ||
67 | /*static int ber_puts(BIO *h,char *str); */ | ||
68 | /*static int ber_gets(BIO *h,char *str,int size); */ | ||
69 | static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
70 | static int ber_new(BIO *h); | ||
71 | static int ber_free(BIO *data); | ||
72 | static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)()); | ||
73 | #define BER_BUF_SIZE (32) | ||
74 | |||
75 | /* This is used to hold the state of the BER objects being read. */ | ||
76 | typedef struct ber_struct | ||
77 | { | ||
78 | int tag; | ||
79 | int class; | ||
80 | long length; | ||
81 | int inf; | ||
82 | int num_left; | ||
83 | int depth; | ||
84 | } BER_CTX; | ||
85 | |||
86 | typedef struct bio_ber_struct | ||
87 | { | ||
88 | int tag; | ||
89 | int class; | ||
90 | long length; | ||
91 | int inf; | ||
92 | |||
93 | /* most of the following are used when doing non-blocking IO */ | ||
94 | /* reading */ | ||
95 | long num_left; /* number of bytes still to read/write in block */ | ||
96 | int depth; /* used with indefinite encoding. */ | ||
97 | int finished; /* No more read data */ | ||
98 | |||
99 | /* writting */ | ||
100 | char *w_addr; | ||
101 | int w_offset; | ||
102 | int w_left; | ||
103 | |||
104 | int buf_len; | ||
105 | int buf_off; | ||
106 | unsigned char buf[BER_BUF_SIZE]; | ||
107 | } BIO_BER_CTX; | ||
108 | |||
109 | static BIO_METHOD methods_ber = { | ||
110 | .type = BIO_TYPE_CIPHER, | ||
111 | .name = "cipher", | ||
112 | .bwrite = ber_write, | ||
113 | .bread = ber_read, | ||
114 | .ctrl = ber_ctrl, | ||
115 | .create = ber_new, | ||
116 | .destroy = ber_free, | ||
117 | .callback_ctrl = ber_callback_ctrl | ||
118 | }; | ||
119 | |||
120 | BIO_METHOD *BIO_f_ber(void) | ||
121 | { | ||
122 | return(&methods_ber); | ||
123 | } | ||
124 | |||
125 | static int ber_new(BIO *bi) | ||
126 | { | ||
127 | BIO_BER_CTX *ctx; | ||
128 | |||
129 | ctx=calloc(1, sizeof(BIO_BER_CTX)); | ||
130 | if (ctx == NULL) return(0); | ||
131 | |||
132 | bi->init=0; | ||
133 | bi->ptr=(char *)ctx; | ||
134 | bi->flags=0; | ||
135 | return(1); | ||
136 | } | ||
137 | |||
138 | static int ber_free(BIO *a) | ||
139 | { | ||
140 | BIO_BER_CTX *b; | ||
141 | |||
142 | if (a == NULL) return(0); | ||
143 | b=(BIO_BER_CTX *)a->ptr; | ||
144 | OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX)); | ||
145 | free(a->ptr); | ||
146 | a->ptr=NULL; | ||
147 | a->init=0; | ||
148 | a->flags=0; | ||
149 | return(1); | ||
150 | } | ||
151 | |||
152 | int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx) | ||
153 | { | ||
154 | int i,j,n; | ||
155 | int ret; | ||
156 | unsigned char *p; | ||
157 | unsigned long length | ||
158 | int tag; | ||
159 | int class; | ||
160 | long max; | ||
161 | |||
162 | BIO_clear_retry_flags(b); | ||
163 | |||
164 | /* Pack the buffer down if there is a hole at the front */ | ||
165 | if (ctx->buf_off != 0) | ||
166 | { | ||
167 | p=ctx->buf; | ||
168 | j=ctx->buf_off; | ||
169 | n=ctx->buf_len-j; | ||
170 | for (i=0; i<n; i++) | ||
171 | { | ||
172 | p[0]=p[j]; | ||
173 | p++; | ||
174 | } | ||
175 | ctx->buf_len-j; | ||
176 | ctx->buf_off=0; | ||
177 | } | ||
178 | |||
179 | /* If there is more room, read some more data */ | ||
180 | i=BER_BUF_SIZE-ctx->buf_len; | ||
181 | if (i) | ||
182 | { | ||
183 | i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i); | ||
184 | if (i <= 0) | ||
185 | { | ||
186 | BIO_copy_next_retry(b); | ||
187 | return(i); | ||
188 | } | ||
189 | else | ||
190 | ctx->buf_len+=i; | ||
191 | } | ||
192 | |||
193 | max=ctx->buf_len; | ||
194 | p=ctx->buf; | ||
195 | ret=ASN1_get_object(&p,&length,&tag,&class,max); | ||
196 | |||
197 | if (ret & 0x80) | ||
198 | { | ||
199 | if ((ctx->buf_len < BER_BUF_SIZE) && | ||
200 | (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG)) | ||
201 | { | ||
202 | ERR_clear_error(); /* clear the error */ | ||
203 | BIO_set_retry_read(b); | ||
204 | } | ||
205 | return(-1); | ||
206 | } | ||
207 | |||
208 | /* We have no error, we have a header, so make use of it */ | ||
209 | |||
210 | if ((ctx->tag >= 0) && (ctx->tag != tag)) | ||
211 | { | ||
212 | BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH); | ||
213 | ERR_asprintf_error_data("tag=%d, got %d", ctx->tag, tag); | ||
214 | return(-1); | ||
215 | } | ||
216 | if (ret & 0x01) | ||
217 | if (ret & V_ASN1_CONSTRUCTED) | ||
218 | } | ||
219 | |||
220 | static int ber_read(BIO *b, char *out, int outl) | ||
221 | { | ||
222 | int ret=0,i,n; | ||
223 | BIO_BER_CTX *ctx; | ||
224 | |||
225 | BIO_clear_retry_flags(b); | ||
226 | |||
227 | if (out == NULL) return(0); | ||
228 | ctx=(BIO_BER_CTX *)b->ptr; | ||
229 | |||
230 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
231 | |||
232 | if (ctx->finished) return(0); | ||
233 | |||
234 | again: | ||
235 | /* First see if we are half way through reading a block */ | ||
236 | if (ctx->num_left > 0) | ||
237 | { | ||
238 | if (ctx->num_left < outl) | ||
239 | n=ctx->num_left; | ||
240 | else | ||
241 | n=outl; | ||
242 | i=BIO_read(b->next_bio,out,n); | ||
243 | if (i <= 0) | ||
244 | { | ||
245 | BIO_copy_next_retry(b); | ||
246 | return(i); | ||
247 | } | ||
248 | ctx->num_left-=i; | ||
249 | outl-=i; | ||
250 | ret+=i; | ||
251 | if (ctx->num_left <= 0) | ||
252 | { | ||
253 | ctx->depth--; | ||
254 | if (ctx->depth <= 0) | ||
255 | ctx->finished=1; | ||
256 | } | ||
257 | if (outl <= 0) | ||
258 | return(ret); | ||
259 | else | ||
260 | goto again; | ||
261 | } | ||
262 | else /* we need to read another BER header */ | ||
263 | { | ||
264 | } | ||
265 | } | ||
266 | |||
267 | static int ber_write(BIO *b, char *in, int inl) | ||
268 | { | ||
269 | int ret=0,n,i; | ||
270 | BIO_ENC_CTX *ctx; | ||
271 | |||
272 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
273 | ret=inl; | ||
274 | |||
275 | BIO_clear_retry_flags(b); | ||
276 | n=ctx->buf_len-ctx->buf_off; | ||
277 | while (n > 0) | ||
278 | { | ||
279 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | ||
280 | if (i <= 0) | ||
281 | { | ||
282 | BIO_copy_next_retry(b); | ||
283 | return(i); | ||
284 | } | ||
285 | ctx->buf_off+=i; | ||
286 | n-=i; | ||
287 | } | ||
288 | /* at this point all pending data has been written */ | ||
289 | |||
290 | if ((in == NULL) || (inl <= 0)) return(0); | ||
291 | |||
292 | ctx->buf_off=0; | ||
293 | while (inl > 0) | ||
294 | { | ||
295 | n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl; | ||
296 | EVP_CipherUpdate(&(ctx->cipher), | ||
297 | (unsigned char *)ctx->buf,&ctx->buf_len, | ||
298 | (unsigned char *)in,n); | ||
299 | inl-=n; | ||
300 | in+=n; | ||
301 | |||
302 | ctx->buf_off=0; | ||
303 | n=ctx->buf_len; | ||
304 | while (n > 0) | ||
305 | { | ||
306 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | ||
307 | if (i <= 0) | ||
308 | { | ||
309 | BIO_copy_next_retry(b); | ||
310 | return(i); | ||
311 | } | ||
312 | n-=i; | ||
313 | ctx->buf_off+=i; | ||
314 | } | ||
315 | ctx->buf_len=0; | ||
316 | ctx->buf_off=0; | ||
317 | } | ||
318 | BIO_copy_next_retry(b); | ||
319 | return(ret); | ||
320 | } | ||
321 | |||
322 | static long ber_ctrl(BIO *b, int cmd, long num, char *ptr) | ||
323 | { | ||
324 | BIO *dbio; | ||
325 | BIO_ENC_CTX *ctx,*dctx; | ||
326 | long ret=1; | ||
327 | int i; | ||
328 | |||
329 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
330 | |||
331 | switch (cmd) | ||
332 | { | ||
333 | case BIO_CTRL_RESET: | ||
334 | ctx->ok=1; | ||
335 | ctx->finished=0; | ||
336 | EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, | ||
337 | ctx->cipher.berrypt); | ||
338 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
339 | break; | ||
340 | case BIO_CTRL_EOF: /* More to read */ | ||
341 | if (ctx->cont <= 0) | ||
342 | ret=1; | ||
343 | else | ||
344 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
345 | break; | ||
346 | case BIO_CTRL_WPENDING: | ||
347 | ret=ctx->buf_len-ctx->buf_off; | ||
348 | if (ret <= 0) | ||
349 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
350 | break; | ||
351 | case BIO_CTRL_PENDING: /* More to read in buffer */ | ||
352 | ret=ctx->buf_len-ctx->buf_off; | ||
353 | if (ret <= 0) | ||
354 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
355 | break; | ||
356 | case BIO_CTRL_FLUSH: | ||
357 | /* do a final write */ | ||
358 | again: | ||
359 | while (ctx->buf_len != ctx->buf_off) | ||
360 | { | ||
361 | i=ber_write(b,NULL,0); | ||
362 | if (i < 0) | ||
363 | { | ||
364 | ret=i; | ||
365 | break; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | if (!ctx->finished) | ||
370 | { | ||
371 | ctx->finished=1; | ||
372 | ctx->buf_off=0; | ||
373 | ret=EVP_CipherFinal_ex(&(ctx->cipher), | ||
374 | (unsigned char *)ctx->buf, | ||
375 | &(ctx->buf_len)); | ||
376 | ctx->ok=(int)ret; | ||
377 | if (ret <= 0) break; | ||
378 | |||
379 | /* push out the bytes */ | ||
380 | goto again; | ||
381 | } | ||
382 | |||
383 | /* Finally flush the underlying BIO */ | ||
384 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
385 | break; | ||
386 | case BIO_C_GET_CIPHER_STATUS: | ||
387 | ret=(long)ctx->ok; | ||
388 | break; | ||
389 | case BIO_C_DO_STATE_MACHINE: | ||
390 | BIO_clear_retry_flags(b); | ||
391 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
392 | BIO_copy_next_retry(b); | ||
393 | break; | ||
394 | |||
395 | case BIO_CTRL_DUP: | ||
396 | dbio=(BIO *)ptr; | ||
397 | dctx=(BIO_ENC_CTX *)dbio->ptr; | ||
398 | memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher)); | ||
399 | dbio->init=1; | ||
400 | break; | ||
401 | default: | ||
402 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
403 | break; | ||
404 | } | ||
405 | return(ret); | ||
406 | } | ||
407 | |||
408 | static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)()) | ||
409 | { | ||
410 | long ret=1; | ||
411 | |||
412 | if (b->next_bio == NULL) return(0); | ||
413 | switch (cmd) | ||
414 | { | ||
415 | default: | ||
416 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
417 | break; | ||
418 | } | ||
419 | return(ret); | ||
420 | } | ||
421 | |||
422 | /* | ||
423 | void BIO_set_cipher_ctx(b,c) | ||
424 | BIO *b; | ||
425 | EVP_CIPHER_ctx *c; | ||
426 | { | ||
427 | if (b == NULL) return; | ||
428 | |||
429 | if ((b->callback != NULL) && | ||
430 | (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) | ||
431 | return; | ||
432 | |||
433 | b->init=1; | ||
434 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
435 | memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); | ||
436 | |||
437 | if (b->callback != NULL) | ||
438 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); | ||
439 | } | ||
440 | */ | ||
441 | |||
442 | void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i, | ||
443 | int e) | ||
444 | { | ||
445 | BIO_ENC_CTX *ctx; | ||
446 | |||
447 | if (b == NULL) return; | ||
448 | |||
449 | if ((b->callback != NULL) && | ||
450 | (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) | ||
451 | return; | ||
452 | |||
453 | b->init=1; | ||
454 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
455 | EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e); | ||
456 | |||
457 | if (b->callback != NULL) | ||
458 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); | ||
459 | } | ||
460 | |||
diff --git a/src/lib/libssl/src/crypto/pkcs7/bio_ber.c b/src/lib/libssl/src/crypto/pkcs7/bio_ber.c deleted file mode 100644 index 0f0a5ba7dc..0000000000 --- a/src/lib/libssl/src/crypto/pkcs7/bio_ber.c +++ /dev/null | |||
@@ -1,460 +0,0 @@ | |||
1 | /* $OpenBSD: bio_ber.c,v 1.11 2014/06/12 15:49:30 deraadt Exp $ */ | ||
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This package is an SSL implementation written | ||
6 | * by Eric Young (eay@cryptsoft.com). | ||
7 | * The implementation was written so as to conform with Netscapes SSL. | ||
8 | * | ||
9 | * This library is free for commercial and non-commercial use as long as | ||
10 | * the following conditions are aheared to. The following conditions | ||
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 | ||
13 | * included with this distribution is covered by the same copyright terms | ||
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
15 | * | ||
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
17 | * the code are not to be removed. | ||
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. | ||
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. | ||
22 | * | ||
23 | * Redistribution and use in source and binary forms, with or without | ||
24 | * modification, are permitted provided that the following conditions | ||
25 | * are met: | ||
26 | * 1. Redistributions of source code must retain the copyright | ||
27 | * notice, this list of conditions and the following disclaimer. | ||
28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
29 | * notice, this list of conditions and the following disclaimer in the | ||
30 | * documentation and/or other materials provided with the distribution. | ||
31 | * 3. All advertising materials mentioning features or use of this software | ||
32 | * must display the following acknowledgement: | ||
33 | * "This product includes cryptographic software written by | ||
34 | * Eric Young (eay@cryptsoft.com)" | ||
35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
36 | * being used are not cryptographic related :-). | ||
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: | ||
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
40 | * | ||
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
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 | ||
51 | * SUCH DAMAGE. | ||
52 | * | ||
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 | ||
55 | * copied and put under another distribution licence | ||
56 | * [including the GNU Public Licence.] | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include <errno.h> | ||
61 | #include "cryptlib.h" | ||
62 | #include <openssl/buffer.h> | ||
63 | #include <openssl/evp.h> | ||
64 | |||
65 | static int ber_write(BIO *h,char *buf,int num); | ||
66 | static int ber_read(BIO *h,char *buf,int size); | ||
67 | /*static int ber_puts(BIO *h,char *str); */ | ||
68 | /*static int ber_gets(BIO *h,char *str,int size); */ | ||
69 | static long ber_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
70 | static int ber_new(BIO *h); | ||
71 | static int ber_free(BIO *data); | ||
72 | static long ber_callback_ctrl(BIO *h,int cmd,void *(*fp)()); | ||
73 | #define BER_BUF_SIZE (32) | ||
74 | |||
75 | /* This is used to hold the state of the BER objects being read. */ | ||
76 | typedef struct ber_struct | ||
77 | { | ||
78 | int tag; | ||
79 | int class; | ||
80 | long length; | ||
81 | int inf; | ||
82 | int num_left; | ||
83 | int depth; | ||
84 | } BER_CTX; | ||
85 | |||
86 | typedef struct bio_ber_struct | ||
87 | { | ||
88 | int tag; | ||
89 | int class; | ||
90 | long length; | ||
91 | int inf; | ||
92 | |||
93 | /* most of the following are used when doing non-blocking IO */ | ||
94 | /* reading */ | ||
95 | long num_left; /* number of bytes still to read/write in block */ | ||
96 | int depth; /* used with indefinite encoding. */ | ||
97 | int finished; /* No more read data */ | ||
98 | |||
99 | /* writting */ | ||
100 | char *w_addr; | ||
101 | int w_offset; | ||
102 | int w_left; | ||
103 | |||
104 | int buf_len; | ||
105 | int buf_off; | ||
106 | unsigned char buf[BER_BUF_SIZE]; | ||
107 | } BIO_BER_CTX; | ||
108 | |||
109 | static BIO_METHOD methods_ber = { | ||
110 | .type = BIO_TYPE_CIPHER, | ||
111 | .name = "cipher", | ||
112 | .bwrite = ber_write, | ||
113 | .bread = ber_read, | ||
114 | .ctrl = ber_ctrl, | ||
115 | .create = ber_new, | ||
116 | .destroy = ber_free, | ||
117 | .callback_ctrl = ber_callback_ctrl | ||
118 | }; | ||
119 | |||
120 | BIO_METHOD *BIO_f_ber(void) | ||
121 | { | ||
122 | return(&methods_ber); | ||
123 | } | ||
124 | |||
125 | static int ber_new(BIO *bi) | ||
126 | { | ||
127 | BIO_BER_CTX *ctx; | ||
128 | |||
129 | ctx=calloc(1, sizeof(BIO_BER_CTX)); | ||
130 | if (ctx == NULL) return(0); | ||
131 | |||
132 | bi->init=0; | ||
133 | bi->ptr=(char *)ctx; | ||
134 | bi->flags=0; | ||
135 | return(1); | ||
136 | } | ||
137 | |||
138 | static int ber_free(BIO *a) | ||
139 | { | ||
140 | BIO_BER_CTX *b; | ||
141 | |||
142 | if (a == NULL) return(0); | ||
143 | b=(BIO_BER_CTX *)a->ptr; | ||
144 | OPENSSL_cleanse(a->ptr,sizeof(BIO_BER_CTX)); | ||
145 | free(a->ptr); | ||
146 | a->ptr=NULL; | ||
147 | a->init=0; | ||
148 | a->flags=0; | ||
149 | return(1); | ||
150 | } | ||
151 | |||
152 | int bio_ber_get_header(BIO *bio, BIO_BER_CTX *ctx) | ||
153 | { | ||
154 | int i,j,n; | ||
155 | int ret; | ||
156 | unsigned char *p; | ||
157 | unsigned long length | ||
158 | int tag; | ||
159 | int class; | ||
160 | long max; | ||
161 | |||
162 | BIO_clear_retry_flags(b); | ||
163 | |||
164 | /* Pack the buffer down if there is a hole at the front */ | ||
165 | if (ctx->buf_off != 0) | ||
166 | { | ||
167 | p=ctx->buf; | ||
168 | j=ctx->buf_off; | ||
169 | n=ctx->buf_len-j; | ||
170 | for (i=0; i<n; i++) | ||
171 | { | ||
172 | p[0]=p[j]; | ||
173 | p++; | ||
174 | } | ||
175 | ctx->buf_len-j; | ||
176 | ctx->buf_off=0; | ||
177 | } | ||
178 | |||
179 | /* If there is more room, read some more data */ | ||
180 | i=BER_BUF_SIZE-ctx->buf_len; | ||
181 | if (i) | ||
182 | { | ||
183 | i=BIO_read(bio->next_bio,&(ctx->buf[ctx->buf_len]),i); | ||
184 | if (i <= 0) | ||
185 | { | ||
186 | BIO_copy_next_retry(b); | ||
187 | return(i); | ||
188 | } | ||
189 | else | ||
190 | ctx->buf_len+=i; | ||
191 | } | ||
192 | |||
193 | max=ctx->buf_len; | ||
194 | p=ctx->buf; | ||
195 | ret=ASN1_get_object(&p,&length,&tag,&class,max); | ||
196 | |||
197 | if (ret & 0x80) | ||
198 | { | ||
199 | if ((ctx->buf_len < BER_BUF_SIZE) && | ||
200 | (ERR_GET_REASON(ERR_peek_error()) == ASN1_R_TOO_LONG)) | ||
201 | { | ||
202 | ERR_clear_error(); /* clear the error */ | ||
203 | BIO_set_retry_read(b); | ||
204 | } | ||
205 | return(-1); | ||
206 | } | ||
207 | |||
208 | /* We have no error, we have a header, so make use of it */ | ||
209 | |||
210 | if ((ctx->tag >= 0) && (ctx->tag != tag)) | ||
211 | { | ||
212 | BIOerr(BIO_F_BIO_BER_GET_HEADER,BIO_R_TAG_MISMATCH); | ||
213 | ERR_asprintf_error_data("tag=%d, got %d", ctx->tag, tag); | ||
214 | return(-1); | ||
215 | } | ||
216 | if (ret & 0x01) | ||
217 | if (ret & V_ASN1_CONSTRUCTED) | ||
218 | } | ||
219 | |||
220 | static int ber_read(BIO *b, char *out, int outl) | ||
221 | { | ||
222 | int ret=0,i,n; | ||
223 | BIO_BER_CTX *ctx; | ||
224 | |||
225 | BIO_clear_retry_flags(b); | ||
226 | |||
227 | if (out == NULL) return(0); | ||
228 | ctx=(BIO_BER_CTX *)b->ptr; | ||
229 | |||
230 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
231 | |||
232 | if (ctx->finished) return(0); | ||
233 | |||
234 | again: | ||
235 | /* First see if we are half way through reading a block */ | ||
236 | if (ctx->num_left > 0) | ||
237 | { | ||
238 | if (ctx->num_left < outl) | ||
239 | n=ctx->num_left; | ||
240 | else | ||
241 | n=outl; | ||
242 | i=BIO_read(b->next_bio,out,n); | ||
243 | if (i <= 0) | ||
244 | { | ||
245 | BIO_copy_next_retry(b); | ||
246 | return(i); | ||
247 | } | ||
248 | ctx->num_left-=i; | ||
249 | outl-=i; | ||
250 | ret+=i; | ||
251 | if (ctx->num_left <= 0) | ||
252 | { | ||
253 | ctx->depth--; | ||
254 | if (ctx->depth <= 0) | ||
255 | ctx->finished=1; | ||
256 | } | ||
257 | if (outl <= 0) | ||
258 | return(ret); | ||
259 | else | ||
260 | goto again; | ||
261 | } | ||
262 | else /* we need to read another BER header */ | ||
263 | { | ||
264 | } | ||
265 | } | ||
266 | |||
267 | static int ber_write(BIO *b, char *in, int inl) | ||
268 | { | ||
269 | int ret=0,n,i; | ||
270 | BIO_ENC_CTX *ctx; | ||
271 | |||
272 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
273 | ret=inl; | ||
274 | |||
275 | BIO_clear_retry_flags(b); | ||
276 | n=ctx->buf_len-ctx->buf_off; | ||
277 | while (n > 0) | ||
278 | { | ||
279 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | ||
280 | if (i <= 0) | ||
281 | { | ||
282 | BIO_copy_next_retry(b); | ||
283 | return(i); | ||
284 | } | ||
285 | ctx->buf_off+=i; | ||
286 | n-=i; | ||
287 | } | ||
288 | /* at this point all pending data has been written */ | ||
289 | |||
290 | if ((in == NULL) || (inl <= 0)) return(0); | ||
291 | |||
292 | ctx->buf_off=0; | ||
293 | while (inl > 0) | ||
294 | { | ||
295 | n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl; | ||
296 | EVP_CipherUpdate(&(ctx->cipher), | ||
297 | (unsigned char *)ctx->buf,&ctx->buf_len, | ||
298 | (unsigned char *)in,n); | ||
299 | inl-=n; | ||
300 | in+=n; | ||
301 | |||
302 | ctx->buf_off=0; | ||
303 | n=ctx->buf_len; | ||
304 | while (n > 0) | ||
305 | { | ||
306 | i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n); | ||
307 | if (i <= 0) | ||
308 | { | ||
309 | BIO_copy_next_retry(b); | ||
310 | return(i); | ||
311 | } | ||
312 | n-=i; | ||
313 | ctx->buf_off+=i; | ||
314 | } | ||
315 | ctx->buf_len=0; | ||
316 | ctx->buf_off=0; | ||
317 | } | ||
318 | BIO_copy_next_retry(b); | ||
319 | return(ret); | ||
320 | } | ||
321 | |||
322 | static long ber_ctrl(BIO *b, int cmd, long num, char *ptr) | ||
323 | { | ||
324 | BIO *dbio; | ||
325 | BIO_ENC_CTX *ctx,*dctx; | ||
326 | long ret=1; | ||
327 | int i; | ||
328 | |||
329 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
330 | |||
331 | switch (cmd) | ||
332 | { | ||
333 | case BIO_CTRL_RESET: | ||
334 | ctx->ok=1; | ||
335 | ctx->finished=0; | ||
336 | EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL, | ||
337 | ctx->cipher.berrypt); | ||
338 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
339 | break; | ||
340 | case BIO_CTRL_EOF: /* More to read */ | ||
341 | if (ctx->cont <= 0) | ||
342 | ret=1; | ||
343 | else | ||
344 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
345 | break; | ||
346 | case BIO_CTRL_WPENDING: | ||
347 | ret=ctx->buf_len-ctx->buf_off; | ||
348 | if (ret <= 0) | ||
349 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
350 | break; | ||
351 | case BIO_CTRL_PENDING: /* More to read in buffer */ | ||
352 | ret=ctx->buf_len-ctx->buf_off; | ||
353 | if (ret <= 0) | ||
354 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
355 | break; | ||
356 | case BIO_CTRL_FLUSH: | ||
357 | /* do a final write */ | ||
358 | again: | ||
359 | while (ctx->buf_len != ctx->buf_off) | ||
360 | { | ||
361 | i=ber_write(b,NULL,0); | ||
362 | if (i < 0) | ||
363 | { | ||
364 | ret=i; | ||
365 | break; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | if (!ctx->finished) | ||
370 | { | ||
371 | ctx->finished=1; | ||
372 | ctx->buf_off=0; | ||
373 | ret=EVP_CipherFinal_ex(&(ctx->cipher), | ||
374 | (unsigned char *)ctx->buf, | ||
375 | &(ctx->buf_len)); | ||
376 | ctx->ok=(int)ret; | ||
377 | if (ret <= 0) break; | ||
378 | |||
379 | /* push out the bytes */ | ||
380 | goto again; | ||
381 | } | ||
382 | |||
383 | /* Finally flush the underlying BIO */ | ||
384 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
385 | break; | ||
386 | case BIO_C_GET_CIPHER_STATUS: | ||
387 | ret=(long)ctx->ok; | ||
388 | break; | ||
389 | case BIO_C_DO_STATE_MACHINE: | ||
390 | BIO_clear_retry_flags(b); | ||
391 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
392 | BIO_copy_next_retry(b); | ||
393 | break; | ||
394 | |||
395 | case BIO_CTRL_DUP: | ||
396 | dbio=(BIO *)ptr; | ||
397 | dctx=(BIO_ENC_CTX *)dbio->ptr; | ||
398 | memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher)); | ||
399 | dbio->init=1; | ||
400 | break; | ||
401 | default: | ||
402 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
403 | break; | ||
404 | } | ||
405 | return(ret); | ||
406 | } | ||
407 | |||
408 | static long ber_callback_ctrl(BIO *b, int cmd, void *(*fp)()) | ||
409 | { | ||
410 | long ret=1; | ||
411 | |||
412 | if (b->next_bio == NULL) return(0); | ||
413 | switch (cmd) | ||
414 | { | ||
415 | default: | ||
416 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
417 | break; | ||
418 | } | ||
419 | return(ret); | ||
420 | } | ||
421 | |||
422 | /* | ||
423 | void BIO_set_cipher_ctx(b,c) | ||
424 | BIO *b; | ||
425 | EVP_CIPHER_ctx *c; | ||
426 | { | ||
427 | if (b == NULL) return; | ||
428 | |||
429 | if ((b->callback != NULL) && | ||
430 | (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) | ||
431 | return; | ||
432 | |||
433 | b->init=1; | ||
434 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
435 | memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX)); | ||
436 | |||
437 | if (b->callback != NULL) | ||
438 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); | ||
439 | } | ||
440 | */ | ||
441 | |||
442 | void BIO_set_cipher(BIO *b, EVP_CIPHER *c, unsigned char *k, unsigned char *i, | ||
443 | int e) | ||
444 | { | ||
445 | BIO_ENC_CTX *ctx; | ||
446 | |||
447 | if (b == NULL) return; | ||
448 | |||
449 | if ((b->callback != NULL) && | ||
450 | (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0)) | ||
451 | return; | ||
452 | |||
453 | b->init=1; | ||
454 | ctx=(BIO_ENC_CTX *)b->ptr; | ||
455 | EVP_CipherInit_ex(&(ctx->cipher),c,NULL,k,i,e); | ||
456 | |||
457 | if (b->callback != NULL) | ||
458 | b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L); | ||
459 | } | ||
460 | |||