private void TL_Set_client_DH_params_answer(TL.Object obj) { if (obj.name.equals("dh_gen_ok")) { // check auth key /* if (!Arrays.equals( obj.getBytes("new_nonce_hash1"), GEN_nonce_hash(1))) { Common.logError("auth_key check failed"); setAuthKey(null); auth(); // transport.disconnect(); return; } */ // done MTProto authorization server_salt = Common.toLong(Common.AXOR(Common.ASUB(new_nonce, 0, 8), Common.ASUB(sv_nonce, 0, 8))); // clean temporary data cl_nonce = sv_nonce = new_nonce = null; g = g_a = dh_prime = null; fp = 0; onAuthorized(); } if (obj.name.equals("dh_gen_retry")) send_client_DH_inner_data(auth_key_aux_hash); if (obj.name.equals("dh_gen_fail")) transport.disconnect(); }
public void setAuthKey(byte[] value) { if (value == null || value.length == 0) { auth_key = new byte[0]; auth_key_aux_hash = auth_key_id = 0; return; } auth_key = value; dcState.set("auth_key", auth_key); byte[] auth_hash = Common.getSHA1(auth_key); auth_key_aux_hash = Common.toLong(Common.ASUB(auth_hash, 0, 8)); auth_key_id = Common.toLong(Common.ASUB(auth_hash, 12, 8)); }
public synchronized boolean sendMessage(Message msg) { if (msg == null) return false; if (!connected || (msg.encrypted && (auth_key == null || auth_key.length == 0)) || bad_seq) { Common.logError("add to queue " + connected + " " + msg.encrypted); TLMessageQueue.add(msg); return false; } ByteBuffer b = null; Common.logInfo("message send: " + msg.obj.name + ":" + msg.obj.type); byte[] msg_data = msg.obj.serialize(); /* if (msg_data.length > 255) { Common.logError("compress..."); byte[] data = Common.gzipDeflate(msg_data); // TODO: compress message data if (data.length < msg_data.length && data.length > 32) msg_data = TL.newObject("gzip_packed", data).serialize(); } */ if (msg.encrypted) { long message_id = GEN_message_id(); TLMessage.put(message_id, msg); int size_real = 32 + msg_data.length; int size_padding = (size_real + 15) / 16 * 16; ByteBuffer p = BufferAlloc(size_padding); p.putLong(server_salt); p.putLong(session); p.putLong(message_id); if (msg.accept) seqno++; p.putInt(seqno); if (msg.accept) seqno++; p.putInt(msg_data.length); p.put(msg_data); byte[] data = p.array(); GEN_random_bytes(data, p.position(), data.length); // encrypt data byte[] msg_key = Common.ASUB(Common.getSHA1(Common.ASUB(data, 0, size_real)), 4, 16); synchronized (aes) { aes.prepare(true, msg_key, auth_key); data = aes.IGE(data, true); } // write encrypted packet b = BufferAlloc(24 + data.length); b.putLong(auth_key_id); b.put(msg_key); b.put(data); } else { b = BufferAlloc(20 + msg_data.length); b.putLong(0); b.putLong(GEN_message_id()); b.putInt(msg_data.length); b.put(msg_data); } try { synchronized (transport) { transport.send(b.array()); return true; } } catch (Exception e) { return false; } }