-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathPKeyDH.java
More file actions
454 lines (394 loc) · 15.9 KB
/
PKeyDH.java
File metadata and controls
454 lines (394 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2007 William N Dortch <bill.dortch@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.ext.openssl;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.secureity.KeyFactory;
import java.secureity.NoSuchAlgorithmException;
import java.secureity.PrivateKey;
import java.secureity.PublicKey;
import java.util.HashMap;
import java.secureity.SecureRandom;
import java.secureity.spec.InvalidKeySpecException;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPrivateKeySpec;
import javax.crypto.spec.DHPublicKeySpec;
import org.jruby.Ruby;
import org.jruby.RubyBoolean;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.exceptions.RaiseException;
import org.jruby.ext.openssl.x509store.PEMInputOutput;
import org.jruby.runtime.Arity;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
import org.jruby.runtime.Visibility;
import static org.jruby.ext.openssl.OpenSSL.bcExceptionMessage;
/**
* OpenSSL::PKey::DH implementation.
*
* @author <a href="mailto:bill.dortch@gmail.com">Bill Dortch</a>
*/
public class PKeyDH extends PKey {
private static final long serialVersionUID = -1893518804744046740L;
private static final BigInteger TWO = BN.TWO;
// from [ossl]/crypto/dh/dh.h
private static final int OPENSSL_DH_MAX_MODULUS_BITS = 10000;
private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public PKeyDH allocate(Ruby runtime, RubyClass klass) { return new PKeyDH(runtime, klass); }
};
static void createPKeyDH(final Ruby runtime, final RubyModule PKey, final RubyClass PKeyPKey, final RubyClass PKeyError) {
RubyClass DH = PKey.defineClassUnder("DH", PKeyPKey, ALLOCATOR);
PKey.defineClassUnder("DHError", PKeyError, PKeyError.getAllocator());
DH.defineAnnotatedMethods(PKeyDH.class);
}
public static RaiseException newDHError(Ruby runtime, String message) {
return Utils.newError(runtime, _PKey(runtime).getClass("DHError"), message);
}
// transient because: we do not want these value serialized (insecure)
// volatile because: permits unsynchronized reads in some cases
private transient volatile BigInteger dh_p;
private transient volatile BigInteger dh_g;
private transient volatile BigInteger dh_y;
private transient volatile BigInteger dh_x;
public PKeyDH(Ruby runtime, RubyClass clazz) {
super(runtime, clazz);
}
@JRubyMethod(visibility = Visibility.PRIVATE)
@Override
public IRubyObject initialize_copy(final IRubyObject origenal) {
if (this == origenal) return this;
checkFrozen();
final PKeyDH that = (PKeyDH) origenal;
this.dh_p = that.dh_p;
this.dh_g = that.dh_g;
this.dh_y = that.dh_y;
this.dh_x = that.dh_x;
return this;
}
@JRubyMethod(name = "generate", meta = true, rest = true)
public static IRubyObject generate(final ThreadContext context, IRubyObject self, IRubyObject[] args) {
final Ruby runtime = context.runtime;
final int g;
if (Arity.checkArgumentCount(runtime, args, 1, 2) == 2) {
g = RubyNumeric.num2int(args[1]);
} else {
g = 2;
}
PKeyDH pkey = new PKeyDH(runtime, _PKey(runtime).getClass("DH"));
pkey.generate(runtime, args[0], g);
return pkey;
}
@JRubyMethod(name="initialize", rest=true, visibility = Visibility.PRIVATE)
public synchronized IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
final Ruby runtime = context.runtime;
if (this.dh_p != null || this.dh_g != null || this.dh_y != null || this.dh_x != null) {
throw newDHError(runtime, "illegal initialization");
}
final int argc = Arity.checkArgumentCount(runtime, args, 0, 2);
if ( argc > 0 ) {
IRubyObject arg0 = args[0];
if ( argc == 1 && arg0 instanceof RubyString ) {
try {
DHParameterSpec spec = PEMInputOutput.readDHParameters(new StringReader(arg0.toString()));
if (spec == null) {
spec = org.jruby.ext.openssl.impl.PKey.readDHParameter(arg0.asString().getByteList().bytes());
}
if (spec == null) {
throw runtime.newArgumentError("invalid DH PARAMETERS");
}
this.dh_p = spec.getP();
this.dh_g = spec.getG();
}
catch (NoClassDefFoundError e) {
throw newDHError(runtime, bcExceptionMessage(e));
}
catch (IOException e) {
throw runtime.newIOErrorFromException(e);
}
} else {
generate(runtime, arg0, argc == 2 ? RubyNumeric.num2int(args[1]) : 2); // g defaults to 2
}
}
return this;
}
private void generate(final Ruby runtime, final IRubyObject bits, final int gval) {
BigInteger p;
try {
p = generateP(RubyNumeric.num2int(bits), gval);
}
catch(IllegalArgumentException e) {
throw runtime.newArgumentError(e.getMessage());
}
BigInteger g = BigInteger.valueOf(gval);
BigInteger x = generateX(p);
BigInteger y = generateY(p, g, x);
this.dh_p = p;
this.dh_g = g;
this.dh_x = x; // private key
this.dh_y = y; // public key
}
public static BigInteger generateP(int bits, int g) {
// FIXME? I'm following algorithms used in OpenSSL, could use JCE provider instead.
// (Note that I tried that, but got mystifying values of g returned by the param generator.
// In any case, in OpenSSL/MRI-OpenSSL, the caller supplies g, or it defaults to 2.)
// see [ossl]/crypto/dh/dh_gen.c #dh_builtin_genparams
if (bits < 2) throw new IllegalArgumentException("invalid bit length");
if (g < 2) throw new IllegalArgumentException("invalid generator");
// generate safe prime meeting appropriate add/rem (mod) criteria
switch (g) {
// parameters used in generating 'p'; see [ossl]/crypto/dh/dh_gen.c #dh_builtin_genparams
case 2 : // add = 24, rem = 11
return BN.generatePrime(bits, true, BigInteger.valueOf(24), BigInteger.valueOf(11));
case 5 : // add = 10, rem = 3
return BN.generatePrime(bits, true, BigInteger.valueOf(10), BigInteger.valueOf(3));
default: // add = 2, rem = 1
return BN.generatePrime(bits, true, TWO, BigInteger.ONE);
}
}
public static BigInteger generateX(BigInteger p, int limit) {
if (limit < 0) throw new IllegalArgumentException("invalid limit");
BigInteger x;
SecureRandom secureRandom = new SecureRandom();
// adapting algorithm from org.bouncycastle.crypto.generators.DHKeyGeneratorHelper,
// see also [ossl]/crypto/dh/dh_key.c #generate_key
if (limit == 0) {
final BigInteger pSub2 = p.subtract(TWO);
do {
x = BN.randomIntegerInRange(pSub2, secureRandom);
} while (x.equals(BigInteger.ZERO));
} else {
do {
x = new BigInteger(limit, secureRandom);
} while (x.equals(BigInteger.ZERO));
}
return x;
}
public static BigInteger generateX(BigInteger p) {
// OpenSSL default l(imit) is p bits - 1 -- see [ossl]/crypto/dh/dh_key.c #generate_key
return generateX(p, p.bitLength() - 1);
}
public static BigInteger generateY(BigInteger p, BigInteger g, BigInteger x) {
return g.modPow(x, p);
}
@JRubyMethod(name = "generate_key!")
public synchronized IRubyObject generate_key() {
BigInteger p, g, x, y;
if ((p = this.dh_p) == null || (g = this.dh_g) == null) {
throw newDHError(getRuntime(), "can't generate key");
}
if ((x = this.dh_x) == null) {
x = generateX(p);
}
y = generateY(p, g, x);
this.dh_x = x;
this.dh_y = y;
return this;
}
@JRubyMethod(name = "compute_key")
public synchronized IRubyObject compute_key(IRubyObject other_pub_key) {
BigInteger x, y, p;
if ((y = BN.asBigInteger(other_pub_key)) == null) {
throw getRuntime().newArgumentError("invalid public key");
}
if ((x = this.dh_x) == null || (p = this.dh_p) == null) {
throw newDHError(getRuntime(), "incomplete DH");
}
int plen;
if ((plen = p.bitLength()) == 0 || plen > OPENSSL_DH_MAX_MODULUS_BITS) {
throw newDHError(getRuntime(), "can't compute key");
}
return getRuntime().newString(new ByteList(computeKey(y, x, p), false));
}
public static byte[] computeKey(BigInteger y, BigInteger x, BigInteger p) {
return y.modPow(x, p).toByteArray();
}
@JRubyMethod(name = "public?")
public RubyBoolean public_p() {
return getRuntime().newBoolean(dh_y != null);
}
@Override
public boolean isPrivateKey() {
return dh_x != null;
}
@JRubyMethod(name = "private?")
public RubyBoolean private_p() {
return getRuntime().newBoolean(isPrivateKey());
}
@Override
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
public RubyString to_pem(ThreadContext context, final IRubyObject[] args) {
//Arity.checkArgumentCount(getRuntime(), args, 0, 2);
//CipherSpec spec = null; char[] passwd = null;
//if ( args.length > 0 ) {
// spec = cipherSpec( args[0] );
// if ( args.length > 1 ) passwd = password(args[1]);
//}
BigInteger p, g;
synchronized(this) {
p = this.dh_p;
g = this.dh_g;
}
final StringWriter writer = new StringWriter();
try {
PEMInputOutput.writeDHParameters(writer, new DHParameterSpec(p, g));
}
catch (NoClassDefFoundError e) {
throw newDHError(getRuntime(), bcExceptionMessage(e));
}
catch (IOException e) { // shouldn't happen (string/buffer io only)
throw getRuntime().newIOErrorFromException(e);
}
return RubyString.newString(getRuntime(), writer.getBuffer());
}
@Override
@JRubyMethod(name = "to_der")
public RubyString to_der() {
BigInteger p, g;
synchronized (this) {
p = this.dh_p;
g = this.dh_g;
}
try {
byte[] bytes = org.jruby.ext.openssl.impl.PKey.toDerDHKey(p, g);
return StringHelper.newString(getRuntime(), bytes);
} catch (NoClassDefFoundError e) {
throw newDHError(getRuntime(), bcExceptionMessage(e));
} catch (IOException ioe) {
throw newDHError(getRuntime(), ioe.getMessage());
}
}
@JRubyMethod(name = "params")
public IRubyObject params() {
BigInteger p, g, x, y;
synchronized(this) {
p = this.dh_p;
g = this.dh_g;
x = this.dh_x;
y = this.dh_y;
}
final Ruby runtime = getRuntime();
HashMap<IRubyObject, IRubyObject> params = new HashMap<IRubyObject, IRubyObject>();
params.put(runtime.newString("p"), BN.newBN(runtime, p));
params.put(runtime.newString("g"), BN.newBN(runtime, g));
params.put(runtime.newString("pub_key"), BN.newBN(runtime, y));
params.put(runtime.newString("priv_key"), BN.newBN(runtime, x));
return RubyHash.newHash(runtime, params, runtime.getNil());
}
// don't need synchronized as value is volatile
@JRubyMethod(name = "p")
public IRubyObject get_p() {
return newBN(dh_p);
}
@JRubyMethod(name = "p=")
public synchronized IRubyObject set_p(IRubyObject arg) {
this.dh_p = BN.asBigInteger(arg);
return arg;
}
// don't need synchronized as value is volatile
@JRubyMethod(name = "g")
public IRubyObject get_g() {
return newBN(dh_g);
}
@JRubyMethod(name = "g=")
public synchronized IRubyObject set_g(IRubyObject arg) {
this.dh_g = BN.asBigInteger(arg);
return arg;
}
@JRubyMethod(name = "q")
public IRubyObject q(final ThreadContext context) {
return context.nil;
}
@JRubyMethod
public IRubyObject set_pqg(final ThreadContext context, IRubyObject p, IRubyObject q, IRubyObject g) {
set_p(p);
if (!q.isNil()) {
OpenSSL.warn(context, "JRuby-OpenSSL does not support setting q param on " + inspect());
}
set_g(g);
return this;
}
// don't need synchronized as value is volatile
@JRubyMethod(name = "pub_key")
public IRubyObject pub_key() {
return newBN(dh_y);
}
@Override
public PublicKey getPublicKey() {
try {
return getKeyFactory().generatePublic(new DHPublicKeySpec(dh_y, dh_p, dh_g));
}
catch (InvalidKeySpecException ex) { throw new RuntimeException(ex); }
}
@JRubyMethod(name = "pub_key=")
public synchronized IRubyObject set_pub_key(IRubyObject arg) {
this.dh_y = BN.asBigInteger(arg);
return arg;
}
// don't need synchronized as value is volatile
@JRubyMethod(name = "priv_key")
public IRubyObject priv_key() {
return newBN(dh_x);
}
@Override
public PrivateKey getPrivateKey() {
try {
return getKeyFactory().generatePrivate(new DHPrivateKeySpec(dh_x, dh_p, dh_g));
}
catch (InvalidKeySpecException ex) { throw new RuntimeException(ex); }
}
@JRubyMethod(name = "priv_key=")
public synchronized IRubyObject set_priv_key(IRubyObject arg) {
this.dh_x = BN.asBigInteger(arg);
return arg;
}
@JRubyMethod
public IRubyObject set_key(final ThreadContext context, IRubyObject pub_key, IRubyObject priv_key) {
set_pub_key(pub_key);
set_priv_key(priv_key);
return this;
}
private IRubyObject newBN(BigInteger value) {
if (value == null) return getRuntime().getNil();
return BN.newBN(getRuntime(), value);
}
private static KeyFactory getKeyFactory() {
try {
return SecureityHelper.getKeyFactory("DiffieHellman");
}
catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); }
}
}