SignalOmemoRatchet.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * This file is part of smack-omemo-signal.
  6.  *
  7.  * smack-omemo-signal is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  20.  */
  21. package org.jivesoftware.smackx.omemo.signal;

  22. import java.io.IOException;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;

  25. import org.jivesoftware.smackx.omemo.OmemoManager;
  26. import org.jivesoftware.smackx.omemo.OmemoRatchet;
  27. import org.jivesoftware.smackx.omemo.OmemoStore;
  28. import org.jivesoftware.smackx.omemo.element.OmemoElement;
  29. import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
  30. import org.jivesoftware.smackx.omemo.exceptions.CryptoFailedException;
  31. import org.jivesoftware.smackx.omemo.exceptions.NoRawSessionException;
  32. import org.jivesoftware.smackx.omemo.exceptions.UntrustedOmemoIdentityException;
  33. import org.jivesoftware.smackx.omemo.internal.CiphertextTuple;
  34. import org.jivesoftware.smackx.omemo.internal.OmemoDevice;

  35. import org.whispersystems.libsignal.DuplicateMessageException;
  36. import org.whispersystems.libsignal.IdentityKey;
  37. import org.whispersystems.libsignal.IdentityKeyPair;
  38. import org.whispersystems.libsignal.InvalidKeyException;
  39. import org.whispersystems.libsignal.InvalidKeyIdException;
  40. import org.whispersystems.libsignal.InvalidMessageException;
  41. import org.whispersystems.libsignal.InvalidVersionException;
  42. import org.whispersystems.libsignal.LegacyMessageException;
  43. import org.whispersystems.libsignal.NoSessionException;
  44. import org.whispersystems.libsignal.SessionCipher;
  45. import org.whispersystems.libsignal.SignalProtocolAddress;
  46. import org.whispersystems.libsignal.UntrustedIdentityException;
  47. import org.whispersystems.libsignal.ecc.ECPublicKey;
  48. import org.whispersystems.libsignal.protocol.CiphertextMessage;
  49. import org.whispersystems.libsignal.protocol.PreKeySignalMessage;
  50. import org.whispersystems.libsignal.protocol.SignalMessage;
  51. import org.whispersystems.libsignal.state.PreKeyBundle;
  52. import org.whispersystems.libsignal.state.PreKeyRecord;
  53. import org.whispersystems.libsignal.state.SessionRecord;
  54. import org.whispersystems.libsignal.state.SignedPreKeyRecord;

  55. public class SignalOmemoRatchet
  56.         extends OmemoRatchet<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
  57.                 SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> {

  58.     private static final Logger LOGGER = Logger.getLogger(OmemoRatchet.class.getName());
  59.     private final SignalOmemoStoreConnector storeConnector;

  60.     SignalOmemoRatchet(OmemoManager omemoManager,
  61.                               OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord,
  62.                                              SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle,
  63.                                              SessionCipher> store) {
  64.         super(omemoManager, store);
  65.         this.storeConnector = new SignalOmemoStoreConnector(omemoManager, store);
  66.     }

  67.     @Override
  68.     public byte[] doubleRatchetDecrypt(OmemoDevice sender, byte[] encryptedKey)
  69.             throws CorruptedOmemoKeyException, NoRawSessionException, CryptoFailedException,
  70.             UntrustedOmemoIdentityException, IOException {

  71.         SessionCipher cipher = getCipher(sender);
  72.         byte[] decryptedKey;

  73.         // Try to handle the message as a PreKeySignalMessage...
  74.         try {
  75.             PreKeySignalMessage preKeyMessage = new PreKeySignalMessage(encryptedKey);

  76.             if (!preKeyMessage.getPreKeyId().isPresent()) {
  77.                 throw new CryptoFailedException("PreKeyMessage did not contain a preKeyId.");
  78.             }

  79.             IdentityKey messageIdentityKey = preKeyMessage.getIdentityKey();
  80.             IdentityKey previousIdentityKey = store.loadOmemoIdentityKey(storeConnector.getOurDevice(), sender);

  81.             if (previousIdentityKey != null &&
  82.                     !previousIdentityKey.getFingerprint().equals(messageIdentityKey.getFingerprint())) {
  83.                 throw new UntrustedOmemoIdentityException(sender,
  84.                         store.keyUtil().getFingerprintOfIdentityKey(previousIdentityKey),
  85.                         store.keyUtil().getFingerprintOfIdentityKey(messageIdentityKey));
  86.             }

  87.             try {
  88.                 decryptedKey = cipher.decrypt(preKeyMessage);
  89.             }
  90.             catch (UntrustedIdentityException e) {
  91.                 throw new AssertionError("Signals trust management MUST be disabled.");
  92.             }
  93.             catch (LegacyMessageException | InvalidKeyException e) {
  94.                 throw new CryptoFailedException(e);
  95.             }
  96.             catch (InvalidKeyIdException e) {
  97.                 throw new NoRawSessionException(sender, e);
  98.             }
  99.             catch (DuplicateMessageException e) {
  100.                 LOGGER.log(Level.INFO, "Decryption of PreKeyMessage from " + sender +
  101.                         " failed, since the message has been decrypted before.");
  102.                 return null;
  103.             }

  104.         } catch (InvalidVersionException | InvalidMessageException noPreKeyMessage) {
  105.             // ...if that fails, handle it as a SignalMessage
  106.             try {
  107.                 SignalMessage message = new SignalMessage(encryptedKey);
  108.                 decryptedKey = getCipher(sender).decrypt(message);
  109.             }
  110.             catch (UntrustedIdentityException e) {
  111.                 throw new AssertionError("Signals trust management MUST be disabled.");
  112.             }
  113.             catch (InvalidMessageException | NoSessionException e) {
  114.                 throw new NoRawSessionException(sender, e);
  115.             }
  116.             catch (LegacyMessageException e) {
  117.                 throw new CryptoFailedException(e);
  118.             }
  119.             catch (DuplicateMessageException e1) {
  120.                 LOGGER.log(Level.INFO, "Decryption of SignalMessage from " + sender +
  121.                         " failed, since the message has been decrypted before.");
  122.                 return null;
  123.             }
  124.         }

  125.         return decryptedKey;
  126.     }

  127.     @Override
  128.     public CiphertextTuple doubleRatchetEncrypt(OmemoDevice recipient, byte[] messageKey) {
  129.         CiphertextMessage ciphertextMessage;
  130.         try {
  131.             ciphertextMessage = getCipher(recipient).encrypt(messageKey);
  132.         } catch (UntrustedIdentityException e) {
  133.             throw new AssertionError("Signals trust management MUST be disabled.");
  134.         }

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

  137.         return new CiphertextTuple(ciphertextMessage.serialize(), type);
  138.     }

  139.     private SessionCipher getCipher(OmemoDevice device) {
  140.         return new SessionCipher(storeConnector, storeConnector, storeConnector, storeConnector,
  141.                 SignalOmemoStoreConnector.asAddress(device));
  142.     }
  143. }