puma 8.0.3-java → 8.1.1-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 (66) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +125 -0
  3. data/README.md +28 -378
  4. data/docs/binding.md +135 -0
  5. data/docs/cluster_mode.md +241 -0
  6. data/docs/configuration.md +78 -0
  7. data/docs/{stats.md → control_server.md} +28 -8
  8. data/docs/{deployment.md → deployment/README.md} +23 -7
  9. data/docs/{kubernetes.md → deployment/kubernetes.md} +4 -4
  10. data/docs/{systemd.md → deployment/systemd.md} +4 -2
  11. data/docs/plugins.md +62 -3
  12. data/docs/puma_support.md +92 -0
  13. data/docs/rails_dev_mode.md +2 -2
  14. data/docs/restart.md +8 -3
  15. data/docs/signals.md +4 -2
  16. data/docs/{5.0-Upgrade.md → upgrading/5.0-Upgrade.md} +3 -3
  17. data/docs/{6.0-Upgrade.md → upgrading/6.0-Upgrade.md} +3 -3
  18. data/docs/{7.0-Upgrade.md → upgrading/7.0-Upgrade.md} +2 -2
  19. data/docs/{8.0-Upgrade.md → upgrading/8.0-Upgrade.md} +4 -4
  20. data/lib/puma/binder.rb +29 -0
  21. data/lib/puma/cli.rb +6 -0
  22. data/lib/puma/client.rb +75 -29
  23. data/lib/puma/client_env.rb +32 -18
  24. data/lib/puma/cluster/worker.rb +109 -33
  25. data/lib/puma/cluster/worker_handle.rb +16 -0
  26. data/lib/puma/cluster.rb +173 -45
  27. data/lib/puma/commonlogger.rb +3 -3
  28. data/lib/puma/configuration.rb +22 -3
  29. data/lib/puma/const.rb +11 -9
  30. data/lib/puma/dsl.rb +265 -91
  31. data/lib/puma/launcher.rb +6 -4
  32. data/lib/puma/minissl.rb +2 -5
  33. data/lib/puma/plugin/systemd.rb +1 -1
  34. data/lib/puma/puma_http11.jar +0 -0
  35. data/lib/puma/response.rb +13 -5
  36. data/lib/puma/runner.rb +5 -1
  37. data/lib/puma/sd_notify.rb +4 -1
  38. data/lib/puma/server.rb +45 -20
  39. data/lib/puma/thread_pool.rb +12 -1
  40. data/lib/puma.rb +1 -1
  41. metadata +17 -34
  42. data/docs/fork_worker.md +0 -41
  43. data/docs/grpc.md +0 -62
  44. data/docs/jungle/README.md +0 -9
  45. data/docs/testing_benchmarks_local_files.md +0 -150
  46. data/docs/testing_test_rackup_ci_files.md +0 -36
  47. data/ext/puma_http11/PumaHttp11Service.java +0 -17
  48. data/ext/puma_http11/extconf.rb +0 -65
  49. data/ext/puma_http11/http11_parser.c +0 -1057
  50. data/ext/puma_http11/http11_parser.h +0 -65
  51. data/ext/puma_http11/http11_parser.java.rl +0 -131
  52. data/ext/puma_http11/http11_parser.rl +0 -149
  53. data/ext/puma_http11/http11_parser_common.rl +0 -54
  54. data/ext/puma_http11/mini_ssl.c +0 -859
  55. data/ext/puma_http11/no_ssl/PumaHttp11Service.java +0 -15
  56. data/ext/puma_http11/org/jruby/puma/EnvKey.java +0 -241
  57. data/ext/puma_http11/org/jruby/puma/Http11.java +0 -321
  58. data/ext/puma_http11/org/jruby/puma/Http11Parser.java +0 -441
  59. data/ext/puma_http11/org/jruby/puma/MiniSSL.java +0 -519
  60. data/ext/puma_http11/puma_http11.c +0 -500
  61. data/tools/Dockerfile +0 -26
  62. data/tools/trickletest.rb +0 -44
  63. /data/docs/{nginx.md → deployment/nginx.md} +0 -0
  64. /data/docs/{jungle → deployment}/rc.d/README.md +0 -0
  65. /data/docs/{jungle → deployment}/rc.d/puma +0 -0
  66. /data/docs/{jungle → deployment}/rc.d/puma.conf +0 -0
@@ -1,859 +0,0 @@
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
- ERR_clear_error();
444
- if (SSL_CTX_set_cipher_list(ctx, RSTRING_PTR(ssl_cipher_filter)) != 1) {
445
- if (ERR_GET_REASON(ERR_peek_last_error()) == SSL_R_NO_CIPHER_MATCH) {
446
- ERR_clear_error();
447
- } else {
448
- raise_param_error("SSL_CTX_set_cipher_list", "ssl_cipher_filter");
449
- }
450
- }
451
- }
452
- else {
453
- SSL_CTX_set_cipher_list(ctx, "HIGH:!aNULL@STRENGTH");
454
- }
455
-
456
- #if HAVE_SSL_CTX_SET_CIPHERSUITES
457
- // Only override OpenSSL default ciphersuites if config option is supplied.
458
- if (!NIL_P(ssl_ciphersuites)) {
459
- StringValue(ssl_ciphersuites);
460
- SSL_CTX_set_ciphersuites(ctx, RSTRING_PTR(ssl_ciphersuites));
461
- }
462
- #endif
463
-
464
- #if OPENSSL_VERSION_NUMBER < 0x10002000L
465
- // Remove this case if OpenSSL 1.0.1 (now EOL) support is no longer needed.
466
- ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
467
- if (ecdh) {
468
- SSL_CTX_set_tmp_ecdh(ctx, ecdh);
469
- EC_KEY_free(ecdh);
470
- }
471
- #elif OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
472
- SSL_CTX_set_ecdh_auto(ctx, 1);
473
- #endif
474
-
475
- if (NIL_P(verify_mode)) {
476
- /* SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL); */
477
- } else {
478
- SSL_CTX_set_verify(ctx, NUM2INT(verify_mode), engine_verify_callback);
479
- }
480
-
481
- session_id_bytes = rb_funcall(
482
- rb_cRandom,
483
- rb_intern_const("bytes"),
484
- 1, ULL2NUM(SSL_MAX_SSL_SESSION_ID_LENGTH));
485
-
486
- SSL_CTX_set_session_id_context(ctx,
487
- (unsigned char *) RSTRING_PTR(session_id_bytes),
488
- SSL_MAX_SSL_SESSION_ID_LENGTH);
489
-
490
- // printf("\ninitialize end security_level %d\n", SSL_CTX_get_security_level(ctx));
491
-
492
- #ifdef HAVE_SSL_CTX_SET_DH_AUTO
493
- // https://www.openssl.org/docs/man3.0/man3/SSL_CTX_set_dh_auto.html
494
- SSL_CTX_set_dh_auto(ctx, 1);
495
- #else
496
- dh = get_dh2048();
497
- SSL_CTX_set_tmp_dh(ctx, dh);
498
- #endif
499
-
500
- rb_obj_freeze(self);
501
- return self;
502
- }
503
-
504
- VALUE engine_init_server(VALUE self, VALUE sslctx) {
505
- ms_conn* conn;
506
- VALUE obj;
507
- SSL_CTX* ctx;
508
- SSL* ssl;
509
-
510
- conn = engine_alloc(self, &obj);
511
-
512
- TypedData_Get_Struct(sslctx, SSL_CTX, &sslctx_type, ctx);
513
-
514
- ssl = SSL_new(ctx);
515
- conn->ssl = ssl;
516
- SSL_set_app_data(ssl, NULL);
517
- SSL_set_bio(ssl, conn->read, conn->write);
518
- SSL_set_accept_state(ssl);
519
- return obj;
520
- }
521
-
522
- VALUE engine_init_client(VALUE klass) {
523
- VALUE obj;
524
- ms_conn* conn = engine_alloc(klass, &obj);
525
- #ifdef HAVE_DTLS_METHOD
526
- conn->ctx = SSL_CTX_new(DTLS_method());
527
- #else
528
- conn->ctx = SSL_CTX_new(DTLSv1_method());
529
- #endif
530
- conn->ssl = SSL_new(conn->ctx);
531
- SSL_set_app_data(conn->ssl, NULL);
532
- SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
533
-
534
- SSL_set_bio(conn->ssl, conn->read, conn->write);
535
-
536
- SSL_set_connect_state(conn->ssl);
537
- return obj;
538
- }
539
-
540
- VALUE engine_inject(VALUE self, VALUE str) {
541
- ms_conn* conn;
542
- long used;
543
-
544
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
545
-
546
- StringValue(str);
547
-
548
- used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
549
-
550
- if(used == 0 || used == -1) {
551
- return Qfalse;
552
- }
553
-
554
- return INT2FIX(used);
555
- }
556
-
557
- NORETURN(void raise_error(SSL* ssl, int result));
558
-
559
- void raise_error(SSL* ssl, int result) {
560
- char buf[512];
561
- char msg[768];
562
- const char* err_str;
563
- int err = errno;
564
- int mask = 4095;
565
- int ssl_err = SSL_get_error(ssl, result);
566
- int verify_err = (int) SSL_get_verify_result(ssl);
567
-
568
- if(SSL_ERROR_SYSCALL == ssl_err) {
569
- snprintf(msg, sizeof(msg), "System error: %s - %d", strerror(err), err);
570
-
571
- } else if(SSL_ERROR_SSL == ssl_err) {
572
- if(X509_V_OK != verify_err) {
573
- err_str = X509_verify_cert_error_string(verify_err);
574
- snprintf(msg, sizeof(msg),
575
- "OpenSSL certificate verification error: %s - %d",
576
- err_str, verify_err);
577
-
578
- } else {
579
- err = (int) ERR_get_error();
580
- ERR_error_string_n(err, buf, sizeof(buf));
581
- snprintf(msg, sizeof(msg), "OpenSSL error: %s - %d", buf, err & mask);
582
- }
583
- } else {
584
- snprintf(msg, sizeof(msg), "Unknown OpenSSL error: %d", ssl_err);
585
- }
586
-
587
- ERR_clear_error();
588
- rb_raise(eError, "%s", msg);
589
- }
590
-
591
- VALUE engine_read(VALUE self) {
592
- ms_conn* conn;
593
- char buf[512];
594
- int bytes, error;
595
-
596
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
597
-
598
- ERR_clear_error();
599
-
600
- bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
601
-
602
- if(bytes > 0) {
603
- return rb_str_new(buf, bytes);
604
- }
605
-
606
- if(SSL_want_read(conn->ssl)) return Qnil;
607
-
608
- error = SSL_get_error(conn->ssl, bytes);
609
-
610
- if(error == SSL_ERROR_ZERO_RETURN) {
611
- rb_eof_error();
612
- } else {
613
- raise_error(conn->ssl, bytes);
614
- }
615
-
616
- return Qnil;
617
- }
618
-
619
- VALUE engine_write(VALUE self, VALUE str) {
620
- ms_conn* conn;
621
- int bytes;
622
-
623
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
624
-
625
- StringValue(str);
626
-
627
- ERR_clear_error();
628
-
629
- bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
630
- if(bytes > 0) {
631
- return INT2FIX(bytes);
632
- }
633
-
634
- if(SSL_want_write(conn->ssl)) return Qnil;
635
-
636
- raise_error(conn->ssl, bytes);
637
-
638
- return Qnil;
639
- }
640
-
641
- VALUE engine_extract(VALUE self) {
642
- ms_conn* conn;
643
- int bytes;
644
- size_t pending;
645
- // https://www.openssl.org/docs/manmaster/man3/BIO_f_buffer.html
646
- // crypto/bio/bf_buff.c DEFAULT_BUFFER_SIZE
647
- char buf[4096];
648
-
649
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
650
-
651
- pending = BIO_pending(conn->write);
652
- if(pending > 0) {
653
- bytes = BIO_read(conn->write, buf, sizeof(buf));
654
- if(bytes > 0) {
655
- return rb_str_new(buf, bytes);
656
- } else if(!BIO_should_retry(conn->write)) {
657
- raise_error(conn->ssl, bytes);
658
- }
659
- }
660
-
661
- return Qnil;
662
- }
663
-
664
- VALUE engine_shutdown(VALUE self) {
665
- ms_conn* conn;
666
- int ok;
667
-
668
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
669
-
670
- if (SSL_in_init(conn->ssl)) {
671
- // Avoid "shutdown while in init" error
672
- // See https://github.com/openssl/openssl/blob/openssl-3.5.2/ssl/ssl_lib.c#L2827-L2828
673
- return Qtrue;
674
- }
675
-
676
- ERR_clear_error();
677
-
678
- ok = SSL_shutdown(conn->ssl);
679
- // See https://github.com/openssl/openssl/blob/openssl-3.5.2/ssl/ssl_lib.c#L2792-L2797
680
- // for description of SSL_shutdown return values.
681
- switch (ok) {
682
- case 0:
683
- // "close notify" alert is sent by us.
684
- return Qfalse;
685
- case 1:
686
- // "close notify" alert was received from peer.
687
- return Qtrue;
688
- default:
689
- raise_error(conn->ssl, ok);
690
- }
691
-
692
- return Qnil;
693
- }
694
-
695
- VALUE engine_init(VALUE self) {
696
- ms_conn* conn;
697
-
698
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
699
-
700
- return SSL_in_init(conn->ssl) ? Qtrue : Qfalse;
701
- }
702
-
703
- VALUE engine_peercert(VALUE self) {
704
- ms_conn* conn;
705
- X509* cert;
706
- int bytes;
707
- unsigned char* buf = NULL;
708
- ms_cert_buf* cert_buf = NULL;
709
- VALUE rb_cert_buf;
710
-
711
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
712
-
713
- #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE
714
- cert = SSL_get1_peer_certificate(conn->ssl);
715
- #else
716
- cert = SSL_get_peer_certificate(conn->ssl);
717
- #endif
718
- if(!cert) {
719
- /*
720
- * See if there was a failed certificate associated with this client.
721
- */
722
- cert_buf = (ms_cert_buf*)SSL_get_app_data(conn->ssl);
723
- if(!cert_buf) {
724
- return Qnil;
725
- }
726
- buf = cert_buf->buf;
727
- bytes = cert_buf->bytes;
728
-
729
- } else {
730
- bytes = i2d_X509(cert, &buf);
731
- X509_free(cert);
732
-
733
- if(bytes < 0) {
734
- return Qnil;
735
- }
736
- }
737
-
738
- rb_cert_buf = rb_str_new((const char*)(buf), bytes);
739
- if(!cert_buf) {
740
- OPENSSL_free(buf);
741
- }
742
-
743
- return rb_cert_buf;
744
- }
745
-
746
- /* @see Puma::MiniSSL::Socket#ssl_version_state
747
- * @version 5.0.0
748
- */
749
- static VALUE
750
- engine_ssl_vers_st(VALUE self) {
751
- ms_conn* conn;
752
- TypedData_Get_Struct(self, ms_conn, &engine_data_type, conn);
753
- return rb_ary_new3(2, rb_str_new2(SSL_get_version(conn->ssl)), rb_str_new2(SSL_state_string(conn->ssl)));
754
- }
755
-
756
- VALUE noop(VALUE self) {
757
- return Qnil;
758
- }
759
-
760
- void Init_mini_ssl(VALUE puma) {
761
- VALUE mod, eng, sslctx;
762
-
763
- /* Fake operation for documentation (RDoc, YARD) */
764
- #if 0 == 1
765
- puma = rb_define_module("Puma");
766
- #endif
767
-
768
- SSL_library_init();
769
- OpenSSL_add_ssl_algorithms();
770
- SSL_load_error_strings();
771
- ERR_load_crypto_strings();
772
-
773
- mod = rb_define_module_under(puma, "MiniSSL");
774
-
775
- eng = rb_define_class_under(mod, "Engine", rb_cObject);
776
- rb_undef_alloc_func(eng);
777
-
778
- sslctx = rb_define_class_under(mod, "SSLContext", rb_cObject);
779
- rb_define_alloc_func(sslctx, sslctx_alloc);
780
- rb_define_method(sslctx, "initialize", sslctx_initialize, 1);
781
- rb_undef_method(sslctx, "initialize_copy");
782
-
783
-
784
- // OpenSSL Build / Runtime/Load versions
785
-
786
- /* Version of OpenSSL that Puma was compiled with */
787
- rb_define_const(mod, "OPENSSL_VERSION", rb_str_new2(OPENSSL_VERSION_TEXT));
788
-
789
- #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000
790
- /* Version of OpenSSL that Puma loaded with */
791
- rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(OpenSSL_version(OPENSSL_VERSION)));
792
- #else
793
- rb_define_const(mod, "OPENSSL_LIBRARY_VERSION", rb_str_new2(SSLeay_version(SSLEAY_VERSION)));
794
- #endif
795
-
796
- #if defined(OPENSSL_NO_SSL3) || defined(OPENSSL_NO_SSL3_METHOD)
797
- /* True if SSL3 is not available */
798
- rb_define_const(mod, "OPENSSL_NO_SSL3", Qtrue);
799
- #else
800
- rb_define_const(mod, "OPENSSL_NO_SSL3", Qfalse);
801
- #endif
802
-
803
- #if defined(OPENSSL_NO_TLS1) || defined(OPENSSL_NO_TLS1_METHOD)
804
- /* True if TLS1 is not available */
805
- rb_define_const(mod, "OPENSSL_NO_TLS1", Qtrue);
806
- #else
807
- rb_define_const(mod, "OPENSSL_NO_TLS1", Qfalse);
808
- #endif
809
-
810
- #if defined(OPENSSL_NO_TLS1_1) || defined(OPENSSL_NO_TLS1_1_METHOD)
811
- /* True if TLS1_1 is not available */
812
- rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qtrue);
813
- #else
814
- rb_define_const(mod, "OPENSSL_NO_TLS1_1", Qfalse);
815
- #endif
816
-
817
- rb_define_singleton_method(mod, "check", noop, 0);
818
-
819
- eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
820
-
821
- rb_define_singleton_method(eng, "server", engine_init_server, 1);
822
- rb_define_singleton_method(eng, "client", engine_init_client, 0);
823
-
824
- rb_define_method(eng, "inject", engine_inject, 1);
825
- rb_define_method(eng, "read", engine_read, 0);
826
-
827
- rb_define_method(eng, "write", engine_write, 1);
828
- rb_define_method(eng, "extract", engine_extract, 0);
829
-
830
- rb_define_method(eng, "shutdown", engine_shutdown, 0);
831
-
832
- rb_define_method(eng, "init?", engine_init, 0);
833
-
834
- /* @!attribute [r] peercert
835
- * Returns `nil` when `MiniSSL::Context#verify_mode` is set to `VERIFY_NONE`.
836
- * @return [String, nil] DER encoded cert
837
- */
838
- rb_define_method(eng, "peercert", engine_peercert, 0);
839
-
840
- rb_define_method(eng, "ssl_vers_st", engine_ssl_vers_st, 0);
841
- }
842
-
843
- #else
844
-
845
- NORETURN(VALUE raise_error(VALUE self));
846
-
847
- VALUE raise_error(VALUE self) {
848
- rb_raise(rb_eStandardError, "SSL not available in this build");
849
- }
850
-
851
- void Init_mini_ssl(VALUE puma) {
852
- VALUE mod;
853
-
854
- mod = rb_define_module_under(puma, "MiniSSL");
855
- rb_define_class_under(mod, "SSLError", rb_eStandardError);
856
-
857
- rb_define_singleton_method(mod, "check", raise_error, 0);
858
- }
859
- #endif