3232import java .io .StringReader ;
3333import java .io .StringWriter ;
3434import 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 .*;
4236import java .security .interfaces .DSAKey ;
4337import java .security .interfaces .DSAPrivateKey ;
4438import java .security .interfaces .DSAPublicKey ;
6256import org .jruby .runtime .builtin .IRubyObject ;
6357import org .jruby .runtime .ThreadContext ;
6458import org .jruby .runtime .Visibility ;
59+ import org .jruby .util .ByteList ;
6560
6661import static org .jruby .ext .openssl .OpenSSL .*;
6762import static org .jruby .ext .openssl .impl .PKey .readDSAPrivateKey ;
@@ -359,10 +354,25 @@ public RubyString to_pem(final IRubyObject[] args) {
359354 }
360355 }
361356
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+ }
366376 }
367377
368378 @ JRubyMethod
@@ -372,22 +382,20 @@ public IRubyObject sysverify(IRubyObject arg, IRubyObject arg2) {
372382 }
373383
374384 private DSAKey getDsaKey () {
375- DSAKey result ;
376- return (result = publicKey ) != null ? result : privateKey ;
385+ DSAKey result ;
386+ return (result = publicKey ) != null ? result : privateKey ;
377387 }
378388
379389 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 );
381391 }
382392
383393 private synchronized BigInteger getP () {
384394 DSAKey key = getDsaKey ();
385395 if (key != null ) {
386396 return key .getParams ().getP ();
387397 }
388- else {
389- return dsa_p ;
390- }
398+ return dsa_p ;
391399 }
392400
393401 @ JRubyMethod (name = "p" )
@@ -405,9 +413,7 @@ private synchronized BigInteger getQ() {
405413 if (key != null ) {
406414 return key .getParams ().getQ ();
407415 }
408- else {
409- return dsa_q ;
410- }
416+ return dsa_q ;
411417 }
412418
413419 @ JRubyMethod (name = "q" )
@@ -425,9 +431,7 @@ private synchronized BigInteger getG() {
425431 if (key != null ) {
426432 return key .getParams ().getG ();
427433 }
428- else {
429- return dsa_g ;
430- }
434+ return dsa_g ;
431435 }
432436
433437 @ JRubyMethod (name = "g" )
0 commit comments