@@ -124,28 +124,29 @@ public PKeyDSA(Ruby runtime, RubyClass type, DSAPrivateKey privKey, DSAPublicKey
124
124
125
125
@ JRubyMethod (name = "generate" , meta = true )
126
126
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 );
131
130
}
132
131
133
132
/*
134
133
* c: dsa_generate
135
134
*/
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 {
137
137
try {
138
138
KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("DSA" );
139
- gen .initialize (keysize , new SecureRandom ());
139
+ gen .initialize (keySize , new SecureRandom ());
140
140
KeyPair pair = gen .generateKeyPair ();
141
141
dsa .privateKey = (DSAPrivateKey ) pair .getPrivate ();
142
142
dsa .publicKey = (DSAPublicKey ) pair .getPublic ();
143
+ return dsa ;
143
144
}
144
145
catch (NoSuchAlgorithmException e ) {
145
- throw newDSAError (dsa . getRuntime () , e .getMessage ());
146
+ throw newDSAError (runtime , e .getMessage ());
146
147
}
147
148
catch (RuntimeException e ) {
148
- throw newDSAError (dsa . getRuntime () , e .getMessage (), e );
149
+ throw newDSAError (runtime , e .getMessage (), e );
149
150
}
150
151
}
151
152
@@ -167,8 +168,8 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
167
168
if ( args .length > 1 ) pass = args [1 ];
168
169
169
170
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 ) ;
172
173
}
173
174
174
175
final char [] passwd = password (pass );
@@ -364,21 +365,31 @@ public IRubyObject syssign(IRubyObject data) {
364
365
}
365
366
366
367
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
371
369
return RubyString .newString (runtime , sign );
372
370
}
373
371
catch (GeneralSecurityException ex ) {
374
- throw newPKeyError (runtime , ex .getMessage ());
372
+ throw newDSAError (runtime , ex .getMessage ());
375
373
}
376
374
}
377
375
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
+ }
382
393
}
383
394
384
395
private DSAKey getDsaKey () {
0 commit comments