Skip to content

Commit

Permalink
added DigestAlgorithmIdentifierFinder support as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Dec 3, 2024
1 parent decbe39 commit 05ac657
Showing 1 changed file with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class JcaContentSignerBuilder

private OperatorHelper helper = new OperatorHelper(new DefaultJcaJceHelper());
private SecureRandom random;
private DigestAlgorithmIdentifierFinder digestAlgIdFinder;

private AlgorithmIdentifier sigAlgId;
private AlgorithmParameterSpec sigAlgSpec;
Expand All @@ -78,6 +79,14 @@ public JcaContentSignerBuilder(String signatureAlgorithm, AlgorithmIdentifier si
{
this.signatureAlgorithm = signatureAlgorithm;
this.signatureDigestAlgorithm = signatureDigestAlgorithmID;
this.digestAlgIdFinder = null;
}

public JcaContentSignerBuilder(String signatureAlgorithm, DigestAlgorithmIdentifierFinder digestAlgIdFinder)
{
this.signatureAlgorithm = signatureAlgorithm;
this.signatureDigestAlgorithm = null;
this.digestAlgIdFinder = digestAlgIdFinder;
}

public JcaContentSignerBuilder(String signatureAlgorithm, AlgorithmParameterSpec sigParamSpec)
Expand Down Expand Up @@ -147,20 +156,7 @@ public ContentSigner build(PrivateKey privateKey)
{
if (sigAlgSpec == null)
{
if (isAlgIdFromPrivate.contains(Strings.toUpperCase(signatureAlgorithm)))
{
this.sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER.find(privateKey.getAlgorithm());
if (this.sigAlgId == null)
{
this.sigAlgId = PrivateKeyInfo.getInstance(privateKey.getEncoded()).getPrivateKeyAlgorithm();
}
this.sigAlgSpec = null;
}
else
{
this.sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER.find(signatureAlgorithm);
this.sigAlgSpec = null;
}
this.sigAlgId = getSigAlgId(privateKey);
}
final AlgorithmIdentifier signatureAlgId = sigAlgId;
final Signature sig = helper.createSignature(sigAlgId);
Expand Down Expand Up @@ -201,11 +197,11 @@ public byte[] getSignature()
}
};

if (signatureDigestAlgorithm != null)
if (signatureDigestAlgorithm != null || digestAlgIdFinder != null)
{
return new ExtendedContentSigner()
{
private final AlgorithmIdentifier digestAlgorithm = signatureDigestAlgorithm;
private final AlgorithmIdentifier digestAlgorithm = (signatureDigestAlgorithm != null) ? signatureDigestAlgorithm : digestAlgIdFinder.find(contentSigner.getAlgorithmIdentifier());
private final ContentSigner signer = contentSigner;

public AlgorithmIdentifier getDigestAlgorithmIdentifier()
Expand Down Expand Up @@ -240,6 +236,23 @@ public byte[] getSignature()
}
}

private AlgorithmIdentifier getSigAlgId(PrivateKey privateKey)
{
if (isAlgIdFromPrivate.contains(Strings.toUpperCase(signatureAlgorithm)))
{
AlgorithmIdentifier sigAlgId = SIGNATURE_ALGORITHM_IDENTIFIER_FINDER.find(privateKey.getAlgorithm());
if (sigAlgId == null)
{
return PrivateKeyInfo.getInstance(privateKey.getEncoded()).getPrivateKeyAlgorithm();
}
return sigAlgId;
}
else
{
return SIGNATURE_ALGORITHM_IDENTIFIER_FINDER.find(signatureAlgorithm);
}
}

private ContentSigner buildComposite(CompositePrivateKey privateKey)
throws OperatorCreationException
{
Expand Down

0 comments on commit 05ac657

Please sign in to comment.