@@ -124,28 +124,29 @@ public PKeyDSA(Ruby runtime, RubyClass type, DSAPrivateKey privKey, DSAPublicKey
124124
125125 @ JRubyMethod (name = "generate" , meta = true )
126126 public static IRubyObject generate (IRubyObject self , IRubyObject arg ) {
127- final int keysize = RubyNumeric .fix2int (arg );
128- PKeyDSA dsa = new PKeyDSA (self .getRuntime (), (RubyClass ) self );
129- dsaGenerate (dsa , keysize );
130- return dsa ;
127+ final Ruby runtime = self .getRuntime ();
128+ final int keySize = RubyNumeric .fix2int (arg );
129+ return dsaGenerate (runtime , new PKeyDSA (runtime , (RubyClass ) self ), keySize );
131130 }
132131
133132 /*
134133 * c: dsa_generate
135134 */
136- private static void dsaGenerate (PKeyDSA dsa , int keysize ) throws RaiseException {
135+ private static PKeyDSA dsaGenerate (final Ruby runtime ,
136+ PKeyDSA dsa , int keySize ) throws RaiseException {
137137 try {
138138 KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("DSA" );
139- gen .initialize (keysize , new SecureRandom ());
139+ gen .initialize (keySize , new SecureRandom ());
140140 KeyPair pair = gen .generateKeyPair ();
141141 dsa .privateKey = (DSAPrivateKey ) pair .getPrivate ();
142142 dsa .publicKey = (DSAPublicKey ) pair .getPublic ();
143+ return dsa ;
143144 }
144145 catch (NoSuchAlgorithmException e ) {
145- throw newDSAError (dsa . getRuntime () , e .getMessage ());
146+ throw newDSAError (runtime , e .getMessage ());
146147 }
147148 catch (RuntimeException e ) {
148- throw newDSAError (dsa . getRuntime () , e .getMessage (), e );
149+ throw newDSAError (runtime , e .getMessage (), e );
149150 }
150151 }
151152
@@ -167,8 +168,8 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
167168 if ( args .length > 1 ) pass = args [1 ];
168169
169170 if ( arg instanceof RubyFixnum ) {
170- int keysize = RubyNumeric .fix2int ((RubyFixnum ) arg );
171- dsaGenerate (this , keysize ); return this ;
171+ int keySize = RubyNumeric .fix2int ((RubyFixnum ) arg );
172+ return dsaGenerate (context . runtime , this , keySize ) ;
172173 }
173174
174175 final char [] passwd = password (pass );
@@ -364,21 +365,31 @@ public IRubyObject syssign(IRubyObject data) {
364365 }
365366
366367 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 );
368+ ByteList sign = sign ("NONEwithDSA" , privateKey , data .convertToString ().getByteList ()); // DSS1
371369 return RubyString .newString (runtime , sign );
372370 }
373371 catch (GeneralSecurityException ex ) {
374- throw newPKeyError (runtime , ex .getMessage ());
372+ throw newDSAError (runtime , ex .getMessage ());
375373 }
376374 }
377375
378- @ JRubyMethod
379- public IRubyObject sysverify (IRubyObject arg , IRubyObject arg2 ) {
380- // TODO
381- return getRuntime ().getNil ();
376+ @ JRubyMethod // ossl_dsa_verify
377+ public IRubyObject sysverify (IRubyObject data , IRubyObject sign ) {
378+ final Ruby runtime = getRuntime ();
379+ ByteList sigBytes = convertToString (runtime , sign , "OpenSSL::PKey::DSAError" , "invalid signature" ).getByteList ();
380+ ByteList dataBytes = convertToString (runtime , data , "OpenSSL::PKey::DSAError" , "invalid data" ).getByteList ();
381+ try {
382+ return runtime .newBoolean ( verify ("NONEwithDSA" , getPublicKey (), dataBytes , sigBytes ) );
383+ }
384+ catch (NoSuchAlgorithmException e ) {
385+ throw newDSAError (runtime , e .getMessage ());
386+ }
387+ catch (SignatureException e ) {
388+ throw newDSAError (runtime , "invalid signature" );
389+ }
390+ catch (InvalidKeyException e ) {
391+ throw newDSAError (runtime , "invalid key" );
392+ }
382393 }
383394
384395 private DSAKey getDsaKey () {
0 commit comments