Re: [PR] Add PQC (ML-DSA and ML-KEM) support for XML Dig ital Signature and Enc… [santuario-xml-security-java]
github-advanced-security[bot] (via GitHub) <[email protected]>
| Newsgroups | gmane.text.xml.security.devel |
|---|---|
| Message-ID | <PR_kwDOD7oF9878HQFs-7b4d81bd-44f0-4783-b893-9d6c0137fd35@gitbox.apache.org> |
github-advanced-security[bot] commented on code in PR #645:
URL: https://github.com/apache/santuario-xml-security-java/pull/645#discussion_r3736985198
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, providerId);
Review Comment:
## CodeQL / Use of a broken or risky cryptographic algorithm
Cryptographic algorithm [DESede/CBC/ISO10126Padding](1) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
Cryptographic algorithm [DESedeWrap](2) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1257)
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
Review Comment:
## CodeQL / Use of a broken or risky cryptographic algorithm
Cryptographic algorithm [DESede/CBC/ISO10126Padding](1) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
Cryptographic algorithm [DESedeWrap](2) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1256)
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, providerId);
+ }
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, provider);
Review Comment:
## CodeQL / Use of a potentially broken or risky cryptographic algorithm
Cryptographic algorithm [MD5](1) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RIPEMD160](2) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-1](3) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-224](4) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [WHIRLPOOL](5) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA3-224](6) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA1withDSA](7) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RSASSA-PSS](8) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-44](9) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-65](10) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-87](11) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacMD5](12) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HMACRIPEMD160](13) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacSHA224](14) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEED/CBC/ISO10126Padding](15) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](16) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](17) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](18) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-512](19) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-768](20) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-1024](21) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](22) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](23) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](24) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](25) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](26) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](27) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEEDWrap](28) may not be secure. Consider using a different algorithm.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1263)
##########
src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLEncryptedKeyInputHandler.java:
##########
@@ -260,6 +270,100 @@
return this.decryptedKey;
}
}
+
+ /**
+ * Decapsulate/unwrap a CEK transported via KEM-based (Generic Hybrid Cipher) key
+ * transport, per https://www.w3.org/TR/xmlsec-generic-hybrid/ (see SANTUARIO-633).
+ * The {@code ghc:GenericHybridCipherMethod} element has no JAXB binding, so it is
+ * read back as a raw DOM {@link Element} from the wildcard EncryptionMethod content
+ * and parsed with the same DOM classes ({@link KeyDerivationMethodImpl},
+ * {@link XMLCipherUtil}) used by the DOM {@code XMLCipher} API for the same structure.
+ */
+ private byte[] getGenericHybridSecret(InboundSecurityToken wrappingSecurityToken, String correlationID,
+ XMLSecurityConstants.AlgorithmUsage algorithmUsage,
+ String symmetricAlgorithmURI) throws XMLSecurityException {
+ try {
+ Element genericHybridCipherMethodElement = findAnyElement(
+ encryptedKeyType.getEncryptionMethod().getContent(),
+ XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod);
+ if (genericHybridCipherMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ Element keyEncapsulationMethodElement = (Element) genericHybridCipherMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_GHC, "KeyEncapsulationMethod").item(0);
+ Element dataEncapsulationMethodElement = (Element) genericHybridCipherMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_GHC, "DataEncapsulationMethod").item(0);
+ if (keyEncapsulationMethodElement == null || dataEncapsulationMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ String kemAlgorithm = keyEncapsulationMethodElement.getAttributeNS(null, "Algorithm");
+ String dataEncapsulationAlgorithm = dataEncapsulationMethodElement.getAttributeNS(null, "Algorithm");
+
+ Element keyDerivationMethodElement = (Element) keyEncapsulationMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_XMLENC11, "KeyDerivationMethod").item(0);
+ if (keyDerivationMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ int wrapKeyBitLength = KeyUtils.getAESKeyBitSizeForWrapAlgorithm(dataEncapsulationAlgorithm);
+ KeyDerivationMethodImpl keyDerivationMethod = new KeyDerivationMethodImpl(keyDerivationMethodElement, null);
+ KeyDerivationParameters kdp = XMLCipherUtil.constructKeyDerivationParameter(keyDerivationMethod, wrapKeyBitLength);
+
+ Key wrapKeyToken = wrappingSecurityToken.getSecretKey(kemAlgorithm, algorithmUsage, correlationID);
+ if (!(wrapKeyToken instanceof PrivateKey)) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ if (encryptedKeyType.getCipherData() == null
+ || encryptedKeyType.getCipherData().getCipherValue() == null
+ || encryptedKeyType.getCipherData().getCipherValue().getContent() == null
+ || encryptedKeyType.getCipherData().getCipherValue().getContent().isEmpty()) {
+ throw new XMLSecurityException("stax.encryption.noCipherValue");
+ }
+
+ byte[] encryptedBytes = getEncryptedBytes(encryptedKeyType.getCipherData().getCipherValue());
+ byte[] sha1Bytes = generateDigest(encryptedBytes);
+ String sha1Identifier = XMLUtils.encodeToString(sha1Bytes);
+ super.setSha1Identifier(sha1Identifier);
+
+ try {
+ KeyUtils.KemDecapsulation kemResult = KeyUtils.kemDecapsulate(
+ (PrivateKey) wrapKeyToken, kemAlgorithm, encryptedBytes, kdp);
+ String jceWrapId = JCEMapper.translateURItoJCEID(dataEncapsulationAlgorithm);
+ Cipher cipher = Cipher.getInstance(jceWrapId);
Review Comment:
## CodeQL / Use of a broken or risky cryptographic algorithm
Cryptographic algorithm [DESede/CBC/ISO10126Padding](1) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
Cryptographic algorithm [DESedeWrap](2) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1259)
##########
src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java:
##########
@@ -265,10 +292,111 @@
createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherValue);
createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherData);
+ }
- createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptedKey);
+ /**
+ * Writes the EncryptionMethod/KeyInfo/CipherData for KEM-based (Generic Hybrid Cipher)
+ * key transport, per https://www.w3.org/TR/xmlsec-generic-hybrid/ section 6.1
+ * "Key Transport Example" (see SANTUARIO-633).
+ */
+ private void createGenericHybridCipherKeyInfoStructure(
+ OutputProcessorChain outputProcessorChain, PublicKey pubKey)
+ throws XMLStreamException, XMLSecurityException {
- createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_dsig_KeyInfo);
+ String kemAlgorithm = getSecurityProperties().getEncryptionKeyEncapsulationAlgorithm();
+ String dataEncapsulationAlgorithm = getSecurityProperties().getEncryptionDataEncapsulationAlgorithm();
+ if (kemAlgorithm == null || dataEncapsulationAlgorithm == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+ String hmacAlgorithm = getSecurityProperties().getEncryptionKeyEncapsulationHmacAlgorithm();
+ if (hmacAlgorithm == null) {
+ hmacAlgorithm = XMLSecurityConstants.NS_XMLDSIG_HMACSHA256;
+ }
+
+ int wrapKeyBitLength = KeyUtils.getAESKeyBitSizeForWrapAlgorithm(dataEncapsulationAlgorithm);
+ KeyDerivationParameters kdfParams = HKDFParams.createBuilder(wrapKeyBitLength, hmacAlgorithm).build();
+
+ // EncryptionMethod/ghc:GenericHybridCipherMethod/ghc:KeyEncapsulationMethod/...
+ List<XMLSecAttribute> attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm,
+ EncryptionConstants.ALGO_ID_KEYTRANSPORT_GENERIC_HYBRID));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptionMethod, false, attributes);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod, true, null);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, kemAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyEncapsulationMethod, true, attributes);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, XMLSecurityConstants.NS_HKDF));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc11_KeyDerivationMethod, true, attributes);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_HKDFParams, true, null);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, hmacAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_PRF, true, attributes);
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_PRF);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_KeyLength, false, null);
+ createCharactersAndOutputAsEvent(outputProcessorChain, String.valueOf(wrapKeyBitLength / 8));
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_KeyLength);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_HKDFParams);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc11_KeyDerivationMethod);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyLen, false, null);
+ createCharactersAndOutputAsEvent(outputProcessorChain, String.valueOf(wrapKeyBitLength / 8));
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyLen);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyEncapsulationMethod);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, dataEncapsulationAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_DataEncapsulationMethod, true, attributes);
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_DataEncapsulationMethod);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptionMethod);
+
+ createKeyInfoStructureForEncryptedKey(outputProcessorChain, keyWrappingToken);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherData, false, null);
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherValue, false, null);
+
+ String tokenId = outputProcessorChain.getSecurityContext().get(
+ XMLSecurityConstants.PROP_USE_THIS_TOKEN_ID_FOR_ENCRYPTION);
+ SecurityTokenProvider<OutboundSecurityToken> securityTokenProvider =
+ outputProcessorChain.getSecurityContext().getSecurityTokenProvider(tokenId);
+ final OutboundSecurityToken securityToken = securityTokenProvider.getSecurityToken();
+ Key sessionKey = securityToken.getSecretKey(getSecurityProperties().getEncryptionSymAlgorithm());
+
+ // Encapsulate a shared secret to the recipient's KEM public key, derive the
+ // AES key-wrap key from it, and wrap the CEK with that key. CipherValue holds
+ // the concatenation of the KEM encapsulation (C0) and the wrapped CEK (C1).
+ KeyUtils.KemEncapsulation kemResult = KeyUtils.kemEncapsulate(pubKey, kemAlgorithm, kdfParams);
+ try {
+ String jceWrapId = JCEMapper.translateURItoJCEID(dataEncapsulationAlgorithm);
+ Cipher wrapCipher = Cipher.getInstance(jceWrapId);
Review Comment:
## CodeQL / Use of a potentially broken or risky cryptographic algorithm
Cryptographic algorithm [MD5](1) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RIPEMD160](2) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-1](3) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-224](4) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [WHIRLPOOL](5) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA3-224](6) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA1withDSA](7) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RSASSA-PSS](8) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-44](9) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-65](10) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-87](11) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacMD5](12) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HMACRIPEMD160](13) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacSHA224](14) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEED/CBC/ISO10126Padding](15) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](16) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](17) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](18) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-512](19) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-768](20) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-1024](21) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](22) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](23) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](24) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](25) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](26) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](27) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEEDWrap](28) may not be secure. Consider using a different algorithm.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1265)
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
Review Comment:
## CodeQL / Use of a potentially broken or risky cryptographic algorithm
Cryptographic algorithm [MD5](1) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RIPEMD160](2) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-1](3) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-224](4) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [WHIRLPOOL](5) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA3-224](6) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA1withDSA](7) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RSASSA-PSS](8) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-44](9) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-65](10) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-87](11) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacMD5](12) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HMACRIPEMD160](13) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacSHA224](14) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEED/CBC/ISO10126Padding](15) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](16) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](17) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](18) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-512](19) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-768](20) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-1024](21) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](22) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](23) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](24) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](25) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](26) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](27) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEEDWrap](28) may not be secure. Consider using a different algorithm.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1261)
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, providerId);
+ }
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, provider);
Review Comment:
## CodeQL / Use of a broken or risky cryptographic algorithm
Cryptographic algorithm [DESede/CBC/ISO10126Padding](1) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
Cryptographic algorithm [DESedeWrap](2) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1258)
##########
src/main/java/org/apache/xml/security/algorithms/implementations/SignatureMLDSA.java:
##########
@@ -0,0 +1,208 @@
+/**
+ * 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.algorithms.implementations;
+
+import java.lang.System.Logger;
+import java.lang.System.Logger.Level;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.Key;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Signature;
+import java.security.SignatureException;
+import java.security.spec.AlgorithmParameterSpec;
+
+import org.apache.xml.security.algorithms.JCEMapper;
+import org.apache.xml.security.algorithms.SignatureAlgorithmSpi;
+import org.apache.xml.security.signature.XMLSignature;
+import org.apache.xml.security.signature.XMLSignatureException;
+import org.apache.xml.security.utils.XMLUtils;
+
+/**
+ * ML-DSA (FIPS 204) signature algorithm implementation for XML-Dsig.
+ * Supports ML-DSA-44 (NIST security level 2), ML-DSA-65 (level 3),
+ * and ML-DSA-87 (level 5). Requires BouncyCastle 1.81+ as the JCA provider.
+ */
+public abstract class SignatureMLDSA extends SignatureAlgorithmSpi {
+
+ private static final Logger LOG = System.getLogger(SignatureMLDSA.class.getName());
+
+ private final Signature signatureAlgorithm;
+
+ public SignatureMLDSA() throws XMLSignatureException {
+ this(null);
+ }
+
+ public SignatureMLDSA(Provider provider) throws XMLSignatureException {
+ String algorithmID = JCEMapper.translateURItoJCEID(this.engineGetURI());
+ LOG.log(Level.DEBUG, "Created SignatureMLDSA using {0}", algorithmID);
+
+ try {
+ if (provider == null) {
+ String providerId = JCEMapper.getProviderId();
+ if (providerId == null) {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID);
+ } else {
+ this.signatureAlgorithm = Signature.getInstance(algorithmID, providerId);
Review Comment:
## CodeQL / Use of a potentially broken or risky cryptographic algorithm
Cryptographic algorithm [MD5](1) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RIPEMD160](2) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-1](3) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-224](4) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [WHIRLPOOL](5) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA3-224](6) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA1withDSA](7) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RSASSA-PSS](8) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-44](9) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-65](10) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-87](11) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacMD5](12) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HMACRIPEMD160](13) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacSHA224](14) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEED/CBC/ISO10126Padding](15) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](16) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](17) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](18) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-512](19) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-768](20) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-1024](21) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](22) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](23) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](24) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](25) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](26) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](27) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEEDWrap](28) may not be secure. Consider using a different algorithm.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1262)
##########
src/main/java/org/apache/xml/security/stax/impl/processor/input/XMLEncryptedKeyInputHandler.java:
##########
@@ -260,6 +270,100 @@
return this.decryptedKey;
}
}
+
+ /**
+ * Decapsulate/unwrap a CEK transported via KEM-based (Generic Hybrid Cipher) key
+ * transport, per https://www.w3.org/TR/xmlsec-generic-hybrid/ (see SANTUARIO-633).
+ * The {@code ghc:GenericHybridCipherMethod} element has no JAXB binding, so it is
+ * read back as a raw DOM {@link Element} from the wildcard EncryptionMethod content
+ * and parsed with the same DOM classes ({@link KeyDerivationMethodImpl},
+ * {@link XMLCipherUtil}) used by the DOM {@code XMLCipher} API for the same structure.
+ */
+ private byte[] getGenericHybridSecret(InboundSecurityToken wrappingSecurityToken, String correlationID,
+ XMLSecurityConstants.AlgorithmUsage algorithmUsage,
+ String symmetricAlgorithmURI) throws XMLSecurityException {
+ try {
+ Element genericHybridCipherMethodElement = findAnyElement(
+ encryptedKeyType.getEncryptionMethod().getContent(),
+ XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod);
+ if (genericHybridCipherMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ Element keyEncapsulationMethodElement = (Element) genericHybridCipherMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_GHC, "KeyEncapsulationMethod").item(0);
+ Element dataEncapsulationMethodElement = (Element) genericHybridCipherMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_GHC, "DataEncapsulationMethod").item(0);
+ if (keyEncapsulationMethodElement == null || dataEncapsulationMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ String kemAlgorithm = keyEncapsulationMethodElement.getAttributeNS(null, "Algorithm");
+ String dataEncapsulationAlgorithm = dataEncapsulationMethodElement.getAttributeNS(null, "Algorithm");
+
+ Element keyDerivationMethodElement = (Element) keyEncapsulationMethodElement
+ .getElementsByTagNameNS(XMLSecurityConstants.NS_XMLENC11, "KeyDerivationMethod").item(0);
+ if (keyDerivationMethodElement == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ int wrapKeyBitLength = KeyUtils.getAESKeyBitSizeForWrapAlgorithm(dataEncapsulationAlgorithm);
+ KeyDerivationMethodImpl keyDerivationMethod = new KeyDerivationMethodImpl(keyDerivationMethodElement, null);
+ KeyDerivationParameters kdp = XMLCipherUtil.constructKeyDerivationParameter(keyDerivationMethod, wrapKeyBitLength);
+
+ Key wrapKeyToken = wrappingSecurityToken.getSecretKey(kemAlgorithm, algorithmUsage, correlationID);
+ if (!(wrapKeyToken instanceof PrivateKey)) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+
+ if (encryptedKeyType.getCipherData() == null
+ || encryptedKeyType.getCipherData().getCipherValue() == null
+ || encryptedKeyType.getCipherData().getCipherValue().getContent() == null
+ || encryptedKeyType.getCipherData().getCipherValue().getContent().isEmpty()) {
+ throw new XMLSecurityException("stax.encryption.noCipherValue");
+ }
+
+ byte[] encryptedBytes = getEncryptedBytes(encryptedKeyType.getCipherData().getCipherValue());
+ byte[] sha1Bytes = generateDigest(encryptedBytes);
+ String sha1Identifier = XMLUtils.encodeToString(sha1Bytes);
+ super.setSha1Identifier(sha1Identifier);
+
+ try {
+ KeyUtils.KemDecapsulation kemResult = KeyUtils.kemDecapsulate(
+ (PrivateKey) wrapKeyToken, kemAlgorithm, encryptedBytes, kdp);
+ String jceWrapId = JCEMapper.translateURItoJCEID(dataEncapsulationAlgorithm);
+ Cipher cipher = Cipher.getInstance(jceWrapId);
Review Comment:
## CodeQL / Use of a potentially broken or risky cryptographic algorithm
Cryptographic algorithm [MD5](1) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RIPEMD160](2) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-1](3) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA-224](4) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [WHIRLPOOL](5) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA3-224](6) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SHA1withDSA](7) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [RSASSA-PSS](8) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-44](9) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-65](10) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-DSA-87](11) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacMD5](12) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HMACRIPEMD160](13) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [HmacSHA224](14) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEED/CBC/ISO10126Padding](15) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](16) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](17) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [Camellia/CBC/ISO10126Padding](18) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-512](19) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-768](20) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [ML-KEM-1024](21) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](22) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](23) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [AESWrap](24) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](25) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](26) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [CamelliaWrap](27) may not be secure. Consider using a different algorithm.
Cryptographic algorithm [SEEDWrap](28) may not be secure. Consider using a different algorithm.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1264)
##########
src/main/java/org/apache/xml/security/stax/impl/processor/output/XMLEncryptOutputProcessor.java:
##########
@@ -265,10 +292,111 @@
createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherValue);
createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherData);
+ }
- createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptedKey);
+ /**
+ * Writes the EncryptionMethod/KeyInfo/CipherData for KEM-based (Generic Hybrid Cipher)
+ * key transport, per https://www.w3.org/TR/xmlsec-generic-hybrid/ section 6.1
+ * "Key Transport Example" (see SANTUARIO-633).
+ */
+ private void createGenericHybridCipherKeyInfoStructure(
+ OutputProcessorChain outputProcessorChain, PublicKey pubKey)
+ throws XMLStreamException, XMLSecurityException {
- createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_dsig_KeyInfo);
+ String kemAlgorithm = getSecurityProperties().getEncryptionKeyEncapsulationAlgorithm();
+ String dataEncapsulationAlgorithm = getSecurityProperties().getEncryptionDataEncapsulationAlgorithm();
+ if (kemAlgorithm == null || dataEncapsulationAlgorithm == null) {
+ throw new XMLSecurityException("stax.unsupportedKeyTransp");
+ }
+ String hmacAlgorithm = getSecurityProperties().getEncryptionKeyEncapsulationHmacAlgorithm();
+ if (hmacAlgorithm == null) {
+ hmacAlgorithm = XMLSecurityConstants.NS_XMLDSIG_HMACSHA256;
+ }
+
+ int wrapKeyBitLength = KeyUtils.getAESKeyBitSizeForWrapAlgorithm(dataEncapsulationAlgorithm);
+ KeyDerivationParameters kdfParams = HKDFParams.createBuilder(wrapKeyBitLength, hmacAlgorithm).build();
+
+ // EncryptionMethod/ghc:GenericHybridCipherMethod/ghc:KeyEncapsulationMethod/...
+ List<XMLSecAttribute> attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm,
+ EncryptionConstants.ALGO_ID_KEYTRANSPORT_GENERIC_HYBRID));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptionMethod, false, attributes);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod, true, null);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, kemAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyEncapsulationMethod, true, attributes);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, XMLSecurityConstants.NS_HKDF));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc11_KeyDerivationMethod, true, attributes);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_HKDFParams, true, null);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, hmacAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_PRF, true, attributes);
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_PRF);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_KeyLength, false, null);
+ createCharactersAndOutputAsEvent(outputProcessorChain, String.valueOf(wrapKeyBitLength / 8));
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_KeyLength);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_hkdf_HKDFParams);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc11_KeyDerivationMethod);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyLen, false, null);
+ createCharactersAndOutputAsEvent(outputProcessorChain, String.valueOf(wrapKeyBitLength / 8));
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyLen);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_KeyEncapsulationMethod);
+
+ attributes = new ArrayList<>(1);
+ attributes.add(createAttribute(XMLSecurityConstants.ATT_NULL_Algorithm, dataEncapsulationAlgorithm));
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_DataEncapsulationMethod, true, attributes);
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_DataEncapsulationMethod);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_ghc_GenericHybridCipherMethod);
+
+ createEndElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_EncryptionMethod);
+
+ createKeyInfoStructureForEncryptedKey(outputProcessorChain, keyWrappingToken);
+
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherData, false, null);
+ createStartElementAndOutputAsEvent(outputProcessorChain, XMLSecurityConstants.TAG_xenc_CipherValue, false, null);
+
+ String tokenId = outputProcessorChain.getSecurityContext().get(
+ XMLSecurityConstants.PROP_USE_THIS_TOKEN_ID_FOR_ENCRYPTION);
+ SecurityTokenProvider<OutboundSecurityToken> securityTokenProvider =
+ outputProcessorChain.getSecurityContext().getSecurityTokenProvider(tokenId);
+ final OutboundSecurityToken securityToken = securityTokenProvider.getSecurityToken();
+ Key sessionKey = securityToken.getSecretKey(getSecurityProperties().getEncryptionSymAlgorithm());
+
+ // Encapsulate a shared secret to the recipient's KEM public key, derive the
+ // AES key-wrap key from it, and wrap the CEK with that key. CipherValue holds
+ // the concatenation of the KEM encapsulation (C0) and the wrapped CEK (C1).
+ KeyUtils.KemEncapsulation kemResult = KeyUtils.kemEncapsulate(pubKey, kemAlgorithm, kdfParams);
+ try {
+ String jceWrapId = JCEMapper.translateURItoJCEID(dataEncapsulationAlgorithm);
+ Cipher wrapCipher = Cipher.getInstance(jceWrapId);
Review Comment:
## CodeQL / Use of a broken or risky cryptographic algorithm
Cryptographic algorithm [DESede/CBC/ISO10126Padding](1) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
Cryptographic algorithm [DESedeWrap](2) is insecure. It has a short key length of 56 bits, making it vulnerable to brute-force attacks. Consider using AES instead.
[Show more details](https://github.com/apache/santuario-xml-security-java/security/code-scanning/1260)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]