Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/4.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ OpenPDF 3.0.x for Java 21 is maintained in the [3.0-java21](https://github.com/L
algorithm identifiers of an ML-DSA signature are written without parameters. The filter `VERISIGN_SIGNED` signs
with SHA-512 when the key is an ML-DSA key, and `WINCER_SIGNED`, whose sub-filter is defined with SHA-1, no longer
accepts one.
* **Legacy Dilithium keys and signatures are rejected (fixes #1812).** `PdfPKCS7` no longer treats the names
`Dilithium2`, `Dilithium3` and `Dilithium5` as ML-DSA-44, ML-DSA-65 and ML-DSA-87. CRYSTALS-Dilithium is a different
algorithm, and its signatures were written under the OIDs of ML-DSA. Signing with a Dilithium key, or
`setExternalDigest` with a Dilithium name, now throws. Use an ML-DSA key.

## 🔒 Security
These fixes are also in [OpenPDF 3.0.7](3.0.7.md).
Expand Down
12 changes: 6 additions & 6 deletions openpdf-core/src/main/java/org/openpdf/text/pdf/PdfPKCS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ public static String getDigestOid(String digestName) {

/**
* Tests whether the given JCE key/signature algorithm name refers to an
* ML-DSA (NIST FIPS 204) post-quantum signature algorithm. The legacy
* Bouncy Castle "Dilithium" names are accepted as aliases.
* ML-DSA (NIST FIPS 204) post-quantum signature algorithm. The names of the legacy CRYSTALS-Dilithium algorithm
* are not: Dilithium is a different algorithm with other OIDs, and its signatures are not ML-DSA signatures.
*/
static boolean isMlDsaName(String name) {
return name != null && (name.startsWith("ML-DSA") || name.startsWith("Dilithium"));
return name != null && name.startsWith("ML-DSA");
}

/**
Expand All @@ -655,9 +655,9 @@ static boolean isMlDsaName(String name) {
*/
private static String mlDsaParameterSetOid(String name) {
return switch (name) {
case "ML-DSA-44", "Dilithium2" -> ID_ML_DSA_44;
case "ML-DSA-65", "Dilithium3" -> ID_ML_DSA_65;
case "ML-DSA-87", "Dilithium5" -> ID_ML_DSA_87;
case "ML-DSA-44" -> ID_ML_DSA_44;
case "ML-DSA-65" -> ID_ML_DSA_65;
case "ML-DSA-87" -> ID_ML_DSA_87;
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openpdf.text.Document;
import org.openpdf.text.ExceptionConverter;
import org.openpdf.text.Paragraph;

/**
Expand Down Expand Up @@ -176,6 +177,71 @@ void externalSignatureTakesTheParameterSetFromTheCertificate(String parameterSet
assertThat(verify(cms, CONTENT)).isTrue();
}

/**
* A private key of the legacy CRYSTALS-Dilithium algorithm, which is not ML-DSA.
*/
private record DilithiumKey(String algorithm) implements PrivateKey {

@Override
public String getAlgorithm() {
return algorithm;
}

@Override
public String getFormat() {
return "PKCS#8";
}

@Override
public byte[] getEncoded() {
return new byte[0];
}
}

@ParameterizedTest
@CsvSource({"Dilithium2", "Dilithium3", "Dilithium5", "DILITHIUM3"})
void rejectsDilithiumKeys(String algorithm) throws Exception {
Signer signer = signer("ML-DSA-65");

assertThatThrownBy(() -> new PdfPKCS7(new DilithiumKey(algorithm), signer.chain(), null, "SHA512",
JDK_PROVIDER, false))
.isInstanceOf(NoSuchAlgorithmException.class)
.hasMessageContaining(algorithm);
}

@ParameterizedTest
@CsvSource({"Dilithium2", "Dilithium3", "Dilithium5"})
void rejectsExternalDilithiumSignatures(String algorithm) throws Exception {
Signer signer = signer("ML-DSA-65");
PdfPKCS7 pkcs7 = new PdfPKCS7(null, signer.chain(), null, "SHA512", JDK_PROVIDER, false);

// a Dilithium signature must not be written under an ML-DSA OID, even when the certificate has an ML-DSA key
assertThatThrownBy(() -> pkcs7.setExternalDigest(new byte[3309], null, algorithm))
.isInstanceOf(ExceptionConverter.class)
.hasMessageContaining(algorithm);
}

@ParameterizedTest
@CsvSource({
"ML-DSA-44, 2.16.840.1.101.3.4.3.17",
"ML-DSA-65, 2.16.840.1.101.3.4.3.18",
"ML-DSA-87, 2.16.840.1.101.3.4.3.19"})
void externalSignatureNamedWithItsParameterSet(String parameterSet, String oid) throws Exception {
Signer signer = signer(parameterSet);
Calendar signingTime = Calendar.getInstance();
byte[] hash = MessageDigest.getInstance("SHA-512").digest(CONTENT);

PdfPKCS7 pkcs7 = new PdfPKCS7(null, signer.chain(), null, "SHA512", JDK_PROVIDER, false);
Signature signature = Signature.getInstance(parameterSet, JDK_PROVIDER);
signature.initSign(signer.key());
signature.update(pkcs7.getAuthenticatedAttributeBytes(hash, signingTime, null));
pkcs7.setExternalDigest(signature.sign(), null, parameterSet);
byte[] cms = pkcs7.getEncodedPKCS7(hash, signingTime);

assertAlgorithm(signerInfo(cms).getDigestEncryptionAlgorithm(), oid);
assertThat(verify(cms, CONTENT)).isTrue();
}

@ParameterizedTest
@CsvSource({"ML-DSA-44", "ML-DSA-65", "ML-DSA-87"})
void signsPdfDocument(String parameterSet) throws Exception {
Expand Down
Loading