summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bio/bio_lib.c
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/bio/bio_lib.c
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/bio/bio_lib.c')
-rw-r--r--src/lib/libcrypto/bio/bio_lib.c208
1 files changed, 116 insertions, 92 deletions
diff --git a/src/lib/libcrypto/bio/bio_lib.c b/src/lib/libcrypto/bio/bio_lib.c
index 7a66b0892e..50df2238fa 100644
--- a/src/lib/libcrypto/bio/bio_lib.c
+++ b/src/lib/libcrypto/bio/bio_lib.c
@@ -58,20 +58,16 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include <errno.h> 60#include <errno.h>
61#include "crypto.h" 61#include <openssl/crypto.h>
62#include "cryptlib.h" 62#include "cryptlib.h"
63#include "bio.h" 63#include <openssl/bio.h>
64#include "stack.h" 64#include <openssl/stack.h>
65 65
66static STACK *bio_meth=NULL; 66BIO *BIO_new(BIO_METHOD *method)
67static int bio_meth_num=0;
68
69BIO *BIO_new(method)
70BIO_METHOD *method;
71 { 67 {
72 BIO *ret=NULL; 68 BIO *ret=NULL;
73 69
74 ret=(BIO *)Malloc(sizeof(BIO)); 70 ret=(BIO *)OPENSSL_malloc(sizeof(BIO));
75 if (ret == NULL) 71 if (ret == NULL)
76 { 72 {
77 BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE); 73 BIOerr(BIO_F_BIO_NEW,ERR_R_MALLOC_FAILURE);
@@ -79,15 +75,13 @@ BIO_METHOD *method;
79 } 75 }
80 if (!BIO_set(ret,method)) 76 if (!BIO_set(ret,method))
81 { 77 {
82 Free(ret); 78 OPENSSL_free(ret);
83 ret=NULL; 79 ret=NULL;
84 } 80 }
85 return(ret); 81 return(ret);
86 } 82 }
87 83
88int BIO_set(bio,method) 84int BIO_set(BIO *bio, BIO_METHOD *method)
89BIO *bio;
90BIO_METHOD *method;
91 { 85 {
92 bio->method=method; 86 bio->method=method;
93 bio->callback=NULL; 87 bio->callback=NULL;
@@ -103,15 +97,18 @@ BIO_METHOD *method;
103 bio->references=1; 97 bio->references=1;
104 bio->num_read=0L; 98 bio->num_read=0L;
105 bio->num_write=0L; 99 bio->num_write=0L;
106 CRYPTO_new_ex_data(bio_meth,(char *)bio,&bio->ex_data); 100 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
107 if (method->create != NULL) 101 if (method->create != NULL)
108 if (!method->create(bio)) 102 if (!method->create(bio))
103 {
104 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio,
105 &bio->ex_data);
109 return(0); 106 return(0);
107 }
110 return(1); 108 return(1);
111 } 109 }
112 110
113int BIO_free(a) 111int BIO_free(BIO *a)
114BIO *a;
115 { 112 {
116 int ret=0,i; 113 int ret=0,i;
117 114
@@ -121,7 +118,7 @@ BIO *a;
121#ifdef REF_PRINT 118#ifdef REF_PRINT
122 REF_PRINT("BIO",a); 119 REF_PRINT("BIO",a);
123#endif 120#endif
124 if (i > 0) return(1); 121 if (i > 0) return(1);
125#ifdef REF_CHECK 122#ifdef REF_CHECK
126 if (i < 0) 123 if (i < 0)
127 { 124 {
@@ -133,18 +130,18 @@ BIO *a;
133 ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0)) 130 ((i=(int)a->callback(a,BIO_CB_FREE,NULL,0,0L,1L)) <= 0))
134 return(i); 131 return(i);
135 132
136 CRYPTO_free_ex_data(bio_meth,(char *)a,&a->ex_data); 133 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
137 134
138 if ((a->method == NULL) || (a->method->destroy == NULL)) return(1); 135 if ((a->method == NULL) || (a->method->destroy == NULL)) return(1);
139 ret=a->method->destroy(a); 136 ret=a->method->destroy(a);
140 Free(a); 137 OPENSSL_free(a);
141 return(1); 138 return(1);
142 } 139 }
143 140
144int BIO_read(b,out,outl) 141void BIO_vfree(BIO *a)
145BIO *b; 142 { BIO_free(a); }
146char *out; 143
147int outl; 144int BIO_read(BIO *b, void *out, int outl)
148 { 145 {
149 int i; 146 int i;
150 long (*cb)(); 147 long (*cb)();
@@ -162,11 +159,12 @@ int outl;
162 159
163 if (!b->init) 160 if (!b->init)
164 { 161 {
165 BIOerr(BIO_F_BIO_READ,BIO_R_UNINITALISED); 162 BIOerr(BIO_F_BIO_READ,BIO_R_UNINITIALIZED);
166 return(-2); 163 return(-2);
167 } 164 }
168 165
169 i=b->method->bread(b,out,outl); 166 i=b->method->bread(b,out,outl);
167
170 if (i > 0) b->num_read+=(unsigned long)i; 168 if (i > 0) b->num_read+=(unsigned long)i;
171 169
172 if (cb != NULL) 170 if (cb != NULL)
@@ -175,10 +173,7 @@ int outl;
175 return(i); 173 return(i);
176 } 174 }
177 175
178int BIO_write(b,in,inl) 176int BIO_write(BIO *b, const void *in, int inl)
179BIO *b;
180char *in;
181int inl;
182 { 177 {
183 int i; 178 int i;
184 long (*cb)(); 179 long (*cb)();
@@ -199,11 +194,12 @@ int inl;
199 194
200 if (!b->init) 195 if (!b->init)
201 { 196 {
202 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITALISED); 197 BIOerr(BIO_F_BIO_WRITE,BIO_R_UNINITIALIZED);
203 return(-2); 198 return(-2);
204 } 199 }
205 200
206 i=b->method->bwrite(b,in,inl); 201 i=b->method->bwrite(b,in,inl);
202
207 if (i > 0) b->num_write+=(unsigned long)i; 203 if (i > 0) b->num_write+=(unsigned long)i;
208 204
209 if (cb != NULL) 205 if (cb != NULL)
@@ -212,9 +208,7 @@ int inl;
212 return(i); 208 return(i);
213 } 209 }
214 210
215int BIO_puts(b,in) 211int BIO_puts(BIO *b, const char *in)
216BIO *b;
217char *in;
218 { 212 {
219 int i; 213 int i;
220 long (*cb)(); 214 long (*cb)();
@@ -233,22 +227,21 @@ char *in;
233 227
234 if (!b->init) 228 if (!b->init)
235 { 229 {
236 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITALISED); 230 BIOerr(BIO_F_BIO_PUTS,BIO_R_UNINITIALIZED);
237 return(-2); 231 return(-2);
238 } 232 }
239 233
240 i=b->method->bputs(b,in); 234 i=b->method->bputs(b,in);
241 235
236 if (i > 0) b->num_write+=(unsigned long)i;
237
242 if (cb != NULL) 238 if (cb != NULL)
243 i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0, 239 i=(int)cb(b,BIO_CB_PUTS|BIO_CB_RETURN,in,0,
244 0L,(long)i); 240 0L,(long)i);
245 return(i); 241 return(i);
246 } 242 }
247 243
248int BIO_gets(b,in,inl) 244int BIO_gets(BIO *b, char *in, int inl)
249BIO *b;
250char *in;
251int inl;
252 { 245 {
253 int i; 246 int i;
254 long (*cb)(); 247 long (*cb)();
@@ -267,7 +260,7 @@ int inl;
267 260
268 if (!b->init) 261 if (!b->init)
269 { 262 {
270 BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITALISED); 263 BIOerr(BIO_F_BIO_GETS,BIO_R_UNINITIALIZED);
271 return(-2); 264 return(-2);
272 } 265 }
273 266
@@ -279,11 +272,7 @@ int inl;
279 return(i); 272 return(i);
280 } 273 }
281 274
282long BIO_int_ctrl(b,cmd,larg,iarg) 275long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
283BIO *b;
284int cmd;
285long larg;
286int iarg;
287 { 276 {
288 int i; 277 int i;
289 278
@@ -291,10 +280,7 @@ int iarg;
291 return(BIO_ctrl(b,cmd,larg,(char *)&i)); 280 return(BIO_ctrl(b,cmd,larg,(char *)&i));
292 } 281 }
293 282
294char *BIO_ptr_ctrl(b,cmd,larg) 283char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
295BIO *b;
296int cmd;
297long larg;
298 { 284 {
299 char *p=NULL; 285 char *p=NULL;
300 286
@@ -304,11 +290,7 @@ long larg;
304 return(p); 290 return(p);
305 } 291 }
306 292
307long BIO_ctrl(b,cmd,larg,parg) 293long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
308BIO *b;
309int cmd;
310long larg;
311char *parg;
312 { 294 {
313 long ret; 295 long ret;
314 long (*cb)(); 296 long (*cb)();
@@ -335,9 +317,49 @@ char *parg;
335 return(ret); 317 return(ret);
336 } 318 }
337 319
320long BIO_callback_ctrl(BIO *b, int cmd, void (*fp)(struct bio_st *, int, const char *, int, long, long))
321 {
322 long ret;
323 long (*cb)();
324
325 if (b == NULL) return(0);
326
327 if ((b->method == NULL) || (b->method->callback_ctrl == NULL))
328 {
329 BIOerr(BIO_F_BIO_CTRL,BIO_R_UNSUPPORTED_METHOD);
330 return(-2);
331 }
332
333 cb=b->callback;
334
335 if ((cb != NULL) &&
336 ((ret=cb(b,BIO_CB_CTRL,(void *)&fp,cmd,0,1L)) <= 0))
337 return(ret);
338
339 ret=b->method->callback_ctrl(b,cmd,fp);
340
341 if (cb != NULL)
342 ret=cb(b,BIO_CB_CTRL|BIO_CB_RETURN,(void *)&fp,cmd,
343 0,ret);
344 return(ret);
345 }
346
347/* It is unfortunate to duplicate in functions what the BIO_(w)pending macros
348 * do; but those macros have inappropriate return type, and for interfacing
349 * from other programming languages, C macros aren't much of a help anyway. */
350size_t BIO_ctrl_pending(BIO *bio)
351 {
352 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
353 }
354
355size_t BIO_ctrl_wpending(BIO *bio)
356 {
357 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
358 }
359
360
338/* put the 'bio' on the end of b's list of operators */ 361/* put the 'bio' on the end of b's list of operators */
339BIO *BIO_push(b,bio) 362BIO *BIO_push(BIO *b, BIO *bio)
340BIO *b,*bio;
341 { 363 {
342 BIO *lb; 364 BIO *lb;
343 365
@@ -354,8 +376,7 @@ BIO *b,*bio;
354 } 376 }
355 377
356/* Remove the first and return the rest */ 378/* Remove the first and return the rest */
357BIO *BIO_pop(b) 379BIO *BIO_pop(BIO *b)
358BIO *b;
359 { 380 {
360 BIO *ret; 381 BIO *ret;
361 382
@@ -373,9 +394,7 @@ BIO *b;
373 return(ret); 394 return(ret);
374 } 395 }
375 396
376BIO *BIO_get_retry_BIO(bio,reason) 397BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
377BIO *bio;
378int *reason;
379 { 398 {
380 BIO *b,*last; 399 BIO *b,*last;
381 400
@@ -391,18 +410,16 @@ int *reason;
391 return(last); 410 return(last);
392 } 411 }
393 412
394int BIO_get_retry_reason(bio) 413int BIO_get_retry_reason(BIO *bio)
395BIO *bio;
396 { 414 {
397 return(bio->retry_reason); 415 return(bio->retry_reason);
398 } 416 }
399 417
400BIO *BIO_find_type(bio,type) 418BIO *BIO_find_type(BIO *bio, int type)
401BIO *bio;
402int type;
403 { 419 {
404 int mt,mask; 420 int mt,mask;
405 421
422 if(!bio) return NULL;
406 mask=type&0xff; 423 mask=type&0xff;
407 do { 424 do {
408 if (bio->method != NULL) 425 if (bio->method != NULL)
@@ -421,8 +438,13 @@ int type;
421 return(NULL); 438 return(NULL);
422 } 439 }
423 440
424void BIO_free_all(bio) 441BIO *BIO_next(BIO *b)
425BIO *bio; 442 {
443 if(!b) return NULL;
444 return b->next_bio;
445 }
446
447void BIO_free_all(BIO *bio)
426 { 448 {
427 BIO *b; 449 BIO *b;
428 int ref; 450 int ref;
@@ -438,8 +460,7 @@ BIO *bio;
438 } 460 }
439 } 461 }
440 462
441BIO *BIO_dup_chain(in) 463BIO *BIO_dup_chain(BIO *in)
442BIO *in;
443 { 464 {
444 BIO *ret=NULL,*eoc=NULL,*bio,*new; 465 BIO *ret=NULL,*eoc=NULL,*bio,*new;
445 466
@@ -461,9 +482,10 @@ BIO *in;
461 goto err; 482 goto err;
462 } 483 }
463 484
464 /* copy app data */ 485 /* copy app data */
465 if (!CRYPTO_dup_ex_data(bio_meth,&new->ex_data,&bio->ex_data)) 486 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new->ex_data,
466 goto err; 487 &bio->ex_data))
488 goto err;
467 489
468 if (ret == NULL) 490 if (ret == NULL)
469 { 491 {
@@ -483,37 +505,39 @@ err:
483 return(NULL); 505 return(NULL);
484 } 506 }
485 507
486void BIO_copy_next_retry(b) 508void BIO_copy_next_retry(BIO *b)
487BIO *b;
488 { 509 {
489 BIO_set_flags(b,BIO_get_retry_flags(b->next_bio)); 510 BIO_set_flags(b,BIO_get_retry_flags(b->next_bio));
490 b->retry_reason=b->next_bio->retry_reason; 511 b->retry_reason=b->next_bio->retry_reason;
491 } 512 }
492 513
493int BIO_get_ex_new_index(argl,argp,new_func,dup_func,free_func) 514int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
494long argl; 515 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
495char *argp; 516 {
496int (*new_func)(); 517 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
497int (*dup_func)(); 518 new_func, dup_func, free_func);
498void (*free_func)(); 519 }
499 { 520
500 bio_meth_num++; 521int BIO_set_ex_data(BIO *bio, int idx, void *data)
501 return(CRYPTO_get_ex_new_index(bio_meth_num-1,&bio_meth,
502 argl,argp,new_func,dup_func,free_func));
503 }
504
505int BIO_set_ex_data(bio,idx,data)
506BIO *bio;
507int idx;
508char *data;
509 { 522 {
510 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data)); 523 return(CRYPTO_set_ex_data(&(bio->ex_data),idx,data));
511 } 524 }
512 525
513char *BIO_get_ex_data(bio,idx) 526void *BIO_get_ex_data(BIO *bio, int idx)
514BIO *bio;
515int idx;
516 { 527 {
517 return(CRYPTO_get_ex_data(&(bio->ex_data),idx)); 528 return(CRYPTO_get_ex_data(&(bio->ex_data),idx));
518 } 529 }
519 530
531unsigned long BIO_number_read(BIO *bio)
532{
533 if(bio) return bio->num_read;
534 return 0;
535}
536
537unsigned long BIO_number_written(BIO *bio)
538{
539 if(bio) return bio->num_write;
540 return 0;
541}
542
543IMPLEMENT_STACK_OF(BIO)