summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/ssl/detail/impl/engine_apple.ipp
blob: b0d22696a3538475cd9e68f6370adde42cc168f5 (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
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

/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#pragma once

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kNetwork

#include "asio/detail/config.hpp"

#include "asio/detail/push_options.hpp"
#include "asio/detail/throw_error.hpp"
#include "asio/error.hpp"

#include <arpa/inet.h>

#include "mongo/logv2/log.h"
#include "mongo/util/net/ssl/apple.hpp"
#include "mongo/util/net/ssl/detail/engine.hpp"
#include "mongo/util/net/ssl/detail/stream_core.hpp"
#include "mongo/util/net/ssl/error.hpp"
#include "mongo/util/str.h"

namespace asio {
namespace ssl {
namespace detail {

namespace {
// Limit size of output buffer to avoid growing indefinitely.
constexpr auto max_outbuf_size = stream_core::max_tls_record_size;

const class osstatus_category : public error_category {
public:
    const char* name() const noexcept final {
        return "Secure.Transport";
    }

    std::string message(int value) const noexcept final {
        const auto status = static_cast<::OSStatus>(value);
        apple::CFUniquePtr<::CFStringRef> errstr(::SecCopyErrorMessageString(status, nullptr));
        if (!errstr) {
            return mongo::str::stream()
                << "Secure.Transport unknown error: " << static_cast<int>(status);
        }
        const auto len = ::CFStringGetMaximumSizeForEncoding(::CFStringGetLength(errstr.get()),
                                                             ::kCFStringEncodingUTF8);
        std::string ret;
        ret.resize(len + 1);
        if (!::CFStringGetCString(errstr.get(), &ret[0], len, ::kCFStringEncodingUTF8)) {
            return mongo::str::stream()
                << "Secure.Transport unknown error: " << static_cast<int>(status);
        }

        ret.resize(strlen(ret.c_str()));
        return mongo::str::stream() << "Secure.Transport: " << ret;
    }
} OSStatus_category;

asio::error_code errorCode(::OSStatus status) {
    return asio::error_code(static_cast<int>(status), OSStatus_category);
}

/**
 * Verify that an SSL session is ready for I/O (state: Connected).
 * In all other states, asio should be speaking to the socket directly.
 */
bool verifyConnected(::SSLContextRef ssl, asio::error_code* ec) {
    auto state = ::kSSLAborted;
    auto status = ::SSLGetSessionState(ssl, &state);
    if (status != ::errSecSuccess) {
        // Unable to determine session state.
        *ec = errorCode(status);
        return false;
    }
    switch (state) {
        case ::kSSLIdle:
            *ec = asio::error::not_connected;
            return false;
        case ::kSSLHandshake:
            *ec = asio::error::in_progress;
            return false;
        case ::kSSLConnected:
            return true;
        case ::kSSLClosed:
            *ec = asio::error::shut_down;
            return false;
        case ::kSSLAborted:
            *ec = asio::error::connection_aborted;
            return false;
        default:
            // Undefined state, call it an internal error.
            *ec = errorCode(::errSSLInternal);
            return false;
    }
}

}  // namespace

engine::engine(context::native_handle_type context, const std::string& remoteHostName)
    : _remoteHostName(remoteHostName) {
    if (context) {
        if (context->certs) {
            ::CFRetain(context->certs.get());
            _certs.reset(context->certs.get());
        }
        _protoMin = context->protoMin;
        _protoMax = context->protoMax;
        if (context->allowInvalidHostnames) {
            _remoteHostName.clear();
        }
    } else {
        apple::Context def;
        _protoMin = def.protoMin;
        _protoMax = def.protoMax;
    }
}

bool engine::_initSSL(stream_base::handshake_type type, asio::error_code& ec) {
    if (_ssl) {
        return true;
    }

    const auto side = (type == stream_base::client) ? ::kSSLClientSide : ::kSSLServerSide;
    _ssl.reset(::SSLCreateContext(nullptr, side, ::kSSLStreamType));
    if (!_ssl) {
        LOGV2_ERROR(24140, "Failed allocating SSLContext");
        ec = errorCode(::errSSLInternal);
        return false;
    }

    auto status = ::SSLSetConnection(_ssl.get(), static_cast<void*>(this));

    if (_certs && (status == ::errSecSuccess)) {
        status = ::SSLSetCertificate(_ssl.get(), _certs.get());
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetPeerID(_ssl.get(), _ssl.get(), sizeof(native_handle_type));
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetIOFuncs(_ssl.get(), read_func, write_func);
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetProtocolVersionMin(_ssl.get(), _protoMin);
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetProtocolVersionMax(_ssl.get(), _protoMax);
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetClientSideAuthenticate(_ssl.get(), ::kTryAuthenticate);
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetSessionOption(_ssl.get(), ::kSSLSessionOptionBreakOnServerAuth, true);
    }

    if (status == ::errSecSuccess) {
        status = ::SSLSetSessionOption(_ssl.get(), ::kSSLSessionOptionBreakOnClientAuth, true);
    }

    if (!_remoteHostName.empty() && (status == ::errSecSuccess)) {
        status =
            ::SSLSetPeerDomainName(_ssl.get(), _remoteHostName.c_str(), _remoteHostName.size());
    }

    if (status != ::errSecSuccess) {
        _ssl.reset(nullptr);
        ec = errorCode(status);
        return false;
    }

    return true;
}

engine::want engine::handshake(stream_base::handshake_type type, asio::error_code& ec) {
    ec = asio::error_code();
    if (!_initSSL(type, ec)) {
        // Error happened, ec has been set.
        return want::want_nothing;
    }

    // We use BreakOnClientAuth and BreakOnServerAuth above to
    // convince the OS not to validate the certs for us.
    // In practice, we'll be validating the peer in ssl_manager_apple.cpp later.
    // As a side effect, we have to call SSLHandshake up to three times.
    // Breaking once for client auth, then for server auth, and finally on completion.
    ::OSStatus status;
    do {
        status = ::SSLHandshake(_ssl.get());
    } while ((status == ::errSSLServerAuthCompleted) || (status == ::errSSLClientAuthCompleted));

    if (status == ::errSSLWouldBlock) {
        return wouldBlock();
    }

    if (status != ::errSecSuccess) {
        _ssl.reset(nullptr);
        ec = errorCode(status);
        return want::want_nothing;
    }

    return _outbuf.size() ? want::want_output : want::want_nothing;
}

engine::want engine::shutdown(asio::error_code& ec) {
    ec = asio::error_code();
    if (_ssl) {
        const auto status = ::SSLClose(_ssl.get());
        if (status == ::errSSLWouldBlock) {
            return wouldBlock();
        }
        if (status == ::errSecSuccess) {
            _ssl.reset(nullptr);
        } else {
            ec = errorCode(status);
        }
    } else {
        LOGV2_ERROR(24141, "SSL connection already shut down");
        ec = errorCode(::errSSLInternal);
    }
    return want::want_nothing;
}

const asio::error_code& engine::map_error_code(asio::error_code& ec) const {
    if (ec != asio::error::eof) {
        return ec;
    }

    if (_inbuf.size() || _outbuf.size()) {
        ec = asio::ssl::error::stream_truncated;
        return ec;
    }

    invariant(_ssl);
    auto state = ::kSSLAborted;
    const auto status = ::SSLGetSessionState(_ssl.get(), &state);
    if (status != ::errSecSuccess) {
        ec = errorCode(status);
        return ec;
    }

    if (state == ::kSSLConnected) {
        ec = asio::ssl::error::stream_truncated;
        return ec;
    }

    return ec;
}

engine::want engine::write(const asio::const_buffer& data,
                           asio::error_code& ec,
                           std::size_t& bytes_transferred) {
    ec = asio::error_code();
    if (!verifyConnected(_ssl.get(), &ec)) {
        return want::want_nothing;
    }
    const auto status = ::SSLWrite(_ssl.get(), data.data(), data.size(), &bytes_transferred);
    if (status == ::errSSLWouldBlock) {
        return (bytes_transferred < data.size()) ? want::want_output_and_retry : want::want_nothing;
    }
    if (status != ::errSecSuccess) {
        ec = errorCode(status);
    }
    return _outbuf.size() ? want::want_output : want::want_nothing;
}

asio::mutable_buffer engine::get_output(const asio::mutable_buffer& data) {
    const auto len = std::min<size_t>(data.size(), _outbuf.size());
    if (len > 0) {
        auto* p = const_cast<char*>(static_cast<const char*>(data.data()));
        std::copy(_outbuf.begin(), _outbuf.begin() + len, p);
        _outbuf.erase(_outbuf.begin(), _outbuf.begin() + len);
    }

    return asio::mutable_buffer(data.data(), len);
}

::OSStatus engine::write_func(::SSLConnectionRef ctx, const void* data, size_t* data_len) {
    auto* this_ = const_cast<engine*>(static_cast<const engine*>(ctx));
    const auto* p = static_cast<const char*>(data);

    const auto requested = *data_len;
    *data_len = std::min<size_t>(requested, max_outbuf_size - this_->_outbuf.size());
    this_->_outbuf.insert(this_->_outbuf.end(), p, p + *data_len);
    return (requested == *data_len) ? ::errSecSuccess : ::errSSLWouldBlock;
}

boost::optional<std::string> engine::get_sni() {
    if (_sni) {
        return _sni;
    }

    size_t len = 0;
    auto status = ::SSLCopyRequestedPeerNameLength(_ssl.get(), &len);
    if (status != ::errSecSuccess) {
        _sni = boost::none;
        return _sni;
    }

    std::string sni;
    sni.resize(len + 1);
    status = ::SSLCopyRequestedPeerName(_ssl.get(), sni.data(), &len);
    if (status != ::errSecSuccess) {
        _sni = boost::none;
        return _sni;
    }

    sni.resize(len);

    // ::SSLCopyRequestedPeerName includes space for a null byte at the end of the string it writes.
    // We do not want to include this null byte in the advertised SNI name
    while (!sni.empty() && sni.back() == '\0') {
        sni.pop_back();
    }

    _sni = sni;
    return _sni;
}

engine::want engine::read(const asio::mutable_buffer& data,
                          asio::error_code& ec,
                          std::size_t& bytes_transferred) {
    ec = asio::error_code();
    if (!verifyConnected(_ssl.get(), &ec)) {
        return want::want_nothing;
    }
    const auto status = ::SSLRead(_ssl.get(), data.data(), data.size(), &bytes_transferred);
    if ((status != ::errSSLWouldBlock) && (status != ::errSecSuccess)) {
        ec = errorCode(status);
    }
    return bytes_transferred ? want::want_nothing : wouldBlock();
}

asio::const_buffer engine::put_input(const asio::const_buffer& data) {
    const auto* p = static_cast<const char*>(data.data());
    _inbuf.insert(_inbuf.end(), p, p + data.size());
    return asio::buffer(data + data.size());
}

::OSStatus engine::read_func(::SSLConnectionRef ctx, void* data, size_t* data_len) {
    ::OSStatus ret = ::errSecSuccess;
    auto* this_ = const_cast<engine*>(static_cast<const engine*>(ctx));
    if (*data_len > this_->_inbuf.size()) {
        // If we're able to 100% satisfy the read request Secure Transport made,
        // then we should ultimately signal that the read is incomplete.
        ret = ::errSSLWouldBlock;
    }
    *data_len = std::min<size_t>(*data_len, this_->_inbuf.size());
    if (*data_len > 0) {
        std::copy(
            this_->_inbuf.begin(), this_->_inbuf.begin() + *data_len, static_cast<char*>(data));
        this_->_inbuf.erase(this_->_inbuf.begin(), this_->_inbuf.begin() + *data_len);
    }
    return ret;
}

engine::want engine::wouldBlock() const {
    return _outbuf.empty() ? want::want_input_and_retry : want::want_output_and_retry;
}

#include "asio/detail/pop_options.hpp"

}  // namespace detail
}  // namespace ssl
}  // namespace asio