Java Code Examples for org.whispersystems.libsignal.state.SignedPreKeyRecord

The following examples show how to use org.whispersystems.libsignal.state.SignedPreKeyRecord. These examples are extracted from open source projects. 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 Project: Conversations   Source File: IqGenerator.java    License: GNU General Public License v3.0 6 votes vote down vote up
public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
                               final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
    final Element item = new Element("item");
    item.setAttribute("id", "current");
    final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
    final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
    signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
    ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
    signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
    final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
    signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
    final Element identityKeyElement = bundle.addChild("identityKey");
    identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));

    final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
    for (PreKeyRecord preKeyRecord : preKeyRecords) {
        final Element prekey = prekeys.addChild("preKeyPublic");
        prekey.setAttribute("preKeyId", preKeyRecord.getId());
        prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
    }

    return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
}
 
Example 2
public void registerPreKeys(IdentityKey identityKey,
                            SignedPreKeyRecord signedPreKey,
                            List<PreKeyRecord> records)
    throws IOException
{
  List<PreKeyEntity> entities = new LinkedList<>();

  for (PreKeyRecord record : records) {
    PreKeyEntity entity = new PreKeyEntity(record.getId(),
                                           record.getKeyPair().getPublicKey());

    entities.add(entity);
  }

  SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(),
                                                                 signedPreKey.getKeyPair().getPublicKey(),
                                                                 signedPreKey.getSignature());

  makeServiceRequest(String.format(PREKEY_PATH, ""), "PUT",
                     JsonUtil.toJson(new PreKeyState(entities, signedPreKeyEntity, identityKey)));
}
 
Example 3
public @Nullable SignedPreKeyRecord getSignedPreKey(int keyId) {
  SQLiteDatabase database = databaseHelper.getReadableDatabase();

  try (Cursor cursor = database.query(TABLE_NAME, null, KEY_ID + " = ?",
                                      new String[] {String.valueOf(keyId)},
                                      null, null, null))
  {
    if (cursor != null && cursor.moveToFirst()) {
      try {
        ECPublicKey  publicKey  = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
        byte[]       signature  = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
        long         timestamp  = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));

        return new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature);
      } catch (InvalidKeyException | IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return null;
}
 
Example 4
public @NonNull List<SignedPreKeyRecord> getAllSignedPreKeys() {
  SQLiteDatabase           database = databaseHelper.getReadableDatabase();
  List<SignedPreKeyRecord> results  = new LinkedList<>();

  try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null)) {
    while (cursor != null && cursor.moveToNext()) {
      try {
        int          keyId      = cursor.getInt(cursor.getColumnIndexOrThrow(KEY_ID));
        ECPublicKey  publicKey  = Curve.decodePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PUBLIC_KEY))), 0);
        ECPrivateKey privateKey = Curve.decodePrivatePoint(Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(PRIVATE_KEY))));
        byte[]       signature  = Base64.decode(cursor.getString(cursor.getColumnIndexOrThrow(SIGNATURE)));
        long         timestamp  = cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP));

        results.add(new SignedPreKeyRecord(keyId, timestamp, new ECKeyPair(publicKey, privateKey), signature));
      } catch (InvalidKeyException | IOException e) {
        Log.w(TAG, e);
      }
    }
  }

  return results;
}
 
Example 5
@Override
public void onRun() throws IOException {
  if (TextSecurePreferences.isSignedPreKeyRegistered(context)) {
    Log.w(TAG, "Signed prekey already registered...");
    return;
  }

  if (!TextSecurePreferences.isPushRegistered(context)) {
    Log.w(TAG, "Not yet registered...");
    return;
  }

  SignalServiceAccountManager accountManager     = ApplicationDependencies.getSignalServiceAccountManager();
  IdentityKeyPair             identityKeyPair    = IdentityKeyUtil.getIdentityKeyPair(context);
  SignedPreKeyRecord          signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKeyPair, true);

  accountManager.setSignedPreKey(signedPreKeyRecord);
  TextSecurePreferences.setSignedPreKeyRegistered(context, true);
}
 
Example 6
@Override
public void onRun() throws Exception {
  Log.i(TAG, "Rotating signed prekey...");

  SignalServiceAccountManager accountManager     = ApplicationDependencies.getSignalServiceAccountManager();
  IdentityKeyPair             identityKey        = IdentityKeyUtil.getIdentityKeyPair(context);
  SignedPreKeyRecord          signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, identityKey, false);

  accountManager.setSignedPreKey(signedPreKeyRecord);

  PreKeyUtil.setActiveSignedPreKeyId(context, signedPreKeyRecord.getId());
  TextSecurePreferences.setSignedPreKeyRegistered(context, true);
  TextSecurePreferences.setSignedPreKeyFailureCount(context, 0);

  ApplicationDependencies.getJobManager().add(new CleanPreKeysJob());
}
 
Example 7
@Override
public void onRun(MasterSecret masterSecret) throws Exception {
    Log.w(TAG, "Rotating signed prekey...");
    if (AMELogin.INSTANCE.isLogin()) {
        IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(accountContext);
        SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, accountContext, identityKey, false);

        if (!AmeLoginCore.INSTANCE.refreshSignedPreKey(accountContext, signedPreKeyRecord)) {
            throw new Exception("refreshSignedPreKey failed");
        }

        PreKeyUtil.setActiveSignedPreKeyId(context, accountContext, signedPreKeyRecord.getId());
        accountContext.setSignedPreKeyRegistered(true);
        accountContext.setSignedPreKeyFailureCount(0);

        JobManager manager = AmeModuleCenter.INSTANCE.accountJobMgr(accountContext);
        if (manager != null) {
            manager.add(new CleanPreKeysJob(context, accountContext));
        }
    } else {
        ALog.w(TAG, "please login first");
    }
}
 
Example 8
Source Project: bcm-android   Source File: PreKeyUtil.java    License: 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 9
@Override
public List<SignedPreKeyRecord> loadSignedPreKeys() {
    synchronized (FILE_LOCK) {
        File directory = getSignedPreKeyDirectory();
        List<SignedPreKeyRecord> results = new LinkedList<>();

        for (File signedPreKeyFile : directory.listFiles()) {
            try {
                if (!"index.dat".equals(signedPreKeyFile.getName())) {
                    results.add(new SignedPreKeyRecord(loadSerializedRecord(signedPreKeyFile)));
                }
            } catch (IOException | InvalidMessageException e) {
                Log.w(TAG, signedPreKeyFile.getAbsolutePath(), e);
            }
        }

        return results;
    }
}
 
Example 10
public void registerPreKeys(IdentityKey identityKey,
                            SignedPreKeyRecord signedPreKey,
                            List<PreKeyRecord> records)
    throws IOException
{
  List<PreKeyEntity> entities = new LinkedList<>();

  for (PreKeyRecord record : records) {
    PreKeyEntity entity = new PreKeyEntity(record.getId(),
                                           record.getKeyPair().getPublicKey());

    entities.add(entity);
  }

  SignedPreKeyEntity signedPreKeyEntity = new SignedPreKeyEntity(signedPreKey.getId(),
                                                                 signedPreKey.getKeyPair().getPublicKey(),
                                                                 signedPreKey.getSignature());

  makeServiceRequest(String.format(PREKEY_PATH, ""), "PUT",
                     JsonUtil.toJson(new PreKeyState(entities, signedPreKeyEntity, identityKey)));
}
 
Example 11
private PreKeyBundle createAlicePreKeyBundle(SignalProtocolStore aliceStore) throws InvalidKeyException {
  ECKeyPair aliceUnsignedPreKey   = Curve.generateKeyPair();
  int       aliceUnsignedPreKeyId = new Random().nextInt(Medium.MAX_VALUE);
  byte[]    aliceSignature        = Curve.calculateSignature(aliceStore.getIdentityKeyPair().getPrivateKey(),
                                                             aliceSignedPreKey.getPublicKey().serialize());

  PreKeyBundle alicePreKeyBundle = new PreKeyBundle(1, 1,
                                                    aliceUnsignedPreKeyId, aliceUnsignedPreKey.getPublicKey(),
                                                    aliceSignedPreKeyId, aliceSignedPreKey.getPublicKey(),
                                                    aliceSignature, aliceStore.getIdentityKeyPair().getPublicKey());

  aliceStore.storeSignedPreKey(aliceSignedPreKeyId, new SignedPreKeyRecord(aliceSignedPreKeyId, System.currentTimeMillis(), aliceSignedPreKey, aliceSignature));
  aliceStore.storePreKey(aliceUnsignedPreKeyId, new PreKeyRecord(aliceUnsignedPreKeyId, aliceUnsignedPreKey));

  return alicePreKeyBundle;
}
 
Example 12
public IqPacket publishBundles(final SignedPreKeyRecord signedPreKeyRecord, final IdentityKey identityKey,
                               final Set<PreKeyRecord> preKeyRecords, final int deviceId, Bundle publishOptions) {
    final Element item = new Element("item");
    item.setAttribute("id", "current");
    final Element bundle = item.addChild("bundle", AxolotlService.PEP_PREFIX);
    final Element signedPreKeyPublic = bundle.addChild("signedPreKeyPublic");
    signedPreKeyPublic.setAttribute("signedPreKeyId", signedPreKeyRecord.getId());
    ECPublicKey publicKey = signedPreKeyRecord.getKeyPair().getPublicKey();
    signedPreKeyPublic.setContent(Base64.encodeToString(publicKey.serialize(), Base64.NO_WRAP));
    final Element signedPreKeySignature = bundle.addChild("signedPreKeySignature");
    signedPreKeySignature.setContent(Base64.encodeToString(signedPreKeyRecord.getSignature(), Base64.NO_WRAP));
    final Element identityKeyElement = bundle.addChild("identityKey");
    identityKeyElement.setContent(Base64.encodeToString(identityKey.serialize(), Base64.NO_WRAP));

    final Element prekeys = bundle.addChild("prekeys", AxolotlService.PEP_PREFIX);
    for (PreKeyRecord preKeyRecord : preKeyRecords) {
        final Element prekey = prekeys.addChild("preKeyPublic");
        prekey.setAttribute("preKeyId", preKeyRecord.getId());
        prekey.setContent(Base64.encodeToString(preKeyRecord.getKeyPair().getPublicKey().serialize(), Base64.NO_WRAP));
    }

    return publish(AxolotlService.PEP_BUNDLES + ":" + deviceId, item, publishOptions);
}
 
Example 13
public List<SignedPreKeyRecord> loadSignedPreKeys(Account account) {
    List<SignedPreKeyRecord> prekeys = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = {SQLiteAxolotlStore.KEY};
    String[] selectionArgs = {account.getUuid()};
    Cursor cursor = db.query(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME,
            columns,
            SQLiteAxolotlStore.ACCOUNT + "=?",
            selectionArgs,
            null, null, null);

    while (cursor.moveToNext()) {
        try {
            prekeys.add(new SignedPreKeyRecord(Base64.decode(cursor.getString(cursor.getColumnIndex(SQLiteAxolotlStore.KEY)), Base64.DEFAULT)));
        } catch (IOException ignored) {
        }
    }
    cursor.close();
    return prekeys;
}
 
Example 14
public void insertSignedPreKey(int keyId, SignedPreKeyRecord record) {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_ID, keyId);
  contentValues.put(PUBLIC_KEY, Base64.encodeBytes(record.getKeyPair().getPublicKey().serialize()));
  contentValues.put(PRIVATE_KEY, Base64.encodeBytes(record.getKeyPair().getPrivateKey().serialize()));
  contentValues.put(SIGNATURE, Base64.encodeBytes(record.getSignature()));
  contentValues.put(TIMESTAMP, record.getTimestamp());

  database.replace(TABLE_NAME, null, contentValues);
}
 
Example 15
/**
 * Load a local SignedPreKeyRecord.
 *
 * @param signedPreKeyId the ID of the local SignedPreKeyRecord.
 * @return the corresponding SignedPreKeyRecord.
 * @throws InvalidKeyIdException when there is no corresponding SignedPreKeyRecord.
 */
@Override
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
	SignedPreKeyRecord record = mXmppConnectionService.databaseBackend.loadSignedPreKey(account, signedPreKeyId);
	if (record == null) {
		throw new InvalidKeyIdException("No such SignedPreKeyRecord: " + signedPreKeyId);
	}
	return record;
}
 
Example 16
@Override
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
  synchronized (FILE_LOCK) {
    SignedPreKeyRecord signedPreKeyRecord = DatabaseFactory.getSignedPreKeyDatabase(context).getSignedPreKey(signedPreKeyId);

    if (signedPreKeyRecord == null) throw new InvalidKeyIdException("No such signed prekey: " + signedPreKeyId);
    else                            return signedPreKeyRecord;
  }
}
 
Example 17
Source Project: Smack   Source File: SignalOmemoStoreConnector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public SignedPreKeyRecord loadSignedPreKey(int i) throws InvalidKeyIdException {
    SignedPreKeyRecord signedPreKeyRecord;
    try {
        signedPreKeyRecord = omemoStore.loadOmemoSignedPreKey(getOurDevice(), i);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    if (signedPreKeyRecord == null) {
        throw new InvalidKeyIdException("No signed preKey with id " + i + " found.");
    }
    return signedPreKeyRecord;
}
 
Example 18
@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 19
private LinkedList<SignedPreKeyRecord> removeRecordFrom(SignedPreKeyRecord currentRecord,
                                                        List<SignedPreKeyRecord> records)

{
  LinkedList<SignedPreKeyRecord> others = new LinkedList<>();

  for (SignedPreKeyRecord record : records) {
    if (record.getId() != currentRecord.getId()) {
      others.add(record);
    }
  }

  return others;
}
 
Example 20
@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    if (accountContext.isSignedPreKeyRegistered()) {
        Log.w(TAG, "Signed prekey already registered...");
        return;
    }

    IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(accountContext);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, accountContext, identityKeyPair, true);


    AmeLoginCore.INSTANCE.refreshSignedPreKey(accountContext, signedPreKeyRecord);
    AmeModuleCenter.INSTANCE.login().setSignedPreKeyRegistered(accountContext.getUid(), true);
}
 
Example 21
@Override
public void onRun(MasterSecret masterSecret) throws IOException {

    int availableKeys = AmeLoginCore.INSTANCE.getAvailablePreKeys(accountContext);
    if (availableKeys < 0) {
        throw new IOException("fetch prekey params failed");
    }

    if (availableKeys >= PREKEY_MINIMUM && accountContext.isSignedPreKeyRegistered()) {
        Log.w(TAG, "Available keys sufficient: " + availableKeys);
        return;
    }

    List<PreKeyRecord> preKeyRecords = PreKeyUtil.generatePreKeys(context, accountContext);
    IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(accountContext);
    SignedPreKeyRecord signedPreKeyRecord = PreKeyUtil.generateSignedPreKey(context, accountContext,identityKey, false);

    Log.w(TAG, "Registering new prekeys...");

    if (!AmeLoginCore.INSTANCE.refreshPreKeys(accountContext, identityKey.getPublicKey(), signedPreKeyRecord, preKeyRecords)) {
        throw new IOException("upload prekey failed");
    }

    PreKeyUtil.setActiveSignedPreKeyId(context, accountContext, signedPreKeyRecord.getId());
    accountContext.setSignedPreKeyRegistered(true);

    JobManager jobManager = AmeModuleCenter.INSTANCE.accountJobMgr(accountContext);
    if (jobManager != null) {
        jobManager.add(new CleanPreKeysJob(context, accountContext));
    }

}
 
Example 22
Source Project: Smack   Source File: SignalOmemoStoreConnector.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void storeSignedPreKey(int i, SignedPreKeyRecord signedPreKeyRecord) {
    try {
        omemoStore.storeOmemoSignedPreKey(getOurDevice(), i, signedPreKeyRecord);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
 
Example 23
Source Project: bcm-android   Source File: CleanPreKeysJob.java    License: GNU General Public License v3.0 5 votes vote down vote up
private LinkedList<SignedPreKeyRecord> removeRecordFrom(SignedPreKeyRecord currentRecord,
                                                        List<SignedPreKeyRecord> records) {
    LinkedList<SignedPreKeyRecord> others = new LinkedList<>();

    for (SignedPreKeyRecord record : records) {
        if (record.getId() != currentRecord.getId()) {
            others.add(record);
        }
    }

    return others;
}
 
Example 24
@Override
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
    synchronized (FILE_LOCK) {
        try {
            return new SignedPreKeyRecord(loadSerializedRecord(getSignedPreKeyFile(signedPreKeyId)));
        } catch (IOException | InvalidMessageException e) {
            Log.w(TAG, e);
            throw new InvalidKeyIdException(e);
        }
    }
}
 
Example 25
@Override
public List<SignedPreKeyRecord> loadSignedPreKeys() {
  try {
    List<SignedPreKeyRecord> results = new LinkedList<>();

    for (byte[] serialized : store.values()) {
      results.add(new SignedPreKeyRecord(serialized));
    }

    return results;
  } catch (IOException e) {
    throw new AssertionError(e);
  }
}
 
Example 26
Source Project: Smack   Source File: LegacySignalOmemoKeyUtilTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void generateOmemoSignedPreKeyTest() {
    IdentityKeyPair ikp = keyUtil.generateOmemoIdentityKeyPair();
    try {
        SignedPreKeyRecord spk = keyUtil.generateOmemoSignedPreKey(ikp, 1);
        assertNotNull("SignedPreKey must not be null.", spk);
        assertEquals("SignedPreKeyId must match.", 1, spk.getId());
        assertEquals("singedPreKeyId must match here also.", 1, keyUtil.signedPreKeyIdFromKey(spk));
    } catch (CorruptedOmemoKeyException e) {
        fail("Caught an exception while generating signedPreKey (" + e + "): " + e.getMessage());
    }
}
 
Example 27
Source Project: signal-bot   Source File: SignalBot.java    License: GNU General Public License v3.0 5 votes vote down vote up
private void refreshPreKeys(IdentityKeyPair identityKeyPair) throws IOException, InvalidKeyException {
    int initialPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
    List<PreKeyRecord> records = KeyHelper.generatePreKeys(initialPreKeyId, BATCH_SIZE);
    records.forEach((v) -> this.protocolStore.storePreKey(v.getId(), v));
    int signedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
    SignedPreKeyRecord signedPreKey = KeyHelper.generateSignedPreKey(identityKeyPair, signedPreKeyId);
    this.protocolStore.storeSignedPreKey(signedPreKey.getId(), signedPreKey);
    this.accountManager.setPreKeys(identityKeyPair.getPublicKey(), signedPreKey, records);
}
 
Example 28
@Override
public SignedPreKeyRecord loadSignedPreKey(int signedPreKeyId) throws InvalidKeyIdException {
    try {
        if (!store.containsKey(signedPreKeyId)) {
            throw new InvalidKeyIdException("No such signedprekeyrecord! " + signedPreKeyId);
        }

        return new SignedPreKeyRecord(store.get(signedPreKeyId));
    } catch (IOException e) {
        throw new AssertionError(e);
    }
}
 
Example 29
Source Project: Smack   Source File: SignalOmemoRatchet.java    License: Apache License 2.0 5 votes vote down vote up
SignalOmemoRatchet(OmemoManager omemoManager,
                          OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord,
                                         SessionRecord, SignalProtocolAddress, ECPublicKey, PreKeyBundle,
                                         SessionCipher> store) {
    super(omemoManager, store);
    this.storeConnector = new SignalOmemoStoreConnector(omemoManager, store);
}
 
Example 30
Source Project: signald   Source File: Manager.java    License: GNU General Public License v3.0 5 votes vote down vote up
private SignedPreKeyRecord generateSignedPreKey(IdentityKeyPair identityKeyPair) throws IOException {
    try {
        ECKeyPair keyPair = Curve.generateKeyPair();
        byte[] signature = Curve.calculateSignature(identityKeyPair.getPrivateKey(), keyPair.getPublicKey().serialize());
        SignedPreKeyRecord record = new SignedPreKeyRecord(accountData.nextSignedPreKeyId, System.currentTimeMillis(), keyPair, signature);

        accountData.axolotlStore.storeSignedPreKey(accountData.nextSignedPreKeyId, record);
        accountData.nextSignedPreKeyId = (accountData.nextSignedPreKeyId + 1) % Medium.MAX_VALUE;
        accountData.save();

        return record;
    } catch (InvalidKeyException e) {
        throw new AssertionError(e);
    }
}