I'm trying to verify a message signed with private key, using the provided public key and a signature.
The command line that works like charm is
cat call.blah | openssl dgst -sha256 -verify public-key.pem -signature signature.sig
Verified OK
The public-key.pem has been created from X509 certificate using the command
openssl x509 -pubkey -noout -in x509key.cer > public-key.pem
Now I am trying to mimic that in groovy, here's my code
static void main(String[] args) {
/* Verify a DSA signature */
if (args.length != 3) {
System.out.println("Usage: VerSig publickeyfile signaturefile datafile")
} else try {
/* import encoded public key */
FileInputStream keyfis = new FileInputStream(args[0])
CertificateFactory f = CertificateFactory.getInstance("X.509")
X509Certificate certificate = (X509Certificate)f.generateCertificate(keyfis)
PublicKey pubKey = certificate.getPublicKey()
/* input the signature bytes */
FileInputStream sigfis = new FileInputStream(args[1])
byte[] sigToVerify = new byte[sigfis.available()]
sigfis.read(sigToVerify)
sigfis.close()
/* create a Signature object and initialize it with the public key */
Signature sig = Signature.getInstance("SHA256withRSA")
sig.initVerify(pubKey)
/* Update and verify the data */
FileInputStream datafis = new FileInputStream(args[2])
BufferedInputStream bufin = new BufferedInputStream(datafis)
byte[] buffer = new byte[1024]
int len
while (bufin.available() != 0) {
len = bufin.read(buffer)
sig.update(buffer, 0, len)
}
bufin.close()
boolean verifies = sig.verify(sigToVerify) // <---- THIS IS WHERE THE ERROR OCCURS
System.out.println("signature verifies: " + verifies)
} catch (Exception e) {
e.printStackTrace()
}
}
and i'm running this with these parameters
x509key.cer signature.sig call.blah
(it's the X509 certificate, signature and the signed message body)
Getting out the following stacktrace
java.security.SignatureException: Signature length not correct: got 512 but was expecting 256
at sun.security.rsa.RSASignature.engineVerify(RSASignature.java:190)
at java.security.Signature$Delegate.engineVerify(Signature.java:1174)
at java.security.Signature.verify(Signature.java:624)
at java_security_Signature$verify$2.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at VerSig.main(VerSign.groovy:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
What am I doing wrong?
I used the following dependency:
compile 'org.bouncycastle:bcprov-jdk15on:1.50'
and that code
import com.google.common.base.Charsets
import groovy.transform.PackageScope
import org.bouncycastle.util.io.pem.PemReader
import org.springframework.core.io.ClassPathResource
import java.security.Key
import java.security.KeyFactory
import java.security.Signature
import java.security.spec.KeySpec
import java.security.spec.PKCS8EncodedKeySpec
import java.security.spec.X509EncodedKeySpec
import static org.apache.commons.codec.binary.Base64.decodeBase64
import static org.apache.commons.codec.binary.Base64.encodeBase64String
import static org.apache.commons.io.IOUtils.toByteArray
@PackageScope
class MessageSigner {
public static final String KEY_TYPE = "RSA"
public static final String SIGNATURE_ALGORITHM = "SHA1withRSA"
private final ClassPathResource privateKey
private final ClassPathResource publicKey
MessageSigner(ClassPathResource privateKey, ClassPathResource publicKey) {
this.privateKey = privateKey
this.publicKey = publicKey
}
String sign(String body) {
KeyFactory keyFactory = KeyFactory.getInstance(KEY_TYPE)
KeySpec keySpec = new PKCS8EncodedKeySpec(toByteArray(privateKey.inputStream))
Key key = keyFactory.generatePrivate(keySpec)
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM)
signature.initSign(key)
signature.update(body.bytes)
return encodeBase64String(signature.sign())
}
boolean verify(String body, String signatureFromTrustly) {
PemReader reader = new PemReader(new InputStreamReader(publicKey.inputStream))
byte[] pubKey = reader.readPemObject().getContent()
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(pubKey)
KeyFactory keyFactory = KeyFactory.getInstance(KEY_TYPE)
Key key = keyFactory.generatePublic(keySpec)
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM)
signature.initVerify(key)
byte[] bodyBytes = body.getBytes(Charsets.UTF_8)
signature.update(bodyBytes)
return signature.verify(decodeBase64(signatureFromTrustly))
}
}
For signing and verificaiton
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With