Use default security provider instead of Bouncy Castle

This commit is contained in:
Christoph Atteneder 2018-09-14 16:02:58 +02:00
parent 1c783e436e
commit 4c068af7b3
No known key found for this signature in database
GPG key ID: CD5DC1C529CDFD3B
21 changed files with 44 additions and 106 deletions

View file

@ -25,6 +25,8 @@ import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.OAEPParameterSpec;
import javax.crypto.spec.PSource;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
@ -36,6 +38,7 @@ import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.MGF1ParameterSpec;
import java.security.spec.X509EncodedKeySpec;
import java.io.ByteArrayOutputStream;
@ -51,7 +54,7 @@ public class Encryption {
private static final Logger log = LoggerFactory.getLogger(Encryption.class);
public static final String ASYM_KEY_ALGO = "RSA";
private static final String ASYM_CIPHER = "RSA/None/OAEPWithSHA256AndMGF1Padding";
private static final String ASYM_CIPHER = "RSA/ECB/OAEPWithSHA-256AndMGF1PADDING";
private static final String SYM_KEY_ALGO = "AES";
private static final String SYM_CIPHER = "AES";
@ -61,7 +64,7 @@ public class Encryption {
public static KeyPair generateKeyPair() {
long ts = System.currentTimeMillis();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ASYM_KEY_ALGO, "BC");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ASYM_KEY_ALGO);
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.genKeyPair();
log.trace("Generate msgEncryptionKeyPair needed {} ms", System.currentTimeMillis() - ts);
@ -80,7 +83,7 @@ public class Encryption {
public static byte[] encrypt(byte[] payload, SecretKey secretKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(SYM_CIPHER, "BC");
Cipher cipher = Cipher.getInstance(SYM_CIPHER);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(payload);
} catch (Throwable e) {
@ -91,7 +94,7 @@ public class Encryption {
public static byte[] decrypt(byte[] encryptedPayload, SecretKey secretKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(SYM_CIPHER, "BC");
Cipher cipher = Cipher.getInstance(SYM_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(encryptedPayload);
} catch (Throwable e) {
@ -157,7 +160,7 @@ public class Encryption {
}
private static byte[] getHmac(byte[] payload, SecretKey secretKey) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException {
Mac mac = Mac.getInstance(HMAC, "BC");
Mac mac = Mac.getInstance(HMAC);
mac.init(secretKey);
return mac.doFinal(payload);
}
@ -195,8 +198,10 @@ public class Encryption {
public static byte[] encryptSecretKey(SecretKey secretKey, PublicKey publicKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(ASYM_CIPHER, "BC");
cipher.init(Cipher.WRAP_MODE, publicKey);
Cipher cipher = Cipher.getInstance(ASYM_CIPHER);
OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-256", "MGF1",
MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.WRAP_MODE, publicKey, oaepParameterSpec);
return cipher.wrap(secretKey);
} catch (Throwable e) {
e.printStackTrace();
@ -206,8 +211,10 @@ public class Encryption {
public static SecretKey decryptSecretKey(byte[] encryptedSecretKey, PrivateKey privateKey) throws CryptoException {
try {
Cipher cipher = Cipher.getInstance(ASYM_CIPHER, "BC");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Cipher cipher = Cipher.getInstance(ASYM_CIPHER);
OAEPParameterSpec oaepParameterSpec = new OAEPParameterSpec("SHA-256", "MGF1",
MGF1ParameterSpec.SHA256, PSource.PSpecified.DEFAULT);
cipher.init(Cipher.UNWRAP_MODE, privateKey, oaepParameterSpec);
return (SecretKey) cipher.unwrap(encryptedSecretKey, "AES", Cipher.SECRET_KEY);
} catch (Throwable e) {
// errors when trying to decrypt foreign network_messages are normal
@ -222,7 +229,7 @@ public class Encryption {
public static SecretKey generateSecretKey(int bits) {
try {
KeyGenerator keyPairGenerator = KeyGenerator.getInstance(SYM_KEY_ALGO, "BC");
KeyGenerator keyPairGenerator = KeyGenerator.getInstance(SYM_KEY_ALGO);
keyPairGenerator.init(bits);
return keyPairGenerator.generateKey();
} catch (Throwable e) {
@ -242,8 +249,8 @@ public class Encryption {
*/
public static PublicKey getPublicKeyFromBytes(byte[] encryptionPubKeyBytes) {
try {
return KeyFactory.getInstance(Encryption.ASYM_KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(encryptionPubKeyBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchProviderException e) {
return KeyFactory.getInstance(Encryption.ASYM_KEY_ALGO).generatePublic(new X509EncodedKeySpec(encryptionPubKeyBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
log.error("Error creating sigPublicKey from bytes. sigPublicKeyBytes as hex={}, error={}", Utilities.bytesAsHexString(encryptionPubKeyBytes), e);
e.printStackTrace();
throw new KeyConversionException(e);

View file

@ -25,7 +25,6 @@ import org.spongycastle.crypto.digests.RIPEMD160Digest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.nio.ByteBuffer;
@ -40,10 +39,10 @@ public class Hash {
*/
public static byte[] getSha256Hash(byte[] data) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256", "BC");
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(data, 0, data.length);
return digest.digest();
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
} catch (NoSuchAlgorithmException e) {
log.error("Could not create MessageDigest for hash. " + e.toString());
e.printStackTrace();
throw new RuntimeException(e);

View file

@ -28,7 +28,6 @@ import org.bouncycastle.openpgp.PGPKeyPair;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.DSAParams;
@ -121,7 +120,7 @@ public class KeyStorage {
FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key", 20);
// long now = System.currentTimeMillis();
try {
KeyFactory keyFactory = KeyFactory.getInstance(keyEntry.getAlgorithm(), "BC");
KeyFactory keyFactory = KeyFactory.getInstance(keyEntry.getAlgorithm());
PublicKey publicKey;
PrivateKey privateKey;
@ -158,7 +157,7 @@ public class KeyStorage {
log.debug("load completed in {} msec", System.currentTimeMillis() - new Date().getTime());
return new KeyPair(publicKey, privateKey);
} catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchProviderException e) {
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
log.error(e.getMessage());
throw new RuntimeException("Could not load key " + keyEntry.toString(), e);

View file

@ -28,7 +28,6 @@ import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
@ -60,12 +59,12 @@ public class Sig {
public static KeyPair generateKeyPair() {
long ts = System.currentTimeMillis();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGO, "BC");
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGO);
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.genKeyPair();
log.trace("Generate msgSignatureKeyPair needed {} ms", System.currentTimeMillis() - ts);
return keyPair;
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
log.error(e.toString());
throw new RuntimeException("Could not create key.");
@ -80,11 +79,11 @@ public class Sig {
*/
public static byte[] sign(PrivateKey privateKey, byte[] data) throws CryptoException {
try {
Signature sig = Signature.getInstance(ALGO, "BC");
Signature sig = Signature.getInstance(ALGO);
sig.initSign(privateKey);
sig.update(data);
return sig.sign();
} catch (SignatureException | NoSuchProviderException | InvalidKeyException | NoSuchAlgorithmException e) {
} catch (SignatureException | InvalidKeyException | NoSuchAlgorithmException e) {
throw new CryptoException("Signing failed. " + e.getMessage());
}
}
@ -107,11 +106,11 @@ public class Sig {
*/
public static boolean verify(PublicKey publicKey, byte[] data, byte[] signature) throws CryptoException {
try {
Signature sig = Signature.getInstance(ALGO, "BC");
Signature sig = Signature.getInstance(ALGO);
sig.initVerify(publicKey);
sig.update(data);
return sig.verify(signature);
} catch (SignatureException | NoSuchProviderException | InvalidKeyException | NoSuchAlgorithmException e) {
} catch (SignatureException | InvalidKeyException | NoSuchAlgorithmException e) {
throw new CryptoException("Signature verification failed. " + e.getMessage());
}
}
@ -132,8 +131,8 @@ public class Sig {
*/
public static PublicKey getPublicKeyFromBytes(byte[] sigPublicKeyBytes) {
try {
return KeyFactory.getInstance(Sig.KEY_ALGO, "BC").generatePublic(new X509EncodedKeySpec(sigPublicKeyBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException | NoSuchProviderException e) {
return KeyFactory.getInstance(Sig.KEY_ALGO).generatePublic(new X509EncodedKeySpec(sigPublicKeyBytes));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
log.error("Error creating sigPublicKey from bytes. sigPublicKeyBytes as hex={}, error={}", Utilities.bytesAsHexString(sigPublicKeyBytes), e);
e.printStackTrace();
throw new KeyConversionException(e);

View file

@ -25,10 +25,7 @@ import org.bitcoinj.store.BlockStoreException;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import lombok.extern.slf4j.Slf4j;
@ -38,8 +35,6 @@ public class CommonSetup {
public static void setup(UncaughtExceptionHandler uncaughtExceptionHandler) {
setupErrorHandler(uncaughtExceptionHandler);
Security.addProvider(new BouncyCastleProvider());
if (Utilities.isLinux())
System.setProperty("prism.lcdtext", "false");
}

View file

@ -444,9 +444,6 @@ public class BisqSetup {
((Ping) tuple.getNetworkEnvelope()).getNonce() == payload.getNonce() &&
((Ping) tuple.getNetworkEnvelope()).getLastRoundTripTime() == payload.getLastRoundTripTime()) {
log.debug("Crypto test succeeded");
if (Security.getProvider("BC") == null)
throw new CryptoException("Security provider BountyCastle is not available.");
} else {
throw new CryptoException("Payload not correct after decryption");
}

View file

@ -34,8 +34,6 @@ import bisq.common.proto.ProtobufferException;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import java.security.Security;
import java.util.Date;
import java.util.function.Consumer;
@ -67,12 +65,6 @@ public class SetupUtils {
((Ping) tuple.getNetworkEnvelope()).getNonce() == payload.getNonce() &&
((Ping) tuple.getNetworkEnvelope()).getLastRoundTripTime() == payload.getLastRoundTripTime()) {
log.debug("Crypto test succeeded");
if (Security.getProvider("BC") != null) {
UserThread.execute(resultHandler::handleResult);
} else {
errorHandler.accept(new CryptoException("Security provider BountyCastle is not available."));
}
} else {
errorHandler.accept(new CryptoException("Payload not correct after decryption"));
}

View file

@ -47,10 +47,7 @@ import static org.mockito.Mockito.mock;
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class ArbitratorManagerTest {
@Before
public void setUp() {
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void testIsArbitratorAvailableForLanguage() {

View file

@ -45,7 +45,7 @@ public class EncryptionTest {
@Before
public void setup() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, CryptoException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests", "");
//noinspection ResultOfMethodCallIgnored
dir.delete();

View file

@ -51,7 +51,7 @@ public class SigTest {
@Before
public void setup() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, CryptoException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests", "");
//noinspection ResultOfMethodCallIgnored
dir.delete();

View file

@ -36,10 +36,7 @@ import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
public class SeedNodeAddressLookupTest {
@Before
public void setUp() {
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void testResolveNodeAddressesWhenLocalAddressSpecified() {

View file

@ -37,10 +37,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class SeedNodeAddressesTest {
@Before
public void setUp() {
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void testCollector() {

View file

@ -38,10 +38,6 @@ import org.bitcoinj.utils.Fiat;
import javafx.collections.FXCollections;
import javafx.collections.ObservableSet;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -128,7 +124,7 @@ public class TradesChartsViewModelTest {
@Before
public void setup() throws IOException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests1", "");
//noinspection ResultOfMethodCallIgnored
dir.delete();

View file

@ -27,17 +27,12 @@ import bisq.network.p2p.NodeAddress;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.util.ArrayList;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -50,10 +45,7 @@ import static org.mockito.Mockito.when;
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class PreferencesViewModelTest {
@Before
public void setUp() {
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void testGetArbitrationLanguages() {

View file

@ -7,7 +7,6 @@ import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class AccountNrValidatorTest {

View file

@ -26,11 +26,8 @@ import bisq.common.storage.FileUtil;
import io.bisq.generated.protobuffer.PB;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.io.File;
@ -56,7 +53,7 @@ public class EncryptionServiceTests {
@Before
public void setup() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, CryptoException {
Security.addProvider(new BouncyCastleProvider());
dir = File.createTempFile("temp_tests", "");
//noinspection ResultOfMethodCallIgnored
dir.delete();

View file

@ -19,10 +19,6 @@ package bisq.network.p2p.network;
import bisq.network.p2p.TestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
@ -30,7 +26,6 @@ import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@ -43,10 +38,7 @@ import org.junit.Test;
public class LocalhostNetworkNodeTest {
private static final Logger log = LoggerFactory.getLogger(LocalhostNetworkNodeTest.class);
@Before
public void setup() {
Security.addProvider(new BouncyCastleProvider());
}
@Test

View file

@ -24,10 +24,6 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.Security;
import java.io.File;
import java.io.IOException;
@ -38,7 +34,6 @@ import org.slf4j.LoggerFactory;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@ -52,10 +47,7 @@ public class TorNetworkNodeTest {
private static final Logger log = LoggerFactory.getLogger(TorNetworkNodeTest.class);
private CountDownLatch latch;
@Before
public void setup() {
Security.addProvider(new BouncyCastleProvider());
}
@Test
public void testTorNodeBeforeSecondReady() throws InterruptedException, IOException {

View file

@ -31,13 +31,10 @@ import bisq.common.proto.network.NetworkProtoResolver;
import bisq.common.proto.persistable.PersistenceProtoResolver;
import bisq.common.storage.FileUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.SignatureException;
import java.security.cert.CertificateException;
@ -84,7 +81,7 @@ public class P2PDataStorageTest {
@Before
public void setup() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException {
Security.addProvider(new BouncyCastleProvider());
dir1 = File.createTempFile("temp_tests1", "");
//noinspection ResultOfMethodCallIgnored
dir1.delete();

View file

@ -34,14 +34,11 @@ import bisq.common.crypto.KeyStorage;
import bisq.common.crypto.Sig;
import bisq.common.storage.FileUtil;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.SignatureException;
import java.security.cert.CertificateException;
@ -84,7 +81,7 @@ public class ProtectedDataStorageTest {
@Before
public void setup() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException {
Security.addProvider(new BouncyCastleProvider());
dir1 = File.createTempFile("temp_tests1", "");
//noinspection ResultOfMethodCallIgnored,ResultOfMethodCallIgnored
dir1.delete();

View file

@ -32,12 +32,9 @@ import io.bisq.generated.protobuffer.PB;
import org.apache.commons.lang3.RandomUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.InvalidKeyException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.SignatureException;
import java.security.cert.CertificateException;
@ -60,7 +57,7 @@ public class AddDataMessageTest {
@Before
public void setup() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException {
Security.addProvider(new BouncyCastleProvider());
dir1 = File.createTempFile("temp_tests1", "");
//noinspection ResultOfMethodCallIgnored
dir1.delete();