Search in sources :

Example 6 with PushTransportDetails

use of org.whispersystems.signalservice.internal.push.PushTransportDetails in project Signal-Android by WhisperSystems.

the class SignalServiceCipher method encryptForGroup.

public byte[] encryptForGroup(DistributionId distributionId, List<SignalProtocolAddress> destinations, SenderCertificate senderCertificate, byte[] unpaddedMessage, ContentHint contentHint, byte[] groupId) throws NoSessionException, UntrustedIdentityException, InvalidKeyException, InvalidRegistrationIdException {
    PushTransportDetails transport = new PushTransportDetails();
    SignalProtocolAddress localProtocolAddress = new SignalProtocolAddress(localAddress.getIdentifier(), localDeviceId);
    SignalGroupCipher groupCipher = new SignalGroupCipher(sessionLock, new GroupCipher(signalProtocolStore, localProtocolAddress));
    SignalSealedSessionCipher sessionCipher = new SignalSealedSessionCipher(sessionLock, new SealedSessionCipher(signalProtocolStore, localAddress.getServiceId().uuid(), localAddress.getNumber().orNull(), localDeviceId));
    CiphertextMessage message = groupCipher.encrypt(distributionId.asUuid(), transport.getPaddedMessageBody(unpaddedMessage));
    UnidentifiedSenderMessageContent messageContent = new UnidentifiedSenderMessageContent(message, senderCertificate, contentHint.getType(), Optional.of(groupId));
    return sessionCipher.multiRecipientEncrypt(destinations, messageContent);
}
Also used : SealedSessionCipher(org.signal.libsignal.metadata.SealedSessionCipher) CiphertextMessage(org.whispersystems.libsignal.protocol.CiphertextMessage) PushTransportDetails(org.whispersystems.signalservice.internal.push.PushTransportDetails) GroupCipher(org.whispersystems.libsignal.groups.GroupCipher) UnidentifiedSenderMessageContent(org.signal.libsignal.metadata.protocol.UnidentifiedSenderMessageContent) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress)

Example 7 with PushTransportDetails

use of org.whispersystems.signalservice.internal.push.PushTransportDetails in project libsignal-service-java by signalapp.

the class SignalServiceCipher method decrypt.

private Plaintext decrypt(SignalServiceEnvelope envelope, byte[] ciphertext) throws InvalidMetadataMessageException, InvalidMetadataVersionException, ProtocolDuplicateMessageException, ProtocolUntrustedIdentityException, ProtocolLegacyMessageException, ProtocolInvalidKeyException, ProtocolInvalidVersionException, ProtocolInvalidMessageException, ProtocolInvalidKeyIdException, ProtocolNoSessionException, SelfSendException {
    try {
        byte[] paddedMessage;
        Metadata metadata;
        int sessionVersion;
        if (!envelope.hasSource() && !envelope.isUnidentifiedSender()) {
            throw new ProtocolInvalidMessageException(new InvalidMessageException("Non-UD envelope is missing a source!"), null, 0);
        }
        if (envelope.isPreKeySignalMessage()) {
            SignalProtocolAddress sourceAddress = getPreferredProtocolAddress(signalProtocolStore, envelope.getSourceAddress(), envelope.getSourceDevice());
            SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress);
            paddedMessage = sessionCipher.decrypt(new PreKeySignalMessage(ciphertext));
            metadata = new Metadata(envelope.getSourceAddress(), envelope.getSourceDevice(), envelope.getTimestamp(), false);
            sessionVersion = sessionCipher.getSessionVersion();
        } else if (envelope.isSignalMessage()) {
            SignalProtocolAddress sourceAddress = getPreferredProtocolAddress(signalProtocolStore, envelope.getSourceAddress(), envelope.getSourceDevice());
            SessionCipher sessionCipher = new SessionCipher(signalProtocolStore, sourceAddress);
            paddedMessage = sessionCipher.decrypt(new SignalMessage(ciphertext));
            metadata = new Metadata(envelope.getSourceAddress(), envelope.getSourceDevice(), envelope.getTimestamp(), false);
            sessionVersion = sessionCipher.getSessionVersion();
        } else if (envelope.isUnidentifiedSender()) {
            SealedSessionCipher sealedSessionCipher = new SealedSessionCipher(signalProtocolStore, localAddress.getUuid().orNull(), localAddress.getNumber().orNull(), 1);
            DecryptionResult result = sealedSessionCipher.decrypt(certificateValidator, ciphertext, envelope.getServerTimestamp());
            SignalServiceAddress resultAddress = new SignalServiceAddress(UuidUtil.parse(result.getSenderUuid().orNull()), result.getSenderE164());
            SignalProtocolAddress protocolAddress = getPreferredProtocolAddress(signalProtocolStore, resultAddress, result.getDeviceId());
            paddedMessage = result.getPaddedMessage();
            metadata = new Metadata(resultAddress, result.getDeviceId(), envelope.getTimestamp(), true);
            sessionVersion = sealedSessionCipher.getSessionVersion(protocolAddress);
        } else {
            throw new InvalidMetadataMessageException("Unknown type: " + envelope.getType());
        }
        PushTransportDetails transportDetails = new PushTransportDetails(sessionVersion);
        byte[] data = transportDetails.getStrippedPaddingMessageBody(paddedMessage);
        return new Plaintext(metadata, data);
    } catch (DuplicateMessageException e) {
        throw new ProtocolDuplicateMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (LegacyMessageException e) {
        throw new ProtocolLegacyMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidMessageException e) {
        throw new ProtocolInvalidMessageException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidKeyIdException e) {
        throw new ProtocolInvalidKeyIdException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidKeyException e) {
        throw new ProtocolInvalidKeyException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (UntrustedIdentityException e) {
        throw new ProtocolUntrustedIdentityException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (InvalidVersionException e) {
        throw new ProtocolInvalidVersionException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    } catch (NoSessionException e) {
        throw new ProtocolNoSessionException(e, envelope.getSourceIdentifier(), envelope.getSourceDevice());
    }
}
Also used : ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) SealedSessionCipher(org.signal.libsignal.metadata.SealedSessionCipher) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ProtocolInvalidMessageException(org.signal.libsignal.metadata.ProtocolInvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolUntrustedIdentityException(org.signal.libsignal.metadata.ProtocolUntrustedIdentityException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) ProtocolInvalidVersionException(org.signal.libsignal.metadata.ProtocolInvalidVersionException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) NoSessionException(org.whispersystems.libsignal.NoSessionException) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) PushTransportDetails(org.whispersystems.signalservice.internal.push.PushTransportDetails) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException) SessionCipher(org.whispersystems.libsignal.SessionCipher) SealedSessionCipher(org.signal.libsignal.metadata.SealedSessionCipher) SignalProtocolAddress(org.whispersystems.libsignal.SignalProtocolAddress) ProtocolNoSessionException(org.signal.libsignal.metadata.ProtocolNoSessionException) PreKeySignalMessage(org.whispersystems.libsignal.protocol.PreKeySignalMessage) SignalMessage(org.whispersystems.libsignal.protocol.SignalMessage) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) InvalidMetadataMessageException(org.signal.libsignal.metadata.InvalidMetadataMessageException) ProtocolInvalidKeyException(org.signal.libsignal.metadata.ProtocolInvalidKeyException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) ProtocolDuplicateMessageException(org.signal.libsignal.metadata.ProtocolDuplicateMessageException) DecryptionResult(org.signal.libsignal.metadata.SealedSessionCipher.DecryptionResult) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) ProtocolInvalidKeyIdException(org.signal.libsignal.metadata.ProtocolInvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException) ProtocolLegacyMessageException(org.signal.libsignal.metadata.ProtocolLegacyMessageException)

Aggregations

PushTransportDetails (org.whispersystems.signalservice.internal.push.PushTransportDetails)7 SealedSessionCipher (org.signal.libsignal.metadata.SealedSessionCipher)6 SessionCipher (org.whispersystems.libsignal.SessionCipher)5 SignalProtocolAddress (org.whispersystems.libsignal.SignalProtocolAddress)5 CiphertextMessage (org.whispersystems.libsignal.protocol.CiphertextMessage)4 InvalidMetadataMessageException (org.signal.libsignal.metadata.InvalidMetadataMessageException)3 ProtocolDuplicateMessageException (org.signal.libsignal.metadata.ProtocolDuplicateMessageException)3 ProtocolInvalidKeyException (org.signal.libsignal.metadata.ProtocolInvalidKeyException)3 ProtocolInvalidKeyIdException (org.signal.libsignal.metadata.ProtocolInvalidKeyIdException)3 ProtocolInvalidMessageException (org.signal.libsignal.metadata.ProtocolInvalidMessageException)3 ProtocolInvalidVersionException (org.signal.libsignal.metadata.ProtocolInvalidVersionException)3 ProtocolLegacyMessageException (org.signal.libsignal.metadata.ProtocolLegacyMessageException)3 ProtocolNoSessionException (org.signal.libsignal.metadata.ProtocolNoSessionException)3 ProtocolUntrustedIdentityException (org.signal.libsignal.metadata.ProtocolUntrustedIdentityException)3 DecryptionResult (org.signal.libsignal.metadata.SealedSessionCipher.DecryptionResult)3 DuplicateMessageException (org.whispersystems.libsignal.DuplicateMessageException)3 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)3 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)3 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)3 InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)3