org.whispersystems.libsignal.state.SignedPreKeyStore Java Examples

The following examples show how to use org.whispersystems.libsignal.state.SignedPreKeyStore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PreKeyUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public synchronized static SignedPreKeyRecord generateSignedPreKey(Context context, IdentityKeyPair identityKeyPair, boolean active) {
  try {
    SignedPreKeyStore  signedPreKeyStore = new TextSecurePreKeyStore(context);
    int                signedPreKeyId    = TextSecurePreferences.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);
    TextSecurePreferences.setNextSignedPreKeyId(context, (signedPreKeyId + 1) % Medium.MAX_VALUE);

    if (active) {
      TextSecurePreferences.setActiveSignedPreKeyId(context, signedPreKeyId);
    }

    return record;
  } catch (InvalidKeyException e) {
    throw new AssertionError(e);
  }
}
 
Example #2
Source File: PreKeyUtil.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
public static SignedPreKeyRecord generateSignedPreKey(Context context, AccountContext accountContext, IdentityKeyPair identityKeyPair, boolean active) {
    try {
        SignedPreKeyStore signedPreKeyStore = new TextSecurePreKeyStore(context, accountContext);
        int signedPreKeyId = getNextSignedPreKeyId(context, accountContext);
        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, accountContext,(signedPreKeyId + 1) % Medium.MAX_VALUE);

        if (active) {
            setActiveSignedPreKeyId(context, accountContext, signedPreKeyId);
        }

        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}
 
Example #3
Source File: KeyExchangeInitiator.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
public static void initiateKeyExchange(Context context, MasterSecret masterSecret, Recipients recipients, int subscriptionId) {
  Recipient         recipient         = recipients.getPrimaryRecipient();
  SessionStore      sessionStore      = new SilenceSessionStore(context, masterSecret, subscriptionId);
  PreKeyStore       preKeyStore       = new SilencePreKeyStore(context, masterSecret, subscriptionId);
  SignedPreKeyStore signedPreKeyStore = new SilencePreKeyStore(context, masterSecret, subscriptionId);
  IdentityKeyStore  identityKeyStore  = new SilenceIdentityKeyStore(context, masterSecret, subscriptionId);

  SessionBuilder    sessionBuilder    = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                                           identityKeyStore, new SignalProtocolAddress(recipient.getNumber(), 1));

  if (identityKeyStore.getIdentityKeyPair() != null) {
    KeyExchangeMessage         keyExchangeMessage = sessionBuilder.process();
    String                     serializedMessage  = Base64.encodeBytesWithoutPadding(keyExchangeMessage.serialize());
    OutgoingKeyExchangeMessage textMessage        = new OutgoingKeyExchangeMessage(recipients, serializedMessage, subscriptionId);

    MessageSender.send(context, masterSecret, textMessage, -1, false);
  } else {
    Toast.makeText(context, R.string.VerifyIdentityActivity_you_do_not_have_an_identity_key,
            Toast.LENGTH_LONG).show();
  }
}
 
Example #4
Source File: CleanPreKeysJob.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() throws IOException {
  try {
    Log.i(TAG, "Cleaning prekeys...");

    int                activeSignedPreKeyId = PreKeyUtil.getActiveSignedPreKeyId(context);
    SignedPreKeyStore  signedPreKeyStore    = new SignalProtocolStoreImpl(context);

    if (activeSignedPreKeyId < 0) return;

    SignedPreKeyRecord             currentRecord = signedPreKeyStore.loadSignedPreKey(activeSignedPreKeyId);
    List<SignedPreKeyRecord>       allRecords    = signedPreKeyStore.loadSignedPreKeys();
    LinkedList<SignedPreKeyRecord> oldRecords    = removeRecordFrom(currentRecord, allRecords);

    Collections.sort(oldRecords, new SignedPreKeySorter());

    Log.i(TAG, "Active signed prekey: " + activeSignedPreKeyId);
    Log.i(TAG, "Old signed prekey record count: " + oldRecords.size());

    boolean foundAgedRecord = false;

    for (SignedPreKeyRecord oldRecord : oldRecords) {
      long archiveDuration = System.currentTimeMillis() - oldRecord.getTimestamp();

      if (archiveDuration >= ARCHIVE_AGE) {
        if (!foundAgedRecord) {
          foundAgedRecord = true;
        } else {
          Log.i(TAG, "Removing signed prekey record: " + oldRecord.getId() + " with timestamp: " + oldRecord.getTimestamp());
          signedPreKeyStore.removeSignedPreKey(oldRecord.getId());
        }
      }
    }
  } catch (InvalidKeyIdException e) {
    Log.w(TAG, e);
  }
}
 
Example #5
Source File: CleanPreKeysJob.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public CleanPreKeysJob(Context context, AccountContext accountContext) {
    super(context, accountContext, JobParameters.newBuilder()
            .withGroupId(CleanPreKeysJob.class.getSimpleName())
            .withRequirement(new MasterSecretRequirement(context, accountContext))
            .withRetryCount(5)
            .create());
    this.signedPreKeyStoreFactory = new SignedPreKeyStoreFactory() {
        @Override
        public SignedPreKeyStore create() {
            return new SignalProtocolStoreImpl(context, accountContext);
        }
    };
}
 
Example #6
Source File: CleanPreKeysJob.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    try {
        Log.w(TAG, "Cleaning prekeys...");

        int activeSignedPreKeyId = PreKeyUtil.getActiveSignedPreKeyId(context, accountContext);
        SignedPreKeyStore signedPreKeyStore = signedPreKeyStoreFactory.create();

        if (activeSignedPreKeyId < 0) return;

        SignedPreKeyRecord currentRecord = signedPreKeyStore.loadSignedPreKey(activeSignedPreKeyId);
        List<SignedPreKeyRecord> allRecords = signedPreKeyStore.loadSignedPreKeys();
        LinkedList<SignedPreKeyRecord> oldRecords = removeRecordFrom(currentRecord, allRecords);

        Collections.sort(oldRecords, new SignedPreKeySorter());

        Log.w(TAG, "Active signed prekey: " + activeSignedPreKeyId);
        Log.w(TAG, "Old signed prekey record count: " + oldRecords.size());

        boolean foundAgedRecord = false;

        for (SignedPreKeyRecord oldRecord : oldRecords) {
            long archiveDuration = System.currentTimeMillis() - oldRecord.getTimestamp();

            if (archiveDuration >= ARCHIVE_AGE) {
                if (!foundAgedRecord) {
                    foundAgedRecord = true;
                } else {
                    Log.w(TAG, "Removing signed prekey record: " + oldRecord.getId() + " with timestamp: " + oldRecord.getTimestamp());
                    signedPreKeyStore.removeSignedPreKey(oldRecord.getId());
                }
            }
        }
    } catch (InvalidKeyIdException e) {
        Log.w(TAG, e);
    }
}
 
Example #7
Source File: SessionBuilder.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a SessionBuilder.
 *
 * @param sessionStore The {@link org.whispersystems.libsignal.state.SessionStore} to store the constructed session in.
 * @param preKeyStore The {@link  org.whispersystems.libsignal.state.PreKeyStore} where the client's local {@link org.whispersystems.libsignal.state.PreKeyRecord}s are stored.
 * @param identityKeyStore The {@link org.whispersystems.libsignal.state.IdentityKeyStore} containing the client's identity key information.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SessionStore sessionStore,
                      PreKeyStore preKeyStore,
                      SignedPreKeyStore signedPreKeyStore,
                      IdentityKeyStore identityKeyStore,
                      SignalProtocolAddress remoteAddress)
{
  this.sessionStore      = sessionStore;
  this.preKeyStore       = preKeyStore;
  this.signedPreKeyStore = signedPreKeyStore;
  this.identityKeyStore  = identityKeyStore;
  this.remoteAddress     = remoteAddress;
}
 
Example #8
Source File: SessionBuilder.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a SessionBuilder.
 *
 * @param sessionStore The {@link org.whispersystems.libsignal.state.SessionStore} to store the constructed session in.
 * @param preKeyStore The {@link  org.whispersystems.libsignal.state.PreKeyStore} where the client's local {@link org.whispersystems.libsignal.state.PreKeyRecord}s are stored.
 * @param identityKeyStore The {@link org.whispersystems.libsignal.state.IdentityKeyStore} containing the client's identity key information.
 * @param remoteAddress The address of the remote user to build a session with.
 */
public SessionBuilder(SessionStore sessionStore,
                      PreKeyStore preKeyStore,
                      SignedPreKeyStore signedPreKeyStore,
                      IdentityKeyStore identityKeyStore,
                      SignalProtocolAddress remoteAddress)
{
  this.sessionStore      = sessionStore;
  this.preKeyStore       = preKeyStore;
  this.signedPreKeyStore = signedPreKeyStore;
  this.identityKeyStore  = identityKeyStore;
  this.remoteAddress     = remoteAddress;
}
 
Example #9
Source File: SessionCipher.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Construct a SessionCipher for encrypt/decrypt operations on a session.
 * In order to use SessionCipher, a session must have already been created
 * and stored using {@link SessionBuilder}.
 *
 * @param  sessionStore The {@link SessionStore} that contains a session for this recipient.
 * @param  remoteAddress  The remote address that messages will be encrypted to or decrypted from.
 */
public SessionCipher(SessionStore sessionStore, PreKeyStore preKeyStore,
                     SignedPreKeyStore signedPreKeyStore, IdentityKeyStore identityKeyStore,
                     SignalProtocolAddress remoteAddress)
{
  this.sessionStore     = sessionStore;
  this.preKeyStore      = preKeyStore;
  this.identityKeyStore = identityKeyStore;
  this.remoteAddress    = remoteAddress;
  this.sessionBuilder   = new SessionBuilder(sessionStore, preKeyStore, signedPreKeyStore,
                                             identityKeyStore, remoteAddress);
}
 
Example #10
Source File: CleanPreKeysJob.java    From bcm-android with GNU General Public License v3.0 votes vote down vote up
public SignedPreKeyStore create();