@@ -97,12 +97,6 @@ public static RaiseException newDHError(Ruby runtime, String message) {
97
97
private transient volatile BigInteger dh_y ;
98
98
private transient volatile BigInteger dh_x ;
99
99
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
-
106
100
public PKeyDH (Ruby runtime , RubyClass clazz ) {
107
101
super (runtime , clazz );
108
102
}
@@ -120,6 +114,21 @@ public IRubyObject initialize_copy(final IRubyObject original) {
120
114
return this ;
121
115
}
122
116
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
+
123
132
@ JRubyMethod (name ="initialize" , rest =true , visibility = Visibility .PRIVATE )
124
133
public synchronized IRubyObject initialize (final ThreadContext context , final IRubyObject [] args ) {
125
134
final Ruby runtime = context .runtime ;
@@ -150,28 +159,29 @@ public synchronized IRubyObject initialize(final ThreadContext context, final IR
150
159
throw runtime .newIOErrorFromException (e );
151
160
}
152
161
} 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
170
163
}
171
164
}
172
165
return this ;
173
166
}
174
167
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
+
175
185
public static BigInteger generateP (int bits , int g ) {
176
186
177
187
// 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) {
225
235
return g .modPow (x , p );
226
236
}
227
237
228
- public static BigInteger generateY (BigInteger p , int g , BigInteger x ) {
229
- return generateY (p , BigInteger .valueOf (g ), x );
230
- }
231
-
232
238
@ JRubyMethod (name = "generate_key!" )
233
239
public synchronized IRubyObject generate_key () {
234
240
BigInteger p , g , x , y ;
@@ -271,14 +277,11 @@ public RubyBoolean public_p() {
271
277
272
278
@ Override
273
279
public boolean isPrivateKey () {
274
- return dh_x != null /* || haveEngine */ ;
280
+ return dh_x != null ;
275
281
}
276
282
277
283
@ JRubyMethod (name = "private?" )
278
284
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.
282
285
return getRuntime ().newBoolean (isPrivateKey ());
283
286
}
284
287
@@ -373,6 +376,11 @@ public synchronized IRubyObject set_g(IRubyObject arg) {
373
376
return arg ;
374
377
}
375
378
379
+ @ JRubyMethod (name = "q" )
380
+ public IRubyObject q (final ThreadContext context ) {
381
+ return context .nil ;
382
+ }
383
+
376
384
// don't need synchronized as value is volatile
377
385
@ JRubyMethod (name = "pub_key" )
378
386
public IRubyObject pub_key () {
0 commit comments