Skip to content
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
10 changes: 10 additions & 0 deletions ext/openssl/ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ VALUE mOSSL;
*/
VALUE eOSSLError;

void
ossl_want_uninitialized(VALUE self, const rb_data_type_t *type)
{
if (rb_check_typeddata(self, type)) {
rb_raise(rb_eTypeError, "%"PRIsVALUE" already initialized",
rb_obj_class(self));
}
rb_check_frozen(self);
}

/*
* Convert to DER string
*/
Expand Down
2 changes: 2 additions & 0 deletions ext/openssl/ossl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ extern VALUE eOSSLError;
}\
} while (0)

void ossl_want_uninitialized(VALUE self, const rb_data_type_t *type);

/*
* Type conversions
*/
Expand Down
20 changes: 5 additions & 15 deletions ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
ossl_raise(rb_eRuntimeError, NULL); \
RTYPEDDATA_DATA(obj) = (ctx); \
} while (0)
#define GetCipherInit(obj, ctx) do { \
TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
} while (0)
#define GetCipher(obj, ctx) do { \
GetCipherInit((obj), (ctx)); \
TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
if (!(ctx)) { \
ossl_raise(rb_eRuntimeError, "Cipher not initialized!"); \
} \
Expand Down Expand Up @@ -147,10 +144,7 @@ ossl_cipher_initialize(VALUE self, VALUE str)
const EVP_CIPHER *cipher;
VALUE cipher_holder;

GetCipherInit(self, ctx);
if (ctx) {
ossl_raise(rb_eRuntimeError, "Cipher already initialized!");
}
ossl_want_uninitialized(self, &ossl_cipher_type);
cipher = ossl_evp_cipher_fetch(str, &cipher_holder);
AllocCipher(self, ctx);
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
Expand All @@ -166,14 +160,10 @@ ossl_cipher_copy(VALUE self, VALUE other)
{
EVP_CIPHER_CTX *ctx1, *ctx2;

rb_check_frozen(self);
if (self == other) return self;

GetCipherInit(self, ctx1);
if (!ctx1) {
AllocCipher(self, ctx1);
}
ossl_want_uninitialized(self, &ossl_cipher_type);
GetCipher(other, ctx2);

AllocCipher(self, ctx1);
if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
ossl_raise(eCipherError, NULL);

Expand Down
9 changes: 3 additions & 6 deletions ext/openssl/ossl_hpke.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ ossl_hpke_ctx_new_sender(VALUE self, VALUE suite)
ossl_hpke_ctx_t *data;
OSSL_HPKE_SUITE *suite_st;

if (RTYPEDDATA_DATA(self))
ossl_raise(eHPKEError, "HPKE context is already initialized");
ossl_want_uninitialized(self, &ossl_hpke_ctx_type);
if (!rb_obj_is_kind_of(suite, cSuite))
ossl_raise(eHPKEError, "invalid suite specified");
GetHpkeSuite(suite, suite_st);
Expand Down Expand Up @@ -155,8 +154,7 @@ ossl_hpke_ctx_new_receiver(VALUE self, VALUE suite)
ossl_hpke_ctx_t *data;
OSSL_HPKE_SUITE *suite_st;

if (RTYPEDDATA_DATA(self))
ossl_raise(eHPKEError, "HPKE context is already initialized");
ossl_want_uninitialized(self, &ossl_hpke_ctx_type);
if (!rb_obj_is_kind_of(suite, cSuite))
ossl_raise(eHPKEError, "invalid suite specified");
GetHpkeSuite(suite, suite_st);
Expand Down Expand Up @@ -379,8 +377,7 @@ ossl_hpke_suite_initialize(VALUE self, VALUE kem, VALUE kdf, VALUE aead)
{
OSSL_HPKE_SUITE *suite, tmp;

if (RTYPEDDATA_DATA(self))
ossl_raise(eHPKEError, "HPKE suite is already initialized");
ossl_want_uninitialized(self, &ossl_hpke_suite_type);

if (RB_INTEGER_TYPE_P(kem) && RB_INTEGER_TYPE_P(kdf) &&
RB_INTEGER_TYPE_P(aead)) {
Expand Down
30 changes: 9 additions & 21 deletions ext/openssl/ossl_ns_spki.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
*/
#include "ossl.h"

#define NewSPKI(klass) \
TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, 0)
#define SetSPKI(obj, spki) do { \
if (!(spki)) { \
ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \
} \
RTYPEDDATA_DATA(obj) = (spki); \
} while (0)
#define GetSPKI(obj, spki) do { \
TypedData_Get_Struct((obj), NETSCAPE_SPKI, &ossl_netscape_spki_type, (spki)); \
if (!(spki)) { \
Expand Down Expand Up @@ -56,16 +48,7 @@ static const rb_data_type_t ossl_netscape_spki_type = {
static VALUE
ossl_spki_alloc(VALUE klass)
{
NETSCAPE_SPKI *spki;
VALUE obj;

obj = NewSPKI(klass);
if (!(spki = NETSCAPE_SPKI_new())) {
ossl_raise(eSPKIError, NULL);
}
SetSPKI(obj, spki);

return obj;
return TypedData_Wrap_Struct(klass, &ossl_netscape_spki_type, NULL);
}

/*
Expand All @@ -82,7 +65,13 @@ ossl_spki_initialize(int argc, VALUE *argv, VALUE self)
VALUE buffer;
const unsigned char *p;

if (rb_scan_args(argc, argv, "01", &buffer) == 0) {
rb_scan_args(argc, argv, "01", &buffer);
ossl_want_uninitialized(self, &ossl_netscape_spki_type);
if (argc == 0) {
spki = NETSCAPE_SPKI_new();
if (!spki)
ossl_raise(eSPKIError, "NETSCAPE_SPKI_new");
RTYPEDDATA_DATA(self) = spki;
return self;
}
StringValue(buffer);
Expand All @@ -93,8 +82,7 @@ ossl_spki_initialize(int argc, VALUE *argv, VALUE self)
ossl_raise(eSPKIError, NULL);
}
}
NETSCAPE_SPKI_free(DATA_PTR(self));
SetSPKI(self, spki);
RTYPEDDATA_DATA(self) = spki;

return self;
}
Expand Down
Loading