SignalOmemoService.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.util.logging.Level;

  23. import org.jivesoftware.smackx.omemo.OmemoManager;
  24. import org.jivesoftware.smackx.omemo.OmemoService;
  25. import org.jivesoftware.smackx.omemo.OmemoStore;
  26. import org.jivesoftware.smackx.omemo.exceptions.CorruptedOmemoKeyException;
  27. import org.jivesoftware.smackx.omemo.internal.OmemoDevice;

  28. import org.whispersystems.libsignal.IdentityKey;
  29. import org.whispersystems.libsignal.IdentityKeyPair;
  30. import org.whispersystems.libsignal.SessionBuilder;
  31. import org.whispersystems.libsignal.SessionCipher;
  32. import org.whispersystems.libsignal.SignalProtocolAddress;
  33. import org.whispersystems.libsignal.UntrustedIdentityException;
  34. import org.whispersystems.libsignal.ecc.ECPublicKey;
  35. import org.whispersystems.libsignal.state.PreKeyBundle;
  36. import org.whispersystems.libsignal.state.PreKeyRecord;
  37. import org.whispersystems.libsignal.state.SessionRecord;
  38. import org.whispersystems.libsignal.state.SignedPreKeyRecord;

  39. /**
  40.  * Concrete implementation of the OmemoService using the Signal library.
  41.  *
  42.  * @author Paul Schaub
  43.  */
  44. @SuppressWarnings("unused")
  45. public final class SignalOmemoService
  46.         extends OmemoService<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
  47.         SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> {

  48.     private static SignalOmemoService INSTANCE;
  49.     private static boolean LICENSE_ACKNOWLEDGED = false;

  50.     @Override
  51.     protected SignalOmemoRatchet instantiateOmemoRatchet(
  52.             OmemoManager manager,
  53.             OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
  54.                     SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store) {

  55.         return new SignalOmemoRatchet(manager, getOmemoStoreBackend());
  56.     }

  57.     public static void setup() {
  58.         if (!LICENSE_ACKNOWLEDGED) {
  59.             throw new IllegalStateException("smack-omemo-signal is licensed under the terms of the GPLv3. " +
  60.                     "Please be aware that you can only use this library within the terms of the GPLv3. " +
  61.                     "See for example https://www.gnu.org/licenses/quick-guide-gplv3 for more details. Please call " +
  62.                     "SignalOmemoService.acknowledgeLicense() prior to the setup() method in order to prevent " +
  63.                     "this exception.");
  64.         }
  65.         if (INSTANCE == null) {
  66.             INSTANCE = new SignalOmemoService();
  67.         }
  68.         setInstance(INSTANCE);
  69.     }

  70.     @Override
  71.     public OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
  72.             SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher>
  73.     createDefaultOmemoStoreBackend() {
  74.         return new SignalCachingOmemoStore();
  75.     }

  76.     private SignalOmemoService() {
  77.         super();
  78.     }

  79.     public static void acknowledgeLicense() {
  80.         LICENSE_ACKNOWLEDGED = true;
  81.     }

  82.     @Override
  83.     protected void processBundle(OmemoManager omemoManager,
  84.                                  PreKeyBundle contactsBundle,
  85.                                  OmemoDevice contactsDevice)
  86.             throws CorruptedOmemoKeyException {

  87.         SignalOmemoStoreConnector connector = new SignalOmemoStoreConnector(omemoManager, getOmemoStoreBackend());
  88.         SessionBuilder builder = new SessionBuilder(connector, connector, connector, connector,
  89.                 SignalOmemoStoreConnector.asAddress(contactsDevice));
  90.         try {
  91.             builder.process(contactsBundle);
  92.             LOGGER.log(Level.FINE, "Session built with " + contactsDevice);
  93.         } catch (org.whispersystems.libsignal.InvalidKeyException e) {
  94.             throw new CorruptedOmemoKeyException(e);
  95.         } catch (UntrustedIdentityException e) {
  96.             // This should never happen.
  97.             throw new AssertionError(e);
  98.         }
  99.     }
  100. }