Exemple #1
0
  private void send_client_DH_inner_data(long retry_id) {
    byte[] b_data = new byte[256];
    Common.random.nextBytes(b_data);

    BigInteger b = new BigInteger(1, b_data);
    BigInteger g_b = g.modPow(b, dh_prime);
    Common.logError(
        "g_b length: " + g_b.toByteArray().length + " -> " + Common.toBytes(g_b).length);

    BigInteger akey = g_a.modPow(b, dh_prime);
    Common.logError("auth_key: " + akey.toString());
    setAuthKey(Common.toBytes(akey));

    // gen data (client_DH_inner_data)
    TL.Object data_obj = TL.newObject("client_DH_inner_data", cl_nonce, sv_nonce, retry_id, g_b);
    byte[] data = data_obj.serialize();
    byte[] hash = Common.getSHA1(data);

    byte[] data_with_hash = new byte[(hash.length + data.length + 15) / 16 * 16];
    System.arraycopy(hash, 0, data_with_hash, 0, hash.length);
    System.arraycopy(data, 0, data_with_hash, hash.length, data.length);

    // send set_client_DH_params
    TL.Object req_obj =
        TL.newObject("set_client_DH_params", cl_nonce, sv_nonce, aes.IGE(data_with_hash, true));
    send(req_obj, false, false);
  }
Exemple #2
0
  public void onReceive(ByteBuffer b) {
    if (b.remaining() == 4) {
      Common.logError("invalid packet:");
      byte[] data = new byte[b.remaining()];
      b.get(data);
      Common.print_dump(data);
      //	retry( TLMessage.keyAt(  TLMessage.size() - 1) );
      return;
    }

    try {
      if (b.getLong() == 0) { // auth_key_id
        b.getLong(); // message_id
        b.getInt(); // message length
        process(TL.deserialize(b));
      } else {
        byte[] msg_key = new byte[16];
        b.get(msg_key);
        byte[] data = new byte[b.remaining()];
        b.get(data);

        synchronized (aes) {
          aes.prepare(false, msg_key, auth_key);
          data = aes.IGE(data, false);
        }

        ByteBuffer btmp = ByteBuffer.wrap(data);
        btmp.order(ByteOrder.LITTLE_ENDIAN);
        server_salt = btmp.getLong();
        btmp.getLong(); // session_id
        cur_message_id = btmp.getLong(); // message_id
        cur_msg_seq = btmp.getInt(); // seq_no
        // if (cur_msg_seq > seqno)
        //	seqno = cur_msg_seq + cur_msg_seq % 2;
        int bsize = btmp.getInt();
        if (bsize < 0 || bsize > 1024 * 1024) {
          Common.print_dump(btmp.array());
          Common.logError(new String(btmp.array()));
          Common.logError("FFFUUUUUUUUUUU!!!");
        }

        b = BufferAlloc(bsize);
        btmp.get(b.array());
        b.getInt();
        ack_message(cur_msg_seq, cur_message_id);
        b.position(0);

        process(TL.deserialize(b));
        send_accept();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemple #3
0
  /*
  	public byte[] GEN_nonce_hash(int id) {
  		byte[] nonce_data = Common.ASUM(Common.ASUM(new_nonce, new byte[]{(byte) id}), auth_key_aux_hash);
  		return Common.ASUB(Common.getSHA1(nonce_data), 4, 16);
  	}
  */
  private void send_accept() {
    if (msg_ack.size() == 0) return;
    /*
    	Common.logInfo("send accept:");
    	for (int i = 0; i < msg_ack.size(); i++)
    		Common.logInfo(" - " + getMessageTime(msg_ack.get(i))  + " " + msg_ack.get(i));
    */
    TL.Object msgs_ack = TL.newObject("msgs_ack", TL.newVector(msg_ack.toArray()));

    send(msgs_ack, true, false);
    msg_ack.clear();
  }
Exemple #4
0
  public static void init() {
    mtp.clear();
    dcStates.clear();
    //	dcStates.add(TL.newObject("joim.dcState", 0, "95.142.192.65", 80, new byte[0], false,
    // Common.random.nextLong(), 0L, 0));
    //	dcStates.add(TL.newObject("joim.dcState", 1, "173.240.5.253", 443, new byte[0], false,
    // Common.random.nextLong(), 0L, 0));
    dcStates.add(
        TL.newObject(
            "joim.dcState", 0, "173.240.5.1", 443, new byte[0], false, GEN_session_id(), 0L, 0));

    dc_date = dc_this = upd_date = upd_pts = upd_seq = 0;
  }
Exemple #5
0
  private void TL_Pong(TL.Object obj) {
    if (obj.name.equals("ping"))
      send(TL.newObject("pong", 0L, obj.getLong("ping_id")), true, false);

    if (obj.name.equals("pong")) {
      /*
      if (!ready) {
      	send_queue();
      	ready = true;
      }
      */
    }
  }
Exemple #6
0
  private void TL_Server_DH_Params(TL.Object obj) {
    if (obj.name.equals("server_DH_params_ok")) {
      aes.prepare(cl_nonce, sv_nonce, new_nonce);
      byte[] answer = aes.IGE(obj.getBytes("encrypted_answer"), false);

      ByteBuffer btmp = ByteBuffer.wrap(answer);
      btmp.order(ByteOrder.LITTLE_ENDIAN);
      btmp.position(20);
      process(TL.deserialize(btmp));
    }

    if (obj.name.equals("server_DH_params_fail")) transport.disconnect();
  }
Exemple #7
0
  private void TL_ResPQ(TL.Object obj) {
    sv_nonce = obj.getBytes("server_nonce");
    BigInteger pq = new BigInteger(1, obj.getBytes("pq"));
    TL.Vector v_fp = obj.getVector("server_public_key_fingerprints");
    fp = v_fp.getLong(0);
    Common.logError("pq: " + pq.toString());

    // prime factorization for pq
    BigInteger q = Common.rho(pq);
    BigInteger p = pq.divide(q);
    if (p.compareTo(q) > 0) {
      BigInteger t = p;
      p = q;
      q = t;
    }
    SecureRandom rnd = new SecureRandom();
    new_nonce = new byte[32];
    rnd.nextBytes(new_nonce);

    // generate encrypted_data
    TL.Object data_obj = TL.newObject("p_q_inner_data", pq, p, q, cl_nonce, sv_nonce, new_nonce);

    byte[] data = data_obj.serialize();
    byte[] hash = Common.getSHA1(data);

    byte[] data_with_hash = new byte[255];
    System.arraycopy(hash, 0, data_with_hash, 0, hash.length);
    System.arraycopy(data, 0, data_with_hash, hash.length, data.length);
    GEN_random_bytes(data_with_hash, data.length + hash.length, 255);

    byte[] encrypted_data = Common.RSA(RSA_MODULUS, RSA_EXPONENT, data_with_hash);

    // req_DH_params
    TL.Object req_obj = TL.newObject("req_DH_params", cl_nonce, sv_nonce, p, q, fp, encrypted_data);
    send(req_obj, false, false);
  }
Exemple #8
0
  private void TL_Config(TL.Object obj) {
    dc_date = obj.getInt("date");
    MTProto.dc_this = obj.getInt("this_dc");
    dcState.set("id", dc_this);
    updateDcStates();

    TL.Vector dc_options = obj.getVector("dc_options");

    ArrayList<TL.Object> new_dcStates = new ArrayList<TL.Object>();

    for (int i = 0; i < dc_options.count; i++) {
      TL.Object dcObj = dc_options.getObject(i);
      int id = dcObj.getInt("id");

      TL.Object state =
          TL.newObject(
              "joim.dcState",
              id,
              dcObj.getString("ip_address"),
              dcObj.getInt("port"),
              new byte[0],
              false,
              GEN_session_id(),
              0L,
              0);

      for (int j = 0; j < MTProto.dcStates.size(); j++) {
        TL.Object item = MTProto.dcStates.get(j);
        if (id == item.getInt("id") && state.getString("ip").equals(item.getString("ip"))) {
          state.set("auth_key", item.getBytes("auth_key"));
          state.set("bind", item.getBool("bind"));
          state.set("session", item.getLong("session"));
          state.set("server_salt", item.getLong("server_salt"));
          state.set("seqno", item.getInt("seqno"));
          if (state.getInt("port") == item.getInt("port")) dcState = state;
          break;
        }
      }

      new_dcStates.add(state);
    }

    dcStates = new_dcStates;
    send_queue();
    cb.onReady();
  }
Exemple #9
0
 private void send_ping() {
   send(TL.newObject("ping", Common.random.nextLong()), true, false);
 }
Exemple #10
0
 public void auth() {
   Common.logError("auth");
   TL.Object req_pq = TL.newObject("req_pq", cl_nonce = GEN_random_bytes(16));
   send(req_pq, false, false);
 }
Exemple #11
0
 public void api_contacts_importContacts(ArrayList<TL.Object> contacts, boolean replace) {
   api(null, null, "contacts.importContacts", TL.newVector(contacts.toArray()), replace);
 }
Exemple #12
0
 // API
 public void api(TL.OnResultRPC result, Object param, String method, Object... params) {
   send(TL.newObject(method, params), true, true, result, param);
 }