2022-05-11 01:50:00 +08:00
|
|
|
/*
|
2022-05-13 00:00:59 +08:00
|
|
|
* Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2022-05-11 01:50:00 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/core_names.h>
|
2022-05-13 00:00:59 +08:00
|
|
|
#include <openssl/rand.h>
|
2022-05-11 01:50:00 +08:00
|
|
|
#include "../../ssl_local.h"
|
|
|
|
#include "../record_local.h"
|
|
|
|
#include "recmethod_local.h"
|
2022-05-13 00:00:59 +08:00
|
|
|
#include "internal/ktls.h"
|
|
|
|
|
2022-09-26 23:35:30 +08:00
|
|
|
static struct record_functions_st ossl_ktls_funcs;
|
|
|
|
|
2022-05-13 00:00:59 +08:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
# include "crypto/cryptodev.h"
|
|
|
|
|
2022-05-20 23:54:12 +08:00
|
|
|
/*-
|
|
|
|
* Check if a given cipher is supported by the KTLS interface.
|
|
|
|
* The kernel might still fail the setsockopt() if no suitable
|
|
|
|
* provider is found, but this checks if the socket option
|
|
|
|
* supports the cipher suite used at all.
|
|
|
|
*/
|
|
|
|
static int ktls_int_check_supported_cipher(OSSL_RECORD_LAYER *rl,
|
|
|
|
const EVP_CIPHER *c,
|
|
|
|
const EVP_MD *md,
|
|
|
|
size_t taglen)
|
|
|
|
{
|
|
|
|
switch (rl->version) {
|
|
|
|
case TLS1_VERSION:
|
|
|
|
case TLS1_1_VERSION:
|
|
|
|
case TLS1_2_VERSION:
|
2022-09-17 00:34:40 +08:00
|
|
|
#ifdef OPENSSL_KTLS_TLS13
|
2022-05-20 23:54:12 +08:00
|
|
|
case TLS1_3_VERSION:
|
2022-09-17 00:34:40 +08:00
|
|
|
#endif
|
2022-05-20 23:54:12 +08:00
|
|
|
break;
|
2022-05-13 00:00:59 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2022-05-20 23:54:12 +08:00
|
|
|
|
|
|
|
if (EVP_CIPHER_is_a(c, "AES-128-GCM")
|
2022-07-27 21:20:23 +08:00
|
|
|
|| EVP_CIPHER_is_a(c, "AES-256-GCM")
|
2022-05-20 23:54:12 +08:00
|
|
|
# ifdef OPENSSL_KTLS_CHACHA20_POLY1305
|
2022-07-27 21:20:23 +08:00
|
|
|
|| EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")
|
2022-05-20 23:54:12 +08:00
|
|
|
# endif
|
|
|
|
)
|
2022-07-27 21:20:23 +08:00
|
|
|
return 1;
|
2022-05-20 23:54:12 +08:00
|
|
|
|
|
|
|
if (!EVP_CIPHER_is_a(c, "AES-128-CBC")
|
|
|
|
&& !EVP_CIPHER_is_a(c, "AES-256-CBC"))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (rl->use_etm)
|
|
|
|
return 0;
|
|
|
|
|
2022-07-27 21:20:23 +08:00
|
|
|
if (md == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (EVP_MD_is_a(md, "SHA1")
|
2022-05-20 23:54:12 +08:00
|
|
|
|| EVP_MD_is_a(md, "SHA2-256")
|
|
|
|
|| EVP_MD_is_a(md, "SHA2-384"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
2022-05-13 00:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function to configure kernel TLS structure */
|
2022-09-17 00:34:40 +08:00
|
|
|
static
|
2022-05-26 00:30:33 +08:00
|
|
|
int ktls_configure_crypto(OSSL_LIB_CTX *libctx, int version, const EVP_CIPHER *c,
|
|
|
|
EVP_MD *md, void *rl_sequence,
|
|
|
|
ktls_crypto_info_t *crypto_info, int is_tx,
|
|
|
|
unsigned char *iv, size_t ivlen,
|
2022-05-13 00:00:59 +08:00
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *mac_key, size_t mac_secret_size)
|
|
|
|
{
|
|
|
|
memset(crypto_info, 0, sizeof(*crypto_info));
|
2022-05-26 00:30:33 +08:00
|
|
|
if (EVP_CIPHER_is_a(c, "AES-128-GCM")
|
|
|
|
|| EVP_CIPHER_is_a(c, "AES-256-GCM")) {
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
|
|
|
|
crypto_info->iv_len = ivlen;
|
2022-05-26 00:30:33 +08:00
|
|
|
} else
|
2022-05-13 00:00:59 +08:00
|
|
|
# ifdef OPENSSL_KTLS_CHACHA20_POLY1305
|
2022-05-26 00:30:33 +08:00
|
|
|
if (EVP_CIPHER_is_a(c, "CHACHA20-POLY1305")) {
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->cipher_algorithm = CRYPTO_CHACHA20_POLY1305;
|
|
|
|
crypto_info->iv_len = ivlen;
|
2022-05-26 00:30:33 +08:00
|
|
|
} else
|
2022-05-13 00:00:59 +08:00
|
|
|
# endif
|
2022-05-26 00:30:33 +08:00
|
|
|
if (EVP_CIPHER_is_a(c, "AES-128-CBC") || EVP_CIPHER_is_a(c, "AES-256-CBC")) {
|
2022-07-27 21:20:23 +08:00
|
|
|
if (md == NULL)
|
|
|
|
return 0;
|
2022-05-26 00:30:33 +08:00
|
|
|
if (EVP_MD_is_a(md, "SHA1"))
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
|
2022-05-26 00:30:33 +08:00
|
|
|
else if (EVP_MD_is_a(md, "SHA2-256")) {
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
|
2022-05-26 00:30:33 +08:00
|
|
|
else if (EVP_MD_is_a(md, "SHA2-384"))
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
|
2022-05-26 00:30:33 +08:00
|
|
|
else
|
2022-05-13 00:00:59 +08:00
|
|
|
return 0;
|
|
|
|
crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
|
|
|
|
crypto_info->iv_len = ivlen;
|
|
|
|
crypto_info->auth_key = mac_key;
|
|
|
|
crypto_info->auth_key_len = mac_secret_size;
|
2022-05-26 00:30:33 +08:00
|
|
|
} else {
|
2022-05-13 00:00:59 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
crypto_info->cipher_key = key;
|
|
|
|
crypto_info->cipher_key_len = keylen;
|
|
|
|
crypto_info->iv = iv;
|
2022-05-26 00:30:33 +08:00
|
|
|
crypto_info->tls_vmajor = (version >> 8) & 0x000000ff;
|
|
|
|
crypto_info->tls_vminor = (version & 0x000000ff);
|
2022-05-13 00:00:59 +08:00
|
|
|
# ifdef TCP_RXTLS_ENABLE
|
|
|
|
memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
|
|
|
|
# else
|
|
|
|
if (!is_tx)
|
|
|
|
return 0;
|
|
|
|
# endif
|
|
|
|
return 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* __FreeBSD__ */
|
|
|
|
|
|
|
|
#if defined(OPENSSL_SYS_LINUX)
|
2022-05-20 23:54:12 +08:00
|
|
|
/* Function to check supported ciphers in Linux */
|
|
|
|
static int ktls_int_check_supported_cipher(OSSL_RECORD_LAYER *rl,
|
|
|
|
const EVP_CIPHER *c,
|
|
|
|
const EVP_MD *md,
|
|
|
|
size_t taglen)
|
|
|
|
{
|
|
|
|
switch (rl->version) {
|
|
|
|
case TLS1_2_VERSION:
|
2022-09-17 00:34:40 +08:00
|
|
|
#ifdef OPENSSL_KTLS_TLS13
|
2022-05-20 23:54:12 +08:00
|
|
|
case TLS1_3_VERSION:
|
2022-09-17 00:34:40 +08:00
|
|
|
#endif
|
2022-05-20 23:54:12 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-27 21:20:23 +08:00
|
|
|
/*
|
|
|
|
* Check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128
|
2022-05-20 23:54:12 +08:00
|
|
|
* or Chacha20-Poly1305
|
|
|
|
*/
|
|
|
|
# ifdef OPENSSL_KTLS_AES_CCM_128
|
|
|
|
if (EVP_CIPHER_is_a(c, "AES-128-CCM")) {
|
2021-12-06 17:50:50 +08:00
|
|
|
if (taglen != EVP_CCM_TLS_TAG_LEN)
|
2022-05-20 23:54:12 +08:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
# endif
|
|
|
|
if (0
|
|
|
|
# ifdef OPENSSL_KTLS_AES_GCM_128
|
|
|
|
|| EVP_CIPHER_is_a(c, "AES-128-GCM")
|
|
|
|
# endif
|
|
|
|
# ifdef OPENSSL_KTLS_AES_GCM_256
|
|
|
|
|| EVP_CIPHER_is_a(c, "AES-256-GCM")
|
|
|
|
# endif
|
|
|
|
# ifdef OPENSSL_KTLS_CHACHA20_POLY1305
|
|
|
|
|| EVP_CIPHER_is_a(c, "ChaCha20-Poly1305")
|
|
|
|
# endif
|
|
|
|
) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-13 00:00:59 +08:00
|
|
|
/* Function to configure kernel TLS structure */
|
2022-09-17 00:34:40 +08:00
|
|
|
static
|
2022-05-26 00:30:33 +08:00
|
|
|
int ktls_configure_crypto(OSSL_LIB_CTX *libctx, int version, const EVP_CIPHER *c,
|
|
|
|
const EVP_MD *md, void *rl_sequence,
|
|
|
|
ktls_crypto_info_t *crypto_info, int is_tx,
|
|
|
|
unsigned char *iv, size_t ivlen,
|
2022-05-13 00:00:59 +08:00
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *mac_key, size_t mac_secret_size)
|
|
|
|
{
|
|
|
|
unsigned char geniv[EVP_GCM_TLS_EXPLICIT_IV_LEN];
|
|
|
|
unsigned char *eiv = NULL;
|
|
|
|
|
|
|
|
# ifdef OPENSSL_NO_KTLS_RX
|
|
|
|
if (!is_tx)
|
|
|
|
return 0;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
if (EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE
|
|
|
|
|| EVP_CIPHER_get_mode(c) == EVP_CIPH_CCM_MODE) {
|
|
|
|
if (!ossl_assert(EVP_GCM_TLS_FIXED_IV_LEN == EVP_CCM_TLS_FIXED_IV_LEN)
|
|
|
|
|| !ossl_assert(EVP_GCM_TLS_EXPLICIT_IV_LEN
|
|
|
|
== EVP_CCM_TLS_EXPLICIT_IV_LEN))
|
|
|
|
return 0;
|
2022-05-26 00:30:33 +08:00
|
|
|
if (version == TLS1_2_VERSION) {
|
2022-05-13 00:00:59 +08:00
|
|
|
if (!ossl_assert(ivlen == EVP_GCM_TLS_FIXED_IV_LEN))
|
|
|
|
return 0;
|
|
|
|
if (is_tx) {
|
2022-05-26 00:30:33 +08:00
|
|
|
if (RAND_bytes_ex(libctx, geniv,
|
2022-05-13 00:00:59 +08:00
|
|
|
EVP_GCM_TLS_EXPLICIT_IV_LEN, 0) <= 0)
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
memset(geniv, 0, EVP_GCM_TLS_EXPLICIT_IV_LEN);
|
|
|
|
}
|
|
|
|
eiv = geniv;
|
|
|
|
} else {
|
|
|
|
if (!ossl_assert(ivlen == EVP_GCM_TLS_FIXED_IV_LEN
|
|
|
|
+ EVP_GCM_TLS_EXPLICIT_IV_LEN))
|
|
|
|
return 0;
|
|
|
|
eiv = iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(crypto_info, 0, sizeof(*crypto_info));
|
2022-07-27 21:20:23 +08:00
|
|
|
switch (EVP_CIPHER_get_nid(c)) {
|
2022-05-13 00:00:59 +08:00
|
|
|
# ifdef OPENSSL_KTLS_AES_GCM_128
|
|
|
|
case NID_aes_128_gcm:
|
2022-07-27 21:20:23 +08:00
|
|
|
if (!ossl_assert(TLS_CIPHER_AES_GCM_128_SALT_SIZE
|
|
|
|
== EVP_GCM_TLS_FIXED_IV_LEN)
|
|
|
|
|| !ossl_assert(TLS_CIPHER_AES_GCM_128_IV_SIZE
|
|
|
|
== EVP_GCM_TLS_EXPLICIT_IV_LEN))
|
2022-05-13 00:00:59 +08:00
|
|
|
return 0;
|
|
|
|
crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
|
2022-05-26 00:30:33 +08:00
|
|
|
crypto_info->gcm128.info.version = version;
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
|
|
|
|
memcpy(crypto_info->gcm128.iv, eiv, TLS_CIPHER_AES_GCM_128_IV_SIZE);
|
|
|
|
memcpy(crypto_info->gcm128.salt, iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
|
|
|
|
memcpy(crypto_info->gcm128.key, key, keylen);
|
|
|
|
memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
|
|
|
|
TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
|
|
|
|
return 1;
|
|
|
|
# endif
|
|
|
|
# ifdef OPENSSL_KTLS_AES_GCM_256
|
|
|
|
case NID_aes_256_gcm:
|
2022-07-27 21:20:23 +08:00
|
|
|
if (!ossl_assert(TLS_CIPHER_AES_GCM_256_SALT_SIZE
|
|
|
|
== EVP_GCM_TLS_FIXED_IV_LEN)
|
|
|
|
|| !ossl_assert(TLS_CIPHER_AES_GCM_256_IV_SIZE
|
|
|
|
== EVP_GCM_TLS_EXPLICIT_IV_LEN))
|
2022-05-13 00:00:59 +08:00
|
|
|
return 0;
|
|
|
|
crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
|
2022-05-26 00:30:33 +08:00
|
|
|
crypto_info->gcm256.info.version = version;
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
|
|
|
|
memcpy(crypto_info->gcm256.iv, eiv, TLS_CIPHER_AES_GCM_256_IV_SIZE);
|
|
|
|
memcpy(crypto_info->gcm256.salt, iv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
|
|
|
|
memcpy(crypto_info->gcm256.key, key, keylen);
|
|
|
|
memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
|
|
|
|
TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
# endif
|
|
|
|
# ifdef OPENSSL_KTLS_AES_CCM_128
|
|
|
|
case NID_aes_128_ccm:
|
2022-07-27 21:20:23 +08:00
|
|
|
if (!ossl_assert(TLS_CIPHER_AES_CCM_128_SALT_SIZE
|
|
|
|
== EVP_CCM_TLS_FIXED_IV_LEN)
|
|
|
|
|| !ossl_assert(TLS_CIPHER_AES_CCM_128_IV_SIZE
|
|
|
|
== EVP_CCM_TLS_EXPLICIT_IV_LEN))
|
2022-05-13 00:00:59 +08:00
|
|
|
return 0;
|
|
|
|
crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
|
2022-05-26 00:30:33 +08:00
|
|
|
crypto_info->ccm128.info.version = version;
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
|
|
|
|
memcpy(crypto_info->ccm128.iv, eiv, TLS_CIPHER_AES_CCM_128_IV_SIZE);
|
|
|
|
memcpy(crypto_info->ccm128.salt, iv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
|
|
|
|
memcpy(crypto_info->ccm128.key, key, keylen);
|
|
|
|
memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
|
|
|
|
TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
|
|
|
|
return 1;
|
|
|
|
# endif
|
|
|
|
# ifdef OPENSSL_KTLS_CHACHA20_POLY1305
|
|
|
|
case NID_chacha20_poly1305:
|
|
|
|
if (!ossl_assert(ivlen == TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE))
|
|
|
|
return 0;
|
2022-07-27 21:20:23 +08:00
|
|
|
crypto_info->chacha20poly1305.info.cipher_type
|
|
|
|
= TLS_CIPHER_CHACHA20_POLY1305;
|
2022-05-26 00:30:33 +08:00
|
|
|
crypto_info->chacha20poly1305.info.version = version;
|
2022-05-13 00:00:59 +08:00
|
|
|
crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
|
|
|
|
memcpy(crypto_info->chacha20poly1305.iv, iv, ivlen);
|
|
|
|
memcpy(crypto_info->chacha20poly1305.key, key, keylen);
|
|
|
|
memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
|
|
|
|
TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
|
|
|
|
return 1;
|
|
|
|
# endif
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* OPENSSL_SYS_LINUX */
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
static int ktls_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
|
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *iv, size_t ivlen,
|
|
|
|
unsigned char *mackey, size_t mackeylen,
|
|
|
|
const EVP_CIPHER *ciph,
|
|
|
|
size_t taglen,
|
|
|
|
int mactype,
|
|
|
|
const EVP_MD *md,
|
2022-09-15 23:03:02 +08:00
|
|
|
COMP_METHOD *comp)
|
2022-05-11 01:50:00 +08:00
|
|
|
{
|
|
|
|
ktls_crypto_info_t crypto_info;
|
|
|
|
|
2022-05-12 23:35:52 +08:00
|
|
|
/*
|
|
|
|
* Check if we are suitable for KTLS. If not suitable we return
|
|
|
|
* OSSL_RECORD_RETURN_NON_FATAL_ERR so that other record layers can be tried
|
|
|
|
* instead
|
|
|
|
*/
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
if (comp != NULL)
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
/* ktls supports only the maximum fragment size */
|
2022-09-23 23:53:23 +08:00
|
|
|
if (rl->max_frag_len != SSL3_RT_MAX_PLAIN_LENGTH)
|
2022-05-23 18:31:53 +08:00
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
/* check that cipher is supported */
|
2022-05-20 23:54:12 +08:00
|
|
|
if (!ktls_int_check_supported_cipher(rl, ciph, md, taglen))
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
/* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
|
|
|
|
if (rl->direction == OSSL_RECORD_DIRECTION_WRITE) {
|
2022-08-31 23:39:36 +08:00
|
|
|
if (BIO_flush(rl->bio) <= 0)
|
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
|
|
|
|
|
|
|
/* KTLS does not support record padding */
|
|
|
|
if (rl->padding != NULL || rl->block_padding > 0)
|
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 00:30:33 +08:00
|
|
|
if (!ktls_configure_crypto(rl->libctx, rl->version, ciph, md, rl->sequence,
|
|
|
|
&crypto_info,
|
2022-05-11 01:50:00 +08:00
|
|
|
rl->direction == OSSL_RECORD_DIRECTION_WRITE,
|
|
|
|
iv, ivlen, key, keylen, mackey, mackeylen))
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
|
|
|
|
if (!BIO_set_ktls(rl->bio, &crypto_info, rl->direction))
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_NON_FATAL_ERR;
|
2022-05-11 01:50:00 +08:00
|
|
|
|
2022-11-09 17:26:11 +08:00
|
|
|
if (rl->direction == OSSL_RECORD_DIRECTION_WRITE &&
|
|
|
|
(rl->options & SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE) != 0)
|
|
|
|
/* Ignore errors. The application opts in to using the zerocopy
|
|
|
|
* optimization. If the running kernel doesn't support it, just
|
|
|
|
* continue without the optimization.
|
|
|
|
*/
|
|
|
|
BIO_set_ktls_tx_zerocopy_sendfile(rl->bio);
|
|
|
|
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_SUCCESS;
|
2022-05-11 01:50:00 +08:00
|
|
|
}
|
|
|
|
|
2022-05-13 00:21:25 +08:00
|
|
|
static int ktls_read_n(OSSL_RECORD_LAYER *rl, size_t n, size_t max, int extend,
|
|
|
|
int clearold, size_t *readbytes)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = tls_default_read_n(rl, n, max, extend, clearold, readbytes);
|
|
|
|
|
|
|
|
if (ret < OSSL_RECORD_RETURN_RETRY) {
|
|
|
|
switch (errno) {
|
|
|
|
case EBADMSG:
|
|
|
|
RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC,
|
|
|
|
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
|
|
|
|
break;
|
|
|
|
case EMSGSIZE:
|
|
|
|
RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
|
|
|
|
SSL_R_PACKET_LENGTH_TOO_LONG);
|
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
RLAYERfatal(rl, SSL_AD_PROTOCOL_VERSION,
|
|
|
|
SSL_R_WRONG_VERSION_NUMBER);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int ktls_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *inrecs,
|
|
|
|
size_t n_recs, int sending, SSL_MAC_BUF *mac,
|
|
|
|
size_t macsize)
|
2022-05-11 01:50:00 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int ktls_validate_record_header(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
|
2022-05-13 00:21:25 +08:00
|
|
|
{
|
|
|
|
if (rec->rec_version != TLS1_2_VERSION) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_WRONG_VERSION_NUMBER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int ktls_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
|
2022-05-13 00:21:25 +08:00
|
|
|
{
|
|
|
|
if (rl->version == TLS1_3_VERSION)
|
2022-05-26 00:30:33 +08:00
|
|
|
return tls13_common_post_process_record(rl, rec);
|
2022-05-13 00:21:25 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ktls_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
|
2022-07-27 21:37:27 +08:00
|
|
|
int role, int direction, int level, uint16_t epoch,
|
2022-11-14 22:19:53 +08:00
|
|
|
unsigned char *secret, size_t secretlen,
|
2022-06-08 21:52:44 +08:00
|
|
|
unsigned char *key, size_t keylen, unsigned char *iv,
|
|
|
|
size_t ivlen, unsigned char *mackey, size_t mackeylen,
|
2022-05-13 00:21:25 +08:00
|
|
|
const EVP_CIPHER *ciph, size_t taglen,
|
|
|
|
int mactype,
|
2022-11-14 22:19:53 +08:00
|
|
|
const EVP_MD *md, COMP_METHOD *comp,
|
|
|
|
const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
|
|
|
|
BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
|
2022-05-13 00:21:25 +08:00
|
|
|
const OSSL_PARAM *settings, const OSSL_PARAM *options,
|
2022-11-11 00:05:16 +08:00
|
|
|
const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
|
2022-05-26 00:30:33 +08:00
|
|
|
OSSL_RECORD_LAYER **retrl)
|
2022-05-13 00:21:25 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = tls_int_new_record_layer(libctx, propq, vers, role, direction, level,
|
|
|
|
key, keylen, iv, ivlen, mackey, mackeylen,
|
2022-05-17 23:16:40 +08:00
|
|
|
ciph, taglen, mactype, md, comp, prev,
|
|
|
|
transport, next, local, peer, settings,
|
2022-05-26 00:30:33 +08:00
|
|
|
options, fns, cbarg, retrl);
|
2022-05-13 00:21:25 +08:00
|
|
|
|
|
|
|
if (ret != OSSL_RECORD_RETURN_SUCCESS)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
(*retrl)->funcs = &ossl_ktls_funcs;
|
|
|
|
|
|
|
|
ret = (*retrl)->funcs->set_crypto_state(*retrl, level, key, keylen, iv,
|
|
|
|
ivlen, mackey, mackeylen, ciph,
|
2022-05-26 00:30:33 +08:00
|
|
|
taglen, mactype, md, comp);
|
2022-05-13 00:21:25 +08:00
|
|
|
|
|
|
|
if (ret != OSSL_RECORD_RETURN_SUCCESS) {
|
|
|
|
OPENSSL_free(*retrl);
|
|
|
|
*retrl = NULL;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* With KTLS we always try and read as much as possible and fill the
|
|
|
|
* buffer
|
|
|
|
*/
|
|
|
|
(*retrl)->read_ahead = 1;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:35:30 +08:00
|
|
|
static int ktls_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
|
|
|
|
OSSL_RECORD_TEMPLATE *templates,
|
|
|
|
size_t numtempl, size_t *prefix)
|
|
|
|
{
|
|
|
|
if (!ossl_assert(numtempl == 1))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We just use the end application buffer in the case of KTLS, so nothing
|
|
|
|
* to do. We pretend we set up one buffer.
|
|
|
|
*/
|
|
|
|
rl->numwpipes = 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ktls_initialise_write_packets(OSSL_RECORD_LAYER *rl,
|
|
|
|
OSSL_RECORD_TEMPLATE *templates,
|
|
|
|
size_t numtempl,
|
|
|
|
OSSL_RECORD_TEMPLATE *prefixtempl,
|
|
|
|
WPACKET *pkt,
|
2022-11-02 23:19:51 +08:00
|
|
|
TLS_BUFFER *bufs,
|
2022-09-26 23:35:30 +08:00
|
|
|
size_t *wpinited)
|
|
|
|
{
|
2022-11-02 23:19:51 +08:00
|
|
|
TLS_BUFFER *wb;
|
2022-09-26 23:35:30 +08:00
|
|
|
|
|
|
|
/*
|
2022-09-27 00:44:11 +08:00
|
|
|
* We just use the application buffer directly and don't use any WPACKET
|
2022-09-26 23:35:30 +08:00
|
|
|
* structures
|
|
|
|
*/
|
|
|
|
wb = &bufs[0];
|
|
|
|
wb->type = templates[0].type;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ktls doesn't modify the buffer, but to avoid a warning we need
|
|
|
|
* to discard the const qualifier.
|
2022-09-27 00:44:11 +08:00
|
|
|
* This doesn't leak memory because the buffers have never been allocated
|
|
|
|
* with KTLS
|
2022-09-26 23:35:30 +08:00
|
|
|
*/
|
2022-11-02 23:19:51 +08:00
|
|
|
TLS_BUFFER_set_buf(wb, (unsigned char *)templates[0].buf);
|
|
|
|
TLS_BUFFER_set_offset(wb, 0);
|
|
|
|
TLS_BUFFER_set_app_buffer(wb, 1);
|
2022-09-26 23:35:30 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-27 00:44:11 +08:00
|
|
|
static int ktls_prepare_record_header(OSSL_RECORD_LAYER *rl,
|
|
|
|
WPACKET *thispkt,
|
|
|
|
OSSL_RECORD_TEMPLATE *templ,
|
|
|
|
unsigned int rectype,
|
|
|
|
unsigned char **recdata)
|
|
|
|
{
|
|
|
|
/* The kernel writes the record header, so nothing to do */
|
|
|
|
*recdata = NULL;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-09-27 23:43:23 +08:00
|
|
|
static int ktls_prepare_for_encryption(OSSL_RECORD_LAYER *rl,
|
|
|
|
size_t mac_size,
|
|
|
|
WPACKET *thispkt,
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD *thiswr)
|
2022-09-27 23:43:23 +08:00
|
|
|
{
|
|
|
|
/* No encryption, so nothing to do */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-04 22:59:06 +08:00
|
|
|
static int ktls_post_encryption_processing(OSSL_RECORD_LAYER *rl,
|
|
|
|
size_t mac_size,
|
|
|
|
OSSL_RECORD_TEMPLATE *templ,
|
|
|
|
WPACKET *thispkt,
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD *thiswr)
|
2022-10-04 22:59:06 +08:00
|
|
|
{
|
|
|
|
/* The kernel does anything that is needed, so nothing to do here */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-10-04 23:32:02 +08:00
|
|
|
static int ktls_prepare_write_bio(OSSL_RECORD_LAYER *rl, int type)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* To prevent coalescing of control and data messages,
|
|
|
|
* such as in buffer_write, we flush the BIO
|
|
|
|
*/
|
|
|
|
if (type != SSL3_RT_APPLICATION_DATA) {
|
|
|
|
int ret, i = BIO_flush(rl->bio);
|
|
|
|
|
|
|
|
if (i <= 0) {
|
|
|
|
if (BIO_should_retry(rl->bio))
|
|
|
|
ret = OSSL_RECORD_RETURN_RETRY;
|
|
|
|
else
|
|
|
|
ret = OSSL_RECORD_RETURN_FATAL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
BIO_set_ktls_ctrl_msg(rl->bio, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OSSL_RECORD_RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:12:31 +08:00
|
|
|
static int ktls_alloc_buffers(OSSL_RECORD_LAYER *rl)
|
|
|
|
{
|
|
|
|
/* We use the application buffer directly for writing */
|
|
|
|
if (rl->direction == OSSL_RECORD_DIRECTION_WRITE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return tls_alloc_buffers(rl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ktls_free_buffers(OSSL_RECORD_LAYER *rl)
|
|
|
|
{
|
|
|
|
/* We use the application buffer directly for writing */
|
|
|
|
if (rl->direction == OSSL_RECORD_DIRECTION_WRITE)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return tls_free_buffers(rl);
|
|
|
|
}
|
|
|
|
|
2022-09-26 23:35:30 +08:00
|
|
|
static struct record_functions_st ossl_ktls_funcs = {
|
|
|
|
ktls_set_crypto_state,
|
|
|
|
ktls_cipher,
|
|
|
|
NULL,
|
|
|
|
tls_default_set_protocol_version,
|
|
|
|
ktls_read_n,
|
|
|
|
tls_get_more_records,
|
|
|
|
ktls_validate_record_header,
|
|
|
|
ktls_post_process_record,
|
|
|
|
tls_get_max_records_default,
|
|
|
|
tls_write_records_default,
|
|
|
|
ktls_allocate_write_buffers,
|
2022-09-27 00:07:02 +08:00
|
|
|
ktls_initialise_write_packets,
|
2022-09-27 00:44:11 +08:00
|
|
|
NULL,
|
2022-09-27 22:32:22 +08:00
|
|
|
ktls_prepare_record_header,
|
2022-09-27 23:43:23 +08:00
|
|
|
NULL,
|
2022-10-04 22:59:06 +08:00
|
|
|
ktls_prepare_for_encryption,
|
2022-10-04 23:32:02 +08:00
|
|
|
ktls_post_encryption_processing,
|
|
|
|
ktls_prepare_write_bio
|
2022-09-26 23:35:30 +08:00
|
|
|
};
|
|
|
|
|
2022-05-13 00:21:25 +08:00
|
|
|
const OSSL_RECORD_METHOD ossl_ktls_record_method = {
|
|
|
|
ktls_new_record_layer,
|
|
|
|
tls_free,
|
|
|
|
tls_unprocessed_read_pending,
|
|
|
|
tls_processed_read_pending,
|
|
|
|
tls_app_data_pending,
|
|
|
|
tls_get_max_records,
|
|
|
|
tls_write_records,
|
2022-08-19 23:54:09 +08:00
|
|
|
tls_retry_write_records,
|
2022-05-13 00:21:25 +08:00
|
|
|
tls_read_record,
|
|
|
|
tls_release_record,
|
|
|
|
tls_get_alert_code,
|
|
|
|
tls_set1_bio,
|
|
|
|
tls_set_protocol_version,
|
|
|
|
tls_set_plain_alerts,
|
|
|
|
tls_set_first_handshake,
|
2022-05-26 00:30:33 +08:00
|
|
|
tls_set_max_pipelines,
|
2022-07-22 22:38:26 +08:00
|
|
|
NULL,
|
2022-07-26 21:34:38 +08:00
|
|
|
tls_get_state,
|
2022-09-15 23:03:02 +08:00
|
|
|
tls_set_options,
|
2022-09-23 23:53:23 +08:00
|
|
|
tls_get_compression,
|
2022-10-14 22:30:55 +08:00
|
|
|
tls_set_max_frag_len,
|
2022-10-17 23:33:40 +08:00
|
|
|
NULL,
|
2022-10-21 23:12:31 +08:00
|
|
|
tls_increment_sequence_ctr,
|
|
|
|
ktls_alloc_buffers,
|
|
|
|
ktls_free_buffers
|
2022-05-11 01:50:00 +08:00
|
|
|
};
|