forked from wolfSSL/wolfssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquic.c
1369 lines (1186 loc) · 40 KB
/
quic.c
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
/* quic.c
*
* Copyright (C) 2006-2023 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Name change compatibility layer no longer needs to be included here */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#ifndef WOLFCRYPT_ONLY
#ifdef WOLFSSL_QUIC
#include <wolfssl/error-ssl.h>
#include <wolfssl/ssl.h>
#include <wolfssl/internal.h>
#include <wolfssl/openssl/buffer.h>
#include <wolfssl/openssl/ecdsa.h>
#include <wolfssl/openssl/evp.h>
#include <wolfssl/openssl/kdf.h>
static int qr_length(const uint8_t *data, size_t len)
{
word32 rlen;
if (len < 4) {
return 0;
}
c24to32(&data[1], &rlen);
return (int)rlen + 4;
}
static void quic_record_free(WOLFSSL *ssl, QuicRecord *r)
{
(void)ssl;
if (r->data) {
ForceZero(r->data, r->capacity);
XFREE(r->data, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
XFREE(r, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
static QuicRecord *quic_record_make(WOLFSSL *ssl,
WOLFSSL_ENCRYPTION_LEVEL level,
const uint8_t *data, size_t len)
{
QuicRecord *qr;
qr = (QuicRecord*)XMALLOC(sizeof(*qr), ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (qr) {
memset(qr, 0, sizeof(*qr));
qr->level = level;
if (level == wolfssl_encryption_early_data) {
qr->capacity = qr->len = (word32)len;
}
else {
qr->capacity = qr->len = qr_length(data, len);
}
if (qr->capacity == 0) {
qr->capacity = 2*1024;
}
qr->data = (uint8_t*)XMALLOC(qr->capacity, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (!qr->data) {
quic_record_free(ssl, qr);
return NULL;
}
}
return qr;
}
static int quic_record_complete(QuicRecord *r)
{
return r->len && r->end >= r->len;
}
static int quic_record_done(QuicRecord *r)
{
return r->len && r->end >= r->len && r->start >= r->end;
}
static int quic_record_append(WOLFSSL *ssl, QuicRecord *qr, const uint8_t *data,
size_t len, size_t *pconsumed)
{
size_t missing, consumed = 0;
int ret = WOLFSSL_SUCCESS;
(void)ssl;
if (!qr->len && len) {
missing = 4 - qr->end;
if (len < missing) {
XMEMCPY(qr->data + qr->end, data, len);
qr->end += len;
consumed = len;
goto cleanup; /* len consumed, but qr->len still unknown */
}
XMEMCPY(qr->data + qr->end, data, missing);
qr->end += missing;
len -= missing;
data += missing;
consumed = missing;
qr->len = qr_length(qr->data, qr->end);
if (qr->len > qr->capacity) {
uint8_t *ndata = (uint8_t*)XREALLOC(qr->data, qr->len, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (!ndata) {
ret = WOLFSSL_FAILURE;
goto cleanup;
}
qr->data = ndata;
qr->capacity = qr->len;
}
}
if (quic_record_complete(qr) || len == 0) {
return 0;
}
missing = qr->len - qr->end;
if (len > missing) {
len = missing;
}
XMEMCPY(qr->data + qr->end, data, len);
qr->end += len;
consumed += len;
cleanup:
*pconsumed = (ret == WOLFSSL_SUCCESS) ? consumed : 0;
return ret;
}
static word32 add_rec_header(byte* output, word32 length, int type)
{
RecordLayerHeader* rl;
/* record layer header */
rl = (RecordLayerHeader*)output;
if (rl == NULL) {
return 0;
}
rl->type = type;
rl->pvMajor = SSLv3_MAJOR;
rl->pvMinor = TLSv1_2_MINOR;
c16toa((word16)length, rl->length);
return RECORD_HEADER_SZ;
}
static word32 quic_record_transfer(QuicRecord* qr, byte* buf, word32 sz)
{
word32 len = qr->end - qr->start;
word32 offset = 0;
word16 rlen;
if (len <= 0) {
return 0;
}
if (qr->rec_hdr_remain == 0) {
/* start a new TLS record */
rlen = (qr->len <= (word32)MAX_RECORD_SIZE) ?
qr->len : (word32)MAX_RECORD_SIZE;
offset += add_rec_header(buf, rlen,
(qr->level == wolfssl_encryption_early_data) ?
application_data : handshake);
qr->rec_hdr_remain = rlen;
sz -= offset;
}
if (len > qr->rec_hdr_remain) {
len = qr->rec_hdr_remain;
}
if (len > sz) {
len = sz;
}
if (len > 0) {
XMEMCPY(buf + offset, qr->data + qr->start, len);
qr->start += len;
qr->rec_hdr_remain -= len;
}
return len + offset;
}
const QuicTransportParam* QuicTransportParam_new(const uint8_t* data,
size_t len, void* heap)
{
QuicTransportParam* tp;
if (len > 65353) return NULL;
tp = (QuicTransportParam*)XMALLOC(sizeof(*tp), heap, DYNAMIC_TYPE_TLSX);
if (!tp) return NULL;
tp->data = (uint8_t*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
if (!tp->data) {
XFREE(tp, heap, DYNAMIC_TYPE_TLSX);
return NULL;
}
XMEMCPY((uint8_t*)tp->data, data, len);
tp->len = len;
return tp;
}
const QuicTransportParam* QuicTransportParam_dup(const QuicTransportParam* tp,
void* heap)
{
QuicTransportParam* tp2;
tp2 = (QuicTransportParam*)XMALLOC(sizeof(*tp2), heap, DYNAMIC_TYPE_TLSX);
if (!tp2) return NULL;
tp2->data = (uint8_t*)XMALLOC(tp->len, heap, DYNAMIC_TYPE_TLSX);
if (!tp2->data) {
XFREE(tp2, heap, DYNAMIC_TYPE_TLSX);
return NULL;
}
XMEMCPY((uint8_t*)tp2->data, tp->data, tp->len);
tp2->len = tp->len;
return tp2;
}
void QuicTransportParam_free(const QuicTransportParam* tp, void* heap)
{
(void)heap;
if (tp) {
if (tp->data) XFREE((uint8_t*)tp->data, heap, DYNAMIC_TYPE_TLSX);
XFREE((void*)tp, heap, DYNAMIC_TYPE_TLSX);
}
}
void wolfSSL_quic_clear(WOLFSSL* ssl)
{
QuicEncData* qd;
/* keep
* - ssl->quic.transport_local
* - ssl->quic.method
* - ssl->quic.transport_version
* reset/free everything else
*/
if (ssl->quic.transport_peer) {
QTP_FREE(ssl->quic.transport_peer, ssl->heap);
ssl->quic.transport_peer = NULL;
}
if (ssl->quic.transport_peer_draft) {
QTP_FREE(ssl->quic.transport_peer_draft, ssl->heap);
ssl->quic.transport_peer_draft = NULL;
}
ssl->quic.enc_level_write = wolfssl_encryption_initial;
ssl->quic.enc_level_latest_recvd = wolfssl_encryption_initial;
while ((qd = ssl->quic.input_head)) {
ssl->quic.input_head = qd->next;
quic_record_free(ssl, qd);
}
ssl->quic.input_tail = NULL;
ssl->quic.output_rec_remain = 0;
if (ssl->quic.scratch) {
quic_record_free(ssl, ssl->quic.scratch);
ssl->quic.scratch = NULL;
}
}
void wolfSSL_quic_free(WOLFSSL* ssl)
{
wolfSSL_quic_clear(ssl);
if (ssl->quic.transport_local) {
QTP_FREE(ssl->quic.transport_local, ssl->heap);
ssl->quic.transport_local = NULL;
}
ssl->quic.method = NULL;
}
static int ctx_check_quic_compat(const WOLFSSL_CTX* ctx)
{
WOLFSSL_ENTER("ctx_check_quic_compat");
if (ctx->method->version.major != SSLv3_MAJOR
|| ctx->method->version.minor != TLSv1_3_MINOR
|| (ctx->method->downgrade && ctx->minDowngrade < TLSv1_3_MINOR)) {
WOLFSSL_MSG_EX("ctx not quic compatible: vmajor=%d, vminor=%d, downgrade=%d",
ctx->method->version.major,
ctx->method->version.minor,
ctx->method->downgrade
);
return WOLFSSL_FAILURE;
}
return WOLFSSL_SUCCESS;
}
static int check_method_sanity(const WOLFSSL_QUIC_METHOD* m)
{
WOLFSSL_ENTER("check_method_sanity");
if (m && m->set_encryption_secrets
&& m->add_handshake_data
&& m->flush_flight
&& m->send_alert) {
return WOLFSSL_SUCCESS;
}
return WOLFSSL_FAILURE;
}
int wolfSSL_CTX_set_quic_method(WOLFSSL_CTX* ctx,
const WOLFSSL_QUIC_METHOD* quic_method)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_quic_method");
if (ctx_check_quic_compat(ctx) != WOLFSSL_SUCCESS
|| check_method_sanity(quic_method) != WOLFSSL_SUCCESS) {
return WOLFSSL_FAILURE;
}
ctx->quic.method = quic_method;
return WOLFSSL_SUCCESS;
}
int wolfSSL_set_quic_method(WOLFSSL* ssl,
const WOLFSSL_QUIC_METHOD* quic_method)
{
WOLFSSL_ENTER("wolfSSL_set_quic_method");
if (ctx_check_quic_compat(ssl->ctx) != WOLFSSL_SUCCESS
|| check_method_sanity(quic_method) != WOLFSSL_SUCCESS) {
return WOLFSSL_FAILURE;
}
ssl->quic.method = quic_method;
return WOLFSSL_SUCCESS;
}
int wolfSSL_is_quic(WOLFSSL* ssl)
{
return WOLFSSL_IS_QUIC(ssl);
}
WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_read_level(const WOLFSSL* ssl)
{
return ssl->quic.enc_level_read;
}
WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_write_level(const WOLFSSL* ssl)
{
return ssl->quic.enc_level_write;
}
int wolfSSL_set_quic_transport_params(WOLFSSL* ssl,
const uint8_t* params,
size_t params_len)
{
const QuicTransportParam* tp;
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_set_quic_transport_params");
if (!params || params_len == 0) {
tp = NULL;
}
else {
tp = QuicTransportParam_new(params, params_len, ssl->heap);
if (!tp) {
ret = WOLFSSL_FAILURE;
goto cleanup;
}
}
if (ssl->quic.transport_local)
QTP_FREE(ssl->quic.transport_local, ssl->heap);
ssl->quic.transport_local = tp;
cleanup:
WOLFSSL_LEAVE("wolfSSL_set_quic_transport_params", ret);
return ret;
}
void wolfSSL_get_peer_quic_transport_params(const WOLFSSL* ssl,
const uint8_t** out_params,
size_t* out_params_len)
{
const QuicTransportParam* tp = ssl->quic.transport_peer ?
ssl->quic.transport_peer : ssl->quic.transport_peer_draft;
*out_params = tp ? tp->data : NULL;
*out_params_len = tp ? tp->len : 0;
}
int wolfSSL_get_peer_quic_transport_version(const WOLFSSL* ssl)
{
return ssl->quic.transport_peer ?
TLSX_KEY_QUIC_TP_PARAMS : (ssl->quic.transport_peer_draft ?
TLSX_KEY_QUIC_TP_PARAMS : -1);
}
void wolfSSL_set_quic_use_legacy_codepoint(WOLFSSL* ssl, int use_legacy)
{
ssl->quic.transport_version = use_legacy ? TLSX_KEY_QUIC_TP_PARAMS_DRAFT
: TLSX_KEY_QUIC_TP_PARAMS;
}
void wolfSSL_set_quic_transport_version(WOLFSSL* ssl, int version)
{
if (version == TLSX_KEY_QUIC_TP_PARAMS
|| version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT
|| !version) {
ssl->quic.transport_version = version;
}
else {
WOLFSSL_MSG("wolfSSL_set_quic_transport_version: invalid version");
}
}
int wolfSSL_get_quic_transport_version(const WOLFSSL* ssl)
{
return ssl->quic.transport_version;
}
int wolfSSL_quic_add_transport_extensions(WOLFSSL* ssl, int msg_type)
{
/* RFC 9001, ch. 8.2: "The quic_transport_parameters extension is carried
* in the ClientHello and the EncryptedExtensions messages during the
* handshake. Endpoints MUST send the quic_transport_parameters extension;"
* Which means, at least one. There can be more to signal compatibility to
* older/newer versions.
*/
int ret = 0, is_resp = (msg_type == encrypted_extensions);
if (ssl->quic.transport_local == NULL) {
return QUIC_TP_MISSING_E;
}
if (is_resp) {
/* server response: time to decide which version to use */
if (ssl->quic.transport_peer && ssl->quic.transport_peer_draft) {
if (ssl->quic.transport_version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) {
ret = TLSX_QuicTP_Use(ssl,
TLSX_KEY_QUIC_TP_PARAMS_DRAFT, is_resp);
QTP_FREE(ssl->quic.transport_peer, ssl->heap);
ssl->quic.transport_peer = NULL;
}
else {
ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp);
QTP_FREE(ssl->quic.transport_peer_draft,
ssl->heap);
ssl->quic.transport_peer_draft = NULL;
}
}
else {
if (ssl->quic.transport_version == TLSX_KEY_QUIC_TP_PARAMS_DRAFT
&& ssl->quic.transport_peer_draft) {
ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT,
is_resp);
}
else if (ssl->quic.transport_peer) {
ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp);
}
else {
/* no match, send none, will let the client fail */
}
}
}
else {
/* client hello */
if (ssl->quic.transport_version == 0) {
/* not being set to a particular id, we send both draft+v1 */
ret = TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS, is_resp)
|| TLSX_QuicTP_Use(ssl, TLSX_KEY_QUIC_TP_PARAMS_DRAFT, is_resp);
}
else {
/* otherwise, send the version configured */
ret = TLSX_QuicTP_Use(ssl, (TLSX_Type)ssl->quic.transport_version,
is_resp);
}
}
return ret;
}
#define QUIC_HS_FLIGHT_LIMIT_DEFAULT (16* 1024)
size_t wolfSSL_quic_max_handshake_flight_len(const WOLFSSL* ssl,
WOLFSSL_ENCRYPTION_LEVEL level)
{
switch (level) {
case wolfssl_encryption_initial:
case wolfssl_encryption_application:
return QUIC_HS_FLIGHT_LIMIT_DEFAULT;
case wolfssl_encryption_early_data:
return 0; /* QUIC does not send at this level */
case wolfssl_encryption_handshake:
/* during handshake itself, certificates may be exchanged which
* exceed our default limit, advise a higher limit one.
*/
if (ssl->options.side == WOLFSSL_SERVER_END) {
if (ssl->options.verifyPeer
&& MAX_CERTIFICATE_SZ > QUIC_HS_FLIGHT_LIMIT_DEFAULT)
return MAX_CERTIFICATE_SZ;
}
else {
/* clients may receive the server cert chain
*/
if (2*MAX_CERTIFICATE_SZ > QUIC_HS_FLIGHT_LIMIT_DEFAULT)
return 2*MAX_CERTIFICATE_SZ;
}
return QUIC_HS_FLIGHT_LIMIT_DEFAULT;
}
return 0;
}
#ifdef WOLFSSL_EARLY_DATA
void wolfSSL_set_quic_early_data_enabled(WOLFSSL* ssl, int enabled)
{
/* This only has effect on server and when the handshake has
* not started yet.
* This function is part of the quictls/openssl API and does
* not return any error, sadly. So we just ignore any
* unsuccessful use. But we can produce some warnings.
*/
if (!WOLFSSL_IS_QUIC(ssl)) {
WOLFSSL_MSG("wolfSSL_set_quic_early_data_enabled: not a QUIC SSL");
}
else if (ssl->options.handShakeState != NULL_STATE) {
WOLFSSL_MSG("wolfSSL_set_quic_early_data_enabled: handshake started");
}
else {
wolfSSL_set_max_early_data(ssl, enabled ? UINT32_MAX : 0);
}
}
#endif /* WOLFSSL_EARLY_DATA */
int wolfSSL_quic_do_handshake(WOLFSSL* ssl)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_quic_do_handshake");
if (!wolfSSL_is_quic(ssl)) {
WOLFSSL_MSG("WOLFSSL_QUIC_DO_HANDSHAKE not a QUIC SSL");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
while (ssl->options.handShakeState != HANDSHAKE_DONE) {
/* Peculiar: do_handshake() is successful, but the state
* indicates that we are not DONE. This seems to happen
* when resuming sessions and an EARLY_DATA indicator
* is presented by the client.
* Theory: wolfSSL expects the APP to read the early data
* and silently continues the handshake when the EndOfEarlyData
* and the client Finished arrives.
* This confuses the QUIC state handling.
*/
#ifdef WOLFSSL_EARLY_DATA
if (ssl->options.maxEarlyDataSz) {
byte tmpbuffer[256];
int len;
if (ssl->options.side == WOLFSSL_CLIENT_END) {
if (ssl->options.resuming) {
ret = wolfSSL_write_early_data(ssl, tmpbuffer, 0, &len);
}
}
else {
ret = wolfSSL_read_early_data(ssl, tmpbuffer,
sizeof(tmpbuffer), &len);
if (ret < 0 && ssl->error == ZERO_RETURN) {
/* this is expected, since QUIC handles the actual early
* data separately. */
ret = WOLFSSL_SUCCESS;
}
}
if (ret < 0) {
goto cleanup;
}
}
#endif /* WOLFSSL_EARLY_DATA */
ret = wolfSSL_SSL_do_handshake_internal(ssl);
if (ret <= 0)
goto cleanup;
}
cleanup:
if (ret <= 0
&& ssl->options.handShakeState == HANDSHAKE_DONE
&& (ssl->error == ZERO_RETURN || ssl->error == WANT_READ)) {
ret = WOLFSSL_SUCCESS;
}
if (ret == WOLFSSL_SUCCESS) {
ssl->error = WOLFSSL_ERROR_NONE;
}
WOLFSSL_LEAVE("wolfSSL_quic_do_handshake", ret);
return ret;
}
int wolfSSL_quic_read_write(WOLFSSL* ssl)
{
int ret = WOLFSSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_quic_read_write");
if (!wolfSSL_is_quic(ssl)) {
WOLFSSL_MSG("WOLFSSL_QUIC_READ_WRITE not a QUIC SSL");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
ret = wolfSSL_quic_do_handshake(ssl);
if (ret != WOLFSSL_SUCCESS)
goto cleanup;
}
ret = wolfSSL_process_quic_post_handshake(ssl);
cleanup:
WOLFSSL_LEAVE("wolfSSL_quic_read_write", ret);
return ret;
}
int wolfSSL_process_quic_post_handshake(WOLFSSL* ssl)
{
int ret = WOLFSSL_SUCCESS, nret;
WOLFSSL_ENTER("wolfSSL_process_quic_post_handshake");
if (!wolfSSL_is_quic(ssl)) {
WOLFSSL_MSG("WOLFSSL_QUIC_POST_HS not a QUIC SSL");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
WOLFSSL_MSG("WOLFSSL_QUIC_POST_HS handshake is not done yet");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
while (ssl->quic.input_head != NULL
|| ssl->buffers.inputBuffer.length > 0) {
if ((nret = ProcessReply(ssl)) < 0) {
ret = nret;
break;
}
}
while (ssl->buffers.outputBuffer.length > 0) {
SendBuffered(ssl);
}
cleanup:
WOLFSSL_LEAVE("wolfSSL_process_quic_post_handshake", ret);
return ret;
}
int wolfSSL_provide_quic_data(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
const uint8_t* data, size_t len)
{
int ret = WOLFSSL_SUCCESS;
size_t l;
WOLFSSL_ENTER("wolfSSL_provide_quic_data");
if (!wolfSSL_is_quic(ssl)) {
WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA not a QUIC SSL");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
if (level < wolfSSL_quic_read_level(ssl)
|| (ssl->quic.input_tail && level < ssl->quic.input_tail->level)
|| level < ssl->quic.enc_level_latest_recvd) {
WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA wrong encryption level");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
while (len > 0) {
if (ssl->quic.scratch) {
if (ssl->quic.scratch->level != level) {
WOLFSSL_MSG("WOLFSSL_QUIC_PROVIDE_DATA wrong encryption level");
ret = WOLFSSL_FAILURE;
goto cleanup;
}
ret = quic_record_append(ssl, ssl->quic.scratch, data, len, &l);
if (ret != WOLFSSL_SUCCESS) {
goto cleanup;
}
data += l;
len -= l;
if (quic_record_complete(ssl->quic.scratch)) {
if (ssl->quic.input_tail) {
ssl->quic.input_tail->next = ssl->quic.scratch;
ssl->quic.input_tail = ssl->quic.scratch;
}
else {
ssl->quic.input_head = ssl->quic.input_tail =
ssl->quic.scratch;
}
ssl->quic.scratch = NULL;
}
}
else {
/* start of next record with all bytes for the header */
ssl->quic.scratch = quic_record_make(ssl, level, data, len);
if (!ssl->quic.scratch) {
ret = WOLFSSL_FAILURE;
goto cleanup;
}
}
}
ssl->quic.enc_level_latest_recvd = level;
cleanup:
WOLFSSL_LEAVE("wolfSSL_provide_quic_data", ret);
return ret;
}
/* Called internally when SSL wants a certain amount of input. */
int wolfSSL_quic_receive(WOLFSSL* ssl, byte* buf, word32 sz)
{
word32 n = 0;
int transferred = 0;
WOLFSSL_ENTER("wolfSSL_quic_receive");
while (sz > 0) {
n = 0;
if (ssl->quic.input_head) {
n = quic_record_transfer(ssl->quic.input_head, buf, sz);
if (quic_record_done(ssl->quic.input_head)) {
QuicRecord* qr = ssl->quic.input_head;
ssl->quic.input_head = qr->next;
if (!qr->next) {
ssl->quic.input_tail = NULL;
}
quic_record_free(ssl, qr);
}
}
if (n == 0) {
if (transferred > 0) {
goto cleanup;
}
ssl->error = transferred = WANT_READ;
goto cleanup;
}
sz -= n;
buf += n;
transferred += n;
}
cleanup:
WOLFSSL_LEAVE("wolfSSL_quic_receive", transferred);
return transferred;
}
/**
* We need to forward the HANDSHAKE messages to the QUIC protocol stack
* via ssl->quic.method->add_handshake_data().
* The messages in the output buffer are unencrypted TLS records. We need
* to forward the content of those records.
*/
static int wolfSSL_quic_send_internal(WOLFSSL* ssl)
{
int ret = 0, aret;
size_t len;
RecordLayerHeader* rl;
word16 rlen;
word32 idx, length;
byte* output;
WOLFSSL_ENTER("wolfSSL_quic_send");
idx = ssl->buffers.outputBuffer.idx;
length = ssl->buffers.outputBuffer.length;
output = ssl->buffers.outputBuffer.buffer + idx;
while (length > 0) {
if (ssl->quic.output_rec_remain > 0) {
len = ssl->quic.output_rec_remain;
if (len > length) {
len = length;
}
aret = ssl->quic.method->add_handshake_data(ssl,
ssl->quic.output_rec_level, (const uint8_t*)output, len);
if (aret != 1) {
/* The application has an error. General disaster. */
WOLFSSL_MSG("WOLFSSL_QUIC_SEND application failed");
ret = FWRITE_ERROR;
goto cleanup;
}
output += len;
length -= len;
ssl->quic.output_rec_remain -= len;
}
else {
/* at start of a TLS Record */
rl = (RecordLayerHeader*)output;
ato16(rl->length, &rlen);
output += RECORD_HEADER_SZ;
length -= RECORD_HEADER_SZ;
ssl->quic.output_rec_remain = rlen;
ssl->quic.output_rec_level = ssl->quic.enc_level_write;
if (rl->type == application_data) {
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
ssl->quic.output_rec_level = wolfssl_encryption_early_data;
}
else {
WOLFSSL_MSG("WOLFSSL_QUIC_SEND app data after handshake");
ret = FWRITE_ERROR;
goto cleanup;
}
}
}
}
ssl->buffers.outputBuffer.idx = 0;
ssl->buffers.outputBuffer.length = 0;
cleanup:
WOLFSSL_LEAVE("wolfSSL_quic_send", ret);
return ret;
}
int wolfSSL_quic_send(WOLFSSL* ssl)
{
return wolfSSL_quic_send_internal(ssl);
}
int wolfSSL_quic_forward_secrets(WOLFSSL* ssl, int ktype, int side)
{
const uint8_t* rx_secret = NULL, *tx_secret = NULL;
WOLFSSL_ENCRYPTION_LEVEL level;
int ret = 0;
WOLFSSL_ENTER("wolfSSL_quic_forward_secrets");
switch (ktype) {
case early_data_key:
level = wolfssl_encryption_early_data;
break;
case handshake_key:
level = wolfssl_encryption_handshake;
break;
case traffic_key:
FALL_THROUGH;
case update_traffic_key:
level = wolfssl_encryption_application;
break;
case no_key:
FALL_THROUGH;
default:
/* ignore */
goto cleanup;
}
if (side == ENCRYPT_AND_DECRYPT_SIDE || side == ENCRYPT_SIDE_ONLY) {
tx_secret = (ssl->options.side == WOLFSSL_CLIENT_END) ?
ssl->clientSecret : ssl->serverSecret;
}
if (side == ENCRYPT_AND_DECRYPT_SIDE || side == DECRYPT_SIDE_ONLY) {
rx_secret = (ssl->options.side == WOLFSSL_CLIENT_END) ?
ssl->serverSecret : ssl->clientSecret;
}
if (!tx_secret && !rx_secret) {
WOLFSSL_MSG("WOLFSSL_QUIC_FORWARD_SECRETS neither "
"enc- nor decrypt specified");
goto cleanup;
}
ret = !ssl->quic.method->set_encryption_secrets(
ssl, level, rx_secret, tx_secret, ssl->specs.hash_size);
/* Having installed the secrets, any future read/write will happen
* at the level. Except early data, which is detected on the record
* type and the handshake state. */
if (ktype == early_data_key) {
goto cleanup;
}
if (tx_secret && ssl->quic.enc_level_write != level) {
ssl->quic.enc_level_write_next = level;
}
if (rx_secret && ssl->quic.enc_level_read != level) {
ssl->quic.enc_level_read_next = level;
}
cleanup:
WOLFSSL_LEAVE("wolfSSL_quic_forward_secrets", ret);
return ret;
}
int wolfSSL_quic_keys_active(WOLFSSL* ssl, enum encrypt_side side)
{
int ret = 0;
WOLFSSL_ENTER("wolfSSL_quic_keys_active");
/* Keys derived from recent secrets have been activated */
if (side == ENCRYPT_AND_DECRYPT_SIDE || side == ENCRYPT_SIDE_ONLY) {
/* If there is data in the output buffers, it was supposed to be
* encrypted at the previous level. We need to remember that when
* forwarding this data to the QUIC protocol application. */
if (ssl->buffers.outputBuffer.length > 0) {
ret = wolfSSL_quic_send_internal(ssl);
if (ret)
goto cleanup;
}
ssl->quic.enc_level_write = ssl->quic.enc_level_write_next;
}
if (side == ENCRYPT_AND_DECRYPT_SIDE || side == DECRYPT_SIDE_ONLY) {
ssl->quic.enc_level_read = ssl->quic.enc_level_read_next;
}
cleanup:
WOLFSSL_LEAVE("wolfSSL_quic_keys_active", ret);
return ret;
}
const WOLFSSL_EVP_CIPHER* wolfSSL_quic_get_aead(WOLFSSL* ssl)
{
WOLFSSL_CIPHER* cipher = NULL;
const WOLFSSL_EVP_CIPHER* evp_cipher = NULL;
if (ssl == NULL) {
return NULL;
}
cipher = wolfSSL_get_current_cipher(ssl);
if (cipher == NULL) {
return NULL;
}
switch (cipher->cipherSuite) {
#if !defined(NO_AES) && defined(HAVE_AESGCM)
case TLS_AES_128_GCM_SHA256:
evp_cipher = wolfSSL_EVP_aes_128_gcm();
break;
case TLS_AES_256_GCM_SHA384:
evp_cipher = wolfSSL_EVP_aes_256_gcm();
break;
#endif
#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
case TLS_CHACHA20_POLY1305_SHA256:
evp_cipher = wolfSSL_EVP_chacha20_poly1305();
break;
#endif
#if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
case TLS_AES_128_CCM_SHA256:
FALL_THROUGH;
case TLS_AES_128_CCM_8_SHA256:
evp_cipher = wolfSSL_EVP_aes_128_ctr();
break;
#endif
default:
evp_cipher = NULL;
break;
}
if (!evp_cipher) {
/* should not happen, as SSL* should not have negotiated it? */
WOLFSSL_MSG("wolfSSL_quic_get_aead: current cipher not supported");
return NULL;
}
return evp_cipher;
}