org.whispersystems.libsignal.state.SignedPreKeyRecord Java Examples

The following examples show how to use org.whispersystems.libsignal.state.SignedPreKeyRecord. 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: IqGenerator.java    From Conversations with 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
Source File: RotateSignedPreKeyJob.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@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 #3
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 #4
Source File: TextSecurePreKeyStore.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@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 #5
Source File: DatabaseBackend.java    From Pix-Art-Messenger with GNU General Public License v3.0 6 votes vote down vote up
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 #6
Source File: RotateSignedPreKeyJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: IqGenerator.java    From Pix-Art-Messenger with 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 #8
Source File: CreateSignedPreKeyJob.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@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 #9
Source File: PushServiceSocket.java    From libsignal-service-java with GNU General Public License v3.0 6 votes vote down vote up
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 #10
Source File: SimultaneousInitiateTests.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
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 #11
Source File: SignedPreKeyDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 #12
Source File: SignedPreKeyDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 #13
Source File: PushServiceSocket.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
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 #14
Source File: InMemorySignedPreKeyStore.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
@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 #15
Source File: SQLiteAxolotlStore.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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
Source File: SignalOmemoKeyUtil.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public SignedPreKeyRecord generateOmemoSignedPreKey(IdentityKeyPair identityKeyPair, int currentPreKeyId)
        throws CorruptedOmemoKeyException {
    try {
        return KeyHelper.generateSignedPreKey(identityKeyPair, currentPreKeyId);
    } catch (InvalidKeyException e) {
        throw new CorruptedOmemoKeyException(e);
    }
}
 
Example #17
Source File: SessionBuilderTest.java    From libsignal-protocol-java with GNU General Public License v3.0 5 votes vote down vote up
public void testRepeatBundleMessageV2() throws InvalidKeyException, UntrustedIdentityException, InvalidVersionException, InvalidMessageException, InvalidKeyIdException, DuplicateMessageException, LegacyMessageException, NoSessionException {
  SignalProtocolStore aliceStore          = new TestInMemorySignalProtocolStore();
  SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceStore, BOB_ADDRESS);

  SignalProtocolStore bobStore = new TestInMemorySignalProtocolStore();

  ECKeyPair bobPreKeyPair            = Curve.generateKeyPair();
  ECKeyPair bobSignedPreKeyPair      = Curve.generateKeyPair();
  byte[]    bobSignedPreKeySignature = Curve.calculateSignature(bobStore.getIdentityKeyPair().getPrivateKey(),
                                                                bobSignedPreKeyPair.getPublicKey().serialize());

  PreKeyBundle bobPreKey = new PreKeyBundle(bobStore.getLocalRegistrationId(), 1,
                                            31337, bobPreKeyPair.getPublicKey(),
                                            0, null, null,
                                            bobStore.getIdentityKeyPair().getPublicKey());

  bobStore.storePreKey(31337, new PreKeyRecord(bobPreKey.getPreKeyId(), bobPreKeyPair));
  bobStore.storeSignedPreKey(22, new SignedPreKeyRecord(22, System.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));

  try {
    aliceSessionBuilder.process(bobPreKey);
    throw new AssertionError("Should fail with missing signed prekey!");
  } catch (InvalidKeyException e) {
    // Good!
    return;
  }
}
 
Example #18
Source File: DatabaseBackend.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public void storeSignedPreKey(Account account, SignedPreKeyRecord record) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(SQLiteAxolotlStore.ID, record.getId());
    values.put(SQLiteAxolotlStore.KEY, Base64.encodeToString(record.serialize(), Base64.DEFAULT));
    values.put(SQLiteAxolotlStore.ACCOUNT, account.getUuid());
    db.insert(SQLiteAxolotlStore.SIGNED_PREKEY_TABLENAME, null, values);
}
 
Example #19
Source File: SignalOmemoStoreConnector.java    From Smack with 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 #20
Source File: SignalOmemoService.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
protected SignalOmemoRatchet instantiateOmemoRatchet(
        OmemoManager manager,
        OmemoStore<IdentityKeyPair, IdentityKey, PreKeyRecord, SignedPreKeyRecord, SessionRecord,
                SignalProtocolAddress, ECPublicKey, PreKeyBundle, SessionCipher> store) {

    return new SignalOmemoRatchet(manager, getOmemoStoreBackend());
}
 
Example #21
Source File: SignedPreKeyStore.java    From signald with GNU General Public License v3.0 5 votes vote down vote up
@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 #22
Source File: TextSecurePreKeyStore.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@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 #23
Source File: SignedPreKeyStore.java    From signald with GNU General Public License v3.0 5 votes vote down vote up
@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 #24
Source File: SignalOmemoRatchet.java    From Smack with 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 #25
Source File: Manager.java    From signald with 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);
    }
}
 
Example #26
Source File: LegacySignalOmemoKeyUtilTest.java    From Smack with 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 File: SignalBot.java    From signal-bot with 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
Source File: SignalOmemoStoreConnector.java    From Smack with Apache License 2.0 5 votes vote down vote up
@Override
public List<SignedPreKeyRecord> loadSignedPreKeys() {

    TreeMap<Integer, SignedPreKeyRecord> signedPreKeyRecordHashMap;
    try {
        signedPreKeyRecordHashMap = omemoStore.loadOmemoSignedPreKeys(getOurDevice());
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    return new ArrayList<>(signedPreKeyRecordHashMap.values());
}
 
Example #29
Source File: JsonSignedPreKeyStore.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
@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 #30
Source File: JsonSignedPreKeyStore.java    From signald with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}