Java Code Examples for org.whispersystems.libsignal.protocol.CiphertextMessage#PREKEY_TYPE

The following examples show how to use org.whispersystems.libsignal.protocol.CiphertextMessage#PREKEY_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public OutgoingPushMessage encrypt(SignalProtocolAddress destination, byte[] unpaddedMessage, PushPurpose pushPurpose)
    throws UntrustedIdentityException
{
  SessionCipher        sessionCipher        = new SessionCipher(signalProtocolStore, destination);
  PushTransportDetails transportDetails     = new PushTransportDetails(sessionCipher.getSessionVersion());
  CiphertextMessage    message              = sessionCipher.encrypt(transportDetails.getPaddedMessageBody(unpaddedMessage));
  int                  remoteRegistrationId = sessionCipher.getRemoteRegistrationId();
  String               body                 = Base64.encodeBytes(message.serialize());

  int type;

  switch (message.getType()) {
    case CiphertextMessage.PREKEY_TYPE:  type = Type.PREKEY_BUNDLE_VALUE; break;
    case CiphertextMessage.WHISPER_TYPE: type = Type.CIPHERTEXT_VALUE;    break;
    default: throw new AssertionError("Bad type: " + message.getType());
  }

  return new OutgoingPushMessage(type, destination.getDeviceId(), remoteRegistrationId, body, pushPurpose);
}
 
Example 2
Source Project: Silence   File: SmsCipher.java    License: GNU General Public License v3.0 6 votes vote down vote up
public OutgoingTextMessage encrypt(OutgoingTextMessage message)
  throws NoSessionException, UntrustedIdentityException
{
  byte[] paddedBody      = transportDetails.getPaddedMessageBody(message.getMessageBody().getBytes());
  String recipientNumber = message.getRecipients().getPrimaryRecipient().getNumber();

  if (!signalProtocolStore.containsSession(new SignalProtocolAddress(recipientNumber, 1))) {
    throw new NoSessionException("No session for: " + recipientNumber);
  }

  SessionCipher     cipher            = new SessionCipher(signalProtocolStore, new SignalProtocolAddress(recipientNumber, 1));
  CiphertextMessage ciphertextMessage = cipher.encrypt(paddedBody);
  String            encodedCiphertext = new String(transportDetails.getEncodedMessage(ciphertextMessage.serialize()));

  if (ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE) {
    return new OutgoingPrekeyBundleMessage(message, encodedCiphertext);
  } else {
    return message.withBody(encodedCiphertext);
  }
}
 
Example 3
@Nullable
public AxolotlKey processSending(@NonNull byte[] outgoingMessage, boolean ignoreSessionTrust) {
    FingerprintStatus status = getTrust();
    if (ignoreSessionTrust || status.isTrustedAndActive()) {
        try {
            CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
            return new AxolotlKey(getRemoteAddress().getDeviceId(), ciphertextMessage.serialize(), ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE);
        } catch (UntrustedIdentityException e) {
            return null;
        }
    } else {
        return null;
    }
}
 
Example 4
Source Project: Smack   File: SignalOmemoRatchet.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CiphertextTuple doubleRatchetEncrypt(OmemoDevice recipient, byte[] messageKey) {
    CiphertextMessage ciphertextMessage;
    try {
        ciphertextMessage = getCipher(recipient).encrypt(messageKey);
    } catch (UntrustedIdentityException e) {
        throw new AssertionError("Signals trust management MUST be disabled.");
    }

    int type = ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE ?
            OmemoElement.TYPE_OMEMO_PREKEY_MESSAGE : OmemoElement.TYPE_OMEMO_MESSAGE;

    return new CiphertextTuple(ciphertextMessage.serialize(), type);
}
 
Example 5
@Nullable
public AxolotlKey processSending(@NonNull byte[] outgoingMessage, boolean ignoreSessionTrust) {
	FingerprintStatus status = getTrust();
	if (ignoreSessionTrust || status.isTrustedAndActive()) {
		try {
			CiphertextMessage ciphertextMessage = cipher.encrypt(outgoingMessage);
			return new AxolotlKey(getRemoteAddress().getDeviceId(), ciphertextMessage.serialize(),ciphertextMessage.getType() == CiphertextMessage.PREKEY_TYPE);
		} catch (UntrustedIdentityException e) {
			return null;
		}
	} else {
		return null;
	}
}