summaryrefslogtreecommitdiff
path: root/Libraries/LibTLS/Socket.cpp
blob: 6b0488104d4add644e359e628bbc8246cd6a639e (plain)
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
/*
 * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <LibCore/Timer.h>
#include <LibCrypto/ASN1/DER.h>
#include <LibCrypto/PK/Code/EMSA_PSS.h>
#include <LibTLS/TLSv12.h>

namespace TLS {

Optional<ByteBuffer> TLSv12::read()
{
    if (m_context.application_buffer.size()) {
        auto buf = m_context.application_buffer.slice(0, m_context.application_buffer.size());
        m_context.application_buffer.clear();
        return buf;
    }
    return {};
}

ByteBuffer TLSv12::read(size_t max_size)
{
    if (m_context.application_buffer.size()) {
        auto length = min(m_context.application_buffer.size(), max_size);
        auto buf = m_context.application_buffer.slice(0, length);
        m_context.application_buffer = m_context.application_buffer.slice(length, m_context.application_buffer.size() - length);
        return buf;
    }
    return {};
}

ByteBuffer TLSv12::read_line(size_t max_size)
{
    if (!can_read_line())
        return {};

    auto* start = m_context.application_buffer.data();
    auto* newline = (u8*)memchr(m_context.application_buffer.data(), '\n', m_context.application_buffer.size());
    ASSERT(newline);

    size_t offset = newline - start;

    if (offset > max_size)
        return {};

    auto buffer = ByteBuffer::copy(start, offset);
    m_context.application_buffer = m_context.application_buffer.slice(offset + 1, m_context.application_buffer.size() - offset - 1);

    return buffer;
}

bool TLSv12::write(const ByteBuffer& buffer)
{
    if (m_context.connection_status != ConnectionStatus::Established) {
        dbg() << "write request while not connected";
        return false;
    }

    PacketBuilder builder { MessageType::ApplicationData, m_context.version, buffer.size() };
    builder.append(buffer);
    auto packet = builder.build();

    update_packet(packet);
    write_packet(packet);

    return true;
}

bool TLSv12::connect(const String& hostname, int port)
{
    set_sni(hostname);
    return Core::Socket::connect(hostname, port);
}

bool TLSv12::common_connect(const struct sockaddr* saddr, socklen_t length)
{
    if (m_context.critical_error)
        return false;

    if (Core::Socket::is_connected()) {
        if (is_established()) {
            ASSERT_NOT_REACHED();
        } else {
            Core::Socket::close(); // reuse?
        }
    }

    auto packet = build_hello();
    write_packet(packet);

    Core::Socket::on_connected = [this] {
        Core::Socket::on_ready_to_read = [this] {
            if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
                // an abrupt closure (the server is a jerk)
                dbg() << "Socket not open, assuming abrupt closure";
                m_context.connection_finished = true;
            }
            if (m_context.critical_error) {
                dbg() << "READ CRITICAL ERROR " << m_context.critical_error << " :(";
                if (on_tls_error)
                    on_tls_error((AlertDescription)m_context.critical_error);
                return;
            }
            if (m_context.application_buffer.size() == 0 && m_context.connection_finished) {
                if (on_tls_finished)
                    on_tls_finished();
                if (m_context.tls_buffer.size()) {
                    dbg() << "connection closed without finishing data transfer, " << m_context.tls_buffer.size() << " bytes still in buffer";
                } else {
                    m_context.connection_finished = false;
                }
                if (!m_context.application_buffer.size())
                    m_context.connection_status = ConnectionStatus::Disconnected;
                return;
            }
            flush();
            consume(Core::Socket::read(4096)); // FIXME: how much is proper?
            if (is_established() && m_context.application_buffer.size())
                if (on_tls_ready_to_read)
                    on_tls_ready_to_read(*this);
        };
        Core::Socket::on_ready_to_write = [this] {
            if (!Core::Socket::is_open() || !Core::Socket::is_connected() || Core::Socket::eof()) {
                // an abrupt closure (the server is a jerk)
                dbg() << "Socket not open, assuming abrupt closure";
                m_context.connection_finished = true;
            }
            if (m_context.critical_error) {
                dbg() << "WRITE CRITICAL ERROR " << m_context.critical_error << " :(";
                if (on_tls_error)
                    on_tls_error((AlertDescription)m_context.critical_error);
                return;
            }
            if (m_context.connection_finished) {
                if (on_tls_finished)
                    on_tls_finished();
                if (m_context.tls_buffer.size()) {
                    dbg() << "connection closed without finishing data transfer, " << m_context.tls_buffer.size() << " bytes still in buffer";
                } else {
                    m_context.connection_finished = false;
                    dbg() << "FINISHED";
                }
                if (!m_context.application_buffer.size()) {
                    m_context.connection_status = ConnectionStatus::Disconnected;
                    return;
                }
            }
            flush();
            if (is_established() && !m_context.application_buffer.size()) // hey client, you still have stuff to read...
                if (on_tls_ready_to_write)
                    on_tls_ready_to_write(*this);
        };
        if (on_tls_connected)
            on_tls_connected();
    };
    bool success = Core::Socket::common_connect(saddr, length);
    if (!success)
        return false;

    return true;
}

bool TLSv12::flush()
{
    auto out_buffer = write_buffer().data();
    size_t out_buffer_index { 0 };
    size_t out_buffer_length = write_buffer().size();

    if (out_buffer_length == 0)
        return true;

#ifdef TLS_DEBUG
    dbg() << "SENDING...";
    print_buffer(out_buffer, out_buffer_length);
#endif
    if (Core::Socket::write(&out_buffer[out_buffer_index], out_buffer_length)) {
        write_buffer().clear();
        return true;
    }
    if (m_context.send_retries++ == 10) {
        // drop the records, we can't send
        dbg() << "Dropping " << write_buffer().size() << " bytes worth of TLS records as max retries has been reached";
        write_buffer().clear();
        m_context.send_retries = 0;
    }
    return false;
}

}