diff --git a/src/main/java/org/jruby/ext/openssl/PKeyEC.java b/src/main/java/org/jruby/ext/openssl/PKeyEC.java index 638ee910..45466e03 100644 --- a/src/main/java/org/jruby/ext/openssl/PKeyEC.java +++ b/src/main/java/org/jruby/ext/openssl/PKeyEC.java @@ -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; } @@ -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()); @@ -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 { diff --git a/src/test/ruby/ec/test_ec.rb b/src/test/ruby/ec/test_ec.rb index 2ac4a041..bb1b81cc 100644 --- a/src/test/ruby/ec/test_ec.rb +++ b/src/test/ruby/ec/test_ec.rb @@ -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