| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/buffer.h> |
| 5 |
#include <openssl/crypto.h> |
| 6 |
#include <openssl/evp.h> |
| 7 |
#include <openssl/mem.h> |
| 8 |
#include <openssl/pkcs7.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include "../../fipsmodule/cipher/internal.h" |
| 11 |
#include "../internal.h" |
| 12 |
|
| 13 |
typedef struct enc_struct { |
| 14 |
uint8_t done; // indicates "EOF" for read, "flushed" for write |
| 15 |
uint8_t ok; // cipher status, either 0 (error) or 1 (ok) |
| 16 |
int buf_off; // start idx of buffered data |
| 17 |
int buf_len; // length of buffered data |
| 18 |
EVP_CIPHER_CTX *cipher; |
| 19 |
uint8_t buf[1024 * 4]; // plaintext for read, ciphertext for writes |
| 20 |
} BIO_ENC_CTX; |
| 21 |
|
| 22 |
static int enc_new(BIO *b) { |
| 23 |
BIO_ENC_CTX *ctx; |
| 24 |
GUARD_PTR(b); |
| 25 |
|
| 26 |
if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) { |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
ctx->cipher = EVP_CIPHER_CTX_new(); |
| 31 |
if (ctx->cipher == NULL) { |
| 32 |
OPENSSL_free(ctx); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
ctx->done = 0; |
| 36 |
ctx->ok = 1; |
| 37 |
ctx->buf_off = 0; |
| 38 |
ctx->buf_len = 0; |
| 39 |
BIO_set_data(b, ctx); |
| 40 |
BIO_set_init(b, 1); |
| 41 |
|
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
static int enc_free(BIO *b) { |
| 46 |
GUARD_PTR(b); |
| 47 |
|
| 48 |
BIO_ENC_CTX *ctx = BIO_get_data(b); |
| 49 |
if (ctx == NULL) { |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
EVP_CIPHER_CTX_free(ctx->cipher); |
| 54 |
OPENSSL_free(ctx); |
| 55 |
BIO_set_data(b, NULL); |
| 56 |
BIO_set_init(b, 0); |
| 57 |
|
| 58 |
return 1; |
| 59 |
} |
| 60 |
|
| 61 |
static int enc_read(BIO *b, char *out, int outl) { |
| 62 |
GUARD_PTR(b); |
| 63 |
GUARD_PTR(out); |
| 64 |
BIO_ENC_CTX *ctx = BIO_get_data(b); |
| 65 |
if (ctx == NULL || ctx->cipher == NULL || !ctx->ok || outl <= 0) { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
BIO *next = BIO_next(b); |
| 69 |
if (next == NULL) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int bytes_output = 0; |
| 74 |
int remaining = outl; |
| 75 |
uint8_t read_buf[sizeof(ctx->buf)]; |
| 76 |
const int cipher_block_size = EVP_CIPHER_CTX_block_size(ctx->cipher); |
| 77 |
while ((!ctx->done || ctx->buf_len > 0) && remaining > 0) { |
| 78 |
assert(bytes_output + remaining == outl); |
| 79 |
if (ctx->buf_len > 0) { |
| 80 |
uint8_t *out_pos = ((uint8_t *)out) + bytes_output; |
| 81 |
int to_copy = remaining > ctx->buf_len ? ctx->buf_len : remaining; |
| 82 |
OPENSSL_memcpy(out_pos, &ctx->buf[ctx->buf_off], to_copy); |
| 83 |
// Update buffer info and counters with number of bytes processed from our |
| 84 |
// buffer. |
| 85 |
ctx->buf_len -= to_copy; |
| 86 |
ctx->buf_off += to_copy; |
| 87 |
bytes_output += to_copy; |
| 88 |
remaining -= to_copy; |
| 89 |
continue; |
| 90 |
} |
| 91 |
ctx->buf_len = 0; |
| 92 |
ctx->buf_off = 0; |
| 93 |
// |EVP_DecryptUpdate| may write up to cipher_block_size-1 more bytes than |
| 94 |
// requested, so only read bytes we're sure we can decrypt in place. |
| 95 |
int to_read = (int)sizeof(ctx->buf) - cipher_block_size + 1; |
| 96 |
int bytes_read = BIO_read(next, read_buf, to_read); |
| 97 |
if (bytes_read > 0) { |
| 98 |
// Decrypt ciphertext in place, update |ctx->buf_len| with num bytes |
| 99 |
// decrypted. |
| 100 |
ctx->ok = EVP_DecryptUpdate(ctx->cipher, ctx->buf, &ctx->buf_len, |
| 101 |
read_buf, bytes_read); |
| 102 |
} else if (BIO_eof(next)) { |
| 103 |
// EVP_DecryptFinal_ex may write up to one block to our buffer. If that |
| 104 |
// happens, continue the loop to process the decrypted block as normal. |
| 105 |
ctx->ok = EVP_DecryptFinal_ex(ctx->cipher, ctx->buf, &ctx->buf_len); |
| 106 |
ctx->done = 1; // If we can't read any more bytes, set done. |
| 107 |
} else { |
| 108 |
// |BIO_read| returned <= 0, but no EOF. Copy retry and return. |
| 109 |
if (bytes_read < 0 && !BIO_should_retry(next)) { |
| 110 |
ctx->done = 1; |
| 111 |
ctx->ok = 0; |
| 112 |
} |
| 113 |
BIO_copy_next_retry(b); |
| 114 |
break; |
| 115 |
} |
| 116 |
if (!ctx->ok) { |
| 117 |
ctx->done = 1; // Set EOF on cipher error. |
| 118 |
} |
| 119 |
} |
| 120 |
return bytes_output; |
| 121 |
} |
| 122 |
|
| 123 |
static int enc_flush(BIO *b, BIO *next, BIO_ENC_CTX *ctx) { |
| 124 |
GUARD_PTR(b); |
| 125 |
GUARD_PTR(next); |
| 126 |
GUARD_PTR(ctx); |
| 127 |
while (ctx->ok > 0 && (ctx->buf_len > 0 || !ctx->done)) { |
| 128 |
int bytes_written = BIO_write(next, &ctx->buf[ctx->buf_off], ctx->buf_len); |
| 129 |
if (ctx->buf_len > 0 && bytes_written <= 0) { |
| 130 |
if (bytes_written < 0 && !BIO_should_retry(next)) { |
| 131 |
ctx->done = 1; |
| 132 |
ctx->ok = 0; |
| 133 |
} |
| 134 |
BIO_copy_next_retry(b); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
ctx->buf_off += bytes_written; |
| 138 |
ctx->buf_len -= bytes_written; |
| 139 |
if (ctx->buf_len == 0 && !ctx->done) { |
| 140 |
ctx->done = 1; |
| 141 |
ctx->buf_off = 0; |
| 142 |
ctx->ok = EVP_EncryptFinal_ex(ctx->cipher, ctx->buf, &ctx->buf_len); |
| 143 |
} |
| 144 |
} |
| 145 |
return ctx->ok; |
| 146 |
} |
| 147 |
|
| 148 |
static int enc_write(BIO *b, const char *in, int inl) { |
| 149 |
GUARD_PTR(b); |
| 150 |
GUARD_PTR(in); |
| 151 |
BIO_ENC_CTX *ctx = BIO_get_data(b); |
| 152 |
if (ctx == NULL || ctx->cipher == NULL || ctx->done || !ctx->ok || inl <= 0) { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
BIO *next = BIO_next(b); |
| 156 |
if (next == NULL) { |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
int bytes_consumed = 0; |
| 161 |
int remaining = inl; |
| 162 |
const int max_crypt_size = |
| 163 |
(int)sizeof(ctx->buf) - EVP_CIPHER_CTX_block_size(ctx->cipher) + 1; |
| 164 |
while ((!ctx->done || ctx->buf_len > 0) && remaining > 0) { |
| 165 |
assert(bytes_consumed + remaining == inl); |
| 166 |
if (ctx->buf_len == 0) { |
| 167 |
ctx->buf_off = 0; |
| 168 |
int to_encrypt = remaining < max_crypt_size ? remaining : max_crypt_size; |
| 169 |
uint8_t *in_pos = ((uint8_t *)in) + bytes_consumed; |
| 170 |
ctx->ok = EVP_EncryptUpdate(ctx->cipher, ctx->buf, &ctx->buf_len, in_pos, |
| 171 |
to_encrypt); |
| 172 |
if (!ctx->ok) { |
| 173 |
break; |
| 174 |
}; |
| 175 |
bytes_consumed += to_encrypt; |
| 176 |
remaining -= to_encrypt; |
| 177 |
} |
| 178 |
int bytes_written = BIO_write(next, &ctx->buf[ctx->buf_off], ctx->buf_len); |
| 179 |
if (bytes_written <= 0) { |
| 180 |
if (bytes_written < 0 && !BIO_should_retry(next)) { |
| 181 |
ctx->done = 1; |
| 182 |
ctx->ok = 0; |
| 183 |
} |
| 184 |
BIO_copy_next_retry(b); |
| 185 |
break; |
| 186 |
} |
| 187 |
ctx->buf_off += bytes_written; |
| 188 |
ctx->buf_len -= bytes_written; |
| 189 |
} |
| 190 |
return bytes_consumed; |
| 191 |
} |
| 192 |
|
| 193 |
static long enc_ctrl(BIO *b, int cmd, long num, void *ptr) { |
| 194 |
GUARD_PTR(b); |
| 195 |
long ret = 1; |
| 196 |
BIO_ENC_CTX *ctx = BIO_get_data(b); |
| 197 |
EVP_CIPHER_CTX **cipher_ctx; |
| 198 |
BIO *next = BIO_next(b); |
| 199 |
if (ctx == NULL) { |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
switch (cmd) { |
| 204 |
case BIO_CTRL_RESET: |
| 205 |
ctx->done = 0; |
| 206 |
ctx->ok = 1; |
| 207 |
ctx->buf_off = 0; |
| 208 |
ctx->buf_len = 0; |
| 209 |
OPENSSL_cleanse(ctx->buf, sizeof(ctx->buf)); |
| 210 |
if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL, |
| 211 |
EVP_CIPHER_CTX_encrypting(ctx->cipher))) { |
| 212 |
return 0; |
| 213 |
} |
| 214 |
ret = BIO_ctrl(next, cmd, num, ptr); |
| 215 |
break; |
| 216 |
case BIO_CTRL_EOF: |
| 217 |
if (ctx->done) { |
| 218 |
ret = 1; |
| 219 |
} else { |
| 220 |
ret = BIO_ctrl(next, cmd, num, ptr); |
| 221 |
} |
| 222 |
break; |
| 223 |
case BIO_CTRL_WPENDING: |
| 224 |
case BIO_CTRL_PENDING: |
| 225 |
// Return number of bytes left to process if we have anything buffered, |
| 226 |
// else consult underlying BIO. |
| 227 |
ret = ctx->buf_len; |
| 228 |
if (ret <= 0) { |
| 229 |
ret = BIO_ctrl(next, cmd, num, ptr); |
| 230 |
} |
| 231 |
break; |
| 232 |
case BIO_CTRL_FLUSH: |
| 233 |
ret = enc_flush(b, next, ctx); |
| 234 |
if (ret <= 0) { |
| 235 |
break; |
| 236 |
} |
| 237 |
// Flush the underlying BIO |
| 238 |
ret = BIO_ctrl(next, cmd, num, ptr); |
| 239 |
BIO_copy_next_retry(b); |
| 240 |
break; |
| 241 |
case BIO_C_GET_CIPHER_STATUS: |
| 242 |
ret = (long)ctx->ok; |
| 243 |
break; |
| 244 |
case BIO_C_GET_CIPHER_CTX: |
| 245 |
cipher_ctx = (EVP_CIPHER_CTX **)ptr; |
| 246 |
if (!cipher_ctx) { |
| 247 |
ret = 0; |
| 248 |
break; |
| 249 |
} |
| 250 |
*cipher_ctx = ctx->cipher; |
| 251 |
BIO_set_init(b, 1); |
| 252 |
break; |
| 253 |
// OpenSSL implements these, but because we don't need them and cipher BIO |
| 254 |
// is internal, we can fail loudly if they're called. If this case is hit, |
| 255 |
// it likely means you're making a change that will require implementing |
| 256 |
// these. |
| 257 |
case BIO_CTRL_DUP: |
| 258 |
case BIO_CTRL_GET_CALLBACK: |
| 259 |
case BIO_CTRL_SET_CALLBACK: |
| 260 |
case BIO_C_DO_STATE_MACHINE: |
| 261 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 262 |
return 0; |
| 263 |
default: |
| 264 |
ret = BIO_ctrl(next, cmd, num, ptr); |
| 265 |
break; |
| 266 |
} |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *key, |
| 271 |
const unsigned char *iv, int enc) { |
| 272 |
GUARD_PTR(b); |
| 273 |
GUARD_PTR(c); |
| 274 |
|
| 275 |
BIO_ENC_CTX *ctx = BIO_get_data(b); |
| 276 |
if (ctx == NULL) { |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
// We only support a modern subset of available EVP_CIPHERs. Other ciphers |
| 281 |
// (e.g. DES) and cipher modes (e.g. CBC, CCM) had issues with block alignment |
| 282 |
// and padding during testing, so they're forbidden for now. |
| 283 |
const EVP_CIPHER *kSupportedCiphers[] = { |
| 284 |
EVP_aes_128_cbc(), EVP_aes_128_ctr(), EVP_aes_128_ofb(), |
| 285 |
EVP_aes_256_cbc(), EVP_aes_256_ctr(), EVP_aes_256_ofb(), |
| 286 |
EVP_chacha20_poly1305(), EVP_des_ede3_cbc(), |
| 287 |
}; |
| 288 |
const size_t kSupportedCiphersCount = |
| 289 |
sizeof(kSupportedCiphers) / sizeof(EVP_CIPHER *); |
| 290 |
int supported = 0; |
| 291 |
for (size_t i = 0; i < kSupportedCiphersCount; i++) { |
| 292 |
if (c == kSupportedCiphers[i]) { |
| 293 |
supported = 1; |
| 294 |
break; |
| 295 |
} |
| 296 |
} |
| 297 |
if (!supported) { |
| 298 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_BIO_LIB); |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, key, iv, enc)) { |
| 303 |
return 0; |
| 304 |
} |
| 305 |
BIO_set_init(b, 1); |
| 306 |
|
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
static const BIO_METHOD methods_enc = { |
| 311 |
BIO_TYPE_CIPHER, // type |
| 312 |
"cipher", // name |
| 313 |
enc_write, // bwrite |
| 314 |
enc_read, // bread |
| 315 |
NULL, // bputs |
| 316 |
NULL, // bgets |
| 317 |
enc_ctrl, // ctrl |
| 318 |
enc_new, // create |
| 319 |
enc_free, // destroy |
| 320 |
NULL, // callback_ctrl |
| 321 |
}; |
| 322 |
|
| 323 |
const BIO_METHOD *BIO_f_cipher(void) { return &methods_enc; } |
| 324 |
|
| 325 |
int BIO_get_cipher_ctx(BIO *b, EVP_CIPHER_CTX **ctx) { |
| 326 |
return BIO_ctrl(b, BIO_C_GET_CIPHER_CTX, 0, ctx); |
| 327 |
} |
| 328 |
|
| 329 |
int BIO_get_cipher_status(BIO *b) { |
| 330 |
return BIO_ctrl(b, BIO_C_GET_CIPHER_STATUS, 0, NULL); |
| 331 |
} |