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
use std::io::ErrorKind;

use mio::net::*;
use rustls::ServerConnection;
use rusty_ulid::Ulid;

use crate::{protocol::ProtocolResult, Readiness, Ready, SessionResult};

pub enum TlsState {
    Initial,
    Handshake,
    Established,
    Error,
}

pub struct TlsHandshake {
    pub stream: TcpStream,
    pub session: ServerConnection,
    pub readiness: Readiness,
    pub request_id: Ulid,
}

impl TlsHandshake {
    pub fn new(session: ServerConnection, stream: TcpStream, request_id: Ulid) -> TlsHandshake {
        TlsHandshake {
            stream,
            session,
            readiness: Readiness {
                interest: Ready::readable() | Ready::hup() | Ready::error(),
                event: Ready::empty(),
            },
            request_id,
        }
    }

    pub fn readable(&mut self) -> (ProtocolResult, SessionResult) {
        let mut can_read = true;

        loop {
            let mut can_work = false;

            if self.session.wants_read() && can_read {
                can_work = true;

                match self.session.read_tls(&mut self.stream) {
                    Ok(0) => {
                        error!("connection closed during handshake");
                        return (ProtocolResult::Continue, SessionResult::CloseSession);
                    }
                    Ok(_) => {}
                    Err(e) => match e.kind() {
                        ErrorKind::WouldBlock => {
                            self.readiness.event.remove(Ready::readable());
                            can_read = false
                        }
                        _ => {
                            error!("could not perform handshake: {:?}", e);
                            return (ProtocolResult::Continue, SessionResult::CloseSession);
                        }
                    },
                }

                if let Err(e) = self.session.process_new_packets() {
                    error!("could not perform handshake: {:?}", e);
                    return (ProtocolResult::Continue, SessionResult::CloseSession);
                }
            }

            if !can_work {
                break;
            }
        }

        if !self.session.wants_read() {
            self.readiness.interest.remove(Ready::readable());
        }

        if self.session.wants_write() {
            self.readiness.interest.insert(Ready::writable());
        }

        if self.session.is_handshaking() {
            (ProtocolResult::Continue, SessionResult::Continue)
        } else {
            // handshake might be finished but we still have something to send
            if self.session.wants_write() {
                (ProtocolResult::Continue, SessionResult::Continue)
            } else {
                self.readiness.interest.insert(Ready::readable());
                self.readiness.event.insert(Ready::readable());
                self.readiness.interest.insert(Ready::writable());
                (ProtocolResult::Upgrade, SessionResult::Continue)
            }
        }
    }

    pub fn writable(&mut self) -> (ProtocolResult, SessionResult) {
        let mut can_write = true;

        loop {
            let mut can_work = false;

            if self.session.wants_write() && can_write {
                can_work = true;

                match self.session.write_tls(&mut self.stream) {
                    Ok(_) => {}
                    Err(e) => match e.kind() {
                        ErrorKind::WouldBlock => {
                            self.readiness.event.remove(Ready::writable());
                            can_write = false
                        }
                        _ => {
                            error!("could not perform handshake: {:?}", e);
                            return (ProtocolResult::Continue, SessionResult::CloseSession);
                        }
                    },
                }

                if let Err(e) = self.session.process_new_packets() {
                    error!("could not perform handshake: {:?}", e);
                    return (ProtocolResult::Continue, SessionResult::CloseSession);
                }
            }

            if !can_work {
                break;
            }
        }

        if !self.session.wants_write() {
            self.readiness.interest.remove(Ready::writable());
        }

        if self.session.wants_read() {
            self.readiness.interest.insert(Ready::readable());
        }

        if self.session.is_handshaking() {
            (ProtocolResult::Continue, SessionResult::Continue)
        } else if self.session.wants_read() {
            self.readiness.interest.insert(Ready::readable());
            (ProtocolResult::Upgrade, SessionResult::Continue)
        } else {
            self.readiness.interest.insert(Ready::writable());
            self.readiness.interest.insert(Ready::readable());
            (ProtocolResult::Upgrade, SessionResult::Continue)
        }
    }
}