Exemplo n.º 1
0
 /**
  * Put a connection into the PLAINTEXT state, first sending the other side a notice that we're
  * doing so if we're currently ENCRYPTED, and we think he's logged in.
  *
  * @throws OTRException
  */
 public void disconnect(OTRCallbacks callback) throws OTRException {
   if (this.msgState.getCurState() == MsgState.ST_ENCRYPTED
       && this.their_keyid > 0
       && callback.isLoggedIn(accountName, protocol, recName) == 1) {
     TLV[] tlvs = new TLV[1];
     tlvs[0] = new TLV(TLV.DISCONNECTED, new byte[0]);
     DataMessage dm = Proto.createData(this, new byte[0], Proto.MSGFLAGS_IGNORE_UNREADABLE, tlvs);
     callback.injectMessage(accountName, protocol, recName, new String(dm.getContent()));
   }
   forceFinished();
   this.msgState.curState = MsgState.ST_UNENCRYPTED;
   callback.updateContextList();
 }
Exemplo n.º 2
0
  void goEncrypted(OTRCallbacks callback) throws OTRException {

    // See if we're talking to ourselves
    byte[] theiry = auth.their_pub.serialize();
    byte[] their_trim = new byte[theiry.length - 4];
    System.arraycopy(theiry, 4, their_trim, 0, their_trim.length);
    byte[] oury = auth.our_dh.getPublicKey().serialize();
    byte[] our_trim = new byte[oury.length - 4];
    System.arraycopy(oury, 4, our_trim, 0, our_trim.length);

    if (prov.compareMPI(new MPI(their_trim), new MPI(our_trim)) == 0) {
      // Yes, we are.
      callback.handleMsgEvent(OTRCallbacks.OTRL_MSGEVENT_MSG_REFLECTED, this, null);
      throw new OTRException("Message reflected");
    }

    // Find the fingerprint
    FingerPrint found_print = this.findFingerPrint(auth.their_fingerprint, true, callback);

    /* Is this a new session or just a refresh of an existing one? */
    if (this.msgState.getCurState() == MsgState.ST_ENCRYPTED
        && Util.arrayEquals(this.activeFingerprint.fingerPrint, found_print.fingerPrint)
        && this.our_keyid - 1 == this.auth.our_keyid
        && prov.compareMPI(
                MPI.readMPI(new InBuf(this.our_old_dh_key.getPublicKey().serialize())),
                MPI.readMPI(new InBuf(this.auth.our_dh.getPublicKey().serialize())))
            == 0
        && ((this.their_keyid > 0
                && this.their_keyid == auth.their_keyid
                && prov.compareMPI(
                        MPI.readMPI(new InBuf(this.their_y.serialize())),
                        MPI.readMPI(new InBuf(this.auth.their_pub.serialize())))
                    == 0)
            || (this.their_keyid > 1
                && this.their_keyid - 1 == this.auth.their_keyid
                && this.their_old_y != null
                && prov.compareMPI(
                        MPI.readMPI(new InBuf(this.their_y.serialize())),
                        MPI.readMPI(new InBuf(this.auth.their_pub.serialize())))
                    == 0))) {
      /* This is just a refresh of the existing session. */
      callback.stillSecure(this, auth.initiated);
      ignore_message = 1;
      return;
    }

    // Copy the information from the auth into the context
    System.arraycopy(auth.secure_session_id, 0, this.sessionId, 0, 20);
    this.sessionid_len = auth.sessionid_len;

    // Copy the keys
    this.their_keyid = auth.their_keyid;
    this.their_y = auth.their_pub;
    this.their_old_y = null;

    if (our_keyid - 1 != auth.our_keyid) {
      this.our_old_dh_key = auth.our_dh;
      this.our_dh_key = prov.getDHKeyPairGenerator().generateKeyPair();
      this.our_keyid = auth.our_keyid + 1;
    }

    // Create the session keys from the DH keys
    this.sesskeys[0][0] = new DHSesskeys(prov);
    this.sesskeys[1][0] = new DHSesskeys(prov);
    this.sesskeys[0][0].computeSession(this.our_dh_key, this.their_y);
    this.sesskeys[1][0].computeSession(this.our_old_dh_key, this.their_y);

    this.generation++;
    this.activeFingerprint = found_print;
    int oldstate = msgState.getCurState();
    msgState.processEvent(MsgState.EVT_AUTHENTICATED);
    callback.updateContextList();
    if (oldstate == MsgState.ST_ENCRYPTED
        && Util.arrayEquals(this.activeFingerprint.fingerPrint, found_print.fingerPrint)) {
      callback.stillSecure(this, this.auth.initiated);
    } else {
      callback.goneSecure(this);
    }
    this.gone_encrypted = 1;
  }