Search in sources :

Example 1 with InvalidCiphertextException

use of org.whispersystems.signalservice.api.crypto.InvalidCiphertextException in project Signal-Android by WhisperSystems.

the class AESCipher method decrypt.

static byte[] decrypt(byte[] key, byte[] iv, byte[] ciphertext, byte[] tag) throws InvalidCiphertextException {
    try {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(TAG_LENGTH_BITS, iv));
        return cipher.doFinal(ByteUtil.combine(ciphertext, tag));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | IllegalBlockSizeException e) {
        throw new AssertionError(e);
    } catch (InvalidKeyException | BadPaddingException e) {
        throw new InvalidCiphertextException(e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) GCMParameterSpec(javax.crypto.spec.GCMParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 2 with InvalidCiphertextException

use of org.whispersystems.signalservice.api.crypto.InvalidCiphertextException in project Signal-Android by WhisperSystems.

the class RetrieveProfileJob method setProfileName.

private void setProfileName(Recipient recipient, String profileName) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.profileKeyOrNull(recipient.getProfileKey());
        if (profileKey == null)
            return;
        String plaintextProfileName = Util.emptyIfNull(ProfileUtil.decryptString(profileKey, profileName));
        ProfileName remoteProfileName = ProfileName.fromSerialized(plaintextProfileName);
        ProfileName localProfileName = recipient.getProfileName();
        if (!remoteProfileName.equals(localProfileName)) {
            Log.i(TAG, "Profile name updated. Writing new value.");
            SignalDatabase.recipients().setProfileName(recipient.getId(), remoteProfileName);
            String remoteDisplayName = remoteProfileName.toString();
            String localDisplayName = localProfileName.toString();
            if (!recipient.isBlocked() && !recipient.isGroup() && !recipient.isSelf() && !localDisplayName.isEmpty() && !remoteDisplayName.equals(localDisplayName)) {
                Log.i(TAG, "Writing a profile name change event for " + recipient.getId());
                SignalDatabase.sms().insertProfileNameChangeMessages(recipient, remoteDisplayName, localDisplayName);
            } else {
                Log.i(TAG, String.format(Locale.US, "Name changed, but wasn't relevant to write an event. blocked: %s, group: %s, self: %s, firstSet: %s, displayChange: %s", recipient.isBlocked(), recipient.isGroup(), recipient.isSelf(), localDisplayName.isEmpty(), !remoteDisplayName.equals(localDisplayName)));
            }
        }
        if (TextUtils.isEmpty(plaintextProfileName)) {
            Log.i(TAG, "No profile name set for " + recipient.getId());
        }
    } catch (InvalidCiphertextException e) {
        Log.w(TAG, "Bad profile key for " + recipient.getId());
    } catch (IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) ProfileName(org.thoughtcrime.securesms.profiles.ProfileName) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 3 with InvalidCiphertextException

use of org.whispersystems.signalservice.api.crypto.InvalidCiphertextException in project Signal-Android by WhisperSystems.

the class RefreshOwnProfileJob method setProfileName.

private void setProfileName(@Nullable String encryptedName) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.getSelfProfileKey();
        String plaintextName = ProfileUtil.decryptString(profileKey, encryptedName);
        ProfileName profileName = ProfileName.fromSerialized(plaintextName);
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextName) ? "non-" : "") + "empty name.");
        SignalDatabase.recipients().setProfileName(Recipient.self().getId(), profileName);
    } catch (InvalidCiphertextException | IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) ProfileName(org.thoughtcrime.securesms.profiles.ProfileName) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 4 with InvalidCiphertextException

use of org.whispersystems.signalservice.api.crypto.InvalidCiphertextException in project Signal-Android by WhisperSystems.

the class RefreshOwnProfileJob method setProfileAbout.

private void setProfileAbout(@Nullable String encryptedAbout, @Nullable String encryptedEmoji) {
    try {
        ProfileKey profileKey = ProfileKeyUtil.getSelfProfileKey();
        String plaintextAbout = ProfileUtil.decryptString(profileKey, encryptedAbout);
        String plaintextEmoji = ProfileUtil.decryptString(profileKey, encryptedEmoji);
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextAbout) ? "non-" : "") + "empty about.");
        Log.d(TAG, "Saving " + (!Util.isEmpty(plaintextEmoji) ? "non-" : "") + "empty emoji.");
        SignalDatabase.recipients().setAbout(Recipient.self().getId(), plaintextAbout, plaintextEmoji);
    } catch (InvalidCiphertextException | IOException e) {
        Log.w(TAG, e);
    }
}
Also used : InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) IOException(java.io.IOException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey)

Example 5 with InvalidCiphertextException

use of org.whispersystems.signalservice.api.crypto.InvalidCiphertextException in project Signal-Android by WhisperSystems.

the class ProfileUtil method getAddressForRecipient.

@WorkerThread
@NonNull
public static MobileCoinPublicAddress getAddressForRecipient(@NonNull Recipient recipient) throws IOException, PaymentsAddressException {
    ProfileKey profileKey;
    try {
        profileKey = getProfileKey(recipient);
    } catch (IOException e) {
        Log.w(TAG, "Profile key not available for " + recipient.getId());
        throw new PaymentsAddressException(PaymentsAddressException.Code.NO_PROFILE_KEY);
    }
    ProfileAndCredential profileAndCredential = ProfileUtil.retrieveProfileSync(ApplicationDependencies.getApplication(), recipient, SignalServiceProfile.RequestType.PROFILE);
    SignalServiceProfile profile = profileAndCredential.getProfile();
    byte[] encryptedPaymentsAddress = profile.getPaymentAddress();
    if (encryptedPaymentsAddress == null) {
        Log.w(TAG, "Payments not enabled for " + recipient.getId());
        throw new PaymentsAddressException(PaymentsAddressException.Code.NOT_ENABLED);
    }
    try {
        IdentityKey identityKey = new IdentityKey(Base64.decode(profileAndCredential.getProfile().getIdentityKey()), 0);
        ProfileCipher profileCipher = new ProfileCipher(profileKey);
        byte[] decrypted = profileCipher.decryptWithLength(encryptedPaymentsAddress);
        SignalServiceProtos.PaymentAddress paymentAddress = SignalServiceProtos.PaymentAddress.parseFrom(decrypted);
        byte[] bytes = MobileCoinPublicAddressProfileUtil.verifyPaymentsAddress(paymentAddress, identityKey);
        MobileCoinPublicAddress mobileCoinPublicAddress = MobileCoinPublicAddress.fromBytes(bytes);
        if (mobileCoinPublicAddress == null) {
            throw new PaymentsAddressException(PaymentsAddressException.Code.INVALID_ADDRESS);
        }
        return mobileCoinPublicAddress;
    } catch (InvalidCiphertextException | IOException e) {
        Log.w(TAG, "Could not decrypt payments address, ProfileKey may be outdated for " + recipient.getId(), e);
        throw new PaymentsAddressException(PaymentsAddressException.Code.COULD_NOT_DECRYPT);
    } catch (InvalidKeyException e) {
        Log.w(TAG, "Could not verify payments address due to bad identity key " + recipient.getId(), e);
        throw new PaymentsAddressException(PaymentsAddressException.Code.INVALID_ADDRESS_SIGNATURE);
    }
}
Also used : IdentityKey(org.whispersystems.libsignal.IdentityKey) InvalidCiphertextException(org.whispersystems.signalservice.api.crypto.InvalidCiphertextException) ProfileCipher(org.whispersystems.signalservice.api.crypto.ProfileCipher) ProfileAndCredential(org.whispersystems.signalservice.api.profiles.ProfileAndCredential) IOException(java.io.IOException) PaymentsAddressException(org.thoughtcrime.securesms.payments.PaymentsAddressException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) ProfileKey(org.signal.zkgroup.profiles.ProfileKey) SignalServiceProfile(org.whispersystems.signalservice.api.profiles.SignalServiceProfile) SignalServiceProtos(org.whispersystems.signalservice.internal.push.SignalServiceProtos) MobileCoinPublicAddress(org.thoughtcrime.securesms.payments.MobileCoinPublicAddress) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

InvalidCiphertextException (org.whispersystems.signalservice.api.crypto.InvalidCiphertextException)16 IOException (java.io.IOException)10 ProfileKey (org.signal.zkgroup.profiles.ProfileKey)10 ProfileName (org.thoughtcrime.securesms.profiles.ProfileName)4 ByteString (com.google.protobuf.ByteString)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)3 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 BadPaddingException (javax.crypto.BadPaddingException)3 Cipher (javax.crypto.Cipher)3 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)3 SecretKeySpec (javax.crypto.spec.SecretKeySpec)3 RemoteAttestation (org.whispersystems.signalservice.internal.contacts.crypto.RemoteAttestation)3 UnauthenticatedResponseException (org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException)3 DiscoveryRequest (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryRequest)3 DiscoveryResponse (org.whispersystems.signalservice.internal.contacts.entities.DiscoveryResponse)3 NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)2