Skip to content

Commit a27a0d0

Browse files
authored
Merge pull request #907 from rhenium/ky/pkey-ec-avoid-sym2id
pkey/ec: avoid calling SYM2ID() on user-supplied objects
2 parents 64b7e11 + 0d66296 commit a27a0d0

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

ext/openssl/ossl_pkey_ec.c

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ static VALUE eEC_GROUP;
4747
static VALUE cEC_POINT;
4848
static VALUE eEC_POINT;
4949

50-
static ID s_GFp, s_GF2m;
51-
52-
static ID ID_uncompressed;
53-
static ID ID_compressed;
54-
static ID ID_hybrid;
50+
static VALUE sym_GFp, sym_GF2m;
51+
static VALUE sym_uncompressed, sym_compressed, sym_hybrid;
5552

5653
static ID id_i_group;
5754

@@ -674,19 +671,20 @@ static VALUE ossl_ec_group_initialize(int argc, VALUE *argv, VALUE self)
674671
break;
675672
case 4:
676673
if (SYMBOL_P(arg1)) {
677-
ID id = SYM2ID(arg1);
678674
EC_GROUP *(*new_curve)(const BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *) = NULL;
679675
const BIGNUM *p = GetBNPtr(arg2);
680676
const BIGNUM *a = GetBNPtr(arg3);
681677
const BIGNUM *b = GetBNPtr(arg4);
682678

683-
if (id == s_GFp) {
679+
if (arg1 == sym_GFp) {
684680
new_curve = EC_GROUP_new_curve_GFp;
681+
}
685682
#if !defined(OPENSSL_NO_EC2M)
686-
} else if (id == s_GF2m) {
683+
else if (arg1 == sym_GF2m) {
687684
new_curve = EC_GROUP_new_curve_GF2m;
685+
}
688686
#endif
689-
} else {
687+
else {
690688
ossl_raise(rb_eArgError, "unknown symbol, must be :GFp or :GF2m");
691689
}
692690

@@ -958,37 +956,36 @@ static VALUE ossl_ec_group_set_asn1_flag(VALUE self, VALUE flag_v)
958956
*/
959957
static VALUE ossl_ec_group_get_point_conversion_form(VALUE self)
960958
{
961-
EC_GROUP *group = NULL;
959+
EC_GROUP *group;
962960
point_conversion_form_t form;
963-
VALUE ret;
964961

965962
GetECGroup(self, group);
966963
form = EC_GROUP_get_point_conversion_form(group);
967964

968965
switch (form) {
969-
case POINT_CONVERSION_UNCOMPRESSED: ret = ID_uncompressed; break;
970-
case POINT_CONVERSION_COMPRESSED: ret = ID_compressed; break;
971-
case POINT_CONVERSION_HYBRID: ret = ID_hybrid; break;
972-
default: ossl_raise(eEC_GROUP, "unsupported point conversion form: %d, this module should be updated", form);
966+
case POINT_CONVERSION_UNCOMPRESSED:
967+
return sym_uncompressed;
968+
case POINT_CONVERSION_COMPRESSED:
969+
return sym_compressed;
970+
case POINT_CONVERSION_HYBRID:
971+
return sym_hybrid;
972+
default:
973+
ossl_raise(eEC_GROUP, "unsupported point conversion form: %d, " \
974+
"this module should be updated", form);
973975
}
974-
975-
return ID2SYM(ret);
976976
}
977977

978978
static point_conversion_form_t
979979
parse_point_conversion_form_symbol(VALUE sym)
980980
{
981-
ID id = SYM2ID(sym);
982-
983-
if (id == ID_uncompressed)
981+
if (sym == sym_uncompressed)
984982
return POINT_CONVERSION_UNCOMPRESSED;
985-
else if (id == ID_compressed)
983+
if (sym == sym_compressed)
986984
return POINT_CONVERSION_COMPRESSED;
987-
else if (id == ID_hybrid)
985+
if (sym == sym_hybrid)
988986
return POINT_CONVERSION_HYBRID;
989-
else
990-
ossl_raise(rb_eArgError, "unsupported point conversion form %+"PRIsVALUE
991-
" (expected :compressed, :uncompressed, or :hybrid)", sym);
987+
ossl_raise(rb_eArgError, "unsupported point conversion form %+"PRIsVALUE
988+
" (expected :compressed, :uncompressed, or :hybrid)", sym);
992989
}
993990

994991
/*
@@ -1557,12 +1554,12 @@ void Init_ossl_ec(void)
15571554
eEC_GROUP = rb_define_class_under(cEC_GROUP, "Error", eOSSLError);
15581555
eEC_POINT = rb_define_class_under(cEC_POINT, "Error", eOSSLError);
15591556

1560-
s_GFp = rb_intern("GFp");
1561-
s_GF2m = rb_intern("GF2m");
1557+
sym_GFp = ID2SYM(rb_intern_const("GFp"));
1558+
sym_GF2m = ID2SYM(rb_intern_const("GF2m"));
15621559

1563-
ID_uncompressed = rb_intern("uncompressed");
1564-
ID_compressed = rb_intern("compressed");
1565-
ID_hybrid = rb_intern("hybrid");
1560+
sym_uncompressed = ID2SYM(rb_intern_const("uncompressed"));
1561+
sym_compressed = ID2SYM(rb_intern_const("compressed"));
1562+
sym_hybrid = ID2SYM(rb_intern_const("hybrid"));
15661563

15671564
rb_define_const(cEC, "NAMED_CURVE", INT2NUM(OPENSSL_EC_NAMED_CURVE));
15681565
rb_define_const(cEC, "EXPLICIT_CURVE", INT2NUM(OPENSSL_EC_EXPLICIT_CURVE));

0 commit comments

Comments
 (0)