Java Code Examples for org.whispersystems.libsignal.util.Pair#second()

The following examples show how to use org.whispersystems.libsignal.util.Pair#second() . 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: RemoteAttestationUtil.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public static RemoteAttestation getAndVerifyRemoteAttestation(PushServiceSocket socket,
                                                              PushServiceSocket.ClientSet clientSet,
                                                              KeyStore iasKeyStore,
                                                              String enclaveName,
                                                              String mrenclave,
                                                              String authorization)
  throws IOException, Quote.InvalidQuoteFormatException, InvalidCiphertextException, UnauthenticatedQuoteException, SignatureException
{
  Curve25519                                    curve                   = Curve25519.getInstance(Curve25519.BEST);
  Curve25519KeyPair                             keyPair                 = curve.generateKeyPair();
  RemoteAttestationRequest                      attestationRequest      = new RemoteAttestationRequest(keyPair.getPublicKey());
  Pair<RemoteAttestationResponse, List<String>> attestationResponsePair = getRemoteAttestation(socket, clientSet, authorization, attestationRequest, enclaveName);
  RemoteAttestationResponse                     attestationResponse     = attestationResponsePair.first();
  List<String>                                  attestationCookies      = attestationResponsePair.second();

  RemoteAttestationKeys keys      = new RemoteAttestationKeys(keyPair, attestationResponse.getServerEphemeralPublic(), attestationResponse.getServerStaticPublic());
  Quote                 quote     = new Quote(attestationResponse.getQuote());
  byte[]                requestId = RemoteAttestationCipher.getRequestId(keys, attestationResponse);

  RemoteAttestationCipher.verifyServerQuote(quote, attestationResponse.getServerStaticPublic(), mrenclave);

  RemoteAttestationCipher.verifyIasSignature(iasKeyStore, attestationResponse.getCertificates(), attestationResponse.getSignatureBody(), attestationResponse.getSignature(), quote);

  return new RemoteAttestation(requestId, keys, attestationCookies);
}
 
Example 2
Source File: ConversationRepository.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private @NonNull ConversationData getConversationDataInternal(long threadId, int jumpToPosition) {
  Pair<Long, Boolean> lastSeenAndHasSent = DatabaseFactory.getThreadDatabase(context).getLastSeenAndHasSent(threadId);

  long    lastSeen         = lastSeenAndHasSent.first();
  boolean hasSent          = lastSeenAndHasSent.second();
  int     lastSeenPosition = 0;

  boolean isMessageRequestAccepted     = RecipientUtil.isMessageRequestAccepted(context, threadId);
  boolean hasPreMessageRequestMessages = RecipientUtil.isPreMessageRequestThread(context, threadId);

  if (lastSeen > 0) {
    lastSeenPosition = DatabaseFactory.getMmsSmsDatabase(context).getMessagePositionForLastSeen(threadId, lastSeen);
  }

  if (lastSeenPosition <= 0) {
    lastSeen = 0;
  }

  return new ConversationData(threadId, lastSeen, lastSeenPosition, hasSent, isMessageRequestAccepted, hasPreMessageRequestMessages, jumpToPosition);
}
 
Example 3
Source File: PhoneNumberFormatter.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public static @NonNull PhoneNumberFormatter get(Context context) {
  String localNumber = TextSecurePreferences.getLocalNumber(context);

  if (!TextUtils.isEmpty(localNumber)) {
    Pair<String, PhoneNumberFormatter> cached = cachedFormatter.get();

    if (cached != null && cached.first().equals(localNumber)) return cached.second();

    PhoneNumberFormatter formatter = new PhoneNumberFormatter(localNumber);
    cachedFormatter.set(new Pair<>(localNumber, formatter));

    return formatter;
  } else {
    return new PhoneNumberFormatter(Util.getSimCountryIso(context).or("US"), true);
  }
}
 
Example 4
Source File: MmsSmsDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @return The user that added you to the group, otherwise null.
 */
public @Nullable RecipientId getGroupAddedBy(long threadId) {
  long lastQuitChecked = System.currentTimeMillis();
  Pair<RecipientId, Long> pair;

  do {
    pair = getGroupAddedBy(threadId, lastQuitChecked);
    if (pair.first() != null) {
      return pair.first();
    } else {
      lastQuitChecked = pair.second();
    }

  } while (pair.second() != -1);

  return null;
}
 
Example 5
Source File: SignalAccount.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
public static SignalAccount create(String dataPath, String username, IdentityKeyPair identityKey, int registrationId, ProfileKey profileKey) throws IOException {
    IOUtils.createPrivateDirectories(dataPath);
    String fileName = getFileName(dataPath, username);
    if (!new File(fileName).exists()) {
        IOUtils.createPrivateFile(fileName);
    }

    final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
    SignalAccount account = new SignalAccount(pair.first(), pair.second());

    account.username = username;
    account.profileKey = profileKey;
    account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
    account.groupStore = new JsonGroupStore();
    account.contactStore = new JsonContactsStore();
    account.recipientStore = new RecipientStore();
    account.registered = false;

    return account;
}
 
Example 6
Source File: SignalAccount.java    From signal-cli with GNU General Public License v3.0 6 votes vote down vote up
public static SignalAccount createLinkedAccount(String dataPath, String username, UUID uuid, String password, int deviceId, IdentityKeyPair identityKey, int registrationId, String signalingKey, ProfileKey profileKey) throws IOException {
    IOUtils.createPrivateDirectories(dataPath);
    String fileName = getFileName(dataPath, username);
    if (!new File(fileName).exists()) {
        IOUtils.createPrivateFile(fileName);
    }

    final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
    SignalAccount account = new SignalAccount(pair.first(), pair.second());

    account.username = username;
    account.uuid = uuid;
    account.password = password;
    account.profileKey = profileKey;
    account.deviceId = deviceId;
    account.signalingKey = signalingKey;
    account.signalProtocolStore = new JsonSignalProtocolStore(identityKey, registrationId);
    account.groupStore = new JsonGroupStore();
    account.contactStore = new JsonContactsStore();
    account.recipientStore = new RecipientStore();
    account.registered = true;
    account.isMultiDevice = true;

    return account;
}
 
Example 7
Source File: SessionCipher.java    From libsignal-protocol-java with GNU General Public License v3.0 6 votes vote down vote up
private ChainKey getOrCreateChainKey(SessionState sessionState, ECPublicKey theirEphemeral)
    throws InvalidMessageException
{
  try {
    if (sessionState.hasReceiverChain(theirEphemeral)) {
      return sessionState.getReceiverChainKey(theirEphemeral);
    } else {
      RootKey                 rootKey         = sessionState.getRootKey();
      ECKeyPair               ourEphemeral    = sessionState.getSenderRatchetKeyPair();
      Pair<RootKey, ChainKey> receiverChain   = rootKey.createChain(theirEphemeral, ourEphemeral);
      ECKeyPair               ourNewEphemeral = Curve.generateKeyPair();
      Pair<RootKey, ChainKey> senderChain     = receiverChain.first().createChain(theirEphemeral, ourNewEphemeral);

      sessionState.setRootKey(senderChain.first());
      sessionState.addReceiverChain(theirEphemeral, receiverChain.second());
      sessionState.setPreviousCounter(Math.max(sessionState.getSenderChainKey().getIndex()-1, 0));
      sessionState.setSenderChain(ourNewEphemeral, senderChain.second());

      return receiverChain.second();
    }
  } catch (InvalidKeyException e) {
    throw new InvalidMessageException(e);
  }
}
 
Example 8
Source File: SignalAccount.java    From signal-cli with GNU General Public License v3.0 5 votes vote down vote up
public static SignalAccount load(String dataPath, String username) throws IOException {
    final String fileName = getFileName(dataPath, username);
    final Pair<FileChannel, FileLock> pair = openFileChannel(fileName);
    try {
        SignalAccount account = new SignalAccount(pair.first(), pair.second());
        account.load();
        return account;
    } catch (Throwable e) {
        pair.second().close();
        pair.first().close();
        throw e;
    }
}
 
Example 9
Source File: HelpViewModel.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private boolean transformValidationData(Pair<Boolean, Boolean> validationData) {
  return validationData.first() == Boolean.TRUE && validationData.second() == Boolean.TRUE;
}
 
Example 10
Source File: RootKeyTest.java    From libsignal-protocol-java with GNU General Public License v3.0 4 votes vote down vote up
public void testRootKeyDerivationV2() throws NoSuchAlgorithmException, InvalidKeyException {
  byte[] rootKeySeed  = {(byte) 0x7b, (byte) 0xa6, (byte) 0xde, (byte) 0xbc, (byte) 0x2b,
                         (byte) 0xc1, (byte) 0xbb, (byte) 0xf9, (byte) 0x1a, (byte) 0xbb,
                         (byte) 0xc1, (byte) 0x36, (byte) 0x74, (byte) 0x04, (byte) 0x17,
                         (byte) 0x6c, (byte) 0xa6, (byte) 0x23, (byte) 0x09, (byte) 0x5b,
                         (byte) 0x7e, (byte) 0xc6, (byte) 0x6b, (byte) 0x45, (byte) 0xf6,
                         (byte) 0x02, (byte) 0xd9, (byte) 0x35, (byte) 0x38, (byte) 0x94,
                         (byte) 0x2d, (byte) 0xcc};

  byte[] alicePublic  = {(byte) 0x05, (byte) 0xee, (byte) 0x4f, (byte) 0xa6, (byte) 0xcd,
                         (byte) 0xc0, (byte) 0x30, (byte) 0xdf, (byte) 0x49, (byte) 0xec,
                         (byte) 0xd0, (byte) 0xba, (byte) 0x6c, (byte) 0xfc, (byte) 0xff,
                         (byte) 0xb2, (byte) 0x33, (byte) 0xd3, (byte) 0x65, (byte) 0xa2,
                         (byte) 0x7f, (byte) 0xad, (byte) 0xbe, (byte) 0xff, (byte) 0x77,
                         (byte) 0xe9, (byte) 0x63, (byte) 0xfc, (byte) 0xb1, (byte) 0x62,
                         (byte) 0x22, (byte) 0xe1, (byte) 0x3a};

  byte[] alicePrivate = {(byte) 0x21, (byte) 0x68, (byte) 0x22, (byte) 0xec, (byte) 0x67,
                         (byte) 0xeb, (byte) 0x38, (byte) 0x04, (byte) 0x9e, (byte) 0xba,
                         (byte) 0xe7, (byte) 0xb9, (byte) 0x39, (byte) 0xba, (byte) 0xea,
                         (byte) 0xeb, (byte) 0xb1, (byte) 0x51, (byte) 0xbb, (byte) 0xb3,
                         (byte) 0x2d, (byte) 0xb8, (byte) 0x0f, (byte) 0xd3, (byte) 0x89,
                         (byte) 0x24, (byte) 0x5a, (byte) 0xc3, (byte) 0x7a, (byte) 0x94,
                         (byte) 0x8e, (byte) 0x50};

  byte[] bobPublic    = {(byte) 0x05, (byte) 0xab, (byte) 0xb8, (byte) 0xeb, (byte) 0x29,
                         (byte) 0xcc, (byte) 0x80, (byte) 0xb4, (byte) 0x71, (byte) 0x09,
                         (byte) 0xa2, (byte) 0x26, (byte) 0x5a, (byte) 0xbe, (byte) 0x97,
                         (byte) 0x98, (byte) 0x48, (byte) 0x54, (byte) 0x06, (byte) 0xe3,
                         (byte) 0x2d, (byte) 0xa2, (byte) 0x68, (byte) 0x93, (byte) 0x4a,
                         (byte) 0x95, (byte) 0x55, (byte) 0xe8, (byte) 0x47, (byte) 0x57,
                         (byte) 0x70, (byte) 0x8a, (byte) 0x30};

  byte[] nextRoot     = {(byte) 0xb1, (byte) 0x14, (byte) 0xf5, (byte) 0xde, (byte) 0x28,
                         (byte) 0x01, (byte) 0x19, (byte) 0x85, (byte) 0xe6, (byte) 0xeb,
                         (byte) 0xa2, (byte) 0x5d, (byte) 0x50, (byte) 0xe7, (byte) 0xec,
                         (byte) 0x41, (byte) 0xa9, (byte) 0xb0, (byte) 0x2f, (byte) 0x56,
                         (byte) 0x93, (byte) 0xc5, (byte) 0xc7, (byte) 0x88, (byte) 0xa6,
                         (byte) 0x3a, (byte) 0x06, (byte) 0xd2, (byte) 0x12, (byte) 0xa2,
                         (byte) 0xf7, (byte) 0x31};

  byte[] nextChain    = {(byte) 0x9d, (byte) 0x7d, (byte) 0x24, (byte) 0x69, (byte) 0xbc,
                         (byte) 0x9a, (byte) 0xe5, (byte) 0x3e, (byte) 0xe9, (byte) 0x80,
                         (byte) 0x5a, (byte) 0xa3, (byte) 0x26, (byte) 0x4d, (byte) 0x24,
                         (byte) 0x99, (byte) 0xa3, (byte) 0xac, (byte) 0xe8, (byte) 0x0f,
                         (byte) 0x4c, (byte) 0xca, (byte) 0xe2, (byte) 0xda, (byte) 0x13,
                         (byte) 0x43, (byte) 0x0c, (byte) 0x5c, (byte) 0x55, (byte) 0xb5,
                         (byte) 0xca, (byte) 0x5f};

  ECPublicKey  alicePublicKey  = Curve.decodePoint(alicePublic, 0);
  ECPrivateKey alicePrivateKey = Curve.decodePrivatePoint(alicePrivate);
  ECKeyPair    aliceKeyPair    = new ECKeyPair(alicePublicKey, alicePrivateKey);

  ECPublicKey bobPublicKey = Curve.decodePoint(bobPublic, 0);
  RootKey     rootKey      = new RootKey(HKDF.createFor(2), rootKeySeed);

  Pair<RootKey, ChainKey> rootKeyChainKeyPair = rootKey.createChain(bobPublicKey, aliceKeyPair);
  RootKey                 nextRootKey         = rootKeyChainKeyPair.first();
  ChainKey                nextChainKey        = rootKeyChainKeyPair.second();

  assertTrue(Arrays.equals(rootKey.getKeyBytes(), rootKeySeed));
  assertTrue(Arrays.equals(nextRootKey.getKeyBytes(), nextRoot));
  assertTrue(Arrays.equals(nextChainKey.getKey(), nextChain));
}