aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/coro/async_crypto_socket.cpp
blob: 51061c98428797724f1ab922214d8cfcbba5adad (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "async_crypto_socket.h"
#include <vespa/vespalib/net/tls/protocol_snooping.h>
#include <vespa/vespalib/net/tls/tls_crypto_engine.h>
#include <vespa/vespalib/net/tls/crypto_codec.h>
#include <vespa/vespalib/data/smart_buffer.h>

namespace vespalib::coro {

namespace {

using net::tls::CryptoCodec;
using net::tls::HandshakeResult;
using net::tls::EncodeResult;
using net::tls::DecodeResult;

struct InvalidSocket : AsyncCryptoSocket {
    Lazy<ssize_t> read(char *, size_t) override { co_return -EINVAL; }
    Lazy<ssize_t> write(const char *, size_t) override { co_return -EINVAL; }
};

struct RawSocket : AsyncCryptoSocket {
    AsyncIo::SP async;
    SocketHandle handle;
    RawSocket(AsyncIo &async_in, SocketHandle handle_in)
        : async(async_in.shared_from_this()), handle(std::move(handle_in)) {}
    RawSocket(RawSocket &&) noexcept;
    ~RawSocket() override;
    Lazy<ssize_t> read(char *buf, size_t len) override {
        return async->read(handle, buf, len);
    }
    Lazy<ssize_t> write(const char *buf, size_t len) override {
        return async->write(handle, buf, len);
    }
};

RawSocket::~RawSocket() = default;

struct SnoopedRawSocket : AsyncCryptoSocket {
    AsyncIo::SP async;
    SocketHandle handle;
    SmartBuffer data;
    SnoopedRawSocket(AsyncIo &async_in, SocketHandle handle_in)
        : async(async_in.shared_from_this()), handle(std::move(handle_in)), data(0) {}
    SnoopedRawSocket(SnoopedRawSocket &&) noexcept;
    ~SnoopedRawSocket() override;
    void inject_data(const char *buf, size_t len) {
        if (len > 0) {
            auto dst = data.reserve(len);
            memcpy(dst.data, buf, len);
            data.commit(len);
        }
    }
    Lazy<ssize_t> read_from_buffer(char *buf, size_t len) {
        auto src = data.obtain();
        size_t frame = std::min(len, src.size);
        if (frame > 0) {
            memcpy(buf, src.data, frame);
            data.evict(frame);
            data.drop_if_empty();
        }
        co_return frame;
    }
    Lazy<ssize_t> read(char *buf, size_t len) override {
        if (data.empty()) {
            return async->read(handle, buf, len);
        } else {
            return read_from_buffer(buf, len);
        }
    }
    Lazy<ssize_t> write(const char *buf, size_t len) override {
        return async->write(handle, buf, len);
    }
};

SnoopedRawSocket::~SnoopedRawSocket() = default;

struct TlsSocket : AsyncCryptoSocket {
    AsyncIo::SP async;
    SocketHandle handle;
    std::unique_ptr<CryptoCodec> codec;
    SmartBuffer app_input;
    SmartBuffer enc_input;
    SmartBuffer enc_output;
    TlsSocket(AsyncIo &async_in, SocketHandle handle_in, std::unique_ptr<CryptoCodec> codec_in)
      : async(async_in.shared_from_this()), handle(std::move(handle_in)), codec(std::move(codec_in)),
        app_input(0), enc_input(0), enc_output(0) {}
    TlsSocket(TlsSocket &&) noexcept;
    ~TlsSocket() override;
    void inject_enc_input(const char *buf, size_t len) {
        if (len > 0) {
            auto dst = enc_input.reserve(len);
            memcpy(dst.data, buf, len);
            enc_input.commit(len);
        }
    }
    Lazy<bool> flush_enc_output() {
        while (!enc_output.empty()) {
            auto pending = enc_output.obtain();
            auto res = co_await async->write(handle, pending.data, pending.size);
            if (res > 0) {
                enc_output.evict(res);
            } else {
                co_return false;
            }
        }
        co_return true;
    }
    Lazy<bool> fill_enc_input() {
        auto dst = enc_input.reserve(codec->min_encode_buffer_size());
        ssize_t res = co_await async->read(handle, dst.data, dst.size);
        if (res > 0) {
            enc_input.commit(res);
            co_return true;
        } else {
            co_return false;
        }
    }
    Lazy<bool> handshake() {
        for (;;) {
            auto in = enc_input.obtain();
            auto out = enc_output.reserve(codec->min_encode_buffer_size());
            auto hs_res = codec->handshake(in.data, in.size, out.data, out.size);
            enc_input.evict(hs_res.bytes_consumed);
            enc_output.commit(hs_res.bytes_produced);
            switch (hs_res.state) {
            case ::vespalib::net::tls::HandshakeResult::State::Failed: co_return false;
            case ::vespalib::net::tls::HandshakeResult::State::Done: co_return co_await flush_enc_output();
            case ::vespalib::net::tls::HandshakeResult::State::NeedsWork:
                codec->do_handshake_work();
                break;
            case ::vespalib::net::tls::HandshakeResult::State::NeedsMorePeerData:
                bool flush_ok = co_await flush_enc_output();
                if (!flush_ok) {
                    co_return false;
                }
                bool fill_ok = co_await fill_enc_input();
                if (!fill_ok) {
                    co_return false;
                }
            }
        }
    }
    Lazy<ssize_t> read(char *buf, size_t len) override {
        while (app_input.empty()) {
            auto src = enc_input.obtain();
            auto dst = app_input.reserve(codec->min_decode_buffer_size());
            auto res = codec->decode(src.data, src.size, dst.data, dst.size);
            app_input.commit(res.bytes_produced);
            enc_input.evict(res.bytes_consumed);
            if (res.failed()) {
                co_return -EIO;
            }
            if (res.closed()) {
                co_return 0;
            }
            if (app_input.empty()) {
                bool fill_ok = co_await fill_enc_input();
                if (!fill_ok) {
                    co_return -EIO;
                }
            }
        }
        auto src = app_input.obtain();
        size_t frame = std::min(len, src.size);
        if (frame > 0) {
            memcpy(buf, src.data, frame);
            app_input.evict(frame);
        }
        co_return frame;
    }
    Lazy<ssize_t> write(const char *buf, size_t len) override {
        auto dst = enc_output.reserve(codec->min_encode_buffer_size());
        auto res = codec->encode(buf, len, dst.data, dst.size);
        if (res.failed) {
            co_return -EIO;
        }
        enc_output.commit(res.bytes_produced);
        bool flush_ok = co_await flush_enc_output();
        if (!flush_ok) {
            co_return -EIO;
        }
        co_return res.bytes_consumed;
    }
};

TlsSocket::~TlsSocket() = default;

Lazy<AsyncCryptoSocket::UP> try_handshake(std::unique_ptr<TlsSocket> tls_socket) {
    bool hs_ok = co_await tls_socket->handshake();
    if (hs_ok) {
        co_return std::move(tls_socket);
    } else {
        co_return std::make_unique<InvalidSocket>();
    }
}

Lazy<AsyncCryptoSocket::UP> accept_tls(AsyncIo &async, AbstractTlsCryptoEngine &crypto, SocketHandle handle) {
    auto tls_codec = crypto.create_tls_server_crypto_codec(handle);
    auto tls_socket = std::make_unique<TlsSocket>(async, std::move(handle), std::move(tls_codec));
    co_return co_await try_handshake(std::move(tls_socket));
}

Lazy<AsyncCryptoSocket::UP> accept_maybe_tls(AsyncIo &async, AbstractTlsCryptoEngine &crypto, SocketHandle handle) {
    char buf[net::tls::snooping::min_header_bytes_to_observe()];
    memset(buf, 0, sizeof(buf));
    size_t snooped = 0;
    while (snooped < sizeof(buf)) {
        auto res = co_await async.read(handle, buf + snooped, sizeof(buf) - snooped);
        if (res <= 0) {
            co_return std::make_unique<InvalidSocket>();
        }
        snooped += res;
    }
    if (net::tls::snooping::snoop_client_hello_header(buf) == net::tls::snooping::TlsSnoopingResult::ProbablyTls) {
        auto tls_codec = crypto.create_tls_server_crypto_codec(handle);
        auto tls_socket = std::make_unique<TlsSocket>(async, std::move(handle), std::move(tls_codec));
        tls_socket->inject_enc_input(buf, snooped);
        co_return co_await try_handshake(std::move(tls_socket));
    } else {
        auto plain_socket = std::make_unique<SnoopedRawSocket>(async, std::move(handle));
        plain_socket->inject_data(buf, snooped);
        co_return plain_socket;
    }
}

Lazy<AsyncCryptoSocket::UP> connect_tls(AsyncIo &async, AbstractTlsCryptoEngine &crypto, SocketHandle handle, SocketSpec spec) {
    auto tls_codec = crypto.create_tls_client_crypto_codec(handle, spec);
    auto tls_socket = std::make_unique<TlsSocket>(async, std::move(handle), std::move(tls_codec));
    co_return co_await try_handshake(std::move(tls_socket));
}

}

AsyncCryptoSocket::~AsyncCryptoSocket() = default;

Lazy<AsyncCryptoSocket::UP>
AsyncCryptoSocket::accept(AsyncIo &async, CryptoEngine &crypto,
                          SocketHandle handle)
{
    if (dynamic_cast<NullCryptoEngine*>(&crypto)) {
        co_return std::make_unique<RawSocket>(async, std::move(handle));
    }
    if (auto *tls_engine = dynamic_cast<AbstractTlsCryptoEngine*>(&crypto)) {
        if (tls_engine->always_use_tls_when_server()) {
            co_return co_await accept_tls(async, *tls_engine, std::move(handle));
        } else {
            co_return co_await accept_maybe_tls(async, *tls_engine, std::move(handle));
        }
    }
    co_return std::make_unique<InvalidSocket>();
}

Lazy<AsyncCryptoSocket::UP>
AsyncCryptoSocket::connect(AsyncIo &async, CryptoEngine &crypto,
                           SocketHandle handle, SocketSpec spec)
{
    if (dynamic_cast<NullCryptoEngine*>(&crypto)) {
        (void) spec; // no SNI for plaintext sockets
        co_return std::make_unique<RawSocket>(async, std::move(handle));
    }
    if (auto *tls_engine = dynamic_cast<AbstractTlsCryptoEngine*>(&crypto)) {
        if (tls_engine->use_tls_when_client()) {
            co_return co_await connect_tls(async, *tls_engine, std::move(handle), spec);
        } else {
            co_return std::make_unique<RawSocket>(async, std::move(handle));
        }
    }
    co_return std::make_unique<InvalidSocket>();
}

}