Skip to content

Commit

Permalink
[feat] implement support for PKey::EC.generate
Browse files Browse the repository at this point in the history
  • Loading branch information
kares committed Feb 12, 2024
1 parent f9957e2 commit bdd1ced
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/jruby/ext/openssl/PKeyEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
IRubyObject arg = args[0];

if ( arg instanceof Group ) {
this.group = (Group) arg;
this.curveName = this.group.getCurveName();
setGroup((Group) arg);
return this;
}

Expand Down Expand Up @@ -380,6 +379,11 @@ private void unwrapPrivateKeyWithName() {
}
}

private void setGroup(final Group group) {
this.group = group;
this.curveName = this.group.getCurveName();
}

//private static ECNamedCurveParameterSpec readECParameters(final byte[] input) throws IOException {
// ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(input);
// return ECNamedCurveTable.getParameterSpec(oid.getId());
Expand Down Expand Up @@ -407,6 +411,19 @@ public PKeyEC generate_key(final ThreadContext context) {
return this;
}

@JRubyMethod(meta = true)
public static IRubyObject generate(final ThreadContext context, final IRubyObject self, final IRubyObject group) {
PKeyEC randomKey = new PKeyEC(context.runtime, (RubyClass) self);

if (group instanceof Group) {
randomKey.setGroup((Group) group);
} else {
randomKey.curveName = group.convertToString().toString();
}

return randomKey.generate_key(context);
}

@JRubyMethod(name = "dsa_sign_asn1")
public IRubyObject dsa_sign_asn1(final ThreadContext context, final IRubyObject data) {
try {
Expand Down
9 changes: 2 additions & 7 deletions src/test/ruby/ec/test_ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,13 @@ def setup
super
self.class.disable_security_restrictions!

# @data1 = 'foo'; @data2 = 'bar' * 1000 # data too long for DSA sig

@groups = []; @keys = []

OpenSSL::PKey::EC.builtin_curves.each do |curve, comment|
next if curve.start_with?("Oakley") # Oakley curves are not suitable for ECDSA
group = OpenSSL::PKey::EC::Group.new(curve)

key = OpenSSL::PKey::EC.new(group)
key.generate_key

@groups << group; @keys << key
@groups << group = OpenSSL::PKey::EC::Group.new(curve)
@keys << OpenSSL::PKey::EC.generate(group)
end
end

Expand Down

0 comments on commit bdd1ced

Please sign in to comment.