1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
|
/**
* parser-quic.c
*
* Created on 2020-11-26
* @author: qyc
*
* @explain: QUIC解析
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser-quic.h"
#include "wsgcrypt.h"
#include "utils.h"
#include "pint.h"
#include "gcrypt.h"
// #define DEBUG_PARSER_QUIC
int gcry_init()
{
//const char * tmp = gcry_check_version("1.8.7");
//gcry_control(GCRYCTL_SET_THREAD_CBS,&gcry_threads_pthread);
return 0;
}
#define QUIC_LPT_INITIAL 0x0
#define QUIC_LPT_0RTT 0x1
#define QUIC_LPT_HANDSHAKE 0x2
#define QUIC_LPT_RETRY 0x3
/* Version Negotiation packets don't have any real packet type */
#define QUIC_LPT_VER_NEG 0xfe
/* dummy value that is definitely not LPT */
#define QUIC_SHORT_PACKET 0xff
/*
* Although the QUIC SCID/DCID length field can store at most 255, v1 limits the
* CID length to 20.
*/
#define QUIC_MAX_CID_LENGTH 20
typedef struct _quic_cid {
unsigned char len;
unsigned char cid[QUIC_MAX_CID_LENGTH];
} quic_cid_t;
/*
* PROTECTED PAYLOAD DECRYPTION (done in first pass)
*
* Long packet types always use a single cipher depending on packet type.
* Short packet types always use 1-RTT secrets for packet protection (pp).
*
* Considerations:
* - QUIC packets might appear out-of-order (short packets before handshake
* message is captured), lost or retransmitted/duplicated.
* - During live capture, keys might not be immediately be available. 1-RTT
* client keys will be ready while client proceses Server Hello (Handshake).
* 1-RTT server keys will be ready while server creates Handshake message in
* response to Initial Handshake.
* - So delay cipher creation until first short packet is received.
*
* Required input from TLS dissector: TLS-Exporter 0-RTT/1-RTT secrets and
* cipher/hash algorithms.
*
* QUIC payload decryption requires proper reconstruction of the packet number
* which requires proper header decryption. The different states are:
*
* Packet type Packet number space Secrets
* Long: Initial Initial Initial secrets
* Long: Handshake Handshake Handshake
* Long: 0-RTT 0/1-RTT (appdata) 0-RTT
* Short header 0/1-RTT (appdata) 1-RTT (KP0 / KP1)
*
* Important to note is that Short Header decryption requires TWO ciphers (one
* for each key phase), but that header protection uses only KP0. Total state
* needed for each peer (client and server):
* - 3 packet number spaces: Initial, Handshake, 0/1-RTT (appdata).
* - 4 header protection ciphers: initial, 0-RTT, HS, 1-RTT.
* - 5 payload protection ciphers: initial, 0-RTT, HS, 1-RTT (KP0), 1-RTT (KP1).
*/
typedef struct _quic_decrypt_result {
// Error message or NULL for success.
const guchar *error;
// Decrypted result on success (file-scoped).
const guint8 *data;
// Size of decrypted data.
guint data_len;
} quic_decrypt_result_t;
/** QUIC decryption context. */
typedef struct _quic_hp_cipher {
// Header protection cipher.
gcry_cipher_hd_t hp_cipher;
} quic_hp_cipher;
typedef struct _quic_pp_cipher {
// Packet protection cipher.
gcry_cipher_hd_t pp_cipher;
guint8 pp_iv[TLS13_AEAD_NONCE_LENGTH];
} quic_pp_cipher;
typedef struct _quic_ciphers {
quic_hp_cipher hp_cipher;
quic_pp_cipher pp_cipher;
} quic_ciphers;
/**
* State for a single QUIC connection, identified by one or more Destination
* Connection IDs (DCID).
*/
typedef struct _quic_info_data {
guint32 version;
quic_ciphers client_initial_ciphers;
quic_ciphers server_initial_ciphers;
// Packet number spaces for Initial, Handshake and appdata.
guint64 max_client_pkn[3];
guint64 max_server_pkn[3];
} quic_info_data_t;
/** Per-packet information about QUIC, populated on the first pass. */
typedef struct _quic_packet_info {
// Reconstructed full packet number.
guint64 packet_number;
quic_decrypt_result_t decryption;
// Length of PKN (1/2/3/4) or unknown (0).
guint8 pkn_len;
// Decrypted flag byte, valid only if pkn_len is non-zero.
guint8 first_byte;
} quic_packet_info_t;
/**
* Given a QUIC message (header + non-empty payload), the actual packet number,
* try to decrypt it using the PP cipher.
* As the header points to the original buffer with an encrypted packet number,
* the (encrypted) packet number length is also included.
*
* The actual packet number must be constructed according to
* https://tools.ietf.org/html/draft-ietf-quic-transport-22#section-12.3
*/
static void quic_decrypt_message(quic_pp_cipher *pp_cipher, const char *payload, guint length, guint header_length,
guint8 first_byte, guint pkn_len, guint64 packet_number, quic_decrypt_result_t *result)
{
gcry_error_t err;
guint8 *header;
guint8 nonce[TLS13_AEAD_NONCE_LENGTH];
guint8 *buffer;
guint8 atag[16];
guint buffer_length;
const guchar **error = &result->error;
g_assert(pp_cipher != NULL);
g_assert(pp_cipher->pp_cipher != NULL);
g_assert(pkn_len < header_length);
g_assert(1 <= pkn_len && pkn_len <= 4);
// copy header, but replace encrypted first byte and PKN by plaintext.
header = (guint8 *)g_malloc(header_length);
memcpy(header, payload, header_length);
header[0] = first_byte;
guint i;
for (i = 0; i < pkn_len; i++)
header[header_length - 1 - i] = (guint8)(packet_number >> (8 * i));
// Input is "header || ciphertext (buffer) || auth tag (16 bytes)"
// buffer_length = length - (header_length + 16);
// buffer_length = 297 - (2 + 16);
buffer_length = length - (pkn_len + 16);
if (buffer_length == 0) {
*error = (const guchar *)"Decryption not possible, ciphertext is too short";
return;
}
buffer = (guint8 *)g_malloc(buffer_length);
memcpy(buffer, payload + header_length, buffer_length);
memcpy(atag, payload + header_length + buffer_length, 16);
memcpy(nonce, pp_cipher->pp_iv, TLS13_AEAD_NONCE_LENGTH);
// Packet number is left-padded with zeroes and XORed with write_iv
phton64(nonce + sizeof(nonce) - 8, pntoh64(nonce + sizeof(nonce) - 8) ^ packet_number);
gcry_cipher_reset(pp_cipher->pp_cipher);
err = gcry_cipher_setiv(pp_cipher->pp_cipher, nonce, TLS13_AEAD_NONCE_LENGTH);
if (err) {
//printf("Decryption (setiv) failed: %s\n", gcry_strerror(err));
*error = (const guchar *)"Decryption (setiv) failed";
return;
}
// associated data (A) is the contents of QUIC header
err = gcry_cipher_authenticate(pp_cipher->pp_cipher, header, header_length);
if (err) {
//printf("Decryption (authenticate) failed: %s\n", gcry_strerror(err));
*error = (const guchar *)"Decryption (authenticate) failed";
return;
}
// Output ciphertext (C)
err = gcry_cipher_decrypt(pp_cipher->pp_cipher, buffer, buffer_length, NULL, 0);
if (err) {
//printf("Decryption (decrypt) failed: %s\n", gcry_strerror(err));
*error = (const guchar *)"Decryption (decrypt) failed";
return;
}
err = gcry_cipher_checktag(pp_cipher->pp_cipher, atag, 16);
if (err) {
//printf("Decryption (checktag) failed: %s\n", gcry_strerror(err));
*error = (const guchar *)"Decryption (checktag) failed";
return;
}
g_free(header);
result->error = NULL;
result->data = buffer;
result->data_len = buffer_length;
}
static gboolean quic_is_pp_cipher_initialized(quic_pp_cipher *pp_cipher)
{
return pp_cipher && pp_cipher->pp_cipher;
}
/**
* Process (protected) payload, adding the encrypted payload to the tree. If
* decryption is possible, frame dissection is also attempted.
*
* The given offset must correspond to the end of the QUIC header and begin of
* the (protected) payload. Dissected frames are appended to "tree" and expert
* info is attached to "ti" (the field with the encrypted payload).
*/
static void quic_process_payload(const char *payload, guint length, guint offset, quic_info_data_t *quic_info,
quic_packet_info_t *quic_packet, gboolean from_server, quic_pp_cipher *pp_cipher, guint8 first_byte, guint pkn_len)
{
/*
* If no decryption error has occurred yet, try decryption on the first
* pass and store the result for later use.
*/
if (quic_is_pp_cipher_initialized(pp_cipher))
quic_decrypt_message(pp_cipher, payload, length, offset, first_byte, pkn_len, quic_packet->packet_number, &quic_packet->decryption);
}
/* Inspired from ngtcp2 */
static guint64 quic_pkt_adjust_pkt_num(guint64 max_pkt_num, guint64 pkt_num, size_t n)
{
guint64 k = max_pkt_num == G_MAXUINT64 ? max_pkt_num : max_pkt_num + 1;
guint64 u = k & ~((G_GUINT64_CONSTANT(1) << n) - 1);
guint64 a = u | pkt_num;
guint64 b = (u + (G_GUINT64_CONSTANT(1) << n)) | pkt_num;
guint64 a1 = k < a ? a - k : k - a;
guint64 b1 = k < b ? b - k : k - b;
if (a1 < b1)
return a;
return b;
}
/**
* Retrieve the maximum valid packet number space for a peer.
*/
static guint64 *quic_max_packet_number(quic_info_data_t *quic_info, gboolean from_server, guint8 first_byte)
{
int pkn_space;
if ((first_byte & 0x80) && (first_byte & 0x30) >> 4 == QUIC_LPT_INITIAL)
// Long header, Initial
pkn_space = 0;
else if ((first_byte & 0x80) && (first_byte & 0x30) >> 4 == QUIC_LPT_HANDSHAKE)
// Long header, Handshake
pkn_space = 1;
else
// Long header (0-RTT) or Short Header (1-RTT appdata).
pkn_space = 2;
if (from_server)
return &quic_info->max_server_pkn[pkn_space];
else
return &quic_info->max_client_pkn[pkn_space];
}
/**
* Calculate the full packet number and store it for later use.
*/
static void quic_set_full_packet_number(quic_info_data_t *quic_info, quic_packet_info_t *quic_packet, gboolean from_server, guint8 first_byte, guint32 pkn32)
{
guint pkn_len = (first_byte & 3) + 1;
guint64 pkn_full;
guint64 max_pn = *quic_max_packet_number(quic_info, from_server, first_byte);
// Sequential first pass, try to reconstruct full packet number.
pkn_full = quic_pkt_adjust_pkt_num(max_pn, pkn32, 8 * pkn_len);
quic_packet->pkn_len = pkn_len;
quic_packet->packet_number = pkn_full;
}
/**
* Given a header protection cipher, a buffer and the packet number offset,
* return the unmasked first byte and packet number.
*/
static gboolean quic_decrypt_header(const char *payload, guint pn_offset, quic_hp_cipher *hp_cipher, int hp_cipher_algo, guint8 *first_byte, guint32 *pn)
{
if (!hp_cipher->hp_cipher)
// need to know the cipher.
return FALSE;
gcry_cipher_hd_t h = hp_cipher->hp_cipher;
// Sample is always 16 bytes and starts after PKN (assuming length 4).
// https://tools.ietf.org/html/draft-ietf-quic-tls-22#section-5.4.2
guint8 sample[16];
memcpy(sample, payload + pn_offset + 4, 16);
guint8 mask[5] = { 0 };
switch (hp_cipher_algo) {
case GCRY_CIPHER_AES128:
case GCRY_CIPHER_AES256:
// Encrypt in-place with AES-ECB and extract the mask.
if (gcry_cipher_encrypt(h, sample, sizeof(sample), NULL, 0))
return FALSE;
memcpy(mask, sample, sizeof(mask));
break;
#ifdef HAVE_LIBGCRYPT_CHACHA20
case GCRY_CIPHER_CHACHA20:
// If Gcrypt receives a 16 byte IV, it will assume the buffer to be
// counter || nonce (in little endian), as desired. */
if (gcry_cipher_setiv(h, sample, 16))
return FALSE;
// Apply ChaCha20, encrypt in-place five zero bytes.
if (gcry_cipher_encrypt(h, mask, sizeof(mask), NULL, 0))
return FALSE;
break;
#endif // HAVE_LIBGCRYPT_CHACHA20
default:
return FALSE;
}
// https://tools.ietf.org/html/draft-ietf-quic-tls-22#section-5.4.1
guint8 packet0 = payload[0];
if ((packet0 & 0x80) == 0x80)
// Long header: 4 bits masked
packet0 ^= mask[0] & 0x0f;
else
// Short header: 5 bits masked
packet0 ^= mask[0] & 0x1f;
guint pkn_len = (packet0 & 0x03) + 1;
guint8 pkn_bytes[4];
memcpy(pkn_bytes, payload + pn_offset, pkn_len);
guint32 pkt_pkn = 0;
guint i;
for (i = 0; i < pkn_len; i++)
pkt_pkn |= (pkn_bytes[i] ^ mask[1 + i]) << (8 * (pkn_len - 1 - i));
*first_byte = packet0;
*pn = pkt_pkn;
return TRUE;
}
static gboolean quic_hkdf_expand_label(int hash_algo, guint8 *secret, guint secret_len, const char *label, guint8 *out, guint out_len)
{
const StringInfo secret_si = { secret, secret_len };
guchar *out_mem = NULL;
if (tls13_hkdf_expand_label(hash_algo, &secret_si, "tls13 ", label, out_len, &out_mem)) {
memcpy(out, out_mem, out_len);
g_free(out_mem);
return TRUE;
}
return FALSE;
}
/**
* Expands the secret (length MUST be the same as the "hash_algo" digest size)
* and initialize cipher with the new key.
*/
static gboolean quic_hp_cipher_init(quic_hp_cipher *hp_cipher, int hash_algo, guint8 key_length, guint8 *secret)
{
guchar hp_key[256/8];
guint hash_len = gcry_md_get_algo_dlen(hash_algo);
if (!quic_hkdf_expand_label(hash_algo, secret, hash_len, "quic hp", hp_key, key_length))
return FALSE;
return gcry_cipher_setkey(hp_cipher->hp_cipher, hp_key, key_length) == 0;
}
static gboolean quic_pp_cipher_init(quic_pp_cipher *pp_cipher, int hash_algo, guint8 key_length, guint8 *secret)
{
// Maximum key size is for AES256 cipher.
guchar write_key[256/8];
guint hash_len = gcry_md_get_algo_dlen(hash_algo);
if (key_length > sizeof(write_key))
return FALSE;
if (!quic_hkdf_expand_label(hash_algo, secret, hash_len, "quic key", write_key, key_length) ||
!quic_hkdf_expand_label(hash_algo, secret, hash_len, "quic iv", pp_cipher->pp_iv, sizeof(pp_cipher->pp_iv)))
return FALSE;
return gcry_cipher_setkey(pp_cipher->pp_cipher, write_key, key_length) == 0;
}
static void quic_hp_cipher_reset(quic_hp_cipher *hp_cipher)
{
gcry_cipher_close(hp_cipher->hp_cipher);
memset(hp_cipher, 0, sizeof(*hp_cipher));
}
static void quic_pp_cipher_reset(quic_pp_cipher *pp_cipher)
{
gcry_cipher_close(pp_cipher->pp_cipher);
memset(pp_cipher, 0, sizeof(*pp_cipher));
}
/**
* Maps a Packet Protection cipher to the Packet Number protection cipher.
* See https://tools.ietf.org/html/draft-ietf-quic-tls-22#section-5.4.3
*/
static gboolean quic_get_pn_cipher_algo(int cipher_algo, int *hp_cipher_mode)
{
switch (cipher_algo) {
case GCRY_CIPHER_AES128:
case GCRY_CIPHER_AES256:
*hp_cipher_mode = GCRY_CIPHER_MODE_ECB;
return TRUE;
#ifdef HAVE_LIBGCRYPT_CHACHA20
case GCRY_CIPHER_CHACHA20:
*hp_cipher_mode = GCRY_CIPHER_MODE_STREAM;
return TRUE;
#endif // HAVE_LIBGCRYPT_CHACHA20
default:
return FALSE;
}
}
/*
* (Re)initialize the PNE/PP ciphers using the given cipher algorithm.
* If the optional base secret is given, then its length MUST match the hash
* algorithm output.
*/
static gboolean quic_hp_cipher_prepare(quic_hp_cipher *hp_cipher, int hash_algo, int cipher_algo, guint8 *secret, const char **error)
{
// Clear previous state (if any).
quic_hp_cipher_reset(hp_cipher);
int hp_cipher_mode;
if (!quic_get_pn_cipher_algo(cipher_algo, &hp_cipher_mode)) {
*error = "Unsupported cipher algorithm";
return FALSE;
}
if (gcry_cipher_open(&hp_cipher->hp_cipher, cipher_algo, hp_cipher_mode, 0)) {
quic_hp_cipher_reset(hp_cipher);
*error = "Failed to create HP cipher";
return FALSE;
}
if (secret) {
guint cipher_keylen = (guint8)gcry_cipher_get_algo_keylen(cipher_algo);
if (!quic_hp_cipher_init(hp_cipher, hash_algo, cipher_keylen, secret)) {
quic_hp_cipher_reset(hp_cipher);
*error = "Failed to derive key material for HP cipher";
return FALSE;
}
}
return TRUE;
}
static gboolean quic_pp_cipher_prepare(quic_pp_cipher *pp_cipher, int hash_algo, int cipher_algo, int cipher_mode, guint8 *secret, const char **error)
{
// Clear previous state (if any).
quic_pp_cipher_reset(pp_cipher);
int hp_cipher_mode;
if (!quic_get_pn_cipher_algo(cipher_algo, &hp_cipher_mode)) {
*error = "Unsupported cipher algorithm";
return FALSE;
}
if (gcry_cipher_open(&pp_cipher->pp_cipher, cipher_algo,cipher_mode, 0)) {
quic_pp_cipher_reset(pp_cipher);
*error = "Failed to create PP cipher";
return FALSE;
}
if (secret) {
guint cipher_keylen = (guint8) gcry_cipher_get_algo_keylen(cipher_algo);
if (!quic_pp_cipher_init(pp_cipher, hash_algo, cipher_keylen, secret)) {
quic_pp_cipher_reset(pp_cipher);
*error = "Failed to derive key material for PP cipher";
return FALSE;
}
}
return TRUE;
}
static gboolean quic_ciphers_prepare(quic_ciphers *ciphers, int hash_algo, int cipher_algo, int cipher_mode, guint8 *secret, const char **error)
{
return quic_hp_cipher_prepare(&ciphers->hp_cipher, hash_algo, cipher_algo, secret, error) &&
quic_pp_cipher_prepare(&ciphers->pp_cipher, hash_algo, cipher_algo, cipher_mode, secret, error);
}
/* Returns the QUIC draft version or 0 if not applicable. */
static inline guint8 quic_draft_version(guint32 version) {
if ((version >> 8) == 0xff0000)
return (guint8) version;
// Facebook mvfst, based on draft -22.
if (version == 0xfaceb001)
return 22;
// Facebook mvfst, based on draft -27.
if (version == 0xfaceb002 || version == 0xfaceb00e)
return 27;
// GQUIC Q050, T050 and T051: they are not really based on any drafts,
// but we must return a sensible value
if (version == 0x51303530 || version == 0x54303530 || version == 0x54303531)
return 27;
/*
* https://tools.ietf.org/html/draft-ietf-quic-transport-32#section-15
* "Versions that follow the pattern 0x?a?a?a?a are reserved for use in
* forcing version negotiation to be exercised"
* It is tricky to return a correct draft version: such number is primarly
* used to select a proper salt (which depends on the version itself), but
* we don't have a real version here! Let's hope that we need to handle
* only latest drafts...
*/
if ((version & 0x0F0F0F0F) == 0x0a0a0a0a)
return 29;
return 0;
}
static inline gboolean is_quic_draft_max(guint32 version, guint8 max_version) {
guint8 draft_version = quic_draft_version(version);
return draft_version && draft_version <= max_version;
}
/**
* Compute the client and server initial secrets given Connection ID "cid".
*
* On success TRUE is returned and the two initial secrets are set.
* FALSE is returned on error (see "error" parameter for the reason).
*/
static gboolean quic_derive_initial_secrets(const quic_cid_t *cid, guint8 client_initial_secret[HASH_SHA2_256_LENGTH], guint8 server_initial_secret[HASH_SHA2_256_LENGTH], guint32 version, const gchar **error)
{
/*
* https://tools.ietf.org/html/draft-ietf-quic-tls-29#section-5.2
*
* initial_salt = 0xafbfec289993d24c9e9786f19c6111e04390a899
* initial_secret = HKDF-Extract(initial_salt, client_dst_connection_id)
*
* client_initial_secret = HKDF-Expand-Label(initial_secret,
* "client in", "", Hash.length)
* server_initial_secret = HKDF-Expand-Label(initial_secret,
* "server in", "", Hash.length)
*
* Hash for handshake packets is SHA-256 (output size 32).
*/
static const guint8 handshake_salt_draft_22[20] = {
0x7f, 0xbc, 0xdb, 0x0e, 0x7c, 0x66, 0xbb, 0xe9, 0x19, 0x3a,
0x96, 0xcd, 0x21, 0x51, 0x9e, 0xbd, 0x7a, 0x02, 0x64, 0x4a
};
static const guint8 handshake_salt_draft_23[20] = {
0xc3, 0xee, 0xf7, 0x12, 0xc7, 0x2e, 0xbb, 0x5a, 0x11, 0xa7,
0xd2, 0x43, 0x2b, 0xb4, 0x63, 0x65, 0xbe, 0xf9, 0xf5, 0x02,
};
static const guint8 handshake_salt_draft_29[20] = {
0xaf, 0xbf, 0xec, 0x28, 0x99, 0x93, 0xd2, 0x4c, 0x9e, 0x97,
0x86, 0xf1, 0x9c, 0x61, 0x11, 0xe0, 0x43, 0x90, 0xa8, 0x99
};
static const guint8 handshake_salt_v1[20] = {
0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17,
0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a
};
static const guint8 hanshake_salt_draft_q50[20] = {
0x50, 0x45, 0x74, 0xEF, 0xD0, 0x66, 0xFE, 0x2F, 0x9D, 0x94,
0x5C, 0xFC, 0xDB, 0xD3, 0xA7, 0xF0, 0xD3, 0xB5, 0x6B, 0x45
};
static const guint8 hanshake_salt_draft_t50[20] = {
0x7f, 0xf5, 0x79, 0xe5, 0xac, 0xd0, 0x72, 0x91, 0x55, 0x80,
0x30, 0x4c, 0x43, 0xa2, 0x36, 0x7c, 0x60, 0x48, 0x83, 0x10
};
static const guint8 hanshake_salt_draft_t51[20] = {
0x7a, 0x4e, 0xde, 0xf4, 0xe7, 0xcc, 0xee, 0x5f, 0xa4, 0x50,
0x6c, 0x19, 0x12, 0x4f, 0xc8, 0xcc, 0xda, 0x6e, 0x03, 0x3d
};
gcry_error_t err;
guint8 secret[HASH_SHA2_256_LENGTH];
if (version == 0x51303530)
{
err = hkdf_extract(GCRY_MD_SHA256, hanshake_salt_draft_q50, sizeof(hanshake_salt_draft_q50), cid->cid, cid->len, secret);
}
else if (version == 0x54303530)
{
err = hkdf_extract(GCRY_MD_SHA256, hanshake_salt_draft_t50, sizeof(hanshake_salt_draft_t50), cid->cid, cid->len, secret);
}
else if (version == 0x54303531)
{
err = hkdf_extract(GCRY_MD_SHA256, hanshake_salt_draft_t51, sizeof(hanshake_salt_draft_t51), cid->cid, cid->len, secret);
}
else if (is_quic_draft_max(version, 22))
{
err = hkdf_extract(GCRY_MD_SHA256, handshake_salt_draft_22, sizeof(handshake_salt_draft_22), cid->cid, cid->len, secret);
}
else if (is_quic_draft_max(version, 28))
{
err = hkdf_extract(GCRY_MD_SHA256, handshake_salt_draft_23, sizeof(handshake_salt_draft_23), cid->cid, cid->len, secret);
}
else if (is_quic_draft_max(version, 32))
{
err = hkdf_extract(GCRY_MD_SHA256, handshake_salt_draft_29, sizeof(handshake_salt_draft_29), cid->cid, cid->len, secret);
}
else
{
err = hkdf_extract(GCRY_MD_SHA256, handshake_salt_v1, sizeof(handshake_salt_v1), cid->cid, cid->len, secret);
}
if (err) {
//printf("Failed to extract secrets: %s\n", gcry_strerror(err));
*error = "Failed to extract secrets";
return FALSE;
}
if (!quic_hkdf_expand_label(GCRY_MD_SHA256, secret, sizeof(secret), "client in", client_initial_secret, HASH_SHA2_256_LENGTH)) {
*error = "Key expansion (client) failed";
return FALSE;
}
if (!quic_hkdf_expand_label(GCRY_MD_SHA256, secret, sizeof(secret), "server in", server_initial_secret, HASH_SHA2_256_LENGTH)) {
*error = "Key expansion (server) failed";
return FALSE;
}
*error = NULL;
return TRUE;
}
static gboolean quic_create_initial_decoders(const quic_cid_t *cid, const gchar **error, quic_info_data_t *quic_info)
{
unsigned char client_secret[HASH_SHA2_256_LENGTH];
unsigned char server_secret[HASH_SHA2_256_LENGTH];
if (!quic_derive_initial_secrets(cid, client_secret, server_secret, quic_info->version, error))
return -1;
// Packet numbers are protected with AES128-CTR,
// initial packets are protected with AEAD_AES_128_GCM.
if (!quic_ciphers_prepare(&quic_info->client_initial_ciphers, GCRY_MD_SHA256, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_GCM, client_secret, error))
{
return FALSE;
}
if(!quic_ciphers_prepare(&quic_info->server_initial_ciphers, GCRY_MD_SHA256, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_GCM, server_secret, error))
{
quic_hp_cipher_reset(&quic_info->client_initial_ciphers.hp_cipher);
quic_pp_cipher_reset(&quic_info->client_initial_ciphers.pp_cipher);
return FALSE;
}
return TRUE;
}
static int quic_extract_header(const char *payload, unsigned char *long_packet_type, unsigned int *version, quic_cid_t *dcid, quic_cid_t *scid)
{
unsigned int offset = 0;
unsigned char packet_type = payload[offset];
unsigned char is_long_header = packet_type & 0x80;
if (is_long_header)
// long header form
*long_packet_type = (packet_type & 0x30) >> 4;
else
// short header form, store dummy value that is not a long packet type.
*long_packet_type = QUIC_SHORT_PACKET;
offset++;
*version = pntoh32((unsigned int *)&payload[offset]);
if (is_long_header) {
// VN packets don't have any real packet type field,
// even if they have a long header: use a dummy value */
if (*version == 0x00000000)
*long_packet_type = QUIC_LPT_VER_NEG;
// skip version
offset += 4;
// read DCID and SCID (both are prefixed by a length byte).
unsigned char dcil = payload[offset];
offset++;
if (dcil && dcil <= QUIC_MAX_CID_LENGTH) {
memcpy(dcid->cid, &payload[offset], dcil);
dcid->len = dcil;
}
offset += dcil;
unsigned char scil = payload[offset];
offset++;
if (scil && scil <= QUIC_MAX_CID_LENGTH) {
memcpy(scid->cid, &payload[offset], scil);
scid->len = scil;
}
offset += scil;
}
else {
// Definitely not draft -10, set version to dummy value.
*version = 0;
// For short headers, the DCID length is unknown and could be 0 or
// anything from 1 to 20 bytes. Copy the maximum possible and let the
// consumer truncate it as necessary.
memcpy(dcid->cid, &payload[offset], QUIC_MAX_CID_LENGTH);
dcid->len = QUIC_MAX_CID_LENGTH;
offset += QUIC_MAX_CID_LENGTH;
}
return offset;
}
int dissect_quic(const char *payload, unsigned int length, unsigned char *out, unsigned int *out_length)
{
guint offset = 0;
quic_packet_info_t quic_packet;
quic_info_data_t conn;
unsigned char long_packet_type;
quic_cid_t dcid = {.len=0}, scid = {.len=0};
guint64 token_length, payload_length;
const char *error = NULL;
guint8 first_byte = 0;
const gboolean from_server = FALSE;
quic_ciphers *ciphers = NULL;
int ret;
memset(&quic_packet, 0, sizeof(quic_packet_info_t));
memset(&conn, 0, sizeof(quic_info_data_t));
ret = quic_extract_header(payload, &long_packet_type, &conn.version, &dcid, &scid);
if (ret < 0)
{
return -1;
}
if (long_packet_type == QUIC_LPT_INITIAL)
{
// Create new decryption context based on the Client Connection ID
// from the *very first* Client Initial packet.
quic_create_initial_decoders(&dcid, &error, &conn);
if (!error)
{
guint32 pkn32 = 0;
// PKN is after type(1) + version(4) + DCIL+DCID + SCIL+SCID
guint pn_offset = 1 + 4 + 1 + dcid.len + 1 + scid.len;
pn_offset += tvb_get_varint(payload, pn_offset, 8, &token_length, ENC_VARINT_QUIC);
pn_offset += (guint)token_length;
// printf("%d\n", token_length);
pn_offset += tvb_get_varint(payload, pn_offset, 8, &payload_length, ENC_VARINT_QUIC);
// printf("%d\n", payload_length);
// Assume failure unless proven otherwise.
ciphers = &conn.client_initial_ciphers;
error = "Header deprotection failed";
if (quic_decrypt_header(payload, pn_offset, &ciphers->hp_cipher, GCRY_CIPHER_AES128, &first_byte, &pkn32))
error = NULL;
if (!error) {
quic_set_full_packet_number(&conn, &quic_packet, from_server, first_byte, pkn32);
quic_packet.first_byte = first_byte;
}
// Payload
// skip type(1) + version(4) + DCIL+DCID + SCIL+SCID + len_token_length + token_length + len_payload_length + len_packet_number
offset = pn_offset + quic_packet.pkn_len;
//quic_process_payload(payload, length, offset, &conn, &quic_packet, from_server, &ciphers->pp_cipher, first_byte, quic_packet.pkn_len);
quic_process_payload(payload, payload_length, offset, &conn, &quic_packet, from_server, &ciphers->pp_cipher, first_byte, quic_packet.pkn_len);
// Out
if (!quic_packet.decryption.error)
{
memcpy(out, quic_packet.decryption.data, quic_packet.decryption.data_len);
*out_length = quic_packet.decryption.data_len;
g_free((gpointer)quic_packet.decryption.data);
quic_packet.decryption.data = NULL;
ret=1;
}
else
{
ret=0;
}
quic_hp_cipher_reset(&conn.client_initial_ciphers.hp_cipher);
quic_pp_cipher_reset(&conn.client_initial_ciphers.pp_cipher);
quic_hp_cipher_reset(&conn.server_initial_ciphers.hp_cipher);
quic_pp_cipher_reset(&conn.server_initial_ciphers.pp_cipher);
return ret;
}
}
return 0;
}
|