Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Ruby 2.2+ compatibility #1

Merged
merged 2 commits into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions ext/openssl/cipher/aead/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
VALUE dOSSL;
VALUE eCipherError;

#define GetCipherInit(obj, ctx) do { \
Data_Get_Struct((obj), EVP_CIPHER_CTX, (ctx)); \
} while (0)

#define GetCipher(obj, ctx) do { \
GetCipherInit((obj), (ctx)); \
if (!(ctx)) { \
ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \
} \
} while (0)
#define GetCipherInit(obj, ctx) do { \
TypedData_Get_Struct((obj), EVP_CIPHER_CTX, RTYPEDDATA_TYPE(obj), (ctx)); \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably the passed obj is being set with TypedData_Wrap_Struct? Is that done by openssl?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, ext/openssl https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L12 , https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L88 && https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L936

This gem basically monkey patches a Data object, which strictly requires having to use the same typed data struct. Also upstream the parent of Cipher should be rb_cData and not rb_cObject as per the extension docs - https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_cipher.c#L933 , https://github.com/ruby/ruby/blob/trunk/doc/extension.rdoc#c-struct-to-ruby-object :

It is recommended that klass derives from a special class called Data (rb_cData) but not from Object or other ordinal classes. If it doesn't, you have to call rb_undef_alloc_func(klass).

} while (0)
#define GetCipher(obj, ctx) do { \
GetCipherInit((obj), (ctx)); \
if (!(ctx)) { \
ossl_raise(rb_eRuntimeError, "Cipher not inititalized!"); \
} \
} while (0)

static VALUE
ossl_make_error(VALUE exc, const char *fmt, va_list args)
Expand Down Expand Up @@ -65,7 +64,7 @@ static VALUE
ossl_cipher_set_aad(VALUE self, VALUE data)
{
EVP_CIPHER_CTX *ctx;
char *in = NULL;
unsigned char *in = NULL;
int in_len = 0;
int out_len = 0;

Expand Down Expand Up @@ -106,7 +105,7 @@ static VALUE
ossl_cipher_set_tag(VALUE self, VALUE data)
{
EVP_CIPHER_CTX *ctx;
char *in = NULL;
unsigned char *in = NULL;
int in_len = 0;

StringValue(data);
Expand All @@ -131,7 +130,6 @@ ossl_cipher_set_iv_length(VALUE self, VALUE iv_length)
{
EVP_CIPHER_CTX *ctx;
int ivlen = NUM2INT(iv_length);

GetCipher(self, ctx);

#ifndef EVP_CTRL_GCM_SET_IVLEN
Expand Down Expand Up @@ -161,15 +159,13 @@ ossl_cipher_verify(VALUE self)
void
Init_aead(void)
{
VALUE mOSSL = rb_define_module("OpenSSL");
VALUE mOSSLCipher = rb_define_class_under(mOSSL, "Cipher", rb_cObject);
VALUE eOSSLError = rb_define_class_under(mOSSL,"OpenSSLError",rb_eStandardError);

eCipherError = rb_define_class_under(mOSSLCipher, "CipherError", eOSSLError);

rb_define_method(mOSSLCipher, "aad=", ossl_cipher_set_aad, 1);
rb_define_method(mOSSLCipher, "gcm_tag", ossl_cipher_get_tag, 0);
rb_define_method(mOSSLCipher, "gcm_tag=", ossl_cipher_set_tag, 1);
rb_define_method(mOSSLCipher, "gcm_iv_len=", ossl_cipher_set_iv_length, 1);
rb_define_method(mOSSLCipher, "verify", ossl_cipher_verify, 0);
rb_require("openssl");
VALUE cOSSLCipher = rb_path2class("OpenSSL::Cipher");
eCipherError = rb_path2class("OpenSSL::Cipher::CipherError");

rb_define_method(cOSSLCipher, "aad=", ossl_cipher_set_aad, 1);
rb_define_method(cOSSLCipher, "gcm_tag", ossl_cipher_get_tag, 0);
rb_define_method(cOSSLCipher, "gcm_tag=", ossl_cipher_set_tag, 1);
rb_define_method(cOSSLCipher, "gcm_iv_len=", ossl_cipher_set_iv_length, 1);
rb_define_method(cOSSLCipher, "verify", ossl_cipher_verify, 0);
}
1 change: 0 additions & 1 deletion lib/aead/cipher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'aead'

require 'openssl'
require 'openssl/cipher/aead'
require 'securerandom'

Expand Down