@@ -97,12 +97,6 @@ public static RaiseException newDHError(Ruby runtime, String message) {
9797 private transient volatile BigInteger dh_y ;
9898 private transient volatile BigInteger dh_x ;
9999
100- // FIXME! need to figure out what it means in MRI/OSSL code to
101- // claim a DH is(/has) private if an engine is present -- doesn't really
102- // map to Java implementation.
103-
104- //private volatile boolean haveEngine;
105-
106100 public PKeyDH (Ruby runtime , RubyClass clazz ) {
107101 super (runtime , clazz );
108102 }
@@ -120,6 +114,21 @@ public IRubyObject initialize_copy(final IRubyObject original) {
120114 return this ;
121115 }
122116
117+ @ JRubyMethod (name = "generate" , meta = true , rest = true )
118+ public static IRubyObject generate (final ThreadContext context , IRubyObject self , IRubyObject [] args ) {
119+ final Ruby runtime = context .runtime ;
120+ final int g ;
121+ if (Arity .checkArgumentCount (runtime , args , 1 , 2 ) == 2 ) {
122+ g = RubyNumeric .num2int (args [1 ]);
123+ } else {
124+ g = 2 ;
125+ }
126+
127+ PKeyDH pkey = new PKeyDH (runtime , _PKey (runtime ).getClass ("DH" ));
128+ pkey .generate (runtime , args [0 ], g );
129+ return pkey ;
130+ }
131+
123132 @ JRubyMethod (name ="initialize" , rest =true , visibility = Visibility .PRIVATE )
124133 public synchronized IRubyObject initialize (final ThreadContext context , final IRubyObject [] args ) {
125134 final Ruby runtime = context .runtime ;
@@ -150,28 +159,29 @@ public synchronized IRubyObject initialize(final ThreadContext context, final IR
150159 throw runtime .newIOErrorFromException (e );
151160 }
152161 } else {
153- int bits = RubyNumeric .fix2int (arg0 );
154- // g defaults to 2
155- int gval = argc == 2 ? RubyNumeric .fix2int (args [1 ]) : 2 ;
156- BigInteger p ;
157- try {
158- p = generateP (bits , gval );
159- }
160- catch (IllegalArgumentException e ) {
161- throw runtime .newArgumentError (e .getMessage ());
162- }
163- BigInteger g = BigInteger .valueOf (gval );
164- BigInteger x = generateX (p );
165- BigInteger y = generateY (p , g , x );
166- this .dh_p = p ;
167- this .dh_g = g ;
168- this .dh_x = x ; // private key
169- this .dh_y = y ; // public key
162+ generate (runtime , arg0 , argc == 2 ? RubyNumeric .num2int (args [1 ]) : 2 ); // g defaults to 2
170163 }
171164 }
172165 return this ;
173166 }
174167
168+ private void generate (final Ruby runtime , final IRubyObject bits , final int gval ) {
169+ BigInteger p ;
170+ try {
171+ p = generateP (RubyNumeric .num2int (bits ), gval );
172+ }
173+ catch (IllegalArgumentException e ) {
174+ throw runtime .newArgumentError (e .getMessage ());
175+ }
176+ BigInteger g = BigInteger .valueOf (gval );
177+ BigInteger x = generateX (p );
178+ BigInteger y = generateY (p , g , x );
179+ this .dh_p = p ;
180+ this .dh_g = g ;
181+ this .dh_x = x ; // private key
182+ this .dh_y = y ; // public key
183+ }
184+
175185 public static BigInteger generateP (int bits , int g ) {
176186
177187 // FIXME? I'm following algorithms used in OpenSSL, could use JCE provider instead.
@@ -225,10 +235,6 @@ public static BigInteger generateY(BigInteger p, BigInteger g, BigInteger x) {
225235 return g .modPow (x , p );
226236 }
227237
228- public static BigInteger generateY (BigInteger p , int g , BigInteger x ) {
229- return generateY (p , BigInteger .valueOf (g ), x );
230- }
231-
232238 @ JRubyMethod (name = "generate_key!" )
233239 public synchronized IRubyObject generate_key () {
234240 BigInteger p , g , x , y ;
@@ -271,14 +277,11 @@ public RubyBoolean public_p() {
271277
272278 @ Override
273279 public boolean isPrivateKey () {
274- return dh_x != null /* || haveEngine */ ;
280+ return dh_x != null ;
275281 }
276282
277283 @ JRubyMethod (name = "private?" )
278284 public RubyBoolean private_p () {
279- // FIXME! need to figure out what it means in MRI/OSSL code to
280- // claim a DH is private if an engine is present -- doesn't really
281- // map to Java implementation.
282285 return getRuntime ().newBoolean (isPrivateKey ());
283286 }
284287
@@ -373,6 +376,11 @@ public synchronized IRubyObject set_g(IRubyObject arg) {
373376 return arg ;
374377 }
375378
379+ @ JRubyMethod (name = "q" )
380+ public IRubyObject q (final ThreadContext context ) {
381+ return context .nil ;
382+ }
383+
376384 // don't need synchronized as value is volatile
377385 @ JRubyMethod (name = "pub_key" )
378386 public IRubyObject pub_key () {
0 commit comments