Skip to content

Commit

Permalink
Fix the signatures done with the JCA provider using multiple updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ebourg committed Jun 7, 2024
1 parent 5a56502 commit 7e69e26
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package net.jsign.jca;

import java.io.ByteArrayOutputStream;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.security.SignatureException;

class SigningServiceSignature extends AbstractSignatureSpi {

private SigningServicePrivateKey privateKey;
private byte[] data;
private ByteArrayOutputStream buffer = new ByteArrayOutputStream();

public SigningServiceSignature(String signingAlgorithm) {
super(signingAlgorithm);
Expand All @@ -36,14 +37,13 @@ protected void engineInitSign(PrivateKey privateKey) {

@Override
protected void engineUpdate(byte[] b, int off, int len) {
data = new byte[len];
System.arraycopy(b, off, data, 0, len);
buffer.write(b, off, len);
}

@Override
protected byte[] engineSign() throws SignatureException {
try {
return privateKey.getService().sign(privateKey, signingAlgorithm, data);
return privateKey.getService().sign(privateKey, signingAlgorithm, buffer.toByteArray());
} catch (GeneralSecurityException e) {
throw new SignatureException(e);
}
Expand Down
14 changes: 12 additions & 2 deletions jsign-crypto/src/test/java/net/jsign/jca/SigningServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ public void testCustomProvider(Provider signingProvider, KeyStore keystore, Stri

Signature signature = Signature.getInstance("SHA256withRSA", signingProvider);
signature.initSign((PrivateKey) key);
signature.update("Hello World".getBytes());
byte[] s1 = signature.sign();

assertNotNull("signature null", s1);

// test with multiple updates
signature = Signature.getInstance("SHA256withRSA", signingProvider);
signature.initSign((PrivateKey) key);
signature.update("Hello".getBytes());
byte[] s = signature.sign();
signature.update(" ".getBytes());
signature.update("World".getBytes());
byte[] s2 = signature.sign();

assertNotNull("signature null", s);
assertArrayEquals("signature", s1, s2);
}

@Test
Expand Down

0 comments on commit 7e69e26

Please sign in to comment.