32
32
import java .io .StringReader ;
33
33
import java .io .StringWriter ;
34
34
import java .math .BigInteger ;
35
- import java .security .KeyFactory ;
36
- import java .security .KeyPair ;
37
- import java .security .KeyPairGenerator ;
38
- import java .security .NoSuchAlgorithmException ;
39
- import java .security .PrivateKey ;
40
- import java .security .PublicKey ;
41
- import java .security .SecureRandom ;
35
+ import java .security .*;
42
36
import java .security .interfaces .DSAKey ;
43
37
import java .security .interfaces .DSAPrivateKey ;
44
38
import java .security .interfaces .DSAPublicKey ;
62
56
import org .jruby .runtime .builtin .IRubyObject ;
63
57
import org .jruby .runtime .ThreadContext ;
64
58
import org .jruby .runtime .Visibility ;
59
+ import org .jruby .util .ByteList ;
65
60
66
61
import static org .jruby .ext .openssl .OpenSSL .*;
67
62
import static org .jruby .ext .openssl .impl .PKey .readDSAPrivateKey ;
@@ -359,10 +354,25 @@ public RubyString to_pem(final IRubyObject[] args) {
359
354
}
360
355
}
361
356
362
- @ JRubyMethod
363
- public IRubyObject syssign (IRubyObject arg ) {
364
- // TODO
365
- return getRuntime ().getNil ();
357
+ @ JRubyMethod // ossl_dsa_sign
358
+ public IRubyObject syssign (IRubyObject data ) {
359
+ final Ruby runtime = getRuntime ();
360
+
361
+ DSAPrivateKey privateKey ;
362
+ if ((privateKey = this .privateKey ) == null ) {
363
+ throw newDSAError (runtime , "Private DSA key needed!" );
364
+ }
365
+
366
+ try {
367
+ Signature signature = SecurityHelper .getSignature ("SHA1withDSA" ); // DSS1
368
+ signature .initSign (privateKey );
369
+ signature .update ( data .convertToString ().getBytes () );
370
+ ByteList sign = new ByteList (signature .sign (), false );
371
+ return RubyString .newString (runtime , sign );
372
+ }
373
+ catch (GeneralSecurityException ex ) {
374
+ throw newPKeyError (runtime , ex .getMessage ());
375
+ }
366
376
}
367
377
368
378
@ JRubyMethod
@@ -372,22 +382,20 @@ public IRubyObject sysverify(IRubyObject arg, IRubyObject arg2) {
372
382
}
373
383
374
384
private DSAKey getDsaKey () {
375
- DSAKey result ;
376
- return (result = publicKey ) != null ? result : privateKey ;
385
+ DSAKey result ;
386
+ return (result = publicKey ) != null ? result : privateKey ;
377
387
}
378
388
379
389
private IRubyObject toBN (BigInteger value ) {
380
- return value == null ? getRuntime ().getNil () : BN .newBN (getRuntime (), value );
390
+ return value == null ? getRuntime ().getNil () : BN .newBN (getRuntime (), value );
381
391
}
382
392
383
393
private synchronized BigInteger getP () {
384
394
DSAKey key = getDsaKey ();
385
395
if (key != null ) {
386
396
return key .getParams ().getP ();
387
397
}
388
- else {
389
- return dsa_p ;
390
- }
398
+ return dsa_p ;
391
399
}
392
400
393
401
@ JRubyMethod (name = "p" )
@@ -405,9 +413,7 @@ private synchronized BigInteger getQ() {
405
413
if (key != null ) {
406
414
return key .getParams ().getQ ();
407
415
}
408
- else {
409
- return dsa_q ;
410
- }
416
+ return dsa_q ;
411
417
}
412
418
413
419
@ JRubyMethod (name = "q" )
@@ -425,9 +431,7 @@ private synchronized BigInteger getG() {
425
431
if (key != null ) {
426
432
return key .getParams ().getG ();
427
433
}
428
- else {
429
- return dsa_g ;
430
- }
434
+ return dsa_g ;
431
435
}
432
436
433
437
@ JRubyMethod (name = "g" )
0 commit comments