From a65dcf16a71bac74b16b49b0bb39c11b0f40062f Mon Sep 17 00:00:00 2001 From: Vysheng Date: Thu, 13 Nov 2014 19:22:12 +0300 Subject: [PATCH] added secret_chat_rekey function --- interface.c | 27 +++++++++++++++++++++++++-- loop.c | 17 +++++++++++++++-- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/interface.c b/interface.c index 057b0e0..ed1fcad 100644 --- a/interface.c +++ b/interface.c @@ -862,6 +862,15 @@ void do_create_secret_chat (int arg_num, struct arg args[], struct in_ev *ev) { tgl_do_create_secret_chat (TLS, args[0].P->id, print_secret_chat_gw, ev); } +void do_secret_chat_rekey (int arg_num, struct arg args[], struct in_ev *ev) { + assert (arg_num == 1); + tgl_peer_t *P = args[0].P; + if (P->encr_chat.state == sc_ok) { + vlogprintf (E_WARNING, "START REKEY\n"); + tgl_do_request_exchange (TLS, (void *)P); + } +} + void do_chat_add_user (int arg_num, struct arg args[], struct in_ev *ev) { assert (arg_num == 3); if (ev) { ev->refcnt ++; } @@ -1110,6 +1119,7 @@ struct command commands[] = { {"restore_msg", {ca_number, ca_none}, do_restore_msg, "restore_msg \tRestores message. Only available shortly (one hour?) after deletion"}, {"safe_quit", {ca_none}, do_safe_quit, "safe_quit\tWaits for all queries to end, then quits"}, {"search", {ca_peer | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_number | ca_optional, ca_string_end}, do_search, "search [peer] [limit] [from] [to] [offset] pattern\tSearch for pattern in messages from date from to date to (unixtime) in messages with peer (if peer not present, in all messages)"}, + {"secret_chat_rekey", { ca_secret_chat, ca_none}, do_secret_chat_rekey, "generate new key for active secret chat"}, {"send_audio", {ca_peer, ca_file_name_end, ca_none}, do_send_audio, "send_audio \tSends audio to peer"}, {"send_contact", {ca_peer, ca_string, ca_string, ca_string, ca_none}, do_send_contact, "send_contact \tSends contact (not necessary telegram user)"}, {"send_document", {ca_peer, ca_file_name_end, ca_none}, do_send_document, "send_document \tSends document to peer"}, @@ -2641,8 +2651,21 @@ void print_service_message (struct in_ev *ev, struct tgl_message *M) { mprintf (ev, " is "); print_typing (ev, M->action.typing); break; - default: - assert (0); + case tgl_message_action_noop: + mprintf (ev, " noop\n"); + break; + case tgl_message_action_request_key: + mprintf (ev, " request rekey #%016llx\n", M->action.exchange_id); + break; + case tgl_message_action_accept_key: + mprintf (ev, " accept rekey #%016llx\n", M->action.exchange_id); + break; + case tgl_message_action_commit_key: + mprintf (ev, " commit rekey #%016llx\n", M->action.exchange_id); + break; + case tgl_message_action_abort_key: + mprintf (ev, " abort rekey #%016llx\n", M->action.exchange_id); + break; } mpop_color (ev); //print_end (); diff --git a/loop.c b/loop.c index 1c7892e..1b2b408 100644 --- a/loop.c +++ b/loop.c @@ -62,6 +62,8 @@ #include #include +#include + int verbosity; extern int readline_disabled; @@ -465,6 +467,7 @@ void write_secret_chat (tgl_peer_t *_P, void *extra) { assert (write (fd, &P->state, 4) == 4); assert (write (fd, &P->key_fingerprint, 8) == 8); assert (write (fd, &P->key, 256) == 256); + assert (write (fd, &P->first_key_sha, 20) == 20); assert (write (fd, &P->in_seq_no, 4) == 4); assert (write (fd, &P->last_in_seq_no, 4) == 4); assert (write (fd, &P->out_seq_no, 4) == 4); @@ -476,7 +479,7 @@ void write_secret_chat_file (void) { assert (secret_chat_fd >= 0); int x = SECRET_CHAT_FILE_MAGIC; assert (write (secret_chat_fd, &x, 4) == 4); - x = 1; + x = 2; assert (write (secret_chat_fd, &x, 4) == 4); // version assert (write (secret_chat_fd, &x, 4) == 4); // num @@ -574,6 +577,7 @@ void read_secret_chat (int fd, int v) { long long access_hash, key_fingerprint; static char s[1000]; static unsigned char key[256]; + static unsigned char sha[20]; assert (read (fd, &id, 4) == 4); //assert (read (fd, &flags, 4) == 4); assert (read (fd, &l, 4) == 4); @@ -588,6 +592,9 @@ void read_secret_chat (int fd, int v) { assert (read (fd, &state, 4) == 4); assert (read (fd, &key_fingerprint, 8) == 8); assert (read (fd, &key, 256) == 256); + if (v >= 2) { + assert (read (fd, sha, 20) == 20); + } int in_seq_no = 0, out_seq_no = 0, last_in_seq_no = 0; if (v >= 1) { assert (read (fd, &in_seq_no, 4) == 4); @@ -604,6 +611,12 @@ void read_secret_chat (int fd, int v) { bl_do_encr_chat_set_access_hash (TLS, P, access_hash); bl_do_encr_chat_set_state (TLS, P, state); bl_do_encr_chat_set_key (TLS, P, key, key_fingerprint); + if (v >= 2) { + bl_do_encr_chat_set_sha (TLS, P, sha); + } else { + SHA1 ((void *)key, 256, sha); + bl_do_encr_chat_set_sha (TLS, P, sha); + } if (v >= 1) { bl_do_encr_chat_set_seq (TLS, P, in_seq_no, last_in_seq_no, out_seq_no); } @@ -619,7 +632,7 @@ void read_secret_chat_file (void) { if (x != SECRET_CHAT_FILE_MAGIC) { close (secret_chat_fd); return; } int v = 0; assert (read (secret_chat_fd, &v, 4) == 4); - assert (v == 0 || v == 1); // version + assert (v == 0 || v == 1 || v == 2); // version assert (read (secret_chat_fd, &x, 4) == 4); assert (x >= 0); while (x --> 0) {