2013-09-04 14:15:56 -04:00
|
|
|
#define RSTRING_NOT_MODIFIED 1
|
2012-08-22 19:53:25 -04:00
|
|
|
#include <ruby.h>
|
2012-08-23 01:38:41 -04:00
|
|
|
#include <rubyio.h>
|
2012-08-22 19:53:25 -04:00
|
|
|
#include <openssl/bio.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
BIO* read;
|
|
|
|
BIO* write;
|
|
|
|
SSL* ssl;
|
|
|
|
SSL_CTX* ctx;
|
|
|
|
} ms_conn;
|
|
|
|
|
|
|
|
void engine_free(ms_conn* conn) {
|
|
|
|
BIO_free(conn->read);
|
|
|
|
BIO_free(conn->write);
|
|
|
|
|
|
|
|
free(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
ms_conn* engine_alloc(VALUE klass, VALUE* obj) {
|
|
|
|
ms_conn* conn;
|
|
|
|
|
|
|
|
*obj = Data_Make_Struct(klass, ms_conn, 0, engine_free, conn);
|
|
|
|
|
|
|
|
conn->read = BIO_new(BIO_s_mem());
|
|
|
|
BIO_set_nbio(conn->read, 1);
|
|
|
|
|
|
|
|
conn->write = BIO_new(BIO_s_mem());
|
|
|
|
BIO_set_nbio(conn->write, 1);
|
|
|
|
|
|
|
|
conn->ssl = 0;
|
|
|
|
conn->ctx = 0;
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE engine_init_server(VALUE self, VALUE key, VALUE cert) {
|
|
|
|
VALUE obj;
|
2012-11-29 14:34:46 -05:00
|
|
|
SSL_CTX* ctx;
|
|
|
|
SSL* ssl;
|
|
|
|
|
2012-08-22 19:53:25 -04:00
|
|
|
ms_conn* conn = engine_alloc(self, &obj);
|
|
|
|
|
|
|
|
StringValue(key);
|
|
|
|
StringValue(cert);
|
|
|
|
|
2012-11-29 14:34:46 -05:00
|
|
|
ctx = SSL_CTX_new(SSLv23_server_method());
|
2012-08-22 19:53:25 -04:00
|
|
|
conn->ctx = ctx;
|
|
|
|
|
|
|
|
SSL_CTX_use_certificate_file(ctx, RSTRING_PTR(cert), SSL_FILETYPE_PEM);
|
2012-08-23 00:43:40 -04:00
|
|
|
SSL_CTX_use_PrivateKey_file(ctx, RSTRING_PTR(key), SSL_FILETYPE_PEM);
|
|
|
|
/* SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE); */
|
2012-08-22 19:53:25 -04:00
|
|
|
|
2012-11-29 14:34:46 -05:00
|
|
|
ssl = SSL_new(ctx);
|
2012-08-22 19:53:25 -04:00
|
|
|
conn->ssl = ssl;
|
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
/* SSL_set_verify(ssl, SSL_VERIFY_NONE, NULL); */
|
2012-08-22 19:53:25 -04:00
|
|
|
|
|
|
|
SSL_set_bio(ssl, conn->read, conn->write);
|
|
|
|
|
|
|
|
SSL_set_accept_state(ssl);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE engine_init_client(VALUE klass) {
|
|
|
|
VALUE obj;
|
|
|
|
ms_conn* conn = engine_alloc(klass, &obj);
|
|
|
|
|
|
|
|
conn->ctx = SSL_CTX_new(DTLSv1_method());
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
VALUE engine_inject(VALUE self, VALUE str) {
|
2012-08-22 19:53:25 -04:00
|
|
|
ms_conn* conn;
|
|
|
|
long used;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, ms_conn, conn);
|
|
|
|
|
|
|
|
StringValue(str);
|
|
|
|
|
2012-11-29 14:34:46 -05:00
|
|
|
used = BIO_write(conn->read, RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
2012-08-22 19:53:25 -04:00
|
|
|
|
|
|
|
if(used == 0 || used == -1) {
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INT2FIX(used);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE eError;
|
|
|
|
|
|
|
|
void raise_error(SSL* ssl, int result) {
|
|
|
|
int error = SSL_get_error(ssl, result);
|
2012-08-23 01:34:10 -04:00
|
|
|
char* msg = ERR_error_string(error, NULL);
|
2012-08-22 19:53:25 -04:00
|
|
|
|
2012-08-23 01:34:10 -04:00
|
|
|
ERR_clear_error();
|
|
|
|
rb_raise(eError, "OpenSSL error: %s - %d", msg, error);
|
2012-08-22 19:53:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2012-08-23 01:34:10 -04:00
|
|
|
if(SSL_get_error(conn->ssl, bytes) == SSL_ERROR_ZERO_RETURN) {
|
|
|
|
rb_eof_error();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:53:25 -04:00
|
|
|
raise_error(conn->ssl, bytes);
|
2012-08-23 01:38:41 -04:00
|
|
|
|
|
|
|
return Qnil;
|
2012-08-22 19:53:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE engine_write(VALUE self, VALUE str) {
|
|
|
|
ms_conn* conn;
|
|
|
|
char buf[512];
|
|
|
|
int bytes;
|
|
|
|
|
|
|
|
Data_Get_Struct(self, ms_conn, conn);
|
|
|
|
|
|
|
|
StringValue(str);
|
|
|
|
|
2012-11-29 14:34:46 -05:00
|
|
|
bytes = SSL_write(conn->ssl, (void*)RSTRING_PTR(str), (int)RSTRING_LEN(str));
|
2012-08-22 19:53:25 -04:00
|
|
|
if(bytes > 0) {
|
|
|
|
return INT2FIX(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SSL_want_write(conn->ssl)) return Qnil;
|
|
|
|
|
|
|
|
raise_error(conn->ssl, bytes);
|
2012-08-23 01:38:41 -04:00
|
|
|
|
|
|
|
return Qnil;
|
2012-08-22 19:53:25 -04:00
|
|
|
}
|
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
VALUE engine_extract(VALUE self) {
|
2012-08-22 19:53:25 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-23 01:12:12 -04:00
|
|
|
void Init_mini_ssl(VALUE puma) {
|
2012-11-29 14:34:46 -05:00
|
|
|
VALUE mod, eng;
|
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
SSL_library_init();
|
|
|
|
OpenSSL_add_ssl_algorithms();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
|
2012-11-29 14:34:46 -05:00
|
|
|
mod = rb_define_module_under(puma, "MiniSSL");
|
|
|
|
eng = rb_define_class_under(mod, "Engine", rb_cObject);
|
2012-08-22 19:53:25 -04:00
|
|
|
|
|
|
|
eError = rb_define_class_under(mod, "SSLError", rb_eStandardError);
|
|
|
|
|
|
|
|
rb_define_singleton_method(eng, "server", engine_init_server, 2);
|
|
|
|
rb_define_singleton_method(eng, "client", engine_init_client, 0);
|
|
|
|
|
2012-08-23 00:43:40 -04:00
|
|
|
rb_define_method(eng, "inject", engine_inject, 1);
|
2012-08-22 19:53:25 -04:00
|
|
|
rb_define_method(eng, "read", engine_read, 0);
|
|
|
|
|
|
|
|
rb_define_method(eng, "write", engine_write, 1);
|
2012-08-23 00:43:40 -04:00
|
|
|
rb_define_method(eng, "extract", engine_extract, 0);
|
2012-08-22 19:53:25 -04:00
|
|
|
}
|