Skip to content

feat: create an EC::PKey from an EC::Point #916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion ext/openssl/ossl_pkey_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,40 @@ ec_key_new_from_group(VALUE arg)
return ec;
}

/*
* Creates a new EC_KEY from the provided EC::Point
*/
static EC_KEY *
ec_key_new_from_point(VALUE arg)
{
EC_KEY *ec = NULL;

if (rb_obj_is_kind_of(arg, cEC_POINT)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if seems redundant.

EC_POINT *point;
EC_GROUP *group;

if (!(ec = EC_KEY_new())) {
ossl_raise(eECError, NULL);
}

GetECPointGroup(arg, group);
if (!EC_KEY_set_group(ec, group)) {
EC_KEY_free(ec);
ossl_raise(eECError, NULL);
}

GetECPoint(arg, point);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetECPoint() and GetECPointGroup() should be called before ec is allocated. They can potentially raise an exception, and in that case ec could leak.

if (EC_KEY_set_public_key(ec, point) == 0) {
EC_KEY_free(ec);
ossl_raise(eECError, NULL);
}
} else {
ossl_raise(eECError, "invalid point");
}

return ec;
}

/*
* call-seq:
* EC.generate(ec_group) -> ec
Expand Down Expand Up @@ -125,7 +159,7 @@ ossl_ec_key_s_generate(VALUE klass, VALUE arg)
/*
* call-seq:
* OpenSSL::PKey::EC.new
* OpenSSL::PKey::EC.new(ec_key)
* OpenSSL::PKey::EC.new(ec_key) # ec_key is PKey::EC, or PKey::EC::Point.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* OpenSSL::PKey::EC.new(ec_key) # ec_key is PKey::EC, or PKey::EC::Point.
* OpenSSL::PKey::EC.new(ec_key)
* OpenSSL::PKey::EC.new(ec_point)

* OpenSSL::PKey::EC.new(ec_group)
* OpenSSL::PKey::EC.new("secp112r1")
* OpenSSL::PKey::EC.new(pem_string [, pwd])
Expand All @@ -151,6 +185,10 @@ static VALUE ossl_ec_key_initialize(int argc, VALUE *argv, VALUE self)
ossl_raise(eECError, "EC_KEY_new");
goto legacy;
}
else if (rb_obj_is_kind_of(arg, cEC_POINT)) {
ec = ec_key_new_from_point(arg);
goto legacy;
}
else if (rb_obj_is_kind_of(arg, cEC_GROUP)) {
ec = ec_key_new_from_group(arg);
goto legacy;
Expand Down
Loading