diff --git a/src/main/java/io/bitsquare/msg/P2PNode.java b/src/main/java/io/bitsquare/msg/P2PNode.java index 1b02ded84c..f04aee2191 100644 --- a/src/main/java/io/bitsquare/msg/P2PNode.java +++ b/src/main/java/io/bitsquare/msg/P2PNode.java @@ -44,7 +44,7 @@ public class P2PNode // just for lightweight client test public static void main(String[] args) { - P2PNode p2pNode = new P2PNode(DSAKeyUtil.getKeyPair(), false, SeedNodeAddress.StaticSeedNodeAddresses.LOCALHOST, + P2PNode p2pNode = new P2PNode(DSAKeyUtil.generateKeyPair(), false, SeedNodeAddress.StaticSeedNodeAddresses.LOCALHOST, (message, peerAddress) -> log.debug("handleMessage: message= " + message + "/ peerAddress=" + peerAddress)); p2pNode.start(new FutureCallback() { diff --git a/src/main/java/io/bitsquare/user/User.java b/src/main/java/io/bitsquare/user/User.java index 4511002056..032af2d8ad 100644 --- a/src/main/java/io/bitsquare/user/User.java +++ b/src/main/java/io/bitsquare/user/User.java @@ -12,7 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The User is stored locally it is never transmitted over the wire. + * The User is persisted locally it is never transmitted over the wire (messageKeyPair contains private key!). */ public class User implements Serializable { @@ -48,8 +48,9 @@ public class User implements Serializable { // First time bankAccounts = new ArrayList<>(); - messageKeyPair = DSAKeyUtil.getKeyPair(); // DSAKeyUtil.getKeyPair() runs in same thread now + messageKeyPair = DSAKeyUtil.generateKeyPair(); // DSAKeyUtil.getKeyPair() runs in same thread now } + DSAKeyUtil.generateKeyPair(); } public void addBankAccount(BankAccount bankAccount) diff --git a/src/main/java/io/bitsquare/util/DSAKeyUtil.java b/src/main/java/io/bitsquare/util/DSAKeyUtil.java index 169922a7a1..824c2a3d1d 100755 --- a/src/main/java/io/bitsquare/util/DSAKeyUtil.java +++ b/src/main/java/io/bitsquare/util/DSAKeyUtil.java @@ -1,26 +1,17 @@ package io.bitsquare.util; import com.google.bitcoin.core.Utils; -import com.google.bitcoin.utils.Threading; -import io.bitsquare.BitSquare; -import java.io.*; -import java.security.*; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; +import java.security.KeyPair; +import java.security.KeyPairGenerator; +import java.security.NoSuchAlgorithmException; +import java.security.PublicKey; import java.security.spec.X509EncodedKeySpec; -import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * Runs in JavaFX Application Thread now but might be sent to a background thread. - * We need to handle threading issues in the client classes if we change. - */ public class DSAKeyUtil { private static final Logger log = LoggerFactory.getLogger(DSAKeyUtil.class); - private static final String prefix = BitSquare.getAppName() + "_"; - private static final ReentrantLock lock = Threading.lock("DSAKeyUtil"); /////////////////////////////////////////////////////////////////////////////////////////// @@ -34,157 +25,18 @@ public class DSAKeyUtil return Utils.bytesToHexString(x509EncodedKeySpec.getEncoded()); } - // not used yet - /* - - public static PublicKey getPublicKeyFromHexString(String publicKeyAsHex) throws NoSuchAlgorithmException, InvalidKeySpecException - { - final byte[] bytes = Utils.parseAsHexOrBase58(publicKeyAsHex); - final KeyFactory keyFactory = KeyFactory.GET_INSTANCE("DSA"); - return keyFactory.generatePublic(new X509EncodedKeySpec(bytes)); - } */ - - - public static KeyPair getKeyPair() - { - return getKeyPair(FileUtil.getFile(prefix + "public", "key"), FileUtil.getFile(prefix + "private", "key")); - } - - - /////////////////////////////////////////////////////////////////////////////////////////// - // Private methods - /////////////////////////////////////////////////////////////////////////////////////////// - - - private static KeyPair getKeyPair(File pubKeyFile, File privKeyFile) + public static KeyPair generateKeyPair() { try { - return readKeyPairFromFiles(pubKeyFile, privKeyFile); - } catch (Throwable throwable) + final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); + keyGen.initialize(1024); + KeyPair generatedKeyPair = keyGen.genKeyPair(); + return generatedKeyPair; + } catch (NoSuchAlgorithmException e) { - if (throwable instanceof FileNotFoundException) - { - log.debug("Files not found. That is ok for the first execute."); - } - else - { - log.error("Could not read key files. " + throwable); - } - - try - { - final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); - keyGen.initialize(1024); - KeyPair generatedKeyPair = keyGen.genKeyPair(); - try - { - saveKeyPairToFiles(pubKeyFile, privKeyFile, generatedKeyPair); - } catch (Throwable t2) - { - t2.printStackTrace(); - log.error("Saving key pair failed. " + t2); - } - return generatedKeyPair; - } catch (Throwable t3) - { - t3.printStackTrace(); - log.error("Generating key pair failed. " + t3); - return null; - } - } - } - - - private static void saveKeyPairToFiles(File pubKeyFile, File privKeyFile, KeyPair keyPair) throws IOException - { - lock.lock(); - final File pubKeyTempFile = FileUtil.getTempFile("pubKey_temp_" + prefix); - final File privKeyTempFile = FileUtil.getTempFile("privKey_temp_" + prefix); - final FileOutputStream pubKeyFileOutputStream = new FileOutputStream(pubKeyTempFile); - final FileOutputStream privKeyFileOutputStream = new FileOutputStream(privKeyTempFile); - try - { - // Don't use auto closeable resources in try() as we need to close it - // manually before replacing file with temp file anyway - - final PublicKey publicKey = keyPair.getPublic(); - final X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); - pubKeyFileOutputStream.write(x509EncodedKeySpec.getEncoded()); - pubKeyFileOutputStream.flush(); - pubKeyFileOutputStream.getFD().sync(); - - final PrivateKey privateKey = keyPair.getPrivate(); - final PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); - privKeyFileOutputStream.write(pkcs8EncodedKeySpec.getEncoded()); - privKeyFileOutputStream.flush(); - privKeyFileOutputStream.getFD().sync(); - - // Close resources before replacing file with temp file because otherwise it causes problems on windows when rename temp file - pubKeyFileOutputStream.close(); - privKeyFileOutputStream.close(); - - FileUtil.writeTempFileToFile(pubKeyTempFile, pubKeyFile); - FileUtil.writeTempFileToFile(privKeyTempFile, privKeyFile); - } catch (IOException e) - { - e.printStackTrace(); - log.error("saveKeyPairToFiles failed." + e); - - } finally - { - if (pubKeyTempFile.exists()) - { - log.warn("PubKeyTempFile still exists after failed save."); - if (!pubKeyTempFile.delete()) - { - log.warn("Cannot delete pubKeyTempFile."); - } - } - if (privKeyTempFile.exists()) - { - log.warn("PrivKeyTempFile still exists after failed save."); - if (!privKeyTempFile.delete()) - { - log.warn("Cannot delete privKeyTempFile."); - } - } - - try - { - pubKeyFileOutputStream.close(); - privKeyFileOutputStream.close(); - } catch (IOException e) - { - e.printStackTrace(); - log.error("Cannot close resources."); - } - - lock.unlock(); - } - } - - private static KeyPair readKeyPairFromFiles(File pubKeyFile, File privKeyFile) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException - { - lock.lock(); - try (final FileInputStream pubKeyFileInputStream = new FileInputStream(pubKeyFile); - final FileInputStream privKeyFileInputStream = new FileInputStream(privKeyFile)) - { - final KeyFactory keyFactory = KeyFactory.getInstance("DSA"); - - final byte[] encodedPubKey = new byte[(int) pubKeyFile.length()]; - //noinspection ResultOfMethodCallIgnored - pubKeyFileInputStream.read(encodedPubKey); - final PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedPubKey)); - - final byte[] encodedPrivKey = new byte[(int) privKeyFile.length()]; - //noinspection ResultOfMethodCallIgnored - privKeyFileInputStream.read(encodedPrivKey); - final PrivateKey privKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivKey)); - return new KeyPair(pubKey, privKey); - } finally - { - lock.unlock(); + log.error(e.toString()); } + return null; } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 144c0e8e14..20a0502932 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -22,7 +22,7 @@ - +