Re: [PR] Add ML-KEM (FIPS 203) post-quantum key transpor t support [santuario-xml-security-java]

github-advanced-security[bot] (via GitHub) <[email protected]>
Newsgroups gmane.text.xml.security.devel
Message-ID <PR_kwDOD7oF988AAAABAj2c-g-7a62c60c-c7c0-48e4-9878-b4bc76172d9a@gitbox.apache.org>
github-advanced-security[bot] commented on code in PR #652:
URL: https://github.com/apache/santuario-xml-security-java/pull/652#discussion_r3830956377


##########
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/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/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]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.