Search in sources :

Example 1 with RecipientKeyNotFoundException

use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException in project tessera by ConsenSys.

the class TransactionManagerTest method receiveNoRecipientKeyFound.

@Test
public void receiveNoRecipientKeyFound() {
    PublicKey recipient = PublicKey.from("recipient".getBytes());
    MessageHash messageHash = mock(MessageHash.class);
    when(messageHash.getHashBytes()).thenReturn("KEY".getBytes());
    ReceiveRequest receiveRequest = mock(ReceiveRequest.class);
    when(receiveRequest.getTransactionHash()).thenReturn(messageHash);
    when(receiveRequest.getRecipient()).thenReturn(Optional.of(recipient));
    EncodedPayload payload = mock(EncodedPayload.class);
    EncryptedTransaction encryptedTransaction = mock(EncryptedTransaction.class);
    when(encryptedTransaction.getPayload()).thenReturn(payload);
    when(encryptedTransaction.getHash()).thenReturn(messageHash);
    when(encryptedTransactionDAO.retrieveByHash(eq(messageHash))).thenReturn(Optional.of(encryptedTransaction));
    PublicKey publicKey = mock(PublicKey.class);
    when(enclave.getPublicKeys()).thenReturn(Collections.singleton(publicKey));
    when(enclave.unencryptTransaction(eq(payload), any(PublicKey.class))).thenThrow(EncryptorException.class);
    try {
        transactionManager.receive(receiveRequest);
        failBecauseExceptionWasNotThrown(RecipientKeyNotFoundException.class);
    } catch (RecipientKeyNotFoundException ex) {
        verify(encryptedTransactionDAO).retrieveByHash(any(MessageHash.class));
        verify(enclave).getPublicKeys();
        verify(enclave).unencryptTransaction(any(EncodedPayload.class), any(PublicKey.class));
    }
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Test(org.junit.Test)

Example 2 with RecipientKeyNotFoundException

use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException in project tessera by ConsenSys.

the class EncodedPayloadManagerImpl method decrypt.

@Override
public ReceiveResponse decrypt(final EncodedPayload payload, final PublicKey maybeDefaultRecipient) {
    final MessageHash customPayloadHash = new MessageHash(payloadDigest.digest(payload.getCipherText()));
    LOGGER.debug("Decrypt request for custom message with hash {}", customPayloadHash);
    final PublicKey recipientKey = Optional.ofNullable(maybeDefaultRecipient).orElseGet(() -> searchForRecipientKey(payload).orElseThrow(() -> new RecipientKeyNotFoundException("No suitable recipient keys found to decrypt payload for " + customPayloadHash)));
    LOGGER.debug("Decryption key found: {}", recipientKey.encodeToBase64());
    final byte[] decryptedTransactionData = enclave.unencryptTransaction(payload, recipientKey);
    final Set<MessageHash> affectedTransaction = payload.getAffectedContractTransactions().keySet().stream().map(TxHash::getBytes).map(MessageHash::new).collect(Collectors.toSet());
    ReceiveResponse.Builder builder = ReceiveResponse.Builder.create().withUnencryptedTransactionData(decryptedTransactionData).withPrivacyMode(payload.getPrivacyMode()).withAffectedTransactions(affectedTransaction).withExecHash(payload.getExecHash()).withSender(payload.getSenderKey());
    payload.getPrivacyGroupId().ifPresent(builder::withPrivacyGroupId);
    return builder.build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)

Example 3 with RecipientKeyNotFoundException

use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException in project tessera by ConsenSys.

the class TransactionManagerImpl method receive.

@Override
public ReceiveResponse receive(ReceiveRequest request) {
    final MessageHash hash = request.getTransactionHash();
    LOGGER.info("Lookup transaction {}", hash);
    if (request.isRaw()) {
        final EncryptedRawTransaction encryptedRawTransaction = encryptedRawTransactionDAO.retrieveByHash(hash).orElseThrow(() -> new TransactionNotFoundException("Raw Message with hash " + hash + " was not found"));
        final PublicKey senderKey = PublicKey.from(encryptedRawTransaction.getSender());
        final RawTransaction rawTransaction = new RawTransaction(encryptedRawTransaction.getEncryptedPayload(), encryptedRawTransaction.getEncryptedKey(), new Nonce(encryptedRawTransaction.getNonce()), senderKey);
        final byte[] response = enclave.unencryptRawPayload(rawTransaction);
        return ReceiveResponse.Builder.create().withPrivacyMode(PrivacyMode.STANDARD_PRIVATE).withUnencryptedTransactionData(response).withManagedParties(Set.of(senderKey)).withSender(senderKey).build();
    }
    final EncryptedTransaction encryptedTransaction = encryptedTransactionDAO.retrieveByHash(hash).orElseThrow(() -> new TransactionNotFoundException("Message with hash " + hash + " was not found"));
    final EncodedPayload payload = Optional.of(encryptedTransaction).map(EncryptedTransaction::getPayload).orElseThrow(() -> new IllegalStateException("Unable to decode previously encoded payload"));
    PublicKey recipientKey = request.getRecipient().orElse(searchForRecipientKey(payload).orElseThrow(() -> new RecipientKeyNotFoundException("No suitable recipient keys found to decrypt payload for : " + hash)));
    byte[] unencryptedTransactionData = enclave.unencryptTransaction(payload, recipientKey);
    Set<MessageHash> affectedTransactions = payload.getAffectedContractTransactions().keySet().stream().map(TxHash::getBytes).map(MessageHash::new).collect(Collectors.toSet());
    Set<PublicKey> managedParties = new HashSet<>();
    if (payload.getRecipientKeys().isEmpty()) {
        // legacy tx
        for (RecipientBox box : payload.getRecipientBoxes()) {
            EncodedPayload singleBoxPayload = EncodedPayload.Builder.from(payload).withRecipientBoxes(List.of(box.getData())).build();
            Optional<PublicKey> possibleRecipient = searchForRecipientKey(singleBoxPayload);
            possibleRecipient.ifPresent(managedParties::add);
        }
    } else {
        managedParties = enclave.getPublicKeys().stream().filter(payload.getRecipientKeys()::contains).collect(Collectors.toSet());
    }
    final ReceiveResponse.Builder responseBuilder = ReceiveResponse.Builder.create();
    payload.getPrivacyGroupId().ifPresent(responseBuilder::withPrivacyGroupId);
    return responseBuilder.withUnencryptedTransactionData(unencryptedTransactionData).withPrivacyMode(payload.getPrivacyMode()).withAffectedTransactions(affectedTransactions).withExecHash(payload.getExecHash()).withManagedParties(managedParties).withSender(payload.getSenderKey()).build();
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) TransactionNotFoundException(com.quorum.tessera.transaction.exception.TransactionNotFoundException) Nonce(com.quorum.tessera.encryption.Nonce) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)

Example 4 with RecipientKeyNotFoundException

use of com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException in project tessera by ConsenSys.

the class SearchRecipientKeyForPayload method execute.

@Override
public boolean execute(BatchWorkflowContext event) {
    final Set<EncodedPayload> encodedPayloads = event.getPayloadsToPublish();
    final boolean keysAreEmpty = encodedPayloads.stream().map(EncodedPayload::getRecipientKeys).allMatch(List::isEmpty);
    // if the payload has someone in it, then we are good
    if (!keysAreEmpty) {
        return true;
    }
    // the keys are not present, so we need to search for the relevant recipient
    final Set<EncodedPayload> adjustedPayloads = encodedPayloads.stream().map(payload -> {
        // this is a pre-PE tx, so find the recipient key
        final PublicKey recipientKey = searchForRecipientKey(payload).orElseThrow(() -> {
            final EncryptedTransaction encryptedTransaction = event.getEncryptedTransaction();
            final MessageHash hash = encryptedTransaction.getHash();
            final String message = String.format("No key found as recipient of message %s", hash);
            return new RecipientKeyNotFoundException(message);
        });
        return EncodedPayload.Builder.from(payload).withRecipientKeys(List.of(recipientKey)).build();
    }).collect(Collectors.toSet());
    event.setPayloadsToPublish(adjustedPayloads);
    return true;
}
Also used : PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) Collectors(java.util.stream.Collectors) EnclaveNotAvailableException(com.quorum.tessera.enclave.EnclaveNotAvailableException) Objects(java.util.Objects) EncryptorException(com.quorum.tessera.encryption.EncryptorException) List(java.util.List) Enclave(com.quorum.tessera.enclave.Enclave) Optional(java.util.Optional) MessageHash(com.quorum.tessera.data.MessageHash) PublicKey(com.quorum.tessera.encryption.PublicKey) EncodedPayload(com.quorum.tessera.enclave.EncodedPayload) List(java.util.List) MessageHash(com.quorum.tessera.data.MessageHash) RecipientKeyNotFoundException(com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException) EncryptedTransaction(com.quorum.tessera.data.EncryptedTransaction)

Aggregations

PublicKey (com.quorum.tessera.encryption.PublicKey)4 RecipientKeyNotFoundException (com.quorum.tessera.transaction.exception.RecipientKeyNotFoundException)4 MessageHash (com.quorum.tessera.data.MessageHash)2 EncryptedTransaction (com.quorum.tessera.data.EncryptedTransaction)1 Enclave (com.quorum.tessera.enclave.Enclave)1 EnclaveNotAvailableException (com.quorum.tessera.enclave.EnclaveNotAvailableException)1 EncodedPayload (com.quorum.tessera.enclave.EncodedPayload)1 EncryptorException (com.quorum.tessera.encryption.EncryptorException)1 Nonce (com.quorum.tessera.encryption.Nonce)1 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)1 TransactionNotFoundException (com.quorum.tessera.transaction.exception.TransactionNotFoundException)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1