Search in sources :

Example 1 with CryptoException

use of io.bitsquare.common.crypto.CryptoException in project bitsquare by bitsquare.

the class P2PService method delayedRemoveEntryFromMailbox.

private void delayedRemoveEntryFromMailbox(DecryptedMsgWithPubKey decryptedMsgWithPubKey) {
    Log.traceCall();
    checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
    if (isBootstrapped()) {
        MailboxMessage mailboxMessage = (MailboxMessage) decryptedMsgWithPubKey.message;
        String uid = mailboxMessage.getUID();
        if (mailboxMap.containsKey(uid)) {
            ProtectedMailboxStorageEntry mailboxData = mailboxMap.get(uid);
            if (mailboxData != null && mailboxData.getStoragePayload() instanceof MailboxStoragePayload) {
                MailboxStoragePayload expirableMailboxStoragePayload = (MailboxStoragePayload) mailboxData.getStoragePayload();
                PublicKey receiversPubKey = mailboxData.receiversPubKey;
                checkArgument(receiversPubKey.equals(optionalKeyRing.get().getSignatureKeyPair().getPublic()), "receiversPubKey is not matching with our key. That must not happen.");
                try {
                    ProtectedMailboxStorageEntry protectedMailboxStorageEntry = p2PDataStorage.getMailboxDataWithSignedSeqNr(expirableMailboxStoragePayload, optionalKeyRing.get().getSignatureKeyPair(), receiversPubKey);
                    p2PDataStorage.removeMailboxData(protectedMailboxStorageEntry, networkNode.getNodeAddress(), true);
                } catch (CryptoException e) {
                    log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
                }
                mailboxMap.remove(uid);
                log.trace("Removed successfully decryptedMsgWithPubKey.");
            }
        } else {
            log.warn("uid for mailbox entry not found in mailboxMap. That should never happen." + "\n\tuid={}\n\tmailboxMap={}\n\tmailboxMessage={}", uid, mailboxMap, mailboxMessage);
        }
    } else {
        throw new NetworkNotReadyException();
    }
}
Also used : PublicKey(java.security.PublicKey) MailboxStoragePayload(io.bitsquare.p2p.storage.payload.MailboxStoragePayload) ProtectedMailboxStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry) CryptoException(io.bitsquare.common.crypto.CryptoException)

Example 2 with CryptoException

use of io.bitsquare.common.crypto.CryptoException in project bitsquare by bitsquare.

the class P2PService method addData.

///////////////////////////////////////////////////////////////////////////////////////////
// Data storage
///////////////////////////////////////////////////////////////////////////////////////////
public boolean addData(StoragePayload storagePayload, boolean isDataOwner) {
    Log.traceCall();
    checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
    if (isBootstrapped()) {
        try {
            ProtectedStorageEntry protectedStorageEntry = p2PDataStorage.getProtectedData(storagePayload, optionalKeyRing.get().getSignatureKeyPair());
            return p2PDataStorage.add(protectedStorageEntry, networkNode.getNodeAddress(), null, isDataOwner);
        } catch (CryptoException e) {
            log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
            return false;
        }
    } else {
        throw new NetworkNotReadyException();
    }
}
Also used : CryptoException(io.bitsquare.common.crypto.CryptoException) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Example 3 with CryptoException

use of io.bitsquare.common.crypto.CryptoException in project bitsquare by bitsquare.

the class P2PService method processProtectedMailboxStorageEntry.

///////////////////////////////////////////////////////////////////////////////////////////
// MailboxMessages
///////////////////////////////////////////////////////////////////////////////////////////
private void processProtectedMailboxStorageEntry(ProtectedMailboxStorageEntry protectedMailboxStorageEntry) {
    Log.traceCall();
    final NodeAddress nodeAddress = networkNode.getNodeAddress();
    // Seed nodes don't receive mailbox messages
    if (optionalEncryptionService.isPresent() && nodeAddress != null && !seedNodesRepository.isSeedNode(nodeAddress)) {
        Log.traceCall();
        MailboxStoragePayload mailboxStoragePayload = protectedMailboxStorageEntry.getMailboxStoragePayload();
        PrefixedSealedAndSignedMessage prefixedSealedAndSignedMessage = mailboxStoragePayload.prefixedSealedAndSignedMessage;
        if (verifyAddressPrefixHash(prefixedSealedAndSignedMessage)) {
            try {
                DecryptedMsgWithPubKey decryptedMsgWithPubKey = optionalEncryptionService.get().decryptAndVerify(prefixedSealedAndSignedMessage.sealedAndSigned);
                if (decryptedMsgWithPubKey.message instanceof MailboxMessage) {
                    MailboxMessage mailboxMessage = (MailboxMessage) decryptedMsgWithPubKey.message;
                    NodeAddress senderNodeAddress = mailboxMessage.getSenderNodeAddress();
                    checkNotNull(senderNodeAddress, "senderAddress must not be null for mailbox messages");
                    mailboxMap.put(mailboxMessage.getUID(), protectedMailboxStorageEntry);
                    log.trace("Decryption of SealedAndSignedMessage succeeded. senderAddress=" + senderNodeAddress + " / my address=" + getAddress());
                    decryptedMailboxListeners.stream().forEach(e -> e.onMailboxMessageAdded(decryptedMsgWithPubKey, senderNodeAddress));
                } else {
                    log.warn("tryDecryptMailboxData: Expected MailboxMessage but got other type. " + "decryptedMsgWithPubKey.message=", decryptedMsgWithPubKey.message);
                }
            } catch (CryptoException e) {
                log.debug(e.toString());
                log.debug("Decryption of prefixedSealedAndSignedMessage.sealedAndSigned failed. " + "That is expected if the message is not intended for us.");
            }
        } else {
            log.debug("Wrong blurredAddressHash. The message is not intended for us.");
        }
    }
}
Also used : DecryptedMsgWithPubKey(io.bitsquare.crypto.DecryptedMsgWithPubKey) MailboxStoragePayload(io.bitsquare.p2p.storage.payload.MailboxStoragePayload) CryptoException(io.bitsquare.common.crypto.CryptoException)

Example 4 with CryptoException

use of io.bitsquare.common.crypto.CryptoException 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 5 with CryptoException

use of io.bitsquare.common.crypto.CryptoException in project bitsquare by bitsquare.

the class P2PService method removeData.

public boolean removeData(StoragePayload storagePayload, boolean isDataOwner) {
    Log.traceCall();
    checkArgument(optionalKeyRing.isPresent(), "keyRing not set. Seems that is called on a seed node which must not happen.");
    if (isBootstrapped()) {
        try {
            ProtectedStorageEntry protectedStorageEntry = p2PDataStorage.getProtectedData(storagePayload, optionalKeyRing.get().getSignatureKeyPair());
            return p2PDataStorage.remove(protectedStorageEntry, networkNode.getNodeAddress(), isDataOwner);
        } catch (CryptoException e) {
            log.error("Signing at getDataWithSignedSeqNr failed. That should never happen.");
            return false;
        }
    } else {
        throw new NetworkNotReadyException();
    }
}
Also used : CryptoException(io.bitsquare.common.crypto.CryptoException) ProtectedStorageEntry(io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)

Aggregations

CryptoException (io.bitsquare.common.crypto.CryptoException)7 MailboxStoragePayload (io.bitsquare.p2p.storage.payload.MailboxStoragePayload)3 ProtectedMailboxStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry)3 ProtectedStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedStorageEntry)3 DecryptedMsgWithPubKey (io.bitsquare.crypto.DecryptedMsgWithPubKey)2 BroadcastHandler (io.bitsquare.p2p.peers.BroadcastHandler)2 HashMapChangedListener (io.bitsquare.p2p.storage.HashMapChangedListener)2 AddDataMessage (io.bitsquare.p2p.storage.messages.AddDataMessage)2 BroadcastMessage (io.bitsquare.p2p.storage.messages.BroadcastMessage)2 RefreshTTLMessage (io.bitsquare.p2p.storage.messages.RefreshTTLMessage)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 Clock (io.bitsquare.common.Clock)1