about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2014-04-26 22:52:25 +0100
committerJames Booth <boothj5@gmail.com>2014-04-26 22:52:25 +0100
commitaff9eee4331a49cabdf9f64e7e03ec3e781c991c (patch)
treef4ba3e5f08c70e822d34e7dad190b29d99f47187
parent06d81ed9ce7808a23267718ad24c32f957d95109 (diff)
downloadprofani-tty-aff9eee4331a49cabdf9f64e7e03ec3e781c991c.tar.gz
Handle SMP secret response
-rw-r--r--src/command/commands.c4
-rw-r--r--src/otr/otr.c16
-rw-r--r--src/otr/otr.h2
-rw-r--r--src/otr/otrlib.h2
-rw-r--r--src/otr/otrlibv3.c16
5 files changed, 30 insertions, 10 deletions
diff --git a/src/command/commands.c b/src/command/commands.c
index 915fbe17..3b066cf7 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -2800,8 +2800,8 @@ cmd_otr(gchar **args, struct cmd_help_t help)
                 cons_show("Usage: %s", help.usage);
             } else {
                 char *recipient = ui_current_recipient();
-                otr_smp_init_secret(recipient, secret);
-                ui_current_print_formatted_line('!', 0, "OTR secret entered", secret);
+                otr_smp_secret(recipient, secret);
+                ui_current_print_formatted_line('!', 0, "OTR secret entered");
             }
         }
         return TRUE;
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 462f6666..348b7620 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -41,6 +41,7 @@ static OtrlUserState user_state;
 static OtrlMessageAppOps ops;
 static char *jid;
 static gboolean data_loaded;
+static GHashTable *smp_initiators;
 
 // ops callbacks
 static OtrlPolicy
@@ -136,6 +137,8 @@ _otr_init(void)
 
     otrlib_init_ops(&ops);
 
+    smp_initiators = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+
     data_loaded = FALSE;
 }
 
@@ -389,7 +392,7 @@ _otr_untrust(const char * const recipient)
 }
 
 static void
-_otr_smp_init_secret(const char * const recipient, const char *secret)
+_otr_smp_secret(const char * const recipient, const char *secret)
 {
     ConnContext *context = otrlib_context_find(user_state, recipient, jid);
 
@@ -401,7 +404,12 @@ _otr_smp_init_secret(const char * const recipient, const char *secret)
         return;
     }
 
-    otrl_message_initiate_smp(user_state, &ops, NULL, context, (const unsigned char*)secret, strlen(secret));
+    // if recipient initiated SMP, send response, else initialise
+    if (g_hash_table_contains(smp_initiators, recipient)) {
+        otrl_message_respond_smp(user_state, &ops, NULL, context, (const unsigned char*)secret, strlen(secret));
+    } else {
+        otrl_message_initiate_smp(user_state, &ops, NULL, context, (const unsigned char*)secret, strlen(secret));
+    }
 }
 
 static void
@@ -471,7 +479,7 @@ _otr_decrypt_message(const char * const from, const char * const message, gboole
         }
 
         // library version specific tlv handling
-        otrlib_handle_tlvs(user_state, &ops, context, tlvs);
+        otrlib_handle_tlvs(user_state, &ops, context, tlvs, smp_initiators);
 
         return NULL;
 
@@ -512,5 +520,5 @@ otr_init_module(void)
     otr_encrypt_message = _otr_encrypt_message;
     otr_decrypt_message = _otr_decrypt_message;
     otr_free_message = _otr_free_message;
-    otr_smp_init_secret = _otr_smp_init_secret;
+    otr_smp_secret = _otr_smp_secret;
 }
diff --git a/src/otr/otr.h b/src/otr/otr.h
index 39eecab6..443ee67c 100644
--- a/src/otr/otr.h
+++ b/src/otr/otr.h
@@ -40,7 +40,7 @@ gboolean (*otr_is_trusted)(const char * const recipient);
 void (*otr_trust)(const char * const recipient);
 void (*otr_untrust)(const char * const recipient);
 
-void (*otr_smp_init_secret)(const char * const recipient, const char *secret);
+void (*otr_smp_secret)(const char * const recipient, const char *secret);
 
 void (*otr_end_session)(const char * const recipient);
 
diff --git a/src/otr/otrlib.h b/src/otr/otrlib.h
index df2144a6..263e44df 100644
--- a/src/otr/otrlib.h
+++ b/src/otr/otrlib.h
@@ -39,6 +39,6 @@ gcry_error_t otrlib_encrypt_message(OtrlUserState user_state, OtrlMessageAppOps
 int otrlib_decrypt_message(OtrlUserState user_state, OtrlMessageAppOps *ops, char *jid, const char * const from,
     const char * const message, char **decrypted, OtrlTLV **tlvs);
 
-void otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext *context, OtrlTLV *tlvs);
+void otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext *context, OtrlTLV *tlvs, GHashTable *smp_initiators);
 
 #endif
diff --git a/src/otr/otrlibv3.c b/src/otr/otrlibv3.c
index 01714f9f..76061c5d 100644
--- a/src/otr/otrlibv3.c
+++ b/src/otr/otrlibv3.c
@@ -108,7 +108,7 @@ otrlib_decrypt_message(OtrlUserState user_state, OtrlMessageAppOps *ops, char *j
 }
 
 void
-otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext *context, OtrlTLV *tlvs)
+otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext *context, OtrlTLV *tlvs, GHashTable *smp_initiators)
 {
     NextExpectedSMP nextMsg = context->smstate->nextExpected;
     OtrlTLV *tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP1);
@@ -116,8 +116,9 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext
         if (nextMsg != OTRL_SMP_EXPECT1) {
             otrl_message_abort_smp(user_state, ops, NULL, context);
         } else {
-            cons_debug("%s initiated SMP", context->username);
             // [get secret from user and continue SMP];
+            cons_debug("%s initiated SMP with secret", context->username);
+            g_hash_table_insert(smp_initiators, strdup(context->username), strdup(context->username));
         }
     }
     tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP2);
@@ -138,6 +139,11 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext
             // We will not expect more messages, so prepare for next SMP
             context->smstate->nextExpected = OTRL_SMP_EXPECT1;
             // Report result to user
+            if ((context->active_fingerprint->trust != NULL) && (context->active_fingerprint->trust[0] != '\0')) {
+                cons_debug("SMP SUCCESSFUL");
+            } else {
+                cons_debug("SMP UNSUCCESSFUL");
+            }
         }
     }
     tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP4);
@@ -148,6 +154,11 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext
             // We will not expect more messages, so prepare for next SMP
             context->smstate->nextExpected = OTRL_SMP_EXPECT1;
             // Report result to user
+            if ((context->active_fingerprint->trust != NULL) && (context->active_fingerprint->trust[0] != '\0')) {
+                cons_debug("SMP SUCCESSFUL");
+            } else {
+                cons_debug("SMP UNSUCCESSFUL");
+            }
         }
     }
     tlv = otrl_tlv_find(tlvs, OTRL_TLV_SMP_ABORT);
@@ -155,5 +166,6 @@ otrlib_handle_tlvs(OtrlUserState user_state, OtrlMessageAppOps *ops, ConnContext
         // The message we are waiting for will not arrive, so reset
         // and prepare for the next SMP
         context->smstate->nextExpected = OTRL_SMP_EXPECT1;
+        cons_debug("SMP ABORTED");
     }
 }
\ No newline at end of file