Skip to content

Commit 2c16821

Browse files
committed
pkey/ec: fix OpenSSL::PKey::EC::Group#curve_name for unknown curves
EC_GROUP_get_curve_name() returns NID_undef when OpenSSL does not recognize the curve and there is no associated OID. Handle this case explicitly and return nil instead of the string "UNDEF", which should not be exposed outside the extension.
1 parent 0aad4f8 commit 2c16821

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

ext/openssl/ossl_pkey_ec.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -849,25 +849,23 @@ static VALUE ossl_ec_group_get_cofactor(VALUE self)
849849

850850
/*
851851
* call-seq:
852-
* group.curve_name => String
852+
* group.curve_name -> string or nil
853853
*
854-
* Returns the curve name (sn).
854+
* Returns the curve name (short name) corresponding to this group, or +nil+
855+
* if \OpenSSL does not have an OID associated with the group.
855856
*
856857
* See the OpenSSL documentation for EC_GROUP_get_curve_name()
857858
*/
858859
static VALUE ossl_ec_group_get_curve_name(VALUE self)
859860
{
860-
EC_GROUP *group = NULL;
861+
EC_GROUP *group;
861862
int nid;
862863

863864
GetECGroup(self, group);
864-
if (group == NULL)
865-
return Qnil;
866-
867865
nid = EC_GROUP_get_curve_name(group);
868-
869-
/* BUG: an nid or asn1 object should be returned, maybe. */
870-
return rb_str_new2(OBJ_nid2sn(nid));
866+
if (nid == NID_undef)
867+
return Qnil;
868+
return rb_str_new_cstr(OBJ_nid2sn(nid));
871869
}
872870

873871
/*

test/openssl/test_pkey_ec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,18 +369,26 @@ def test_ec_point
369369
point2.to_octet_string(:uncompressed)
370370
assert_equal point2.to_octet_string(:uncompressed),
371371
point3.to_octet_string(:uncompressed)
372+
end
372373

374+
def test_small_curve
373375
begin
374376
group = OpenSSL::PKey::EC::Group.new(:GFp, 17, 2, 2)
375377
group.point_conversion_form = :uncompressed
376378
generator = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 05 01 }))
377379
group.set_generator(generator, 19, 1)
378-
point = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 06 03 }))
379380
rescue OpenSSL::PKey::EC::Group::Error
380381
pend "Patched OpenSSL rejected curve" if /unsupported field/ =~ $!.message
381382
raise
382383
end
383-
384+
assert_equal 17.to_bn.num_bits, group.degree
385+
assert_equal B(%w{ 04 05 01 }),
386+
group.generator.to_octet_string(:uncompressed)
387+
assert_equal 19.to_bn, group.order
388+
assert_equal 1.to_bn, group.cofactor
389+
assert_nil group.curve_name
390+
391+
point = OpenSSL::PKey::EC::Point.new(group, B(%w{ 04 06 03 }))
384392
assert_equal 0x040603.to_bn, point.to_bn
385393
assert_equal 0x040603.to_bn, point.to_bn(:uncompressed)
386394
assert_equal 0x0306.to_bn, point.to_bn(:compressed)

0 commit comments

Comments
 (0)