Search in sources :

Example 1 with EncryptionService

use of io.bitsquare.crypto.EncryptionService in project bitsquare by bitsquare.

the class ProtectedDataStorageTest method setup.

@Before
public void setup() throws InterruptedException, NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException, CryptoException, SignatureException, InvalidKeyException {
    Security.addProvider(new BouncyCastleProvider());
    dir1 = File.createTempFile("temp_tests1", "");
    dir1.delete();
    dir1.mkdir();
    dir2 = File.createTempFile("temp_tests2", "");
    dir2.delete();
    dir2.mkdir();
    UserThread.setExecutor(Executors.newSingleThreadExecutor());
    P2PDataStorage.CHECK_TTL_INTERVAL_SEC = 500;
    keyRing1 = new KeyRing(new KeyStorage(dir1));
    storageSignatureKeyPair1 = keyRing1.getSignatureKeyPair();
    encryptionService1 = new EncryptionService(keyRing1);
    P2PService p2PService = TestUtils.getAndStartSeedNode(8001, useClearNet, seedNodes).getSeedNodeP2PService();
    networkNode1 = p2PService.getNetworkNode();
    peerManager1 = p2PService.getPeerManager();
    dataStorage1 = p2PService.getP2PDataStorage();
    // for mailbox
    keyRing2 = new KeyRing(new KeyStorage(dir2));
    storageSignatureKeyPair2 = keyRing2.getSignatureKeyPair();
    encryptionService2 = new EncryptionService(keyRing2);
    mockData = new MockData("mockData", keyRing1.getSignatureKeyPair().getPublic());
    Thread.sleep(sleepTime);
}
Also used : EncryptionService(io.bitsquare.crypto.EncryptionService) MockData(io.bitsquare.p2p.storage.mocks.MockData) P2PService(io.bitsquare.p2p.P2PService) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider)

Example 2 with EncryptionService

use of io.bitsquare.crypto.EncryptionService in project bitsquare by bitsquare.

the class P2PService method onMessage.

///////////////////////////////////////////////////////////////////////////////////////////
// MessageListener implementation
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onMessage(Message message, Connection connection) {
    if (message instanceof PrefixedSealedAndSignedMessage) {
        Log.traceCall("\n\t" + message.toString() + "\n\tconnection=" + connection);
        // Seed nodes don't have set the encryptionService
        if (optionalEncryptionService.isPresent()) {
            try {
                PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = (PrefixedSealedAndSignedMessage) message;
                if (verifyAddressPrefixHash(prefixedSealedAndSignedMessage)) {
                    // We set connectionType to that connection to avoid that is get closed when 
                    // we get too many connection attempts.
                    connection.setPeerType(Connection.PeerType.DIRECT_MSG_PEER);
                    log.debug("Try to decrypt...");
                    DecryptedMsgWithPubKey decryptedMsgWithPubKey = optionalEncryptionService.get().decryptAndVerify(prefixedSealedAndSignedMessage.sealedAndSigned);
                    log.debug("\n\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\n" + "Decrypted SealedAndSignedMessage:\ndecryptedMsgWithPubKey={}" + "\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\n", decryptedMsgWithPubKey);
                    if (connection.getPeersNodeAddressOptional().isPresent())
                        decryptedDirectMessageListeners.stream().forEach(e -> e.onDirectMessage(decryptedMsgWithPubKey, connection.getPeersNodeAddressOptional().get()));
                    else
                        log.error("peersNodeAddress is not available at onMessage.");
                } else {
                    log.debug("Wrong receiverAddressMaskHash. The message is not intended for us.");
                }
            } catch (CryptoException e) {
                log.debug(message.toString());
                log.debug(e.toString());
                log.debug("Decryption of prefixedSealedAndSignedMessage.sealedAndSigned failed. " + "That is expected if the message is not intended for us.");
            }
        }
    }
}
Also used : Clock(io.bitsquare.common.Clock) PubKeyRing(io.bitsquare.common.crypto.PubKeyRing) Inject(com.google.inject.Inject) AddDataMessage(io.bitsquare.p2p.storage.messages.AddDataMessage) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry) LoggerFactory(org.slf4j.LoggerFactory) Socks5ProxyProvider(io.bitsquare.network.Socks5ProxyProvider) SettableFuture(com.google.common.util.concurrent.SettableFuture) CryptoException(io.bitsquare.common.crypto.CryptoException) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) PeerManager(io.bitsquare.p2p.peers.PeerManager) KeyRing(io.bitsquare.common.crypto.KeyRing) RefreshTTLMessage(io.bitsquare.p2p.storage.messages.RefreshTTLMessage) MonadicBinding(org.fxmisc.easybind.monadic.MonadicBinding) FileUtil(io.bitsquare.storage.FileUtil) RequestDataManager(io.bitsquare.p2p.peers.getdata.RequestDataManager) ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) EncryptionService(io.bitsquare.crypto.EncryptionService) Subscription(org.fxmisc.easybind.Subscription) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) NotNull(org.jetbrains.annotations.NotNull) BanList(io.bitsquare.p2p.peers.BanList) java.util(java.util) MailboxStoragePayload(io.bitsquare.p2p.storage.payload.MailboxStoragePayload) HashMapChangedListener(io.bitsquare.p2p.storage.HashMapChangedListener) BroadcastMessage(io.bitsquare.p2p.storage.messages.BroadcastMessage) io.bitsquare.p2p.network(io.bitsquare.p2p.network) PeerExchangeManager(io.bitsquare.p2p.peers.peerexchange.PeerExchangeManager) Log(io.bitsquare.app.Log) P2PDataStorage(io.bitsquare.p2p.storage.P2PDataStorage) BroadcastHandler(io.bitsquare.p2p.peers.BroadcastHandler) javafx.beans.property(javafx.beans.property) Utilities(io.bitsquare.common.util.Utilities) Logger(org.slf4j.Logger) UserThread(io.bitsquare.common.UserThread) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PublicKey(java.security.PublicKey) StoragePayload(io.bitsquare.p2p.storage.payload.StoragePayload) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) Futures(com.google.common.util.concurrent.Futures) SeedNodesRepository(io.bitsquare.p2p.seed.SeedNodesRepository) EasyBind(org.fxmisc.easybind.EasyBind) Broadcaster(io.bitsquare.p2p.peers.Broadcaster) Paths(java.nio.file.Paths) KeepAliveManager(io.bitsquare.p2p.peers.keepalive.KeepAliveManager) Named(com.google.inject.name.Named) Storage(io.bitsquare.storage.Storage) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DecryptedMsgWithPubKey(io.bitsquare.crypto.DecryptedMsgWithPubKey) io.bitsquare.p2p.messaging(io.bitsquare.p2p.messaging) NetworkOptionKeys(io.bitsquare.network.NetworkOptionKeys) DecryptedMsgWithPubKey(io.bitsquare.crypto.DecryptedMsgWithPubKey) CryptoException(io.bitsquare.common.crypto.CryptoException)

Example 3 with EncryptionService

use of io.bitsquare.crypto.EncryptionService in project bitsquare by bitsquare.

the class StressTestMailboxMessage method createPeerNode.

@NotNull
private P2PService createPeerNode(int n, int port) {
    // peer data directories
    final File peerDir = new File(testDataDir.toFile(), String.format("peer-%06d", n));
    final File peerTorDir = new File(peerDir, "tor");
    final File peerStorageDir = new File(peerDir, "db");
    final File peerKeysDir = new File(peerDir, "keys");
    //noinspection ResultOfMethodCallIgnored
    // needed for creating the key ring
    peerKeysDir.mkdirs();
    // peer keys
    final KeyStorage peerKeyStorage = new KeyStorage(peerKeysDir);
    final KeyRing peerKeyRing = new KeyRing(peerKeyStorage);
    final EncryptionService peerEncryptionService = new EncryptionService(peerKeyRing);
    return new P2PService(seedNodesRepository, port, peerTorDir, useLocalhost, REGTEST_NETWORK_ID, P2PService.MAX_CONNECTIONS_DEFAULT, peerStorageDir, null, null, null, new Clock(), null, peerEncryptionService, peerKeyRing);
}
Also used : PubKeyRing(io.bitsquare.common.crypto.PubKeyRing) KeyRing(io.bitsquare.common.crypto.KeyRing) KeyStorage(io.bitsquare.common.crypto.KeyStorage) EncryptionService(io.bitsquare.crypto.EncryptionService) Clock(io.bitsquare.common.Clock) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

EncryptionService (io.bitsquare.crypto.EncryptionService)3 Clock (io.bitsquare.common.Clock)2 KeyRing (io.bitsquare.common.crypto.KeyRing)2 PubKeyRing (io.bitsquare.common.crypto.PubKeyRing)2 File (java.io.File)2 NotNull (org.jetbrains.annotations.NotNull)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 FutureCallback (com.google.common.util.concurrent.FutureCallback)1 Futures (com.google.common.util.concurrent.Futures)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 Log (io.bitsquare.app.Log)1 UserThread (io.bitsquare.common.UserThread)1 CryptoException (io.bitsquare.common.crypto.CryptoException)1 KeyStorage (io.bitsquare.common.crypto.KeyStorage)1 Utilities (io.bitsquare.common.util.Utilities)1 DecryptedMsgWithPubKey (io.bitsquare.crypto.DecryptedMsgWithPubKey)1