Blame crypto/evp/bio_enc.c

Packit Service 084de1
/*
Packit Service 084de1
 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
Packit Service 084de1
 *
Packit Service 084de1
 * Licensed under the OpenSSL license (the "License").  You may not use
Packit Service 084de1
 * this file except in compliance with the License.  You can obtain a copy
Packit Service 084de1
 * in the file LICENSE in the source distribution or at
Packit Service 084de1
 * https://www.openssl.org/source/license.html
Packit Service 084de1
 */
Packit Service 084de1
Packit Service 084de1
#include <stdio.h>
Packit Service 084de1
#include <errno.h>
Packit Service 084de1
#include "internal/cryptlib.h"
Packit Service 084de1
#include <openssl/buffer.h>
Packit Service 084de1
#include <openssl/evp.h>
Packit Service 084de1
#include "internal/bio.h"
Packit Service 084de1
Packit Service 084de1
static int enc_write(BIO *h, const char *buf, int num);
Packit Service 084de1
static int enc_read(BIO *h, char *buf, int size);
Packit Service 084de1
static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
Packit Service 084de1
static int enc_new(BIO *h);
Packit Service 084de1
static int enc_free(BIO *data);
Packit Service 084de1
static long enc_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fps);
Packit Service 084de1
#define ENC_BLOCK_SIZE  (1024*4)
Packit Service 084de1
#define ENC_MIN_CHUNK   (256)
Packit Service 084de1
#define BUF_OFFSET      (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH)
Packit Service 084de1
Packit Service 084de1
typedef struct enc_struct {
Packit Service 084de1
    int buf_len;
Packit Service 084de1
    int buf_off;
Packit Service 084de1
    int cont;                   /* <= 0 when finished */
Packit Service 084de1
    int finished;
Packit Service 084de1
    int ok;                     /* bad decrypt */
Packit Service 084de1
    EVP_CIPHER_CTX *cipher;
Packit Service 084de1
    unsigned char *read_start, *read_end;
Packit Service 084de1
    /*
Packit Service 084de1
     * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
Packit Service 084de1
     * up to a block more data than is presented to it
Packit Service 084de1
     */
Packit Service 084de1
    unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
Packit Service 084de1
} BIO_ENC_CTX;
Packit Service 084de1
Packit Service 084de1
static const BIO_METHOD methods_enc = {
Packit Service 084de1
    BIO_TYPE_CIPHER,
Packit Service 084de1
    "cipher",
Packit Service 084de1
    /* TODO: Convert to new style write function */
Packit Service 084de1
    bwrite_conv,
Packit Service 084de1
    enc_write,
Packit Service 084de1
    /* TODO: Convert to new style read function */
Packit Service 084de1
    bread_conv,
Packit Service 084de1
    enc_read,
Packit Service 084de1
    NULL,                       /* enc_puts, */
Packit Service 084de1
    NULL,                       /* enc_gets, */
Packit Service 084de1
    enc_ctrl,
Packit Service 084de1
    enc_new,
Packit Service 084de1
    enc_free,
Packit Service 084de1
    enc_callback_ctrl,
Packit Service 084de1
};
Packit Service 084de1
Packit Service 084de1
const BIO_METHOD *BIO_f_cipher(void)
Packit Service 084de1
{
Packit Service 084de1
    return &methods_enc;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static int enc_new(BIO *bi)
Packit Service 084de1
{
Packit Service 084de1
    BIO_ENC_CTX *ctx;
Packit Service 084de1
Packit Service 084de1
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
Packit Service 084de1
        EVPerr(EVP_F_ENC_NEW, ERR_R_MALLOC_FAILURE);
Packit Service 084de1
        return 0;
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    ctx->cipher = EVP_CIPHER_CTX_new();
Packit Service 084de1
    if (ctx->cipher == NULL) {
Packit Service 084de1
        OPENSSL_free(ctx);
Packit Service 084de1
        return 0;
Packit Service 084de1
    }
Packit Service 084de1
    ctx->cont = 1;
Packit Service 084de1
    ctx->ok = 1;
Packit Service 084de1
    ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
Packit Service 084de1
    BIO_set_data(bi, ctx);
Packit Service 084de1
    BIO_set_init(bi, 1);
Packit Service 084de1
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static int enc_free(BIO *a)
Packit Service 084de1
{
Packit Service 084de1
    BIO_ENC_CTX *b;
Packit Service 084de1
Packit Service 084de1
    if (a == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    b = BIO_get_data(a);
Packit Service 084de1
    if (b == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    EVP_CIPHER_CTX_free(b->cipher);
Packit Service 084de1
    OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
Packit Service 084de1
    BIO_set_data(a, NULL);
Packit Service 084de1
    BIO_set_init(a, 0);
Packit Service 084de1
Packit Service 084de1
    return 1;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static int enc_read(BIO *b, char *out, int outl)
Packit Service 084de1
{
Packit Service 084de1
    int ret = 0, i, blocksize;
Packit Service 084de1
    BIO_ENC_CTX *ctx;
Packit Service 084de1
    BIO *next;
Packit Service 084de1
Packit Service 084de1
    if (out == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
    ctx = BIO_get_data(b);
Packit Service 084de1
Packit Service 084de1
    next = BIO_next(b);
Packit Service 084de1
    if ((ctx == NULL) || (next == NULL))
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    /* First check if there are bytes decoded/encoded */
Packit Service 084de1
    if (ctx->buf_len > 0) {
Packit Service 084de1
        i = ctx->buf_len - ctx->buf_off;
Packit Service 084de1
        if (i > outl)
Packit Service 084de1
            i = outl;
Packit Service 084de1
        memcpy(out, &(ctx->buf[ctx->buf_off]), i);
Packit Service 084de1
        ret = i;
Packit Service 084de1
        out += i;
Packit Service 084de1
        outl -= i;
Packit Service 084de1
        ctx->buf_off += i;
Packit Service 084de1
        if (ctx->buf_len == ctx->buf_off) {
Packit Service 084de1
            ctx->buf_len = 0;
Packit Service 084de1
            ctx->buf_off = 0;
Packit Service 084de1
        }
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
Packit Service 084de1
    if (blocksize == 1)
Packit Service 084de1
        blocksize = 0;
Packit Service 084de1
Packit Service 084de1
    /*
Packit Service 084de1
     * At this point, we have room of outl bytes and an empty buffer, so we
Packit Service 084de1
     * should read in some more.
Packit Service 084de1
     */
Packit Service 084de1
Packit Service 084de1
    while (outl > 0) {
Packit Service 084de1
        if (ctx->cont <= 0)
Packit Service 084de1
            break;
Packit Service 084de1
Packit Service 084de1
        if (ctx->read_start == ctx->read_end) { /* time to read more data */
Packit Service 084de1
            ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
Packit Service 084de1
            i = BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
Packit Service 084de1
            if (i > 0)
Packit Service 084de1
                ctx->read_end += i;
Packit Service 084de1
        } else {
Packit Service 084de1
            i = ctx->read_end - ctx->read_start;
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        if (i <= 0) {
Packit Service 084de1
            /* Should be continue next time we are called? */
Packit Service 084de1
            if (!BIO_should_retry(next)) {
Packit Service 084de1
                ctx->cont = i;
Packit Service 084de1
                i = EVP_CipherFinal_ex(ctx->cipher,
Packit Service 084de1
                                       ctx->buf, &(ctx->buf_len));
Packit Service 084de1
                ctx->ok = i;
Packit Service 084de1
                ctx->buf_off = 0;
Packit Service 084de1
            } else {
Packit Service 084de1
                ret = (ret == 0) ? i : ret;
Packit Service 084de1
                break;
Packit Service 084de1
            }
Packit Service 084de1
        } else {
Packit Service 084de1
            if (outl > ENC_MIN_CHUNK) {
Packit Service 084de1
                /*
Packit Service 084de1
                 * Depending on flags block cipher decrypt can write
Packit Service 084de1
                 * one extra block and then back off, i.e. output buffer
Packit Service 084de1
                 * has to accommodate extra block...
Packit Service 084de1
                 */
Packit Service 084de1
                int j = outl - blocksize, buf_len;
Packit Service 084de1
Packit Service 084de1
                if (!EVP_CipherUpdate(ctx->cipher,
Packit Service 084de1
                                      (unsigned char *)out, &buf_len,
Packit Service 084de1
                                      ctx->read_start, i > j ? j : i)) {
Packit Service 084de1
                    BIO_clear_retry_flags(b);
Packit Service 084de1
                    return 0;
Packit Service 084de1
                }
Packit Service 084de1
                ret += buf_len;
Packit Service 084de1
                out += buf_len;
Packit Service 084de1
                outl -= buf_len;
Packit Service 084de1
Packit Service 084de1
                if ((i -= j) <= 0) {
Packit Service 084de1
                    ctx->read_start = ctx->read_end;
Packit Service 084de1
                    continue;
Packit Service 084de1
                }
Packit Service 084de1
                ctx->read_start += j;
Packit Service 084de1
            }
Packit Service 084de1
            if (i > ENC_MIN_CHUNK)
Packit Service 084de1
                i = ENC_MIN_CHUNK;
Packit Service 084de1
            if (!EVP_CipherUpdate(ctx->cipher,
Packit Service 084de1
                                  ctx->buf, &ctx->buf_len,
Packit Service 084de1
                                  ctx->read_start, i)) {
Packit Service 084de1
                BIO_clear_retry_flags(b);
Packit Service 084de1
                ctx->ok = 0;
Packit Service 084de1
                return 0;
Packit Service 084de1
            }
Packit Service 084de1
            ctx->read_start += i;
Packit Service 084de1
            ctx->cont = 1;
Packit Service 084de1
            /*
Packit Service 084de1
             * Note: it is possible for EVP_CipherUpdate to decrypt zero
Packit Service 084de1
             * bytes because this is or looks like the final block: if this
Packit Service 084de1
             * happens we should retry and either read more data or decrypt
Packit Service 084de1
             * the final block
Packit Service 084de1
             */
Packit Service 084de1
            if (ctx->buf_len == 0)
Packit Service 084de1
                continue;
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        if (ctx->buf_len <= outl)
Packit Service 084de1
            i = ctx->buf_len;
Packit Service 084de1
        else
Packit Service 084de1
            i = outl;
Packit Service 084de1
        if (i <= 0)
Packit Service 084de1
            break;
Packit Service 084de1
        memcpy(out, ctx->buf, i);
Packit Service 084de1
        ret += i;
Packit Service 084de1
        ctx->buf_off = i;
Packit Service 084de1
        outl -= i;
Packit Service 084de1
        out += i;
Packit Service 084de1
    }
Packit Service 084de1
Packit Service 084de1
    BIO_clear_retry_flags(b);
Packit Service 084de1
    BIO_copy_next_retry(b);
Packit Service 084de1
    return ((ret == 0) ? ctx->cont : ret);
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static int enc_write(BIO *b, const char *in, int inl)
Packit Service 084de1
{
Packit Service 084de1
    int ret = 0, n, i;
Packit Service 084de1
    BIO_ENC_CTX *ctx;
Packit Service 084de1
    BIO *next;
Packit Service 084de1
Packit Service 084de1
    ctx = BIO_get_data(b);
Packit Service 084de1
    next = BIO_next(b);
Packit Service 084de1
    if ((ctx == NULL) || (next == NULL))
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    ret = inl;
Packit Service 084de1
Packit Service 084de1
    BIO_clear_retry_flags(b);
Packit Service 084de1
    n = ctx->buf_len - ctx->buf_off;
Packit Service 084de1
    while (n > 0) {
Packit Service 084de1
        i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
Packit Service 084de1
        if (i <= 0) {
Packit Service 084de1
            BIO_copy_next_retry(b);
Packit Service 084de1
            return i;
Packit Service 084de1
        }
Packit Service 084de1
        ctx->buf_off += i;
Packit Service 084de1
        n -= i;
Packit Service 084de1
    }
Packit Service 084de1
    /* at this point all pending data has been written */
Packit Service 084de1
Packit Service 084de1
    if ((in == NULL) || (inl <= 0))
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    ctx->buf_off = 0;
Packit Service 084de1
    while (inl > 0) {
Packit Service 084de1
        n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
Packit Service 084de1
        if (!EVP_CipherUpdate(ctx->cipher,
Packit Service 084de1
                              ctx->buf, &ctx->buf_len,
Packit Service 084de1
                              (const unsigned char *)in, n)) {
Packit Service 084de1
            BIO_clear_retry_flags(b);
Packit Service 084de1
            ctx->ok = 0;
Packit Service 084de1
            return 0;
Packit Service 084de1
        }
Packit Service 084de1
        inl -= n;
Packit Service 084de1
        in += n;
Packit Service 084de1
Packit Service 084de1
        ctx->buf_off = 0;
Packit Service 084de1
        n = ctx->buf_len;
Packit Service 084de1
        while (n > 0) {
Packit Service 084de1
            i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
Packit Service 084de1
            if (i <= 0) {
Packit Service 084de1
                BIO_copy_next_retry(b);
Packit Service 084de1
                return (ret == inl) ? i : ret - inl;
Packit Service 084de1
            }
Packit Service 084de1
            n -= i;
Packit Service 084de1
            ctx->buf_off += i;
Packit Service 084de1
        }
Packit Service 084de1
        ctx->buf_len = 0;
Packit Service 084de1
        ctx->buf_off = 0;
Packit Service 084de1
    }
Packit Service 084de1
    BIO_copy_next_retry(b);
Packit Service 084de1
    return ret;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
Packit Service 084de1
{
Packit Service 084de1
    BIO *dbio;
Packit Service 084de1
    BIO_ENC_CTX *ctx, *dctx;
Packit Service 084de1
    long ret = 1;
Packit Service 084de1
    int i;
Packit Service 084de1
    EVP_CIPHER_CTX **c_ctx;
Packit Service 084de1
    BIO *next;
Packit Service 084de1
Packit Service 084de1
    ctx = BIO_get_data(b);
Packit Service 084de1
    next = BIO_next(b);
Packit Service 084de1
    if (ctx == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    switch (cmd) {
Packit Service 084de1
    case BIO_CTRL_RESET:
Packit Service 084de1
        ctx->ok = 1;
Packit Service 084de1
        ctx->finished = 0;
Packit Service 084de1
        if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
Packit Service 084de1
                               EVP_CIPHER_CTX_encrypting(ctx->cipher)))
Packit Service 084de1
            return 0;
Packit Service 084de1
        ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_CTRL_EOF:         /* More to read */
Packit Service 084de1
        if (ctx->cont <= 0)
Packit Service 084de1
            ret = 1;
Packit Service 084de1
        else
Packit Service 084de1
            ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_CTRL_WPENDING:
Packit Service 084de1
        ret = ctx->buf_len - ctx->buf_off;
Packit Service 084de1
        if (ret <= 0)
Packit Service 084de1
            ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_CTRL_PENDING:     /* More to read in buffer */
Packit Service 084de1
        ret = ctx->buf_len - ctx->buf_off;
Packit Service 084de1
        if (ret <= 0)
Packit Service 084de1
            ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_CTRL_FLUSH:
Packit Service 084de1
        /* do a final write */
Packit Service 084de1
 again:
Packit Service 084de1
        while (ctx->buf_len != ctx->buf_off) {
Packit Service 084de1
            i = enc_write(b, NULL, 0);
Packit Service 084de1
            if (i < 0)
Packit Service 084de1
                return i;
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        if (!ctx->finished) {
Packit Service 084de1
            ctx->finished = 1;
Packit Service 084de1
            ctx->buf_off = 0;
Packit Service 084de1
            ret = EVP_CipherFinal_ex(ctx->cipher,
Packit Service 084de1
                                     (unsigned char *)ctx->buf,
Packit Service 084de1
                                     &(ctx->buf_len));
Packit Service 084de1
            ctx->ok = (int)ret;
Packit Service 084de1
            if (ret <= 0)
Packit Service 084de1
                break;
Packit Service 084de1
Packit Service 084de1
            /* push out the bytes */
Packit Service 084de1
            goto again;
Packit Service 084de1
        }
Packit Service 084de1
Packit Service 084de1
        /* Finally flush the underlying BIO */
Packit Service 084de1
        ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_C_GET_CIPHER_STATUS:
Packit Service 084de1
        ret = (long)ctx->ok;
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_C_DO_STATE_MACHINE:
Packit Service 084de1
        BIO_clear_retry_flags(b);
Packit Service 084de1
        ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        BIO_copy_next_retry(b);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_C_GET_CIPHER_CTX:
Packit Service 084de1
        c_ctx = (EVP_CIPHER_CTX **)ptr;
Packit Service 084de1
        *c_ctx = ctx->cipher;
Packit Service 084de1
        BIO_set_init(b, 1);
Packit Service 084de1
        break;
Packit Service 084de1
    case BIO_CTRL_DUP:
Packit Service 084de1
        dbio = (BIO *)ptr;
Packit Service 084de1
        dctx = BIO_get_data(dbio);
Packit Service 084de1
        dctx->cipher = EVP_CIPHER_CTX_new();
Packit Service 084de1
        if (dctx->cipher == NULL)
Packit Service 084de1
            return 0;
Packit Service 084de1
        ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
Packit Service 084de1
        if (ret)
Packit Service 084de1
            BIO_set_init(dbio, 1);
Packit Service 084de1
        break;
Packit Service 084de1
    default:
Packit Service 084de1
        ret = BIO_ctrl(next, cmd, num, ptr);
Packit Service 084de1
        break;
Packit Service 084de1
    }
Packit Service 084de1
    return ret;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
static long enc_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
Packit Service 084de1
{
Packit Service 084de1
    long ret = 1;
Packit Service 084de1
    BIO *next = BIO_next(b);
Packit Service 084de1
Packit Service 084de1
    if (next == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
    switch (cmd) {
Packit Service 084de1
    default:
Packit Service 084de1
        ret = BIO_callback_ctrl(next, cmd, fp);
Packit Service 084de1
        break;
Packit Service 084de1
    }
Packit Service 084de1
    return ret;
Packit Service 084de1
}
Packit Service 084de1
Packit Service 084de1
int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
Packit Service 084de1
                   const unsigned char *i, int e)
Packit Service 084de1
{
Packit Service 084de1
    BIO_ENC_CTX *ctx;
Packit Service 084de1
    long (*callback) (struct bio_st *, int, const char *, int, long, long);
Packit Service 084de1
Packit Service 084de1
    ctx = BIO_get_data(b);
Packit Service 084de1
    if (ctx == NULL)
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    callback = BIO_get_callback(b);
Packit Service 084de1
Packit Service 084de1
    if ((callback != NULL) &&
Packit Service 084de1
            (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
Packit Service 084de1
                      0L) <= 0))
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    BIO_set_init(b, 1);
Packit Service 084de1
Packit Service 084de1
    if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
Packit Service 084de1
        return 0;
Packit Service 084de1
Packit Service 084de1
    if (callback != NULL)
Packit Service 084de1
        return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
Packit Service 084de1
    return 1;
Packit Service 084de1
}