about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2016-03-29 21:44:18 +0100
committerJames Booth <boothj5@gmail.com>2016-03-29 21:44:18 +0100
commit94b9b1d8e4d359fad8e8878723fa903d4b5b91eb (patch)
tree390ce76a0b51451c0836ef6e49dd919dd3138ce6
parentc6730783bff0d98b7b290ec707e6f56aaa186128 (diff)
parent66467f976f28579009b33b4759616eb31693de50 (diff)
downloadprofani-tty-94b9b1d8e4d359fad8e8878723fa903d4b5b91eb.tar.gz
Merge branch 'master' into stanza-hooks
-rw-r--r--src/pgp/gpg.c41
-rw-r--r--src/pgp/gpg.h2
-rw-r--r--src/xmpp/message.c2
3 files changed, 32 insertions, 13 deletions
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index b04839c3..8900915b 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -593,7 +593,7 @@ p_gpg_sign(const char *const str, const char *const fp)
 }
 
 char*
-p_gpg_encrypt(const char *const barejid, const char *const message)
+p_gpg_encrypt(const char *const barejid, const char *const message, const char *const fp)
 {
     ProfPGPPubKeyId *pubkeyid = g_hash_table_lookup(pubkeys, barejid);
     if (!pubkeyid) {
@@ -603,10 +603,11 @@ p_gpg_encrypt(const char *const barejid, const char *const message)
         return NULL;
     }
 
-    gpgme_key_t keys[2];
+    gpgme_key_t keys[3];
 
     keys[0] = NULL;
     keys[1] = NULL;
+    keys[2] = NULL;
 
     gpgme_ctx_t ctx;
     gpgme_error_t error = gpgme_new(&ctx);
@@ -615,16 +616,23 @@ p_gpg_encrypt(const char *const barejid, const char *const message)
         return NULL;
     }
 
-    gpgme_key_t key;
-    error = gpgme_get_key(ctx, pubkeyid->id, &key, 0);
-
-    if (error || key == NULL) {
-        log_error("GPG: Failed to get key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
+    gpgme_key_t receiver_key;
+    error = gpgme_get_key(ctx, pubkeyid->id, &receiver_key, 0);
+    if (error || receiver_key == NULL) {
+        log_error("GPG: Failed to get receiver_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
         gpgme_release(ctx);
         return NULL;
     }
+    keys[0] = receiver_key;
 
-    keys[0] = key;
+    gpgme_key_t sender_key = NULL;
+    error = gpgme_get_key(ctx, fp, &sender_key, 0);
+    if (error || sender_key == NULL) {
+        log_error("GPG: Failed to get sender_key. %s %s", gpgme_strsource(error), gpgme_strerror(error));
+        gpgme_release(ctx);
+        return NULL;
+    }
+    keys[1] = sender_key;
 
     gpgme_data_t plain;
     gpgme_data_new_from_mem(&plain, message, strlen(message), 1);
@@ -636,7 +644,8 @@ p_gpg_encrypt(const char *const barejid, const char *const message)
     error = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, plain, cipher);
     gpgme_data_release(plain);
     gpgme_release(ctx);
-    gpgme_key_unref(key);
+    gpgme_key_unref(receiver_key);
+    gpgme_key_unref(sender_key);
 
     if (error) {
         log_error("GPG: Failed to encrypt message. %s %s", gpgme_strsource(error), gpgme_strerror(error));
@@ -691,19 +700,29 @@ p_gpg_decrypt(const char *const cipher)
 
     gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx);
     if (res) {
+        GString *recipients_str = g_string_new("");
         gpgme_recipient_t recipient = res->recipients;
-        if (recipient) {
+        while (recipient) {
             gpgme_key_t key;
             error = gpgme_get_key(ctx, recipient->keyid, &key, 1);
 
             if (!error && key) {
                 const char *addr = gpgme_key_get_string_attr(key, GPGME_ATTR_EMAIL, NULL, 0);
                 if (addr) {
-                    log_debug("GPG: Decrypted message for recipient: %s", addr);
+                    g_string_append(recipients_str, addr);
                 }
                 gpgme_key_unref(key);
             }
+
+            if (recipient->next) {
+                g_string_append(recipients_str, ", ");
+            }
+
+            recipient = recipient->next;
         }
+
+        log_debug("GPG: Decrypted message for recipients: %s", recipients_str->str);
+        g_string_free(recipients_str, TRUE);
     }
     gpgme_release(ctx);
 
diff --git a/src/pgp/gpg.h b/src/pgp/gpg.h
index a603dd56..dd2a633b 100644
--- a/src/pgp/gpg.h
+++ b/src/pgp/gpg.h
@@ -64,7 +64,7 @@ gboolean p_gpg_available(const char *const barejid);
 const char* p_gpg_libver(void);
 char* p_gpg_sign(const char *const str, const char *const fp);
 void p_gpg_verify(const char *const barejid, const char *const sign);
-char* p_gpg_encrypt(const char *const barejid, const char *const message);
+char* p_gpg_encrypt(const char *const barejid, const char *const message, const char *const fp);
 char* p_gpg_decrypt(const char *const cipher);
 void p_gpg_free_decrypted(char *decrypted);
 char* p_gpg_autocomplete_key(const char *const search_str);
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 248a56f7..5878a0d5 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -203,7 +203,7 @@ message_send_chat_pgp(const char *const barejid, const char *const msg)
     ProfAccount *account = accounts_get_account(account_name);
     if (account->pgp_keyid) {
         Jid *jidp = jid_create(jid);
-        char *encrypted = p_gpg_encrypt(jidp->barejid, msg);
+        char *encrypted = p_gpg_encrypt(jidp->barejid, msg, account->pgp_keyid);
         if (encrypted) {
             message = stanza_create_message(ctx, id, jid, STANZA_TYPE_CHAT, "This message is encrypted.");
             xmpp_stanza_t *x = xmpp_stanza_new(ctx);