Latest update - 9132
This commit is contained in:
@@ -15,13 +15,6 @@
|
||||
|
||||
#define NGX_QUIC_AES_128_KEY_LEN 16
|
||||
|
||||
#ifndef TLS1_3_CK_AES_128_GCM_SHA256
|
||||
#define TLS1_3_CK_AES_128_GCM_SHA256 0x03001301
|
||||
#define TLS1_3_CK_AES_256_GCM_SHA384 0x03001302
|
||||
#define TLS1_3_CK_CHACHA20_POLY1305_SHA256 \
|
||||
0x03001303
|
||||
#endif
|
||||
|
||||
|
||||
static ngx_int_t ngx_hkdf_expand(u_char *out_key, size_t out_len,
|
||||
const EVP_MD *digest, const u_char *prk, size_t prk_len,
|
||||
@@ -94,6 +87,15 @@ ngx_quic_ciphers(ngx_uint_t id, ngx_quic_ciphers_t *ciphers,
|
||||
len = 32;
|
||||
break;
|
||||
|
||||
#ifndef OPENSSL_IS_BORINGSSL
|
||||
case TLS1_3_CK_AES_128_CCM_SHA256:
|
||||
ciphers->c = EVP_aes_128_ccm();
|
||||
ciphers->hp = EVP_aes_128_ctr();
|
||||
ciphers->d = EVP_sha256();
|
||||
len = 16;
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
return NGX_ERROR;
|
||||
}
|
||||
@@ -343,8 +345,7 @@ failed:
|
||||
|
||||
static ngx_int_t
|
||||
ngx_quic_tls_open(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
ngx_str_t *out, u_char *nonce, ngx_str_t *in, ngx_str_t *ad,
|
||||
ngx_log_t *log)
|
||||
ngx_str_t *out, u_char *nonce, ngx_str_t *in, ngx_str_t *ad, ngx_log_t *log)
|
||||
{
|
||||
|
||||
#ifdef OPENSSL_IS_BORINGSSL
|
||||
@@ -369,7 +370,6 @@ ngx_quic_tls_open(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
EVP_AEAD_CTX_free(ctx);
|
||||
#else
|
||||
int len;
|
||||
u_char *tag;
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
|
||||
ctx = EVP_CIPHER_CTX_new();
|
||||
@@ -384,12 +384,24 @@ ngx_quic_tls_open(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, s->iv.len, NULL)
|
||||
in->len -= NGX_QUIC_TAG_LEN;
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, NGX_QUIC_TAG_LEN,
|
||||
in->data + in->len)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_IVLEN) failed");
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_TAG) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, s->iv.len, NULL)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_IVLEN) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
@@ -399,34 +411,29 @@ ngx_quic_tls_open(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE
|
||||
&& EVP_DecryptUpdate(ctx, NULL, &len, NULL, in->len) != 1)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_DecryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_DecryptUpdate(ctx, out->data, &len, in->data,
|
||||
in->len - EVP_GCM_TLS_TAG_LEN)
|
||||
!= 1)
|
||||
{
|
||||
if (EVP_DecryptUpdate(ctx, out->data, &len, in->data, in->len) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptUpdate() failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
out->len = len;
|
||||
tag = in->data + in->len - EVP_GCM_TLS_TAG_LEN;
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, EVP_GCM_TLS_TAG_LEN, tag)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_TAG) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_DecryptFinal_ex(ctx, out->data + len, &len) <= 0) {
|
||||
if (EVP_DecryptFinal_ex(ctx, out->data + out->len, &len) <= 0) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_DecryptFinal_ex failed");
|
||||
return NGX_ERROR;
|
||||
@@ -482,12 +489,23 @@ ngx_quic_tls_seal(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, s->iv.len, NULL)
|
||||
if (EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE
|
||||
&& EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, NGX_QUIC_TAG_LEN,
|
||||
NULL)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_TAG) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, s->iv.len, NULL)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_SET_IVLEN) failed");
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_SET_IVLEN) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
@@ -497,6 +515,14 @@ ngx_quic_tls_seal(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE
|
||||
&& EVP_EncryptUpdate(ctx, NULL, &len, NULL, in->len) != 1)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (EVP_EncryptUpdate(ctx, NULL, &len, ad->data, ad->len) != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0, "EVP_EncryptUpdate() failed");
|
||||
@@ -519,20 +545,21 @@ ngx_quic_tls_seal(const ngx_quic_cipher_t *cipher, ngx_quic_secret_t *s,
|
||||
|
||||
out->len += len;
|
||||
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, EVP_GCM_TLS_TAG_LEN,
|
||||
out->data + in->len)
|
||||
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, NGX_QUIC_TAG_LEN,
|
||||
out->data + out->len)
|
||||
== 0)
|
||||
{
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
ngx_ssl_error(NGX_LOG_INFO, log, 0,
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_GCM_GET_TAG) failed");
|
||||
"EVP_CIPHER_CTX_ctrl(EVP_CTRL_AEAD_GET_TAG) failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
out->len += NGX_QUIC_TAG_LEN;
|
||||
|
||||
out->len += EVP_GCM_TLS_TAG_LEN;
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
#endif
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
@@ -738,7 +765,7 @@ ngx_quic_create_packet(ngx_quic_header_t *pkt, ngx_str_t *res)
|
||||
ad.data = res->data;
|
||||
ad.len = ngx_quic_create_header(pkt, ad.data, &pnp);
|
||||
|
||||
out.len = pkt->payload.len + EVP_GCM_TLS_TAG_LEN;
|
||||
out.len = pkt->payload.len + NGX_QUIC_TAG_LEN;
|
||||
out.data = res->data + ad.len;
|
||||
|
||||
#ifdef NGX_QUIC_DEBUG_CRYPTO
|
||||
@@ -802,7 +829,7 @@ ngx_quic_create_retry_packet(ngx_quic_header_t *pkt, ngx_str_t *res)
|
||||
ad.len = ngx_quic_create_retry_itag(pkt, ad.data, &start);
|
||||
|
||||
itag.data = ad.data + ad.len;
|
||||
itag.len = EVP_GCM_TLS_TAG_LEN;
|
||||
itag.len = NGX_QUIC_TAG_LEN;
|
||||
|
||||
#ifdef NGX_QUIC_DEBUG_CRYPTO
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, pkt->log, 0,
|
||||
@@ -979,7 +1006,7 @@ ngx_quic_decrypt(ngx_quic_header_t *pkt, uint64_t *largest_pn)
|
||||
* AES and ChaCha20 algorithms sample 16 bytes
|
||||
*/
|
||||
|
||||
if (len < EVP_GCM_TLS_TAG_LEN + 4) {
|
||||
if (len < NGX_QUIC_TAG_LEN + 4) {
|
||||
return NGX_DECLINED;
|
||||
}
|
||||
|
||||
@@ -1039,7 +1066,7 @@ ngx_quic_decrypt(ngx_quic_header_t *pkt, uint64_t *largest_pn)
|
||||
"quic ad len:%uz %xV", ad.len, &ad);
|
||||
#endif
|
||||
|
||||
pkt->payload.len = in.len - EVP_GCM_TLS_TAG_LEN;
|
||||
pkt->payload.len = in.len - NGX_QUIC_TAG_LEN;
|
||||
pkt->payload.data = pkt->plaintext + ad.len;
|
||||
|
||||
rc = ngx_quic_tls_open(ciphers.c, secret, &pkt->payload,
|
||||
|
||||
Reference in New Issue
Block a user