-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inconsistency when signing data (#656)
- Fixes a NPE in case the Testcontainers module receives an immutable set of vault names - Fixes RSA signature and verification algorithms (allowing the client to do hashing) - Changes EC signature and verification logic to use BC provider - Simplifies signature and verification implementation by extracting common parts - Adds additional validation steps to verify digest length based on the accepted hashes when signature and verification are used - Adds new test cases to make sure correct algorithms are used by sign logic - Updates tests where necessary Resolves #651 {minor} Signed-off-by: Esta Nagy <[email protected]>
- Loading branch information
Showing
14 changed files
with
336 additions
and
179 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...src/main/java/com/github/nagyesta/lowkeyvault/model/v7_2/key/constants/HashAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.github.nagyesta.lowkeyvault.model.v7_2.key.constants; | ||
|
||
import org.bouncycastle.asn1.ASN1ObjectIdentifier; | ||
import org.bouncycastle.asn1.DERNull; | ||
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; | ||
import org.bouncycastle.asn1.x509.AlgorithmIdentifier; | ||
import org.bouncycastle.asn1.x509.DigestInfo; | ||
import org.springframework.util.Assert; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Array; | ||
import java.security.spec.MGF1ParameterSpec; | ||
import java.security.spec.PSSParameterSpec; | ||
import java.util.Optional; | ||
|
||
@SuppressWarnings("checkstyle:JavadocVariable") | ||
public enum HashAlgorithm { | ||
SHA256("SHA-256", 32, NISTObjectIdentifiers.id_sha256), | ||
SHA384("SHA-384", 48, NISTObjectIdentifiers.id_sha384), | ||
SHA512("SHA-512", 64, NISTObjectIdentifiers.id_sha512); | ||
|
||
private final String algorithmName; | ||
private final ASN1ObjectIdentifier algorithmIdentifier; | ||
private final int digestLength; | ||
|
||
HashAlgorithm(final String algorithmName, final int digestLength, final ASN1ObjectIdentifier algorithmIdentifier) { | ||
this.algorithmName = algorithmName; | ||
this.algorithmIdentifier = algorithmIdentifier; | ||
this.digestLength = digestLength; | ||
} | ||
|
||
public String getAlgorithmName() { | ||
return algorithmName; | ||
} | ||
|
||
public byte[] encodeDigest(final byte[] digest) throws IOException { | ||
return new DigestInfo(new AlgorithmIdentifier(algorithmIdentifier, DERNull.INSTANCE), digest).getEncoded(); | ||
} | ||
|
||
public PSSParameterSpec getPssParameter() { | ||
return new PSSParameterSpec( | ||
algorithmName, | ||
"MGF1", | ||
new MGF1ParameterSpec(algorithmName), | ||
digestLength, | ||
PSSParameterSpec.TRAILER_FIELD_BC | ||
); | ||
} | ||
|
||
public void verifyDigestLength(final byte[] digest) { | ||
final int length = Optional.ofNullable(digest) | ||
.map(Array::getLength) | ||
.orElseThrow(() -> new IllegalArgumentException("Digest is null.")); | ||
Assert.isTrue(digestLength == length, | ||
"This algorithm does not support digest length: " + length + ". Expected: " + digestLength); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
lowkey-vault-app/src/test/java/com/github/nagyesta/lowkeyvault/HashUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.nagyesta.lowkeyvault; | ||
|
||
import com.github.nagyesta.lowkeyvault.model.v7_2.key.constants.HashAlgorithm; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
public final class HashUtil { | ||
|
||
private HashUtil() { | ||
} | ||
|
||
public static byte[] hash(final byte[] data, final HashAlgorithm algorithm) { | ||
try { | ||
return hash(data, algorithm.getAlgorithmName()); | ||
} catch (final NoSuchAlgorithmException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
|
||
private static byte[] hash(final byte[] data, final String algorithm) throws NoSuchAlgorithmException { | ||
final MessageDigest md = MessageDigest.getInstance(algorithm); | ||
md.update(data); | ||
return md.digest(); | ||
} | ||
} |
Oops, something went wrong.