Search in sources :

Example 1 with MailboxStoragePayload

use of io.bitsquare.p2p.storage.payload.MailboxStoragePayload 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 MailboxStoragePayload

use of io.bitsquare.p2p.storage.payload.MailboxStoragePayload 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)

Aggregations

CryptoException (io.bitsquare.common.crypto.CryptoException)2 MailboxStoragePayload (io.bitsquare.p2p.storage.payload.MailboxStoragePayload)2 DecryptedMsgWithPubKey (io.bitsquare.crypto.DecryptedMsgWithPubKey)1 ProtectedMailboxStorageEntry (io.bitsquare.p2p.storage.storageentry.ProtectedMailboxStorageEntry)1 PublicKey (java.security.PublicKey)1