Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -12097,6 +12097,11 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
uint8_t *iv =
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;

if (msgsz > 16384) {
MG_ERROR(("msg longer than recordsz"));
return false;
}

#if MG_ENABLE_CHACHA20
#else
mg_gcm_initialize();
Expand All @@ -12118,9 +12123,8 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
#if MG_ENABLE_CHACHA20
(void) tag; // tag is only used in aes gcm
{
size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE;
size_t n;
uint8_t *enc = (uint8_t *) mg_calloc(1, maxlen + 256 + 1);
uint8_t *enc = (uint8_t *) mg_calloc(1, msgsz + 256 + 1);
if (enc == NULL) return false;
n = mg_chacha20_poly1305_encrypt(enc, key, nonce, associated_data,
sizeof(associated_data), outmsg,
Expand Down Expand Up @@ -12379,9 +12383,7 @@ static const uint8_t secp256r1_sig_algs[12] = {

static bool mg_tls_server_send_cert_request(struct mg_connection *c) {
struct tls_data *tls = (struct tls_data *) c->tls;
uint8_t *req = (uint8_t *) mg_calloc(1, 13 + sizeof(secp256r1_sig_algs));
bool res;
if (req == NULL) return false;
uint8_t req[13 + sizeof(secp256r1_sig_algs)];
req[0] = MG_TLS_CERTIFICATE_REQUEST; // handshake header
MG_STORE_BE24(req + 1, 9 + sizeof(secp256r1_sig_algs));
req[4] = 0; // context length
Expand All @@ -12392,10 +12394,8 @@ static bool mg_tls_server_send_cert_request(struct mg_connection *c) {
req + 11,
sizeof(secp256r1_sig_algs)); // signature hash algorithms length
memcpy(req + 13, (uint8_t *) secp256r1_sig_algs, sizeof(secp256r1_sig_algs));
mg_sha256_update(&tls->sha256, req, 13 + sizeof(secp256r1_sig_algs));
res = mg_tls_encrypt(c, req, 13 + sizeof(secp256r1_sig_algs), MG_TLS_HANDSHAKE);
mg_free(req);
return res;
mg_sha256_update(&tls->sha256, req, sizeof(req));
return mg_tls_encrypt(c, req, sizeof(req), MG_TLS_HANDSHAKE);
}

static bool mg_tls_send_cert(struct mg_connection *c, bool is_client) {
Expand Down Expand Up @@ -13312,6 +13312,7 @@ void mg_tls_handshake(struct mg_connection *c) {
struct tls_data *tls = (struct tls_data *) c->tls;
long n;
bool res;
if (c->is_closing) return; // we don't clear rx buf, so ignore what's left
if (c->is_client) {
// will clear is_hs when sending last chunk
res = mg_tls_client_handshake(c);
Expand Down
19 changes: 10 additions & 9 deletions src/tls_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
uint8_t *iv =
c->is_client ? tls->enc.client_write_iv : tls->enc.server_write_iv;

if (msgsz > 16384) {
MG_ERROR(("msg longer than recordsz"));
return false;
}

#if MG_ENABLE_CHACHA20
#else
mg_gcm_initialize();
Expand All @@ -440,9 +445,8 @@ static bool mg_tls_encrypt(struct mg_connection *c, const uint8_t *msg,
#if MG_ENABLE_CHACHA20
(void) tag; // tag is only used in aes gcm
{
size_t maxlen = MG_IO_SIZE > 16384 ? 16384 : MG_IO_SIZE;
size_t n;
uint8_t *enc = (uint8_t *) mg_calloc(1, maxlen + 256 + 1);
uint8_t *enc = (uint8_t *) mg_calloc(1, msgsz + 256 + 1);
if (enc == NULL) return false;
n = mg_chacha20_poly1305_encrypt(enc, key, nonce, associated_data,
sizeof(associated_data), outmsg,
Expand Down Expand Up @@ -701,9 +705,7 @@ static const uint8_t secp256r1_sig_algs[12] = {

static bool mg_tls_server_send_cert_request(struct mg_connection *c) {
struct tls_data *tls = (struct tls_data *) c->tls;
uint8_t *req = (uint8_t *) mg_calloc(1, 13 + sizeof(secp256r1_sig_algs));
bool res;
if (req == NULL) return false;
uint8_t req[13 + sizeof(secp256r1_sig_algs)];
req[0] = MG_TLS_CERTIFICATE_REQUEST; // handshake header
MG_STORE_BE24(req + 1, 9 + sizeof(secp256r1_sig_algs));
req[4] = 0; // context length
Expand All @@ -714,10 +716,8 @@ static bool mg_tls_server_send_cert_request(struct mg_connection *c) {
req + 11,
sizeof(secp256r1_sig_algs)); // signature hash algorithms length
memcpy(req + 13, (uint8_t *) secp256r1_sig_algs, sizeof(secp256r1_sig_algs));
mg_sha256_update(&tls->sha256, req, 13 + sizeof(secp256r1_sig_algs));
res = mg_tls_encrypt(c, req, 13 + sizeof(secp256r1_sig_algs), MG_TLS_HANDSHAKE);
mg_free(req);
return res;
mg_sha256_update(&tls->sha256, req, sizeof(req));
return mg_tls_encrypt(c, req, sizeof(req), MG_TLS_HANDSHAKE);
}

static bool mg_tls_send_cert(struct mg_connection *c, bool is_client) {
Expand Down Expand Up @@ -1634,6 +1634,7 @@ void mg_tls_handshake(struct mg_connection *c) {
struct tls_data *tls = (struct tls_data *) c->tls;
long n;
bool res;
if (c->is_closing) return; // we don't clear rx buf, so ignore what's left
if (c->is_client) {
// will clear is_hs when sending last chunk
res = mg_tls_client_handshake(c);
Expand Down