Skip to content

Commit 680370b

Browse files
authored
Merge pull request #1046 from rhenium/ky/alloc-in-initialize
Allocate OpenSSL objects in #initialize{,_copy}
2 parents 99d442e + f902610 commit 680370b

27 files changed

Lines changed: 429 additions & 786 deletions

ext/openssl/ossl.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ VALUE mOSSL;
231231
*/
232232
VALUE eOSSLError;
233233

234+
void
235+
ossl_want_uninitialized(VALUE self, const rb_data_type_t *type)
236+
{
237+
if (rb_check_typeddata(self, type)) {
238+
rb_raise(rb_eTypeError, "%"PRIsVALUE" already initialized",
239+
rb_obj_class(self));
240+
}
241+
rb_check_frozen(self);
242+
}
243+
234244
/*
235245
* Convert to DER string
236246
*/

ext/openssl/ossl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ extern VALUE eOSSLError;
9292
}\
9393
} while (0)
9494

95+
void ossl_want_uninitialized(VALUE self, const rb_data_type_t *type);
96+
9597
/*
9698
* Type conversions
9799
*/

ext/openssl/ossl_cipher.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
ossl_raise(rb_eRuntimeError, NULL); \
1818
RTYPEDDATA_DATA(obj) = (ctx); \
1919
} while (0)
20-
#define GetCipherInit(obj, ctx) do { \
21-
TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
22-
} while (0)
2320
#define GetCipher(obj, ctx) do { \
24-
GetCipherInit((obj), (ctx)); \
21+
TypedData_Get_Struct((obj), EVP_CIPHER_CTX, &ossl_cipher_type, (ctx)); \
2522
if (!(ctx)) { \
2623
ossl_raise(rb_eRuntimeError, "Cipher not initialized!"); \
2724
} \
@@ -147,10 +144,7 @@ ossl_cipher_initialize(VALUE self, VALUE str)
147144
const EVP_CIPHER *cipher;
148145
VALUE cipher_holder;
149146

150-
GetCipherInit(self, ctx);
151-
if (ctx) {
152-
ossl_raise(rb_eRuntimeError, "Cipher already initialized!");
153-
}
147+
ossl_want_uninitialized(self, &ossl_cipher_type);
154148
cipher = ossl_evp_cipher_fetch(str, &cipher_holder);
155149
AllocCipher(self, ctx);
156150
if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, -1) != 1)
@@ -166,14 +160,10 @@ ossl_cipher_copy(VALUE self, VALUE other)
166160
{
167161
EVP_CIPHER_CTX *ctx1, *ctx2;
168162

169-
rb_check_frozen(self);
170-
if (self == other) return self;
171-
172-
GetCipherInit(self, ctx1);
173-
if (!ctx1) {
174-
AllocCipher(self, ctx1);
175-
}
163+
ossl_want_uninitialized(self, &ossl_cipher_type);
176164
GetCipher(other, ctx2);
165+
166+
AllocCipher(self, ctx1);
177167
if (EVP_CIPHER_CTX_copy(ctx1, ctx2) != 1)
178168
ossl_raise(eCipherError, NULL);
179169

ext/openssl/ossl_hpke.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ ossl_hpke_ctx_new_sender(VALUE self, VALUE suite)
121121
ossl_hpke_ctx_t *data;
122122
OSSL_HPKE_SUITE *suite_st;
123123

124-
if (RTYPEDDATA_DATA(self))
125-
ossl_raise(eHPKEError, "HPKE context is already initialized");
124+
ossl_want_uninitialized(self, &ossl_hpke_ctx_type);
126125
if (!rb_obj_is_kind_of(suite, cSuite))
127126
ossl_raise(eHPKEError, "invalid suite specified");
128127
GetHpkeSuite(suite, suite_st);
@@ -155,8 +154,7 @@ ossl_hpke_ctx_new_receiver(VALUE self, VALUE suite)
155154
ossl_hpke_ctx_t *data;
156155
OSSL_HPKE_SUITE *suite_st;
157156

158-
if (RTYPEDDATA_DATA(self))
159-
ossl_raise(eHPKEError, "HPKE context is already initialized");
157+
ossl_want_uninitialized(self, &ossl_hpke_ctx_type);
160158
if (!rb_obj_is_kind_of(suite, cSuite))
161159
ossl_raise(eHPKEError, "invalid suite specified");
162160
GetHpkeSuite(suite, suite_st);
@@ -379,8 +377,7 @@ ossl_hpke_suite_initialize(VALUE self, VALUE kem, VALUE kdf, VALUE aead)
379377
{
380378
OSSL_HPKE_SUITE *suite, tmp;
381379

382-
if (RTYPEDDATA_DATA(self))
383-
ossl_raise(eHPKEError, "HPKE suite is already initialized");
380+
ossl_want_uninitialized(self, &ossl_hpke_suite_type);
384381

385382
if (RB_INTEGER_TYPE_P(kem) && RB_INTEGER_TYPE_P(kdf) &&
386383
RB_INTEGER_TYPE_P(aead)) {

ext/openssl/ossl_ns_spki.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
*/
1010
#include "ossl.h"
1111

12-
#define NewSPKI(klass) \
13-
TypedData_Wrap_Struct((klass), &ossl_netscape_spki_type, 0)
14-
#define SetSPKI(obj, spki) do { \
15-
if (!(spki)) { \
16-
ossl_raise(rb_eRuntimeError, "SPKI wasn't initialized!"); \
17-
} \
18-
RTYPEDDATA_DATA(obj) = (spki); \
19-
} while (0)
2012
#define GetSPKI(obj, spki) do { \
2113
TypedData_Get_Struct((obj), NETSCAPE_SPKI, &ossl_netscape_spki_type, (spki)); \
2214
if (!(spki)) { \
@@ -56,16 +48,7 @@ static const rb_data_type_t ossl_netscape_spki_type = {
5648
static VALUE
5749
ossl_spki_alloc(VALUE klass)
5850
{
59-
NETSCAPE_SPKI *spki;
60-
VALUE obj;
61-
62-
obj = NewSPKI(klass);
63-
if (!(spki = NETSCAPE_SPKI_new())) {
64-
ossl_raise(eSPKIError, NULL);
65-
}
66-
SetSPKI(obj, spki);
67-
68-
return obj;
51+
return TypedData_Wrap_Struct(klass, &ossl_netscape_spki_type, NULL);
6952
}
7053

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

85-
if (rb_scan_args(argc, argv, "01", &buffer) == 0) {
68+
rb_scan_args(argc, argv, "01", &buffer);
69+
ossl_want_uninitialized(self, &ossl_netscape_spki_type);
70+
if (argc == 0) {
71+
spki = NETSCAPE_SPKI_new();
72+
if (!spki)
73+
ossl_raise(eSPKIError, "NETSCAPE_SPKI_new");
74+
RTYPEDDATA_DATA(self) = spki;
8675
return self;
8776
}
8877
StringValue(buffer);
@@ -93,8 +82,7 @@ ossl_spki_initialize(int argc, VALUE *argv, VALUE self)
9382
ossl_raise(eSPKIError, NULL);
9483
}
9584
}
96-
NETSCAPE_SPKI_free(DATA_PTR(self));
97-
SetSPKI(self, spki);
85+
RTYPEDDATA_DATA(self) = spki;
9886

9987
return self;
10088
}

0 commit comments

Comments
 (0)