Skip to content

Commit 909fbf0

Browse files
committed
don't depend on BouncyCastle library directly
1 parent b2e8e3f commit 909fbf0

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

src/main/java/com/aliyun/oss/crypto/CryptoRuntime.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919

2020
package com.aliyun.oss.crypto;
2121

22+
import com.aliyun.oss.common.utils.BinaryUtil;
23+
24+
import java.lang.reflect.InvocationTargetException;
25+
import java.lang.reflect.Method;
26+
import java.math.BigInteger;
2227
import java.security.Provider;
2328
import java.security.Security;
29+
import java.security.spec.RSAPrivateKeySpec;
2430

2531
public class CryptoRuntime {
2632
static final String BOUNCY_CASTLE_PROVIDER = "BC";
@@ -41,6 +47,27 @@ public static void enableBouncyCastle() {
4147
}
4248
}
4349

50+
public static RSAPrivateKeySpec convertPemPKCS1ToPrivateKey(byte[] buffer)
51+
{
52+
try {
53+
Class<?> clz = Class.forName("org.bouncycastle.asn1.pkcs.RSAPrivateKey");
54+
Method method = clz.getMethod("getInstance", new Class[] { Object.class});
55+
Object obj = method.invoke(null, new Object[] {buffer});
56+
57+
clz = Class.forName("org.bouncycastle.asn1.pkcs.RSAPrivateKey");
58+
method = clz.getMethod("getModulus", null);
59+
BigInteger modulus = (BigInteger) method.invoke(obj, null);
60+
61+
method = clz.getMethod("getPrivateExponent", null);
62+
BigInteger exponent = (BigInteger) method.invoke(obj, null);
63+
64+
return new RSAPrivateKeySpec(modulus,exponent);
65+
66+
} catch (Exception e) {
67+
throw new UnsupportedOperationException("convertPemPKCS1ToPrivateKey fail.");
68+
}
69+
}
70+
4471
private static synchronized boolean isBouncyCastleAvailable() {
4572
return Security.getProvider(BOUNCY_CASTLE_PROVIDER) != null;
4673
}

src/main/java/com/aliyun/oss/crypto/SimpleRSAEncryptionMaterials.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,12 @@ public static RSAPrivateKey getPrivateKeyFromPemPKCS1(final String privateKeyStr
204204
CryptoRuntime.enableBouncyCastle();
205205

206206
byte[] buffer = BinaryUtil.fromBase64String(adjustStr);
207-
org.bouncycastle.asn1.pkcs.RSAPrivateKey asn1PrivKey = org.bouncycastle.asn1.pkcs.RSAPrivateKey
208-
.getInstance(buffer);
209-
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(asn1PrivKey.getModulus(),
210-
asn1PrivKey.getPrivateExponent());
207+
RSAPrivateKeySpec keySpec = CryptoRuntime.convertPemPKCS1ToPrivateKey(buffer);
208+
211209
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
212210

213211
return (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
212+
214213
} catch (Exception e) {
215214
throw new ClientException("get private key from PKCS1 pem String error." + e.getMessage(), e);
216215
}

src/test/java/com/aliyun/oss/crypto/ContentCryptoMaterialTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package com.aliyun.oss.crypto;
2121

2222
import junit.framework.Assert;
23-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2423
import org.junit.Test;
2524

2625
import java.security.SecureRandom;

src/test/java/com/aliyun/oss/crypto/CryptoConfigurationTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
package com.aliyun.oss.crypto;
2121

22+
23+
import java.security.Provider;
2224
import java.security.SecureRandom;
23-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
25+
2426
import org.junit.Test;
2527
import junit.framework.Assert;
2628

@@ -37,7 +39,7 @@ public void testConstruction() {
3739
ContentCryptoMode.AES_CTR_MODE,
3840
CryptoStorageMethod.ObjectMetadata,
3941
new SecureRandom(),
40-
new BouncyCastleProvider());
42+
getBouncyCastleProvider());
4143

4244
Assert.assertEquals(ContentCryptoMode.AES_CTR_MODE, cryptoConfig.getContentCryptoMode());
4345
Assert.assertEquals(CryptoStorageMethod.ObjectMetadata, cryptoConfig.getStorageMethod());
@@ -63,11 +65,21 @@ public void testProvider() {
6365
CryptoConfiguration cryptoConfig = new CryptoConfiguration();
6466
Assert.assertNull(cryptoConfig.getContentCryptoProvider());
6567

66-
cryptoConfig = new CryptoConfiguration().withContentCryptoProvider(new BouncyCastleProvider());
68+
cryptoConfig = new CryptoConfiguration().withContentCryptoProvider(getBouncyCastleProvider());
6769
Assert.assertEquals("BC", cryptoConfig.getContentCryptoProvider().getName());
6870

6971
cryptoConfig = new CryptoConfiguration();
70-
cryptoConfig.setContentCryptoProvider(new BouncyCastleProvider());
72+
cryptoConfig.setContentCryptoProvider(getBouncyCastleProvider());
7173
Assert.assertEquals("BC", cryptoConfig.getContentCryptoProvider().getName());
7274
}
75+
76+
public static Provider getBouncyCastleProvider()
77+
{
78+
try {
79+
Class<?> clz = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
80+
return (Provider)clz.newInstance();
81+
} catch (Exception e) {
82+
return null;
83+
}
84+
}
7385
}

src/test/java/com/aliyun/oss/crypto/RSAEncryptionUnitTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
import java.security.KeyPair;
2323
import java.security.PrivateKey;
24+
import java.security.Provider;
2425
import java.security.PublicKey;
2526
import java.security.interfaces.RSAPrivateKey;
2627
import java.security.interfaces.RSAPublicKey;
2728
import javax.crypto.Cipher;
28-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
29+
2930
import org.junit.Test;
3031
import junit.framework.Assert;
3132

@@ -139,7 +140,7 @@ private byte[] encrypt(PublicKey publicKey, byte[] plainData) throws Exception {
139140
throw new Exception("public key is null.");
140141
}
141142

142-
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", new BouncyCastleProvider());
143+
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", getBouncyCastleProvider());
143144
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
144145
byte[] output = cipher.doFinal(plainData);
145146
return output;
@@ -150,9 +151,19 @@ private byte[] decrypt(PrivateKey privateKey, byte[] cipherData) throws Exceptio
150151
throw new Exception("private key is null.");
151152
}
152153
Cipher cipher = null;
153-
cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", new BouncyCastleProvider());
154+
cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", getBouncyCastleProvider());
154155
cipher.init(Cipher.DECRYPT_MODE, privateKey);
155156
byte[] output = cipher.doFinal(cipherData);
156157
return output;
157158
}
159+
160+
public static Provider getBouncyCastleProvider()
161+
{
162+
try {
163+
Class<?> clz = Class.forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
164+
return (Provider)clz.newInstance();
165+
} catch (Exception e) {
166+
return null;
167+
}
168+
}
158169
}

0 commit comments

Comments
 (0)