diff options
Diffstat (limited to 'src/crypto/bio/bio.c')
-rw-r--r-- | src/crypto/bio/bio.c | 165 |
1 files changed, 55 insertions, 110 deletions
diff --git a/src/crypto/bio/bio.c b/src/crypto/bio/bio.c index 5cab843b..4e889662 100644 --- a/src/crypto/bio/bio.c +++ b/src/crypto/bio/bio.c @@ -96,13 +96,6 @@ int BIO_free(BIO *bio) { return 0; } - if (bio->callback != NULL) { - int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1); - if (i <= 0) { - return i; - } - } - next_bio = BIO_pop(bio); if (bio->method != NULL && bio->method->destroy != NULL) { @@ -127,64 +120,61 @@ void BIO_free_all(BIO *bio) { BIO_free(bio); } -static int bio_io(BIO *bio, void *buf, int len, size_t method_offset, - int callback_flags, size_t *num) { - int i; - typedef int (*io_func_t)(BIO *, char *, int); - io_func_t io_func = NULL; - - if (bio != NULL && bio->method != NULL) { - io_func = - *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset)); - } - - if (io_func == NULL) { +int BIO_read(BIO *bio, void *buf, int len) { + if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); return -2; } - - if (bio->callback != NULL) { - i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L); - if (i <= 0) { - return i; - } - } - if (!bio->init) { OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); return -2; } - - i = 0; - if (buf != NULL && len > 0) { - i = io_func(bio, buf, len); - } - - if (i > 0) { - *num += i; + if (len <= 0) { + return 0; } - - if (bio->callback != NULL) { - i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L, - (long)i)); + int ret = bio->method->bread(bio, buf, len); + if (ret > 0) { + bio->num_read += ret; } - - return i; -} - -int BIO_read(BIO *bio, void *buf, int len) { - return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ, - &bio->num_read); + return ret; } int BIO_gets(BIO *bio, char *buf, int len) { - return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS, - &bio->num_read); + if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) { + OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); + return -2; + } + if (!bio->init) { + OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); + return -2; + } + if (len <= 0) { + return 0; + } + int ret = bio->method->bgets(bio, buf, len); + if (ret > 0) { + bio->num_read += ret; + } + return ret; } int BIO_write(BIO *bio, const void *in, int inl) { - return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite), - BIO_CB_WRITE, &bio->num_write); + if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) { + OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD); + return -2; + } + if (!bio->init) { + OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED); + return -2; + } + if (inl <= 0) { + return 0; + } + int ret = bio->method->bwrite(bio, in, inl); + if (ret > 0) { + bio->num_write += ret; + } + return ret; } int BIO_puts(BIO *bio, const char *in) { @@ -196,8 +186,6 @@ int BIO_flush(BIO *bio) { } long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { - long ret; - if (bio == NULL) { return 0; } @@ -207,20 +195,7 @@ long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) { return -2; } - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1); - if (ret <= 0) { - return ret; - } - } - - ret = bio->method->ctrl(bio, cmd, larg, parg); - - if (bio->callback != NULL) { - ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret); - } - - return ret; + return bio->method->ctrl(bio, cmd, larg, parg); } char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) { @@ -305,9 +280,6 @@ void BIO_copy_next_retry(BIO *bio) { } long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { - long ret; - bio_info_cb cb; - if (bio == NULL) { return 0; } @@ -317,22 +289,7 @@ long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { return 0; } - cb = bio->callback; - - if (cb != NULL) { - ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L); - if (ret <= 0) { - return ret; - } - } - - ret = bio->method->callback_ctrl(bio, cmd, fp); - - if (cb != NULL) { - ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret); - } - - return ret; + return bio->method->callback_ctrl(bio, cmd, fp); } size_t BIO_pending(const BIO *bio) { @@ -363,18 +320,6 @@ int BIO_set_close(BIO *bio, int close_flag) { return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL); } -void BIO_set_callback(BIO *bio, bio_info_cb callback_func) { - bio->callback = callback_func; -} - -void BIO_set_callback_arg(BIO *bio, char *arg) { - bio->cb_arg = arg; -} - -char *BIO_get_callback_arg(const BIO *bio) { - return bio->cb_arg; -} - OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) { return bio->num_read; } @@ -464,14 +409,14 @@ void ERR_print_errors(BIO *bio) { ERR_print_errors_cb(print_bio, bio); } -/* bio_read_all reads everything from |bio| and prepends |prefix| to it. On - * success, |*out| is set to an allocated buffer (which should be freed with - * |OPENSSL_free|), |*out_len| is set to its length and one is returned. The - * buffer will contain |prefix| followed by the contents of |bio|. On failure, - * zero is returned. - * - * The function will fail if the size of the output would equal or exceed - * |max_len|. */ +// bio_read_all reads everything from |bio| and prepends |prefix| to it. On +// success, |*out| is set to an allocated buffer (which should be freed with +// |OPENSSL_free|), |*out_len| is set to its length and one is returned. The +// buffer will contain |prefix| followed by the contents of |bio|. On failure, +// zero is returned. +// +// The function will fail if the size of the output would equal or exceed +// |max_len|. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len, const uint8_t *prefix, size_t prefix_len, size_t max_len) { @@ -535,20 +480,20 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) { const uint8_t length_byte = header[1]; if ((tag & 0x1f) == 0x1f) { - /* Long form tags are not supported. */ + // Long form tags are not supported. return 0; } size_t len, header_len; if ((length_byte & 0x80) == 0) { - /* Short form length. */ + // Short form length. len = length_byte; header_len = kInitialHeaderLen; } else { const size_t num_bytes = length_byte & 0x7f; if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) { - /* indefinite length. */ + // indefinite length. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen, max_len); } @@ -571,12 +516,12 @@ int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) { } if (len32 < 128) { - /* Length should have used short-form encoding. */ + // Length should have used short-form encoding. return 0; } if ((len32 >> ((num_bytes-1)*8)) == 0) { - /* Length should have been at least one byte shorter. */ + // Length should have been at least one byte shorter. return 0; } |