Skip to content

Commit 24f72a8

Browse files
committed
pkey, ssl, cipher, hpke: use ossl_want_uninitialized()
Simplify the code and raise exceptions with a consistent error message. This unifies the exception type raised when #initialize is called twice to TypeError. This should not affect valid usage of these classes.
1 parent 02872d3 commit 24f72a8

11 files changed

Lines changed: 40 additions & 83 deletions

File tree

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_pkey.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,10 +622,8 @@ ossl_pkey_initialize_copy(VALUE self, VALUE other)
622622
{
623623
EVP_PKEY *pkey, *pkey_other;
624624

625-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
625+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
626626
TypedData_Get_Struct(other, EVP_PKEY, &ossl_evp_pkey_type, pkey_other);
627-
if (pkey)
628-
rb_raise(rb_eTypeError, "pkey already initialized");
629627
if (pkey_other) {
630628
pkey = EVP_PKEY_dup(pkey_other);
631629
if (!pkey)

ext/openssl/ossl_pkey_dh.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ ossl_dh_initialize(int argc, VALUE *argv, VALUE self)
8181
BIO *in = NULL;
8282
VALUE arg;
8383

84-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
85-
if (pkey)
86-
rb_raise(rb_eTypeError, "pkey already initialized");
84+
rb_scan_args(argc, argv, "01", &arg);
85+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
8786

8887
/* The DH.new(size, generator) form is handled by lib/openssl/pkey.rb */
89-
if (rb_scan_args(argc, argv, "01", &arg) == 0) {
88+
if (argc == 0) {
9089
#ifdef OSSL_HAVE_IMMUTABLE_PKEY
9190
rb_raise(rb_eArgError, "OpenSSL::PKey::DH.new cannot be called " \
9291
"without arguments; pkeys are immutable with OpenSSL 3.0");
@@ -144,9 +143,7 @@ ossl_dh_initialize_copy(VALUE self, VALUE other)
144143
DH *dh, *dh_other;
145144
const BIGNUM *pub, *priv;
146145

147-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
148-
if (pkey)
149-
rb_raise(rb_eTypeError, "pkey already initialized");
146+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
150147
GetDH(other, dh_other);
151148

152149
dh = DHparams_dup(dh_other);

ext/openssl/ossl_pkey_dsa.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,10 @@ ossl_dsa_initialize(int argc, VALUE *argv, VALUE self)
9191
VALUE arg, pass;
9292
int type;
9393

94-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
95-
if (pkey)
96-
rb_raise(rb_eTypeError, "pkey already initialized");
94+
rb_scan_args(argc, argv, "02", &arg, &pass);
95+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
9796

9897
/* The DSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
99-
rb_scan_args(argc, argv, "02", &arg, &pass);
10098
if (argc == 0) {
10199
#ifdef OSSL_HAVE_IMMUTABLE_PKEY
102100
rb_raise(rb_eArgError, "OpenSSL::PKey::DSA.new cannot be called " \
@@ -155,9 +153,7 @@ ossl_dsa_initialize_copy(VALUE self, VALUE other)
155153
const DSA *dsa;
156154
DSA *dsa_new;
157155

158-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
159-
if (pkey)
160-
rb_raise(rb_eTypeError, "pkey already initialized");
156+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
161157
GetDSA(other, dsa);
162158

163159
dsa_new = (DSA *)ASN1_dup((i2d_of_void *)i2d_DSAPrivateKey,

ext/openssl/ossl_pkey_ec.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,9 @@ static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
142142
VALUE arg, pass;
143143
int type;
144144

145-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
146-
if (pkey)
147-
rb_raise(rb_eTypeError, "pkey already initialized");
148-
149145
rb_scan_args(argc, argv, "02", &arg, &pass);
146+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
147+
150148
if (NIL_P(arg)) {
151149
#ifdef OSSL_HAVE_IMMUTABLE_PKEY
152150
rb_raise(rb_eArgError, "OpenSSL::PKey::EC.new cannot be called " \
@@ -202,9 +200,7 @@ ossl_ec_key_initialize_copy(VALUE self, VALUE other)
202200
const EC_KEY *ec;
203201
EC_KEY *ec_new;
204202

205-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
206-
if (pkey)
207-
rb_raise(rb_eTypeError, "pkey already initialized");
203+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
208204
GetEC(other, ec);
209205

210206
ec_new = EC_KEY_dup(ec);
@@ -632,11 +628,10 @@ static VALUE ossl_ec_group_initialize(int argc, VALUE *argv, VALUE self)
632628
VALUE arg1, arg2, arg3, arg4;
633629
EC_GROUP *group;
634630

635-
TypedData_Get_Struct(self, EC_GROUP, &ossl_ec_group_type, group);
636-
if (group)
637-
ossl_raise(rb_eRuntimeError, "EC_GROUP is already initialized");
631+
rb_scan_args(argc, argv, "13", &arg1, &arg2, &arg3, &arg4);
632+
ossl_want_uninitialized(self, &ossl_ec_group_type);
638633

639-
switch (rb_scan_args(argc, argv, "13", &arg1, &arg2, &arg3, &arg4)) {
634+
switch (argc) {
640635
case 1:
641636
if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
642637
const EC_GROUP *arg1_group;
@@ -718,9 +713,7 @@ ossl_ec_group_initialize_copy(VALUE self, VALUE other)
718713
{
719714
EC_GROUP *group, *group_new;
720715

721-
TypedData_Get_Struct(self, EC_GROUP, &ossl_ec_group_type, group_new);
722-
if (group_new)
723-
ossl_raise(eEC_GROUP, "EC::Group already initialized");
716+
ossl_want_uninitialized(self, &ossl_ec_group_type);
724717
GetECGroup(other, group);
725718

726719
group_new = EC_GROUP_dup(group);
@@ -1220,11 +1213,9 @@ static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
12201213
VALUE group_v, arg2;
12211214
const EC_GROUP *group;
12221215

1223-
TypedData_Get_Struct(self, EC_POINT, &ossl_ec_point_type, point);
1224-
if (point)
1225-
rb_raise(eEC_POINT, "EC_POINT already initialized");
1226-
12271216
rb_scan_args(argc, argv, "11", &group_v, &arg2);
1217+
ossl_want_uninitialized(self, &ossl_ec_point_type);
1218+
12281219
if (rb_obj_is_kind_of(group_v, cEC_POINT)) {
12291220
if (argc != 1)
12301221
rb_raise(rb_eArgError, "invalid second argument");
@@ -1271,9 +1262,7 @@ ossl_ec_point_initialize_copy(VALUE self, VALUE other)
12711262
EC_GROUP *group;
12721263
VALUE group_v;
12731264

1274-
TypedData_Get_Struct(self, EC_POINT, &ossl_ec_point_type, point_new);
1275-
if (point_new)
1276-
ossl_raise(eEC_POINT, "EC::Point already initialized");
1265+
ossl_want_uninitialized(self, &ossl_ec_point_type);
12771266
GetECPoint(other, point);
12781267

12791268
group_v = rb_obj_dup(rb_attr_get(other, id_i_group));

ext/openssl/ossl_pkey_rsa.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ ossl_rsa_initialize(int argc, VALUE *argv, VALUE self)
8484
VALUE arg, pass;
8585
int type;
8686

87-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
88-
if (pkey)
89-
rb_raise(rb_eTypeError, "pkey already initialized");
87+
rb_scan_args(argc, argv, "02", &arg, &pass);
88+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
9089

9190
/* The RSA.new(size, generator) form is handled by lib/openssl/pkey.rb */
92-
rb_scan_args(argc, argv, "02", &arg, &pass);
9391
if (argc == 0) {
9492
#ifdef OSSL_HAVE_IMMUTABLE_PKEY
9593
rb_raise(rb_eArgError, "OpenSSL::PKey::RSA.new cannot be called " \
@@ -151,9 +149,7 @@ ossl_rsa_initialize_copy(VALUE self, VALUE other)
151149
const RSA *rsa;
152150
RSA *rsa_new;
153151

154-
TypedData_Get_Struct(self, EVP_PKEY, &ossl_evp_pkey_type, pkey);
155-
if (pkey)
156-
rb_raise(rb_eTypeError, "pkey already initialized");
152+
ossl_want_uninitialized(self, &ossl_evp_pkey_type);
157153
GetRSA(other, rsa);
158154

159155
rsa_new = (RSA *)ASN1_dup((i2d_of_void *)i2d_RSAPrivateKey,

ext/openssl/ossl_ssl.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,18 +1669,15 @@ ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
16691669
SSL *ssl;
16701670
SSL_CTX *ctx;
16711671

1672-
TypedData_Get_Struct(self, SSL, &ossl_ssl_type, ssl);
1673-
if (ssl)
1674-
ossl_raise(eSSLError, "SSL already initialized");
1675-
1676-
if (rb_scan_args(argc, argv, "11:", &io, &v_ctx, &opts) == 1)
1677-
v_ctx = rb_funcall(cSSLContext, rb_intern("new"), 0);
1678-
1672+
argc = rb_scan_args(argc, argv, "11:", &io, &v_ctx, &opts);
16791673
if (!kw_ids[0]) {
16801674
kw_ids[0] = rb_intern_const("sync_close");
16811675
}
1682-
16831676
rb_get_kwargs(opts, kw_ids, 0, 1, kw_args);
1677+
ossl_want_uninitialized(self, &ossl_ssl_type);
1678+
1679+
if (argc == 1)
1680+
v_ctx = rb_funcall(cSSLContext, rb_intern("new"), 0);
16841681
if (kw_args[0] != Qundef) {
16851682
rb_ivar_set(self, id_i_sync_close, kw_args[0]);
16861683
}

ext/openssl/ossl_ssl_session.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ ossl_ssl_session_initialize(VALUE self, VALUE arg1)
4040
{
4141
SSL_SESSION *ctx;
4242

43-
if (RTYPEDDATA_DATA(self))
44-
ossl_raise(eSSLSession, "SSL Session already initialized");
43+
ossl_want_uninitialized(self, &ossl_ssl_session_type);
4544

4645
if (rb_obj_is_instance_of(arg1, cSSLSocket)) {
4746
SSL *ssl;
@@ -73,10 +72,9 @@ ossl_ssl_session_initialize(VALUE self, VALUE arg1)
7372
static VALUE
7473
ossl_ssl_session_initialize_copy(VALUE self, VALUE other)
7574
{
76-
SSL_SESSION *sess, *sess_other, *sess_new;
75+
SSL_SESSION *sess_other, *sess_new;
7776

78-
rb_check_frozen(self);
79-
sess = RTYPEDDATA_DATA(self); /* XXX */
77+
ossl_want_uninitialized(self, &ossl_ssl_session_type);
8078
GetSSLSession(other, sess_other);
8179

8280
sess_new = ASN1_dup((i2d_of_void *)i2d_SSL_SESSION, (d2i_of_void *)d2i_SSL_SESSION,
@@ -85,7 +83,6 @@ ossl_ssl_session_initialize_copy(VALUE self, VALUE other)
8583
ossl_raise(eSSLSession, "ASN1_dup");
8684

8785
RTYPEDDATA_DATA(self) = sess_new;
88-
SSL_SESSION_free(sess);
8986

9087
return self;
9188
}

test/openssl/test_cipher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_random_key_iv
110110

111111
def test_initialize
112112
cipher = OpenSSL::Cipher.new("AES-256-CBC")
113-
assert_raise(RuntimeError) { cipher.__send__(:initialize, "AES-256-CBC") }
113+
assert_raise(TypeError) { cipher.__send__(:initialize, "AES-256-CBC") }
114114
assert_raise(RuntimeError) { OpenSSL::Cipher.allocate.final }
115115
assert_raise(OpenSSL::Cipher::CipherError) {
116116
OpenSSL::Cipher.new("no such algorithm")

0 commit comments

Comments
 (0)