Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit 2e418bf

Browse files
committed
quic: fixups to fix compiling
1 parent 8219b0e commit 2e418bf

File tree

5 files changed

+159
-74
lines changed

5 files changed

+159
-74
lines changed

src/node_quic_crypto.cc

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,12 @@ const ngtcp2_crypto_ctx* GetCryptoContext(ngtcp2_conn* conn, SSL* ssl) {
218218
return ctx;
219219
}
220220

221-
const ngtcp2_crypto_ctx* GetInitialCryptoContext(ngtcp2_conn* conn {
221+
const ngtcp2_crypto_ctx* GetInitialCryptoContext(ngtcp2_conn* conn) {
222222
const ngtcp2_crypto_ctx* ctx = ngtcp2_conn_get_initial_crypto_ctx(conn);
223223
// ctx will always be non-null, so check members
224224
if (ctx->aead.native_handle == nullptr) {
225225
ngtcp2_crypto_ctx context;
226+
ngtcp2_crypto_ctx_initial(&context);
226227
ngtcp2_conn_set_initial_crypto_ctx(conn, &context);
227228
ctx = ngtcp2_conn_get_initial_crypto_ctx(conn);
228229
}
@@ -324,32 +325,62 @@ bool Encrypt(
324325
size_t taglen = aead_tag_length(aead);
325326
const EVP_CIPHER* cipher =
326327
static_cast<const EVP_CIPHER*>(aead->native_handle);
327-
DCHECK_NOT_NULL(cipher);
328+
CHECK_NOT_NULL(cipher);
329+
328330

329331
CipherCtxPointer actx(EVP_CIPHER_CTX_new());
330332
CHECK(actx);
331333

332-
size_t outlen = 0;
333334
int len;
334335

335-
if (EVP_EncryptInit_ex(actx.get(), cipher, nullptr, nullptr, nullptr) != 1 ||
336-
EVP_CIPHER_CTX_ctrl(actx.get(), EVP_CTRL_AEAD_SET_IVLEN,
337-
noncelen, nullptr) != 1) ||
338-
EVP_EncryptInit_ex(actx.get(), nullptr, nullptr, key, nonce) != 1 ||
339-
EVP_EncryptUpdate(actx.get(), nullptr, &len, ad, adlen) != 1 ||
340-
EVP_EncryptUpdate(actx.get(), dest, &len, plaintext, plaintextlen) != 1) {
336+
if (EVP_EncryptInit_ex(
337+
actx.get(),
338+
cipher,
339+
nullptr,
340+
nullptr,
341+
nullptr) != 1 ||
342+
EVP_CIPHER_CTX_ctrl(
343+
actx.get(),
344+
EVP_CTRL_AEAD_SET_IVLEN,
345+
noncelen,
346+
nullptr) != 1 ||
347+
EVP_EncryptInit_ex(
348+
actx.get(),
349+
nullptr,
350+
nullptr,
351+
key,
352+
nonce) != 1 ||
353+
EVP_EncryptUpdate(
354+
actx.get(),
355+
nullptr,
356+
&len,
357+
ad,
358+
adlen) != 1 ||
359+
EVP_EncryptUpdate(
360+
actx.get(),
361+
dest,
362+
&len,
363+
plaintext,
364+
plaintextlen) != 1) {
341365
return false;
342366
}
343367

344-
outlen = len;
368+
dest += len;
345369

346-
if (EVP_EncryptFinal_ex(actx.get(), dest + outlen, &len) != 1)
370+
if (EVP_EncryptFinal_ex(
371+
actx.get(),
372+
dest,
373+
&len) != 1) {
347374
return false;
375+
}
348376

349-
outlen += len;
377+
dest += len;
350378

351-
if (EVP_CIPHER_CTX_ctrl(actx.get(), EVP_CTRL_AEAD_GET_TAG, taglen,
352-
dest + outlen) != 1) {
379+
if (EVP_CIPHER_CTX_ctrl(
380+
actx.get(),
381+
EVP_CTRL_AEAD_GET_TAG,
382+
taglen,
383+
dest) != 1) {
353384
return false;
354385
}
355386

@@ -535,6 +566,11 @@ bool DerivePacketProtectionKey(
535566
static const uint8_t KEY_LABEL[] = "quic key";
536567
static const uint8_t IV_LABEL[] = "quic iv";
537568
static const uint8_t HP_KEY_LABEL[] = "quic hp";
569+
570+
CHECK_NOT_NULL(ctx);
571+
CHECK_NOT_NULL(ctx->aead.native_handle);
572+
CHECK_NOT_NULL(ctx->md.native_handle);
573+
538574
size_t keylen = aead_key_length(&ctx->aead);
539575
size_t ivlen = packet_protection_ivlen(ctx);
540576

@@ -569,9 +605,10 @@ bool DerivePacketProtectionKey(
569605
// TODO(@jasnell): Replace with ngtcp2_crypto_derive_and_install_initial_key
570606
// once we move to ngtcp2_crypto
571607
bool DeriveAndInstallInitialKey(
572-
ngtcp2_conn* conn,
573-
const ngtcp2_cid* dcid,
574-
ngtcp2_crypto_side side) {
608+
ngtcp2_conn* conn,
609+
const ngtcp2_crypto_ctx* ctx,
610+
const ngtcp2_cid* dcid,
611+
ngtcp2_crypto_side side) {
575612
InitialSecret rx_secret;
576613
InitialSecret tx_secret;
577614
InitialKey rx_key;
@@ -581,8 +618,7 @@ bool DeriveAndInstallInitialKey(
581618
InitialKey rx_hp;
582619
InitialKey tx_hp;
583620

584-
ngtcp2_crypto_ctx ctx;
585-
ngtcp2_crypto_ctx_initial(&ctx);
621+
CHECK_NOT_NULL(ctx);
586622

587623
return
588624
DeriveInitialSecrets(
@@ -595,14 +631,14 @@ bool DeriveAndInstallInitialKey(
595631
rx_key.data(),
596632
rx_iv.data(),
597633
rx_hp.data(),
598-
&ctx,
634+
ctx,
599635
rx_secret.data(),
600636
NGTCP2_CRYPTO_INITIAL_SECRETLEN) &&
601637
DerivePacketProtectionKey(
602638
tx_key.data(),
603639
tx_iv.data(),
604640
tx_hp.data(),
605-
&ctx,
641+
ctx,
606642
tx_secret.data(),
607643
NGTCP2_CRYPTO_INITIAL_SECRETLEN) &&
608644
ngtcp2_conn_install_initial_key(
@@ -1254,7 +1290,6 @@ bool GenerateRetryToken(
12541290
ngtcp2_crypto_ctx_initial(&ctx);
12551291

12561292
const size_t addrlen = SocketAddress::GetAddressLen(addr);
1257-
size_t keylen = aead_key_length(&ctx.aead);
12581293
size_t ivlen = packet_protection_ivlen(&ctx);
12591294

12601295
uint64_t now = uv_hrtime();
@@ -1312,7 +1347,6 @@ bool InvalidRetryToken(
13121347
ngtcp2_crypto_ctx ctx;
13131348
ngtcp2_crypto_ctx_initial(&ctx);
13141349

1315-
size_t keylen = aead_key_length(&ctx.aead);
13161350
size_t ivlen = packet_protection_ivlen(&ctx);
13171351
const size_t addrlen = SocketAddress::GetAddressLen(addr);
13181352

src/node_quic_crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ bool DerivePacketProtectionKey(
133133
// once we move to ngtcp2_crypto
134134
bool DeriveAndInstallInitialKey(
135135
ngtcp2_conn* conn,
136+
const ngtcp2_crypto_ctx* ctx,
136137
const ngtcp2_cid* dcid,
137138
ngtcp2_crypto_side side);
138139

0 commit comments

Comments
 (0)