rustls 0.5.5

Rustls is a modern TLS library written in Rust.
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
use msgs::enums::{ContentType, HandshakeType, ExtensionType, SignatureScheme};
use msgs::enums::{Compression, ProtocolVersion, AlertDescription, NamedGroup};
use msgs::message::{Message, MessagePayload};
use msgs::base::{Payload, PayloadU8};
use msgs::handshake::{HandshakePayload, HandshakeMessagePayload, ClientHelloPayload};
use msgs::handshake::{SessionID, Random, ServerHelloPayload};
use msgs::handshake::{ClientExtension, ServerExtension, HasServerExtensions};
use msgs::handshake::{SupportedSignatureSchemes, SupportedMandatedSignatureSchemes};
use msgs::handshake::DecomposedSignatureScheme;
use msgs::handshake::{NamedGroups, SupportedGroups, KeyShareEntry, EncryptedExtensions};
use msgs::handshake::{ECPointFormatList, SupportedPointFormats};
use msgs::handshake::{ProtocolNameList, ConvertProtocolNameList};
use msgs::handshake::{CertificatePayloadTLS13, CertificateEntry};
use msgs::handshake::ServerKeyExchangePayload;
use msgs::handshake::DigitallySignedStruct;
use msgs::handshake::{PresharedKeyIdentity, PresharedKeyOffer, HelloRetryRequest};
use msgs::enums::{ClientCertificateType, PSKKeyExchangeMode, ECPointFormat};
use msgs::codec::Codec;
use msgs::persist;
use msgs::ccs::ChangeCipherSpecPayload;
use client::{ClientSessionImpl, ConnState};
use session::SessionSecrets;
use key_schedule::{KeySchedule, SecretKind};
use cipher;
use suites;
use hash_hs;
use verify;
use rand;
use time;
use error::TLSError;
use handshake::Expectation;

use std::mem;

// draft-ietf-tls-tls13-18
const TLS13_DRAFT: u16 = 0x7f12;

macro_rules! extract_handshake(
  ( $m:expr, $t:path ) => (
    match $m.payload {
      MessagePayload::Handshake(ref hsp) => match hsp.payload {
        $t(ref hm) => Some(hm),
        _ => None
      },
      _ => None
    }
  )
);

pub type HandleFunction = fn(&mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError>;

// These are effectively operations on the ClientSessionImpl, variant on the
// connection state. They must not have state of their own -- so they're
// functions rather than a trait.
pub struct Handler {
    pub expect: Expectation,
    pub handle: HandleFunction,
}

fn illegal_param(sess: &mut ClientSessionImpl, why: &str) -> TLSError {
    sess.common.send_fatal_alert(AlertDescription::IllegalParameter);
    TLSError::PeerMisbehavedError(why.to_string())
}

fn ticket_timebase() -> u64 {
    time::get_time().sec as u64
}

fn check_aligned_handshake(sess: &mut ClientSessionImpl) -> Result<(), TLSError> {
    if !sess.common.handshake_joiner.is_empty() {
        Err(illegal_param(sess, "keys changed with pending hs fragment"))
    } else {
        Ok(())
    }
}

fn find_session(sess: &mut ClientSessionImpl) -> Option<persist::ClientSessionValue> {
    let key = persist::ClientSessionKey::session_for_dns_name(&sess.handshake_data.dns_name);
    let key_buf = key.get_encoding();

    let mut persist = sess.config.session_persistence.lock().unwrap();
    let maybe_value = persist.get(&key_buf);

    if maybe_value.is_none() {
        info!("No cached session for {:?}", sess.handshake_data.dns_name);
        return None;
    }

    let value = maybe_value.unwrap();
    if let Some(result) = persist::ClientSessionValue::read_bytes(&value) {
        if result.has_expired(ticket_timebase()) {
            None
        } else {
            Some(result)
        }
    } else {
        None
    }
}

fn find_kx_hint(sess: &mut ClientSessionImpl) -> Option<NamedGroup> {
    let key = persist::ClientSessionKey::hint_for_dns_name(&sess.handshake_data.dns_name);
    let key_buf = key.get_encoding();

    let mut persist = sess.config.session_persistence.lock().unwrap();
    let maybe_value = persist.get(&key_buf);
    maybe_value.and_then(|enc| NamedGroup::read_bytes(&enc))
}

fn save_kx_hint(sess: &mut ClientSessionImpl, group: NamedGroup) {
    let key = persist::ClientSessionKey::hint_for_dns_name(&sess.handshake_data.dns_name);

    let mut persist = sess.config.session_persistence.lock().unwrap();
    persist.put(key.get_encoding(), group.get_encoding());
}

/// If we have a ticket, we use the sessionid as a signal that we're
/// doing an abbreviated handshake.  See section 3.4 in RFC5077.
fn randomise_sessionid_for_ticket(csv: &mut persist::ClientSessionValue) {
    if csv.ticket.len() > 0 {
        let mut random_id = [0u8; 32];
        rand::fill_random(&mut random_id);
        csv.session_id = SessionID::new(&random_id);
    }
}

/// This implements the horrifying TLS1.3 hack where PSK binders have a
/// data dependency on the message they are contained within.
pub fn fill_in_psk_binder(sess: &mut ClientSessionImpl, hmp: &mut HandshakeMessagePayload) {
    // We need to know the hash function of the suite we're trying to resume into.
    let resuming = sess.handshake_data.resuming_session.as_ref().unwrap();
    let suite_hash = sess.find_cipher_suite(resuming.cipher_suite).unwrap().get_hash();

    // The binder is calculated over the clienthello, but doesn't include itself or its
    // length, or the length of its container.
    let binder_plaintext = hmp.get_encoding_for_binder_signing();
    let handshake_hash =
        sess.handshake_data.transcript.get_hash_given(suite_hash, &binder_plaintext);

    let mut empty_hash_ctx = hash_hs::HandshakeHash::new();
    empty_hash_ctx.start_hash(suite_hash);
    let empty_hash = empty_hash_ctx.get_current_hash();

    // Run a fake key_schedule to simulate what the server will do if it choses
    // to resume.
    let mut key_schedule = KeySchedule::new(suite_hash);
    key_schedule.input_secret(&resuming.master_secret.0);
    let base_key = key_schedule.derive(SecretKind::ResumptionPSKBinderKey, &empty_hash);
    let real_binder = key_schedule.sign_verify_data(&base_key, &handshake_hash);

    match hmp.payload {
        HandshakePayload::ClientHello(ref mut ch) => {
            ch.set_psk_binder(real_binder);
        }
        _ => {}
    };
}

pub fn emit_client_hello(sess: &mut ClientSessionImpl) -> ConnState {
    emit_client_hello_for_retry(sess, None)
}

fn emit_client_hello_for_retry(sess: &mut ClientSessionImpl,
                               retryreq: Option<&HelloRetryRequest>)
                               -> ConnState {
    // Do we have a SessionID or ticket cached for this host?
    sess.handshake_data.resuming_session = find_session(sess);
    let (session_id, ticket, resume_version) = if sess.handshake_data.resuming_session.is_some() {
        let mut resuming = sess.handshake_data.resuming_session.as_mut().unwrap();
        if resuming.version == ProtocolVersion::TLSv1_2 {
            randomise_sessionid_for_ticket(resuming);
        }
        info!("Resuming session");
        (resuming.session_id.clone(), resuming.ticket.0.clone(), resuming.version)
    } else {
        info!("Not resuming any session");
        (SessionID::empty(), Vec::new(), ProtocolVersion::Unknown(0))
    };

    let support_tls12 = sess.config.versions.contains(&ProtocolVersion::TLSv1_2);
    let support_tls13 = sess.config.versions.contains(&ProtocolVersion::TLSv1_3);

    let mut supported_versions = Vec::new();
    if support_tls13 {
        supported_versions.push(ProtocolVersion::Unknown(TLS13_DRAFT));
    }

    if support_tls12 {
        supported_versions.push(ProtocolVersion::TLSv1_2);
    }

    let mut key_shares = vec![];

    if support_tls13 {
        // Choose our groups:
        // - if we've been asked via HelloRetryRequest for a specific
        //   one, do that.
        // - if not, we might have a hint of what the server supports
        // - if not, send just X25519.
        //
        let groups = retryreq.and_then(|req| req.get_requested_key_share_group())
            .or_else(|| find_kx_hint(sess))
            .or_else(|| Some(NamedGroup::X25519))
            .map(|grp| vec![ grp ])
            .unwrap();

        for group in groups {
            // in reply to HelloRetryRequest, we must not alter any existing key
            // shares
            if let Some(already_offered_share) = find_key_share(sess, group) {
                key_shares.push(KeyShareEntry::new(group, &already_offered_share.pubkey));
                sess.handshake_data.offered_key_shares.push(already_offered_share);
                continue;
            }

            if let Some(key_share) = suites::KeyExchange::start_ecdhe(group) {
                key_shares.push(KeyShareEntry::new(group, &key_share.pubkey));
                sess.handshake_data.offered_key_shares.push(key_share);
            }
        }
    }

    let mut exts = Vec::new();
    if !supported_versions.is_empty() {
        exts.push(ClientExtension::SupportedVersions(supported_versions));
    }
    exts.push(ClientExtension::make_sni(&sess.handshake_data.dns_name));
    exts.push(ClientExtension::ECPointFormats(ECPointFormatList::supported()));
    exts.push(ClientExtension::NamedGroups(NamedGroups::supported()));
    exts.push(ClientExtension::SignatureAlgorithms(SupportedSignatureSchemes::supported_verify()));

    if support_tls13 {
        exts.push(ClientExtension::KeyShare(key_shares));
    }

    if let Some(cookie) = retryreq.and_then(|req| req.get_cookie()) {
        exts.push(ClientExtension::Cookie(cookie.clone()));
    }

    if support_tls13 && sess.config.enable_tickets {
        // We could support PSK_KE here too. Such connections don't
        // have forward secrecy, and are similar to TLS1.2 resumption.
        let psk_modes = vec![ PSKKeyExchangeMode::PSK_DHE_KE ];
        exts.push(ClientExtension::PresharedKeyModes(psk_modes));
    }

    if !sess.config.alpn_protocols.is_empty() {
        exts.push(ClientExtension::Protocols(ProtocolNameList::from_strings(&sess.config
            .alpn_protocols)));
    }

    let fill_in_binder = if support_tls13 && sess.config.enable_tickets &&
                            resume_version == ProtocolVersion::TLSv1_3 &&
                            !ticket.is_empty() {
        // Finally, and only for TLS1.3 with a ticket resumption, include a binder
        // for our ticket.  This must go last.
        //
        // Include an empty binder. It gets filled in below because it depends on
        // the message it's contained in (!!!).
        let (obfuscated_ticket_age, suite) = {
            let resuming = sess.handshake_data
                .resuming_session
                .as_ref()
                .unwrap();
            (resuming.get_obfuscated_ticket_age(ticket_timebase()), resuming.cipher_suite)
        };

        let binder_len = sess.find_cipher_suite(suite).unwrap().get_hash().output_len;
        let binder = vec![0u8; binder_len];

        let psk_identity = PresharedKeyIdentity::new(ticket, obfuscated_ticket_age);
        let psk_ext = PresharedKeyOffer::new(psk_identity, binder);
        exts.push(ClientExtension::PresharedKey(psk_ext));
        true
    } else if sess.config.enable_tickets {
        // If we have a ticket, include it.  Otherwise, request one.
        if ticket.is_empty() {
            exts.push(ClientExtension::SessionTicketRequest);
        } else {
            exts.push(ClientExtension::SessionTicketOffer(Payload::new(ticket)));
        }
        false
    } else {
        false
    };

    // Note what extensions we sent.
    sess.handshake_data.sent_extensions = exts.iter()
        .map(|ext| ext.get_type())
        .collect();

    let mut chp = HandshakeMessagePayload {
        typ: HandshakeType::ClientHello,
        payload: HandshakePayload::ClientHello(ClientHelloPayload {
            client_version: ProtocolVersion::TLSv1_2,
            random: Random::from_slice(&sess.handshake_data.randoms.client),
            session_id: session_id,
            cipher_suites: sess.get_cipher_suites(),
            compression_methods: vec![Compression::Null],
            extensions: exts,
        }),
    };

    if fill_in_binder {
        fill_in_psk_binder(sess, &mut chp);
    }

    let ch = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_0,
        payload: MessagePayload::Handshake(chp),
    };

    debug!("Sending ClientHello {:#?}", ch);

    sess.handshake_data.transcript.add_message(&ch);
    sess.common.send_msg(ch, false);

    if support_tls13 && retryreq.is_none() {
        ConnState::ExpectServerHelloOrHelloRetryRequest
    } else {
        ConnState::ExpectServerHello
    }
}

fn sent_unsolicited_extensions(sess: &ClientSessionImpl,
                               received_exts: &[ServerExtension],
                               allowed_unsolicited: &[ExtensionType]) -> bool {

    let sent = &sess.handshake_data.sent_extensions;
    for ext in received_exts {
        let ext_type = ext.get_type();
        if !sent.contains(&ext_type) && !allowed_unsolicited.contains(&ext_type) {
            debug!("Unsolicited extension {:?}", ext_type);
            return true;
        }
    }

    false
}

fn has_key_share(sess: &mut ClientSessionImpl,
                 group: NamedGroup) -> bool {
    sess.handshake_data.offered_key_shares
        .iter()
        .any(|share| share.group == group)
}

fn find_key_share(sess: &mut ClientSessionImpl,
                  group: NamedGroup)
                  -> Option<suites::KeyExchange> {
    let shares = &mut sess.handshake_data.offered_key_shares;
    for i in 0..shares.len() {
        if shares[i].group == group {
            return Some(shares.remove(i));
        }
    }

    None
}

fn find_key_share_and_discard_others(sess: &mut ClientSessionImpl,
                                     group: NamedGroup)
                                     -> Result<suites::KeyExchange, TLSError> {
    let ret = find_key_share(sess, group)
        .ok_or_else(|| illegal_param(sess, "wrong group for key share"));
    sess.handshake_data.offered_key_shares.clear();
    ret
}

// Extensions we expect in plaintext in the ServerHello.
static ALLOWED_PLAINTEXT_EXTS: &'static [ExtensionType] = &[
    ExtensionType::KeyShare,
    ExtensionType::PreSharedKey
];

// Only the intersection of things we offer, and those disallowed
// in TLS1.3
static DISALLOWED_TLS13_EXTS: &'static [ExtensionType] = &[
    ExtensionType::ECPointFormats,
    ExtensionType::SessionTicket,
    ExtensionType::RenegotiationInfo
];

fn validate_server_hello_tls13(sess: &mut ClientSessionImpl,
                               server_hello: &ServerHelloPayload)
                               -> Result<(), TLSError> {
    for ext in &server_hello.extensions {
        if !ALLOWED_PLAINTEXT_EXTS.contains(&ext.get_type()) {
            sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
            return Err(TLSError::PeerMisbehavedError("server sent unexpected cleartext ext"
                                                     .to_string()));
        }
    }

    Ok(())
}

fn start_handshake_traffic(sess: &mut ClientSessionImpl,
                           server_hello: &ServerHelloPayload)
                           -> Result<(), TLSError> {
    let suite = sess.common.get_suite();
    let hash = suite.get_hash();
    let mut key_schedule = KeySchedule::new(hash);

    if let Some(selected_psk) = server_hello.get_psk_index() {
        if let Some(ref resuming) = sess.handshake_data.resuming_session {
            let resume_from_suite = sess.find_cipher_suite(resuming.cipher_suite).unwrap();
            if !resume_from_suite.can_resume_to(suite) {
                return Err(TLSError::PeerMisbehavedError("server resuming incompatible suite"
                    .to_string()));
            }

            if selected_psk != 0 {
                return Err(TLSError::PeerMisbehavedError("server selected invalid psk"
                    .to_string()));
            }

            info!("Resuming using PSK");
            key_schedule.input_secret(&resuming.master_secret.0);
        } else {
            return Err(TLSError::PeerMisbehavedError("server selected unoffered psk".to_string()));
        }
    } else {
        info!("Not resuming");
        key_schedule.input_empty();
        sess.handshake_data.resuming_session.take();
    }

    let their_key_share = try! {
        server_hello.get_key_share()
            .ok_or_else(|| {
                sess.common.send_fatal_alert(AlertDescription::MissingExtension);
                TLSError::PeerMisbehavedError("missing key share".to_string())
                })
    };

    let our_key_share = try!(find_key_share_and_discard_others(sess,
                                                               their_key_share.group));
    let shared = try! {
        our_key_share.complete(&their_key_share.payload.0)
            .ok_or_else(|| TLSError::PeerMisbehavedError("key exchange failed"
                                                         .to_string()))
    };

    save_kx_hint(sess, their_key_share.group);
    key_schedule.input_secret(&shared.premaster_secret);

    try!(check_aligned_handshake(sess));

    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    let write_key = key_schedule.derive(SecretKind::ClientHandshakeTrafficSecret, &handshake_hash);
    let read_key = key_schedule.derive(SecretKind::ServerHandshakeTrafficSecret, &handshake_hash);
    sess.common.set_message_encrypter(cipher::new_tls13_write(suite, &write_key));
    sess.common.set_message_decrypter(cipher::new_tls13_read(suite, &read_key));
    key_schedule.current_client_traffic_secret = write_key;
    key_schedule.current_server_traffic_secret = read_key;
    sess.common.set_key_schedule(key_schedule);

    Ok(())
}

fn process_alpn_protocol(sess: &mut ClientSessionImpl,
                         proto: Option<String>)
                         -> Result<(), TLSError> {
    sess.alpn_protocol = proto;
    if sess.alpn_protocol.is_some() {
        if !sess.config.alpn_protocols.contains(sess.alpn_protocol.as_ref().unwrap()) {
            return Err(illegal_param(sess, "server sent non-offered ALPN protocol"));
        }
    }
    info!("ALPN protocol is {:?}", sess.alpn_protocol);
    Ok(())
}

fn handle_server_hello(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let server_hello = extract_handshake!(m, HandshakePayload::ServerHello).unwrap();
    debug!("We got ServerHello {:#?}", server_hello);

    match server_hello.server_version {
        ProtocolVersion::TLSv1_2 if sess.config.versions.contains(&ProtocolVersion::TLSv1_2) => {
            sess.common.is_tls13 = false;
        }
        ProtocolVersion::TLSv1_3 |
        ProtocolVersion::Unknown(TLS13_DRAFT) if sess.config
            .versions
            .contains(&ProtocolVersion::TLSv1_3) => {
            sess.common.is_tls13 = true;
        }
        _ => {
            sess.common.send_fatal_alert(AlertDescription::ProtocolVersion);
            return Err(TLSError::PeerIncompatibleError("server does not support TLS v1.2/v1.3"
                .to_string()));
        }
    };

    if server_hello.compression_method != Compression::Null {
        sess.common.send_fatal_alert(AlertDescription::HandshakeFailure);
        return Err(TLSError::PeerMisbehavedError("server chose non-Null compression".to_string()));
    }

    if server_hello.has_duplicate_extension() {
        sess.common.send_fatal_alert(AlertDescription::DecodeError);
        return Err(TLSError::PeerMisbehavedError("server sent duplicate extensions".to_string()));
    }

    let allowed_unsolicited = [ ExtensionType::RenegotiationInfo ];
    if sent_unsolicited_extensions(sess, &server_hello.extensions, &allowed_unsolicited) {
        sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
        return Err(TLSError::PeerMisbehavedError("server sent unsolicited extension".to_string()));
    }

    // Extract ALPN protocol
    if !sess.common.is_tls13 {
        try!(process_alpn_protocol(sess, server_hello.get_alpn_protocol()));
    }

    // If ECPointFormats extension is supplied by the server, it must contain
    // Uncompressed.  But it's allowed to be omitted.
    if let Some(point_fmts) = server_hello.get_ecpoints_extension() {
        if !point_fmts.contains(&ECPointFormat::Uncompressed) {
            sess.common.send_fatal_alert(AlertDescription::HandshakeFailure);
            return Err(TLSError::PeerMisbehavedError("server does not support uncompressed points"
                                                     .to_string()));
        }
    }

    let scs = sess.find_cipher_suite(server_hello.cipher_suite);

    if scs.is_none() {
        sess.common.send_fatal_alert(AlertDescription::HandshakeFailure);
        return Err(TLSError::PeerMisbehavedError("server chose non-offered ciphersuite"
            .to_string()));
    }

    info!("Using ciphersuite {:?}", server_hello.cipher_suite);
    sess.common.set_suite(scs.unwrap());

    let version = if sess.common.is_tls13 {
        ProtocolVersion::TLSv1_3
    } else {
        ProtocolVersion::TLSv1_2
    };
    if !sess.common.get_suite().usable_for_version(version) {
        return Err(illegal_param(sess, "server chose unusable ciphersuite for version"));
    }

    // Start our handshake hash, and input the server-hello.
    sess.handshake_data.transcript.start_hash(sess.common.get_suite().get_hash());
    sess.handshake_data.transcript.add_message(&m);

    // For TLS1.3, start message encryption using
    // handshake_traffic_secret.
    if sess.common.is_tls13 {
        try!(validate_server_hello_tls13(sess, &server_hello));
        try!(start_handshake_traffic(sess, &server_hello));
        return Ok(ConnState::ExpectEncryptedExtensions);
    }

    // TLS1.2 only from here-on

    // Save ServerRandom and SessionID
    server_hello.random.write_slice(&mut sess.handshake_data.randoms.server);
    sess.handshake_data.session_id = server_hello.session_id.clone();

    // Might the server send a ticket?
    if server_hello.find_extension(ExtensionType::SessionTicket).is_some() {
        info!("Server supports tickets");
        sess.handshake_data.must_issue_new_ticket = true;
    }

    // See if we're successfully resuming.
    let mut abbreviated_handshake = false;
    if let Some(ref resuming) = sess.handshake_data.resuming_session {
        if resuming.session_id == sess.handshake_data.session_id {
            info!("Server agreed to resume");
            abbreviated_handshake = true;

            // Is the server telling lies about the ciphersuite?
            if resuming.cipher_suite != scs.unwrap().suite {
                let error_msg = "abbreviated handshake offered, but with varied cs".to_string();
                return Err(TLSError::PeerMisbehavedError(error_msg));
            }

            sess.secrets = Some(SessionSecrets::new_resume(&sess.handshake_data.randoms,
                                                           scs.unwrap().get_hash(),
                                                           &resuming.master_secret.0));
        }
    }

    if abbreviated_handshake {
        sess.start_encryption_tls12();

        if sess.handshake_data.must_issue_new_ticket {
            Ok(ConnState::ExpectNewTicketResume)
        } else {
            Ok(ConnState::ExpectCCSResume)
        }
    } else {
        Ok(ConnState::ExpectCertificate)
    }
}

pub static EXPECT_SERVER_HELLO: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::ServerHello],
    },
    handle: handle_server_hello,
};

fn handle_hello_retry_request(sess: &mut ClientSessionImpl,
                              m: Message)
                              -> Result<ConnState, TLSError> {
    let hrr = extract_handshake!(m, HandshakePayload::HelloRetryRequest).unwrap();
    sess.handshake_data.transcript.add_message(&m);
    debug!("Got HRR {:?}", hrr);

    let has_cookie = hrr.get_cookie().is_some();
    let req_group = hrr.get_requested_key_share_group();

    // A retry request is illegal if it contains no cookie and asks for
    // retry of a group we already sent.
    if !has_cookie && req_group.map(|g| has_key_share(sess, g)).unwrap_or(false) {
        return Err(illegal_param(sess, "server requested hrr with our group"));
    }

    // Or asks for us to retry on an unsupported group.
    if let Some(group) = req_group {
        if !NamedGroups::supported().contains(&group) {
            return Err(illegal_param(sess, "server requested hrr with bad group"));
        }
    }

    // Or has an empty cookie.
    if has_cookie && hrr.get_cookie().unwrap().len() == 0 {
        return Err(illegal_param(sess, "server requested hrr with empty cookie"));
    }

    // Or has something unrecognised
    if hrr.has_unknown_extension() {
        sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
        return Err(TLSError::PeerIncompatibleError("server sent hrr with unhandled extension"
                                                   .to_string()));
    }

    // Or has the same extensions more than once
    if hrr.has_duplicate_extension() {
        return Err(illegal_param(sess, "server send duplicate hrr extensions"));
    }

    // Or asks us to change nothing.
    if !has_cookie && req_group.is_none() {
        return Err(illegal_param(sess, "server requested hrr with no changes"));
    }

    // Or asks us to talk a protocol we didn't offer, or doesn't support HRR at all.
    match hrr.server_version {
        ProtocolVersion::TLSv1_3 | ProtocolVersion::Unknown(TLS13_DRAFT) => {
        }
        _ => {
            return Err(illegal_param(sess, "server requested unsupported version in hrr"));
        }
    }

    Ok(emit_client_hello_for_retry(sess, Some(hrr)))
}

fn handle_server_hello_or_retry(sess: &mut ClientSessionImpl,
                                m: Message)
                                -> Result<ConnState, TLSError> {
    if m.is_handshake_type(HandshakeType::ServerHello) {
        handle_server_hello(sess, m)
    } else {
        handle_hello_retry_request(sess, m)
    }
}

pub static EXPECT_SERVER_HELLO_OR_RETRY: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::ServerHello, HandshakeType::HelloRetryRequest],
    },
    handle: handle_server_hello_or_retry,
};

fn validate_encrypted_extensions(sess: &mut ClientSessionImpl,
                                 exts: &EncryptedExtensions) -> Result<(), TLSError> {
    if exts.has_duplicate_extension() {
        sess.common.send_fatal_alert(AlertDescription::DecodeError);
        return Err(TLSError::PeerMisbehavedError("server sent duplicate encrypted extensions"
                                                 .to_string()));
    }

    if sent_unsolicited_extensions(sess, &exts, &[]) {
        sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
        let msg = "server sent unsolicited encrypted extension".to_string();
        return Err(TLSError::PeerMisbehavedError(msg));
    }

    for ext in exts {
        if ALLOWED_PLAINTEXT_EXTS.contains(&ext.get_type()) ||
           DISALLOWED_TLS13_EXTS.contains(&ext.get_type()) {
            sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
            let msg = "server sent inappropriate encrypted extension".to_string();
            return Err(TLSError::PeerMisbehavedError(msg));
        }
    }

    Ok(())
}

fn handle_encrypted_extensions(sess: &mut ClientSessionImpl,
                               m: Message)
                               -> Result<ConnState, TLSError> {
    let exts = extract_handshake!(m, HandshakePayload::EncryptedExtensions).unwrap();
    info!("TLS1.3 encrypted extensions: {:?}", exts);
    sess.handshake_data.transcript.add_message(&m);

    try!(validate_encrypted_extensions(sess, exts));
    try!(process_alpn_protocol(sess, exts.get_alpn_protocol()));

    if sess.handshake_data.resuming_session.is_some() {
        Ok(ConnState::ExpectFinished)
    } else {
        Ok(ConnState::ExpectCertificateOrCertReq)
    }
}

pub static EXPECT_ENCRYPTED_EXTENSIONS: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::EncryptedExtensions],
    },
    handle: handle_encrypted_extensions,
};

fn handle_certificate(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    sess.handshake_data.transcript.add_message(&m);

    if sess.common.is_tls13 {
        let cert_chain = extract_handshake!(m, HandshakePayload::CertificateTLS13).unwrap();

        // This is only non-empty for client auth.
        if cert_chain.context.len() > 0 {
            warn!("certificate with non-empty context during handshake");
            sess.common.send_fatal_alert(AlertDescription::DecodeError);
            return Err(TLSError::CorruptMessagePayload(ContentType::Handshake));
        }

        if cert_chain.any_entry_has_duplicate_extension() ||
            cert_chain.any_entry_has_unknown_extension() {
            warn!("certificate chain contains unsolicited/unknown extension");
            sess.common.send_fatal_alert(AlertDescription::UnsupportedExtension);
            return Err(TLSError::PeerMisbehavedError("bad cert chain extensions".to_string()));
        }

        sess.handshake_data.server_cert_chain = cert_chain.convert();
        Ok(ConnState::ExpectCertificateVerify)
    } else {
        let cert_chain = extract_handshake!(m, HandshakePayload::Certificate).unwrap();
        sess.handshake_data.server_cert_chain = cert_chain.clone();
        Ok(ConnState::ExpectServerKX)
    }
}

pub static EXPECT_CERTIFICATE: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::Certificate],
    },
    handle: handle_certificate,
};

fn handle_certificate_or_cert_req(sess: &mut ClientSessionImpl,
                                  m: Message)
                                  -> Result<ConnState, TLSError> {
    assert!(sess.common.is_tls13);

    if m.is_handshake_type(HandshakeType::Certificate) {
        handle_certificate(sess, m)
    } else {
        handle_certificate_req_tls13(sess, m)
    }
}

pub static EXPECT_CERTIFICATE_OR_CERTREQ: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::Certificate, HandshakeType::CertificateRequest],
    },
    handle: handle_certificate_or_cert_req,
};

fn handle_server_kx(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let opaque_kx = extract_handshake!(m, HandshakePayload::ServerKeyExchange).unwrap();
    let maybe_decoded_kx = opaque_kx.unwrap_given_kxa(&sess.common.get_suite().kx);
    sess.handshake_data.transcript.add_message(&m);

    if maybe_decoded_kx.is_none() {
        sess.common.send_fatal_alert(AlertDescription::DecodeError);
        return Err(TLSError::CorruptMessagePayload(ContentType::Handshake));
    }

    let decoded_kx = maybe_decoded_kx.unwrap();

    // Save the signature and signed parameters for later verification.
    sess.handshake_data.server_kx_sig = decoded_kx.get_sig();
    decoded_kx.encode_params(&mut sess.handshake_data.server_kx_params);

    match decoded_kx {
        ServerKeyExchangePayload::ECDHE(ecdhe) => {
            info!("ECDHE curve is {:?}", ecdhe.params.curve_params)
        }
        _ => (),
    }

    Ok(ConnState::ExpectServerHelloDoneOrCertRequest)
}

pub static EXPECT_SERVER_KX: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::ServerKeyExchange],
    },
    handle: handle_server_kx,
};

// --- TLS1.3 CertificateVerify ---
fn handle_certificate_verify(sess: &mut ClientSessionImpl,
                             m: Message)
                             -> Result<ConnState, TLSError> {
    let cert_verify = extract_handshake!(m, HandshakePayload::CertificateVerify).unwrap();

    // 1. Verify the certificate chain.
    // 2. Verify their signature on the handshake.
    try!(verify::verify_server_cert(&sess.config.root_store,
                                  &sess.handshake_data.server_cert_chain,
                                  &sess.handshake_data.dns_name));

    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    try!(verify::verify_tls13(&sess.handshake_data.server_cert_chain[0],
                            &cert_verify,
                            &handshake_hash,
                            b"TLS 1.3, server CertificateVerify\x00"));

    sess.handshake_data.transcript.add_message(&m);

    Ok(ConnState::ExpectFinished)
}

pub static EXPECT_CERTIFICATE_VERIFY: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::CertificateVerify],
    },
    handle: handle_certificate_verify,
};

fn emit_certificate(sess: &mut ClientSessionImpl) {
    let chosen_cert = sess.handshake_data.client_auth_cert.take();

    let cert = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_2,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::Certificate,
            payload: HandshakePayload::Certificate(chosen_cert.unwrap_or_else(Vec::new)),
        }),
    };

    sess.handshake_data.transcript.add_message(&cert);
    sess.common.send_msg(cert, false);
}

fn emit_clientkx(sess: &mut ClientSessionImpl, kxd: &suites::KeyExchangeResult) {
    let mut buf = Vec::new();
    let ecpoint = PayloadU8::new(kxd.pubkey.clone());
    ecpoint.encode(&mut buf);
    let pubkey = Payload::new(buf);

    let ckx = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_2,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::ClientKeyExchange,
            payload: HandshakePayload::ClientKeyExchange(pubkey),
        }),
    };

    sess.handshake_data.transcript.add_message(&ckx);
    sess.common.send_msg(ckx, false);
}

fn emit_certverify(sess: &mut ClientSessionImpl) {
    if sess.handshake_data.client_auth_key.is_none() {
        debug!("Not sending CertificateVerify, no key");
        sess.handshake_data.transcript.abandon_client_auth();
        return;
    }

    let message = sess.handshake_data.transcript.take_handshake_buf();
    let key = sess.handshake_data.client_auth_key.take().unwrap();
    let sigscheme = sess.handshake_data
        .client_auth_sigscheme
        .clone()
        .unwrap();
    let sig = key.sign(sigscheme, &message)
        .expect("client auth signing failed unexpectedly");
    let body = DigitallySignedStruct::new(sigscheme, sig);

    let m = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_2,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::CertificateVerify,
            payload: HandshakePayload::CertificateVerify(body),
        }),
    };

    sess.handshake_data.transcript.add_message(&m);
    sess.common.send_msg(m, false);
}

fn emit_ccs(sess: &mut ClientSessionImpl) {
    let ccs = Message {
        typ: ContentType::ChangeCipherSpec,
        version: ProtocolVersion::TLSv1_2,
        payload: MessagePayload::ChangeCipherSpec(ChangeCipherSpecPayload {}),
    };

    sess.common.send_msg(ccs, false);
    sess.common.we_now_encrypting();
}

fn emit_finished(sess: &mut ClientSessionImpl) {
    let vh = sess.handshake_data.transcript.get_current_hash();
    let verify_data = sess.secrets.as_ref().unwrap().client_verify_data(&vh);
    let verify_data_payload = Payload::new(verify_data);

    let f = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_2,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::Finished,
            payload: HandshakePayload::Finished(verify_data_payload),
        }),
    };

    sess.handshake_data.transcript.add_message(&f);
    sess.common.send_msg(f, true);
}

// --- Either a CertificateRequest, or a ServerHelloDone. ---
// Existence of the CertificateRequest tells us the server is asking for
// client auth.  Otherwise we go straight to ServerHelloDone.
fn handle_certificate_req(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let certreq = extract_handshake!(m, HandshakePayload::CertificateRequest).unwrap();
    sess.handshake_data.transcript.add_message(&m);
    sess.handshake_data.doing_client_auth = true;
    info!("Got CertificateRequest {:?}", certreq);

    // The RFC jovially describes the design here as 'somewhat complicated'
    // and 'somewhat underspecified'.  So thanks for that.

    // We only support RSA signing at the moment.  If you don't support that,
    // we're not doing client auth.
    if !certreq.certtypes.contains(&ClientCertificateType::RSASign) {
        warn!("Server asked for client auth but without RSASign");
        return Ok(ConnState::ExpectServerHelloDone);
    }

    let canames = certreq.canames
        .iter()
        .map(|p| p.0.as_slice())
        .collect::<Vec<&[u8]>>();
    let maybe_certkey =
        sess.config.client_auth_cert_resolver.resolve(&canames, &certreq.sigschemes);

    let scs = sess.common.get_suite();
    let maybe_sigscheme = scs.resolve_sig_scheme(&certreq.sigschemes);

    if maybe_certkey.is_some() && maybe_sigscheme.is_some() {
        let (cert, key) = maybe_certkey.unwrap();
        info!("Attempting client auth, will use {:?}", maybe_sigscheme.as_ref().unwrap());
        sess.handshake_data.client_auth_cert = Some(cert);
        sess.handshake_data.client_auth_key = Some(key);
        sess.handshake_data.client_auth_sigscheme = maybe_sigscheme;
    } else {
        info!("Client auth requested but no cert/sigscheme available");
    }

    Ok(ConnState::ExpectServerHelloDone)
}

// TLS1.3 version of the above.  We then move to expecting the server Certificate.
// Unfortunately the CertificateRequest type changed in an annoying way in TLS1.3.
fn handle_certificate_req_tls13(sess: &mut ClientSessionImpl,
                                m: Message)
                                -> Result<ConnState, TLSError> {
    let ref mut certreq = extract_handshake!(m, HandshakePayload::CertificateRequestTLS13).unwrap();
    sess.handshake_data.transcript.add_message(&m);
    sess.handshake_data.doing_client_auth = true;
    info!("Got CertificateRequest {:?}", certreq);

    // Fortunately the problems here in TLS1.2 and prior are corrected in
    // TLS1.3.

    // Must be empty during handshake.
    if certreq.context.len() > 0 {
        warn!("Server sent non-empty certreq context");
        sess.common.send_fatal_alert(AlertDescription::DecodeError);
        return Err(TLSError::CorruptMessagePayload(ContentType::Handshake));
    }

    let tls13_sign_schemes = SupportedSignatureSchemes::supported_sign_tls13();
    let compat_sigschemes = certreq.sigschemes
        .iter()
        .cloned()
        .filter(|scheme| tls13_sign_schemes.contains(scheme))
        .collect::<Vec<SignatureScheme>>();

    if compat_sigschemes.is_empty() {
        sess.common.send_fatal_alert(AlertDescription::DecodeError);
        return Err(TLSError::PeerIncompatibleError("server sent bad certreq schemes".to_string()));
    }

    let canames = certreq.canames
        .iter()
        .map(|p| p.0.as_slice())
        .collect::<Vec<&[u8]>>();
    let maybe_certkey =
        sess.config.client_auth_cert_resolver.resolve(&canames, &compat_sigschemes);

    if maybe_certkey.is_some() {
        let (cert, key) = maybe_certkey.unwrap();
        let maybe_sigscheme = key.choose_scheme(&compat_sigschemes);
        info!("Attempting client auth, will use sigscheme {:?}", maybe_sigscheme);
        sess.handshake_data.client_auth_cert = Some(cert);
        sess.handshake_data.client_auth_key = Some(key);
        sess.handshake_data.client_auth_sigscheme = maybe_sigscheme;
        sess.handshake_data.client_auth_context = Some(certreq.context.0.clone());
    } else {
        info!("Client auth requested but no cert selected");
    }

    Ok(ConnState::ExpectCertificate)
}

fn handle_done_or_certreq(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    if extract_handshake!(m, HandshakePayload::CertificateRequest).is_some() {
        handle_certificate_req(sess, m)
    } else {
        sess.handshake_data.transcript.abandon_client_auth();
        handle_server_hello_done(sess, m)
    }
}

pub static EXPECT_DONE_OR_CERTREQ: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::CertificateRequest, HandshakeType::ServerHelloDone],
    },
    handle: handle_done_or_certreq,
};

fn handle_server_hello_done(sess: &mut ClientSessionImpl,
                            m: Message)
                            -> Result<ConnState, TLSError> {
    sess.handshake_data.transcript.add_message(&m);

    info!("Server cert is {:?}", sess.handshake_data.server_cert_chain);
    info!("Server DNS name is {:?}", sess.handshake_data.dns_name);

    // 1. Verify the cert chain.
    // 2. Verify that the top certificate signed their kx.
    // 3. If doing client auth, send our Certificate.
    // 4. Complete the key exchange:
    //    a) generate our kx pair
    //    b) emit a ClientKeyExchange containing it
    //    c) if doing client auth, emit a CertificateVerify
    //    d) emit a CCS
    //    e) derive the shared keys, and start encryption
    // 5. emit a Finished, our first encrypted message under the new keys.

    // 1.
    try!(verify::verify_server_cert(&sess.config.root_store,
                                  &sess.handshake_data.server_cert_chain,
                                  &sess.handshake_data.dns_name));

    // 2.
    // Build up the contents of the signed message.
    // It's ClientHello.random || ServerHello.random || ServerKeyExchange.params
    {
        let mut message = Vec::new();
        message.extend_from_slice(&sess.handshake_data.randoms.client);
        message.extend_from_slice(&sess.handshake_data.randoms.server);
        message.extend_from_slice(&sess.handshake_data.server_kx_params);

        // Check the signature is compatible with the ciphersuite.
        let sig = sess.handshake_data.server_kx_sig.as_ref().unwrap();
        let scs = sess.common.get_suite();
        if scs.sign != sig.scheme.sign() {
            let error_message =
                format!("peer signed kx with wrong algorithm (got {:?} expect {:?})",
                                  sig.scheme.sign(), scs.sign);
            return Err(TLSError::PeerMisbehavedError(error_message));
        }

        try!(verify::verify_signed_struct(&message,
                                      &sess.handshake_data.server_cert_chain[0],
                                      sig));
    }

    // 3.
    if sess.handshake_data.doing_client_auth {
        emit_certificate(sess);
    }

    // 4a.
    let kxd = try! {
        sess.common.get_suite()
        .do_client_kx(&sess.handshake_data.server_kx_params)
        .ok_or_else(|| TLSError::PeerMisbehavedError("key exchange failed".to_string()))
    };

    // 4b.
    emit_clientkx(sess, &kxd);

    // 4c.
    if sess.handshake_data.doing_client_auth {
        emit_certverify(sess);
    }

    // 4d.
    emit_ccs(sess);

    // 4e. Now commit secrets.
    let hashalg = sess.common.get_suite().get_hash();
    sess.secrets =
        Some(SessionSecrets::new(&sess.handshake_data.randoms, hashalg, &kxd.premaster_secret));
    sess.start_encryption_tls12();

    // 5.
    emit_finished(sess);

    if sess.handshake_data.must_issue_new_ticket {
        Ok(ConnState::ExpectNewTicket)
    } else {
        Ok(ConnState::ExpectCCS)
    }
}

pub static EXPECT_SERVER_HELLO_DONE: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::ServerHelloDone],
    },
    handle: handle_server_hello_done,
};

// -- Waiting for their CCS --
fn handle_ccs(sess: &mut ClientSessionImpl, _m: Message) -> Result<ConnState, TLSError> {
    // CCS should not be received interleaved with fragmented handshake-level
    // message.
    if !sess.common.handshake_joiner.is_empty() {
        warn!("CCS received interleaved with fragmented handshake");
        return Err(TLSError::InappropriateMessage {
            expect_types: vec![ ContentType::Handshake ],
            got_type: ContentType::ChangeCipherSpec,
        });
    }

    // nb. msgs layer validates trivial contents of CCS
    sess.common.peer_now_encrypting();
    Ok(ConnState::ExpectFinished)
}

pub static EXPECT_CCS: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::ChangeCipherSpec],
        handshake_types: &[],
    },
    handle: handle_ccs,
};

fn handle_new_ticket(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let ticket = extract_handshake!(m, HandshakePayload::NewSessionTicket).unwrap();
    sess.handshake_data.transcript.add_message(&m);
    sess.handshake_data.new_ticket = ticket.ticket.0.clone();
    sess.handshake_data.new_ticket_lifetime = ticket.lifetime_hint;
    Ok(ConnState::ExpectCCS)
}

pub static EXPECT_NEW_TICKET: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::NewSessionTicket],
    },
    handle: handle_new_ticket,
};

fn handle_ccs_resume(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    handle_ccs(sess, m).and(Ok(ConnState::ExpectFinishedResume))
}

pub static EXPECT_CCS_RESUME: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::ChangeCipherSpec],
        handshake_types: &[],
    },
    handle: handle_ccs_resume,
};

fn handle_new_ticket_resume(sess: &mut ClientSessionImpl,
                            m: Message)
                            -> Result<ConnState, TLSError> {
    handle_new_ticket(sess, m).and(Ok(ConnState::ExpectCCSResume))
}

pub static EXPECT_NEW_TICKET_RESUME: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::NewSessionTicket],
    },
    handle: handle_new_ticket_resume,
};

// -- Waiting for their finished --
fn save_session(sess: &mut ClientSessionImpl) {
    // Save a ticket.  If we got a new ticket, save that.  Otherwise, save the
    // original ticket again.
    let mut ticket = mem::replace(&mut sess.handshake_data.new_ticket, Vec::new());
    if ticket.is_empty() && sess.handshake_data.resuming_session.is_some() {
        ticket = sess.handshake_data.resuming_session.as_mut().unwrap().take_ticket();
    }

    if sess.handshake_data.session_id.is_empty() && ticket.is_empty() {
        info!("Session not saved: server didn't allocate id or ticket");
        return;
    }

    let key = persist::ClientSessionKey::session_for_dns_name(&sess.handshake_data.dns_name);

    let scs = sess.common.get_suite();
    let master_secret = sess.secrets.as_ref().unwrap().get_master_secret();
    let version = sess.get_protocol_version().unwrap();
    let mut value = persist::ClientSessionValue::new(version,
                                                     scs.suite,
                                                     &sess.handshake_data.session_id,
                                                     ticket,
                                                     master_secret);
    value.set_times(ticket_timebase(),
                    sess.handshake_data.new_ticket_lifetime,
                    0);

    let mut persist = sess.config.session_persistence.lock().unwrap();
    let worked = persist.put(key.get_encoding(), value.get_encoding());

    if worked {
        info!("Session saved");
    } else {
        info!("Session not saved");
    }
}

fn emit_certificate_tls13(sess: &mut ClientSessionImpl) {
    let context = sess.handshake_data
        .client_auth_context
        .take()
        .unwrap_or_else(|| Vec::new());

    let mut cert_payload = CertificatePayloadTLS13 {
        context: PayloadU8::new(context),
        list: Vec::new(),
    };

    if let Some(cert_chain) = sess.handshake_data.client_auth_cert.take() {
        for cert in cert_chain {
            cert_payload.list.push(CertificateEntry::new(cert));
        }
    }

    let m = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_3,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::Certificate,
            payload: HandshakePayload::CertificateTLS13(cert_payload),
        }),
    };
    sess.handshake_data.transcript.add_message(&m);
    sess.common.send_msg(m, true);
}

fn emit_certverify_tls13(sess: &mut ClientSessionImpl) -> Result<(), TLSError> {
    if sess.handshake_data.client_auth_sigscheme.is_none() ||
       sess.handshake_data.client_auth_key.is_none() {
        info!("Skipping certverify message (no client scheme/key)");
        return Ok(());
    }

    let mut message = Vec::new();
    message.resize(64, 0x20u8);
    message.extend_from_slice(b"TLS 1.3, client CertificateVerify\x00");
    message.extend_from_slice(&sess.handshake_data.transcript.get_current_hash());

    let scheme = sess.handshake_data.client_auth_sigscheme.take().unwrap();
    let key = sess.handshake_data.client_auth_key.take().unwrap();
    let sig = try! {
        key.sign(scheme, &message)
            .map_err(|_| TLSError::General("cannot sign".to_string()))
    };
    let verf = DigitallySignedStruct::new(scheme, sig);

    let m = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_3,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::CertificateVerify,
            payload: HandshakePayload::CertificateVerify(verf),
        }),
    };

    sess.handshake_data.transcript.add_message(&m);
    sess.common.send_msg(m, true);
    Ok(())
}

fn emit_finished_tls13(sess: &mut ClientSessionImpl) {
    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    let verify_data = sess.common
        .get_key_schedule()
        .sign_finish(SecretKind::ClientHandshakeTrafficSecret, &handshake_hash);
    let verify_data_payload = Payload::new(verify_data);

    let m = Message {
        typ: ContentType::Handshake,
        version: ProtocolVersion::TLSv1_3,
        payload: MessagePayload::Handshake(HandshakeMessagePayload {
            typ: HandshakeType::Finished,
            payload: HandshakePayload::Finished(verify_data_payload),
        }),
    };

    sess.handshake_data.transcript.add_message(&m);
    sess.common.send_msg(m, true);
}

fn handle_finished_tls13(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let finished = extract_handshake!(m, HandshakePayload::Finished).unwrap();

    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    let expect_verify_data = sess.common
        .get_key_schedule()
        .sign_finish(SecretKind::ServerHandshakeTrafficSecret, &handshake_hash);

    use ring;
    try! {
        ring::constant_time::verify_slices_are_equal(&expect_verify_data, &finished.0)
            .map_err(|_| {
                     sess.common.send_fatal_alert(AlertDescription::DecryptError);
                     TLSError::DecryptError
                     })
    };

    sess.handshake_data.transcript.add_message(&m);

    /* Transition to application data */
    sess.common.get_mut_key_schedule().input_empty();

    /* Traffic from server is now encrypted with application data keys. */
    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    let read_key = sess.common
        .get_key_schedule()
        .derive(SecretKind::ServerApplicationTrafficSecret, &handshake_hash);
    let suite = sess.common.get_suite();
    sess.common.set_message_decrypter(cipher::new_tls13_read(suite, &read_key));
    sess.common
        .get_mut_key_schedule()
        .current_server_traffic_secret = read_key;

    /* Send our authentication/finished messages.  These are still encrypted
     * with our handshake keys. */
    if sess.handshake_data.doing_client_auth {
        emit_certificate_tls13(sess);
        try!(emit_certverify_tls13(sess));
    }

    emit_finished_tls13(sess);

    /* Now move to our application traffic keys. */
    try!(check_aligned_handshake(sess));
    let write_key = sess.common
        .get_key_schedule()
        .derive(SecretKind::ClientApplicationTrafficSecret, &handshake_hash);
    sess.common.set_message_encrypter(cipher::new_tls13_write(suite, &write_key));
    sess.common
        .get_mut_key_schedule()
        .current_client_traffic_secret = write_key;

    Ok(ConnState::TrafficTLS13)
}

fn handle_finished_tls12(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let finished = extract_handshake!(m, HandshakePayload::Finished).unwrap();

    // Work out what verify_data we expect.
    let vh = sess.handshake_data.transcript.get_current_hash();
    let expect_verify_data = sess.secrets.as_ref().unwrap().server_verify_data(&vh);

    // Constant-time verification of this is relatively unimportant: they only
    // get one chance.  But it can't hurt.
    use ring;
    try! {
        ring::constant_time::verify_slices_are_equal(&expect_verify_data, &finished.0)
            .map_err(|_| {
                     sess.common.send_fatal_alert(AlertDescription::DecryptError);
                     TLSError::DecryptError
                     })
    };

    // Hash this message too.
    sess.handshake_data.transcript.add_message(&m);

    save_session(sess);

    Ok(ConnState::TrafficTLS12)
}

fn handle_finished(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    if sess.common.is_tls13 {
        handle_finished_tls13(sess, m)
    } else {
        handle_finished_tls12(sess, m)
    }
}

fn handle_finished_resume(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    let next_state = try!(handle_finished(sess, m));

    emit_ccs(sess);
    emit_finished(sess);
    Ok(next_state)
}

pub static EXPECT_FINISHED: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[HandshakeType::Finished],
    },
    handle: handle_finished,
};

pub static EXPECT_FINISHED_RESUME: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::Handshake],
        handshake_types: &[],
    },
    handle: handle_finished_resume,
};

// -- Traffic transit state --
fn handle_traffic(sess: &mut ClientSessionImpl, mut m: Message) -> Result<ConnState, TLSError> {
    sess.common.take_received_plaintext(m.take_opaque_payload().unwrap());
    Ok(ConnState::TrafficTLS12)
}

pub static TRAFFIC_TLS12: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::ApplicationData],
        handshake_types: &[],
    },
    handle: handle_traffic,
};

// -- Traffic transit state (TLS1.3) --
// In this state we can be sent tickets, keyupdates,
// and application data.
fn handle_traffic_tls13(sess: &mut ClientSessionImpl, m: Message) -> Result<ConnState, TLSError> {
    if m.is_content_type(ContentType::ApplicationData) {
        try!(handle_traffic(sess, m));
    } else if m.is_handshake_type(HandshakeType::NewSessionTicket) {
        try!(handle_new_ticket_tls13(sess, m));
    } else if m.is_handshake_type(HandshakeType::KeyUpdate) {
        try!(handle_key_update(sess, m));
    }

    Ok(ConnState::TrafficTLS13)
}

fn handle_new_ticket_tls13(sess: &mut ClientSessionImpl, m: Message) -> Result<(), TLSError> {
    let nst = extract_handshake!(m, HandshakePayload::NewSessionTicketTLS13).unwrap();
    let handshake_hash = sess.handshake_data.transcript.get_current_hash();
    let secret =
        sess.common.get_key_schedule().derive(SecretKind::ResumptionMasterSecret, &handshake_hash);
    let mut value = persist::ClientSessionValue::new(ProtocolVersion::TLSv1_3,
                                                     sess.common.get_suite().suite,
                                                     &SessionID::empty(),
                                                     nst.ticket.0.clone(),
                                                     secret);
    value.set_times(ticket_timebase(),
                    nst.lifetime,
                    nst.age_add);

    let key = persist::ClientSessionKey::session_for_dns_name(&sess.handshake_data.dns_name);

    let mut persist = sess.config.session_persistence.lock().unwrap();
    let worked = persist.put(key.get_encoding(), value.get_encoding());

    if worked {
        info!("Ticket saved");
    } else {
        info!("Ticket not saved");
    }
    Ok(())
}

fn handle_key_update(sess: &mut ClientSessionImpl, m: Message) -> Result<(), TLSError> {
    let kur = extract_handshake!(m, HandshakePayload::KeyUpdate).unwrap();
    sess.common.process_key_update(kur, SecretKind::ServerApplicationTrafficSecret)
}

pub static TRAFFIC_TLS13: Handler = Handler {
    expect: Expectation {
        content_types: &[ContentType::ApplicationData, ContentType::Handshake],
        handshake_types: &[HandshakeType::NewSessionTicket, HandshakeType::KeyUpdate],
    },
    handle: handle_traffic_tls13,
};