| 1 | /* |
| 2 | This file is part of MTProto-Server |
| 3 | |
| 4 | MTProto-Server is free software: you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation, either version 2 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | MTProto-Server is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with MTProto-Server. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | This program is released under the GPL with the additional exemption |
| 18 | that compiling, linking, and/or using OpenSSL is allowed. |
| 19 | You are free to remove this exemption from derived works. |
| 20 | |
| 21 | Copyright 2012-2015 Nikolai Durov |
| 22 | 2012-2013 Andrey Lopatin |
| 23 | 2014-2018 Telegram Messenger Inc |
| 24 | */ |
| 25 | #pragma once |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <openssl/rsa.h> |
| 29 | #include <openssl/bn.h> |
| 30 | #include <openssl/aes.h> |
| 31 | |
| 32 | #include "rpc-const.h" |
| 33 | |
| 34 | #define tls_push() { struct tl_out_state *tlio_out = tl_out_state_alloc (); |
| 35 | #define tls_pop() tl_out_state_free (tlio_out); } |
| 36 | #define TLS_START(C) tls_push(); tls_init_tcp_raw_msg (tlio_out, C, 0); |
| 37 | #define TLS_START_UNALIGN(C) tls_push(); tls_init_tcp_raw_msg_unaligned (tlio_out, C, 0); |
| 38 | #define TLS_END tl_store_end_ext (0); tls_pop(); |
| 39 | |
| 40 | /* DH key exchange protocol data structures */ |
| 41 | #define CODE_req_pq 0x60469778 |
| 42 | #define CODE_req_pq_multi 0xbe7e8ef1 |
| 43 | #define CODE_req_DH_params 0xd712e4be |
| 44 | #define CODE_set_client_DH_params 0xf5045f1f |
| 45 | |
| 46 | /* RPC for front/proxy */ |
| 47 | #define RPC_PROXY_REQ 0x36cef1ee |
| 48 | #define RPC_PROXY_ANS 0x4403da0d |
| 49 | #define RPC_CLOSE_CONN 0x1fcf425d |
| 50 | #define RPC_CLOSE_EXT 0x5eb634a2 |
| 51 | #define RPC_SIMPLE_ACK 0x3bac409b |
| 52 | |
| 53 | /* not really a limit, for struct encrypted_message only */ |
| 54 | // #define MAX_MESSAGE_INTS 16384 |
| 55 | #define MAX_MESSAGE_INTS 1048576 |
| 56 | #define MAX_PROTO_MESSAGE_INTS 524288 |
| 57 | |
| 58 | #pragma pack(push,4) |
| 59 | struct encrypted_message { |
| 60 | // unencrypted header |
| 61 | long long auth_key_id; |
| 62 | char msg_key[16]; |
| 63 | // encrypted part, starts with encrypted header |
| 64 | long long server_salt; |
| 65 | long long session_id; |
| 66 | // first message follows |
| 67 | long long msg_id; |
| 68 | int seq_no; |
| 69 | int msg_len; // divisible by 4 |
| 70 | int message[MAX_MESSAGE_INTS + 8]; |
| 71 | }; |
| 72 | |
| 73 | #define 16384 |
| 74 | |
| 75 | struct rpc_proxy_req { |
| 76 | int type; // RPC_PROXY_REQ |
| 77 | int flags; |
| 78 | long long ext_conn_id; |
| 79 | unsigned char remote_ipv6[16]; |
| 80 | int remote_port; |
| 81 | unsigned char our_ipv6[16]; |
| 82 | int our_port; |
| 83 | union { |
| 84 | int data[0]; |
| 85 | struct { |
| 86 | int ; |
| 87 | int [MAX_PROXY_EXTRA_BYTES / 4]; |
| 88 | }; |
| 89 | }; |
| 90 | }; |
| 91 | |
| 92 | struct rpc_proxy_ans { |
| 93 | int type; // RPC_PROXY_ANS |
| 94 | int flags; // +16 = small error packet, +8 = flush immediately |
| 95 | long long ext_conn_id; |
| 96 | int data[]; |
| 97 | }; |
| 98 | |
| 99 | struct rpc_close_conn { |
| 100 | int type; // RPC_CLOSE_CONN |
| 101 | long long ext_conn_id; |
| 102 | }; |
| 103 | |
| 104 | struct rpc_close_ext { |
| 105 | int type; // RPC_CLOSE_EXT |
| 106 | long long ext_conn_id; |
| 107 | }; |
| 108 | |
| 109 | struct rpc_simple_ack { |
| 110 | int type; // RPC_SIMPLE_ACK |
| 111 | long long ext_conn_id; |
| 112 | int confirm_key; |
| 113 | }; |
| 114 | |
| 115 | #pragma pack(pop) |
| 116 | |