Skip to content

Commit 8b1ae5e

Browse files
author
Carter Kozak
committed
hadoop-crypto no longer depends on commons-crypto or openssl
1 parent f24f1ca commit 8b1ae5e

File tree

13 files changed

+20
-376
lines changed

13 files changed

+20
-376
lines changed

README.md

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -187,44 +187,3 @@ Hadoop Configuration Properties
187187
License
188188
-------
189189
This repository is made available under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).
190-
191-
192-
FAQ
193-
---
194-
195-
## log.warn lines from `CryptoStreamFactory`
196-
197-
`WARN: Unable to initialize cipher with OpenSSL, falling back to JCE implementation`
198-
199-
'Falling back to the JCE implementation' results in slower cipher performance than native OpenSSL. Resolve this by installing a compatible OpenSSL and symlinking it to the correct location, `/usr/lib/libcrypto.so`. (OpenSSL 1.0 and 1.1 are currently supported)
200-
201-
_Note: to support OpenSSL 1.1, we use releases from the [Palantir fork of commons-crypto](https://github.com/palantir/commons-crypto/releases) as support has been added to the mainline Apache repo, but no release made [since 2016](https://github.com/apache/commons-crypto/releases)._
202-
203-
```
204-
Exception in thread "main" java.io.IOException: java.security.GeneralSecurityException: CryptoCipher {org.apache.commons.crypto.cipher.OpenSslCipher} is not available or transformation AES/CTR/NoPadding is not supported.
205-
at org.apache.commons.crypto.utils.Utils.getCipherInstance(Utils.java:130)
206-
at ApacheCommonsCryptoLoad.main(ApacheCommonsCryptoLoad.java:10)
207-
Caused by: java.security.GeneralSecurityException: CryptoCipher {org.apache.commons.crypto.cipher.OpenSslCipher} is not available or transformation AES/CTR/NoPadding is not supported.
208-
at org.apache.commons.crypto.cipher.CryptoCipherFactory.getCryptoCipher(CryptoCipherFactory.java:176)
209-
at org.apache.commons.crypto.utils.Utils.getCipherInstance(Utils.java:128)
210-
... 1 more
211-
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
212-
at org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:90)
213-
at org.apache.commons.crypto.cipher.CryptoCipherFactory.getCryptoCipher(CryptoCipherFactory.java:160)
214-
... 2 more
215-
Caused by: java.lang.reflect.InvocationTargetException
216-
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
217-
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
218-
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
219-
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
220-
at org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:88)
221-
... 3 more
222-
Caused by: java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: EVP_CIPHER_CTX_cleanup
223-
at org.apache.commons.crypto.cipher.OpenSslCipher.<init>(OpenSslCipher.java:59)
224-
... 8 more
225-
Caused by: java.lang.UnsatisfiedLinkError: EVP_CIPHER_CTX_cleanup
226-
at org.apache.commons.crypto.cipher.OpenSslNative.initIDs(Native Method)
227-
at org.apache.commons.crypto.cipher.OpenSsl.<clinit>(OpenSsl.java:95)
228-
at org.apache.commons.crypto.cipher.OpenSslCipher.<init>(OpenSslCipher.java:57)
229-
... 8 more
230-
```

crypto-core/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
apply from: "${rootDir}/gradle/openssl.gradle"
2-
31
dependencies {
42
compile project(':crypto-keys')
53

64
compile "com.google.guava:guava"
75
compile "com.palantir.seek-io:seek-io"
8-
compile "org.apache.commons:commons-crypto"
9-
implementation "org.slf4j:slf4j-api"
106

117
annotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess'
128
compileOnly 'org.openjdk.jmh:jmh-generator-annprocess'

crypto-core/src/main/java/com/palantir/crypto2/cipher/ApacheCiphers.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,22 @@
1717
package com.palantir.crypto2.cipher;
1818

1919
import java.util.Properties;
20-
import org.apache.commons.crypto.cipher.CryptoCipherFactory;
2120

2221
public final class ApacheCiphers {
2322

2423
private ApacheCiphers() {}
2524

2625
/**
27-
* Configures the provided {@link Properties} such that {@link CryptoCipherFactory#getCryptoCipher(String,
28-
* Properties)} will only try to use the OpenSSL cipher implementation which uses AES-NI.
26+
* Does nothing.
27+
*
28+
* Previously configured the provided {@link Properties} such that
29+
* {@code CryptoCipherFactory#getCryptoCipher(String, Properties)} will only try to use the OpenSSL cipher
30+
* implementation which uses AES-NI.
31+
*
32+
* @deprecated Exists for abi compatibility, no longer does anything.
2933
*/
34+
@Deprecated
3035
public static Properties forceOpenSsl(Properties properties) {
31-
properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
32-
CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());
3336
return properties;
3437
}
3538

crypto-core/src/main/java/com/palantir/crypto2/io/ApacheCtrDecryptingSeekableInput.java

Lines changed: 0 additions & 132 deletions
This file was deleted.

crypto-core/src/main/java/com/palantir/crypto2/io/CryptoStreamFactory.java

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package com.palantir.crypto2.io;
1818

19-
import com.google.common.annotations.VisibleForTesting;
20-
import com.palantir.crypto2.cipher.ApacheCiphers;
2119
import com.palantir.crypto2.cipher.SeekableCipher;
2220
import com.palantir.crypto2.cipher.SeekableCipherFactory;
2321
import com.palantir.crypto2.keys.KeyMaterial;
@@ -26,48 +24,19 @@
2624
import java.io.IOException;
2725
import java.io.InputStream;
2826
import java.io.OutputStream;
29-
import java.util.Properties;
3027
import javax.crypto.Cipher;
3128
import javax.crypto.CipherOutputStream;
32-
import javax.crypto.SecretKey;
33-
import org.apache.commons.crypto.stream.CtrCryptoOutputStream;
34-
import org.slf4j.Logger;
35-
import org.slf4j.LoggerFactory;
3629

3730
public final class CryptoStreamFactory {
3831

39-
private static final Logger log = LoggerFactory.getLogger(CryptoStreamFactory.class);
40-
private static final Properties PROPS = ApacheCiphers.forceOpenSsl(new Properties());
41-
private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
42-
private static final String OPEN_SSL_INIT_WARNING = "Unable to initialize cipher with OpenSSL, falling back to "
43-
+ "JCE implementation - see github.com/palantir/hadoop-crypto#faq";
44-
45-
private static volatile boolean fullExceptionLoggedAlready = false;
46-
4732
private CryptoStreamFactory() {}
4833

4934
/**
5035
* Returns a {@link SeekableInput} that decrypts the given SeekableInput using the given {@link KeyMaterial} and
5136
* cipher {@code algorithm}. When OpenSSL is available an implementation that uses AES-NI will be returned.
5237
*/
5338
public static SeekableInput decrypt(SeekableInput encryptedInput, KeyMaterial keyMaterial, String algorithm) {
54-
return decrypt(encryptedInput, keyMaterial, algorithm, false);
55-
}
56-
57-
@SuppressWarnings("CatchBlockLogException")
58-
@VisibleForTesting
59-
static SeekableInput decrypt(
60-
SeekableInput encryptedInput, KeyMaterial keyMaterial, String algorithm, boolean forceJce) {
61-
if (!algorithm.equals(AES_ALGORITHM) || forceJce) {
62-
return new DecryptingSeekableInput(encryptedInput, SeekableCipherFactory.getCipher(algorithm, keyMaterial));
63-
}
64-
65-
try {
66-
return new ApacheCtrDecryptingSeekableInput(encryptedInput, keyMaterial);
67-
} catch (IOException e) {
68-
warningLog(e);
69-
return new DecryptingSeekableInput(encryptedInput, SeekableCipherFactory.getCipher(algorithm, keyMaterial));
70-
}
39+
return new DecryptingSeekableInput(encryptedInput, SeekableCipherFactory.getCipher(algorithm, keyMaterial));
7140
}
7241

7342
/**
@@ -83,39 +52,7 @@ public static InputStream decrypt(InputStream input, KeyMaterial keyMaterial, St
8352
* cipher {@code algorithm}. When OpenSSL is available an implementation that uses AES-NI will be returned.
8453
*/
8554
public static OutputStream encrypt(OutputStream output, KeyMaterial keyMaterial, String algorithm) {
86-
return encrypt(output, keyMaterial, algorithm, false);
87-
}
88-
89-
@SuppressWarnings("CatchBlockLogException")
90-
@VisibleForTesting
91-
static OutputStream encrypt(OutputStream output, KeyMaterial keyMaterial, String algorithm, boolean forceJce) {
92-
if (!algorithm.equals(AES_ALGORITHM) || forceJce) {
93-
return createDefaultEncryptedStream(output, keyMaterial, algorithm);
94-
}
95-
96-
try {
97-
return createApacheEncryptedStream(output, keyMaterial);
98-
} catch (IOException e) {
99-
warningLog(e);
100-
return createDefaultEncryptedStream(output, keyMaterial, algorithm);
101-
}
102-
}
103-
104-
/** To avoid spamming logs with exceptions, we only log the exception once. */
105-
private static void warningLog(IOException exception) {
106-
if (fullExceptionLoggedAlready) {
107-
log.warn(OPEN_SSL_INIT_WARNING);
108-
} else {
109-
log.warn(OPEN_SSL_INIT_WARNING, exception);
110-
fullExceptionLoggedAlready = true;
111-
}
112-
}
113-
114-
private static OutputStream createApacheEncryptedStream(OutputStream output, KeyMaterial keyMaterial)
115-
throws IOException {
116-
SecretKey secretKey = keyMaterial.getSecretKey();
117-
byte[] iv = keyMaterial.getIv();
118-
return new CtrCryptoOutputStream(PROPS, output, secretKey.getEncoded(), iv);
55+
return createDefaultEncryptedStream(output, keyMaterial, algorithm);
11956
}
12057

12158
private static OutputStream createDefaultEncryptedStream(OutputStream output, KeyMaterial keyMaterial,

crypto-core/src/test/java/com/palantir/crypto2/io/ApacheCtrDecryptingSeekableInputTests.java

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)