當前位置: 首頁>>代碼示例>>Java>>正文


Java SessionState類代碼示例

本文整理匯總了Java中org.whispersystems.libaxolotl.state.SessionState的典型用法代碼示例。如果您正苦於以下問題:Java SessionState類的具體用法?Java SessionState怎麽用?Java SessionState使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SessionState類屬於org.whispersystems.libaxolotl.state包,在下文中一共展示了SessionState類的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getOrCreateChainKey

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
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);
  }
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:25,代碼來源:SessionCipher.java

示例2: loadSession

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
@Override
public SessionRecord loadSession(long recipientId, int deviceId) {
  synchronized (FILE_LOCK) {
    try {
      MasterCipher cipher = new MasterCipher(masterSecret);
      FileInputStream in     = new FileInputStream(getSessionFile(recipientId, deviceId));

      int versionMarker  = readInteger(in);

      if (versionMarker > CURRENT_VERSION) {
        throw new AssertionError("Unknown version: " + versionMarker);
      }

      byte[] serialized = cipher.decryptBytes(readBlob(in));
      in.close();

      if (versionMarker == SINGLE_STATE_VERSION) {
        SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
        SessionState     sessionState     = new SessionState(sessionStructure);
        return new SessionRecord(sessionState);
      } else if (versionMarker == ARCHIVE_STATES_VERSION) {
        return new SessionRecord(serialized);
      } else {
        throw new AssertionError("Unknown version: " + versionMarker);
      }
    } catch (InvalidMessageException | IOException e) {
      Log.w(TAG, "No existing session information found.");
      return new SessionRecord();
    }
  }
}
 
開發者ID:redcracker,項目名稱:TextSecure,代碼行數:32,代碼來源:TextSecureSessionStore.java

示例3: loadSession

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
@Override
public SessionRecord loadSession(AxolotlAddress address) {
  synchronized (FILE_LOCK) {
    try {
      MasterCipher    cipher = new MasterCipher(masterSecret);
      FileInputStream in     = new FileInputStream(getSessionFile(address));

      int versionMarker  = readInteger(in);

      if (versionMarker > CURRENT_VERSION) {
        throw new AssertionError("Unknown version: " + versionMarker);
      }

      byte[] serialized = cipher.decryptBytes(readBlob(in));
      in.close();

      if (versionMarker == SINGLE_STATE_VERSION) {
        SessionStructure sessionStructure = SessionStructure.parseFrom(serialized);
        SessionState     sessionState     = new SessionState(sessionStructure);
        return new SessionRecord(sessionState);
      } else if (versionMarker == ARCHIVE_STATES_VERSION) {
        return new SessionRecord(serialized);
      } else {
        throw new AssertionError("Unknown version: " + versionMarker);
      }
    } catch (InvalidMessageException | IOException e) {
      Log.w(TAG, "No existing session information found.");
      return new SessionRecord();
    }
  }
}
 
開發者ID:Agilitum,項目名稱:TextSecureSMP,代碼行數:32,代碼來源:TextSecureSessionStore.java

示例4: decrypt

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
private byte[] decrypt(SessionState sessionState, WhisperMessage ciphertextMessage)
    throws InvalidMessageException, DuplicateMessageException, LegacyMessageException
{
  if (!sessionState.hasSenderChain()) {
    throw new InvalidMessageException("Uninitialized session!");
  }

  if (ciphertextMessage.getMessageVersion() != sessionState.getSessionVersion()) {
    throw new InvalidMessageException(String.format("Message version %d, but session version %d",
                                                    ciphertextMessage.getMessageVersion(),
                                                    sessionState.getSessionVersion()));
  }

  int            messageVersion    = ciphertextMessage.getMessageVersion();
  ECPublicKey    theirEphemeral    = ciphertextMessage.getSenderRatchetKey();
  int            counter           = ciphertextMessage.getCounter();
  ChainKey       chainKey          = getOrCreateChainKey(sessionState, theirEphemeral);
  MessageKeys    messageKeys       = getOrCreateMessageKeys(sessionState, theirEphemeral,
                                                            chainKey, counter);

  ciphertextMessage.verifyMac(messageVersion,
                              sessionState.getRemoteIdentityKey(),
                              sessionState.getLocalIdentityKey(),
                              messageKeys.getMacKey());

  byte[] plaintext = getPlaintext(messageVersion, messageKeys, ciphertextMessage.getBody());

  sessionState.clearUnacknowledgedPreKeyMessage();

  return plaintext;
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:32,代碼來源:SessionCipher.java

示例5: getOrCreateMessageKeys

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
private MessageKeys getOrCreateMessageKeys(SessionState sessionState,
                                           ECPublicKey theirEphemeral,
                                           ChainKey chainKey, int counter)
    throws InvalidMessageException, DuplicateMessageException
{
  if (chainKey.getIndex() > counter) {
    if (sessionState.hasMessageKeys(theirEphemeral, counter)) {
      return sessionState.removeMessageKeys(theirEphemeral, counter);
    } else {
      throw new DuplicateMessageException("Received message with old counter: " +
                                              chainKey.getIndex() + " , " + counter);
    }
  }

  if (chainKey.getIndex() - counter > 2000) {
    throw new InvalidMessageException("Over 2000 messages into the future!");
  }

  while (chainKey.getIndex() < counter) {
    MessageKeys messageKeys = chainKey.getMessageKeys();
    sessionState.setMessageKeys(theirEphemeral, messageKeys);
    chainKey = chainKey.getNextChainKey();
  }

  sessionState.setReceiverChainKey(theirEphemeral, chainKey.getNextChainKey());
  return chainKey.getMessageKeys();
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:28,代碼來源:SessionCipher.java

示例6: initializeSession

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
public static void initializeSession(SessionState sessionState,
                                     int sessionVersion,
                                     SymmetricAxolotlParameters parameters)
    throws InvalidKeyException
{
  if (isAlice(parameters.getOurBaseKey().getPublicKey(), parameters.getTheirBaseKey())) {
    AliceAxolotlParameters.Builder aliceParameters = AliceAxolotlParameters.newBuilder();

    aliceParameters.setOurBaseKey(parameters.getOurBaseKey())
                   .setOurIdentityKey(parameters.getOurIdentityKey())
                   .setTheirRatchetKey(parameters.getTheirRatchetKey())
                   .setTheirIdentityKey(parameters.getTheirIdentityKey())
                   .setTheirSignedPreKey(parameters.getTheirBaseKey())
                   .setTheirOneTimePreKey(Optional.<ECPublicKey>absent());

    RatchetingSession.initializeSession(sessionState, sessionVersion, aliceParameters.create());
  } else {
    BobAxolotlParameters.Builder bobParameters = BobAxolotlParameters.newBuilder();

    bobParameters.setOurIdentityKey(parameters.getOurIdentityKey())
                 .setOurRatchetKey(parameters.getOurRatchetKey())
                 .setOurSignedPreKey(parameters.getOurBaseKey())
                 .setOurOneTimePreKey(Optional.<ECKeyPair>absent())
                 .setTheirBaseKey(parameters.getTheirBaseKey())
                 .setTheirIdentityKey(parameters.getTheirIdentityKey());

    RatchetingSession.initializeSession(sessionState, sessionVersion, bobParameters.create());
  }
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:30,代碼來源:RatchetingSession.java

示例7: initializeSessionsV2

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
private void initializeSessionsV2(SessionState aliceSessionState, SessionState bobSessionState)
    throws InvalidKeyException
{
  ECKeyPair       aliceIdentityKeyPair = Curve.generateKeyPair();
  IdentityKeyPair aliceIdentityKey     = new IdentityKeyPair(new IdentityKey(aliceIdentityKeyPair.getPublicKey()),
                                                             aliceIdentityKeyPair.getPrivateKey());
  ECKeyPair       aliceBaseKey         = Curve.generateKeyPair();
  ECKeyPair       aliceEphemeralKey    = Curve.generateKeyPair();

  ECKeyPair       bobIdentityKeyPair   = Curve.generateKeyPair();
  IdentityKeyPair bobIdentityKey       = new IdentityKeyPair(new IdentityKey(bobIdentityKeyPair.getPublicKey()),
                                                             bobIdentityKeyPair.getPrivateKey());
  ECKeyPair       bobBaseKey           = Curve.generateKeyPair();
  ECKeyPair       bobEphemeralKey      = bobBaseKey;

  AliceAxolotlParameters aliceParameters = AliceAxolotlParameters.newBuilder()
      .setOurIdentityKey(aliceIdentityKey)
      .setOurBaseKey(aliceBaseKey)
      .setTheirIdentityKey(bobIdentityKey.getPublicKey())
      .setTheirSignedPreKey(bobEphemeralKey.getPublicKey())
      .setTheirRatchetKey(bobEphemeralKey.getPublicKey())
      .setTheirOneTimePreKey(Optional.<ECPublicKey>absent())
      .create();

  BobAxolotlParameters bobParameters = BobAxolotlParameters.newBuilder()
      .setOurIdentityKey(bobIdentityKey)
      .setOurOneTimePreKey(Optional.<ECKeyPair>absent())
      .setOurRatchetKey(bobEphemeralKey)
      .setOurSignedPreKey(bobBaseKey)
      .setTheirBaseKey(aliceBaseKey.getPublicKey())
      .setTheirIdentityKey(aliceIdentityKey.getPublicKey())
      .create();

  RatchetingSession.initializeSession(aliceSessionState, 2, aliceParameters);
  RatchetingSession.initializeSession(bobSessionState, 2, bobParameters);
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:37,代碼來源:SessionCipherTest.java

示例8: initializeSessionsV3

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
private void initializeSessionsV3(SessionState aliceSessionState, SessionState bobSessionState)
    throws InvalidKeyException
{
  ECKeyPair       aliceIdentityKeyPair = Curve.generateKeyPair();
  IdentityKeyPair aliceIdentityKey     = new IdentityKeyPair(new IdentityKey(aliceIdentityKeyPair.getPublicKey()),
                                                             aliceIdentityKeyPair.getPrivateKey());
  ECKeyPair       aliceBaseKey         = Curve.generateKeyPair();
  ECKeyPair       aliceEphemeralKey    = Curve.generateKeyPair();

  ECKeyPair       alicePreKey          = aliceBaseKey;

  ECKeyPair       bobIdentityKeyPair   = Curve.generateKeyPair();
  IdentityKeyPair bobIdentityKey       = new IdentityKeyPair(new IdentityKey(bobIdentityKeyPair.getPublicKey()),
                                                             bobIdentityKeyPair.getPrivateKey());
  ECKeyPair       bobBaseKey           = Curve.generateKeyPair();
  ECKeyPair       bobEphemeralKey      = bobBaseKey;

  ECKeyPair       bobPreKey            = Curve.generateKeyPair();

  AliceAxolotlParameters aliceParameters = AliceAxolotlParameters.newBuilder()
      .setOurBaseKey(aliceBaseKey)
      .setOurIdentityKey(aliceIdentityKey)
      .setTheirOneTimePreKey(Optional.<ECPublicKey>absent())
      .setTheirRatchetKey(bobEphemeralKey.getPublicKey())
      .setTheirSignedPreKey(bobBaseKey.getPublicKey())
      .setTheirIdentityKey(bobIdentityKey.getPublicKey())
      .create();

  BobAxolotlParameters bobParameters = BobAxolotlParameters.newBuilder()
      .setOurRatchetKey(bobEphemeralKey)
      .setOurSignedPreKey(bobBaseKey)
      .setOurOneTimePreKey(Optional.<ECKeyPair>absent())
      .setOurIdentityKey(bobIdentityKey)
      .setTheirIdentityKey(aliceIdentityKey.getPublicKey())
      .setTheirBaseKey(aliceBaseKey.getPublicKey())
      .create();

  RatchetingSession.initializeSession(aliceSessionState, 3, aliceParameters);
  RatchetingSession.initializeSession(bobSessionState, 3, bobParameters);
}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:41,代碼來源:SessionCipherTest.java

示例9: processResponse

import org.whispersystems.libaxolotl.state.SessionState; //導入依賴的package包/類
private void processResponse(KeyExchangeMessage message)
    throws StaleKeyExchangeException, InvalidKeyException
{
  SessionRecord sessionRecord                  = sessionStore.loadSession(recipientId, deviceId);
  SessionState  sessionState                   = sessionRecord.getSessionState();
  boolean       hasPendingKeyExchange          = sessionState.hasPendingKeyExchange();
  boolean       isSimultaneousInitiateResponse = message.isResponseForSimultaneousInitiate();

  if (!hasPendingKeyExchange || sessionState.getPendingKeyExchangeSequence() != message.getSequence()) {
    Log.w(TAG, "No matching sequence for response. Is simultaneous initiate response: " + isSimultaneousInitiateResponse);
    if (!isSimultaneousInitiateResponse) throw new StaleKeyExchangeException();
    else                                 return;
  }

  SymmetricAxolotlParameters.Builder parameters = SymmetricAxolotlParameters.newBuilder();

  parameters.setOurBaseKey(sessionRecord.getSessionState().getPendingKeyExchangeBaseKey())
            .setOurRatchetKey(sessionRecord.getSessionState().getPendingKeyExchangeRatchetKey())
            .setOurIdentityKey(sessionRecord.getSessionState().getPendingKeyExchangeIdentityKey())
            .setTheirBaseKey(message.getBaseKey())
            .setTheirRatchetKey(message.getRatchetKey())
            .setTheirIdentityKey(message.getIdentityKey());

  if (!sessionRecord.isFresh()) sessionRecord.archiveCurrentState();

  RatchetingSession.initializeSession(sessionRecord.getSessionState(),
                                      Math.min(message.getMaxVersion(), CiphertextMessage.CURRENT_VERSION),
                                      parameters.create());

  if (sessionRecord.getSessionState().getSessionVersion() >= 3 &&
      !Curve.verifySignature(message.getIdentityKey().getPublicKey(),
                             message.getBaseKey().serialize(),
                             message.getBaseKeySignature()))
  {
    throw new InvalidKeyException("Base key signature doesn't match!");
  }

  sessionStore.storeSession(recipientId, deviceId, sessionRecord);
  identityKeyStore.saveIdentity(recipientId, message.getIdentityKey());

}
 
開發者ID:Securecom,項目名稱:Securecom-Messaging,代碼行數:42,代碼來源:SessionBuilder.java


注:本文中的org.whispersystems.libaxolotl.state.SessionState類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。