Skip to content

Commit

Permalink
Fix tests, they now all pass
Browse files Browse the repository at this point in the history
  • Loading branch information
GoeLin committed Jan 11, 2024
1 parent a8f8b96 commit af1cee7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion test/jdk/javax/xml/crypto/dsig/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void main(String[] args) throws Exception {
var b = Path.of("").toUri().toString();
var d = string2doc(x);
var pass = "changeit".toCharArray();
for (String alg: List.of("DSA", "RSA", "RSASSA-PSS", "EC", "EdDSA", "Ed25519", "Ed448")) {
for (String alg: List.of("DSA", "RSA", /*"RSASSA-PSS", 8241305 not in 11*/ "EC" /*, "EdDSA", "Ed25519", "Ed448" 8166597 not in 11*/)) {
SecurityTools.keytool(String.format(
"-keystore ks -keyalg %s -storepass changeit -genkeypair -alias %s -dname CN=%s",
alg, alg, alg)).shouldHaveExitValue(0);
Expand Down
16 changes: 12 additions & 4 deletions test/jdk/javax/xml/crypto/dsig/GenerationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import org.w3c.dom.*;

import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;

/**
* Test that recreates merlin-xmldsig-twenty-three test vectors (and more)
Expand All @@ -107,6 +108,7 @@ public class GenerationTests {
private static KeyInfo dsa1024, dsa2048, rsa, rsa1024, rsa2048,
p256ki, p384ki, p521ki, ed25519ki, ed448ki;
private static KeySelector kvks = new KeySelectors.KeyValueKeySelector();
private static KeySelector x5ks = new KeySelectors.RawX509KeySelector();
private static KeySelector sks;
private static Key signingKey;
private static PublicKey validatingKey;
Expand Down Expand Up @@ -300,8 +302,10 @@ public static void main(String args[]) throws Exception {
test_create_signature_enveloping_p256_sha512();
test_create_signature_enveloping_p384_sha1();
test_create_signature_enveloping_p521_sha1();
/* "8166597: Crypto support for the EdDSA Signature Algorithm" missing in 11.
test_create_signature_enveloping_ed25519();
test_create_signature_enveloping_ed448();
*/
test_create_signature_external_b64_dsa();
test_create_signature_external_dsa();
test_create_signature_keyname();
Expand Down Expand Up @@ -849,6 +853,7 @@ static void test_create_signature_enveloping_p521_sha1() throws Exception {
System.out.println();
}

/* "8166597: Crypto support for the EdDSA Signature Algorithm" missing in 11.
static void test_create_signature_enveloping_ed25519() throws Exception {
System.out.println("* Generating signature-enveloping-ed25519.xml");
test_create_signature_enveloping(sha1, ed25519, ed25519ki,
Expand All @@ -862,6 +867,7 @@ static void test_create_signature_enveloping_ed448() throws Exception {
getEd448PrivateKey(), x5ks, false, true);
System.out.println();
}
*/

static void test_create_signature_external_b64_dsa() throws Exception {
System.out.println("* Generating signature-external-b64-dsa.xml");
Expand Down Expand Up @@ -2180,12 +2186,12 @@ private static PublicKey getECPublicKey(String curve) throws Exception {

private static X509Certificate getEd25519Certificate() throws Exception {
return (X509Certificate) CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(HexFormat.of().parseHex(ED25519_CERT)));
.generateCertificate(new ByteArrayInputStream(Utils.toByteArray(ED25519_CERT)));
}

private static X509Certificate getEd448Certificate() throws Exception {
return (X509Certificate) CertificateFactory.getInstance("X.509")
.generateCertificate(new ByteArrayInputStream(HexFormat.of().parseHex(ED448_CERT)));
.generateCertificate(new ByteArrayInputStream(Utils.toByteArray(ED448_CERT)));
}

private static PrivateKey getPrivateKey(String algo, int keysize)
Expand Down Expand Up @@ -2241,15 +2247,17 @@ private static PrivateKey getECPrivateKey(String curve) throws Exception {
return kf.generatePrivate(kspec);
}

/* "8166597: Crypto support for the EdDSA Signature Algorithm" missing in 11.
private static PrivateKey getEd25519PrivateKey() throws Exception {
return KeyFactory.getInstance("Ed25519").generatePrivate(new EdECPrivateKeySpec(
NamedParameterSpec.ED25519, HexFormat.of().parseHex(ED25519_KEY)));
NamedParameterSpec.ED25519, Utils.toByteArray(ED25519_KEY)));
}
private static PrivateKey getEd448PrivateKey() throws Exception {
return KeyFactory.getInstance("Ed448").generatePrivate(new EdECPrivateKeySpec(
NamedParameterSpec.ED448, HexFormat.of().parseHex(ED448_KEY)));
NamedParameterSpec.ED448, Utils.toByteArray(ED448_KEY)));
}
*/

private static SecretKey getSecretKey(final byte[] secret) {
return new SecretKey() {
Expand Down
2 changes: 2 additions & 0 deletions test/jdk/javax/xml/crypto/dsig/HereFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ public static void main(String args[]) throws Throwable {
if (!args[0].equals("default")) {
Security.setProperty("jdk.xml.dsig.hereFunctionSupported", args[0]);
}
/* "8259709: Disable SHA-1 XML Signatures" not in 11.
// Re-enable sha1 algs
SecurityUtils.removeAlgsFromDSigPolicy("sha1");
*/

boolean expected = Boolean.parseBoolean(args[1]);

Expand Down
64 changes: 38 additions & 26 deletions test/lib/jdk/test/lib/security/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
import java.net.URI;
import java.security.*;
import java.security.cert.X509Certificate;
import java.security.interfaces.EdECPrivateKey;
// "8166597: Crypto support for the EdDSA Signature Algorithm" missing in 11.
//import java.security.interfaces.EdECPrivateKey;
import java.security.interfaces.RSAKey;
import java.security.spec.NamedParameterSpec;
import java.security.spec.PSSParameterSpec;
Expand Down Expand Up @@ -332,17 +333,20 @@ public Document sign(String str) throws Exception {

// Builds a SignedInfo for a string reference
private SignedInfo buildSignedInfo(String ref) throws Exception {
TransformParameterSpec pSpec;
if (tr.equals(Transform.XPATH)) {
pSpec = new XPathFilterParameterSpec("//.");
} else if (tr.equals(Transform.XPATH2)) {
pSpec = new XPathFilter2ParameterSpec(
Collections.singletonList(new XPathType("//.",
XPathType.Filter.INTERSECT)));
} else {
pSpec = null;
}
return buildSignedInfo(FAC.newReference(
ref,
FAC.newDigestMethod(dm, null),
List.of(FAC.newTransform(tr, switch (tr) {
case Transform.XPATH ->
new XPathFilterParameterSpec("//.");
case Transform.XPATH2 -> new XPathFilter2ParameterSpec(
Collections.singletonList(new XPathType("//.",
XPathType.Filter.INTERSECT)));
default -> null;
})),
List.of(FAC.newTransform(tr, pSpec)),
null, null));
}

Expand All @@ -364,24 +368,32 @@ private SignedInfo buildSignedInfo(Reference ref) throws Exception {
*/
throw new Exception("Code not expected to be used in tests for 11. Backport 8241306.");
} else {
signatureMethod = FAC.newSignatureMethod(switch (alg) {
case "RSA" -> SignatureMethod.RSA_SHA256;
case "DSA" -> SignatureMethod.DSA_SHA256;
case "EC" -> SignatureMethod.ECDSA_SHA256;
case "ED25519" -> "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed25519";
case "ED448" -> "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed448";
case "EDDSA" -> {
if (privateKey instanceof EdECPrivateKey edsk) {
yield edsk.getParams().getName()
.equals(NamedParameterSpec.ED25519.getName())
? "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed25519"
: "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed448";
} else {
throw new InvalidKeyException();
}
if (alg.equals("RSA")) {
signatureMethod = FAC.newSignatureMethod(SignatureMethod.RSA_SHA256, null);
} else if (alg.equals("DSA")) {
signatureMethod = FAC.newSignatureMethod(SignatureMethod.DSA_SHA256, null);
} else if (alg.equals("EC")) {
signatureMethod = FAC.newSignatureMethod(SignatureMethod.ECDSA_SHA256, null);
} else if (alg.equals("ED25519")) {
signatureMethod = FAC.newSignatureMethod("http://www.w3.org/2021/04/xmldsig-more#eddsa-ed25519", null);
} else if (alg.equals("ED448")) {
signatureMethod = FAC.newSignatureMethod("http://www.w3.org/2021/04/xmldsig-more#eddsa-ed448", null);
} else if (alg.equals("EDDSA")) {
/*
if (privateKey instanceof EdECPrivateKey) {
EdECPrivateKey edsk = (EdECPrivateKey)privateKey;
FAC.newSignatureMethod(edsk.getParams().getName()
.equals(NamedParameterSpec.ED25519.getName())
? "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed25519"
: "http://www.w3.org/2021/04/xmldsig-more#eddsa-ed448", null);
} else {
throw new InvalidKeyException();
}
default -> throw new InvalidKeyException();
}, null);
*/
throw new Exception("Code not expected to be used in tests for 11. Backport 8166597.");
} else {
throw new InvalidKeyException();
}
}
} else {
signatureMethod = FAC.newSignatureMethod(sm, smSpec);
Expand Down

0 comments on commit af1cee7

Please sign in to comment.