Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit af62090

Browse files
committed
Remove unneeded reflection code from XMLCipherUtils
git-svn-id: https://svn.apache.org/repos/asf/santuario/xml-security-java/trunk@1874014 13f79535-47bb-0310-9956-ffa450edef68
1 parent 92f67cd commit af62090

4 files changed

Lines changed: 12 additions & 24 deletions

File tree

src/main/java/org/apache/xml/security/encryption/XMLCipher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,7 @@ private EncryptedData encryptData(
12261226
* specified algorithm
12271227
*/
12281228
private AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv) {
1229-
return XMLCipherUtil.constructBlockCipherParameters(algorithm, iv, this.getClass());
1229+
return XMLCipherUtil.constructBlockCipherParameters(algorithm, iv);
12301230
}
12311231

12321232
/**

src/main/java/org/apache/xml/security/encryption/XMLCipherUtil.java

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import java.security.PrivilegedAction;
2323
import java.security.spec.AlgorithmParameterSpec;
2424

25+
import javax.crypto.spec.GCMParameterSpec;
2526
import javax.crypto.spec.IvParameterSpec;
2627

27-
import org.apache.xml.security.utils.ClassLoaderUtils;
2828
import org.apache.xml.security.utils.EncryptionConstants;
2929

3030
public final class XMLCipherUtil {
@@ -45,27 +45,27 @@ public final class XMLCipherUtil {
4545
* @return the newly constructed AlgorithmParameterSpec instance, appropriate for the
4646
* specified algorithm
4747
*/
48-
public static AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv, Class<?> callingClass) {
48+
public static AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv) {
4949
if (EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128_GCM.equals(algorithm)
5050
|| EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192_GCM.equals(algorithm)
5151
|| EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256_GCM.equals(algorithm)) {
52-
return constructBlockCipherParametersForGCMAlgorithm(algorithm, iv, callingClass);
52+
return constructBlockCipherParametersForGCMAlgorithm(algorithm, iv);
5353
} else {
5454
LOG.debug("Saw non-AES-GCM mode block cipher, returning IvParameterSpec: {}", algorithm);
5555
return new IvParameterSpec(iv);
5656
}
5757
}
5858

59-
public static AlgorithmParameterSpec constructBlockCipherParameters(boolean gcmAlgorithm, byte[] iv, Class<?> callingClass) {
59+
public static AlgorithmParameterSpec constructBlockCipherParameters(boolean gcmAlgorithm, byte[] iv) {
6060
if (gcmAlgorithm) {
61-
return constructBlockCipherParametersForGCMAlgorithm("AES/GCM/NoPadding", iv, callingClass);
61+
return constructBlockCipherParametersForGCMAlgorithm("AES/GCM/NoPadding", iv);
6262
} else {
6363
LOG.debug("Saw non-AES-GCM mode block cipher, returning IvParameterSpec");
6464
return new IvParameterSpec(iv);
6565
}
6666
}
6767

68-
private static AlgorithmParameterSpec constructBlockCipherParametersForGCMAlgorithm(String algorithm, byte[] iv, Class<?> callingClass) {
68+
private static AlgorithmParameterSpec constructBlockCipherParametersForGCMAlgorithm(String algorithm, byte[] iv) {
6969
if (gcmUseIvParameterSpec) {
7070
// This override allows to support Java 1.7+ with (usually older versions of) third-party security
7171
// providers which support or even require GCM via IvParameterSpec rather than GCMParameterSpec,
@@ -76,20 +76,8 @@ private static AlgorithmParameterSpec constructBlockCipherParametersForGCMAlgori
7676

7777
LOG.debug("Saw AES-GCM block cipher, attempting to create GCMParameterSpec: {}", algorithm);
7878

79-
try {
80-
// This class only added in Java 1.7. So load reflectively until Santuario starts targeting a minimum of Java 1.7.
81-
Class<?> gcmSpecClass = ClassLoaderUtils.loadClass("javax.crypto.spec.GCMParameterSpec", callingClass);
82-
83-
// XML Encryption 1.1 mandates a 128-bit Authentication Tag for AES GCM modes.
84-
AlgorithmParameterSpec gcmSpec = (AlgorithmParameterSpec) gcmSpecClass.getConstructor(int.class, byte[].class)
85-
.newInstance(128, iv);
86-
LOG.debug("Successfully created GCMParameterSpec");
87-
return gcmSpec;
88-
} catch (Exception e) {
89-
// This handles the case of Java < 1.7 with a third-party security provider that
90-
// supports GCM mode using only an IvParameterSpec, such as BouncyCastle.
91-
LOG.debug("Failed to create GCMParameterSpec, falling back to returning IvParameterSpec", e);
92-
return new IvParameterSpec(iv);
93-
}
79+
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, iv);
80+
LOG.debug("Successfully created GCMParameterSpec");
81+
return gcmSpec;
9482
}
9583
}

src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void init(OutputProcessorChain outputProcessorChain) throws XMLSecurityEx
170170
int ivLen = JCEMapper.getIVLengthFromURI(encryptionSymAlgorithm) / 8;
171171
byte[] iv = XMLSecurityConstants.generateBytes(ivLen);
172172
AlgorithmParameterSpec parameterSpec =
173-
XMLCipherUtil.constructBlockCipherParameters(encryptionSymAlgorithm, iv, this.getClass());
173+
XMLCipherUtil.constructBlockCipherParameters(encryptionSymAlgorithm, iv);
174174
symmetricCipher.init(Cipher.ENCRYPT_MODE, encryptionPartDef.getSymmetricKey(), parameterSpec);
175175

176176
characterEventGeneratorOutputStream = new CharacterEventGeneratorOutputStream();

src/main/java/org/apache/xml/security/stax/impl/util/IVSplittingOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public boolean isIVComplete() {
6464
}
6565

6666
private void initializeCipher() throws IOException {
67-
AlgorithmParameterSpec iv = XMLCipherUtil.constructBlockCipherParameters(cipher.getAlgorithm().toUpperCase().contains("GCM"), this.getIv(), this.getClass());
67+
AlgorithmParameterSpec iv = XMLCipherUtil.constructBlockCipherParameters(cipher.getAlgorithm().toUpperCase().contains("GCM"), this.getIv());
6868
try {
6969
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
7070
} catch (InvalidKeyException e) {

0 commit comments

Comments
 (0)