Search in sources :

Example 31 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class CustomPayloadEncryptionIT method payloadDecryptionFailsOnBadMessage.

@Test
public void payloadDecryptionFailsOnBadMessage() {
    final Party sender = partyHelper.findByAlias(NodeAlias.A);
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(Base64.getEncoder().encode("Test Payload".getBytes()));
    Response result = sender.getRestClientWebTarget().path("/encodedpayload/create").request().post(Entity.entity(sendRequest, mediaType));
    assertThat(result.getStatus()).isEqualTo(200);
    final PayloadEncryptResponse payloadEncryptResponse = result.readEntity(PayloadEncryptResponse.class);
    // edit the cipher text to something rubbish, so it can't be decrypted
    payloadEncryptResponse.setCipherText("Unexpected data".getBytes());
    // attempt to decrypt it
    final Response decryptResultForSender = sender.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
    assertThat(decryptResultForSender.getStatus()).isEqualTo(500);
}
Also used : PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Test(org.junit.Test)

Example 32 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class CustomPayloadEncryptionIT method createAndDecryptPayload.

@Test
public void createAndDecryptPayload() {
    final Party sender = partyHelper.findByAlias(NodeAlias.A);
    final Party recipient = partyHelper.findByAlias(NodeAlias.B);
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(Base64.getEncoder().encode("Test Payload".getBytes()));
    sendRequest.setTo(recipient.getPublicKey());
    final Response encryptResult = sender.getRestClientWebTarget().path("/encodedpayload/create").request().post(Entity.entity(sendRequest, mediaType));
    assertThat(encryptResult.getStatus()).isEqualTo(200);
    final PayloadEncryptResponse payloadEncryptResponse = encryptResult.readEntity(PayloadEncryptResponse.class);
    // decrypt it again with the sender
    final Response decryptResultForSender = sender.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
    final ReceiveResponse decryptedPayload = decryptResultForSender.readEntity(ReceiveResponse.class);
    assertThat(Base64.getDecoder().decode(decryptedPayload.getPayload())).isEqualTo("Test Payload".getBytes());
    // decrypt it using the recipient
    final String firstRecipientInList = Base64.getEncoder().encodeToString(payloadEncryptResponse.getRecipientKeys().get(0));
    if (Objects.equals(firstRecipientInList, sender.getPublicKey())) {
        payloadEncryptResponse.getRecipientBoxes().remove(0);
    } else {
        payloadEncryptResponse.getRecipientBoxes().remove(1);
    }
    payloadEncryptResponse.setRecipientKeys(Collections.emptyList());
    final Response decryptResultForRecipient = recipient.getRestClientWebTarget().path("/encodedpayload/decrypt").request().post(Entity.entity(payloadEncryptResponse, mediaType));
    final ReceiveResponse decryptedPayloadForRecipient = decryptResultForRecipient.readEntity(ReceiveResponse.class);
    assertThat(Base64.getDecoder().decode(decryptedPayloadForRecipient.getPayload())).isEqualTo("Test Payload".getBytes());
}
Also used : PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) Response(jakarta.ws.rs.core.Response) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Party(com.quorum.tessera.test.Party) SendRequest(com.quorum.tessera.api.SendRequest) PayloadEncryptResponse(com.quorum.tessera.api.PayloadEncryptResponse) ReceiveResponse(com.quorum.tessera.api.ReceiveResponse) Test(org.junit.Test)

Example 33 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class RequestPrivacyValidatorTest method testPrivacyValidationOnSendRequest.

@Test
public void testPrivacyValidationOnSendRequest() {
    SendRequest request = new SendRequest();
    request.setPrivacyFlag(-1000);
    assertThat(validator.isValid(request, context)).isTrue();
    request.setPrivacyFlag(0);
    assertThat(validator.isValid(request, context)).isTrue();
    request.setPrivacyFlag(1);
    assertThat(validator.isValid(request, context)).isTrue();
    request.setPrivacyFlag(2);
    assertThat(validator.isValid(request, context)).isTrue();
    request.setPrivacyFlag(3);
    assertThat(validator.isValid(request, context)).isFalse();
    verify(context).buildConstraintViolationWithTemplate("Exec hash missing");
    verify(builder).addConstraintViolation();
    request.setExecHash("execHash");
    assertThat(validator.isValid(request, context)).isTrue();
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) Test(org.junit.Test)

Example 34 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class RequestPrivacyValidator method isValid.

@Override
public boolean isValid(Object request, ConstraintValidatorContext context) {
    PrivacyMode privacyMode;
    String execHash;
    if (request instanceof SendRequest) {
        privacyMode = PrivacyMode.fromFlag(((SendRequest) request).getPrivacyFlag());
        execHash = ((SendRequest) request).getExecHash();
    } else if (request instanceof SendSignedRequest) {
        privacyMode = PrivacyMode.fromFlag(((SendSignedRequest) request).getPrivacyFlag());
        execHash = ((SendSignedRequest) request).getExecHash();
    } else {
        context.buildConstraintViolationWithTemplate("Invalid usage. This validator can only be apply to SendRequest or SendSignedRequest").addConstraintViolation();
        return false;
    }
    if (PrivacyMode.PRIVATE_STATE_VALIDATION == privacyMode) {
        if (Objects.isNull(execHash) || execHash.length() == 0) {
            context.buildConstraintViolationWithTemplate("Exec hash missing").addConstraintViolation();
            return false;
        }
    }
    return true;
}
Also used : SendRequest(com.quorum.tessera.api.SendRequest) PrivacyMode(com.quorum.tessera.enclave.PrivacyMode) SendSignedRequest(com.quorum.tessera.api.SendSignedRequest)

Example 35 with SendRequest

use of com.quorum.tessera.api.SendRequest in project tessera by ConsenSys.

the class BesuTransactionResourceTest method send.

@Test
public void send() {
    final Base64.Encoder base64Encoder = Base64.getEncoder();
    final String base64Key = "BULeR8JyUWhiuuCMU/HLA0Q5pzkYT+cHII3ZKBey3Bo=";
    final SendRequest sendRequest = new SendRequest();
    sendRequest.setPayload(base64Encoder.encode("PAYLOAD".getBytes()));
    sendRequest.setTo(base64Key);
    final PublicKey sender = mock(PublicKey.class);
    when(transactionManager.defaultPublicKey()).thenReturn(sender);
    final byte[] recipientKeyBytes = Base64.getDecoder().decode(base64Key);
    final com.quorum.tessera.transaction.SendResponse sendResponse = mock(com.quorum.tessera.transaction.SendResponse.class);
    final MessageHash messageHash = mock(MessageHash.class);
    final byte[] txnData = "TxnData".getBytes();
    when(messageHash.getHashBytes()).thenReturn(txnData);
    when(sendResponse.getTransactionHash()).thenReturn(messageHash);
    when(transactionManager.send(any(com.quorum.tessera.transaction.SendRequest.class))).thenReturn(sendResponse);
    PrivacyGroup legacy = mock(PrivacyGroup.class);
    when(legacy.getId()).thenReturn(PrivacyGroup.Id.fromBytes("group".getBytes()));
    when(privacyGroupManager.createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))))).thenReturn(legacy);
    final Response result = besuTransactionResource.send(sendRequest);
    // jersey.target("send").request().post(Entity.entity(sendRequest,
    // MediaType.APPLICATION_JSON));
    assertThat(result.getStatus()).isEqualTo(200);
    assertThat(result.getLocation().getPath()).isEqualTo("transaction/" + base64Encoder.encodeToString(txnData));
    SendResponse resultSendResponse = (SendResponse) result.getEntity();
    assertThat(resultSendResponse.getKey()).isEqualTo(Base64.getEncoder().encodeToString(txnData));
    ArgumentCaptor<com.quorum.tessera.transaction.SendRequest> argumentCaptor = ArgumentCaptor.forClass(com.quorum.tessera.transaction.SendRequest.class);
    verify(transactionManager).send(argumentCaptor.capture());
    verify(transactionManager).defaultPublicKey();
    verify(privacyGroupManager).createLegacyPrivacyGroup(eq(sender), eq(List.of(PublicKey.from(recipientKeyBytes))));
    com.quorum.tessera.transaction.SendRequest businessObject = argumentCaptor.getValue();
    assertThat(businessObject).isNotNull();
    assertThat(businessObject.getPayload()).isEqualTo(sendRequest.getPayload());
    assertThat(businessObject.getSender()).isEqualTo(sender);
    assertThat(businessObject.getRecipients()).hasSize(1);
    assertThat(businessObject.getRecipients().get(0).encodeToBase64()).isEqualTo(base64Key);
    assertThat(businessObject.getPrivacyMode()).isEqualTo(PrivacyMode.STANDARD_PRIVATE);
    assertThat(businessObject.getAffectedContractTransactions()).isEmpty();
    assertThat(businessObject.getExecHash()).isEmpty();
    assertThat(businessObject.getPrivacyGroupId()).isPresent().get().isEqualTo(PrivacyGroup.Id.fromBytes("group".getBytes()));
}
Also used : Base64(java.util.Base64) SendRequest(com.quorum.tessera.api.SendRequest) PublicKey(com.quorum.tessera.encryption.PublicKey) SendResponse(com.quorum.tessera.api.SendResponse) MessageHash(com.quorum.tessera.data.MessageHash) PrivacyGroup(com.quorum.tessera.enclave.PrivacyGroup) SendResponse(com.quorum.tessera.api.SendResponse) BesuReceiveResponse(com.quorum.tessera.api.BesuReceiveResponse) ReceiveResponse(com.quorum.tessera.transaction.ReceiveResponse) Response(jakarta.ws.rs.core.Response) Test(org.junit.Test)

Aggregations

SendRequest (com.quorum.tessera.api.SendRequest)64 Response (jakarta.ws.rs.core.Response)60 SendResponse (com.quorum.tessera.api.SendResponse)50 Party (com.quorum.tessera.test.Party)43 Test (org.junit.Test)43 ReceiveResponse (com.quorum.tessera.api.ReceiveResponse)27 MessageHash (com.quorum.tessera.data.MessageHash)13 PublicKey (com.quorum.tessera.encryption.PublicKey)13 URI (java.net.URI)13 RestUtils (com.quorum.tessera.test.rest.RestUtils)11 PayloadEncryptResponse (com.quorum.tessera.api.PayloadEncryptResponse)8 ReceiveResponse (com.quorum.tessera.transaction.ReceiveResponse)8 Stream (java.util.stream.Stream)6 PrivacyGroup (com.quorum.tessera.enclave.PrivacyGroup)5 PrivacyMode (com.quorum.tessera.enclave.PrivacyMode)5 MIME_TYPE_JSON_2_1 (com.quorum.tessera.version.MultiTenancyVersion.MIME_TYPE_JSON_2_1)5 TransactionManager (com.quorum.tessera.transaction.TransactionManager)4 Operation (io.swagger.v3.oas.annotations.Operation)4 PartyHelper (com.quorum.tessera.test.PartyHelper)3 Content (io.swagger.v3.oas.annotations.media.Content)3