-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathxqc_packet_parser.c
More file actions
1517 lines (1276 loc) · 51.9 KB
/
xqc_packet_parser.c
File metadata and controls
1517 lines (1276 loc) · 51.9 KB
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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @copyright Copyright (c) 2022, Alibaba Group Holding Limited
*/
#include <string.h>
#ifdef XQC_SYS_WINDOWS
#include <openssl/x509.h>
#endif
#include <openssl/hmac.h>
#include "src/transport/xqc_packet_parser.h"
#include "src/transport/xqc_cid.h"
#include "src/common/utils/vint/xqc_variable_len_int.h"
#include "src/transport/xqc_packet_out.h"
#include "src/common/xqc_algorithm.h"
#include "src/common/xqc_log.h"
#include "src/transport/xqc_engine.h"
#include "src/transport/xqc_conn.h"
#include "src/transport/xqc_packet.h"
#include "src/transport/xqc_stream.h"
#include "src/transport/xqc_utils.h"
#include "src/transport/xqc_send_ctl.h"
#include "src/transport/xqc_defs.h"
#include "src/common/xqc_random.h"
#define xqc_packet_number_bits2len(b) ((b) + 1)
#define XQC_RESET_TOKEN_LEN 16
unsigned
xqc_short_packet_header_size(unsigned char dcid_len, unsigned char pktno_bits)
{
return 1 /* first byte */
+ dcid_len
+ xqc_packet_number_bits2len(pktno_bits);
}
unsigned
xqc_long_packet_header_size (unsigned char dcid_len, unsigned char scid_len, unsigned token_len,
unsigned char pktno_bits, xqc_pkt_type_t type)
{
return 1 /* first byte */
+ 4 /* version */
+ 2 /* DCID Len (8) SCID Len (8) */
+ dcid_len
+ scid_len
+ (type == XQC_PTYPE_INIT ? xqc_vint_len_by_val((unsigned)token_len) + token_len : 0)
+ XQC_LONG_HEADER_LENGTH_BYTE /* Length (i) */
+ xqc_packet_number_bits2len(pktno_bits);
}
xqc_int_t
xqc_packet_parse_cid(xqc_cid_t *dcid, xqc_cid_t *scid, uint8_t cid_len, const unsigned char *buf, size_t size)
{
const unsigned char *pos = NULL;
const unsigned char *end = buf + size;
if (size <= 0) {
return -XQC_EPARAM;
}
if ((buf[0] & 0x40) == 0 && (buf[0] & 0x80) == 0) {
return -XQC_EILLPKT;
}
/* short header */
if (XQC_PACKET_IS_SHORT_HEADER(buf)) {
if (size < 1 + cid_len) {
return -XQC_EILLPKT;
}
xqc_cid_set(dcid, buf + 1, cid_len);
return XQC_OK;
}
/* long header */
if (size < XQC_PACKET_LONG_HEADER_PREFIX_LENGTH + 2) {
return -XQC_EILLPKT;
}
pos = buf + 1 + XQC_PACKET_VERSION_LENGTH;
dcid->cid_len = (uint8_t)(*pos);
if (dcid->cid_len > XQC_MAX_CID_LEN) {
return -XQC_EILLPKT;
}
pos += 1;
if (XQC_BUFF_LEFT_SIZE(pos, end) < dcid->cid_len + 1) {
return -XQC_EILLPKT;
}
xqc_memcpy(dcid->cid_buf, pos, dcid->cid_len);
pos += dcid->cid_len;
scid->cid_len = (uint8_t)(*pos);
if (scid->cid_len > XQC_MAX_CID_LEN) {
return -XQC_EILLPKT;
}
pos += 1;
if (XQC_BUFF_LEFT_SIZE(pos, end) < scid->cid_len) {
return -XQC_EILLPKT;
}
xqc_memcpy(scid->cid_buf, pos, scid->cid_len);
pos += scid->cid_len;
return XQC_OK;
}
void
xqc_packet_parse_packet_number(uint8_t *pos, xqc_uint_t packet_number_len, uint64_t *packet_num)
{
*packet_num = 0;
for (int i = 0; i < packet_number_len; i++) {
*packet_num = ((*packet_num) << 8u) + (*pos);
pos++;
}
}
/**
* https://datatracker.ietf.org/doc/html/rfc9000#appendix-A.3
* @param largest_pn Largest received packet number
* @param truncated_pn Packet number parsed from header
* @param pn_nbits Number of bits the truncated_pn has
* @return
*/
xqc_packet_number_t
xqc_packet_decode_packet_number(xqc_packet_number_t largest_pn, xqc_packet_number_t truncated_pn,
unsigned pn_nbits)
{
xqc_packet_number_t expected_pn, pn_win, pn_hwin, pn_mask, candidate_pn;
expected_pn = largest_pn + 1;
pn_win = (xqc_packet_number_t) 1 << pn_nbits;
pn_hwin = pn_win >> (xqc_packet_number_t) 1;
pn_mask = pn_win - 1;
candidate_pn = (expected_pn & ~pn_mask) | truncated_pn;
// To fully align with RFC9000
if ((candidate_pn + pn_hwin <= expected_pn)
&& (candidate_pn < ((1ULL << 62) - pn_win)))
{
return candidate_pn + pn_win;
}
if (candidate_pn > expected_pn + pn_hwin && candidate_pn >= pn_win) {
return candidate_pn - pn_win;
}
return candidate_pn;
}
int
xqc_write_packet_number(unsigned char *buf, xqc_packet_number_t packet_number, unsigned char packet_number_bits)
{
unsigned char *p = buf;
unsigned int packet_number_len = xqc_packet_number_bits2len(packet_number_bits);
if (packet_number_len == 4) {
*buf++ = packet_number >> 24;
}
if (packet_number_len >= 3) {
*buf++ = packet_number >> 16;
}
if (packet_number_len >= 2) {
*buf++ = packet_number >> 8;
}
*buf++ = packet_number;
return buf - p;
}
int
xqc_gen_short_packet_header(xqc_packet_out_t *packet_out, unsigned char *dcid, unsigned int dcid_len,
unsigned char packet_number_bits, xqc_packet_number_t packet_number, xqc_uint_t key_phase)
{
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|0|1|S|R|R|K|P P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/24/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protected Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Short Header Packet Format
*/
unsigned char spin_bit = 0x01;
unsigned char reserved_bits = 0x00;
unsigned char key_phase_bit = key_phase ? 0x01 : 0x00;
unsigned int packet_number_len = xqc_packet_number_bits2len(packet_number_bits);
unsigned int need = 1 + dcid_len + packet_number_len;
unsigned char *dst_buf = packet_out->po_buf;
size_t dst_buf_size = xqc_get_po_remained_size(packet_out);
packet_out->po_pkt.pkt_type = XQC_PTYPE_SHORT_HEADER;
if (need > dst_buf_size) {
return -XQC_ENOBUF;
}
dst_buf[0] = 0x40 | spin_bit << 5 | reserved_bits << 3 | key_phase_bit << 2 | packet_number_bits;
dst_buf++;
if (dcid_len) {
memcpy(dst_buf, dcid, dcid_len);
}
dst_buf += dcid_len;
packet_out->po_ppktno = dst_buf;
dst_buf += xqc_write_packet_number(dst_buf, packet_number, packet_number_bits);
packet_out->po_payload = dst_buf;
return need;
}
/*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|0|1|S|R|R|K|P P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/24/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protected Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Short Header Packet Format
*/
xqc_int_t
xqc_packet_parse_short_header(xqc_connection_t *c, xqc_packet_in_t *packet_in)
{
unsigned char *pos = packet_in->pos;
unsigned char *end = packet_in->last;
xqc_packet_t *packet = &packet_in->pi_pkt;
xqc_path_ctx_t *path = NULL;
uint8_t cid_len = c->scid_set.user_scid.cid_len;
packet_in->pi_pkt.pkt_type = XQC_PTYPE_SHORT_HEADER;
packet_in->pi_pkt.pkt_pns = XQC_PNS_APP_DATA;
if (XQC_BUFF_LEFT_SIZE(pos, packet_in->last) < 1 + cid_len) {
xqc_log(c->log, XQC_LOG_ERROR, "|cid len error|cid_len:%d|size:%d",
1 + cid_len, XQC_BUFF_LEFT_SIZE(pos, packet_in->last));
return -XQC_EILLPKT;
}
/* check fixed bit(0x40) = 1 */
if ((pos[0] & 0x40) == 0) {
xqc_log(c->log, XQC_LOG_ERROR, "|parse short header: fixed bit err|pos[0]:%d", (uint32_t)pos[0]);
return -XQC_EILLPKT;
}
xqc_uint_t spin_bit = (pos[0] & 0x20) >> 5;
pos += 1;
/* check dcid */
xqc_cid_set(&(packet->pkt_dcid), pos, cid_len);
pos += cid_len;
if (xqc_conn_check_dcid(c, &(packet->pkt_dcid)) != XQC_OK) {
/* log & ignore, the pkt might be corrupted or stateless reset */
xqc_log(c->log, XQC_LOG_WARN, "|parse short header|invalid destination cid, pkt dcid: %s, conn scid: %s|",
xqc_dcid_str(c->engine, &packet->pkt_dcid), xqc_scid_str(c->engine, &c->scid_set.user_scid));
return -XQC_EILLPKT;
}
packet_in->pi_path_id = packet_in->pi_pkt.pkt_dcid.path_id;
xqc_log(c->log, XQC_LOG_DEBUG, "|parse short header|path:%ui|pkt_dcid:%s|spin_bit:%ud|",
packet_in->pi_path_id, xqc_scid_str(c->engine, &(packet->pkt_dcid)), spin_bit);
/* packet number */
packet_in->pi_pkt.length = packet_in->last - pos;
packet_in->pi_pkt.pkt_num_offset = pos - packet_in->buf;
if (packet_in->last > end) {
xqc_log(c->log, XQC_LOG_ERROR, "|illegal pkt with wrong length");
return -XQC_EILLPKT;
}
if (c->conn_type == XQC_CONN_TYPE_CLIENT) {
c->discard_vn_flag = 1;
}
return XQC_OK;
}
void
xqc_long_packet_update_length(xqc_packet_out_t *packet_out)
{
if (packet_out->po_pkt.pkt_type == XQC_PTYPE_INIT
|| packet_out->po_pkt.pkt_type == XQC_PTYPE_HSK
|| packet_out->po_pkt.pkt_type == XQC_PTYPE_0RTT)
{
unsigned char *plength = packet_out->po_ppktno - XQC_LONG_HEADER_LENGTH_BYTE;
unsigned length = packet_out->po_buf + packet_out->po_used_size - packet_out->po_ppktno;
xqc_vint_write(plength, length, 0x01, 2);
}
}
void
xqc_short_packet_update_key_phase(xqc_packet_out_t *packet_out, xqc_uint_t key_phase)
{
if (packet_out->po_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER) {
unsigned char key_phase_bit = key_phase ? 0x01 : 0x00;
unsigned char *dst_buf = packet_out->po_buf;
dst_buf[0] &= (~(1<<2)); /* clear up original key phase bit */
dst_buf[0] |= (key_phase_bit << 2); /* set current updated key phase bit */
}
}
void
xqc_short_packet_update_dcid(xqc_packet_out_t *packet_out, xqc_cid_t dcid)
{
if (packet_out->po_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER) {
unsigned char *dst = packet_out->po_buf + 1;
/* dcid len can't be changed */
xqc_memcpy(dst, dcid.cid_buf, dcid.cid_len);
}
}
/* TODO: remove custom logic and disable spin bit */
void xqc_packet_update_reserved_bits(xqc_packet_out_t *packet_out)
{
if (packet_out->po_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER
&& packet_out->po_flag & XQC_POF_REINJECTED_REPLICA)
{
unsigned char *dst_buf = packet_out->po_buf;
/* reserved bits 10 */
dst_buf[0] |= (1 << 4);
}
if (packet_out->po_pkt.pkt_type == XQC_PTYPE_0RTT
&& packet_out->po_flag & XQC_POF_REINJECTED_REPLICA)
{
/* reserved bits 10 */
unsigned char *dst_buf = packet_out->po_buf;
dst_buf[0] |= (1 << 3);
}
}
ssize_t
xqc_gen_long_packet_header (xqc_packet_out_t *packet_out,
const unsigned char *dcid, unsigned char dcid_len,
const unsigned char *scid, unsigned char scid_len,
const unsigned char *token, uint32_t token_len,
xqc_proto_version_t ver,
unsigned char pktno_bits)
{
unsigned char *dst_buf = packet_out->po_buf;
size_t dst_buf_size = xqc_get_po_remained_size(packet_out);
xqc_pkt_type_t type = packet_out->po_pkt.pkt_type;
xqc_packet_number_t packet_number = packet_out->po_pkt.pkt_num;
unsigned int need = xqc_long_packet_header_size(dcid_len, scid_len, token_len, pktno_bits, type);
unsigned char *begin = dst_buf;
unsigned char bits;
unsigned int vlen;
if (!xqc_check_proto_version_valid(ver)) {
return -XQC_EPROTO;
}
if (need > dst_buf_size) {
return -XQC_ENOBUF;
}
if (scid_len < 3) {
return -XQC_EILLPKT;
}
unsigned char first_byte = 0xC0;
first_byte |= type << 4;
first_byte |= pktno_bits;
*dst_buf++ = first_byte;
memcpy(dst_buf, xqc_proto_version_field[ver], XQC_PROTO_VERSION_LEN);
dst_buf += XQC_PROTO_VERSION_LEN;
*dst_buf = dcid_len;
dst_buf++;
memcpy(dst_buf, dcid, dcid_len);
dst_buf += dcid_len;
*dst_buf = scid_len;
dst_buf++;
memcpy(dst_buf, scid, scid_len);
dst_buf += scid_len;
if (type == XQC_PTYPE_INIT) {
bits = xqc_vint_get_2bit(token_len);
vlen = xqc_vint_len(bits);
xqc_vint_write(dst_buf, token_len, bits, vlen);
dst_buf += vlen;
if (token_len > 0) {
memcpy(dst_buf, token, token_len);
dst_buf += token_len;
}
}
dst_buf += XQC_LONG_HEADER_LENGTH_BYTE;
packet_out->po_ppktno = dst_buf;
dst_buf += xqc_write_packet_number(dst_buf, packet_number, pktno_bits);
packet_out->po_payload = dst_buf;
return dst_buf - begin;
}
/*
+-+-+-+-+-+-+-+-+
|1|1| 0 |R R|P P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| DCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| SCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Token Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Token (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/24/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 12: Initial Packet
*/
xqc_int_t
xqc_packet_parse_initial(xqc_connection_t *c, xqc_packet_in_t *packet_in)
{
unsigned char *pos = packet_in->pos;
unsigned char *end = packet_in->last;
int size = 0;
uint64_t token_len = 0, length = 0;
xqc_log(c->log, XQC_LOG_DEBUG, "|packet parse|initial|");
packet_in->pi_pkt.pkt_type = XQC_PTYPE_INIT;
packet_in->pi_pkt.pkt_pns = XQC_PNS_INIT;
/* The smallest length of udp datagrams carrying initial packet frome client is 1200 */
if (c->conn_type == XQC_CONN_TYPE_SERVER) {
if (XQC_BUFF_LEFT_SIZE(packet_in->buf, end) < XQC_PACKET_INITIAL_MIN_LENGTH) {
xqc_log(c->log, XQC_LOG_ERROR, "|initial size too small|%z|",
(size_t)XQC_BUFF_LEFT_SIZE(packet_in->buf, end));
XQC_CONN_ERR(c, TRA_PROTOCOL_VIOLATION);
return -XQC_EILLPKT;
}
}
/* Token Length(i) & Token */
size = xqc_vint_read(pos, end, &token_len);
if (size < 0 || XQC_BUFF_LEFT_SIZE(pos, end) < size + token_len) {
xqc_log(c->log, XQC_LOG_ERROR, "|token length err|%ui|", token_len);
return -XQC_EILLPKT;
}
pos += size;
if (token_len > XQC_MAX_TOKEN_LEN) {
xqc_log(c->log, XQC_LOG_ERROR, "|token length exceed XQC_MAX_TOKEN_LEN|%ui|", token_len);
return -XQC_EILLPKT;
}
/* server save token and check token when decode crypto frame */
if (c->conn_type == XQC_CONN_TYPE_SERVER) {
memcpy(c->conn_token, pos, token_len);
c->conn_token_len = token_len;
}
pos += token_len;
/* Length(i) */
size = xqc_vint_read(pos, end, &length);
if (size < 0
|| XQC_BUFF_LEFT_SIZE(pos, end) < size + length)
{
xqc_log(c->log, XQC_LOG_ERROR, "|length err|%ui|", length);
return -XQC_EILLPKT;
}
pos += size;
packet_in->last = pos + length;
packet_in->pi_pkt.length = length;
packet_in->pi_pkt.pkt_num_offset = pos - packet_in->buf;
if (packet_in->last > end) {
xqc_log(c->log, XQC_LOG_ERROR, "|illegal pkt with wrong length");
return -XQC_EILLPKT;
}
xqc_log(c->log, XQC_LOG_DEBUG, "|success|Length:%ui|", length);
return XQC_OK;
}
/*
+-+-+-+-+-+-+-+-+
|1|1| 1 |R R|P P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| DCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| SCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/24/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0-RTT Packet
*/
xqc_int_t
xqc_packet_parse_zero_rtt(xqc_connection_t *c, xqc_packet_in_t *packet_in)
{
unsigned char *pos = packet_in->pos;
unsigned char *end = packet_in->last;
ssize_t size = 0;
uint64_t length = 0;
xqc_log(c->log, XQC_LOG_DEBUG, "|packet parse|0-RTT|");
packet_in->pi_pkt.pkt_type = XQC_PTYPE_0RTT;
packet_in->pi_pkt.pkt_pns = XQC_PNS_APP_DATA;
/* Length(i) */
size = xqc_vint_read(pos, packet_in->last, &length);
if (size < 0
|| XQC_BUFF_LEFT_SIZE(pos, end) < size + length)
{
xqc_log(c->log, XQC_LOG_ERROR, "|length err|%ui|", length);
return -XQC_EILLPKT;
}
pos += size;
packet_in->last = pos + length;
packet_in->pi_pkt.length = length;
packet_in->pi_pkt.pkt_num_offset = pos - packet_in->buf;
if (packet_in->last > end) {
xqc_log(c->log, XQC_LOG_ERROR, "|illegal pkt with wrong length");
return -XQC_EILLPKT;
}
xqc_log(c->log, XQC_LOG_DEBUG, "|success|Length:%ui|", length);
return XQC_OK;
}
xqc_int_t
xqc_packet_encrypt_buf(xqc_connection_t *conn, xqc_packet_out_t *packet_out,
unsigned char *enc_pkt, size_t enc_pkt_cap, size_t *enc_pkt_len)
{
xqc_int_t ret;
size_t enc_payload_len = 0;
xqc_encrypt_level_t level = xqc_packet_type_to_enc_level(packet_out->po_pkt.pkt_type);
/* check encrypt key ready */
if (xqc_tls_is_key_ready(conn->tls, level, XQC_KEY_TYPE_TX_WRITE) == XQC_FALSE) {
xqc_log(conn->log, XQC_LOG_ERROR, "|crypto not initialized|level:%d|", level);
return -XQC_TLS_INVALID_STATE;
}
/* source buffer */
uint8_t *header = (uint8_t *)packet_out->po_buf;
size_t header_len = packet_out->po_payload - packet_out->po_buf;
uint8_t *payload = (uint8_t *)packet_out->po_payload;
size_t payload_len = packet_out->po_used_size - header_len;
uint8_t *pktno = (uint8_t *)packet_out->po_ppktno;
/* destination buffer */
uint8_t *dst_header = enc_pkt;
uint8_t *dst_pktno = dst_header + (pktno - header);
uint8_t *dst_payload = dst_header + header_len;
uint8_t *dst_end = dst_payload;
xqc_path_ctx_t *path = xqc_conn_find_path_by_path_id(conn, packet_out->po_path_id);
if (path == NULL) {
XQC_CONN_ERR(conn, TRA_INTERNAL_ERROR);
xqc_log(conn->log, XQC_LOG_ERROR, "|path:%ui|does not exist|",
packet_out->po_path_id);
return XQC_EMP_PATH_NOT_FOUND;
}
/* copy header to dest */
xqc_memcpy(dst_header, header, header_len);
/* refresh header length */
if (level == XQC_ENC_LEV_INIT || level == XQC_ENC_LEV_0RTT
|| level == XQC_ENC_LEV_HSK)
{
unsigned char *plength = dst_pktno - XQC_LONG_HEADER_LENGTH_BYTE;
uint32_t length = header + packet_out->po_used_size - pktno;
length += xqc_tls_aead_tag_len(conn->tls, level);
xqc_vint_write(plength, length, 0x01, 2);
}
/* do packet protection */
uint32_t nonce_path_id = (conn->enable_multipath) ?
(uint32_t)path->path_id : 0;
xqc_log(conn->log, XQC_LOG_DEBUG, "|encryption nonce|path_id:%ui|pn:%ui|", nonce_path_id, packet_out->po_pkt.pkt_num);
ret = xqc_tls_encrypt_payload(conn->tls, level,
packet_out->po_pkt.pkt_num, nonce_path_id,
dst_header, header_len, payload, payload_len,
dst_payload, enc_pkt_cap - header_len, &enc_payload_len);
if (ret != XQC_OK) {
XQC_CONN_ERR(conn, TRA_CRYPTO_ERROR);
xqc_log(conn->log, XQC_LOG_ERROR, "|packet protection error|pkt_type:%d|pkt_num:%ui",
packet_out->po_pkt.pkt_type, packet_out->po_pkt.pkt_num);
return ret;
}
*enc_pkt_len = header_len + enc_payload_len;
/* do header protection */
ret = xqc_tls_encrypt_header(conn->tls, level, packet_out->po_pkt.pkt_type,
dst_header, dst_pktno, dst_end);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_ERROR, "|header protection error|pkt_type:%d|pkt_num:%ui",
packet_out->po_pkt.pkt_type, packet_out->po_pkt.pkt_num);
return ret;
}
/* update enc_pkt_cnt for current 1-rtt key & maybe initiate a key update */
if (conn->conn_settings.keyupdate_pkt_threshold > 0
&& packet_out->po_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER && level == XQC_ENC_LEV_1RTT)
{
conn->key_update_ctx.enc_pkt_cnt++;
if (conn->key_update_ctx.enc_pkt_cnt > conn->conn_settings.keyupdate_pkt_threshold
&& conn->key_update_ctx.first_sent_pktno <=
conn->conn_initial_path->path_send_ctl->ctl_largest_acked[XQC_PNS_APP_DATA]
&& xqc_monotonic_timestamp() > conn->key_update_ctx.initiate_time_guard)
{
ret = xqc_tls_update_1rtt_keys(conn->tls, XQC_KEY_TYPE_RX_READ);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_ERROR, "|xqc_tls_update_rx_keys error|");
return ret;
}
ret = xqc_tls_update_1rtt_keys(conn->tls, XQC_KEY_TYPE_TX_WRITE);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_ERROR, "|xqc_tls_update_tx_keys error|");
return ret;
}
ret = xqc_conn_confirm_key_update(conn);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_ERROR, "|xqc_conn_confirm_key_update error|");
return ret;
}
}
}
packet_out->po_enc_size = *enc_pkt_len;
return XQC_OK;
}
xqc_int_t
xqc_packet_encrypt(xqc_connection_t *conn, xqc_packet_out_t *packet_out)
{
return xqc_packet_encrypt_buf(conn, packet_out, conn->enc_pkt, conn->enc_pkt_cap,
&conn->enc_pkt_len);
}
xqc_int_t
xqc_packet_decrypt(xqc_connection_t *conn, xqc_packet_in_t *packet_in)
{
xqc_int_t ret;
xqc_encrypt_level_t level = xqc_packet_type_to_enc_level(packet_in->pi_pkt.pkt_type);
if (level == XQC_ENC_LEV_0RTT
&& xqc_tls_is_early_data_accepted(conn->tls) != XQC_TLS_EARLY_DATA_ACCEPT)
{
xqc_log(conn->log, XQC_LOG_DEBUG, "|early data not decrypt|");
return -XQC_TLS_DATA_REJECT;
}
/* destination buffer for payload decryption */
unsigned char *dst = packet_in->decode_payload;
size_t dst_cap = packet_in->decode_payload_size;
/* source buffer for header decryption */
uint8_t *header = (uint8_t *)packet_in->buf;
uint8_t *pktno = header + packet_in->pi_pkt.pkt_num_offset;
uint8_t *end = pktno + packet_in->pi_pkt.length;
/*
* remove header protection, which will modify the first byte and packet number bytes in
* original header buffer.
*/
ret = xqc_tls_decrypt_header(conn->tls, level, packet_in->pi_pkt.pkt_type,
header, pktno, end);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_INFO, "|remove header protection error|ret:%d|", ret);
return ret;
}
/* source buffer for payload decryption */
size_t pktno_len = XQC_PACKET_SHORT_HEADER_PKTNO_LEN(header);
size_t header_len = packet_in->pi_pkt.pkt_num_offset + pktno_len;
uint8_t *payload = header + header_len;
size_t payload_len = packet_in->pi_pkt.length - pktno_len;
uint8_t reserved_bits = 0;
if (packet_in->pi_pkt.pkt_type == XQC_PTYPE_0RTT) {
reserved_bits = (header[0] & 0x0c) >> 2;
} else if (packet_in->pi_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER) {
reserved_bits = (header[0] & 0x18) >> 3;
}
if (reserved_bits == 0x02 && conn->conn_settings.marking_reinjection) {
packet_in->pi_flag |= XQC_PIF_REINJECTED_REPLICA;
}
/* parse packet number from header */
xqc_packet_number_t truncated_pn;
xqc_packet_parse_packet_number(pktno, pktno_len, &truncated_pn);
/* decode packet number */
xqc_packet_number_t largest_pn = 0;
xqc_path_ctx_t *path = xqc_conn_find_path_by_path_id(conn, packet_in->pi_path_id);
if (path) {
xqc_pn_ctl_t *pn_ctl = xqc_get_pn_ctl(conn, path);
xqc_pkt_num_space_t pns = packet_in->pi_pkt.pkt_pns;
largest_pn = xqc_recv_record_largest(&pn_ctl->ctl_recv_record[pns]);
}
packet_in->pi_pkt.pkt_num =
xqc_packet_decode_packet_number(largest_pn, truncated_pn, pktno_len * 8);
xqc_log(conn->log, XQC_LOG_DEBUG, "|largest_pn:%ui|", largest_pn);
/* check key phase, determine weather to update read keys */
xqc_uint_t key_phase = XQC_PACKET_SHORT_HEADER_KEY_PHASE(header);
if (packet_in->pi_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER && level == XQC_ENC_LEV_1RTT
&& key_phase != conn->key_update_ctx.next_in_key_phase
&& packet_in->pi_pkt.pkt_num > conn->key_update_ctx.first_recv_pktno
&& xqc_tls_is_key_update_confirmed(conn->tls))
{
ret = xqc_tls_update_1rtt_keys(conn->tls, XQC_KEY_TYPE_RX_READ);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_INFO, "|xqc_tls_update_rx_keys error|");
return ret;
}
}
/* decrypt packet payload */
uint32_t nonce_path_id = (conn->enable_multipath) ?
(uint32_t)packet_in->pi_path_id : 0;
xqc_log(conn->log, XQC_LOG_DEBUG, "|decryption nonce|path_id:%ui|pn:%ui|", nonce_path_id, packet_in->pi_pkt.pkt_num);
ret = xqc_tls_decrypt_payload(conn->tls, level,
packet_in->pi_pkt.pkt_num, nonce_path_id,
header, header_len, payload, payload_len,
dst, dst_cap, &packet_in->decode_payload_len);
if (ret != XQC_OK) {
if (!xqc_tls_is_key_update_confirmed(conn->tls)) {
xqc_log(conn->log, XQC_LOG_WARN, "|xqc_tls_decrypt_payload error when keyupdate|");
return -XQC_TLS_DECRYPT_WHEN_KU_ERROR;
} else {
xqc_log(conn->log, XQC_LOG_WARN, "|xqc_tls_decrypt_payload error|");
return ret;
}
}
packet_in->pos = dst;
packet_in->last = dst + packet_in->decode_payload_len;
/* update write keys, apply key update */
if (packet_in->pi_pkt.pkt_type == XQC_PTYPE_SHORT_HEADER && level == XQC_ENC_LEV_1RTT) {
if (key_phase != conn->key_update_ctx.next_in_key_phase
&& !xqc_tls_is_key_update_confirmed(conn->tls))
{
ret = xqc_tls_update_1rtt_keys(conn->tls, XQC_KEY_TYPE_TX_WRITE);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_WARN, "|xqc_tls_update_tx_keys error|");
return ret;
}
ret = xqc_conn_confirm_key_update(conn);
if (ret != XQC_OK) {
xqc_log(conn->log, XQC_LOG_WARN, "|xqc_conn_confirm_key_update error|");
return ret;
}
} else if (key_phase == conn->key_update_ctx.next_in_key_phase
&& packet_in->pi_pkt.pkt_num < conn->key_update_ctx.first_recv_pktno)
{
conn->key_update_ctx.first_recv_pktno = packet_in->pi_pkt.pkt_num;
}
}
return XQC_OK;
}
/*
+-+-+-+-+-+-+-+-+
|1|1| 2 |R R|P P|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| DCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| SCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/24/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Figure 13: Handshake Protected Packet
*/
xqc_int_t
xqc_packet_parse_handshake(xqc_connection_t *c, xqc_packet_in_t *packet_in)
{
unsigned char *pos = packet_in->pos;
unsigned char *end = packet_in->last;
int size = 0;
uint64_t length = 0;
packet_in->pi_pkt.pkt_type = XQC_PTYPE_HSK;
packet_in->pi_pkt.pkt_pns = XQC_PNS_HSK;
/* Length(i) */
size = xqc_vint_read(pos, end, &length);
if (size < 0
|| XQC_BUFF_LEFT_SIZE(pos, end) < size + length)
{
xqc_log(c->log, XQC_LOG_ERROR, "|length err|");
return -XQC_EILLPKT;
}
pos += size;
packet_in->last = pos + length;
/* packet number */
packet_in->pi_pkt.length = length;
packet_in->pi_pkt.pkt_num_offset = pos - packet_in->buf;
if (packet_in->last > end) {
xqc_log(c->log, XQC_LOG_ERROR, "|illegal pkt with wrong length");
return -XQC_EILLPKT;
}
xqc_log(c->log, XQC_LOG_DEBUG, "|success|Length:%ui|", length);
return XQC_OK;
}
#define RETRY_PSEUDO_PACKET_SIZE (3 * (1 + XQC_MAX_CID_LEN) + 1 + 4 + XQC_MAX_TOKEN_LEN)
xqc_int_t
xqc_conn_gen_retry_integrity_tag(xqc_connection_t *conn, const uint8_t *odcid_buf, unsigned odcid_len,
uint8_t *retry_buf, unsigned retry_len, uint8_t *tag_buf, size_t *tag_len)
{
uint8_t pseudo_buf[RETRY_PSEUDO_PACKET_SIZE] = {0};
size_t pseudo_len = sizeof(uint8_t) + odcid_len + retry_len;
uint8_t *dst_buf = pseudo_buf;
if (retry_len + odcid_len > RETRY_PSEUDO_PACKET_SIZE) {
xqc_log(conn->log, XQC_LOG_ERROR,
"|xqc_tls_gen_retry_integrity_tag length invalid|retry_len:%d|odcid_len:%d|",
retry_len, odcid_len);
return XQC_ERROR;
}
*dst_buf++ = odcid_len;
memcpy(dst_buf, odcid_buf, odcid_len);
dst_buf += odcid_len;
xqc_memcpy(dst_buf, retry_buf, retry_len);
dst_buf += retry_len;
xqc_int_t ret = xqc_tls_cal_retry_integrity_tag(conn->log,
pseudo_buf, pseudo_len,
tag_buf, XQC_RETRY_INTEGRITY_TAG_LEN,
tag_len, conn->version);
if (ret != XQC_OK || *tag_len != XQC_RETRY_INTEGRITY_TAG_LEN) {
xqc_log(conn->log, XQC_LOG_ERROR,
"|xqc_tls_cal_retry_integrity_tag error|ret:%d|tag_len:%d|", ret, *tag_len);
return XQC_ERROR;
}
return XQC_OK;
}
/*
*
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|1|1| 3 | Unused|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| DCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| SCID Len (8) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0..160) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Retry Token (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ Retry Integrity Tag (128) +
| |
+ +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Retry Packet
*/
/* protocol update RFC9000 */
int
xqc_gen_retry_packet(xqc_connection_t *conn, unsigned char *dst_buf,
const unsigned char *dcid, unsigned char dcid_len,
const unsigned char *scid, unsigned char scid_len,
const unsigned char *odcid, unsigned char odcid_len,
const unsigned char *token, unsigned token_len,
unsigned ver)
{
unsigned char *begin = dst_buf;
unsigned char first_byte = 0xC0;
first_byte |= XQC_PTYPE_RETRY << 4;
*dst_buf++ = first_byte;
ver = htonl(ver);
memcpy(dst_buf, &ver, sizeof(ver));
dst_buf += sizeof(ver);
*dst_buf++ = dcid_len;
memcpy(dst_buf, dcid, dcid_len);
dst_buf += dcid_len;
*dst_buf++ = scid_len;
memcpy(dst_buf, scid, scid_len);
dst_buf += scid_len;
memcpy(dst_buf, token, token_len);
dst_buf += token_len;
uint8_t retry_integrity_tag[XQC_RETRY_INTEGRITY_TAG_LEN] = {0};
size_t retry_integrity_tag_len = XQC_RETRY_INTEGRITY_TAG_LEN;
xqc_int_t ret = xqc_conn_gen_retry_integrity_tag(conn, odcid, odcid_len, begin, dst_buf - begin,
retry_integrity_tag, &retry_integrity_tag_len);
if (ret != XQC_OK) {
return XQC_ERROR;
}
memcpy(dst_buf, retry_integrity_tag, retry_integrity_tag_len);
dst_buf += retry_integrity_tag_len;