/** * Decrypt a message. * * @param ciphertext The {@link WhisperMessage} to decrypt. * @param callback A callback that is triggered after decryption is complete, * but before the updated session state has been committed to the session * DB. This allows some implementations to store the committed plaintext * to a DB first, in case they are concerned with a crash happening between * the time the session state is updated but before they're able to store * the plaintext to disk. * * @return The plaintext. * @throws InvalidMessageException if the input is not valid ciphertext. * @throws DuplicateMessageException if the input is a message that has already been received. * @throws LegacyMessageException if the input is a message formatted by a protocol version that * is no longer supported. * @throws NoSessionException if there is no established session for this contact. */ public byte[] Decrypt(WhisperMessage ciphertext, DecryptionCallback callback) { lock (SESSION_LOCK) { if (!sessionStore.ContainsSession(remoteAddress)) { throw new NoSessionException($"No session for: {remoteAddress}"); } SessionRecord sessionRecord = sessionStore.LoadSession(remoteAddress); byte[] plaintext = Decrypt(sessionRecord, ciphertext); callback.HandlePlaintext(plaintext); sessionStore.StoreSession(remoteAddress, sessionRecord); return(plaintext); } }
/** * Decrypt a message. * * @param ciphertext The {@link PreKeyWhisperMessage} to decrypt. * @param callback A callback that is triggered after decryption is complete, * but before the updated session state has been committed to the session * DB. This allows some implementations to store the committed plaintext * to a DB first, in case they are concerned with a crash happening between * the time the session state is updated but before they're able to store * the plaintext to disk. * * @return The plaintext. * @throws InvalidMessageException if the input is not valid ciphertext. * @throws DuplicateMessageException if the input is a message that has already been received. * @throws LegacyMessageException if the input is a message formatted by a protocol version that * is no longer supported. * @throws InvalidKeyIdException when there is no local {@link org.whispersystems.libaxolotl.state.PreKeyRecord} * that corresponds to the PreKey ID in the message. * @throws InvalidKeyException when the message is formatted incorrectly. * @throws UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted. */ public byte[] Decrypt(PreKeyWhisperMessage ciphertext, DecryptionCallback callback) { lock (SESSION_LOCK) { SessionRecord sessionRecord = sessionStore.LoadSession(remoteAddress); May <uint> unsignedPreKeyId = sessionBuilder.Process(sessionRecord, ciphertext); byte[] plaintext = Decrypt(sessionRecord, ciphertext.GetWhisperMessage()); callback.HandlePlaintext(plaintext); sessionStore.StoreSession(remoteAddress, sessionRecord); if (unsignedPreKeyId.HasValue) { preKeyStore.RemovePreKey(unsignedPreKeyId.ForceGetValue()); } return(plaintext); } }
/** * Decrypt a SenderKey group message. * * @param senderKeyMessageBytes The received ciphertext. * @param callback A callback that is triggered after decryption is complete, * but before the updated session state has been committed to the session * DB. This allows some implementations to store the committed plaintext * to a DB first, in case they are concerned with a crash happening between * the time the session state is updated but before they're able to store * the plaintext to disk. * @return Plaintext * @throws LegacyMessageException * @throws InvalidMessageException * @throws DuplicateMessageException */ public byte[] Decrypt(byte[] senderKeyMessageBytes, DecryptionCallback callback) { lock (LOCK) { try { SenderKeyRecord record = senderKeyStore.LoadSenderKey(senderKeyId); if (record.IsEmpty()) { throw new NoSessionException("No Sender Key For: " + senderKeyId); } SenderKeyMessage senderKeyMessage = new SenderKeyMessage(senderKeyMessageBytes); SenderKeyState senderKeyState = record.GetSenderKeyState(senderKeyMessage.GetKeyId()); senderKeyMessage.VerifySignature(senderKeyState.GetSigningKeyPublic()); SenderMessageKey senderKey = GetSenderKey(senderKeyState, senderKeyMessage.GetIteration()); byte[] plaintext = GetPlainText(senderKey.GetIv(), senderKey.GetCipherKey(), senderKeyMessage.GetCipherText()); callback.HandlePlaintext(plaintext); senderKeyStore.StoreSenderKey(senderKeyId, record); return(plaintext); } catch (InvalidKeyException e) { throw new InvalidMessageException(e); } catch (InvalidKeyIdException e) { throw new InvalidMessageException(e); } } }