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

Commit 02e5389

Browse files
committed
quic: use const refs
PR-URL: #216 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 4e9a96e commit 02e5389

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

src/node_quic_crypto.cc

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,32 @@ constexpr char QUIC_SERVER_HANDSHAKE_TRAFFIC_SECRET[] =
6565
constexpr char QUIC_SERVER_TRAFFIC_SECRET_0[] =
6666
"QUIC_SERVER_TRAFFIC_SECRET_0";
6767

68+
namespace {
69+
// Used solely to derive the keys used to generate retry tokens.
6870
bool DeriveTokenKey(
6971
uint8_t* token_key,
7072
uint8_t* token_iv,
7173
const uint8_t* rand_data,
7274
size_t rand_datalen,
73-
const ngtcp2_crypto_ctx* ctx,
74-
std::array<uint8_t, TOKEN_SECRETLEN>* token_secret) {
75+
const ngtcp2_crypto_ctx& ctx,
76+
const std::array<uint8_t, TOKEN_SECRETLEN>& token_secret) {
7577
TokenSecret secret;
7678

7779
return
7880
NGTCP2_OK(ngtcp2_crypto_hkdf_extract(
7981
secret.data(),
8082
secret.size(),
81-
&ctx->md,
82-
token_secret->data(),
83-
token_secret->size(),
83+
&ctx.md,
84+
token_secret.data(),
85+
token_secret.size(),
8486
rand_data,
8587
rand_datalen)) &&
8688
NGTCP2_OK(ngtcp2_crypto_derive_packet_protection_key(
8789
token_key,
8890
token_iv,
8991
nullptr,
90-
&ctx->aead,
91-
&ctx->md,
92+
&ctx.aead,
93+
&ctx.md,
9294
secret.data(),
9395
secret.size()));
9496
}
@@ -101,29 +103,25 @@ bool MessageDigest(
101103
ctx.reset(EVP_MD_CTX_new());
102104
CHECK(ctx);
103105

104-
if (EVP_DigestInit_ex(ctx.get(), meth, nullptr) != 1)
105-
return false;
106-
107-
if (EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()) != 1)
106+
if (EVP_DigestInit_ex(ctx.get(), meth, nullptr) != 1 ||
107+
EVP_DigestUpdate(ctx.get(), rand.data(), rand.size()) != 1) {
108108
return false;
109+
}
109110

110111
unsigned int mdlen = EVP_MD_size(meth);
111112

112113
return EVP_DigestFinal_ex(ctx.get(), dest->data(), &mdlen) == 1;
113114
}
114115

115-
bool GenerateRandData(uint8_t* buf, size_t len) {
116+
void GenerateRandData(uint8_t* buf, size_t len) {
116117
std::array<uint8_t, 16> rand;
117118
std::array<uint8_t, 32> md;
118119
EntropySource(rand.data(), rand.size());
119-
120-
if (!MessageDigest(&md, rand))
121-
return false;
122-
120+
CHECK(MessageDigest(&md, rand));
123121
CHECK_LE(len, md.size());
124122
std::copy_n(std::begin(md), len, buf);
125-
return true;
126123
}
124+
} // namespace
127125

128126
// The Retry Token is an encrypted token that is sent to the client
129127
// by the server as part of the path validation flow. The plaintext
@@ -139,7 +137,7 @@ bool GenerateRetryToken(
139137
size_t* tokenlen,
140138
const sockaddr* addr,
141139
const ngtcp2_cid* ocid,
142-
std::array<uint8_t, TOKEN_SECRETLEN>* token_secret) {
140+
const std::array<uint8_t, TOKEN_SECRETLEN>& token_secret) {
143141
std::array<uint8_t, 4096> plaintext;
144142

145143
ngtcp2_crypto_ctx ctx;
@@ -159,15 +157,14 @@ bool GenerateRetryToken(
159157
TokenKey token_key;
160158
TokenIV token_iv;
161159

162-
if (!GenerateRandData(rand_data.data(), TOKEN_RAND_DATALEN))
163-
return false;
160+
GenerateRandData(rand_data.data(), TOKEN_RAND_DATALEN);
164161

165162
if (!DeriveTokenKey(
166163
token_key.data(),
167164
token_iv.data(),
168165
rand_data.data(),
169166
TOKEN_RAND_DATALEN,
170-
&ctx,
167+
ctx,
171168
token_secret)) {
172169
return false;
173170
}
@@ -192,12 +189,13 @@ bool GenerateRetryToken(
192189
return true;
193190
}
194191

192+
// True if the received retry token is invalid.
195193
bool InvalidRetryToken(
196194
Environment* env,
197195
ngtcp2_cid* ocid,
198196
const ngtcp2_pkt_hd* hd,
199197
const sockaddr* addr,
200-
std::array<uint8_t, TOKEN_SECRETLEN>* token_secret,
198+
const std::array<uint8_t, TOKEN_SECRETLEN>& token_secret,
201199
uint64_t verification_expiration) {
202200

203201
ngtcp2_crypto_ctx ctx;
@@ -207,7 +205,7 @@ bool InvalidRetryToken(
207205
const size_t addrlen = SocketAddress::GetLength(addr);
208206

209207
if (hd->tokenlen < TOKEN_RAND_DATALEN)
210-
return true;
208+
return true;
211209

212210
uint8_t* rand_data = hd->token + hd->tokenlen - TOKEN_RAND_DATALEN;
213211
uint8_t* ciphertext = hd->token;
@@ -221,7 +219,7 @@ bool InvalidRetryToken(
221219
token_iv.data(),
222220
rand_data,
223221
TOKEN_RAND_DATALEN,
224-
&ctx,
222+
ctx,
225223
token_secret)) {
226224
return true;
227225
}

src/node_quic_crypto.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ bool GenerateRetryToken(
6868
size_t* tokenlen,
6969
const sockaddr* addr,
7070
const ngtcp2_cid* ocid,
71-
std::array<uint8_t, TOKEN_SECRETLEN>* token_secret);
71+
const std::array<uint8_t, TOKEN_SECRETLEN>& token_secret);
7272

7373
bool InvalidRetryToken(
7474
Environment* env,
7575
ngtcp2_cid* ocid,
7676
const ngtcp2_pkt_hd* hd,
7777
const sockaddr* addr,
78-
std::array<uint8_t, TOKEN_SECRETLEN>* token_secret,
78+
const std::array<uint8_t, TOKEN_SECRETLEN>& token_secret,
7979
uint64_t verification_expiration);
8080

8181
int VerifyHostnameIdentity(SSL* ssl, const char* hostname);

src/node_quic_socket.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ bool QuicSocket::SendRetry(
556556
&tokenlen,
557557
addr,
558558
*dcid,
559-
&token_secret_)) {
559+
token_secret_)) {
560560
return false;
561561
}
562562

@@ -685,7 +685,7 @@ BaseObjectPtr<QuicSession> QuicSocket::AcceptInitialPacket(
685685
&ocid,
686686
&hd,
687687
addr,
688-
&token_secret_,
688+
token_secret_,
689689
retry_token_expiration_)) {
690690
Debug(this, "A valid retry token was not found. Sending retry.");
691691
SendRetry(version, dcid, scid, addr);

0 commit comments

Comments
 (0)