Search in sources :

Example 1 with InvalidKeyException

use of org.whispersystems.libsignal.InvalidKeyException in project Signal-Android by WhisperSystems.

the class PreKeyUtil method generateSignedPreKey.

public static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
    try {
        SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context);
        int signedPreKeyId = getNextSignedPreKeyId(context);
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(signedPreKeyId, System.currentTimeMillis(), keyPair, signature);
        signedPreKeyStore.storeSignedPreKey(signedPreKeyId, record);
        setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);
        if (active) {
            setActiveSignedPreKeyId(context, signedPreKeyId);
        }
        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : SignedPreKeyStore(org.whispersystems.libsignal.state.SignedPreKeyStore) TextSecurePreKeyStore(org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore) ECKeyPair(org.whispersystems.libsignal.ecc.ECKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignedPreKeyRecord(org.whispersystems.libsignal.state.SignedPreKeyRecord)

Example 2 with InvalidKeyException

use of org.whispersystems.libsignal.InvalidKeyException in project Signal-Android by WhisperSystems.

the class IdentityKeyUtil method getLegacyIdentityKeyPair.

private static IdentityKeyPair getLegacyIdentityKeyPair(@NonNull Context context, @NonNull MasterSecret masterSecret) {
    try {
        MasterCipher masterCipher = new MasterCipher(masterSecret);
        byte[] publicKeyBytes = Base64.decode(retrieve(context, IDENTITY_PUBLIC_KEY_CIPHERTEXT_LEGACY_PREF));
        IdentityKey identityKey = new IdentityKey(publicKeyBytes, 0);
        ECPrivateKey privateKey = masterCipher.decryptKey(Base64.decode(retrieve(context, IDENTITY_PRIVATE_KEY_CIPHERTEXT_LEGACY_PREF)));
        return new IdentityKeyPair(identityKey, privateKey);
    } catch (IOException | InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) IdentityKey(org.whispersystems.libsignal.IdentityKey) IOException(java.io.IOException) IdentityKeyPair(org.whispersystems.libsignal.IdentityKeyPair) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 3 with InvalidKeyException

use of org.whispersystems.libsignal.InvalidKeyException in project Signal-Android by WhisperSystems.

the class PushDecryptJob method handleMessage.

private void handleMessage(MasterSecretUnion masterSecret, SignalServiceEnvelope envelope, Optional<Long> smsMessageId) {
    try {
        GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
        SignalProtocolStore axolotlStore = new SignalProtocolStoreImpl(context);
        SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
        SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore);
        SignalServiceContent content = cipher.decrypt(envelope);
        if (content.getDataMessage().isPresent()) {
            SignalServiceDataMessage message = content.getDataMessage().get();
            if (message.isEndSession())
                handleEndSessionMessage(masterSecret, envelope, message, smsMessageId);
            else if (message.isGroupUpdate())
                handleGroupMessage(masterSecret, envelope, message, smsMessageId);
            else if (message.isExpirationUpdate())
                handleExpirationUpdate(masterSecret, envelope, message, smsMessageId);
            else if (message.getAttachments().isPresent())
                handleMediaMessage(masterSecret, envelope, message, smsMessageId);
            else
                handleTextMessage(masterSecret, envelope, message, smsMessageId);
            if (message.getGroupInfo().isPresent() && groupDatabase.isUnknownGroup(message.getGroupInfo().get().getGroupId())) {
                handleUnknownGroupMessage(envelope, message.getGroupInfo().get());
            }
        } else if (content.getSyncMessage().isPresent()) {
            SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
            if (syncMessage.getSent().isPresent())
                handleSynchronizeSentMessage(masterSecret, envelope, syncMessage.getSent().get(), smsMessageId);
            else if (syncMessage.getRequest().isPresent())
                handleSynchronizeRequestMessage(masterSecret, syncMessage.getRequest().get());
            else if (syncMessage.getRead().isPresent())
                handleSynchronizeReadMessage(masterSecret, syncMessage.getRead().get(), envelope.getTimestamp());
            else
                Log.w(TAG, "Contains no known sync types...");
        } else if (content.getCallMessage().isPresent()) {
            Log.w(TAG, "Got call message...");
            SignalServiceCallMessage message = content.getCallMessage().get();
            if (message.getOfferMessage().isPresent())
                handleCallOfferMessage(envelope, message.getOfferMessage().get(), smsMessageId);
            else if (message.getAnswerMessage().isPresent())
                handleCallAnswerMessage(envelope, message.getAnswerMessage().get());
            else if (message.getIceUpdateMessages().isPresent())
                handleCallIceUpdateMessage(envelope, message.getIceUpdateMessages().get());
            else if (message.getHangupMessage().isPresent())
                handleCallHangupMessage(envelope, message.getHangupMessage().get(), smsMessageId);
        } else {
            Log.w(TAG, "Got unrecognized message...");
        }
        if (envelope.isPreKeySignalMessage()) {
            ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
        }
    } catch (InvalidVersionException e) {
        Log.w(TAG, e);
        handleInvalidVersionMessage(masterSecret, envelope, smsMessageId);
    } catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
        Log.w(TAG, e);
        handleCorruptMessage(masterSecret, envelope, smsMessageId);
    } catch (NoSessionException e) {
        Log.w(TAG, e);
        handleNoSessionMessage(masterSecret, envelope, smsMessageId);
    } catch (LegacyMessageException e) {
        Log.w(TAG, e);
        handleLegacyMessage(masterSecret, envelope, smsMessageId);
    } catch (DuplicateMessageException e) {
        Log.w(TAG, e);
        handleDuplicateMessage(masterSecret, envelope, smsMessageId);
    } catch (UntrustedIdentityException e) {
        Log.w(TAG, e);
        handleUntrustedIdentityMessage(masterSecret, envelope, smsMessageId);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) UntrustedIdentityException(org.whispersystems.libsignal.UntrustedIdentityException) SignalServiceCipher(org.whispersystems.signalservice.api.crypto.SignalServiceCipher) InvalidVersionException(org.whispersystems.libsignal.InvalidVersionException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) SignalServiceSyncMessage(org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage) SignalServiceContent(org.whispersystems.signalservice.api.messages.SignalServiceContent) NoSessionException(org.whispersystems.libsignal.NoSessionException) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalProtocolStore(org.whispersystems.libsignal.state.SignalProtocolStore) MmsException(ws.com.google.android.mms.MmsException) DuplicateMessageException(org.whispersystems.libsignal.DuplicateMessageException) SignalProtocolStoreImpl(org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) SignalServiceCallMessage(org.whispersystems.signalservice.api.messages.calls.SignalServiceCallMessage) InvalidKeyIdException(org.whispersystems.libsignal.InvalidKeyIdException) LegacyMessageException(org.whispersystems.libsignal.LegacyMessageException)

Example 4 with InvalidKeyException

use of org.whispersystems.libsignal.InvalidKeyException in project Signal-Android by WhisperSystems.

the class MasterSecretUtil method getAsymmetricMasterSecret.

public static AsymmetricMasterSecret getAsymmetricMasterSecret(@NonNull Context context, @Nullable MasterSecret masterSecret) {
    try {
        byte[] djbPublicBytes = retrieve(context, ASYMMETRIC_LOCAL_PUBLIC_DJB);
        byte[] djbPrivateBytes = retrieve(context, ASYMMETRIC_LOCAL_PRIVATE_DJB);
        ECPublicKey djbPublicKey = null;
        ECPrivateKey djbPrivateKey = null;
        if (djbPublicBytes != null) {
            djbPublicKey = Curve.decodePoint(djbPublicBytes, 0);
        }
        if (masterSecret != null) {
            MasterCipher masterCipher = new MasterCipher(masterSecret);
            if (djbPrivateBytes != null) {
                djbPrivateKey = masterCipher.decryptKey(djbPrivateBytes);
            }
        }
        return new AsymmetricMasterSecret(djbPublicKey, djbPrivateKey);
    } catch (InvalidKeyException | IOException ike) {
        throw new AssertionError(ike);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) IOException(java.io.IOException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Example 5 with InvalidKeyException

use of org.whispersystems.libsignal.InvalidKeyException in project Signal-Android by WhisperSystems.

the class AsymmetricMasterCipher method decryptBytes.

public byte[] decryptBytes(byte[] combined) throws IOException, InvalidMessageException {
    try {
        byte[][] parts = Util.split(combined, PublicKey.KEY_SIZE, combined.length - PublicKey.KEY_SIZE);
        PublicKey theirPublicKey = new PublicKey(parts[0], 0);
        ECPrivateKey ourPrivateKey = asymmetricMasterSecret.getPrivateKey();
        byte[] secret = Curve.calculateAgreement(theirPublicKey.getKey(), ourPrivateKey);
        MasterCipher masterCipher = getMasterCipherForSecret(secret);
        return masterCipher.decryptBytes(parts[1]);
    } catch (InvalidKeyException e) {
        throw new InvalidMessageException(e);
    }
}
Also used : ECPrivateKey(org.whispersystems.libsignal.ecc.ECPrivateKey) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) ECPublicKey(org.whispersystems.libsignal.ecc.ECPublicKey) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException)

Aggregations

InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)7 IOException (java.io.IOException)3 ECPrivateKey (org.whispersystems.libsignal.ecc.ECPrivateKey)3 ECPublicKey (org.whispersystems.libsignal.ecc.ECPublicKey)3 IdentityKey (org.whispersystems.libsignal.IdentityKey)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 ECKeyPair (org.whispersystems.libsignal.ecc.ECKeyPair)2 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 SignalProtocolStoreImpl (org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl)1 TextSecurePreKeyStore (org.thoughtcrime.securesms.crypto.storage.TextSecurePreKeyStore)1 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)1 DuplicateMessageException (org.whispersystems.libsignal.DuplicateMessageException)1 IdentityKeyPair (org.whispersystems.libsignal.IdentityKeyPair)1 InvalidKeyIdException (org.whispersystems.libsignal.InvalidKeyIdException)1 InvalidVersionException (org.whispersystems.libsignal.InvalidVersionException)1 LegacyMessageException (org.whispersystems.libsignal.LegacyMessageException)1 NoSessionException (org.whispersystems.libsignal.NoSessionException)1 UntrustedIdentityException (org.whispersystems.libsignal.UntrustedIdentityException)1 SignalProtocolStore (org.whispersystems.libsignal.state.SignalProtocolStore)1