diff options
Diffstat (limited to 'src/lib/libcrypto/evp/bio_b64.c')
| -rw-r--r-- | src/lib/libcrypto/evp/bio_b64.c | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/src/lib/libcrypto/evp/bio_b64.c b/src/lib/libcrypto/evp/bio_b64.c index 73172b9a07..f12eac1b55 100644 --- a/src/lib/libcrypto/evp/bio_b64.c +++ b/src/lib/libcrypto/evp/bio_b64.c | |||
| @@ -59,27 +59,17 @@ | |||
| 59 | #include <stdio.h> | 59 | #include <stdio.h> |
| 60 | #include <errno.h> | 60 | #include <errno.h> |
| 61 | #include "cryptlib.h" | 61 | #include "cryptlib.h" |
| 62 | #include "buffer.h" | 62 | #include <openssl/buffer.h> |
| 63 | #include "evp.h" | 63 | #include <openssl/evp.h> |
| 64 | 64 | ||
| 65 | #ifndef NOPROTO | 65 | static int b64_write(BIO *h, const char *buf, int num); |
| 66 | static int b64_write(BIO *h,char *buf,int num); | 66 | static int b64_read(BIO *h, char *buf, int size); |
| 67 | static int b64_read(BIO *h,char *buf,int size); | 67 | /*static int b64_puts(BIO *h, const char *str); */ |
| 68 | /*static int b64_puts(BIO *h,char *str); */ | 68 | /*static int b64_gets(BIO *h, char *str, int size); */ |
| 69 | /*static int b64_gets(BIO *h,char *str,int size); */ | 69 | static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
| 70 | static long b64_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 71 | static int b64_new(BIO *h); | 70 | static int b64_new(BIO *h); |
| 72 | static int b64_free(BIO *data); | 71 | static int b64_free(BIO *data); |
| 73 | #else | 72 | static long b64_callback_ctrl(BIO *h,int cmd,bio_info_cb *fp); |
| 74 | static int b64_write(); | ||
| 75 | static int b64_read(); | ||
| 76 | /*static int b64_puts(); */ | ||
| 77 | /*static int b64_gets(); */ | ||
| 78 | static long b64_ctrl(); | ||
| 79 | static int b64_new(); | ||
| 80 | static int b64_free(); | ||
| 81 | #endif | ||
| 82 | |||
| 83 | #define B64_BLOCK_SIZE 1024 | 73 | #define B64_BLOCK_SIZE 1024 |
| 84 | #define B64_BLOCK_SIZE2 768 | 74 | #define B64_BLOCK_SIZE2 768 |
| 85 | #define B64_NONE 0 | 75 | #define B64_NONE 0 |
| @@ -111,19 +101,19 @@ static BIO_METHOD methods_b64= | |||
| 111 | b64_ctrl, | 101 | b64_ctrl, |
| 112 | b64_new, | 102 | b64_new, |
| 113 | b64_free, | 103 | b64_free, |
| 104 | b64_callback_ctrl, | ||
| 114 | }; | 105 | }; |
| 115 | 106 | ||
| 116 | BIO_METHOD *BIO_f_base64() | 107 | BIO_METHOD *BIO_f_base64(void) |
| 117 | { | 108 | { |
| 118 | return(&methods_b64); | 109 | return(&methods_b64); |
| 119 | } | 110 | } |
| 120 | 111 | ||
| 121 | static int b64_new(bi) | 112 | static int b64_new(BIO *bi) |
| 122 | BIO *bi; | ||
| 123 | { | 113 | { |
| 124 | BIO_B64_CTX *ctx; | 114 | BIO_B64_CTX *ctx; |
| 125 | 115 | ||
| 126 | ctx=(BIO_B64_CTX *)Malloc(sizeof(BIO_B64_CTX)); | 116 | ctx=(BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX)); |
| 127 | if (ctx == NULL) return(0); | 117 | if (ctx == NULL) return(0); |
| 128 | 118 | ||
| 129 | ctx->buf_len=0; | 119 | ctx->buf_len=0; |
| @@ -140,21 +130,17 @@ BIO *bi; | |||
| 140 | return(1); | 130 | return(1); |
| 141 | } | 131 | } |
| 142 | 132 | ||
| 143 | static int b64_free(a) | 133 | static int b64_free(BIO *a) |
| 144 | BIO *a; | ||
| 145 | { | 134 | { |
| 146 | if (a == NULL) return(0); | 135 | if (a == NULL) return(0); |
| 147 | Free(a->ptr); | 136 | OPENSSL_free(a->ptr); |
| 148 | a->ptr=NULL; | 137 | a->ptr=NULL; |
| 149 | a->init=0; | 138 | a->init=0; |
| 150 | a->flags=0; | 139 | a->flags=0; |
| 151 | return(1); | 140 | return(1); |
| 152 | } | 141 | } |
| 153 | 142 | ||
| 154 | static int b64_read(b,out,outl) | 143 | static int b64_read(BIO *b, char *out, int outl) |
| 155 | BIO *b; | ||
| 156 | char *out; | ||
| 157 | int outl; | ||
| 158 | { | 144 | { |
| 159 | int ret=0,i,ii,j,k,x,n,num,ret_code=0; | 145 | int ret=0,i,ii,j,k,x,n,num,ret_code=0; |
| 160 | BIO_B64_CTX *ctx; | 146 | BIO_B64_CTX *ctx; |
| @@ -253,8 +239,8 @@ int outl; | |||
| 253 | &(ctx->tmp[0])); | 239 | &(ctx->tmp[0])); |
| 254 | for (x=0; x < i; x++) | 240 | for (x=0; x < i; x++) |
| 255 | ctx->tmp[x]=p[x]; | 241 | ctx->tmp[x]=p[x]; |
| 256 | EVP_DecodeInit(&ctx->base64); | ||
| 257 | } | 242 | } |
| 243 | EVP_DecodeInit(&ctx->base64); | ||
| 258 | ctx->start=0; | 244 | ctx->start=0; |
| 259 | break; | 245 | break; |
| 260 | } | 246 | } |
| @@ -354,10 +340,7 @@ int outl; | |||
| 354 | return((ret == 0)?ret_code:ret); | 340 | return((ret == 0)?ret_code:ret); |
| 355 | } | 341 | } |
| 356 | 342 | ||
| 357 | static int b64_write(b,in,inl) | 343 | static int b64_write(BIO *b, const char *in, int inl) |
| 358 | BIO *b; | ||
| 359 | char *in; | ||
| 360 | int inl; | ||
| 361 | { | 344 | { |
| 362 | int ret=inl,n,i; | 345 | int ret=inl,n,i; |
| 363 | BIO_B64_CTX *ctx; | 346 | BIO_B64_CTX *ctx; |
| @@ -387,10 +370,11 @@ int inl; | |||
| 387 | n-=i; | 370 | n-=i; |
| 388 | } | 371 | } |
| 389 | /* at this point all pending data has been written */ | 372 | /* at this point all pending data has been written */ |
| 373 | ctx->buf_off=0; | ||
| 374 | ctx->buf_len=0; | ||
| 390 | 375 | ||
| 391 | if ((in == NULL) || (inl <= 0)) return(0); | 376 | if ((in == NULL) || (inl <= 0)) return(0); |
| 392 | 377 | ||
| 393 | ctx->buf_off=0; | ||
| 394 | while (inl > 0) | 378 | while (inl > 0) |
| 395 | { | 379 | { |
| 396 | n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl; | 380 | n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl; |
| @@ -400,14 +384,20 @@ int inl; | |||
| 400 | if (ctx->tmp_len > 0) | 384 | if (ctx->tmp_len > 0) |
| 401 | { | 385 | { |
| 402 | n=3-ctx->tmp_len; | 386 | n=3-ctx->tmp_len; |
| 387 | /* There's a teoretical possibility for this */ | ||
| 388 | if (n > inl) | ||
| 389 | n=inl; | ||
| 403 | memcpy(&(ctx->tmp[ctx->tmp_len]),in,n); | 390 | memcpy(&(ctx->tmp[ctx->tmp_len]),in,n); |
| 404 | ctx->tmp_len+=n; | 391 | ctx->tmp_len+=n; |
| 405 | n=ctx->tmp_len; | 392 | if (ctx->tmp_len < 3) |
| 406 | if (n < 3) | ||
| 407 | break; | 393 | break; |
| 408 | ctx->buf_len=EVP_EncodeBlock( | 394 | ctx->buf_len=EVP_EncodeBlock( |
| 409 | (unsigned char *)ctx->buf, | 395 | (unsigned char *)ctx->buf, |
| 410 | (unsigned char *)ctx->tmp,n); | 396 | (unsigned char *)ctx->tmp, |
| 397 | ctx->tmp_len); | ||
| 398 | /* Since we're now done using the temporary | ||
| 399 | buffer, the length should be 0'd */ | ||
| 400 | ctx->tmp_len=0; | ||
| 411 | } | 401 | } |
| 412 | else | 402 | else |
| 413 | { | 403 | { |
| @@ -451,11 +441,7 @@ int inl; | |||
| 451 | return(ret); | 441 | return(ret); |
| 452 | } | 442 | } |
| 453 | 443 | ||
| 454 | static long b64_ctrl(b,cmd,num,ptr) | 444 | static long b64_ctrl(BIO *b, int cmd, long num, void *ptr) |
| 455 | BIO *b; | ||
| 456 | int cmd; | ||
| 457 | long num; | ||
| 458 | char *ptr; | ||
| 459 | { | 445 | { |
| 460 | BIO_B64_CTX *ctx; | 446 | BIO_B64_CTX *ctx; |
| 461 | long ret=1; | 447 | long ret=1; |
| @@ -479,7 +465,8 @@ char *ptr; | |||
| 479 | break; | 465 | break; |
| 480 | case BIO_CTRL_WPENDING: /* More to write in buffer */ | 466 | case BIO_CTRL_WPENDING: /* More to write in buffer */ |
| 481 | ret=ctx->buf_len-ctx->buf_off; | 467 | ret=ctx->buf_len-ctx->buf_off; |
| 482 | if ((ret == 0) && (ctx->base64.num != 0)) | 468 | if ((ret == 0) && (ctx->encode != B64_NONE) |
| 469 | && (ctx->base64.num != 0)) | ||
| 483 | ret=1; | 470 | ret=1; |
| 484 | else if (ret <= 0) | 471 | else if (ret <= 0) |
| 485 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | 472 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); |
| @@ -514,7 +501,7 @@ again: | |||
| 514 | goto again; | 501 | goto again; |
| 515 | } | 502 | } |
| 516 | } | 503 | } |
| 517 | else if (ctx->base64.num != 0) | 504 | else if (ctx->encode != B64_NONE && ctx->base64.num != 0) |
| 518 | { | 505 | { |
| 519 | ctx->buf_off=0; | 506 | ctx->buf_off=0; |
| 520 | EVP_EncodeFinal(&(ctx->base64), | 507 | EVP_EncodeFinal(&(ctx->base64), |
| @@ -545,3 +532,17 @@ again: | |||
| 545 | return(ret); | 532 | return(ret); |
| 546 | } | 533 | } |
| 547 | 534 | ||
| 535 | static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 536 | { | ||
| 537 | long ret=1; | ||
| 538 | |||
| 539 | if (b->next_bio == NULL) return(0); | ||
| 540 | switch (cmd) | ||
| 541 | { | ||
| 542 | default: | ||
| 543 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 544 | break; | ||
| 545 | } | ||
| 546 | return(ret); | ||
| 547 | } | ||
| 548 | |||
