@@ -65,30 +65,32 @@ constexpr char QUIC_SERVER_HANDSHAKE_TRAFFIC_SECRET[] =
65
65
constexpr char QUIC_SERVER_TRAFFIC_SECRET_0[] =
66
66
" QUIC_SERVER_TRAFFIC_SECRET_0" ;
67
67
68
+ namespace {
69
+ // Used solely to derive the keys used to generate retry tokens.
68
70
bool DeriveTokenKey (
69
71
uint8_t * token_key,
70
72
uint8_t * token_iv,
71
73
const uint8_t * rand_data,
72
74
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) {
75
77
TokenSecret secret;
76
78
77
79
return
78
80
NGTCP2_OK (ngtcp2_crypto_hkdf_extract (
79
81
secret.data (),
80
82
secret.size (),
81
- &ctx-> md ,
82
- token_secret-> data (),
83
- token_secret-> size (),
83
+ &ctx. md ,
84
+ token_secret. data (),
85
+ token_secret. size (),
84
86
rand_data,
85
87
rand_datalen)) &&
86
88
NGTCP2_OK (ngtcp2_crypto_derive_packet_protection_key (
87
89
token_key,
88
90
token_iv,
89
91
nullptr ,
90
- &ctx-> aead ,
91
- &ctx-> md ,
92
+ &ctx. aead ,
93
+ &ctx. md ,
92
94
secret.data (),
93
95
secret.size ()));
94
96
}
@@ -101,29 +103,25 @@ bool MessageDigest(
101
103
ctx.reset (EVP_MD_CTX_new ());
102
104
CHECK (ctx);
103
105
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 ) {
108
108
return false ;
109
+ }
109
110
110
111
unsigned int mdlen = EVP_MD_size (meth);
111
112
112
113
return EVP_DigestFinal_ex (ctx.get (), dest->data (), &mdlen) == 1 ;
113
114
}
114
115
115
- bool GenerateRandData (uint8_t * buf, size_t len) {
116
+ void GenerateRandData (uint8_t * buf, size_t len) {
116
117
std::array<uint8_t , 16 > rand;
117
118
std::array<uint8_t , 32 > md;
118
119
EntropySource (rand.data (), rand.size ());
119
-
120
- if (!MessageDigest (&md, rand))
121
- return false ;
122
-
120
+ CHECK (MessageDigest (&md, rand));
123
121
CHECK_LE (len, md.size ());
124
122
std::copy_n (std::begin (md), len, buf);
125
- return true ;
126
123
}
124
+ } // namespace
127
125
128
126
// The Retry Token is an encrypted token that is sent to the client
129
127
// by the server as part of the path validation flow. The plaintext
@@ -139,7 +137,7 @@ bool GenerateRetryToken(
139
137
size_t * tokenlen,
140
138
const sockaddr* addr,
141
139
const ngtcp2_cid* ocid,
142
- std::array<uint8_t , TOKEN_SECRETLEN>* token_secret) {
140
+ const std::array<uint8_t , TOKEN_SECRETLEN>& token_secret) {
143
141
std::array<uint8_t , 4096 > plaintext;
144
142
145
143
ngtcp2_crypto_ctx ctx;
@@ -159,15 +157,14 @@ bool GenerateRetryToken(
159
157
TokenKey token_key;
160
158
TokenIV token_iv;
161
159
162
- if (!GenerateRandData (rand_data.data (), TOKEN_RAND_DATALEN))
163
- return false ;
160
+ GenerateRandData (rand_data.data (), TOKEN_RAND_DATALEN);
164
161
165
162
if (!DeriveTokenKey (
166
163
token_key.data (),
167
164
token_iv.data (),
168
165
rand_data.data (),
169
166
TOKEN_RAND_DATALEN,
170
- & ctx,
167
+ ctx,
171
168
token_secret)) {
172
169
return false ;
173
170
}
@@ -192,12 +189,13 @@ bool GenerateRetryToken(
192
189
return true ;
193
190
}
194
191
192
+ // True if the received retry token is invalid.
195
193
bool InvalidRetryToken (
196
194
Environment* env,
197
195
ngtcp2_cid* ocid,
198
196
const ngtcp2_pkt_hd* hd,
199
197
const sockaddr* addr,
200
- std::array<uint8_t , TOKEN_SECRETLEN>* token_secret,
198
+ const std::array<uint8_t , TOKEN_SECRETLEN>& token_secret,
201
199
uint64_t verification_expiration) {
202
200
203
201
ngtcp2_crypto_ctx ctx;
@@ -207,7 +205,7 @@ bool InvalidRetryToken(
207
205
const size_t addrlen = SocketAddress::GetLength (addr);
208
206
209
207
if (hd->tokenlen < TOKEN_RAND_DATALEN)
210
- return true ;
208
+ return true ;
211
209
212
210
uint8_t * rand_data = hd->token + hd->tokenlen - TOKEN_RAND_DATALEN;
213
211
uint8_t * ciphertext = hd->token ;
@@ -221,7 +219,7 @@ bool InvalidRetryToken(
221
219
token_iv.data (),
222
220
rand_data,
223
221
TOKEN_RAND_DATALEN,
224
- & ctx,
222
+ ctx,
225
223
token_secret)) {
226
224
return true ;
227
225
}
0 commit comments