wj_eventmachine 1.3.0.dev.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +179 -0
- data/GNU +281 -0
- data/LICENSE +60 -0
- data/README.md +110 -0
- data/docs/DocumentationGuidesIndex.md +27 -0
- data/docs/GettingStarted.md +520 -0
- data/docs/old/ChangeLog +211 -0
- data/docs/old/DEFERRABLES +246 -0
- data/docs/old/EPOLL +141 -0
- data/docs/old/INSTALL +13 -0
- data/docs/old/KEYBOARD +42 -0
- data/docs/old/LEGAL +25 -0
- data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/old/PURE_RUBY +75 -0
- data/docs/old/RELEASE_NOTES +94 -0
- data/docs/old/SMTP +4 -0
- data/docs/old/SPAWNED_PROCESSES +148 -0
- data/docs/old/TODO +8 -0
- data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
- data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
- data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
- data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
- data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
- data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
- data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
- data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
- data/examples/old/ex_channel.rb +43 -0
- data/examples/old/ex_queue.rb +2 -0
- data/examples/old/ex_tick_loop_array.rb +15 -0
- data/examples/old/ex_tick_loop_counter.rb +32 -0
- data/examples/old/helper.rb +2 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +52 -0
- data/ext/cmain.cpp +1046 -0
- data/ext/ed.cpp +2238 -0
- data/ext/ed.h +460 -0
- data/ext/em.cpp +2378 -0
- data/ext/em.h +266 -0
- data/ext/eventmachine.h +152 -0
- data/ext/extconf.rb +285 -0
- data/ext/fastfilereader/extconf.rb +120 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +126 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +354 -0
- data/ext/project.h +174 -0
- data/ext/rubymain.cpp +1610 -0
- data/ext/ssl.cpp +627 -0
- data/ext/ssl.h +103 -0
- data/ext/wait_for_single_fd.h +36 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +625 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EmReactorInterface.java +70 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
- data/java/src/com/rubyeventmachine/NullEmReactor.java +157 -0
- data/java/src/com/rubyeventmachine/NullEventableChannel.java +81 -0
- data/lib/em/buftok.rb +59 -0
- data/lib/em/callback.rb +58 -0
- data/lib/em/channel.rb +69 -0
- data/lib/em/completion.rb +307 -0
- data/lib/em/connection.rb +776 -0
- data/lib/em/deferrable.rb +210 -0
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/file_watch.rb +73 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/io_streamer.rb +68 -0
- data/lib/em/iterator.rb +252 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/pool.rb +151 -0
- data/lib/em/process_watch.rb +45 -0
- data/lib/em/processes.rb +123 -0
- data/lib/em/protocols.rb +37 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +303 -0
- data/lib/em/protocols/httpclient2.rb +602 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +33 -0
- data/lib/em/protocols/linetext2.rb +179 -0
- data/lib/em/protocols/memcache.rb +331 -0
- data/lib/em/protocols/object_protocol.rb +46 -0
- data/lib/em/protocols/postgres3.rb +246 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +394 -0
- data/lib/em/protocols/smtpserver.rb +666 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +205 -0
- data/lib/em/protocols/tcptest.rb +54 -0
- data/lib/em/pure_ruby.rb +1299 -0
- data/lib/em/queue.rb +80 -0
- data/lib/em/resolver.rb +232 -0
- data/lib/em/spawnable.rb +84 -0
- data/lib/em/streamer.rb +118 -0
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +61 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1602 -0
- data/lib/jeventmachine.rb +318 -0
- data/rakelib/package.rake +120 -0
- data/rakelib/test.rake +6 -0
- data/rakelib/test_pure.rake +11 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/dhparam.pem +13 -0
- data/tests/em_ssl_handlers.rb +153 -0
- data/tests/em_test_helper.rb +198 -0
- data/tests/jruby/test_jeventmachine.rb +38 -0
- data/tests/test_attach.rb +199 -0
- data/tests/test_basic.rb +321 -0
- data/tests/test_channel.rb +75 -0
- data/tests/test_completion.rb +178 -0
- data/tests/test_connection_count.rb +83 -0
- data/tests/test_connection_write.rb +35 -0
- data/tests/test_defer.rb +35 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +141 -0
- data/tests/test_error_handler.rb +38 -0
- data/tests/test_exc.rb +37 -0
- data/tests/test_file_watch.rb +86 -0
- data/tests/test_fork.rb +75 -0
- data/tests/test_futures.rb +170 -0
- data/tests/test_handler_check.rb +35 -0
- data/tests/test_hc.rb +155 -0
- data/tests/test_httpclient.rb +238 -0
- data/tests/test_httpclient2.rb +132 -0
- data/tests/test_idle_connection.rb +31 -0
- data/tests/test_inactivity_timeout.rb +102 -0
- data/tests/test_io_streamer.rb +47 -0
- data/tests/test_ipv4.rb +96 -0
- data/tests/test_ipv6.rb +107 -0
- data/tests/test_iterator.rb +122 -0
- data/tests/test_kb.rb +28 -0
- data/tests/test_keepalive.rb +113 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_ltp.rb +155 -0
- data/tests/test_ltp2.rb +332 -0
- data/tests/test_many_fds.rb +21 -0
- data/tests/test_next_tick.rb +104 -0
- data/tests/test_object_protocol.rb +36 -0
- data/tests/test_pause.rb +109 -0
- data/tests/test_pending_connect_timeout.rb +52 -0
- data/tests/test_pool.rb +196 -0
- data/tests/test_process_watch.rb +50 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +180 -0
- data/tests/test_pure.rb +156 -0
- data/tests/test_queue.rb +64 -0
- data/tests/test_resolver.rb +129 -0
- data/tests/test_running.rb +14 -0
- data/tests/test_sasl.rb +46 -0
- data/tests/test_send_file.rb +217 -0
- data/tests/test_servers.rb +32 -0
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +75 -0
- data/tests/test_smtpserver.rb +90 -0
- data/tests/test_sock_opt.rb +53 -0
- data/tests/test_spawn.rb +290 -0
- data/tests/test_ssl_args.rb +41 -0
- data/tests/test_ssl_dhparam.rb +57 -0
- data/tests/test_ssl_ecdh_curve.rb +57 -0
- data/tests/test_ssl_extensions.rb +24 -0
- data/tests/test_ssl_methods.rb +31 -0
- data/tests/test_ssl_protocols.rb +190 -0
- data/tests/test_ssl_verify.rb +52 -0
- data/tests/test_stomp.rb +38 -0
- data/tests/test_system.rb +46 -0
- data/tests/test_threaded_resource.rb +68 -0
- data/tests/test_tick_loop.rb +58 -0
- data/tests/test_timers.rb +150 -0
- data/tests/test_ud.rb +8 -0
- data/tests/test_unbind_reason.rb +40 -0
- metadata +384 -0
data/ext/ssl.cpp
ADDED
@@ -0,0 +1,627 @@
|
|
1
|
+
/*****************************************************************************
|
2
|
+
|
3
|
+
$Id$
|
4
|
+
|
5
|
+
File: ssl.cpp
|
6
|
+
Date: 30Apr06
|
7
|
+
|
8
|
+
Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
9
|
+
Gmail: blackhedd
|
10
|
+
|
11
|
+
This program is free software; you can redistribute it and/or modify
|
12
|
+
it under the terms of either: 1) the GNU General Public License
|
13
|
+
as published by the Free Software Foundation; either version 2 of the
|
14
|
+
License, or (at your option) any later version; or 2) Ruby's License.
|
15
|
+
|
16
|
+
See the file COPYING for complete licensing information.
|
17
|
+
|
18
|
+
*****************************************************************************/
|
19
|
+
|
20
|
+
|
21
|
+
#ifdef WITH_SSL
|
22
|
+
|
23
|
+
#include "project.h"
|
24
|
+
|
25
|
+
|
26
|
+
bool SslContext_t::bLibraryInitialized = false;
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
static void InitializeDefaultCredentials();
|
31
|
+
static EVP_PKEY *DefaultPrivateKey = NULL;
|
32
|
+
static X509 *DefaultCertificate = NULL;
|
33
|
+
|
34
|
+
static char PrivateMaterials[] = {
|
35
|
+
"-----BEGIN RSA PRIVATE KEY-----\n"
|
36
|
+
"MIICXAIBAAKBgQDCYYhcw6cGRbhBVShKmbWm7UVsEoBnUf0cCh8AX+MKhMxwVDWV\n"
|
37
|
+
"Igdskntn3cSJjRtmgVJHIK0lpb/FYHQB93Ohpd9/Z18pDmovfFF9nDbFF0t39hJ/\n"
|
38
|
+
"AqSzFB3GiVPoFFZJEE1vJqh+3jzsSF5K56bZ6azz38VlZgXeSozNW5bXkQIDAQAB\n"
|
39
|
+
"AoGALA89gIFcr6BIBo8N5fL3aNHpZXjAICtGav+kTUpuxSiaym9cAeTHuAVv8Xgk\n"
|
40
|
+
"H2Wbq11uz+6JMLpkQJH/WZ7EV59DPOicXrp0Imr73F3EXBfR7t2EQDYHPMthOA1D\n"
|
41
|
+
"I9EtCzvV608Ze90hiJ7E3guGrGppZfJ+eUWCPgy8CZH1vRECQQDv67rwV/oU1aDo\n"
|
42
|
+
"6/+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\n"
|
71
|
+
"uw73m4YIGI0Zw2XdBpiOGkx2H56Kya6mJJe/5XORZedh1wpI7zki01tHYbcy\n"
|
72
|
+
"-----END CERTIFICATE-----\n"};
|
73
|
+
|
74
|
+
/* These private materials were made with:
|
75
|
+
* openssl req -new -x509 -keyout cakey.pem -out cacert.pem -nodes -days 6500
|
76
|
+
* TODO: We need a full-blown capability to work with user-supplied
|
77
|
+
* keypairs and properly-signed certificates.
|
78
|
+
*/
|
79
|
+
|
80
|
+
|
81
|
+
/*****************
|
82
|
+
builtin_passwd_cb
|
83
|
+
*****************/
|
84
|
+
|
85
|
+
extern "C" int builtin_passwd_cb (char *buf UNUSED, int bufsize UNUSED, int rwflag UNUSED, void *userdata UNUSED)
|
86
|
+
{
|
87
|
+
strcpy (buf, "kittycat");
|
88
|
+
return 8;
|
89
|
+
}
|
90
|
+
|
91
|
+
/****************************
|
92
|
+
InitializeDefaultCredentials
|
93
|
+
****************************/
|
94
|
+
|
95
|
+
static void InitializeDefaultCredentials()
|
96
|
+
{
|
97
|
+
BIO *bio = BIO_new_mem_buf (PrivateMaterials, -1);
|
98
|
+
assert (bio);
|
99
|
+
|
100
|
+
if (DefaultPrivateKey) {
|
101
|
+
// we may come here in a restart.
|
102
|
+
EVP_PKEY_free (DefaultPrivateKey);
|
103
|
+
DefaultPrivateKey = NULL;
|
104
|
+
}
|
105
|
+
PEM_read_bio_PrivateKey (bio, &DefaultPrivateKey, builtin_passwd_cb, 0);
|
106
|
+
|
107
|
+
if (DefaultCertificate) {
|
108
|
+
// we may come here in a restart.
|
109
|
+
X509_free (DefaultCertificate);
|
110
|
+
DefaultCertificate = NULL;
|
111
|
+
}
|
112
|
+
PEM_read_bio_X509 (bio, &DefaultCertificate, NULL, 0);
|
113
|
+
|
114
|
+
BIO_free (bio);
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
/**************************
|
120
|
+
SslContext_t::SslContext_t
|
121
|
+
**************************/
|
122
|
+
|
123
|
+
SslContext_t::SslContext_t (bool is_server, const std::string &privkeyfile, const std::string &certchainfile, const std::string &cipherlist, const std::string &ecdh_curve, const std::string &dhparam, int ssl_version) :
|
124
|
+
bIsServer (is_server),
|
125
|
+
pCtx (NULL),
|
126
|
+
PrivateKey (NULL),
|
127
|
+
Certificate (NULL)
|
128
|
+
{
|
129
|
+
/* TODO: the usage of the specified private-key and cert-chain filenames only applies to
|
130
|
+
* client-side connections at this point. Server connections currently use the default materials.
|
131
|
+
* That needs to be fixed asap.
|
132
|
+
* Also, in this implementation, server-side connections use statically defined X-509 defaults.
|
133
|
+
* One thing I'm really not clear on is whether or not you have to explicitly free X509 and EVP_PKEY
|
134
|
+
* objects when we call our destructor, or whether just calling SSL_CTX_free is enough.
|
135
|
+
*/
|
136
|
+
|
137
|
+
if (!bLibraryInitialized) {
|
138
|
+
bLibraryInitialized = true;
|
139
|
+
SSL_library_init();
|
140
|
+
OpenSSL_add_ssl_algorithms();
|
141
|
+
OpenSSL_add_all_algorithms();
|
142
|
+
SSL_load_error_strings();
|
143
|
+
ERR_load_crypto_strings();
|
144
|
+
|
145
|
+
InitializeDefaultCredentials();
|
146
|
+
}
|
147
|
+
#ifdef HAVE_TLS_SERVER_METHOD
|
148
|
+
pCtx = SSL_CTX_new (bIsServer ? TLS_server_method() : TLS_client_method());
|
149
|
+
#else
|
150
|
+
pCtx = SSL_CTX_new (bIsServer ? SSLv23_server_method() : SSLv23_client_method());
|
151
|
+
#endif
|
152
|
+
if (!pCtx)
|
153
|
+
throw std::runtime_error ("no SSL context");
|
154
|
+
|
155
|
+
SSL_CTX_set_options (pCtx, SSL_OP_ALL);
|
156
|
+
|
157
|
+
#ifdef SSL_CTRL_CLEAR_OPTIONS
|
158
|
+
SSL_CTX_clear_options (pCtx, SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1);
|
159
|
+
# ifdef SSL_OP_NO_TLSv1_1
|
160
|
+
SSL_CTX_clear_options (pCtx, SSL_OP_NO_TLSv1_1);
|
161
|
+
# endif
|
162
|
+
# ifdef SSL_OP_NO_TLSv1_2
|
163
|
+
SSL_CTX_clear_options (pCtx, SSL_OP_NO_TLSv1_2);
|
164
|
+
# endif
|
165
|
+
#endif
|
166
|
+
|
167
|
+
if (!(ssl_version & EM_PROTO_SSLv2))
|
168
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_SSLv2);
|
169
|
+
|
170
|
+
if (!(ssl_version & EM_PROTO_SSLv3))
|
171
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_SSLv3);
|
172
|
+
|
173
|
+
if (!(ssl_version & EM_PROTO_TLSv1))
|
174
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_TLSv1);
|
175
|
+
|
176
|
+
#ifdef SSL_OP_NO_TLSv1_1
|
177
|
+
if (!(ssl_version & EM_PROTO_TLSv1_1))
|
178
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_TLSv1_1);
|
179
|
+
#endif
|
180
|
+
|
181
|
+
#ifdef SSL_OP_NO_TLSv1_2
|
182
|
+
if (!(ssl_version & EM_PROTO_TLSv1_2))
|
183
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_TLSv1_2);
|
184
|
+
#endif
|
185
|
+
|
186
|
+
#ifdef SSL_OP_NO_TLSv1_3
|
187
|
+
if (!(ssl_version & EM_PROTO_TLSv1_3))
|
188
|
+
SSL_CTX_set_options (pCtx, SSL_OP_NO_TLSv1_3);
|
189
|
+
#endif
|
190
|
+
|
191
|
+
#ifdef SSL_MODE_RELEASE_BUFFERS
|
192
|
+
SSL_CTX_set_mode (pCtx, SSL_MODE_RELEASE_BUFFERS);
|
193
|
+
#endif
|
194
|
+
|
195
|
+
if (bIsServer) {
|
196
|
+
|
197
|
+
// The SSL_CTX calls here do NOT allocate memory.
|
198
|
+
int e;
|
199
|
+
if (privkeyfile.length() > 0)
|
200
|
+
e = SSL_CTX_use_PrivateKey_file (pCtx, privkeyfile.c_str(), SSL_FILETYPE_PEM);
|
201
|
+
else
|
202
|
+
e = SSL_CTX_use_PrivateKey (pCtx, DefaultPrivateKey);
|
203
|
+
if (e <= 0) ERR_print_errors_fp(stderr);
|
204
|
+
assert (e > 0);
|
205
|
+
|
206
|
+
if (certchainfile.length() > 0)
|
207
|
+
e = SSL_CTX_use_certificate_chain_file (pCtx, certchainfile.c_str());
|
208
|
+
else
|
209
|
+
e = SSL_CTX_use_certificate (pCtx, DefaultCertificate);
|
210
|
+
if (e <= 0) ERR_print_errors_fp(stderr);
|
211
|
+
assert (e > 0);
|
212
|
+
|
213
|
+
if (dhparam.length() > 0) {
|
214
|
+
DH *dh;
|
215
|
+
BIO *bio;
|
216
|
+
|
217
|
+
bio = BIO_new_file(dhparam.c_str(), "r");
|
218
|
+
if (bio == NULL) {
|
219
|
+
char buf [500];
|
220
|
+
snprintf (buf, sizeof(buf)-1, "dhparam: BIO_new_file(%s) failed", dhparam.c_str());
|
221
|
+
throw std::runtime_error (buf);
|
222
|
+
}
|
223
|
+
|
224
|
+
dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
|
225
|
+
|
226
|
+
if (dh == NULL) {
|
227
|
+
BIO_free(bio);
|
228
|
+
char buf [500];
|
229
|
+
snprintf (buf, sizeof(buf)-1, "dhparam: PEM_read_bio_DHparams(%s) failed", dhparam.c_str());
|
230
|
+
throw std::runtime_error (buf);
|
231
|
+
}
|
232
|
+
|
233
|
+
SSL_CTX_set_tmp_dh(pCtx, dh);
|
234
|
+
|
235
|
+
DH_free(dh);
|
236
|
+
BIO_free(bio);
|
237
|
+
}
|
238
|
+
|
239
|
+
if (ecdh_curve.length() > 0) {
|
240
|
+
#if OPENSSL_VERSION_NUMBER >= 0x0090800fL && !defined(OPENSSL_NO_ECDH)
|
241
|
+
int nid;
|
242
|
+
EC_KEY *ecdh;
|
243
|
+
|
244
|
+
nid = OBJ_sn2nid((const char *) ecdh_curve.c_str());
|
245
|
+
if (nid == 0) {
|
246
|
+
char buf [200];
|
247
|
+
snprintf (buf, sizeof(buf)-1, "ecdh_curve: Unknown curve name: %s", ecdh_curve.c_str());
|
248
|
+
throw std::runtime_error (buf);
|
249
|
+
}
|
250
|
+
|
251
|
+
ecdh = EC_KEY_new_by_curve_name(nid);
|
252
|
+
if (ecdh == NULL) {
|
253
|
+
char buf [200];
|
254
|
+
snprintf (buf, sizeof(buf)-1, "ecdh_curve: Unable to create: %s", ecdh_curve.c_str());
|
255
|
+
throw std::runtime_error (buf);
|
256
|
+
}
|
257
|
+
|
258
|
+
SSL_CTX_set_options(pCtx, SSL_OP_SINGLE_ECDH_USE);
|
259
|
+
|
260
|
+
SSL_CTX_set_tmp_ecdh(pCtx, ecdh);
|
261
|
+
|
262
|
+
EC_KEY_free(ecdh);
|
263
|
+
#else
|
264
|
+
throw std::runtime_error ("No openssl ECDH support");
|
265
|
+
#endif
|
266
|
+
}
|
267
|
+
}
|
268
|
+
|
269
|
+
if (cipherlist.length() > 0)
|
270
|
+
SSL_CTX_set_cipher_list (pCtx, cipherlist.c_str());
|
271
|
+
else
|
272
|
+
SSL_CTX_set_cipher_list (pCtx, "ALL:!ADH:!LOW:!EXP:!DES-CBC3-SHA:@STRENGTH");
|
273
|
+
|
274
|
+
if (bIsServer) {
|
275
|
+
SSL_CTX_sess_set_cache_size (pCtx, 128);
|
276
|
+
SSL_CTX_set_session_id_context (pCtx, (unsigned char*)"eventmachine", 12);
|
277
|
+
}
|
278
|
+
else {
|
279
|
+
int e;
|
280
|
+
if (privkeyfile.length() > 0) {
|
281
|
+
e = SSL_CTX_use_PrivateKey_file (pCtx, privkeyfile.c_str(), SSL_FILETYPE_PEM);
|
282
|
+
if (e <= 0) ERR_print_errors_fp(stderr);
|
283
|
+
assert (e > 0);
|
284
|
+
}
|
285
|
+
if (certchainfile.length() > 0) {
|
286
|
+
e = SSL_CTX_use_certificate_chain_file (pCtx, certchainfile.c_str());
|
287
|
+
if (e <= 0) ERR_print_errors_fp(stderr);
|
288
|
+
assert (e > 0);
|
289
|
+
}
|
290
|
+
}
|
291
|
+
}
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
/***************************
|
296
|
+
SslContext_t::~SslContext_t
|
297
|
+
***************************/
|
298
|
+
|
299
|
+
SslContext_t::~SslContext_t()
|
300
|
+
{
|
301
|
+
if (pCtx)
|
302
|
+
SSL_CTX_free (pCtx);
|
303
|
+
if (PrivateKey)
|
304
|
+
EVP_PKEY_free (PrivateKey);
|
305
|
+
if (Certificate)
|
306
|
+
X509_free (Certificate);
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
/******************
|
312
|
+
SslBox_t::SslBox_t
|
313
|
+
******************/
|
314
|
+
|
315
|
+
SslBox_t::SslBox_t (bool is_server, const std::string &privkeyfile, const std::string &certchainfile, bool verify_peer, bool fail_if_no_peer_cert, const std::string &snihostname, const std::string &cipherlist, const std::string &ecdh_curve, const std::string &dhparam, int ssl_version, const uintptr_t binding):
|
316
|
+
bIsServer (is_server),
|
317
|
+
bHandshakeCompleted (false),
|
318
|
+
bVerifyPeer (verify_peer),
|
319
|
+
bFailIfNoPeerCert (fail_if_no_peer_cert),
|
320
|
+
pSSL (NULL),
|
321
|
+
pbioRead (NULL),
|
322
|
+
pbioWrite (NULL)
|
323
|
+
{
|
324
|
+
/* TODO someday: make it possible to re-use SSL contexts so we don't have to create
|
325
|
+
* a new one every time we come here.
|
326
|
+
*/
|
327
|
+
|
328
|
+
Context = new SslContext_t (bIsServer, privkeyfile, certchainfile, cipherlist, ecdh_curve, dhparam, ssl_version);
|
329
|
+
assert (Context);
|
330
|
+
|
331
|
+
pbioRead = BIO_new (BIO_s_mem());
|
332
|
+
assert (pbioRead);
|
333
|
+
|
334
|
+
pbioWrite = BIO_new (BIO_s_mem());
|
335
|
+
assert (pbioWrite);
|
336
|
+
|
337
|
+
pSSL = SSL_new (Context->pCtx);
|
338
|
+
assert (pSSL);
|
339
|
+
|
340
|
+
if (snihostname.length() > 0) {
|
341
|
+
SSL_set_tlsext_host_name (pSSL, snihostname.c_str());
|
342
|
+
}
|
343
|
+
|
344
|
+
SSL_set_bio (pSSL, pbioRead, pbioWrite);
|
345
|
+
|
346
|
+
// Store a pointer to the binding signature in the SSL object so we can retrieve it later
|
347
|
+
SSL_set_ex_data(pSSL, 0, (void*) binding);
|
348
|
+
|
349
|
+
if (bVerifyPeer) {
|
350
|
+
int mode = SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
|
351
|
+
if (bFailIfNoPeerCert)
|
352
|
+
mode = mode | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
|
353
|
+
SSL_set_verify(pSSL, mode, ssl_verify_wrapper);
|
354
|
+
}
|
355
|
+
|
356
|
+
if (!bIsServer) {
|
357
|
+
int e = SSL_connect (pSSL);
|
358
|
+
if (e != 1)
|
359
|
+
ERR_print_errors_fp(stderr);
|
360
|
+
}
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
/*******************
|
366
|
+
SslBox_t::~SslBox_t
|
367
|
+
*******************/
|
368
|
+
|
369
|
+
SslBox_t::~SslBox_t()
|
370
|
+
{
|
371
|
+
// Freeing pSSL will also free the associated BIOs, so DON'T free them separately.
|
372
|
+
if (pSSL) {
|
373
|
+
if (SSL_get_shutdown (pSSL) & SSL_RECEIVED_SHUTDOWN)
|
374
|
+
SSL_shutdown (pSSL);
|
375
|
+
else
|
376
|
+
SSL_clear (pSSL);
|
377
|
+
SSL_free (pSSL);
|
378
|
+
}
|
379
|
+
|
380
|
+
delete Context;
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
/***********************
|
386
|
+
SslBox_t::PutCiphertext
|
387
|
+
***********************/
|
388
|
+
|
389
|
+
bool SslBox_t::PutCiphertext (const char *buf, int bufsize)
|
390
|
+
{
|
391
|
+
assert (buf && (bufsize > 0));
|
392
|
+
|
393
|
+
assert (pbioRead);
|
394
|
+
int n = BIO_write (pbioRead, buf, bufsize);
|
395
|
+
|
396
|
+
return (n == bufsize) ? true : false;
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
/**********************
|
401
|
+
SslBox_t::GetPlaintext
|
402
|
+
**********************/
|
403
|
+
|
404
|
+
int SslBox_t::GetPlaintext (char *buf, int bufsize)
|
405
|
+
{
|
406
|
+
if (!SSL_is_init_finished (pSSL)) {
|
407
|
+
int e = bIsServer ? SSL_accept (pSSL) : SSL_connect (pSSL);
|
408
|
+
if (e != 1) {
|
409
|
+
int er = SSL_get_error (pSSL, e);
|
410
|
+
if (er != SSL_ERROR_WANT_READ) {
|
411
|
+
ERR_print_errors_fp(stderr);
|
412
|
+
// Return -1 for a nonfatal error, -2 for an error that should force the connection down.
|
413
|
+
return (er == SSL_ERROR_SSL) ? (-2) : (-1);
|
414
|
+
}
|
415
|
+
else
|
416
|
+
return 0;
|
417
|
+
}
|
418
|
+
bHandshakeCompleted = true;
|
419
|
+
// If handshake finished, FALL THROUGH and return the available plaintext.
|
420
|
+
}
|
421
|
+
|
422
|
+
if (!SSL_is_init_finished (pSSL)) {
|
423
|
+
// We can get here if a browser abandons a handshake.
|
424
|
+
// The user can see a warning dialog and abort the connection.
|
425
|
+
//cerr << "<SSL_incomp>";
|
426
|
+
return 0;
|
427
|
+
}
|
428
|
+
|
429
|
+
//cerr << "CIPH: " << SSL_get_cipher (pSSL) << endl;
|
430
|
+
|
431
|
+
int n = SSL_read (pSSL, buf, bufsize);
|
432
|
+
if (n >= 0) {
|
433
|
+
return n;
|
434
|
+
}
|
435
|
+
else {
|
436
|
+
if (SSL_get_error (pSSL, n) == SSL_ERROR_WANT_READ) {
|
437
|
+
return 0;
|
438
|
+
}
|
439
|
+
else {
|
440
|
+
return -1;
|
441
|
+
}
|
442
|
+
}
|
443
|
+
|
444
|
+
return 0;
|
445
|
+
}
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
/**************************
|
450
|
+
SslBox_t::CanGetCiphertext
|
451
|
+
**************************/
|
452
|
+
|
453
|
+
bool SslBox_t::CanGetCiphertext()
|
454
|
+
{
|
455
|
+
assert (pbioWrite);
|
456
|
+
return BIO_pending (pbioWrite) ? true : false;
|
457
|
+
}
|
458
|
+
|
459
|
+
|
460
|
+
|
461
|
+
/***********************
|
462
|
+
SslBox_t::GetCiphertext
|
463
|
+
***********************/
|
464
|
+
|
465
|
+
int SslBox_t::GetCiphertext (char *buf, int bufsize)
|
466
|
+
{
|
467
|
+
assert (pbioWrite);
|
468
|
+
assert (buf && (bufsize > 0));
|
469
|
+
|
470
|
+
return BIO_read (pbioWrite, buf, bufsize);
|
471
|
+
}
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
/**********************
|
476
|
+
SslBox_t::PutPlaintext
|
477
|
+
**********************/
|
478
|
+
|
479
|
+
int SslBox_t::PutPlaintext (const char *buf, int bufsize)
|
480
|
+
{
|
481
|
+
// The caller will interpret the return value as the number of bytes written.
|
482
|
+
// WARNING WARNING WARNING, are there any situations in which a 0 or -1 return
|
483
|
+
// from SSL_write means we should immediately retry? The socket-machine loop
|
484
|
+
// will probably wait for a time-out cycle (perhaps a second) before re-trying.
|
485
|
+
// THIS WOULD CAUSE A PERCEPTIBLE DELAY!
|
486
|
+
|
487
|
+
/* We internally queue any outbound plaintext that can't be dispatched
|
488
|
+
* because we're in the middle of a handshake or something.
|
489
|
+
* When we get called, try to send any queued data first, and then
|
490
|
+
* send the caller's data (or queue it). We may get called with no outbound
|
491
|
+
* data, which means we try to send the outbound queue and that's all.
|
492
|
+
*
|
493
|
+
* Return >0 if we wrote any data, 0 if we didn't, and <0 for a fatal error.
|
494
|
+
* Note that if we return 0, the connection is still considered live
|
495
|
+
* and we are signalling that we have accepted the outbound data (if any).
|
496
|
+
*/
|
497
|
+
|
498
|
+
OutboundQ.Push (buf, bufsize);
|
499
|
+
|
500
|
+
if (!SSL_is_init_finished (pSSL))
|
501
|
+
return 0;
|
502
|
+
|
503
|
+
bool fatal = false;
|
504
|
+
bool did_work = false;
|
505
|
+
int pending = BIO_pending(pbioWrite);
|
506
|
+
|
507
|
+
while (OutboundQ.HasPages() && pending < SSLBOX_WRITE_BUFFER_SIZE) {
|
508
|
+
const char *page;
|
509
|
+
int length;
|
510
|
+
OutboundQ.Front (&page, &length);
|
511
|
+
assert (page && (length > 0));
|
512
|
+
int n = SSL_write (pSSL, page, length);
|
513
|
+
pending = BIO_pending(pbioWrite);
|
514
|
+
|
515
|
+
if (n > 0) {
|
516
|
+
did_work = true;
|
517
|
+
OutboundQ.PopFront();
|
518
|
+
}
|
519
|
+
else {
|
520
|
+
int er = SSL_get_error (pSSL, n);
|
521
|
+
if ((er != SSL_ERROR_WANT_READ) && (er != SSL_ERROR_WANT_WRITE))
|
522
|
+
fatal = true;
|
523
|
+
break;
|
524
|
+
}
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
if (did_work)
|
529
|
+
return 1;
|
530
|
+
else if (fatal)
|
531
|
+
return -1;
|
532
|
+
else
|
533
|
+
return 0;
|
534
|
+
}
|
535
|
+
|
536
|
+
/**********************
|
537
|
+
SslBox_t::GetPeerCert
|
538
|
+
**********************/
|
539
|
+
|
540
|
+
X509 *SslBox_t::GetPeerCert()
|
541
|
+
{
|
542
|
+
X509 *cert = NULL;
|
543
|
+
|
544
|
+
if (pSSL)
|
545
|
+
cert = SSL_get_peer_certificate(pSSL);
|
546
|
+
|
547
|
+
return cert;
|
548
|
+
}
|
549
|
+
|
550
|
+
/**********************
|
551
|
+
SslBox_t::GetCipherBits
|
552
|
+
**********************/
|
553
|
+
|
554
|
+
int SslBox_t::GetCipherBits()
|
555
|
+
{
|
556
|
+
int bits = -1;
|
557
|
+
if (pSSL)
|
558
|
+
SSL_get_cipher_bits(pSSL, &bits);
|
559
|
+
return bits;
|
560
|
+
}
|
561
|
+
|
562
|
+
/**********************
|
563
|
+
SslBox_t::GetCipherName
|
564
|
+
**********************/
|
565
|
+
|
566
|
+
const char *SslBox_t::GetCipherName()
|
567
|
+
{
|
568
|
+
if (pSSL)
|
569
|
+
return SSL_get_cipher_name(pSSL);
|
570
|
+
return NULL;
|
571
|
+
}
|
572
|
+
|
573
|
+
/**********************
|
574
|
+
SslBox_t::GetCipherProtocol
|
575
|
+
**********************/
|
576
|
+
|
577
|
+
const char *SslBox_t::GetCipherProtocol()
|
578
|
+
{
|
579
|
+
if (pSSL)
|
580
|
+
return SSL_get_cipher_version(pSSL);
|
581
|
+
return NULL;
|
582
|
+
}
|
583
|
+
|
584
|
+
/**********************
|
585
|
+
SslBox_t::GetSNIHostname
|
586
|
+
**********************/
|
587
|
+
|
588
|
+
const char *SslBox_t::GetSNIHostname()
|
589
|
+
{
|
590
|
+
#ifdef TLSEXT_NAMETYPE_host_name
|
591
|
+
if (pSSL)
|
592
|
+
return SSL_get_servername (pSSL, TLSEXT_NAMETYPE_host_name);
|
593
|
+
#endif
|
594
|
+
return NULL;
|
595
|
+
}
|
596
|
+
|
597
|
+
/******************
|
598
|
+
ssl_verify_wrapper
|
599
|
+
*******************/
|
600
|
+
|
601
|
+
extern "C" int ssl_verify_wrapper(int preverify_ok UNUSED, X509_STORE_CTX *ctx)
|
602
|
+
{
|
603
|
+
uintptr_t binding;
|
604
|
+
X509 *cert;
|
605
|
+
SSL *ssl;
|
606
|
+
BUF_MEM *buf;
|
607
|
+
BIO *out;
|
608
|
+
int result;
|
609
|
+
|
610
|
+
cert = X509_STORE_CTX_get_current_cert(ctx);
|
611
|
+
ssl = (SSL*) X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
|
612
|
+
binding = (uintptr_t) SSL_get_ex_data(ssl, 0);
|
613
|
+
|
614
|
+
out = BIO_new(BIO_s_mem());
|
615
|
+
PEM_write_bio_X509(out, cert);
|
616
|
+
BIO_write(out, "\0", 1);
|
617
|
+
BIO_get_mem_ptr(out, &buf);
|
618
|
+
|
619
|
+
ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject(binding));
|
620
|
+
result = (cd->VerifySslPeer(buf->data) == true ? 1 : 0);
|
621
|
+
BIO_free(out);
|
622
|
+
|
623
|
+
return result;
|
624
|
+
}
|
625
|
+
|
626
|
+
#endif // WITH_SSL
|
627
|
+
|