Class: Rack::TCTP::Engine
- Inherits:
-
Object
- Object
- Rack::TCTP::Engine
- Defined in:
- ext/engine/engine.c
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.client ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'ext/engine/engine.c', line 94
VALUE engine_init_client(VALUE klass) {
VALUE obj;
ms_conn* conn = engine_alloc(klass, &obj);
conn->ctx = SSL_CTX_new(TLSv1_client_method());
SSL_CTX_set_cipher_list(conn->ctx, "ALL");
conn->ssl = SSL_new(conn->ctx);
SSL_set_verify(conn->ssl, SSL_VERIFY_NONE, NULL);
SSL_set_bio(conn->ssl, conn->read, conn->write);
SSL_set_connect_state(conn->ssl);
return obj;
}
|
.server(key, cert) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'ext/engine/engine.c', line 56
VALUE engine_init_server(VALUE self, VALUE key, VALUE cert) {
VALUE obj;
SSL_CTX* ctx;
SSL* ssl;
int use_certificate_file_ret, use_pk_file_ret;
ms_conn* conn = engine_alloc(self, &obj);
StringValue(key);
StringValue(cert);
ctx = SSL_CTX_new(TLSv1_server_method());
conn->ctx = ctx;
use_certificate_file_ret = SSL_CTX_use_certificate_file(ctx, RSTRING_PTR(cert), SSL_FILETYPE_PEM);
if(use_certificate_file_ret != 1) {
raise_error(conn->ssl, 0);
}
use_pk_file_ret = SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
if(use_pk_file_ret != 1) {
raise_error(conn->ssl, 0);
}
SSL_CTX_set_cipher_list(ctx, "ALL");
SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
ssl = SSL_new(ctx);
conn->ssl = ssl;
SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL);
SSL_set_bio(conn->ssl, conn->read, conn->write);
SSL_set_accept_state(ssl);
return obj;
}
|
Instance Method Details
#extract ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'ext/engine/engine.c', line 173
VALUE engine_extract(VALUE self) {
ms_conn* conn;
int bytes;
size_t pending;
char buf[512];
Data_Get_Struct(self, ms_conn, conn);
pending = BIO_pending(conn->write);
if(pending > 0) {
bytes = BIO_read(conn->write, buf, sizeof(buf));
if(bytes > 0) {
return rb_str_new(buf, bytes);
} else if(!BIO_should_retry(conn->write)) {
raise_error(conn->ssl, bytes);
}
}
return Qnil;
}
|
#inject(str) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'ext/engine/engine.c', line 111
VALUE engine_inject(VALUE self, VALUE str) {
ms_conn* conn;
long used;
Data_Get_Struct(self, ms_conn, conn);
StringValue(str);
used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
if(used == 0 || used == -1) {
return Qfalse;
}
return INT2FIX(used);
}
|
#read ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'ext/engine/engine.c', line 128
VALUE engine_read(VALUE self) {
ms_conn* conn;
char buf[512];
int bytes, n;
Data_Get_Struct(self, ms_conn, conn);
bytes = SSL_read(conn->ssl, (void*)buf, sizeof(buf));
if(bytes > 0) {
return rb_str_new(buf, bytes);
}
if(SSL_want_read(conn->ssl)) return Qnil;
if(SSL_get_error(conn->ssl, bytes) == SSL_ERROR_ZERO_RETURN) {
rb_eof_error();
}
raise_error(conn->ssl, bytes);
return Qnil;
}
|
#write(str) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'ext/engine/engine.c', line 152
VALUE engine_write(VALUE self, VALUE str) {
ms_conn* conn;
char buf[512];
int bytes;
Data_Get_Struct(self, ms_conn, conn);
StringValue(str);
bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
if(bytes > 0) {
return INT2FIX(bytes);
}
if(SSL_want_write(conn->ssl)) return Qnil;
raise_error(conn->ssl, bytes);
return Qnil;
}
|