summaryrefslogtreecommitdiff
path: root/src/mongo/util/net/ssl/detail/engine_schannel.hpp
blob: 775ab843d24747c6acb3d9c8f1a700e0a274acf6 (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
/**
 * Copyright (C) 2018 MongoDB Inc.
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * 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
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * 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 GNU Affero General 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

#include "asio/detail/config.hpp"

#include "asio/buffer.hpp"
#include "asio/detail/static_mutex.hpp"
#include "mongo/util/net/ssl/detail/schannel.hpp"
#include "mongo/util/net/ssl/stream_base.hpp"

#include "asio/detail/push_options.hpp"

namespace asio {
namespace ssl {
namespace detail {

class engine {
public:
    enum want {
        // Returned by functions to indicate that the engine wants input. The input
        // buffer should be updated to point to the data. The engine then needs to
        // be called again to retry the operation.
        want_input_and_retry = -2,

        // Returned by functions to indicate that the engine wants to write output.
        // The output buffer points to the data to be written. The engine then
        // needs to be called again to retry the operation.
        want_output_and_retry = -1,

        // Returned by functions to indicate that the engine doesn't need input or
        // output.
        want_nothing = 0,

        // Returned by functions to indicate that the engine wants to write output.
        // The output buffer points to the data to be written. After that the
        // operation is complete, and the engine does not need to be called again.
        want_output = 1
    };

    // Construct a new engine for the specified context.
    ASIO_DECL explicit engine(SCHANNEL_CRED* context, const std::string& remoteHostName);

    // Destructor.
    ASIO_DECL ~engine();

    // Get the underlying implementation in the native type.
    ASIO_DECL PCtxtHandle native_handle();

    // Perform an SSL handshake using either SSL_connect (client-side) or
    // SSL_accept (server-side).
    ASIO_DECL want handshake(stream_base::handshake_type type, asio::error_code& ec);

    // Perform a graceful shutdown of the SSL session.
    ASIO_DECL want shutdown(asio::error_code& ec);

    // Write bytes to the SSL session.
    ASIO_DECL want write(const asio::const_buffer& data,
                         asio::error_code& ec,
                         std::size_t& bytes_transferred);

    // Read bytes from the SSL session.
    ASIO_DECL want read(const asio::mutable_buffer& data,
                        asio::error_code& ec,
                        std::size_t& bytes_transferred);

    // Get output data to be written to the transport.
    ASIO_DECL asio::mutable_buffer get_output(const asio::mutable_buffer& data);

    // Put input data that was read from the transport.
    ASIO_DECL asio::const_buffer put_input(const asio::const_buffer& data);

    // Map an error::eof code returned by the underlying transport according to
    // the type and state of the SSL session. Returns a const reference to the
    // error code object, suitable for passing to a completion handler.
    ASIO_DECL const asio::error_code& map_error_code(asio::error_code& ec) const;

private:
    // Disallow copying and assignment.
    engine(const engine&);
    engine& operator=(const engine&);

private:
    // SChannel context handle
    CtxtHandle _hcxt;

    // Credential handle
    CredHandle _hcred;

    // Credentials for TLS handshake
    SCHANNEL_CRED* _pCred;

    // TLS SNI server name
    std::wstring _remoteHostName;

    // Engine State machine
    //
    enum class EngineState {
        // Initial State
        NeedsHandshake,

        // Normal SSL Conversation in progress
        InProgress,

        // In SSL shutdown
        InShutdown,
    };

    // Engine state
    EngineState _state{EngineState::NeedsHandshake};

    // Data received from remote side, shared across state machines
    ReusableBuffer _inBuffer;

    // Data to send to remote side, shared across state machines
    ReusableBuffer _outBuffer;

    // Extra buffer - for when more then one packet is read from the remote side
    ReusableBuffer _extraBuffer;

    // Handshake state machine
    SSLHandshakeManager _handshakeManager;

    // Read state machine
    SSLReadManager _readManager;

    // Write state machine
    SSLWriteManager _writeManager;
};

#include "asio/detail/pop_options.hpp"

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