You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2024/03/21 11:14:17 UTC
(santuario-xml-security-java) branch main updated: Add brainpool curves for EC key generation/cypher (main branch) (#293)
This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/santuario-xml-security-java.git
The following commit(s) were added to refs/heads/main by this push:
new e0eeef36 Add brainpool curves for EC key generation/cypher (main branch) (#293)
e0eeef36 is described below
commit e0eeef36f1b55a4605255b1c6d5c443ee7fd120a
Author: Yevgen Klymenko <ye...@klymenko.de>
AuthorDate: Thu Mar 21 12:14:12 2024 +0100
Add brainpool curves for EC key generation/cypher (main branch) (#293)
* Add brainpool curves
* Add test for brainpool signature
---------
Co-authored-by: Yevgen Klymenko <co...@klymenko.de>
---
.../jcp/xml/dsig/internal/dom/DOMKeyValue.java | 48 ++++
.../algorithms/implementations/ECDSAUtils.java | 40 +++
.../keys/content/keyvalues/ECKeyValue.java | 53 +++-
.../dom/encryption/XMLCipherBrainpoolTest.java | 247 ++++++++++++++++++
.../dom/signature/ECBrainpoolSignatureTest.java | 289 +++++++++++++++++++++
.../xml/security/samples/input/ecbrainpool.jks | Bin 0 -> 722 bytes
6 files changed, 676 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
index 3549c780..3cbb042e 100644
--- a/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
+++ b/src/main/java/org/apache/jcp/xml/dsig/internal/dom/DOMKeyValue.java
@@ -392,6 +392,42 @@ public abstract class DOMKeyValue<K extends PublicKey> extends DOMStructure impl
1
);
+ private static final Curve BRAINPOOLP256R1 = initializeCurve(
+ "brainpoolP256r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.7",
+ "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377",
+ "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9",
+ "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6",
+ "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262",
+ "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997",
+ "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7",
+ 1
+ );
+
+ private static final Curve BRAINPOOLP384R1 = initializeCurve(
+ "brainpoolP384r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.11",
+ "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53",
+ "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826",
+ "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11",
+ "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E",
+ "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315",
+ "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565",
+ 1
+ );
+
+ private static final Curve BRAINPOOLP512R1 = initializeCurve(
+ "brainpoolP512r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.13",
+ "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3",
+ "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA",
+ "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723",
+ "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822",
+ "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892",
+ "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069",
+ 1
+ );
+
private static Curve initializeCurve(String name, String oid,
String sfield, String a, String b,
String x, String y, String n, int h) {
@@ -471,6 +507,12 @@ public abstract class DOMKeyValue<K extends PublicKey> extends DOMStructure impl
match = SECP384R1;
} else if (matchCurve(params, SECP521R1)) {
match = SECP521R1;
+ } else if (matchCurve(params, BRAINPOOLP256R1)) {
+ match = BRAINPOOLP256R1;
+ } else if (matchCurve(params, BRAINPOOLP384R1)) {
+ match = BRAINPOOLP384R1;
+ } else if (matchCurve(params, BRAINPOOLP512R1)) {
+ match = BRAINPOOLP512R1;
} else {
return null;
}
@@ -578,6 +620,12 @@ public abstract class DOMKeyValue<K extends PublicKey> extends DOMStructure impl
return SECP384R1;
} else if (oid.equals(SECP521R1.getObjectId())) {
return SECP521R1;
+ } else if (oid.equals(BRAINPOOLP256R1.getObjectId())) {
+ return BRAINPOOLP256R1;
+ } else if (oid.equals(BRAINPOOLP384R1.getObjectId())) {
+ return BRAINPOOLP384R1;
+ } else if (oid.equals(BRAINPOOLP512R1.getObjectId())) {
+ return BRAINPOOLP512R1;
} else {
return null;
}
diff --git a/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java b/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
index f821950b..7f09310e 100644
--- a/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
+++ b/src/main/java/org/apache/xml/security/algorithms/implementations/ECDSAUtils.java
@@ -764,6 +764,46 @@ public final class ECDSAUtils {
"0340340340340340340340340340340340340340340340340340340323c313fab50589703b5ec68d3587fec60d161cc149c1ad4a91",
0x2760)
);
+
+ ecCurveDefinitions.add(
+ new ECCurveDefinition(
+ "brainpoolP256r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.7",
+ "a9fb57dba1eea9bc3e660a909d838d726e3bf623d52620282013481d1f6e5377",
+ "7d5a0975fc2c3057eef67530417affe7fb8055c126dc5c6ce94a4b44f330b5d9",
+ "26dc5c6ce94a4b44f330b5d9bbd77cbf958416295cf7e1ce6bccdc18ff8c07b6",
+ "8bd2aeb9cb7e57cb2c4b482ffc81b7afb9de27e1e3bd23c23a4453bd9ace3262",
+ "547ef835c3dac4fd97f8461a14611dc9c27745132ded8e545c1d54c72f046997",
+ "a9fb57dba1eea9bc3e660a909d838d718c397aa3b561a6f7901e0e82974856a7",
+ 1)
+ );
+
+ ecCurveDefinitions.add(
+ new ECCurveDefinition(
+ "brainpoolP384r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.11",
+ "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b412b1da197fb71123acd3a729901d1a71874700133107ec53",
+ "7bc382c63d8c150c3c72080ace05afa0c2bea28e4fb22787139165efba91f90f8aa5814a503ad4eb04a8c7dd22ce2826",
+ "04a8c7dd22ce28268b39b55416f0447c2fb77de107dcd2a62e880ea53eeb62d57cb4390295dbc9943ab78696fa504c11",
+ "1d1c64f068cf45ffa2a63a81b7c13f6b8847a3e77ef14fe3db7fcafe0cbd10e8e826e03436d646aaef87b2e247d4af1e",
+ "8abe1d7520f9c2a45cb1eb8e95cfd55262b70b29feec5864e19c054ff99129280e4646217791811142820341263c5315",
+ "8cb91e82a3386d280f5d6f7e50e641df152f7109ed5456b31f166e6cac0425a7cf3ab6af6b7fc3103b883202e9046565",
+ 1)
+ );
+
+ ecCurveDefinitions.add(
+ new ECCurveDefinition(
+ "brainpoolP512r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.13",
+ "aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca703308717d4d9b009bc66842aecda12ae6a380e62881ff2f2d82c68528aa6056583a48f3",
+ "7830a3318b603b89e2327145ac234cc594cbdd8d3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94ca",
+ "3df91610a83441caea9863bc2ded5d5aa8253aa10a2ef1c98b9ac8b57f1117a72bf2c7b9e7c1ac4d77fc94cadc083e67984050b75ebae5dd2809bd638016f723",
+ "81aee4bdd82ed9645a21322e9c4c6a9385ed9f70b5d916c1b43b62eef4d0098eff3b1f78e2d0d48d50d1687b93b97d5f7c6d5047406a5e688b352209bcb9f822",
+ "7dde385d566332ecc0eabfa9cf7822fdf209f70024a57b1aa000c55b881f8111b2dcde494a5f485e5bca4bd88a2763aed1ca2b2fa8f0540678cd1e0f3ad80892",
+ "aadd9db8dbe9c48b3fd4e6ae33c9fc07cb308db3b3c9d20ed6639cca70330870553e5c414ca92619418661197fac10471db1d381085ddaddb58796829ca90069",
+ 1)
+ );
+
}
public static String getOIDFromPublicKey(ECPublicKey ecPublicKey) {
diff --git a/src/main/java/org/apache/xml/security/keys/content/keyvalues/ECKeyValue.java b/src/main/java/org/apache/xml/security/keys/content/keyvalues/ECKeyValue.java
index 1ef4f7fb..4f124693 100644
--- a/src/main/java/org/apache/xml/security/keys/content/keyvalues/ECKeyValue.java
+++ b/src/main/java/org/apache/xml/security/keys/content/keyvalues/ECKeyValue.java
@@ -87,6 +87,45 @@ public class ECKeyValue extends Signature11ElementProxy implements KeyValueConte
1
);
+ /* Supported curve brainpoolP256r1 */
+ private static final Curve BRAINPOOLP256R1 = initializeCurve(
+ "brainpoolP256r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.7",
+ "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377",
+ "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9",
+ "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6",
+ "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262",
+ "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997",
+ "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7",
+ 1
+ );
+
+ /* Supported curve brainpoolP384r1 */
+ private static final Curve BRAINPOOLP384R1 = initializeCurve(
+ "brainpoolP384r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.11",
+ "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53",
+ "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826",
+ "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11",
+ "1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E",
+ "8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315",
+ "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565",
+ 1
+ );
+
+ /* Supported curve brainpoolP512r1 */
+ private static final Curve BRAINPOOLP512R1 = initializeCurve(
+ "brainpoolP512r1 [RFC 5639]",
+ "1.3.36.3.3.2.8.1.1.13",
+ "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3",
+ "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA",
+ "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723",
+ "81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822",
+ "7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892",
+ "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069",
+ 1
+ );
+
private static Curve initializeCurve(String name, String oid,
String sfield, String a, String b,
String x, String y, String n, int h) {
@@ -260,7 +299,13 @@ public class ECKeyValue extends Signature11ElementProxy implements KeyValueConte
match = SECP384R1;
} else if (matchCurve(params, SECP521R1)) {
match = SECP521R1;
- } else {
+ } else if (matchCurve(params, BRAINPOOLP256R1)) {
+ match = BRAINPOOLP256R1;
+ } else if (matchCurve(params, BRAINPOOLP384R1)) {
+ match = BRAINPOOLP384R1;
+ } else if (matchCurve(params, BRAINPOOLP512R1)) {
+ match = BRAINPOOLP512R1;
+ }else {
return null;
}
return match.getObjectId();
@@ -328,6 +373,12 @@ public class ECKeyValue extends Signature11ElementProxy implements KeyValueConte
return SECP384R1;
} else if (oid.equals(SECP521R1.getObjectId())) {
return SECP521R1;
+ } else if (oid.equals(BRAINPOOLP256R1.getObjectId())) {
+ return BRAINPOOLP256R1;
+ } else if (oid.equals(BRAINPOOLP384R1.getObjectId())) {
+ return BRAINPOOLP384R1;
+ } else if (oid.equals(BRAINPOOLP512R1.getObjectId())) {
+ return BRAINPOOLP512R1;
} else {
return null;
}
diff --git a/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java
new file mode 100644
index 00000000..32d0d1c1
--- /dev/null
+++ b/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherBrainpoolTest.java
@@ -0,0 +1,247 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.xml.security.test.dom.encryption;
+
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.Key;
+import java.security.KeyPair;
+import java.security.NoSuchAlgorithmException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.Security;
+import java.security.spec.AlgorithmParameterSpec;
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.NoSuchPaddingException;
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.MessageDigestAlgorithm;
+import org.apache.xml.security.c14n.Canonicalizer;
+import org.apache.xml.security.encryption.EncryptedData;
+import org.apache.xml.security.encryption.EncryptedKey;
+import org.apache.xml.security.encryption.XMLCipher;
+import org.apache.xml.security.encryption.keys.KeyInfoEnc;
+import org.apache.xml.security.encryption.params.ConcatKDFParams;
+import org.apache.xml.security.encryption.params.KeyAgreementParameters;
+import org.apache.xml.security.encryption.params.KeyDerivationParameters;
+import org.apache.xml.security.keys.KeyInfo;
+import org.apache.xml.security.parser.XMLParserException;
+import org.apache.xml.security.testutils.JDKTestUtils;
+import org.apache.xml.security.testutils.KeyTestUtils;
+import org.apache.xml.security.utils.EncryptionConstants;
+import org.apache.xml.security.utils.KeyUtils;
+import org.apache.xml.security.utils.XMLUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+
+/**
+ *
+ */
+class XMLCipherBrainpoolTest {
+
+ static {
+ org.apache.xml.security.Init.init();
+ }
+ private final String documentName;
+ private final String elementName;
+ private final String elementIndex;
+ private final String basedir;
+ private boolean haveISOPadding;
+
+ private static boolean bcInstalled;
+
+ public XMLCipherBrainpoolTest() throws Exception {
+ basedir = System.getProperty("basedir",".");
+ documentName = System.getProperty("org.apache.xml.enc.test.doc",
+ basedir + "/pom.xml");
+ elementName = System.getProperty("org.apache.xml.enc.test.elem", "project");
+ elementIndex = System.getProperty("org.apache.xml.enc.test.idx", "0");
+
+ // Determine if we have ISO 10126 Padding - needed for Bulk AES or
+ // 3DES encryption
+
+ haveISOPadding = false;
+ String algorithmId =
+ JCEMapper.translateURItoJCEID(EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128);
+
+ if (algorithmId != null) {
+ try {
+ if (Cipher.getInstance(algorithmId) != null) {
+ haveISOPadding = true;
+ }
+ } catch (NoSuchAlgorithmException nsae) {
+ //
+ } catch (NoSuchPaddingException nspe) {
+ //
+ }
+ }
+
+ //
+ // If the BouncyCastle provider is not installed, then try to load it
+ // via reflection.
+ //
+ if (Security.getProvider("BC") == null) {
+ Constructor<?> cons = null;
+ try {
+ Class<?> c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
+ cons = c.getConstructor(new Class[] {});
+ } catch (Exception e) {
+ //ignore
+ }
+ if (cons != null) {
+ Provider provider = (Provider)cons.newInstance();
+ Security.insertProviderAt(provider, 2);
+ bcInstalled = true;
+ }
+ }
+ }
+
+ @AfterAll
+ public static void cleanup() throws Exception {
+ Security.removeProvider("BC");
+ }
+
+ /**
+ * Test that encrypt and decrypt using ECDH-ES for key encryption
+ * <p/>
+ * @throws Exception Thrown when there is any problem in signing or verification
+ */
+ @ParameterizedTest
+ @EnumSource(value = KeyUtils.KeyType.class, mode = EnumSource.Mode.INCLUDE,
+ names = {"BRAINPOOLP256R1", "BRAINPOOLP384R1", "BRAINPOOLP512R1"})
+ void testAES128ElementEcdhEsKWCipher(KeyUtils.KeyType keyType) throws Exception {
+ Assumptions.assumeTrue(bcInstalled);
+ // Skip test for IBM JDK
+ Assumptions.assumeTrue(haveISOPadding,
+ "Test testAES128ElementEcdhEsKWCipher for key ["+keyType+"] was skipped as necessary algorithms not available!" );
+ Assumptions.assumeTrue(JDKTestUtils.isAlgorithmSupported(keyType.getAlgorithm().getJceName(), true),
+ "Test testAES128ElementEcdhEsKWCipher for key ["+keyType+"] was skipped as necessary algorithms not available!" );
+
+ // init parameters encrypted key object
+ String dataEncryptionAlgorithm = XMLCipher.AES_256_GCM;
+ String keyWrapAlgorithm = XMLCipher.AES_128_KeyWrap;
+ int transportKeyBitLength = 128;
+
+ // prepare the test document
+ Document d = document(); // source
+ Document ed = null;
+ Document dd = null;
+ Element e = (Element) d.getElementsByTagName(element()).item(index());
+ Element ee = null;
+ String source = null;
+ String target = null;
+
+ source = toString(d);
+
+ // Generate test recipient key pair
+ KeyPair recipientKeyPair = KeyTestUtils.generateKeyPair(keyType);
+
+ PrivateKey privRecipientKey = recipientKeyPair.getPrivate();
+ PublicKey pubRecipientKey = recipientKeyPair.getPublic();
+
+ // Generate a traffic key
+ KeyGenerator keygen = KeyGenerator.getInstance("AES");
+ keygen.init(transportKeyBitLength);
+ Key ephemeralSymmetricKey = keygen.generateKey();
+
+
+ XMLCipher cipherEncKey = XMLCipher.getInstance(keyWrapAlgorithm);
+ cipherEncKey.init(XMLCipher.WRAP_MODE, pubRecipientKey);
+ cipherEncKey.setSecureValidation(true);
+ // create key agreement parameters
+ int keyBitLen = KeyUtils.getAESKeyBitSizeForWrapAlgorithm(keyWrapAlgorithm);
+ KeyDerivationParameters keyDerivationParameter = new ConcatKDFParams(keyBitLen,
+ MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256);
+ AlgorithmParameterSpec parameterSpec = new KeyAgreementParameters(
+ KeyAgreementParameters.ActorType.ORIGINATOR,
+ EncryptionConstants.ALGO_ID_KEYAGREEMENT_ECDH_ES,
+ keyDerivationParameter);
+ // encrypt transport key with KeyAgreement
+ EncryptedKey encryptedKey = cipherEncKey.encryptKey(d, ephemeralSymmetricKey, parameterSpec, null);
+ assertEquals(1, ((KeyInfoEnc)encryptedKey.getKeyInfo()).lengthAgreementMethod());
+
+
+ // encrypt data
+ XMLCipher cipherEncData = XMLCipher.getInstance(dataEncryptionAlgorithm);
+ cipherEncData.init(XMLCipher.ENCRYPT_MODE, ephemeralSymmetricKey);
+ EncryptedData builder = cipherEncData.getEncryptedData();
+ // add encrypted key to key info in encrypted data
+ KeyInfo builderKeyInfo = builder.getKeyInfo();
+ if (builderKeyInfo == null) {
+ builderKeyInfo = new KeyInfo(d);
+ builder.setKeyInfo(builderKeyInfo);
+ }
+ builderKeyInfo.add(encryptedKey);
+
+ ed = cipherEncData.doFinal(d, e);
+
+ Files.write(Paths.get("target","test-enc-"+keyType.name()+".xml"), toString(ed).getBytes());
+
+ //decrypt
+ ee = (Element) ed.getElementsByTagName("xenc:EncryptedData").item(0);
+ XMLCipher cipherDecData = XMLCipher.getInstance(dataEncryptionAlgorithm);
+ cipherDecData.init(XMLCipher.DECRYPT_MODE, null);
+ cipherDecData.setKEK(privRecipientKey);
+ cipherDecData.setSecureValidation(true);
+ dd = cipherDecData.doFinal(ed, ee);
+
+ target = toString(dd);
+ assertEquals(source, target);
+ }
+
+ private String toString (Node n) throws Exception {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ Canonicalizer c14n = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
+
+ c14n.canonicalizeSubtree(n, baos);
+ baos.flush();
+
+ return baos.toString(StandardCharsets.UTF_8);
+ }
+
+ private Document document() throws XMLParserException, IOException {
+ File f = new File(documentName);
+ return XMLUtils.read(new FileInputStream(f), false);
+ }
+
+ private String element() {
+ return elementName;
+ }
+
+ private int index() {
+ return Integer.parseInt(elementIndex);
+ }
+
+}
diff --git a/src/test/java/org/apache/xml/security/test/dom/signature/ECBrainpoolSignatureTest.java b/src/test/java/org/apache/xml/security/test/dom/signature/ECBrainpoolSignatureTest.java
new file mode 100644
index 00000000..1d9f3bd6
--- /dev/null
+++ b/src/test/java/org/apache/xml/security/test/dom/signature/ECBrainpoolSignatureTest.java
@@ -0,0 +1,289 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.xml.security.test.dom.signature;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.lang.reflect.Constructor;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.cert.X509Certificate;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathFactory;
+import org.apache.xml.security.algorithms.SignatureAlgorithm;
+import org.apache.xml.security.c14n.Canonicalizer;
+import org.apache.xml.security.keys.KeyInfo;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.test.dom.DSNamespaceContext;
+import org.apache.xml.security.test.dom.TestUtils;
+import org.apache.xml.security.transforms.Transforms;
+import org.apache.xml.security.utils.Constants;
+import org.apache.xml.security.utils.KeyUtils;
+import org.apache.xml.security.utils.XMLUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.Assumptions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.w3c.dom.Element;
+
+
+/**
+ * Tests that creates and verifies EC signatures with brainpool curves.
+ *
+ */
+class ECBrainpoolSignatureTest {
+
+ private static final String ECDSA_JKS =
+ "src/test/resources/org/apache/xml/security/samples/input/ecbrainpool.jks";
+ private static final String ECDSA_JKS_PASSWORD = "security";
+ private static boolean bcInstalled;
+
+ public ECBrainpoolSignatureTest() throws Exception {
+ org.apache.xml.security.Init.init();
+
+ //
+ // If the BouncyCastle provider is not installed, then try to load it
+ // via reflection.
+ //
+ if (Security.getProvider("BC") == null) {
+ Constructor<?> cons = null;
+ try {
+ Class<?> c = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
+ cons = c.getConstructor(new Class[] {});
+ } catch (Exception e) {
+ //ignore
+ }
+ if (cons != null) {
+ Provider provider = (Provider)cons.newInstance();
+ Security.insertProviderAt(provider, 2);
+ bcInstalled = true;
+ }
+ }
+ }
+
+ @AfterAll
+ public static void cleanup() throws Exception {
+ Security.removeProvider("BC");
+ }
+
+ @Test
+ void testOne() throws Exception {
+ Assumptions.assumeTrue(bcInstalled);
+ //
+ // This test fails with the IBM JDK
+ //
+ if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
+ return;
+ }
+
+ KeyStore keyStore = KeyStore.getInstance("JKS");
+ keyStore.load(new FileInputStream(ECDSA_JKS), ECDSA_JKS_PASSWORD.toCharArray());
+
+ PrivateKey privateKey =
+ (PrivateKey)keyStore.getKey("ECDSA", ECDSA_JKS_PASSWORD.toCharArray());
+
+ doVerify(doSign(privateKey, (X509Certificate)keyStore.getCertificate("ECDSA"), null));
+ doVerify(doSign(privateKey, (X509Certificate)keyStore.getCertificate("ECDSA"), null));
+ }
+
+ @ParameterizedTest
+ @CsvSource({"BRAINPOOLP256R1,ECDH", "BRAINPOOLP384R1,ECDH", "BRAINPOOLP512R1,ECDH",
+ "BRAINPOOLP256R1,ECDSA", "BRAINPOOLP384R1,ECDSA", "BRAINPOOLP512R1,ECDSA"
+ })
+ void testKeyValue(KeyUtils.KeyType keyType, String algorithm) throws Exception {
+ Assumptions.assumeTrue(bcInstalled);
+ //
+ // This test fails with the IBM JDK
+ //
+ if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
+ return;
+ }
+
+ java.security.spec.ECGenParameterSpec bSpec =
+ new java.security.spec.ECGenParameterSpec(keyType.getName());
+
+ final SecureRandom secureRandom = new SecureRandom ();
+ final KeyPairGenerator bGenerator = KeyPairGenerator.getInstance (algorithm, "BC");
+ bGenerator.initialize (bSpec, secureRandom);
+
+ final KeyPair keyPair = bGenerator.genKeyPair ();
+
+ doVerify(doSign(keyPair.getPrivate(), null, keyPair.getPublic()));
+ }
+
+ private byte[] doSign(PrivateKey privateKey, X509Certificate x509, PublicKey publicKey) throws Exception {
+ org.w3c.dom.Document doc = TestUtils.newDocument();
+ doc.appendChild(doc.createComment(" Comment before "));
+ Element root = doc.createElementNS("", "RootElement");
+
+ doc.appendChild(root);
+ root.appendChild(doc.createTextNode("Some simple text\n"));
+
+ Element canonElem =
+ XMLUtils.createElementInSignatureSpace(doc, Constants._TAG_CANONICALIZATIONMETHOD);
+ canonElem.setAttributeNS(
+ null, Constants._ATT_ALGORITHM, Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS
+ );
+
+ SignatureAlgorithm signatureAlgorithm =
+ new SignatureAlgorithm(doc, XMLSignature.ALGO_ID_SIGNATURE_ECDSA_SHA1);
+ XMLSignature sig =
+ new XMLSignature(doc, null, signatureAlgorithm.getElement(), canonElem);
+
+ root.appendChild(sig.getElement());
+ doc.appendChild(doc.createComment(" Comment after "));
+ Transforms transforms = new Transforms(doc);
+ transforms.addTransform(Transforms.TRANSFORM_ENVELOPED_SIGNATURE);
+ transforms.addTransform(Transforms.TRANSFORM_C14N_WITH_COMMENTS);
+ sig.addDocument("", transforms, Constants.ALGO_ID_DIGEST_SHA1);
+
+ if (x509 != null) {
+ sig.addKeyInfo(x509);
+ } else {
+ sig.addKeyInfo(publicKey);
+ }
+ sig.sign(privateKey);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+ XMLUtils.outputDOMc14nWithComments(doc, bos);
+ return bos.toByteArray();
+ }
+
+ private void doVerify(byte[] signedXml) throws Exception {
+ try (InputStream is = new ByteArrayInputStream(signedXml)) {
+ doVerify(is);
+ }
+ }
+
+ private void doVerify(InputStream is) throws Exception {
+ org.w3c.dom.Document doc = XMLUtils.read(is, false);
+
+ XPathFactory xpf = XPathFactory.newInstance();
+ XPath xpath = xpf.newXPath();
+ xpath.setNamespaceContext(new DSNamespaceContext());
+
+ String expression = "//ds:Signature[1]";
+ Element sigElement =
+ (Element) xpath.evaluate(expression, doc, XPathConstants.NODE);
+ XMLSignature signature = new XMLSignature(sigElement, "");
+
+ signature.addResourceResolver(new XPointerResourceResolver(sigElement));
+
+ KeyInfo ki = signature.getKeyInfo();
+ if (ki == null) {
+ throw new RuntimeException("No keyinfo");
+ }
+ X509Certificate cert = signature.getKeyInfo().getX509Certificate();
+ if (cert != null) {
+ assertTrue(signature.checkSignatureValue(cert));
+ } else {
+ assertTrue(signature.checkSignatureValue(signature.getKeyInfo().getPublicKey()));
+ }
+ }
+
+
+ /**
+ * DO NOT DELETE THIS COMMENTED OUT METHOD!
+ *
+ * The reason this method is commented out is to avoid introducing explicit
+ * BouncyCastle dependencies.
+ *
+ * Create an X.509 Certificate and associated private key using the Elliptic Curve
+ * DSA algorithm, and store in a KeyStore. This method was used to generate the
+ * keystore used for this test
+ * ("src/test/resources/org/apache/xml/security/samples/input/ecbrainpool.jks").
+ *
+ private static void setUpKeyAndCertificate() throws Exception {
+ final ECNamedCurveParameterSpec ecGenParameterSpec = ECNamedCurveTable.getParameterSpec (KeyType.BRAINPOOLP256R1.getName());
+
+ java.security.KeyPairGenerator kpg =
+ java.security.KeyPairGenerator.getInstance("ECDH", "BC");
+
+ kpg.initialize(ecGenParameterSpec, new java.security.SecureRandom());
+
+ java.security.KeyPair kp = kpg.generateKeyPair();
+
+ org.bouncycastle.x509.X509V3CertificateGenerator certGen =
+ new org.bouncycastle.x509.X509V3CertificateGenerator();
+
+ long now = System.currentTimeMillis();
+ certGen.setSerialNumber(java.math.BigInteger.valueOf(now));
+
+ org.bouncycastle.jce.X509Principal subject =
+ new org.bouncycastle.jce.X509Principal(
+ "CN=XML ECDSA Signature Test,DC=apache,DC=org"
+ );
+ certGen.setIssuerDN(subject);
+ certGen.setSubjectDN(subject);
+
+ java.util.Date from_date = new java.util.Date(now);
+ certGen.setNotBefore(from_date);
+ java.util.Calendar cal = new java.util.GregorianCalendar();
+ cal.setTime(from_date);
+ cal.add(java.util.Calendar.YEAR, 4);
+ java.util.Date to_date = cal.getTime();
+ certGen.setNotAfter(to_date);
+
+ certGen.setPublicKey(kp.getPublic());
+ certGen.setSignatureAlgorithm("SHA1withECDSA");
+ certGen.addExtension(
+ org.bouncycastle.asn1.x509.X509Extensions.BasicConstraints,
+ true,
+ new org.bouncycastle.asn1.x509.BasicConstraints(false)
+ );
+ certGen.addExtension(
+ org.bouncycastle.asn1.x509.X509Extensions.KeyUsage,
+ true,
+ new org.bouncycastle.asn1.x509.KeyUsage(
+ org.bouncycastle.asn1.x509.KeyUsage.digitalSignature |
+ org.bouncycastle.asn1.x509.KeyUsage.keyEncipherment |
+ org.bouncycastle.asn1.x509.KeyUsage.keyCertSign |
+ org.bouncycastle.asn1.x509.KeyUsage.cRLSign
+ )
+ );
+
+ X509Certificate x509 = certGen.generateX509Certificate(kp.getPrivate());
+
+ KeyStore keyStore = KeyStore.getInstance("JKS");
+ keyStore.load(null, ECDSA_JKS_PASSWORD.toCharArray());
+ keyStore.setKeyEntry(
+ "ECDSA", kp.getPrivate(),
+ ECDSA_JKS_PASSWORD.toCharArray(), new java.security.cert.Certificate[]{x509}
+ );
+ keyStore.store(
+ new java.io.FileOutputStream(ECDSA_JKS), ECDSA_JKS_PASSWORD.toCharArray()
+ );
+
+ }
+*/
+
+}
diff --git a/src/test/resources/org/apache/xml/security/samples/input/ecbrainpool.jks b/src/test/resources/org/apache/xml/security/samples/input/ecbrainpool.jks
new file mode 100644
index 00000000..7c51cf57
Binary files /dev/null and b/src/test/resources/org/apache/xml/security/samples/input/ecbrainpool.jks differ