spring-bootrsasignaturesha256node-forge

Why is the signature verification not working when the signature is constructed by node-forge?


I have a Nuxt application that needs to retrieve some information from a Spring Boot-based auth service.

Right now I sign a text message on the Nuxt app (the auth server is aware of that text message), using node-forge, and then I send it encrypted and with the signature for verification on the auth service.

The problem is that the auth service keeps telling me that the size of the signature is wrong, with a java.security.SignatureException: Signature length not correct: got 3XX but was expecting 256.

Here is the code generating the encrypted message and signature on the Nuxt side:

var md = forge.md.sha256.create();
md.update("123"); // for example purposes
var sign = pPrivateKey.sign(md);
var digestBytes = md.digest().bytes();
console.log("Signature:", sign );
console.log("Encrypted:", digestBytes);
console.log("Encrypted B64:", Buffer.from(digestBytes).toString("base64"));

var keyAuthB64Url = Buffer.from(digestBytes).toString("base64url");

var signB64Url = Buffer.from(sign).toString("base64url");

var jwt = await axios.get(process.env.URL + "/auth", { params: { encrypted: keyAuthB64Url, signature: signB64Url } });

On the auth service I have the following code:

byte[] messageBytes = Base64.getUrlDecoder().decode(encryptedMessage);
byte[] signatureBytes = Base64.getUrlDecoder().decode(signature);

Signature sign = Signature.getInstance("SHA256withRSA");

sign.initVerify(certPublicKey);

sign.update(messageBytes);

boolean verified = sign.verify(signatureBytes);

if (!verified) {
        throw new Exception("Not verified!");
}

From all the debugging I have done, it seems like the Spring Boot app has a problem with the signature generated by node-forge on the Nuxt side, with a signature generated in the Spring Boot app the verification works.


Solution

  • There are several issues:

    With these fixes, verification with the Java code works.