-
Notifications
You must be signed in to change notification settings - Fork 1
/
sslHandshakeHello.c
1812 lines (1519 loc) · 57.6 KB
/
sslHandshakeHello.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
/*
* Copyright (c) 1999-2001,2005-2008,2010-2012 Apple Inc. All Rights Reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/*
* sslHandshakeHello.c - Support for client hello and server hello messages.
*/
#include "tls_handshake_priv.h"
#include "sslHandshake.h"
#include "sslHandshake_priv.h"
#include "sslMemory.h"
#include "sslSession.h"
#include "sslUtils.h"
#include "sslDebug.h"
#include "sslCrypto.h"
#include "sslDigests.h"
#include "sslCipherSpecs.h"
#include <string.h>
#include <time.h>
#include <assert.h>
#include <stddef.h>
#include <inttypes.h>
int
SSLEncodeServerHelloRequest(tls_buffer *helloDone, tls_handshake_t ctx)
{ int err;
int head;
assert(ctx->negProtocolVersion >= tls_protocol_version_SSL_3);
head = SSLHandshakeHeaderSize(ctx);
if ((err = SSLAllocBuffer(helloDone, head)))
return err;
SSLEncodeHandshakeHeader(ctx, helloDone, SSL_HdskHelloRequest, 0); /* Message has 0 length */
return errSSLSuccess;
}
/*
* Given a protocol version sent by peer, determine if we accept that version
* and downgrade if appropriate (which can not be done for the client side).
*/
static
int sslVerifyProtVersion(
tls_handshake_t ctx,
tls_protocol_version peerVersion, // sent by peer
tls_protocol_version *negVersion) // final negotiated version if return success
{
if ((ctx->isDTLS)
? peerVersion > ctx->minProtocolVersion
: peerVersion < ctx->minProtocolVersion) {
return errSSLNegotiation;
}
if ((ctx->isDTLS)
? peerVersion < ctx->maxProtocolVersion
: peerVersion > ctx->maxProtocolVersion) {
if (!ctx->isServer) {
return errSSLNegotiation;
}
*negVersion = ctx->maxProtocolVersion;
} else {
*negVersion = peerVersion;
}
return errSSLSuccess;
}
/* IE treats null session id as valid; two consecutive sessions with NULL ID
* are considered a match. Workaround: when resumable sessions are disabled,
* send a random session ID. */
#define SSL_IE_NULL_RESUME_BUG 1
#if SSL_IE_NULL_RESUME_BUG
#define SSL_NULL_ID_LEN 32 /* length of bogus session ID */
#endif
int
SSLEncodeServerHello(tls_buffer *serverHello, tls_handshake_t ctx)
{ int err;
UInt8 *charPtr;
int sessionIDLen;
size_t msglen;
int head;
size_t extnsLen = 0;
size_t sctLen = 0;
sessionIDLen = 0;
if (ctx->sessionID.data != 0)
sessionIDLen = (UInt8)ctx->sessionID.length;
#if SSL_IE_NULL_RESUME_BUG
if(sessionIDLen == 0) {
sessionIDLen = SSL_NULL_ID_LEN;
}
#endif /* SSL_IE_NULL_RESUME_BUG */
msglen = 38 + sessionIDLen;
/* We send a extension if:
- NPN was enabled by the client app,
- The client sent an NPN extension,
- We didnt send the extension in a previous handshake.
The list of protocols sent is optional */
if (ctx->npn_enabled && ctx->npn_announced && !ctx->npn_confirmed) {
extnsLen +=
2 + 2 + /* ext hdr + ext len */
ctx->npnOwnData.length;
ctx->npn_confirmed = true;
}
/* We send a extension if:
- A selected protocol was provided
- The client sent an ALPN extension,
The list of protocols sent is optional */
if (ctx->alpnOwnData.length && ctx->alpn_announced) {
extnsLen +=
2 + 2 + 2 + /* ext hdr + ext len + list len */
ctx->alpnOwnData.length;
ctx->alpn_confirmed = true;
}
/* We send this (empty) extension if:
- ocsp stapling was enabled
- We received an ocsp stapling extension from the client */
if (ctx->ocsp_peer_enabled && ctx->ocsp_enabled) {
extnsLen += 2 + 2;
}
/* We send the SCT extension if:
- the peer sent it
- we have an SCT list set
- We are not resuming */
if (ctx->sct_peer_enabled && ctx->sct_list && !ctx->sessionMatch) {
sctLen = SSLEncodedBufferListSize(ctx->sct_list, 2);
extnsLen += 2 + 2 + 2 /* ext hdr + ext len + list len */
+ sctLen;
}
/* We send this empty SNI extension if:
- the client sent it */
if (ctx->peerDomainName.data != NULL) {
extnsLen += 2 + 2; /* ext hdr + ext len */
}
if (ctx->secure_renegotiation) {
extnsLen += 2 + 2 + 1 /* ext hdr + ext len + data len */
+ ctx->ownVerifyData.length+ctx->peerVerifyData.length;
}
/* If we received a extended master secret in client hello */
if (ctx->extMSEnabled && ctx->extMSReceived) {
extnsLen += 2 + 2; /* ext hdr + ext len */
}
if (extnsLen) {
msglen +=
2 /* ServerHello extns length */
+ extnsLen;
}
/* this was set to a known quantity in SSLProcessClientHello */
check(ctx->negProtocolVersion != tls_protocol_version_Undertermined);
sslLogNegotiateDebug("===SSL3 server: sending version %d_%d",
ctx->negProtocolVersion >> 8, ctx->negProtocolVersion & 0xff);
sslLogNegotiateDebug("...sessionIDLen = %d", sessionIDLen);
head = SSLHandshakeHeaderSize(ctx);
if ((err = SSLAllocBuffer(serverHello, msglen + head)))
return err;
charPtr = SSLEncodeHandshakeHeader(ctx, serverHello, SSL_HdskServerHello, msglen);
charPtr = SSLEncodeInt(charPtr, ctx->negProtocolVersion, 2);
#if SSL_PAC_SERVER_ENABLE
/* serverRandom might have already been set, in SSLAdvanceHandshake() */
if(!ctx->serverRandomValid) {
if ((err = SSLEncodeRandom(ctx->serverRandom, ctx)) != 0) {
return err;
}
}
#else
/* This is the normal production code path */
if ((err = SSLEncodeRandom(ctx->serverRandom, ctx)) != 0)
return err;
#endif /* SSL_PAC_SERVER_ENABLE */
memcpy(charPtr, ctx->serverRandom, SSL_CLIENT_SRVR_RAND_SIZE);
charPtr += SSL_CLIENT_SRVR_RAND_SIZE;
*(charPtr++) = (UInt8)sessionIDLen;
#if SSL_IE_NULL_RESUME_BUG
if(ctx->sessionID.data != NULL) {
/* normal path for enabled resumable session */
memcpy(charPtr, ctx->sessionID.data, sessionIDLen);
}
else {
/* IE workaround */
tls_buffer rb;
rb.data = charPtr;
rb.length = SSL_NULL_ID_LEN;
sslRand(&rb);
}
#else
if (sessionIDLen > 0)
memcpy(charPtr, ctx->sessionID.data, sessionIDLen);
#endif /* SSL_IE_NULL_RESUME_BUG */
charPtr += sessionIDLen;
charPtr = SSLEncodeInt(charPtr, ctx->selectedCipher, 2);
*(charPtr++) = 0; /* Null compression */
sslLogNegotiateDebug("TLS server specifying cipherSuite 0x%04x",
(unsigned long)ctx->selectedCipher);
/* Encoding extensions */
if (extnsLen) {
charPtr = SSLEncodeInt(charPtr, extnsLen, 2);
if (ctx->peerDomainName.data != NULL) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_ServerName, 2);
charPtr = SSLEncodeInt(charPtr, 0, 2);
}
/* We are confirming we support NPN, and sending a list of protocols */
if (ctx->npn_confirmed) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_NPN, 2);
charPtr = SSLEncodeInt(charPtr, ctx->npnOwnData.length, 2);
memcpy(charPtr, ctx->npnOwnData.data, ctx->npnOwnData.length);
charPtr += ctx->npnOwnData.length;
}
if(ctx->alpn_confirmed) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_ALPN, 2);
charPtr = SSLEncodeInt(charPtr, ctx->alpnOwnData.length + 2, 2);
charPtr = SSLEncodeInt(charPtr, ctx->alpnOwnData.length, 2);
memcpy(charPtr, ctx->alpnOwnData.data, ctx->alpnOwnData.length);
charPtr += ctx->alpnOwnData.length;
}
if(ctx->ocsp_enabled && ctx->ocsp_peer_enabled) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_StatusReguest, 2);
charPtr = SSLEncodeInt(charPtr, 0, 2);
}
if(sctLen) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_SCT, 2);
charPtr = SSLEncodeInt(charPtr, sctLen + 2, 2);
charPtr = SSLEncodeInt(charPtr, sctLen, 2);
charPtr = SSLEncodeBufferList(ctx->sct_list, 2, charPtr);
}
/* RFC 5746: Secure Renegotiation */
if(ctx->secure_renegotiation) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_SecureRenegotation, 2);
charPtr = SSLEncodeInt(charPtr, ctx->ownVerifyData.length+ctx->peerVerifyData.length+1, 2);
charPtr = SSLEncodeInt(charPtr, ctx->ownVerifyData.length+ctx->peerVerifyData.length, 1);
memcpy(charPtr, ctx->peerVerifyData.data, ctx->peerVerifyData.length);
charPtr += ctx->peerVerifyData.length;
memcpy(charPtr, ctx->ownVerifyData.data, ctx->ownVerifyData.length);
charPtr += ctx->ownVerifyData.length;
}
if (ctx->extMSEnabled && ctx->extMSReceived) {
charPtr = SSLEncodeInt(charPtr, SSL_HE_ExtendedMasterSecret, 2);
charPtr = SSLEncodeInt(charPtr, 0, 2);
}
}
// This will catch inconsistency between encoded and actual length.
if(charPtr != serverHello->data + serverHello->length) {
assert(0);
return errSSLInternal;
} else {
return errSSLSuccess;
}
}
int
SSLEncodeServerHelloVerifyRequest(tls_buffer *helloVerifyRequest, tls_handshake_t ctx)
{ int err;
UInt8 *charPtr;
size_t msglen;
int head;
assert(ctx->isServer);
assert(ctx->isDTLS);
assert(ctx->dtlsCookie.length);
msglen = 3 + ctx->dtlsCookie.length;
head = SSLHandshakeHeaderSize(ctx);
if ((err = SSLAllocBuffer(helloVerifyRequest, msglen + head)))
return err;
charPtr = SSLEncodeHandshakeHeader(ctx, helloVerifyRequest, SSL_HdskHelloVerifyRequest, msglen);
charPtr = SSLEncodeInt(charPtr, ctx->negProtocolVersion, 2);
*charPtr++ = ctx->dtlsCookie.length;
memcpy(charPtr, ctx->dtlsCookie.data, ctx->dtlsCookie.length);
charPtr += ctx->dtlsCookie.length;
if(charPtr != (helloVerifyRequest->data + helloVerifyRequest->length)) {
assert(0);
return errSSLInternal;
} else {
return errSSLSuccess;
}
}
int
SSLProcessServerHelloVerifyRequest(tls_buffer message, tls_handshake_t ctx)
{ int err;
tls_protocol_version protocolVersion;
unsigned int cookieLen;
UInt8 *p;
assert(!ctx->isServer);
/* TODO: those length values should not be hardcoded */
/* 3 bytes at least with empty cookie */
if (message.length < 3 ) {
sslErrorLog("SSLProcessServerHelloVerifyRequest: msg len error\n");
return errSSLProtocol;
}
p = message.data;
protocolVersion = (tls_protocol_version)SSLDecodeInt(p, 2);
p += 2;
/* TODO: Not clear what else to do with protocol version here */
if(protocolVersion != tls_protocol_version_DTLS_1_0) {
sslErrorLog("SSLProcessServerHelloVerifyRequest: protocol version error\n");
return errSSLProtocol;
}
cookieLen = *p++;
sslLogNegotiateDebug("cookieLen = %d, msglen=%d\n", (int)cookieLen, (int)message.length);
/* TODO: hardcoded '15' again */
if (message.length < (3 + cookieLen)) {
sslErrorLog("SSLProcessServerHelloVerifyRequest: msg len error 2\n");
return errSSLProtocol;
}
err = SSLAllocBuffer(&ctx->dtlsCookie, cookieLen);
if (err == 0)
memcpy(ctx->dtlsCookie.data, p, cookieLen);
return err;
}
static void
SSLProcessServerHelloExtension_SecureRenegotiation(tls_handshake_t ctx, UInt16 extLen, UInt8 *p)
{
if(extLen!= (1 + ctx->ownVerifyData.length + ctx->peerVerifyData.length))
return;
if(*p!=ctx->ownVerifyData.length + ctx->peerVerifyData.length)
return;
p++;
if(memcmp(p, ctx->ownVerifyData.data, ctx->ownVerifyData.length))
return;
p+=ctx->ownVerifyData.length;
if(memcmp(p, ctx->peerVerifyData.data, ctx->peerVerifyData.length))
return;
ctx->secure_renegotiation_received = true;
}
static int
SSLProcessServerHelloExtension_NPN(tls_handshake_t ctx, UInt16 extLen, UInt8 *p)
{
if (!ctx->npn_announced)
return errSSLProtocol;
ctx->npn_received = true;
/* We already have an NPN extension */
if(ctx->npnPeerData.data)
return errSSLProtocol;
return SSLCopyBufferFromData(p, extLen, &ctx->npnPeerData);
}
static int
SSLProcessServerHelloExtension_ALPN(tls_handshake_t ctx, UInt16 extLen, UInt8 *p)
{
size_t plen;
if (!ctx->alpn_announced)
return errSSLProtocol;
/* Need at least 4 bytes */
if (extLen<=3)
return errSSLProtocol;
ctx->alpn_received = true;
/* We already have an ALPN extension */
if(ctx->alpnPeerData.data)
return errSSLProtocol;
plen = SSLDecodeSize(p, 2); p+=2;
if (plen!=extLen-2)
return errSSLProtocol;
return SSLCopyBufferFromData(p, plen, &ctx->alpnPeerData);
}
static int
SSLProcessServerHelloExtension_StatusRequest(tls_handshake_t ctx, UInt16 extLen, UInt8 *p)
{
if (!ctx->ocsp_enabled)
return errSSLProtocol;
if(extLen!=0)
return errSSLProtocol;
ctx->ocsp_peer_enabled = true;
return 0;
}
static int
SSLProcessServerHelloExtension_SCT(tls_handshake_t ctx, uint16_t extLen, uint8_t *p)
{
size_t listLen;
if (!ctx->sct_enabled)
return errSSLProtocol;
if(extLen<2) {
sslErrorLog("SSLProcessClientHelloExtension_SCT: length decode error 1\n");
return errSSLProtocol;
}
listLen = SSLDecodeSize(p, 2); p+=2; extLen-=2;
if(extLen!=listLen) {
sslErrorLog("SSLProcessClientHelloExtension_SCT: length decode error 2\n");
return errSSLProtocol;
}
return SSLDecodeBufferList(p, listLen, 2, &ctx->sct_list);
}
static int
SSLProcessServerHelloExtension_SessionTicket(tls_handshake_t ctx, UInt16 extLen, UInt8 *p)
{
if (!ctx->sessionTicket_announced)
return errSSLProtocol;
if(extLen!=0)
return errSSLProtocol;
ctx->sessionTicket_confirmed = true;
return 0;
}
static int
SSLProcessServerHelloExtensions(tls_handshake_t ctx, UInt16 extensionsLen, UInt8 *p)
{
Boolean got_secure_renegotiation = false;
UInt16 remaining;
OSStatus err;
if(extensionsLen<2) {
sslErrorLog("SSLProcessHelloExtensions: need a least 2 bytes\n");
return errSSLProtocol;
}
/* Reset state of Server Hello extensions */
ctx->ocsp_peer_enabled = false;
ctx->sct_peer_enabled = false;
ctx->secure_renegotiation_received = false;
tls_free_buffer_list(ctx->sct_list);
ctx->extMSReceived = false;
remaining = SSLDecodeInt(p, 2); p+=2;
extensionsLen -=2;
/* remaining = number of bytes remaining to process according to buffer data */
/* extensionsLen = number of bytes in the buffer */
if(remaining>extensionsLen) {
sslErrorLog("SSLProcessHelloExtensions: ext len error 1\n");
return errSSLProtocol;
}
if(remaining<extensionsLen) {
sslErrorLog("Warning: SSLProcessServerHelloExtensions: Too many bytes\n");
}
while(remaining) {
UInt16 extType;
UInt16 extLen;
if (remaining<4) {
sslErrorLog("SSLProcessHelloExtensions: ext len error\n");
return errSSLProtocol;
}
extType = SSLDecodeInt(p, 2); p+=2;
extLen = SSLDecodeInt(p, 2); p+=2;
if (remaining<(4+extLen)) {
sslErrorLog("SSLProcessHelloExtension: ext len error 2\n");
return errSSLProtocol;
}
remaining -= (4+extLen);
switch (extType) {
case SSL_HE_SecureRenegotation:
if(got_secure_renegotiation)
return errSSLProtocol; /* Fail if we already processed one */
got_secure_renegotiation = true;
SSLProcessServerHelloExtension_SecureRenegotiation(ctx, extLen, p);
break;
case SSL_HE_NPN:
if ((err = SSLProcessServerHelloExtension_NPN(ctx, extLen, p)) != 0)
return err;
break;
case SSL_HE_ALPN:
if ((err = SSLProcessServerHelloExtension_ALPN(ctx, extLen, p)) != 0)
return err;
break;
case SSL_HE_SCT:
if ((err = SSLProcessServerHelloExtension_SCT(ctx, extLen, p)) != 0)
return err;
break;
case SSL_HE_StatusReguest:
if ((err = SSLProcessServerHelloExtension_StatusRequest(ctx, extLen, p)) != 0)
return err;
break;
case SSL_HE_SessionTicket:
if ((err = SSLProcessServerHelloExtension_SessionTicket(ctx, extLen, p)) != 0)
return err;
break;
case SSL_HE_ExtendedMasterSecret:
ctx->extMSReceived = true;
break;
default:
/*
Do nothing for other extensions. Per RFC 5246, we should (MUST) error
if we received extensions we didnt specify in the Client Hello.
Client should also abort handshake if multiple extensions of the same
type are found
*/
break;
}
p+=extLen;
}
return errSSLSuccess;
}
int
SSLProcessServerHello(tls_buffer message, tls_handshake_t ctx)
{ int err;
tls_protocol_version protocolVersion, negVersion;
size_t sessionIDLen;
size_t extensionsLen;
UInt8 *p;
assert(!ctx->isServer);
if (message.length < 38) {
sslErrorLog("SSLProcessServerHello: msg len error\n");
return errSSLProtocol;
}
p = message.data;
protocolVersion = (tls_protocol_version)SSLDecodeInt(p, 2);
p += 2;
/* FIXME this should probably send appropriate alerts */
err = sslVerifyProtVersion(ctx, protocolVersion, &negVersion);
if(err) {
return err;
}
ctx->negProtocolVersion = negVersion;
switch(negVersion) {
case tls_protocol_version_SSL_3:
ctx->sslTslCalls = &Ssl3Callouts;
break;
case tls_protocol_version_TLS_1_0:
case tls_protocol_version_TLS_1_1:
case tls_protocol_version_DTLS_1_0:
ctx->sslTslCalls = &Tls1Callouts;
break;
case tls_protocol_version_TLS_1_2:
ctx->sslTslCalls = &Tls12Callouts;
break;
default:
return errSSLNegotiation;
}
err = ctx->callbacks->set_protocol_version(ctx->callback_ctx, negVersion);
if(err) {
return err;
}
sslLogNegotiateDebug("SSLProcessServerHello: negVersion is %d_%d",
(negVersion >> 8) & 0xff, negVersion & 0xff);
memcpy(ctx->serverRandom, p, 32);
p += 32;
sessionIDLen = *p++;
if (message.length < (38 + sessionIDLen)) {
sslErrorLog("SSLProcessServerHello: msg len error 2\n");
return errSSLProtocol;
}
SSLFreeBuffer(&ctx->sessionID);
if (sessionIDLen > 0 && ctx->peerID.data != 0)
{ /* Don't die on error; just treat it as an uncached session */
err = SSLAllocBuffer(&ctx->sessionID, sessionIDLen);
if (err == 0)
memcpy(ctx->sessionID.data, p, sessionIDLen);
}
p += sessionIDLen;
ctx->selectedCipher = (UInt16)SSLDecodeInt(p,2);
sslLogNegotiateDebug("SSLProcessServerHello: server selected ciphersuite %04x",
(unsigned)ctx->selectedCipher);
p += 2;
if ((err = ValidateSelectedCiphersuite(ctx)) != 0) {
return err;
}
if (*p++ != 0) /* Compression */
return errSSLUnimplemented;
/* Process ServerHello extensions */
extensionsLen = message.length - (38 + sessionIDLen);
if(extensionsLen) {
err = SSLProcessServerHelloExtensions(ctx, extensionsLen, p);
if(err)
return err;
}
/* RFC 5746: Make sure the renegotiation is secure */
if(ctx->secure_renegotiation && !ctx->secure_renegotiation_received)
return errSSLNegotiation;
if(ctx->secure_renegotiation_received)
ctx->secure_renegotiation = true;
/*
* Note: the server MAY send a SSL_HE_EC_PointFormats extension if
* we've negotiated an ECDSA ciphersuite...but
* a) the provided format list MUST contain SSL_PointFormatUncompressed per
* RFC 4492 5.2; and
* b) The uncompressed format is the only one we support.
*
* Thus we drop a possible incoming SSL_HE_EC_PointFormats extension here.
* IF we ever support other point formats, we have to parse the extension
* to see what the server supports.
*/
return errSSLSuccess;
}
int
SSLEncodeClientHello(tls_buffer *clientHello, tls_handshake_t ctx)
{
size_t length;
unsigned i;
int err;
unsigned char *p;
tls_buffer sessionIdentifier = { 0, NULL };
tls_buffer sessionTicket = { 0, NULL };
size_t sessionIDLen;
size_t sessionTicketLen = 0;
size_t serverNameLen = 0;
size_t pointFormatLen = 0;
size_t suppCurveLen = 0;
size_t signatureAlgorithmsLen = 0;
size_t npnLen = 0; /* NPN support for SPDY */
size_t alpnLen = 0; /* ALPN support for SPDY, HTTP 2.0, ... */
size_t ocspLen = 0; /* OCSP Stapling extension */
size_t sctLen = 0; /* SCT extension */
size_t extMasterSecretLen = 0;
size_t paddingLen = 0; /* Padding extension */
size_t totalExtenLen = 0;
UInt16 numCipherSuites = 0;
int head;
assert(!ctx->isServer);
clientHello->length = 0;
clientHello->data = NULL;
sessionIDLen = 0;
head = SSLHandshakeHeaderSize(ctx);
if (ctx->externalSessionTicket.length)
{
sessionTicket = ctx->externalSessionTicket;
}
else if ((ctx->resumableSession.data != 0) && (SSLClientValidateSessionDataBefore(ctx->resumableSession, ctx) == 0))
{
/* We should always have a sessionID, even when using tickets. */
err = SSLRetrieveSessionID(ctx->resumableSession, &sessionIdentifier);
if(err != 0) {
goto err_exit; // error means something really wrong.
}
err = SSLRetrieveSessionTicket(ctx->resumableSession, &sessionTicket);
if (err != 0) {
goto err_exit; // error means something really wrong.
}
/* We resume if this is not a ticket, or if tickets are enabled
and if other session parameters are valid for the current context */
if ((sessionTicket.length == 0) || (ctx->sessionTicket_enabled))
{
sessionIDLen = sessionIdentifier.length;
SSLCopyBuffer(&sessionIdentifier, &ctx->proposedSessionID);
}
}
/* Count valid ciphersuites */
for(i=0;i<ctx->numEnabledCipherSuites; i++) {
if(tls_handshake_ciphersuite_is_valid(ctx, ctx->enabledCipherSuites[i])) {
numCipherSuites++;
}
}
/* RFC 5746 : add the fake ciphersuite unless we are including the extension */
if(!ctx->secure_renegotiation)
numCipherSuites+=1;
/* https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv-00 */
if(ctx->fallback)
numCipherSuites+=1;
length = 39 + 2*numCipherSuites + sessionIDLen;
/* We always use the max enabled version in the ClientHello.client_version,
even in the renegotiation case. This value is saved in the context so it
can be used in the RSA key exchange */
err = sslGetMaxProtVersion(ctx, &ctx->clientReqProtocol);
if(err) {
/* we don't have a protocol enabled */
goto err_exit;
}
/* RFC 5746: If are starting a new handshake, so we didnt received this yet */
ctx->secure_renegotiation_received = false;
/* This is the protocol version used at the record layer, If we already
negotiated the protocol version previously, we should just use that,
otherwise we use the the minimum supported version.
We do not always use the minimum version because some TLS only servers
will reject an SSL 3 version in client_hello.
*/
tls_protocol_version pv;
if(ctx->negProtocolVersion != tls_protocol_version_Undertermined) {
pv = ctx->negProtocolVersion;
} else {
if(ctx->minProtocolVersion<tls_protocol_version_TLS_1_0 && ctx->maxProtocolVersion>=tls_protocol_version_TLS_1_0)
pv = tls_protocol_version_TLS_1_0;
else
pv = ctx->minProtocolVersion;
}
//Initialize the record layer protocol version
//FIXME: error handling.
ctx->callbacks->set_protocol_version(ctx->callback_ctx, pv);
#if ENABLE_DTLS
if(ctx->isDTLS) {
/* extra space for cookie */
/* TODO: cookie len - 0 for now */
length += 1 + ctx->dtlsCookie.length;
sslLogNegotiateDebug("DTLS Hello: len=%lu\n", length);
}
/* Because of the way the version number for DTLS is encoded,
the following code mean that you can use extensions with DTLS... */
#endif /* ENABLE_DTLS */
/* RFC 5746: We add the extension only for renegotiation ClientHello */
if(ctx->secure_renegotiation) {
totalExtenLen += 2 + /* extension type */
2 + /* extension length */
1 + /* lenght of renegotiated_conection (client verify data) */
ctx->ownVerifyData.length;
}
/* prepare for optional ClientHello extensions */
if((ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0) &&
(ctx->peerDomainName.data != NULL) &&
(ctx->peerDomainName.length != 0)) {
serverNameLen = 2 + /* extension type */
2 + /* 2-byte vector length, extension_data */
2 + /* length of server_name_list */
1 + /* length of name_type */
2 + /* length of HostName */
ctx->peerDomainName.length;
totalExtenLen += serverNameLen;
}
if(ctx->sessionTicket_enabled || ctx->externalSessionTicket.length) {
sessionTicketLen = 2 + /* extension type */
2 + /* 2-byte vector length, extension_data */
sessionTicket.length;
totalExtenLen += sessionTicketLen;
ctx->sessionTicket_announced = true;
}
if((ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0) &&
(ctx->ecdsaEnable)) {
/* Two more extensions: point format, supported curves */
pointFormatLen = 2 + /* extension type */
2 + /* 2-byte vector length, extension_data */
1 + /* length of the ec_point_format_list */
1; /* the single format we support */
suppCurveLen = 2 + /* extension type */
2 + /* 2-byte vector length, extension_data */
2 + /* length of the elliptic_curve_list */
(2 * ctx->ecdhNumCurves); /* each curve is 2 bytes */
totalExtenLen += (pointFormatLen + suppCurveLen);
}
if(ctx->isDTLS
? ctx->clientReqProtocol < tls_protocol_version_DTLS_1_0
: ctx->clientReqProtocol >= tls_protocol_version_TLS_1_2) {
signatureAlgorithmsLen = 2 + /* extension type */
2 + /* 2-byte vector length, extension_data */
2 + /* length of signatureAlgorithms list */
2 * ctx->numLocalSigAlgs;
totalExtenLen += signatureAlgorithmsLen;
}
/* Only send NPN extension if:
- Requested protocol is appropriate
- Client app enabled it.
- We didnt send it in a previous handshake
*/
if (ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0 && ctx->npn_enabled && !ctx->npn_announced) {
npnLen = 2 + /* extension type */
2 + /* extension length */
0; /* empty extension data */
totalExtenLen += npnLen;
}
/* Only send ALPN extension if:
- Requested protocol is appropriate
- Client app set a list of protocols.
- We didnt send it in a previous handshake
*/
if (ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0 && ctx->alpnOwnData.length) {
alpnLen = 2 + /* extension type */
2 + /* extension length */
2 + /* protocol list len */
ctx->alpnOwnData.length; /* protocol list data */
totalExtenLen += alpnLen;
}
/* Send the status request extension (OCSP stapling) RFC 6066 if enabled */
if (ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0 && ctx->ocsp_enabled) {
ocspLen = 2 + /* extension type */
2 + /* extension lenght */
1 + /* status_type */
2 + SSLEncodedBufferListSize(ctx->ocsp_responder_id_list, 2) + /* responder ID list len */
2 + ctx->ocsp_request_extensions.length; /* extensions len */
totalExtenLen += ocspLen;
}
/* Send the SCT extension (RFC 6962) if enabled */
if (ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0 && ctx->sct_enabled) {
sctLen = 2 + /* extension type */
2 + /* extension lenght */
0; /* empty extension data */
totalExtenLen += sctLen;
}
/* Extended Master Secret */
if (ctx->clientReqProtocol >= tls_protocol_version_TLS_1_0 && ctx->extMSEnabled) {
extMasterSecretLen = 2 +
2 +
0;
totalExtenLen += extMasterSecretLen;
}
/* Last extension is the padding one:
Some F5 load balancers (and maybe other TLS implementations) will hang
if the ClientHello size is between 256 and 511 (inclusive). So we make
sure our ClientHello is always smaller than 256 or bigger than 512.
See section 4 in https://tools.ietf.org/html/draft-ietf-tls-padding-01
*/
if(((length + totalExtenLen + 2 + head) >= 256) &&
((length + totalExtenLen + 2 + head) < 512)) {
paddingLen = (512 - (length + totalExtenLen + 2 + head));
if(paddingLen<4) paddingLen = 4;
totalExtenLen += paddingLen;
}
if(totalExtenLen != 0) {
/*
* Total length extensions have to fit in a 16 bit field...
*/
if(totalExtenLen > 0xffff) {
sslErrorLog("Total extensions length EXCEEDED\n");
totalExtenLen = 0;
sessionTicketLen = 0;
serverNameLen = 0;
pointFormatLen = 0;
suppCurveLen = 0;
signatureAlgorithmsLen = 0;
}
else {
/* add length of total length plus lengths of extensions */
length += (totalExtenLen + 2);
}
}
if ((err = SSLAllocBuffer(clientHello, length + head)))
goto err_exit;
p = SSLEncodeHandshakeHeader(ctx, clientHello, SSL_HdskClientHello, length);
p = SSLEncodeInt(p, ctx->clientReqProtocol, 2);
sslLogNegotiateDebug("Requesting protocol version %04x", ctx->clientReqProtocol);
if ((err = SSLEncodeRandom(p, ctx)) != 0)
{ goto err_exit;
}
memcpy(ctx->clientRandom, p, SSL_CLIENT_SRVR_RAND_SIZE);
p += 32;
*p++ = sessionIDLen; /* 1 byte vector length */
sslLogNegotiateDebug("SessionIDLen = %lu\n", sessionIDLen);
if (sessionIDLen > 0)
{
memcpy(p, sessionIdentifier.data, sessionIDLen);
sslLogNegotiateDebug("SessionID = %02x...\n", p[0]);
}
p += sessionIDLen;
#if ENABLE_DTLS
if (ctx->isDTLS) {
/* TODO: Add the cookie ! Currently: size=0 -> no cookie */
*p++ = ctx->dtlsCookie.length;
if(ctx->dtlsCookie.length) {
memcpy(p, ctx->dtlsCookie.data, ctx->dtlsCookie.length);
p+=ctx->dtlsCookie.length;
}
sslLogNegotiateDebug("DTLS Hello: cookie len = %d\n",(int)ctx->dtlsCookie.length);
}
#endif
p = SSLEncodeInt(p, 2*numCipherSuites, 2);
/* 2 byte long vector length */
/* RFC 5746 : add the fake ciphersuite unless we are including the extension */
if(!ctx->secure_renegotiation) {
p = SSLEncodeInt(p, TLS_EMPTY_RENEGOTIATION_INFO_SCSV, 2);
}
/* https://tools.ietf.org/html/draft-ietf-tls-downgrade-scsv-00 */
if(ctx->fallback) {
p = SSLEncodeInt(p, TLS_FALLBACK_SCSV, 2);
}
for (i = 0; i<ctx->numEnabledCipherSuites; ++i) {
if(tls_handshake_ciphersuite_is_valid(ctx, ctx->enabledCipherSuites[i])) {
sslLogNegotiateDebug("Sending ciphersuite %04x",
(unsigned)ctx->enabledCipherSuites[i]);
p = SSLEncodeInt(p, ctx->enabledCipherSuites[i], 2);
}
}