rack-tctp 0.9.13 → 0.9.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/engine/engine.c +229 -229
- data/ext/engine/extconf.rb +6 -6
- data/lib/rack/tctp.rb +263 -260
- data/lib/rack/tctp/halec.rb +220 -212
- metadata +32 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4e3c755444802e57349bd7046ad8f151de2f9b1
|
4
|
+
data.tar.gz: bdea2ab33607130a18f195cfc56b54eef24ea8d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 499b9e7a53bafd4380a5770eaefdfeedd3d9bc4822689abc393f9d9424f657b522f3da2829391ad0e02fca3d14546d0ec35b9c0c9f6f174feb064f9336c50e58
|
7
|
+
data.tar.gz: 07baec6b6bdc036c630ae739b978fd7e9bdc6a846a594af85f9c23453f077eb39907b68bd18ede1d7686f62d89ca88e745821f0e7d90be10555d2a1a7f84506c
|
data/ext/engine/engine.c
CHANGED
@@ -1,229 +1,229 @@
|
|
1
|
-
//Modified from: https://github.com/puma/puma/blob/master/ext/puma_http11/mini_ssl.c
|
2
|
-
#define RSTRING_NOT_MODIFIED 1
|
3
|
-
#include <assert.h>
|
4
|
-
#include <ruby.h>
|
5
|
-
#include <rubyio.h>
|
6
|
-
#include <openssl/bio.h>
|
7
|
-
#include <openssl/ssl.h>
|
8
|
-
#include <openssl/err.h>
|
9
|
-
|
10
|
-
typedef struct {
|
11
|
-
BIO* read;
|
12
|
-
BIO* write;
|
13
|
-
SSL* ssl;
|
14
|
-
SSL_CTX* ctx;
|
15
|
-
} ms_conn;
|
16
|
-
|
17
|
-
void engine_free(ms_conn* conn) {
|
18
|
-
BIO_free(conn->read);
|
19
|
-
BIO_free(conn->write);
|
20
|
-
|
21
|
-
free(conn);
|
22
|
-
}
|
23
|
-
|
24
|
-
static VALUE eError;
|
25
|
-
|
26
|
-
void raise_error(SSL* ssl, int result) {
|
27
|
-
char buf[256];
|
28
|
-
u_long err;
|
29
|
-
|
30
|
-
while ((err = ERR_get_error()) != 0) {
|
31
|
-
ERR_error_string_n(err, buf, sizeof(buf));
|
32
|
-
printf("*** %s\n", buf);
|
33
|
-
}
|
34
|
-
|
35
|
-
ERR_clear_error();
|
36
|
-
rb_raise(eError, "OpenSSL error");
|
37
|
-
}
|
38
|
-
|
39
|
-
ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
|
40
|
-
ms_conn* conn;
|
41
|
-
|
42
|
-
*obj = Data_Make_Struct(klass, ms_conn, 0, engine_free, conn);
|
43
|
-
|
44
|
-
conn->read = BIO_new(BIO_s_mem());
|
45
|
-
BIO_set_nbio(conn->read, 1);
|
46
|
-
|
47
|
-
conn->write = BIO_new(BIO_s_mem());
|
48
|
-
BIO_set_nbio(conn->write, 1);
|
49
|
-
|
50
|
-
conn->ssl = 0;
|
51
|
-
conn->ctx = 0;
|
52
|
-
|
53
|
-
return conn;
|
54
|
-
}
|
55
|
-
|
56
|
-
VALUE engine_init_server(VALUE self, VALUE key, VALUE cert) {
|
57
|
-
VALUE obj;
|
58
|
-
SSL_CTX* ctx;
|
59
|
-
SSL* ssl;
|
60
|
-
int use_certificate_file_ret, use_pk_file_ret;
|
61
|
-
|
62
|
-
ms_conn* conn = engine_alloc(self, &obj);
|
63
|
-
|
64
|
-
StringValue(key);
|
65
|
-
StringValue(cert);
|
66
|
-
|
67
|
-
ctx = SSL_CTX_new(TLSv1_server_method());
|
68
|
-
conn->ctx = ctx;
|
69
|
-
|
70
|
-
use_certificate_file_ret = SSL_CTX_use_certificate_file(ctx, RSTRING_PTR(cert), SSL_FILETYPE_PEM);
|
71
|
-
if(use_certificate_file_ret != 1) {
|
72
|
-
raise_error(conn->ssl, 0);
|
73
|
-
}
|
74
|
-
|
75
|
-
use_pk_file_ret = SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
|
76
|
-
if(use_pk_file_ret != 1) {
|
77
|
-
raise_error(conn->ssl, 0);
|
78
|
-
}
|
79
|
-
|
80
|
-
SSL_CTX_set_cipher_list(ctx, "ALL");
|
81
|
-
SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
|
82
|
-
|
83
|
-
ssl = SSL_new(ctx);
|
84
|
-
conn->ssl = ssl;
|
85
|
-
|
86
|
-
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
|
87
|
-
|
88
|
-
SSL_set_bio(conn->ssl, conn->read, conn->write);
|
89
|
-
|
90
|
-
SSL_set_accept_state(ssl);
|
91
|
-
return obj;
|
92
|
-
}
|
93
|
-
|
94
|
-
VALUE engine_init_client(VALUE klass) {
|
95
|
-
VALUE obj;
|
96
|
-
ms_conn* conn = engine_alloc(klass, &obj);
|
97
|
-
|
98
|
-
conn->ctx = SSL_CTX_new(TLSv1_client_method());
|
99
|
-
SSL_CTX_set_cipher_list(conn->ctx, "ALL");
|
100
|
-
|
101
|
-
conn->ssl = SSL_new(conn->ctx);
|
102
|
-
|
103
|
-
SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
|
104
|
-
|
105
|
-
SSL_set_bio(conn->ssl, conn->read, conn->write);
|
106
|
-
|
107
|
-
SSL_set_connect_state(conn->ssl);
|
108
|
-
return obj;
|
109
|
-
}
|
110
|
-
|
111
|
-
VALUE engine_inject(VALUE self, VALUE str) {
|
112
|
-
ms_conn* conn;
|
113
|
-
long used;
|
114
|
-
|
115
|
-
Data_Get_Struct(self, ms_conn, conn);
|
116
|
-
|
117
|
-
StringValue(str);
|
118
|
-
|
119
|
-
used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
120
|
-
|
121
|
-
if(used == 0 || used == -1) {
|
122
|
-
return Qfalse;
|
123
|
-
}
|
124
|
-
|
125
|
-
return INT2FIX(used);
|
126
|
-
}
|
127
|
-
|
128
|
-
VALUE engine_read(VALUE self) {
|
129
|
-
ms_conn* conn;
|
130
|
-
char buf[512];
|
131
|
-
int bytes, n;
|
132
|
-
|
133
|
-
Data_Get_Struct(self, ms_conn, conn);
|
134
|
-
|
135
|
-
bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
|
136
|
-
|
137
|
-
if(bytes > 0) {
|
138
|
-
return rb_str_new(buf, bytes);
|
139
|
-
}
|
140
|
-
|
141
|
-
if(SSL_want_read(conn->ssl)) return Qnil;
|
142
|
-
|
143
|
-
if(SSL_get_error(conn->ssl, bytes) == SSL_ERROR_ZERO_RETURN) {
|
144
|
-
rb_eof_error();
|
145
|
-
}
|
146
|
-
|
147
|
-
raise_error(conn->ssl, bytes);
|
148
|
-
|
149
|
-
return Qnil;
|
150
|
-
}
|
151
|
-
|
152
|
-
VALUE engine_write(VALUE self, VALUE str) {
|
153
|
-
ms_conn* conn;
|
154
|
-
char buf[512];
|
155
|
-
int bytes;
|
156
|
-
|
157
|
-
Data_Get_Struct(self, ms_conn, conn);
|
158
|
-
|
159
|
-
StringValue(str);
|
160
|
-
|
161
|
-
bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
162
|
-
if(bytes > 0) {
|
163
|
-
return INT2FIX(bytes);
|
164
|
-
}
|
165
|
-
|
166
|
-
if(SSL_want_write(conn->ssl)) return Qnil;
|
167
|
-
|
168
|
-
raise_error(conn->ssl, bytes);
|
169
|
-
|
170
|
-
return Qnil;
|
171
|
-
}
|
172
|
-
|
173
|
-
VALUE engine_extract(VALUE self) {
|
174
|
-
ms_conn* conn;
|
175
|
-
int bytes;
|
176
|
-
size_t pending;
|
177
|
-
char buf[512];
|
178
|
-
|
179
|
-
Data_Get_Struct(self, ms_conn, conn);
|
180
|
-
|
181
|
-
pending = BIO_pending(conn->write);
|
182
|
-
if(pending > 0) {
|
183
|
-
bytes = BIO_read(conn->write, buf, sizeof(buf));
|
184
|
-
if(bytes > 0) {
|
185
|
-
return rb_str_new(buf, bytes);
|
186
|
-
} else if(!BIO_should_retry(conn->write)) {
|
187
|
-
raise_error(conn->ssl, bytes);
|
188
|
-
}
|
189
|
-
}
|
190
|
-
|
191
|
-
return Qnil;
|
192
|
-
}
|
193
|
-
|
194
|
-
VALUE engine_state(VALUE self) {
|
195
|
-
ms_conn* conn;
|
196
|
-
VALUE str;
|
197
|
-
|
198
|
-
Data_Get_Struct(self, ms_conn, conn);
|
199
|
-
|
200
|
-
str = rb_str_new(SSL_state_string(conn->ssl), 6);
|
201
|
-
|
202
|
-
return str;
|
203
|
-
}
|
204
|
-
|
205
|
-
void Init_engine() {
|
206
|
-
VALUE mod, eng, rack;
|
207
|
-
|
208
|
-
SSL_library_init();
|
209
|
-
OpenSSL_add_ssl_algorithms();
|
210
|
-
SSL_load_error_strings();
|
211
|
-
ERR_load_crypto_strings();
|
212
|
-
|
213
|
-
rack = rb_define_module("Rack");
|
214
|
-
mod = rb_define_class_under(rack, "TCTP", rb_cObject);
|
215
|
-
eng = rb_define_class_under(mod, "Engine", rb_cObject);
|
216
|
-
|
217
|
-
eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
|
218
|
-
|
219
|
-
rb_define_singleton_method(eng, "server", engine_init_server, 2);
|
220
|
-
rb_define_singleton_method(eng, "client", engine_init_client, 0);
|
221
|
-
|
222
|
-
rb_define_method(eng, "inject", engine_inject, 1);
|
223
|
-
rb_define_method(eng, "read", engine_read, 0);
|
224
|
-
|
225
|
-
rb_define_method(eng, "write", engine_write, 1);
|
226
|
-
rb_define_method(eng, "extract", engine_extract, 0);
|
227
|
-
|
228
|
-
rb_define_method(eng, "state", engine_state, 0);
|
229
|
-
}
|
1
|
+
//Modified from: https://github.com/puma/puma/blob/master/ext/puma_http11/mini_ssl.c
|
2
|
+
#define RSTRING_NOT_MODIFIED 1
|
3
|
+
#include <assert.h>
|
4
|
+
#include <ruby.h>
|
5
|
+
#include <rubyio.h>
|
6
|
+
#include <openssl/bio.h>
|
7
|
+
#include <openssl/ssl.h>
|
8
|
+
#include <openssl/err.h>
|
9
|
+
|
10
|
+
typedef struct {
|
11
|
+
BIO* read;
|
12
|
+
BIO* write;
|
13
|
+
SSL* ssl;
|
14
|
+
SSL_CTX* ctx;
|
15
|
+
} ms_conn;
|
16
|
+
|
17
|
+
void engine_free(ms_conn* conn) {
|
18
|
+
BIO_free(conn->read);
|
19
|
+
BIO_free(conn->write);
|
20
|
+
|
21
|
+
free(conn);
|
22
|
+
}
|
23
|
+
|
24
|
+
static VALUE eError;
|
25
|
+
|
26
|
+
void raise_error(SSL* ssl, int result) {
|
27
|
+
char buf[256];
|
28
|
+
u_long err;
|
29
|
+
|
30
|
+
while ((err = ERR_get_error()) != 0) {
|
31
|
+
ERR_error_string_n(err, buf, sizeof(buf));
|
32
|
+
printf("*** %s\n", buf);
|
33
|
+
}
|
34
|
+
|
35
|
+
ERR_clear_error();
|
36
|
+
rb_raise(eError, "OpenSSL error");
|
37
|
+
}
|
38
|
+
|
39
|
+
ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
|
40
|
+
ms_conn* conn;
|
41
|
+
|
42
|
+
*obj = Data_Make_Struct(klass, ms_conn, 0, engine_free, conn);
|
43
|
+
|
44
|
+
conn->read = BIO_new(BIO_s_mem());
|
45
|
+
BIO_set_nbio(conn->read, 1);
|
46
|
+
|
47
|
+
conn->write = BIO_new(BIO_s_mem());
|
48
|
+
BIO_set_nbio(conn->write, 1);
|
49
|
+
|
50
|
+
conn->ssl = 0;
|
51
|
+
conn->ctx = 0;
|
52
|
+
|
53
|
+
return conn;
|
54
|
+
}
|
55
|
+
|
56
|
+
VALUE engine_init_server(VALUE self, VALUE key, VALUE cert) {
|
57
|
+
VALUE obj;
|
58
|
+
SSL_CTX* ctx;
|
59
|
+
SSL* ssl;
|
60
|
+
int use_certificate_file_ret, use_pk_file_ret;
|
61
|
+
|
62
|
+
ms_conn* conn = engine_alloc(self, &obj);
|
63
|
+
|
64
|
+
StringValue(key);
|
65
|
+
StringValue(cert);
|
66
|
+
|
67
|
+
ctx = SSL_CTX_new(TLSv1_server_method());
|
68
|
+
conn->ctx = ctx;
|
69
|
+
|
70
|
+
use_certificate_file_ret = SSL_CTX_use_certificate_file(ctx, RSTRING_PTR(cert), SSL_FILETYPE_PEM);
|
71
|
+
if(use_certificate_file_ret != 1) {
|
72
|
+
raise_error(conn->ssl, 0);
|
73
|
+
}
|
74
|
+
|
75
|
+
use_pk_file_ret = SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
|
76
|
+
if(use_pk_file_ret != 1) {
|
77
|
+
raise_error(conn->ssl, 0);
|
78
|
+
}
|
79
|
+
|
80
|
+
SSL_CTX_set_cipher_list(ctx, "ALL");
|
81
|
+
SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
|
82
|
+
|
83
|
+
ssl = SSL_new(ctx);
|
84
|
+
conn->ssl = ssl;
|
85
|
+
|
86
|
+
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
|
87
|
+
|
88
|
+
SSL_set_bio(conn->ssl, conn->read, conn->write);
|
89
|
+
|
90
|
+
SSL_set_accept_state(ssl);
|
91
|
+
return obj;
|
92
|
+
}
|
93
|
+
|
94
|
+
VALUE engine_init_client(VALUE klass) {
|
95
|
+
VALUE obj;
|
96
|
+
ms_conn* conn = engine_alloc(klass, &obj);
|
97
|
+
|
98
|
+
conn->ctx = SSL_CTX_new(TLSv1_client_method());
|
99
|
+
SSL_CTX_set_cipher_list(conn->ctx, "ALL");
|
100
|
+
|
101
|
+
conn->ssl = SSL_new(conn->ctx);
|
102
|
+
|
103
|
+
SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
|
104
|
+
|
105
|
+
SSL_set_bio(conn->ssl, conn->read, conn->write);
|
106
|
+
|
107
|
+
SSL_set_connect_state(conn->ssl);
|
108
|
+
return obj;
|
109
|
+
}
|
110
|
+
|
111
|
+
VALUE engine_inject(VALUE self, VALUE str) {
|
112
|
+
ms_conn* conn;
|
113
|
+
long used;
|
114
|
+
|
115
|
+
Data_Get_Struct(self, ms_conn, conn);
|
116
|
+
|
117
|
+
StringValue(str);
|
118
|
+
|
119
|
+
used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
120
|
+
|
121
|
+
if(used == 0 || used == -1) {
|
122
|
+
return Qfalse;
|
123
|
+
}
|
124
|
+
|
125
|
+
return INT2FIX(used);
|
126
|
+
}
|
127
|
+
|
128
|
+
VALUE engine_read(VALUE self) {
|
129
|
+
ms_conn* conn;
|
130
|
+
char buf[512];
|
131
|
+
int bytes, n;
|
132
|
+
|
133
|
+
Data_Get_Struct(self, ms_conn, conn);
|
134
|
+
|
135
|
+
bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
|
136
|
+
|
137
|
+
if(bytes > 0) {
|
138
|
+
return rb_str_new(buf, bytes);
|
139
|
+
}
|
140
|
+
|
141
|
+
if(SSL_want_read(conn->ssl)) return Qnil;
|
142
|
+
|
143
|
+
if(SSL_get_error(conn->ssl, bytes) == SSL_ERROR_ZERO_RETURN) {
|
144
|
+
rb_eof_error();
|
145
|
+
}
|
146
|
+
|
147
|
+
raise_error(conn->ssl, bytes);
|
148
|
+
|
149
|
+
return Qnil;
|
150
|
+
}
|
151
|
+
|
152
|
+
VALUE engine_write(VALUE self, VALUE str) {
|
153
|
+
ms_conn* conn;
|
154
|
+
char buf[512];
|
155
|
+
int bytes;
|
156
|
+
|
157
|
+
Data_Get_Struct(self, ms_conn, conn);
|
158
|
+
|
159
|
+
StringValue(str);
|
160
|
+
|
161
|
+
bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
162
|
+
if(bytes > 0) {
|
163
|
+
return INT2FIX(bytes);
|
164
|
+
}
|
165
|
+
|
166
|
+
if(SSL_want_write(conn->ssl)) return Qnil;
|
167
|
+
|
168
|
+
raise_error(conn->ssl, bytes);
|
169
|
+
|
170
|
+
return Qnil;
|
171
|
+
}
|
172
|
+
|
173
|
+
VALUE engine_extract(VALUE self) {
|
174
|
+
ms_conn* conn;
|
175
|
+
int bytes;
|
176
|
+
size_t pending;
|
177
|
+
char buf[512];
|
178
|
+
|
179
|
+
Data_Get_Struct(self, ms_conn, conn);
|
180
|
+
|
181
|
+
pending = BIO_pending(conn->write);
|
182
|
+
if(pending > 0) {
|
183
|
+
bytes = BIO_read(conn->write, buf, sizeof(buf));
|
184
|
+
if(bytes > 0) {
|
185
|
+
return rb_str_new(buf, bytes);
|
186
|
+
} else if(!BIO_should_retry(conn->write)) {
|
187
|
+
raise_error(conn->ssl, bytes);
|
188
|
+
}
|
189
|
+
}
|
190
|
+
|
191
|
+
return Qnil;
|
192
|
+
}
|
193
|
+
|
194
|
+
VALUE engine_state(VALUE self) {
|
195
|
+
ms_conn* conn;
|
196
|
+
VALUE str;
|
197
|
+
|
198
|
+
Data_Get_Struct(self, ms_conn, conn);
|
199
|
+
|
200
|
+
str = rb_str_new(SSL_state_string(conn->ssl), 6);
|
201
|
+
|
202
|
+
return str;
|
203
|
+
}
|
204
|
+
|
205
|
+
void Init_engine() {
|
206
|
+
VALUE mod, eng, rack;
|
207
|
+
|
208
|
+
SSL_library_init();
|
209
|
+
OpenSSL_add_ssl_algorithms();
|
210
|
+
SSL_load_error_strings();
|
211
|
+
ERR_load_crypto_strings();
|
212
|
+
|
213
|
+
rack = rb_define_module("Rack");
|
214
|
+
mod = rb_define_class_under(rack, "TCTP", rb_cObject);
|
215
|
+
eng = rb_define_class_under(mod, "Engine", rb_cObject);
|
216
|
+
|
217
|
+
eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
|
218
|
+
|
219
|
+
rb_define_singleton_method(eng, "server", engine_init_server, 2);
|
220
|
+
rb_define_singleton_method(eng, "client", engine_init_client, 0);
|
221
|
+
|
222
|
+
rb_define_method(eng, "inject", engine_inject, 1);
|
223
|
+
rb_define_method(eng, "read", engine_read, 0);
|
224
|
+
|
225
|
+
rb_define_method(eng, "write", engine_write, 1);
|
226
|
+
rb_define_method(eng, "extract", engine_extract, 0);
|
227
|
+
|
228
|
+
rb_define_method(eng, "state", engine_state, 0);
|
229
|
+
}
|