Skip to content

Commit d434bc6

Browse files
committed
implement PKeyDSA#syssign and PKeyDSA#sysverify methods
1 parent 2fbb259 commit d434bc6

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

src/main/java/org/jruby/ext/openssl/PKeyDSA.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

src/test/ruby/dsa/test_dsa.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,14 @@ def test_dsa_from_params_public_first
6161
assert_equal(key.send(param), dsa.send(param), param)
6262
end
6363
end
64+
65+
def test_dsa_sys_sign_verify
66+
dsa = OpenSSL::PKey::DSA.new(1024)
67+
doc = 'Sign ME!'
68+
digest = OpenSSL::Digest::SHA1.digest(doc)
69+
sig = dsa.syssign(digest)
70+
puts sig.inspect if $VERBOSE
71+
assert dsa.sysverify(digest, sig).eql?(true)
72+
end
73+
6474
end

0 commit comments

Comments
 (0)