/
tls13ech.c
2273 lines (2027 loc) · 72.9 KB
/
tls13ech.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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nss.h"
#include "pk11func.h"
#include "pk11hpke.h"
#include "ssl.h"
#include "sslproto.h"
#include "sslimpl.h"
#include "selfencrypt.h"
#include "ssl3exthandle.h"
#include "tls13ech.h"
#include "tls13exthandle.h"
#include "tls13hashstate.h"
#include "tls13hkdf.h"
extern SECStatus
ssl3_UpdateHandshakeHashesInt(sslSocket *ss, const unsigned char *b,
unsigned int l, sslBuffer *transcriptBuf);
extern SECStatus
ssl3_HandleClientHelloPreamble(sslSocket *ss, PRUint8 **b, PRUint32 *length, SECItem *sidBytes,
SECItem *cookieBytes, SECItem *suites, SECItem *comps);
extern SECStatus
tls13_DeriveSecret(sslSocket *ss, PK11SymKey *key,
const char *label,
unsigned int labelLen,
const SSL3Hashes *hashes,
PK11SymKey **dest,
SSLHashType hash);
void
tls13_DestroyEchConfig(sslEchConfig *config)
{
if (!config) {
return;
}
SECITEM_FreeItem(&config->contents.publicKey, PR_FALSE);
SECITEM_FreeItem(&config->contents.suites, PR_FALSE);
SECITEM_FreeItem(&config->raw, PR_FALSE);
PORT_Free(config->contents.publicName);
config->contents.publicName = NULL;
PORT_ZFree(config, sizeof(*config));
}
void
tls13_DestroyEchConfigs(PRCList *list)
{
PRCList *cur_p;
while (!PR_CLIST_IS_EMPTY(list)) {
cur_p = PR_LIST_TAIL(list);
PR_REMOVE_LINK(cur_p);
tls13_DestroyEchConfig((sslEchConfig *)cur_p);
}
}
void
tls13_DestroyEchXtnState(sslEchXtnState *state)
{
if (!state) {
return;
}
SECITEM_FreeItem(&state->innerCh, PR_FALSE);
SECITEM_FreeItem(&state->senderPubKey, PR_FALSE);
SECITEM_FreeItem(&state->retryConfigs, PR_FALSE);
PORT_ZFree(state, sizeof(*state));
}
SECStatus
tls13_CopyEchConfigs(PRCList *oConfigs, PRCList *configs)
{
SECStatus rv;
sslEchConfig *config;
sslEchConfig *newConfig = NULL;
for (PRCList *cur_p = PR_LIST_HEAD(oConfigs);
cur_p != oConfigs;
cur_p = PR_NEXT_LINK(cur_p)) {
config = (sslEchConfig *)PR_LIST_TAIL(oConfigs);
newConfig = PORT_ZNew(sslEchConfig);
if (!newConfig) {
goto loser;
}
rv = SECITEM_CopyItem(NULL, &newConfig->raw, &config->raw);
if (rv != SECSuccess) {
goto loser;
}
newConfig->contents.publicName = PORT_Strdup(config->contents.publicName);
if (!newConfig->contents.publicName) {
goto loser;
}
rv = SECITEM_CopyItem(NULL, &newConfig->contents.publicKey,
&config->contents.publicKey);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_CopyItem(NULL, &newConfig->contents.suites,
&config->contents.suites);
if (rv != SECSuccess) {
goto loser;
}
newConfig->contents.configId = config->contents.configId;
newConfig->contents.kemId = config->contents.kemId;
newConfig->contents.kdfId = config->contents.kdfId;
newConfig->contents.aeadId = config->contents.aeadId;
newConfig->contents.maxNameLen = config->contents.maxNameLen;
newConfig->version = config->version;
PR_APPEND_LINK(&newConfig->link, configs);
}
return SECSuccess;
loser:
tls13_DestroyEchConfig(newConfig);
tls13_DestroyEchConfigs(configs);
return SECFailure;
}
/*
* struct {
* HpkeKdfId kdf_id;
* HpkeAeadId aead_id;
* } HpkeSymmetricCipherSuite;
*
* struct {
* uint8 config_id;
* HpkeKemId kem_id;
* HpkePublicKey public_key;
* HpkeSymmetricCipherSuite cipher_suites<4..2^16-4>;
* } HpkeKeyConfig;
*
* struct {
* HpkeKeyConfig key_config;
* uint16 maximum_name_length;
* opaque public_name<1..2^16-1>;
* Extension extensions<0..2^16-1>;
* } ECHConfigContents;
*
* struct {
* uint16 version;
* uint16 length;
* select (ECHConfig.version) {
* case 0xfe0a: ECHConfigContents contents;
* }
* } ECHConfig;
*/
static SECStatus
tls13_DecodeEchConfigContents(const sslReadBuffer *rawConfig,
sslEchConfig **outConfig)
{
SECStatus rv;
sslEchConfigContents contents = { 0 };
sslEchConfig *decodedConfig;
PRUint64 tmpn;
PRUint64 tmpn2;
sslReadBuffer tmpBuf;
PRUint16 *extensionTypes = NULL;
unsigned int extensionIndex = 0;
sslReader configReader = SSL_READER(rawConfig->buf, rawConfig->len);
sslReader suiteReader;
sslReader extensionReader;
PRBool hasValidSuite = PR_FALSE;
/* HpkeKeyConfig key_config */
/* uint8 config_id */
rv = sslRead_ReadNumber(&configReader, 1, &tmpn);
if (rv != SECSuccess) {
goto loser;
}
contents.configId = tmpn;
/* HpkeKemId kem_id */
rv = sslRead_ReadNumber(&configReader, 2, &tmpn);
if (rv != SECSuccess) {
goto loser;
}
contents.kemId = tmpn;
/* HpkePublicKey public_key */
rv = sslRead_ReadVariable(&configReader, 2, &tmpBuf);
if (rv != SECSuccess) {
goto loser;
}
rv = SECITEM_MakeItem(NULL, &contents.publicKey, (PRUint8 *)tmpBuf.buf, tmpBuf.len);
if (rv != SECSuccess) {
goto loser;
}
/* HpkeSymmetricCipherSuite cipher_suites<4..2^16-4> */
rv = sslRead_ReadVariable(&configReader, 2, &tmpBuf);
if (rv != SECSuccess) {
goto loser;
}
if (tmpBuf.len & 1) {
PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_CONFIG);
goto loser;
}
suiteReader = (sslReader)SSL_READER(tmpBuf.buf, tmpBuf.len);
while (SSL_READER_REMAINING(&suiteReader)) {
/* HpkeKdfId kdf_id */
rv = sslRead_ReadNumber(&suiteReader, 2, &tmpn);
if (rv != SECSuccess) {
goto loser;
}
/* HpkeAeadId aead_id */
rv = sslRead_ReadNumber(&suiteReader, 2, &tmpn2);
if (rv != SECSuccess) {
goto loser;
}
if (!hasValidSuite) {
/* Use the first compatible ciphersuite. */
rv = PK11_HPKE_ValidateParameters(contents.kemId, tmpn, tmpn2);
if (rv == SECSuccess) {
hasValidSuite = PR_TRUE;
contents.kdfId = tmpn;
contents.aeadId = tmpn2;
break;
}
}
}
rv = SECITEM_MakeItem(NULL, &contents.suites, (PRUint8 *)tmpBuf.buf, tmpBuf.len);
if (rv != SECSuccess) {
goto loser;
}
/* uint16 maximum_name_length */
rv = sslRead_ReadNumber(&configReader, 2, &tmpn);
if (rv != SECSuccess) {
goto loser;
}
contents.maxNameLen = (PRUint16)tmpn;
/* opaque public_name<1..2^16-1> */
rv = sslRead_ReadVariable(&configReader, 2, &tmpBuf);
if (rv != SECSuccess) {
goto loser;
}
if (tmpBuf.len == 0) {
PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_CONFIG);
goto loser;
}
for (tmpn = 0; tmpn < tmpBuf.len; tmpn++) {
if (tmpBuf.buf[tmpn] == '\0') {
PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_CONFIG);
goto loser;
}
}
contents.publicName = PORT_ZAlloc(tmpBuf.len + 1);
if (!contents.publicName) {
goto loser;
}
PORT_Memcpy(contents.publicName, (PRUint8 *)tmpBuf.buf, tmpBuf.len);
/* Extensions. We don't support any, but must
* check for any that are marked critical. */
rv = sslRead_ReadVariable(&configReader, 2, &tmpBuf);
if (rv != SECSuccess) {
goto loser;
}
extensionReader = (sslReader)SSL_READER(tmpBuf.buf, tmpBuf.len);
extensionTypes = PORT_NewArray(PRUint16, tmpBuf.len / 2 * sizeof(PRUint16));
if (!extensionTypes) {
goto loser;
}
while (SSL_READER_REMAINING(&extensionReader)) {
/* Get the extension's type field */
rv = sslRead_ReadNumber(&extensionReader, 2, &tmpn);
if (rv != SECSuccess) {
goto loser;
}
for (unsigned int i = 0; i < extensionIndex; i++) {
if (extensionTypes[i] == tmpn) {
PORT_SetError(SEC_ERROR_EXTENSION_VALUE_INVALID);
goto loser;
}
}
extensionTypes[extensionIndex++] = (PRUint16)tmpn;
/* If it's mandatory, fail. */
if (tmpn & (1 << 15)) {
PORT_SetError(SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION);
goto loser;
}
/* Skip. */
rv = sslRead_ReadVariable(&extensionReader, 2, &tmpBuf);
if (rv != SECSuccess) {
goto loser;
}
}
/* Check that we consumed the entire ECHConfig */
if (SSL_READER_REMAINING(&configReader)) {
PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_CONFIG);
goto loser;
}
/* If the ciphersuites weren't compatible, don't
* set the outparam. Return success to indicate
* the config was well-formed. */
if (hasValidSuite) {
decodedConfig = PORT_ZNew(sslEchConfig);
if (!decodedConfig) {
goto loser;
}
decodedConfig->contents = contents;
*outConfig = decodedConfig;
} else {
PORT_Free(contents.publicName);
SECITEM_FreeItem(&contents.publicKey, PR_FALSE);
SECITEM_FreeItem(&contents.suites, PR_FALSE);
}
PORT_Free(extensionTypes);
return SECSuccess;
loser:
PORT_Free(extensionTypes);
PORT_Free(contents.publicName);
SECITEM_FreeItem(&contents.publicKey, PR_FALSE);
SECITEM_FreeItem(&contents.suites, PR_FALSE);
return SECFailure;
}
/* Decode an ECHConfigList struct and store each ECHConfig
* into |configs|. */
SECStatus
tls13_DecodeEchConfigs(const SECItem *data, PRCList *configs)
{
SECStatus rv;
sslEchConfig *decodedConfig = NULL;
sslReader rdr = SSL_READER(data->data, data->len);
sslReadBuffer tmp;
sslReadBuffer singleConfig;
PRUint64 version;
PRUint64 length;
PORT_Assert(PR_CLIST_IS_EMPTY(configs));
rv = sslRead_ReadVariable(&rdr, 2, &tmp);
if (rv != SECSuccess) {
return SECFailure;
}
if (SSL_READER_REMAINING(&rdr)) {
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
sslReader configsReader = SSL_READER(tmp.buf, tmp.len);
if (!SSL_READER_REMAINING(&configsReader)) {
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
/* Handle each ECHConfig. */
while (SSL_READER_REMAINING(&configsReader)) {
singleConfig.buf = SSL_READER_CURRENT(&configsReader);
/* uint16 version */
rv = sslRead_ReadNumber(&configsReader, 2, &version);
if (rv != SECSuccess) {
goto loser;
}
/* uint16 length */
rv = sslRead_ReadNumber(&configsReader, 2, &length);
if (rv != SECSuccess) {
goto loser;
}
singleConfig.len = 4 + length;
rv = sslRead_Read(&configsReader, length, &tmp);
if (rv != SECSuccess) {
goto loser;
}
if (version == TLS13_ECH_VERSION) {
rv = tls13_DecodeEchConfigContents(&tmp, &decodedConfig);
if (rv != SECSuccess) {
goto loser; /* code set */
}
if (decodedConfig) {
decodedConfig->version = version;
rv = SECITEM_MakeItem(NULL, &decodedConfig->raw, singleConfig.buf,
singleConfig.len);
if (rv != SECSuccess) {
goto loser;
}
PR_APPEND_LINK(&decodedConfig->link, configs);
decodedConfig = NULL;
}
}
}
return SECSuccess;
loser:
tls13_DestroyEchConfigs(configs);
return SECFailure;
}
/* Encode an ECHConfigList structure. We only create one config, and as the
* primary use for this function is to generate test inputs, we don't
* validate against what HPKE and libssl can actually support. */
SECStatus
SSLExp_EncodeEchConfigId(PRUint8 configId, const char *publicName, unsigned int maxNameLen,
HpkeKemId kemId, const SECKEYPublicKey *pubKey,
const HpkeSymmetricSuite *hpkeSuites, unsigned int hpkeSuiteCount,
PRUint8 *out, unsigned int *outlen, unsigned int maxlen)
{
SECStatus rv;
unsigned int savedOffset;
unsigned int len;
sslBuffer b = SSL_BUFFER_EMPTY;
PRUint8 tmpBuf[66]; // Large enough for an EC public key, currently only X25519.
unsigned int tmpLen;
if (!publicName || !hpkeSuites || hpkeSuiteCount == 0 ||
!pubKey || maxNameLen == 0 || !out || !outlen) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* ECHConfig ECHConfigList<1..2^16-1>; */
rv = sslBuffer_Skip(&b, 2, NULL);
if (rv != SECSuccess) {
goto loser;
}
/*
* struct {
* uint16 version;
* uint16 length;
* select (ECHConfig.version) {
* case 0xfe0a: ECHConfigContents contents;
* }
* } ECHConfig;
*/
rv = sslBuffer_AppendNumber(&b, TLS13_ECH_VERSION, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_Skip(&b, 2, &savedOffset);
if (rv != SECSuccess) {
goto loser;
}
/*
* struct {
* uint8 config_id;
* HpkeKemId kem_id;
* HpkePublicKey public_key;
* HpkeSymmetricCipherSuite cipher_suites<4..2^16-4>;
* } HpkeKeyConfig;
*/
rv = sslBuffer_AppendNumber(&b, configId, 1);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendNumber(&b, kemId, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = PK11_HPKE_Serialize(pubKey, tmpBuf, &tmpLen, sizeof(tmpBuf));
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendVariable(&b, tmpBuf, tmpLen, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendNumber(&b, hpkeSuiteCount * 4, 2);
if (rv != SECSuccess) {
goto loser;
}
for (unsigned int i = 0; i < hpkeSuiteCount; i++) {
rv = sslBuffer_AppendNumber(&b, hpkeSuites[i].kdfId, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendNumber(&b, hpkeSuites[i].aeadId, 2);
if (rv != SECSuccess) {
goto loser;
}
}
/*
* struct {
* HpkeKeyConfig key_config;
* uint16 maximum_name_length;
* opaque public_name<1..2^16-1>;
* Extension extensions<0..2^16-1>;
* } ECHConfigContents;
*/
rv = sslBuffer_AppendNumber(&b, maxNameLen, 2);
if (rv != SECSuccess) {
goto loser;
}
len = PORT_Strlen(publicName);
if (len > 0xffff) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
goto loser;
}
rv = sslBuffer_AppendVariable(&b, (const PRUint8 *)publicName, len, 2);
if (rv != SECSuccess) {
goto loser;
}
/* extensions */
rv = sslBuffer_AppendNumber(&b, 0, 2);
if (rv != SECSuccess) {
goto loser;
}
/* Write the length now that we know it. */
rv = sslBuffer_InsertLength(&b, 0, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_InsertLength(&b, savedOffset, 2);
if (rv != SECSuccess) {
goto loser;
}
if (SSL_BUFFER_LEN(&b) > maxlen) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
goto loser;
}
PORT_Memcpy(out, SSL_BUFFER_BASE(&b), SSL_BUFFER_LEN(&b));
*outlen = SSL_BUFFER_LEN(&b);
sslBuffer_Clear(&b);
return SECSuccess;
loser:
sslBuffer_Clear(&b);
return SECFailure;
}
SECStatus
SSLExp_GetEchRetryConfigs(PRFileDesc *fd, SECItem *retryConfigs)
{
SECStatus rv;
sslSocket *ss;
SECItem out = { siBuffer, NULL, 0 };
if (!fd || !retryConfigs) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
ss = ssl_FindSocket(fd);
if (!ss) {
SSL_DBG(("%d: SSL[%d]: bad socket in %s",
SSL_GETPID(), fd, __FUNCTION__));
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* We don't distinguish between "handshake completed
* without retry configs", and "handshake not completed".
* An application should only call this after receiving a
* RETRY_WITH_ECH error code, which implies retry_configs. */
if (!ss->xtnData.ech || !ss->xtnData.ech->retryConfigsValid) {
PORT_SetError(SSL_ERROR_HANDSHAKE_NOT_COMPLETED);
return SECFailure;
}
/* May be empty. */
rv = SECITEM_CopyItem(NULL, &out, &ss->xtnData.ech->retryConfigs);
if (rv == SECFailure) {
return SECFailure;
}
*retryConfigs = out;
return SECSuccess;
}
SECStatus
SSLExp_RemoveEchConfigs(PRFileDesc *fd)
{
sslSocket *ss;
if (!fd) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
ss = ssl_FindSocket(fd);
if (!ss) {
SSL_DBG(("%d: SSL[%d]: bad socket in %s",
SSL_GETPID(), fd, __FUNCTION__));
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
if (!PR_CLIST_IS_EMPTY(&ss->echConfigs)) {
tls13_DestroyEchConfigs(&ss->echConfigs);
}
/* Also remove any retry_configs and handshake context. */
if (ss->xtnData.ech && ss->xtnData.ech->retryConfigs.len) {
SECITEM_FreeItem(&ss->xtnData.ech->retryConfigs, PR_FALSE);
}
if (ss->ssl3.hs.echHpkeCtx) {
PK11_HPKE_DestroyContext(ss->ssl3.hs.echHpkeCtx, PR_TRUE);
ss->ssl3.hs.echHpkeCtx = NULL;
}
PORT_Free(CONST_CAST(char, ss->ssl3.hs.echPublicName));
ss->ssl3.hs.echPublicName = NULL;
return SECSuccess;
}
/* Import one or more ECHConfigs for the given keypair. The AEAD/KDF
* may differ , but only X25519 is supported for the KEM.*/
SECStatus
SSLExp_SetServerEchConfigs(PRFileDesc *fd,
const SECKEYPublicKey *pubKey, const SECKEYPrivateKey *privKey,
const PRUint8 *echConfigs, unsigned int echConfigsLen)
{
sslSocket *ss;
SECStatus rv;
SECItem data = { siBuffer, CONST_CAST(PRUint8, echConfigs), echConfigsLen };
if (!fd || !pubKey || !privKey || !echConfigs || echConfigsLen == 0) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
ss = ssl_FindSocket(fd);
if (!ss) {
SSL_DBG(("%d: SSL[%d]: bad socket in %s",
SSL_GETPID(), fd, __FUNCTION__));
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* Overwrite if we're already configured. */
rv = SSLExp_RemoveEchConfigs(fd);
if (rv != SECSuccess) {
return SECFailure;
}
rv = tls13_DecodeEchConfigs(&data, &ss->echConfigs);
if (rv != SECSuccess) {
goto loser;
}
if (PR_CLIST_IS_EMPTY(&ss->echConfigs)) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
goto loser;
}
ss->echPubKey = SECKEY_CopyPublicKey(pubKey);
if (!ss->echPubKey) {
goto loser;
}
ss->echPrivKey = SECKEY_CopyPrivateKey(privKey);
if (!ss->echPrivKey) {
goto loser;
}
return SECSuccess;
loser:
tls13_DestroyEchConfigs(&ss->echConfigs);
SECKEY_DestroyPrivateKey(ss->echPrivKey);
SECKEY_DestroyPublicKey(ss->echPubKey);
ss->echPubKey = NULL;
ss->echPrivKey = NULL;
return SECFailure;
}
/* Client enable. For now, we'll use the first
* compatible config (server preference). */
SECStatus
SSLExp_SetClientEchConfigs(PRFileDesc *fd,
const PRUint8 *echConfigs,
unsigned int echConfigsLen)
{
SECStatus rv;
sslSocket *ss;
SECItem data = { siBuffer, CONST_CAST(PRUint8, echConfigs), echConfigsLen };
if (!fd || !echConfigs || echConfigsLen == 0) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
ss = ssl_FindSocket(fd);
if (!ss) {
SSL_DBG(("%d: SSL[%d]: bad socket in %s",
SSL_GETPID(), fd, __FUNCTION__));
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
/* Overwrite if we're already configured. */
rv = SSLExp_RemoveEchConfigs(fd);
if (rv != SECSuccess) {
return SECFailure;
}
rv = tls13_DecodeEchConfigs(&data, &ss->echConfigs);
if (rv != SECSuccess) {
return SECFailure;
}
if (PR_CLIST_IS_EMPTY(&ss->echConfigs)) {
PORT_SetError(SEC_ERROR_INVALID_ARGS);
return SECFailure;
}
return SECSuccess;
}
/* Set up ECH. This generates an ephemeral sender
* keypair and the HPKE context */
SECStatus
tls13_ClientSetupEch(sslSocket *ss, sslClientHelloType type)
{
SECStatus rv;
HpkeContext *cx = NULL;
SECKEYPublicKey *pkR = NULL;
SECItem hpkeInfo = { siBuffer, NULL, 0 };
sslEchConfig *cfg = NULL;
if (PR_CLIST_IS_EMPTY(&ss->echConfigs) ||
!ssl_ShouldSendSNIExtension(ss, ss->url) ||
IS_DTLS(ss)) {
return SECSuccess;
}
/* Maybe apply our own priority if >1. For now, we only support
* one version and one KEM. Each ECHConfig can specify multiple
* KDF/AEADs, so just use the first. */
cfg = (sslEchConfig *)PR_LIST_HEAD(&ss->echConfigs);
/* Skip ECH if the public name matches the private name. */
if (0 == PORT_Strcmp(cfg->contents.publicName, ss->url)) {
return SECSuccess;
}
SSL_TRC(50, ("%d: TLS13[%d]: Setup client ECH",
SSL_GETPID(), ss->fd));
switch (type) {
case client_hello_initial:
PORT_Assert(!ss->ssl3.hs.echHpkeCtx && !ss->ssl3.hs.echPublicName);
cx = PK11_HPKE_NewContext(cfg->contents.kemId, cfg->contents.kdfId,
cfg->contents.aeadId, NULL, NULL);
break;
case client_hello_retry:
if (!ss->ssl3.hs.echHpkeCtx || !ss->ssl3.hs.echPublicName) {
FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error);
return SECFailure;
}
/* Nothing else to do. */
return SECSuccess;
default:
PORT_Assert(0);
goto loser;
}
if (!cx) {
goto loser;
}
rv = PK11_HPKE_Deserialize(cx, cfg->contents.publicKey.data, cfg->contents.publicKey.len, &pkR);
if (rv != SECSuccess) {
goto loser;
}
if (!SECITEM_AllocItem(NULL, &hpkeInfo, strlen(kHpkeInfoEch) + 1 + cfg->raw.len)) {
goto loser;
}
PORT_Memcpy(&hpkeInfo.data[0], kHpkeInfoEch, strlen(kHpkeInfoEch));
PORT_Memset(&hpkeInfo.data[strlen(kHpkeInfoEch)], 0, 1);
PORT_Memcpy(&hpkeInfo.data[strlen(kHpkeInfoEch) + 1], cfg->raw.data, cfg->raw.len);
PRINT_BUF(50, (ss, "Info", hpkeInfo.data, hpkeInfo.len));
/* Setup with an ephemeral sender keypair. */
rv = PK11_HPKE_SetupS(cx, NULL, NULL, pkR, &hpkeInfo);
if (rv != SECSuccess) {
goto loser;
}
rv = ssl3_GetNewRandom(ss->ssl3.hs.client_inner_random);
if (rv != SECSuccess) {
goto loser; /* code set */
}
/* If ECH is rejected, the application will use SSLChannelInfo
* to fetch this field and perform cert chain verification. */
ss->ssl3.hs.echPublicName = PORT_Strdup(cfg->contents.publicName);
if (!ss->ssl3.hs.echPublicName) {
goto loser;
}
ss->ssl3.hs.echHpkeCtx = cx;
SECKEY_DestroyPublicKey(pkR);
SECITEM_FreeItem(&hpkeInfo, PR_FALSE);
return SECSuccess;
loser:
PK11_HPKE_DestroyContext(cx, PR_TRUE);
SECKEY_DestroyPublicKey(pkR);
SECITEM_FreeItem(&hpkeInfo, PR_FALSE);
PORT_Assert(PORT_GetError() != 0);
return SECFailure;
}
/*
* enum {
* encrypted_client_hello(0xfe0a), (65535)
* } ExtensionType;
*
* struct {
* HpkeKdfId kdf_id;
* HpkeAeadId aead_id;
* } HpkeSymmetricCipherSuite;
* struct {
* HpkeSymmetricCipherSuite cipher_suite;
* uint8 config_id;
* opaque enc<1..2^16-1>;
* opaque payload<1..2^16-1>;
* } ClientECH;
*
* Takes as input the constructed ClientHelloInner and
* returns a constructed encrypted_client_hello extension
* (replacing the contents of |chInner|).
*/
static SECStatus
tls13_EncryptClientHello(sslSocket *ss, sslBuffer *outerAAD, sslBuffer *chInner)
{
SECStatus rv;
SECItem chPt = { siBuffer, chInner->buf, chInner->len };
SECItem *chCt = NULL;
SECItem aadItem = { siBuffer, outerAAD ? outerAAD->buf : NULL, outerAAD ? outerAAD->len : 0 };
const SECItem *hpkeEnc = NULL;
const sslEchConfig *cfg = (sslEchConfig *)PR_LIST_HEAD(&ss->echConfigs);
PORT_Assert(!PR_CLIST_IS_EMPTY(&ss->echConfigs));
SSL_TRC(50, ("%d: TLS13[%d]: Encrypting Client Hello Inner",
SSL_GETPID(), ss->fd));
PRINT_BUF(50, (ss, "aad", outerAAD->buf, outerAAD->len));
PRINT_BUF(50, (ss, "inner", chInner->buf, chInner->len));
hpkeEnc = PK11_HPKE_GetEncapPubKey(ss->ssl3.hs.echHpkeCtx);
if (!hpkeEnc) {
FATAL_ERROR(ss, SEC_ERROR_LIBRARY_FAILURE, internal_error);
goto loser;
}
#ifndef UNSAFE_FUZZER_MODE
rv = PK11_HPKE_Seal(ss->ssl3.hs.echHpkeCtx, &aadItem, &chPt, &chCt);
if (rv != SECSuccess) {
goto loser;
}
PRINT_BUF(50, (ss, "cipher", chCt->data, chCt->len));
#else
/* Fake a tag. */
SECITEM_AllocItem(NULL, chCt, chPt.len + 16);
if (!chCt) {
goto loser;
}
PORT_Memcpy(chCt->data, chPt.data, chPt.len);
#endif
/* Format the encrypted_client_hello extension. */
sslBuffer_Clear(chInner);
rv = sslBuffer_AppendNumber(chInner, cfg->contents.kdfId, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendNumber(chInner, cfg->contents.aeadId, 2);
if (rv != SECSuccess) {
goto loser;
}
rv = sslBuffer_AppendNumber(chInner, cfg->contents.configId, 1);
if (rv != SECSuccess) {
goto loser;
}
if (!ss->ssl3.hs.helloRetry) {
rv = sslBuffer_AppendVariable(chInner, hpkeEnc->data, hpkeEnc->len, 2);
if (rv != SECSuccess) {
goto loser;
}
} else {
/* |enc| is empty. */
rv = sslBuffer_AppendNumber(chInner, 0, 2);
if (rv != SECSuccess) {
goto loser;
}
}
rv = sslBuffer_AppendVariable(chInner, chCt->data, chCt->len, 2);
if (rv != SECSuccess) {
goto loser;
}
SECITEM_FreeItem(chCt, PR_TRUE);
return SECSuccess;
loser:
SECITEM_FreeItem(chCt, PR_TRUE);
return SECFailure;
}
SECStatus
tls13_GetMatchingEchConfigs(const sslSocket *ss, HpkeKdfId kdf, HpkeAeadId aead,
const PRUint8 configId, const sslEchConfig *cur, sslEchConfig **next)
{
SSL_TRC(50, ("%d: TLS13[%d]: GetMatchingEchConfig %d",
SSL_GETPID(), ss->fd, configId));
/* If |cur|, resume the search at that node, else the list head. */
for (PRCList *cur_p = cur ? ((PRCList *)cur)->next : PR_LIST_HEAD(&ss->echConfigs);
cur_p != &ss->echConfigs;
cur_p = PR_NEXT_LINK(cur_p)) {
sslEchConfig *echConfig = (sslEchConfig *)cur_p;
if (echConfig->contents.configId == configId &&
echConfig->contents.aeadId == aead &&
echConfig->contents.kdfId == kdf) {
*next = echConfig;
return SECSuccess;
}
}
*next = NULL;
return SECSuccess;
}
/* Given a CH with extensions, copy from the start up to the extensions
* into |writer| and return the extensions themselves in |extensions|.
* If |explicitSid|, place this value into |writer| as the SID. Else,
* the sid is copied from |reader| to |writer|. */
static SECStatus
tls13_CopyChPreamble(sslReader *reader, const SECItem *explicitSid, sslBuffer *writer, sslReadBuffer *extensions)
{
SECStatus rv;
sslReadBuffer tmpReadBuf;
/* Locate the extensions. */
rv = sslRead_Read(reader, 2 + SSL3_RANDOM_LENGTH, &tmpReadBuf);
if (rv != SECSuccess) {
return SECFailure;
}
rv = sslBuffer_Append(writer, tmpReadBuf.buf, tmpReadBuf.len);
if (rv != SECSuccess) {
return SECFailure;
}
/* legacy_session_id */
rv = sslRead_ReadVariable(reader, 1, &tmpReadBuf);
if (explicitSid) {
/* Encoded SID should be empty when copying from CHOuter. */
if (tmpReadBuf.len > 0) {
PORT_SetError(SSL_ERROR_RX_MALFORMED_ECH_EXTENSION);
return SECFailure;
}
rv = sslBuffer_AppendVariable(writer, explicitSid->data, explicitSid->len, 1);
} else {
rv = sslBuffer_AppendVariable(writer, tmpReadBuf.buf, tmpReadBuf.len, 1);
}
if (rv != SECSuccess) {
return SECFailure;
}
/* cipher suites */
rv = sslRead_ReadVariable(reader, 2, &tmpReadBuf);
if (rv != SECSuccess) {
return SECFailure;
}
rv = sslBuffer_AppendVariable(writer, tmpReadBuf.buf, tmpReadBuf.len, 2);
if (rv != SECSuccess) {
return SECFailure;
}
/* compression */
rv = sslRead_ReadVariable(reader, 1, &tmpReadBuf);
if (rv != SECSuccess) {
return SECFailure;
}
rv = sslBuffer_AppendVariable(writer, tmpReadBuf.buf, tmpReadBuf.len, 1);
if (rv != SECSuccess) {
return SECFailure;
}
/* extensions */
rv = sslRead_ReadVariable(reader, 2, extensions);
if (rv != SECSuccess) {
return SECFailure;