forked from zeromq/libzmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwss_engine.cpp
199 lines (157 loc) · 5.22 KB
/
wss_engine.cpp
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
/* SPDX-License-Identifier: MPL-2.0 */
#include "precompiled.hpp"
#include "wss_engine.hpp"
static int verify_certificate_callback (gnutls_session_t session)
{
unsigned int status;
const char *hostname;
// read hostname
hostname = (const char *) gnutls_session_get_ptr (session);
int rc = gnutls_certificate_verify_peers3 (session, hostname, &status);
zmq_assert (rc >= 0);
if (status != 0) {
// TODO: somehow log the error
// Certificate is not trusted
return GNUTLS_E_CERTIFICATE_ERROR;
}
// notify gnutls to continue handshake normally
return 0;
}
zmq::wss_engine_t::wss_engine_t (fd_t fd_,
const options_t &options_,
const endpoint_uri_pair_t &endpoint_uri_pair_,
ws_address_t &address_,
bool client_,
void *tls_server_cred_,
const std::string &hostname_) :
ws_engine_t (fd_, options_, endpoint_uri_pair_, address_, client_),
_established (false),
_tls_client_cred (NULL)
{
int rc = 0;
if (client_) {
// TODO: move to session_base, to allow changing the socket options between connect calls
rc = gnutls_certificate_allocate_credentials (&_tls_client_cred);
zmq_assert (rc == 0);
if (options_.wss_trust_system)
gnutls_certificate_set_x509_system_trust (_tls_client_cred);
if (options_.wss_trust_pem.length () > 0) {
gnutls_datum_t trust = {
(unsigned char *) options_.wss_trust_pem.c_str (),
(unsigned int) options_.wss_trust_pem.length ()};
rc = gnutls_certificate_set_x509_trust_mem (
_tls_client_cred, &trust, GNUTLS_X509_FMT_PEM);
zmq_assert (rc >= 0);
}
gnutls_certificate_set_verify_function (_tls_client_cred,
verify_certificate_callback);
rc = gnutls_init (&_tls_session, GNUTLS_CLIENT | GNUTLS_NONBLOCK);
zmq_assert (rc == GNUTLS_E_SUCCESS);
if (!hostname_.empty ())
gnutls_server_name_set (_tls_session, GNUTLS_NAME_DNS,
hostname_.c_str (), hostname_.size ());
gnutls_session_set_ptr (
_tls_session,
hostname_.empty () ? NULL : const_cast<char *> (hostname_.c_str ()));
rc = gnutls_credentials_set (_tls_session, GNUTLS_CRD_CERTIFICATE,
_tls_client_cred);
zmq_assert (rc == GNUTLS_E_SUCCESS);
} else {
zmq_assert (tls_server_cred_);
rc = gnutls_init (&_tls_session, GNUTLS_SERVER | GNUTLS_NONBLOCK);
zmq_assert (rc == GNUTLS_E_SUCCESS);
rc = gnutls_credentials_set (_tls_session, GNUTLS_CRD_CERTIFICATE,
tls_server_cred_);
zmq_assert (rc == GNUTLS_E_SUCCESS);
}
gnutls_set_default_priority (_tls_session);
gnutls_transport_set_int (_tls_session, fd_);
}
zmq::wss_engine_t::~wss_engine_t ()
{
gnutls_deinit (_tls_session);
if (_tls_client_cred)
gnutls_certificate_free_credentials (_tls_client_cred);
}
void zmq::wss_engine_t::plug_internal ()
{
set_pollin ();
in_event ();
}
void zmq::wss_engine_t::out_event ()
{
if (_established)
return ws_engine_t::out_event ();
do_handshake ();
}
bool zmq::wss_engine_t::do_handshake ()
{
int rc = gnutls_handshake (_tls_session);
reset_pollout ();
if (rc == GNUTLS_E_SUCCESS) {
start_ws_handshake ();
_established = true;
return false;
} else if (rc == GNUTLS_E_AGAIN) {
int direction = gnutls_record_get_direction (_tls_session);
if (direction == 1)
set_pollout ();
return false;
} else if (rc == GNUTLS_E_INTERRUPTED
|| rc == GNUTLS_E_WARNING_ALERT_RECEIVED) {
return false;
} else {
error (zmq::i_engine::connection_error);
return false;
}
return true;
}
bool zmq::wss_engine_t::handshake ()
{
if (!_established) {
if (!do_handshake ()) {
return false;
}
}
return ws_engine_t::handshake ();
}
int zmq::wss_engine_t::read (void *data_, size_t size_)
{
ssize_t rc = gnutls_record_recv (_tls_session, data_, size_);
if (rc == GNUTLS_E_REHANDSHAKE) {
gnutls_alert_send (_tls_session, GNUTLS_AL_WARNING,
GNUTLS_A_NO_RENEGOTIATION);
return 0;
}
if (rc == GNUTLS_E_INTERRUPTED) {
errno = EINTR;
return -1;
}
if (rc == GNUTLS_E_AGAIN) {
errno = EAGAIN;
return -1;
}
if (rc == 0) {
errno = EPIPE;
return -1;
}
if (rc < 0) {
errno = EINVAL;
return -1;
}
// TODO: change return type to ssize_t (signed)
return rc;
}
int zmq::wss_engine_t::write (const void *data_, size_t size_)
{
ssize_t rc = gnutls_record_send (_tls_session, data_, size_);
if (rc == GNUTLS_E_INTERRUPTED || rc == GNUTLS_E_AGAIN) {
return 0;
}
if (rc < 0) {
errno = EINVAL;
return -1;
}
// TODO: change return type to ssize_t (signed)
return rc;
}