jun-puma 1.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +7 -0
  2. data/History.md +2897 -0
  3. data/LICENSE +29 -0
  4. data/README.md +475 -0
  5. data/bin/puma +10 -0
  6. data/bin/puma-wild +25 -0
  7. data/bin/pumactl +12 -0
  8. data/docs/architecture.md +74 -0
  9. data/docs/compile_options.md +55 -0
  10. data/docs/deployment.md +102 -0
  11. data/docs/fork_worker.md +35 -0
  12. data/docs/images/puma-connection-flow-no-reactor.png +0 -0
  13. data/docs/images/puma-connection-flow.png +0 -0
  14. data/docs/images/puma-general-arch.png +0 -0
  15. data/docs/jungle/README.md +9 -0
  16. data/docs/jungle/rc.d/README.md +74 -0
  17. data/docs/jungle/rc.d/puma +61 -0
  18. data/docs/jungle/rc.d/puma.conf +10 -0
  19. data/docs/kubernetes.md +78 -0
  20. data/docs/nginx.md +80 -0
  21. data/docs/plugins.md +38 -0
  22. data/docs/rails_dev_mode.md +28 -0
  23. data/docs/restart.md +65 -0
  24. data/docs/signals.md +98 -0
  25. data/docs/stats.md +142 -0
  26. data/docs/systemd.md +253 -0
  27. data/docs/testing_benchmarks_local_files.md +150 -0
  28. data/docs/testing_test_rackup_ci_files.md +36 -0
  29. data/ext/puma_http11/PumaHttp11Service.java +17 -0
  30. data/ext/puma_http11/ext_help.h +15 -0
  31. data/ext/puma_http11/extconf.rb +80 -0
  32. data/ext/puma_http11/http11_parser.c +1057 -0
  33. data/ext/puma_http11/http11_parser.h +65 -0
  34. data/ext/puma_http11/http11_parser.java.rl +145 -0
  35. data/ext/puma_http11/http11_parser.rl +149 -0
  36. data/ext/puma_http11/http11_parser_common.rl +54 -0
  37. data/ext/puma_http11/mini_ssl.c +842 -0
  38. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +15 -0
  39. data/ext/puma_http11/org/jruby/puma/Http11.java +228 -0
  40. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +455 -0
  41. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +509 -0
  42. data/ext/puma_http11/puma_http11.c +495 -0
  43. data/lib/puma/app/status.rb +96 -0
  44. data/lib/puma/binder.rb +502 -0
  45. data/lib/puma/cli.rb +247 -0
  46. data/lib/puma/client.rb +682 -0
  47. data/lib/puma/cluster/worker.rb +180 -0
  48. data/lib/puma/cluster/worker_handle.rb +96 -0
  49. data/lib/puma/cluster.rb +616 -0
  50. data/lib/puma/commonlogger.rb +115 -0
  51. data/lib/puma/configuration.rb +390 -0
  52. data/lib/puma/const.rb +307 -0
  53. data/lib/puma/control_cli.rb +316 -0
  54. data/lib/puma/detect.rb +45 -0
  55. data/lib/puma/dsl.rb +1425 -0
  56. data/lib/puma/error_logger.rb +113 -0
  57. data/lib/puma/events.rb +57 -0
  58. data/lib/puma/io_buffer.rb +46 -0
  59. data/lib/puma/jruby_restart.rb +11 -0
  60. data/lib/puma/json_serialization.rb +96 -0
  61. data/lib/puma/launcher/bundle_pruner.rb +104 -0
  62. data/lib/puma/launcher.rb +488 -0
  63. data/lib/puma/log_writer.rb +147 -0
  64. data/lib/puma/minissl/context_builder.rb +96 -0
  65. data/lib/puma/minissl.rb +459 -0
  66. data/lib/puma/null_io.rb +84 -0
  67. data/lib/puma/plugin/systemd.rb +90 -0
  68. data/lib/puma/plugin/tmp_restart.rb +36 -0
  69. data/lib/puma/plugin.rb +111 -0
  70. data/lib/puma/puma_http11.jar +0 -0
  71. data/lib/puma/rack/builder.rb +297 -0
  72. data/lib/puma/rack/urlmap.rb +93 -0
  73. data/lib/puma/rack_default.rb +24 -0
  74. data/lib/puma/reactor.rb +125 -0
  75. data/lib/puma/request.rb +688 -0
  76. data/lib/puma/runner.rb +213 -0
  77. data/lib/puma/sd_notify.rb +149 -0
  78. data/lib/puma/server.rb +680 -0
  79. data/lib/puma/single.rb +69 -0
  80. data/lib/puma/state_file.rb +68 -0
  81. data/lib/puma/thread_pool.rb +434 -0
  82. data/lib/puma/util.rb +141 -0
  83. data/lib/puma.rb +78 -0
  84. data/lib/rack/handler/puma.rb +144 -0
  85. data/tools/Dockerfile +16 -0
  86. data/tools/trickletest.rb +44 -0
  87. metadata +153 -0
@@ -0,0 +1,842 @@
1
+ #define RSTRING_NOT_MODIFIED 1
2
+
3
+ #include <ruby.h>
4
+ #include <ruby/version.h>
5
+ #include <ruby/io.h>
6
+
7
+ #ifdef HAVE_OPENSSL_BIO_H
8
+
9
+ #include <openssl/bio.h>
10
+ #include <openssl/ssl.h>
11
+ #include <openssl/dh.h>
12
+ #include <openssl/err.h>
13
+ #include <openssl/x509.h>
14
+
15
+ #ifndef SSL_OP_NO_COMPRESSION
16
+ #define SSL_OP_NO_COMPRESSION 0
17
+ #endif
18
+
19
+ typedef struct {
20
+ BIO* read;
21
+ BIO* write;
22
+ SSL* ssl;
23
+ SSL_CTX* ctx;
24
+ } ms_conn;
25
+
26
+ typedef struct {
27
+ unsigned char* buf;
28
+ int bytes;
29
+ } ms_cert_buf;
30
+
31
+ VALUE eError;
32
+
33
+ NORETURN(void raise_file_error(const char* caller, const char *filename));
34
+
35
+ void raise_file_error(const char* caller, const char *filename) {
36
+ rb_raise(eError, "%s: error in file '%s': %s", caller, filename, ERR_error_string(ERR_get_error(), NULL));
37
+ }
38
+
39
+ NORETURN(void raise_param_error(const char* caller, const char *param));
40
+
41
+ void raise_param_error(const char* caller, const char *param) {
42
+ rb_raise(eError, "%s: error with parameter '%s': %s", caller, param, ERR_error_string(ERR_get_error(), NULL));
43
+ }
44
+
45
+ void engine_free(void *ptr) {
46
+ ms_conn *conn = ptr;
47
+ ms_cert_buf* cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
48
+ if(cert_buf) {
49
+ OPENSSL_free(cert_buf->buf);
50
+ free(cert_buf);
51
+ }
52
+ SSL_free(conn->ssl);
53
+ SSL_CTX_free(conn->ctx);
54
+
55
+ free(conn);
56
+ }
57
+
58
+ const rb_data_type_t engine_data_type = {
59
+ "MiniSSL/ENGINE",
60
+ { 0, engine_free, 0 },
61
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
62
+ };
63
+
64
+ #ifndef HAVE_SSL_CTX_SET_DH_AUTO
65
+ DH *get_dh2048(void) {
66
+ /* `openssl dhparam -C 2048`
67
+ * -----BEGIN DH PARAMETERS-----
68
+ * MIIBCAKCAQEAjmh1uQHdTfxOyxEbKAV30fUfzqMDF/ChPzjfyzl2jcrqQMhrk76o
69
+ * 2NPNXqxHwsddMZ1RzvU8/jl+uhRuPWjXCFZbhET4N1vrviZM3VJhV8PPHuiVOACO
70
+ * y32jFd+Szx4bo2cXSK83hJ6jRd+0asP1awWjz9/06dFkrILCXMIfQLo0D8rqmppn
71
+ * EfDDAwuudCpM9kcDmBRAm9JsKbQ6gzZWjkc5+QWSaQofojIHbjvj3xzguaCJn+oQ
72
+ * vHWM+hsAnaOgEwCyeZ3xqs+/5lwSbkE/tqJW98cEZGygBUVo9jxZRZx6KOfjpdrb
73
+ * yenO9LJr/qtyrZB31WJbqxI0m0AKTAO8UwIBAg==
74
+ * -----END DH PARAMETERS-----
75
+ */
76
+ static unsigned char dh2048_p[] = {
77
+ 0x8E, 0x68, 0x75, 0xB9, 0x01, 0xDD, 0x4D, 0xFC, 0x4E, 0xCB,
78
+ 0x11, 0x1B, 0x28, 0x05, 0x77, 0xD1, 0xF5, 0x1F, 0xCE, 0xA3,
79
+ 0x03, 0x17, 0xF0, 0xA1, 0x3F, 0x38, 0xDF, 0xCB, 0x39, 0x76,
80
+ 0x8D, 0xCA, 0xEA, 0x40, 0xC8, 0x6B, 0x93, 0xBE, 0xA8, 0xD8,
81
+ 0xD3, 0xCD, 0x5E, 0xAC, 0x47, 0xC2, 0xC7, 0x5D, 0x31, 0x9D,
82
+ 0x51, 0xCE, 0xF5, 0x3C, 0xFE, 0x39, 0x7E, 0xBA, 0x14, 0x6E,
83
+ 0x3D, 0x68, 0xD7, 0x08, 0x56, 0x5B, 0x84, 0x44, 0xF8, 0x37,
84
+ 0x5B, 0xEB, 0xBE, 0x26, 0x4C, 0xDD, 0x52, 0x61, 0x57, 0xC3,
85
+ 0xCF, 0x1E, 0xE8, 0x95, 0x38, 0x00, 0x8E, 0xCB, 0x7D, 0xA3,
86
+ 0x15, 0xDF, 0x92, 0xCF, 0x1E, 0x1B, 0xA3, 0x67, 0x17, 0x48,
87
+ 0xAF, 0x37, 0x84, 0x9E, 0xA3, 0x45, 0xDF, 0xB4, 0x6A, 0xC3,
88
+ 0xF5, 0x6B, 0x05, 0xA3, 0xCF, 0xDF, 0xF4, 0xE9, 0xD1, 0x64,
89
+ 0xAC, 0x82, 0xC2, 0x5C, 0xC2, 0x1F, 0x40, 0xBA, 0x34, 0x0F,
90
+ 0xCA, 0xEA, 0x9A, 0x9A, 0x67, 0x11, 0xF0, 0xC3, 0x03, 0x0B,
91
+ 0xAE, 0x74, 0x2A, 0x4C, 0xF6, 0x47, 0x03, 0x98, 0x14, 0x40,
92
+ 0x9B, 0xD2, 0x6C, 0x29, 0xB4, 0x3A, 0x83, 0x36, 0x56, 0x8E,
93
+ 0x47, 0x39, 0xF9, 0x05, 0x92, 0x69, 0x0A, 0x1F, 0xA2, 0x32,
94
+ 0x07, 0x6E, 0x3B, 0xE3, 0xDF, 0x1C, 0xE0, 0xB9, 0xA0, 0x89,
95
+ 0x9F, 0xEA, 0x10, 0xBC, 0x75, 0x8C, 0xFA, 0x1B, 0x00, 0x9D,
96
+ 0xA3, 0xA0, 0x13, 0x00, 0xB2, 0x79, 0x9D, 0xF1, 0xAA, 0xCF,
97
+ 0xBF, 0xE6, 0x5C, 0x12, 0x6E, 0x41, 0x3F, 0xB6, 0xA2, 0x56,
98
+ 0xF7, 0xC7, 0x04, 0x64, 0x6C, 0xA0, 0x05, 0x45, 0x68, 0xF6,
99
+ 0x3C, 0x59, 0x45, 0x9C, 0x7A, 0x28, 0xE7, 0xE3, 0xA5, 0xDA,
100
+ 0xDB, 0xC9, 0xE9, 0xCE, 0xF4, 0xB2, 0x6B, 0xFE, 0xAB, 0x72,
101
+ 0xAD, 0x90, 0x77, 0xD5, 0x62, 0x5B, 0xAB, 0x12, 0x34, 0x9B,
102
+ 0x40, 0x0A, 0x4C, 0x03, 0xBC, 0x53
103
+ };
104
+ static unsigned char dh2048_g[] = { 0x02 };
105
+
106
+ DH *dh;
107
+ #if !(OPENSSL_VERSION_NUMBER < 0x10100005L)
108
+ BIGNUM *p, *g;
109
+ #endif
110
+
111
+ dh = DH_new();
112
+
113
+ #if OPENSSL_VERSION_NUMBER < 0x10100005L
114
+ dh->p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
115
+ dh->g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
116
+
117
+ if ((dh->p == NULL) || (dh->g == NULL)) {
118
+ DH_free(dh);
119
+ return NULL;
120
+ }
121
+ #else
122
+ p = BN_bin2bn(dh2048_p, sizeof(dh2048_p), NULL);
123
+ g = BN_bin2bn(dh2048_g, sizeof(dh2048_g), NULL);
124
+
125
+ if (p == NULL || g == NULL || !DH_set0_pqg(dh, p, NULL, g)) {
126
+ DH_free(dh);
127
+ BN_free(p);
128
+ BN_free(g);
129
+ return NULL;
130
+ }
131
+ #endif
132
+
133
+ return dh;
134
+ }
135
+ #endif
136
+
137
+ static void
138
+ sslctx_free(void *ptr) {
139
+ SSL_CTX *ctx = ptr;
140
+ SSL_CTX_free(ctx);
141
+ }
142
+
143
+ static const rb_data_type_t sslctx_type = {
144
+ "MiniSSL/SSLContext",
145
+ {
146
+ 0, sslctx_free,
147
+ },
148
+ 0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
149
+ };
150
+
151
+ ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
152
+ ms_conn* conn;
153
+
154
+ *obj = TypedData_Make_Struct(klass, ms_conn, &engine_data_type, conn);
155
+
156
+ conn->read = BIO_new(BIO_s_mem());
157
+ BIO_set_nbio(conn->read, 1);
158
+
159
+ conn->write = BIO_new(BIO_s_mem());
160
+ BIO_set_nbio(conn->write, 1);
161
+
162
+ conn->ssl = 0;
163
+ conn->ctx = 0;
164
+
165
+ return conn;
166
+ }
167
+
168
+ static int engine_verify_callback(int preverify_ok, X509_STORE_CTX* ctx) {
169
+ X509* err_cert;
170
+ SSL* ssl;
171
+ int bytes;
172
+ unsigned char* buf = NULL;
173
+
174
+ if(!preverify_ok) {
175
+ err_cert = X509_STORE_CTX_get_current_cert(ctx);
176
+ if(err_cert) {
177
+ /*
178
+ * Save the failed certificate for inspection/logging.
179
+ */
180
+ bytes = i2d_X509(err_cert, &buf);
181
+ if(bytes > 0) {
182
+ ms_cert_buf* cert_buf = (ms_cert_buf*)malloc(sizeof(ms_cert_buf));
183
+ cert_buf->buf = buf;
184
+ cert_buf->bytes = bytes;
185
+ ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
186
+ SSL_set_app_data(ssl, cert_buf);
187
+ }
188
+ }
189
+ }
190
+
191
+ return preverify_ok;
192
+ }
193
+
194
+ static int password_callback(char *buf, int size, int rwflag, void *userdata) {
195
+ const char *password = (const char *) userdata;
196
+ size_t len = strlen(password);
197
+
198
+ if (len > (size_t) size) {
199
+ return 0;
200
+ }
201
+
202
+ memcpy(buf, password, len);
203
+ return (int) len;
204
+ }
205
+
206
+ static VALUE
207
+ sslctx_alloc(VALUE klass) {
208
+ SSL_CTX *ctx;
209
+ long mode = 0 |
210
+ SSL_MODE_ENABLE_PARTIAL_WRITE |
211
+ SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
212
+ SSL_MODE_RELEASE_BUFFERS;
213
+
214
+ #ifdef HAVE_TLS_SERVER_METHOD
215
+ ctx = SSL_CTX_new(TLS_method());
216
+ // printf("\nctx using TLS_method security_level %d\n", SSL_CTX_get_security_level(ctx));
217
+ #else
218
+ ctx = SSL_CTX_new(SSLv23_method());
219
+ #endif
220
+ if (!ctx) {
221
+ rb_raise(eError, "SSL_CTX_new");
222
+ }
223
+ SSL_CTX_set_mode(ctx, mode);
224
+
225
+ return TypedData_Wrap_Struct(klass, &sslctx_type, ctx);
226
+ }
227
+
228
+ VALUE
229
+ sslctx_initialize(VALUE self, VALUE mini_ssl_ctx) {
230
+ SSL_CTX* ctx;
231
+ int ssl_options;
232
+ VALUE key, cert, ca, verify_mode, ssl_cipher_filter, ssl_ciphersuites, no_tlsv1, no_tlsv1_1,
233
+ verification_flags, session_id_bytes, cert_pem, key_pem, key_password_command, key_password;
234
+ BIO *bio;
235
+ X509 *x509 = NULL;
236
+ EVP_PKEY *pkey;
237
+ pem_password_cb *password_cb = NULL;
238
+ const char *password = NULL;
239
+ #ifdef HAVE_SSL_CTX_SET_MIN_PROTO_VERSION
240
+ int min;
241
+ #endif
242
+ #ifndef HAVE_SSL_CTX_SET_DH_AUTO
243
+ DH *dh;
244
+ #endif
245
+ #if OPENSSL_VERSION_NUMBER < 0x10002000L
246
+ EC_KEY *ecdh;
247
+ #endif
248
+ #ifdef HAVE_SSL_CTX_SET_SESSION_CACHE_MODE
249
+ VALUE reuse, reuse_cache_size, reuse_timeout;
250
+
251
+ reuse = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse"), 0);
252
+ reuse_cache_size = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse_cache_size"), 0);
253
+ reuse_timeout = rb_funcall(mini_ssl_ctx, rb_intern_const("reuse_timeout"), 0);
254
+ #endif
255
+
256
+ key = rb_funcall(mini_ssl_ctx, rb_intern_const("key"), 0);
257
+
258
+ key_password_command = rb_funcall(mini_ssl_ctx, rb_intern_const("key_password_command"), 0);
259
+
260
+ cert = rb_funcall(mini_ssl_ctx, rb_intern_const("cert"), 0);
261
+
262
+ ca = rb_funcall(mini_ssl_ctx, rb_intern_const("ca"), 0);
263
+
264
+ cert_pem = rb_funcall(mini_ssl_ctx, rb_intern_const("cert_pem"), 0);
265
+
266
+ key_pem = rb_funcall(mini_ssl_ctx, rb_intern_const("key_pem"), 0);
267
+
268
+ verify_mode = rb_funcall(mini_ssl_ctx, rb_intern_const("verify_mode"), 0);
269
+
270
+ ssl_cipher_filter = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_cipher_filter"), 0);
271
+
272
+ ssl_ciphersuites = rb_funcall(mini_ssl_ctx, rb_intern_const("ssl_ciphersuites"), 0);
273
+
274
+ no_tlsv1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1"), 0);
275
+
276
+ no_tlsv1_1 = rb_funcall(mini_ssl_ctx, rb_intern_const("no_tlsv1_1"), 0);
277
+
278
+ TypedData_Get_Struct(self, SSL_CTX, &sslctx_type, ctx);
279
+
280
+ if (!NIL_P(cert)) {
281
+ StringValue(cert);
282
+
283
+ if (SSL_CTX_use_certificate_chain_file(ctx, RSTRING_PTR(cert)) != 1) {
284
+ raise_file_error("SSL_CTX_use_certificate_chain_file", RSTRING_PTR(cert));
285
+ }
286
+ }
287
+
288
+ if (!NIL_P(key_password_command)) {
289
+ key_password = rb_funcall(mini_ssl_ctx, rb_intern_const("key_password"), 0);
290
+
291
+ if (!NIL_P(key_password)) {
292
+ StringValue(key_password);
293
+ password_cb = password_callback;
294
+ password = RSTRING_PTR(key_password);
295
+ SSL_CTX_set_default_passwd_cb(ctx, password_cb);
296
+ SSL_CTX_set_default_passwd_cb_userdata(ctx, (void *) password);
297
+ }
298
+ }
299
+
300
+ if (!NIL_P(key)) {
301
+ StringValue(key);
302
+
303
+ if (SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM) != 1) {
304
+ raise_file_error("SSL_CTX_use_PrivateKey_file", RSTRING_PTR(key));
305
+ }
306
+ }
307
+
308
+ if (!NIL_P(cert_pem)) {
309
+ X509 *ca = NULL;
310
+ unsigned long err;
311
+
312
+ bio = BIO_new(BIO_s_mem());
313
+ BIO_puts(bio, RSTRING_PTR(cert_pem));
314
+
315
+ /**
316
+ * Much of this pulled as a simplified version of the `use_certificate_chain_file` method
317
+ * from openssl's `ssl_rsa.c` file.
318
+ */
319
+
320
+ /* first read the cert as the first item in the pem file */
321
+ x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
322
+ if (NULL == x509) {
323
+ BIO_free_all(bio);
324
+ raise_param_error("PEM_read_bio_X509", "cert_pem");
325
+ }
326
+
327
+ /* Add the cert to the context */
328
+ /* 1 is success - otherwise check the error codes */
329
+ if (1 != SSL_CTX_use_certificate(ctx, x509)) {
330
+ BIO_free_all(bio);
331
+ raise_param_error("SSL_CTX_use_certificate", "cert_pem");
332
+ }
333
+
334
+ X509_free(x509); /* no longer need our reference */
335
+
336
+ /* Now lets load up the rest of the certificate chain */
337
+ /* 1 is success 0 is error */
338
+ if (0 == SSL_CTX_clear_chain_certs(ctx)) {
339
+ BIO_free_all(bio);
340
+ raise_param_error("SSL_CTX_clear_chain_certs","cert_pem");
341
+ }
342
+
343
+ while (1) {
344
+ ca = PEM_read_bio_X509(bio, NULL, NULL, NULL);
345
+
346
+ if (NULL == ca) {
347
+ break;
348
+ }
349
+
350
+ if (0 == SSL_CTX_add0_chain_cert(ctx, ca)) {
351
+ BIO_free_all(bio);
352
+ raise_param_error("SSL_CTX_add0_chain_cert","cert_pem");
353
+ }
354
+ /* don't free ca - its now owned by the context */
355
+ }
356
+
357
+ /* ca is NULL - so its either the end of the file or an error */
358
+ err = ERR_peek_last_error();
359
+
360
+ /* If its the end of the file - then we are done, in any case free the bio */
361
+ BIO_free_all(bio);
362
+
363
+ if ((ERR_GET_LIB(err) == ERR_LIB_PEM) && (ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
364
+ ERR_clear_error();
365
+ } else {
366
+ raise_param_error("PEM_read_bio_X509","cert_pem");
367
+ }
368
+ }
369
+
370
+ if (!NIL_P(key_pem)) {
371
+ bio = BIO_new(BIO_s_mem());
372
+ BIO_puts(bio, RSTRING_PTR(key_pem));
373
+ pkey = PEM_read_bio_PrivateKey(bio, NULL, password_cb, (void *) password);
374
+
375
+ if (SSL_CTX_use_PrivateKey(ctx, pkey) != 1) {
376
+ BIO_free(bio);
377
+ raise_file_error("SSL_CTX_use_PrivateKey", RSTRING_PTR(key_pem));
378
+ }
379
+ EVP_PKEY_free(pkey);
380
+ BIO_free(bio);
381
+ }
382
+
383
+ verification_flags = rb_funcall(mini_ssl_ctx, rb_intern_const("verification_flags"), 0);
384
+
385
+ if (!NIL_P(verification_flags)) {
386
+ X509_VERIFY_PARAM *param = SSL_CTX_get0_param(ctx);
387
+ X509_VERIFY_PARAM_set_flags(param, NUM2INT(verification_flags));
388
+ SSL_CTX_set1_param(ctx, param);
389
+ }
390
+
391
+ if (!NIL_P(ca)) {
392
+ StringValue(ca);
393
+ if (SSL_CTX_load_verify_locations(ctx, RSTRING_PTR(ca), NULL) != 1) {
394
+ raise_file_error("SSL_CTX_load_verify_locations", RSTRING_PTR(ca));
395
+ }
396
+ }
397
+
398
+ ssl_options = SSL_OP_CIPHER_SERVER_PREFERENCE | SSL_OP_SINGLE_ECDH_USE | SSL_OP_NO_COMPRESSION;
399
+
400
+ #ifdef HAVE_SSL_CTX_SET_MIN_PROTO_VERSION
401
+ if (RTEST(no_tlsv1_1)) {
402
+ min = TLS1_2_VERSION;
403
+ }
404
+ else if (RTEST(no_tlsv1)) {
405
+ min = TLS1_1_VERSION;
406
+ }
407
+ else {
408
+ min = TLS1_VERSION;
409
+ }
410
+
411
+ SSL_CTX_set_min_proto_version(ctx, min);
412
+
413
+ #else
414
+ /* As of 1.0.2f, SSL_OP_SINGLE_DH_USE key use is always on */
415
+ ssl_options |= SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_SINGLE_DH_USE;
416
+
417
+ if (RTEST(no_tlsv1)) {
418
+ ssl_options |= SSL_OP_NO_TLSv1;
419
+ }
420
+ if(RTEST(no_tlsv1_1)) {
421
+ ssl_options |= SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1;
422
+ }
423
+ #endif
424
+
425
+ #ifdef HAVE_SSL_CTX_SET_SESSION_CACHE_MODE
426
+ if (!NIL_P(reuse)) {
427
+ SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
428
+ if (!NIL_P(reuse_cache_size)) {
429
+ SSL_CTX_sess_set_cache_size(ctx, NUM2INT(reuse_cache_size));
430
+ }
431
+ if (!NIL_P(reuse_timeout)) {
432
+ SSL_CTX_set_timeout(ctx, NUM2INT(reuse_timeout));
433
+ }
434
+ } else {
435
+ SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
436
+ }
437
+ #endif
438
+
439
+ SSL_CTX_set_options(ctx, ssl_options);
440
+
441
+ if (!NIL_P(ssl_cipher_filter)) {
442
+ StringValue(ssl_cipher_filter);
443
+ SSL_CTX_set_cipher_list(ctx, RSTRING_PTR(ssl_cipher_filter));
444
+ }
445
+ else {
446
+ SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
447
+ }
448
+
449
+ #if HAVE_SSL_CTX_SET_CIPHERSUITES
450
+ // Only override OpenSSL default ciphersuites if config option is supplied.
451
+ if (!NIL_P(ssl_ciphersuites)) {
452
+ StringValue(ssl_ciphersuites);
453
+ SSL_CTX_set_ciphersuites(ctx, RSTRING_PTR(ssl_ciphersuites));
454
+ }
455
+ #endif
456
+
457
+ #if OPENSSL_VERSION_NUMBER < 0x10002000L
458
+ // Remove this case if OpenSSL 1.0.1 (now EOL) support is no longer needed.
459
+ ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
460
+ if (ecdh) {
461
+ SSL_CTX_set_tmp_ecdh(ctx, ecdh);
462
+ EC_KEY_free(ecdh);
463
+ }
464
+ #elif OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
465
+ SSL_CTX_set_ecdh_auto(ctx, 1);
466
+ #endif
467
+
468
+ if (NIL_P(verify_mode)) {
469
+ /* SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); */
470
+ } else {
471
+ SSL_CTX_set_verify(ctx, NUM2INT(verify_mode), engine_verify_callback);
472
+ }
473
+
474
+ // Random.bytes available in Ruby 2.5 and later, Random::DEFAULT deprecated in 3.0
475
+ session_id_bytes = rb_funcall(
476
+ #ifdef HAVE_RANDOM_BYTES
477
+ rb_cRandom,
478
+ #else
479
+ rb_const_get(rb_cRandom, rb_intern_const("DEFAULT")),
480
+ #endif
481
+ rb_intern_const("bytes"),
482
+ 1, ULL2NUM(SSL_MAX_SSL_SESSION_ID_LENGTH));
483
+
484
+ SSL_CTX_set_session_id_context(ctx,
485
+ (unsigned char *) RSTRING_PTR(session_id_bytes),
486
+ SSL_MAX_SSL_SESSION_ID_LENGTH);
487
+
488
+ // printf("\ninitialize end security_level %d\n", SSL_CTX_get_security_level(ctx));
489
+
490
+ #ifdef HAVE_SSL_CTX_SET_DH_AUTO
491
+ // https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_dh_auto.html
492
+ SSL_CTX_set_dh_auto(ctx, 1);
493
+ #else
494
+ dh = get_dh2048();
495
+ SSL_CTX_set_tmp_dh(ctx, dh);
496
+ #endif
497
+
498
+ rb_obj_freeze(self);
499
+ return self;
500
+ }
501
+
502
+ VALUE engine_init_server(VALUE self, VALUE sslctx) {
503
+ ms_conn* conn;
504
+ VALUE obj;
505
+ SSL_CTX* ctx;
506
+ SSL* ssl;
507
+
508
+ conn = engine_alloc(self, &obj);
509
+
510
+ TypedData_Get_Struct(sslctx, SSL_CTX, &sslctx_type, ctx);
511
+
512
+ ssl = SSL_new(ctx);
513
+ conn->ssl = ssl;
514
+ SSL_set_app_data(ssl, NULL);
515
+ SSL_set_bio(ssl, conn->read, conn->write);
516
+ SSL_set_accept_state(ssl);
517
+ return obj;
518
+ }
519
+
520
+ VALUE engine_init_client(VALUE klass) {
521
+ VALUE obj;
522
+ ms_conn* conn = engine_alloc(klass, &obj);
523
+ #ifdef HAVE_DTLS_METHOD
524
+ conn->ctx = SSL_CTX_new(DTLS_method());
525
+ #else
526
+ conn->ctx = SSL_CTX_new(DTLSv1_method());
527
+ #endif
528
+ conn->ssl = SSL_new(conn->ctx);
529
+ SSL_set_app_data(conn->ssl, NULL);
530
+ SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
531
+
532
+ SSL_set_bio(conn->ssl, conn->read, conn->write);
533
+
534
+ SSL_set_connect_state(conn->ssl);
535
+ return obj;
536
+ }
537
+
538
+ VALUE engine_inject(VALUE self, VALUE str) {
539
+ ms_conn* conn;
540
+ long used;
541
+
542
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
543
+
544
+ StringValue(str);
545
+
546
+ used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
547
+
548
+ if(used == 0 || used == -1) {
549
+ return Qfalse;
550
+ }
551
+
552
+ return INT2FIX(used);
553
+ }
554
+
555
+ NORETURN(void raise_error(SSL* ssl, int result));
556
+
557
+ void raise_error(SSL* ssl, int result) {
558
+ char buf[512];
559
+ char msg[768];
560
+ const char* err_str;
561
+ int err = errno;
562
+ int mask = 4095;
563
+ int ssl_err = SSL_get_error(ssl, result);
564
+ int verify_err = (int) SSL_get_verify_result(ssl);
565
+
566
+ if(SSL_ERROR_SYSCALL == ssl_err) {
567
+ snprintf(msg, sizeof(msg), "System error: %s - %d", strerror(err), err);
568
+
569
+ } else if(SSL_ERROR_SSL == ssl_err) {
570
+ if(X509_V_OK != verify_err) {
571
+ err_str = X509_verify_cert_error_string(verify_err);
572
+ snprintf(msg, sizeof(msg),
573
+ "OpenSSL certificate verification error: %s - %d",
574
+ err_str, verify_err);
575
+
576
+ } else {
577
+ err = (int) ERR_get_error();
578
+ ERR_error_string_n(err, buf, sizeof(buf));
579
+ snprintf(msg, sizeof(msg), "OpenSSL error: %s - %d", buf, err & mask);
580
+ }
581
+ } else {
582
+ snprintf(msg, sizeof(msg), "Unknown OpenSSL error: %d", ssl_err);
583
+ }
584
+
585
+ ERR_clear_error();
586
+ rb_raise(eError, "%s", msg);
587
+ }
588
+
589
+ VALUE engine_read(VALUE self) {
590
+ ms_conn* conn;
591
+ char buf[512];
592
+ int bytes, error;
593
+
594
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
595
+
596
+ ERR_clear_error();
597
+
598
+ bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
599
+
600
+ if(bytes > 0) {
601
+ return rb_str_new(buf, bytes);
602
+ }
603
+
604
+ if(SSL_want_read(conn->ssl)) return Qnil;
605
+
606
+ error = SSL_get_error(conn->ssl, bytes);
607
+
608
+ if(error == SSL_ERROR_ZERO_RETURN) {
609
+ rb_eof_error();
610
+ } else {
611
+ raise_error(conn->ssl, bytes);
612
+ }
613
+
614
+ return Qnil;
615
+ }
616
+
617
+ VALUE engine_write(VALUE self, VALUE str) {
618
+ ms_conn* conn;
619
+ int bytes;
620
+
621
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
622
+
623
+ StringValue(str);
624
+
625
+ ERR_clear_error();
626
+
627
+ bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
628
+ if(bytes > 0) {
629
+ return INT2FIX(bytes);
630
+ }
631
+
632
+ if(SSL_want_write(conn->ssl)) return Qnil;
633
+
634
+ raise_error(conn->ssl, bytes);
635
+
636
+ return Qnil;
637
+ }
638
+
639
+ VALUE engine_extract(VALUE self) {
640
+ ms_conn* conn;
641
+ int bytes;
642
+ size_t pending;
643
+ // https://www.openssl.org/docs/manmaster/man3/BIO_f_buffer.html
644
+ // crypto/bio/bf_buff.c DEFAULT_BUFFER_SIZE
645
+ char buf[4096];
646
+
647
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
648
+
649
+ pending = BIO_pending(conn->write);
650
+ if(pending > 0) {
651
+ bytes = BIO_read(conn->write, buf, sizeof(buf));
652
+ if(bytes > 0) {
653
+ return rb_str_new(buf, bytes);
654
+ } else if(!BIO_should_retry(conn->write)) {
655
+ raise_error(conn->ssl, bytes);
656
+ }
657
+ }
658
+
659
+ return Qnil;
660
+ }
661
+
662
+ VALUE engine_shutdown(VALUE self) {
663
+ ms_conn* conn;
664
+ int ok;
665
+
666
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
667
+
668
+ ERR_clear_error();
669
+
670
+ ok = SSL_shutdown(conn->ssl);
671
+ if (ok == 0) {
672
+ return Qfalse;
673
+ }
674
+
675
+ return Qtrue;
676
+ }
677
+
678
+ VALUE engine_init(VALUE self) {
679
+ ms_conn* conn;
680
+
681
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
682
+
683
+ return SSL_in_init(conn->ssl) ? Qtrue : Qfalse;
684
+ }
685
+
686
+ VALUE engine_peercert(VALUE self) {
687
+ ms_conn* conn;
688
+ X509* cert;
689
+ int bytes;
690
+ unsigned char* buf = NULL;
691
+ ms_cert_buf* cert_buf = NULL;
692
+ VALUE rb_cert_buf;
693
+
694
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
695
+
696
+ #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE
697
+ cert = SSL_get1_peer_certificate(conn->ssl);
698
+ #else
699
+ cert = SSL_get_peer_certificate(conn->ssl);
700
+ #endif
701
+ if(!cert) {
702
+ /*
703
+ * See if there was a failed certificate associated with this client.
704
+ */
705
+ cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
706
+ if(!cert_buf) {
707
+ return Qnil;
708
+ }
709
+ buf = cert_buf->buf;
710
+ bytes = cert_buf->bytes;
711
+
712
+ } else {
713
+ bytes = i2d_X509(cert, &buf);
714
+ X509_free(cert);
715
+
716
+ if(bytes < 0) {
717
+ return Qnil;
718
+ }
719
+ }
720
+
721
+ rb_cert_buf = rb_str_new((const char*)(buf), bytes);
722
+ if(!cert_buf) {
723
+ OPENSSL_free(buf);
724
+ }
725
+
726
+ return rb_cert_buf;
727
+ }
728
+
729
+ /* @see Puma::MiniSSL::Socket#ssl_version_state
730
+ * @version 5.0.0
731
+ */
732
+ static VALUE
733
+ engine_ssl_vers_st(VALUE self) {
734
+ ms_conn* conn;
735
+ TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
736
+ return rb_ary_new3(2, rb_str_new2(SSL_get_version(conn->ssl)), rb_str_new2(SSL_state_string(conn->ssl)));
737
+ }
738
+
739
+ VALUE noop(VALUE self) {
740
+ return Qnil;
741
+ }
742
+
743
+ void Init_mini_ssl(VALUE puma) {
744
+ VALUE mod, eng, sslctx;
745
+
746
+ /* Fake operation for documentation (RDoc, YARD) */
747
+ #if 0 == 1
748
+ puma = rb_define_module("Puma");
749
+ #endif
750
+
751
+ SSL_library_init();
752
+ OpenSSL_add_ssl_algorithms();
753
+ SSL_load_error_strings();
754
+ ERR_load_crypto_strings();
755
+
756
+ mod = rb_define_module_under(puma, "MiniSSL");
757
+
758
+ eng = rb_define_class_under(mod, "Engine", rb_cObject);
759
+ rb_undef_alloc_func(eng);
760
+
761
+ sslctx = rb_define_class_under(mod, "SSLContext", rb_cObject);
762
+ rb_define_alloc_func(sslctx, sslctx_alloc);
763
+ rb_define_method(sslctx, "initialize", sslctx_initialize, 1);
764
+ rb_undef_method(sslctx, "initialize_copy");
765
+
766
+
767
+ // OpenSSL Build / Runtime/Load versions
768
+
769
+ /* Version of OpenSSL that Puma was compiled with */
770
+ rb_define_const(mod, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
771
+
772
+ #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
773
+ /* Version of OpenSSL that Puma loaded with */
774
+ rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
775
+ #else
776
+ rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
777
+ #endif
778
+
779
+ #if defined(OPENSSL_NO_SSL3) || defined(OPENSSL_NO_SSL3_METHOD)
780
+ /* True if SSL3 is not available */
781
+ rb_define_const(mod, "OPENSSL_NO_SSL3", Qtrue);
782
+ #else
783
+ rb_define_const(mod, "OPENSSL_NO_SSL3", Qfalse);
784
+ #endif
785
+
786
+ #if defined(OPENSSL_NO_TLS1) || defined(OPENSSL_NO_TLS1_METHOD)
787
+ /* True if TLS1 is not available */
788
+ rb_define_const(mod, "OPENSSL_NO_TLS1", Qtrue);
789
+ #else
790
+ rb_define_const(mod, "OPENSSL_NO_TLS1", Qfalse);
791
+ #endif
792
+
793
+ #if defined(OPENSSL_NO_TLS1_1) || defined(OPENSSL_NO_TLS1_1_METHOD)
794
+ /* True if TLS1_1 is not available */
795
+ rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qtrue);
796
+ #else
797
+ rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qfalse);
798
+ #endif
799
+
800
+ rb_define_singleton_method(mod, "check", noop, 0);
801
+
802
+ eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
803
+
804
+ rb_define_singleton_method(eng, "server", engine_init_server, 1);
805
+ rb_define_singleton_method(eng, "client", engine_init_client, 0);
806
+
807
+ rb_define_method(eng, "inject", engine_inject, 1);
808
+ rb_define_method(eng, "read", engine_read, 0);
809
+
810
+ rb_define_method(eng, "write", engine_write, 1);
811
+ rb_define_method(eng, "extract", engine_extract, 0);
812
+
813
+ rb_define_method(eng, "shutdown", engine_shutdown, 0);
814
+
815
+ rb_define_method(eng, "init?", engine_init, 0);
816
+
817
+ /* @!attribute [r] peercert
818
+ * Returns `nil` when `MiniSSL::Context#verify_mode` is set to `VERIFY_NONE`.
819
+ * @return [String, nil] DER encoded cert
820
+ */
821
+ rb_define_method(eng, "peercert", engine_peercert, 0);
822
+
823
+ rb_define_method(eng, "ssl_vers_st", engine_ssl_vers_st, 0);
824
+ }
825
+
826
+ #else
827
+
828
+ NORETURN(VALUE raise_error(VALUE self));
829
+
830
+ VALUE raise_error(VALUE self) {
831
+ rb_raise(rb_eStandardError, "SSL not available in this build");
832
+ }
833
+
834
+ void Init_mini_ssl(VALUE puma) {
835
+ VALUE mod;
836
+
837
+ mod = rb_define_module_under(puma, "MiniSSL");
838
+ rb_define_class_under(mod, "SSLError", rb_eStandardError);
839
+
840
+ rb_define_singleton_method(mod, "check", raise_error, 0);
841
+ }
842
+ #endif