Skip to content

Commit 717f4b9

Browse files
committed
pkey/ec: rearrange PKey::EC::Point#initialize
1 parent 3ed3fc5 commit 717f4b9

File tree

1 file changed

+24
-44
lines changed

1 file changed

+24
-44
lines changed

ext/openssl/ossl_pkey_ec.c

Lines changed: 24 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,7 @@ ec_point_new(const EC_POINT *point, const EC_GROUP *group)
13191319
return obj;
13201320
}
13211321

1322+
static VALUE ossl_ec_point_initialize_copy(VALUE, VALUE);
13221323
/*
13231324
* call-seq:
13241325
* OpenSSL::PKey::EC::Point.new(point)
@@ -1330,46 +1331,34 @@ ec_point_new(const EC_POINT *point, const EC_GROUP *group)
13301331
static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
13311332
{
13321333
EC_POINT *point;
1333-
VALUE arg1, arg2;
1334-
VALUE group_v = Qnil;
1335-
const EC_GROUP *group = NULL;
1334+
VALUE group_v, arg2;
1335+
const EC_GROUP *group;
13361336

13371337
TypedData_Get_Struct(self, EC_POINT, &ossl_ec_point_type, point);
13381338
if (point)
1339-
ossl_raise(eEC_POINT, "EC_POINT already initialized");
1340-
1341-
switch (rb_scan_args(argc, argv, "11", &arg1, &arg2)) {
1342-
case 1:
1343-
if (rb_obj_is_kind_of(arg1, cEC_POINT)) {
1344-
const EC_POINT *arg_point;
1345-
1346-
group_v = rb_attr_get(arg1, id_i_group);
1347-
GetECGroup(group_v, group);
1348-
GetECPoint(arg1, arg_point);
1349-
1350-
point = EC_POINT_dup(arg_point, group);
1351-
} else if (rb_obj_is_kind_of(arg1, cEC_GROUP)) {
1352-
group_v = arg1;
1353-
GetECGroup(group_v, group);
1354-
1355-
point = EC_POINT_new(group);
1356-
} else {
1357-
ossl_raise(eEC_POINT, "wrong argument type: must be OpenSSL::PKey::EC::Point or OpenSSL::Pkey::EC::Group");
1358-
}
1359-
1360-
break;
1361-
case 2:
1362-
if (!rb_obj_is_kind_of(arg1, cEC_GROUP))
1363-
ossl_raise(rb_eArgError, "1st argument must be OpenSSL::PKey::EC::Group");
1364-
group_v = arg1;
1365-
GetECGroup(group_v, group);
1339+
rb_raise(eEC_POINT, "EC_POINT already initialized");
13661340

1367-
if (rb_obj_is_kind_of(arg2, cBN)) {
1368-
const BIGNUM *bn = GetBNPtr(arg2);
1341+
rb_scan_args(argc, argv, "11", &group_v, &arg2);
1342+
if (rb_obj_is_kind_of(group_v, cEC_POINT)) {
1343+
if (argc != 1)
1344+
rb_raise(rb_eArgError, "invalid second argument");
1345+
return ossl_ec_point_initialize_copy(self, group_v);
1346+
}
13691347

1370-
point = EC_POINT_bn2point(group, bn, NULL, ossl_bn_ctx);
1371-
} else {
1372-
BIO *in = ossl_obj2bio(&arg1);
1348+
GetECGroup(group_v, group);
1349+
if (argc == 1) {
1350+
point = EC_POINT_new(group);
1351+
if (!point)
1352+
ossl_raise(eEC_POINT, "EC_POINT_new");
1353+
}
1354+
else {
1355+
if (rb_obj_is_kind_of(arg2, cBN)) {
1356+
point = EC_POINT_bn2point(group, GetBNPtr(arg2), NULL, ossl_bn_ctx);
1357+
if (!point)
1358+
ossl_raise(eEC_POINT, "EC_POINT_bn2point");
1359+
}
1360+
else {
1361+
BIO *in = ossl_obj2bio(&arg2);
13731362

13741363
/* BUG: finish me */
13751364

@@ -1379,17 +1368,8 @@ static VALUE ossl_ec_point_initialize(int argc, VALUE *argv, VALUE self)
13791368
ossl_raise(eEC_POINT, "unknown type for 2nd arg");
13801369
}
13811370
}
1382-
break;
1383-
default:
1384-
ossl_raise(rb_eArgError, "wrong number of arguments");
13851371
}
13861372

1387-
if (point == NULL)
1388-
ossl_raise(eEC_POINT, NULL);
1389-
1390-
if (NIL_P(group_v))
1391-
ossl_raise(rb_eRuntimeError, "missing group (internal error)");
1392-
13931373
RTYPEDDATA_DATA(self) = point;
13941374
rb_ivar_set(self, id_i_group, group_v);
13951375

0 commit comments

Comments
 (0)