pgp.c

  1/*!
  2 * @file pgp.c
  3 *
  4 * vim: expandtab:ts=2:sts=2:sw=2
  5 * 
  6 * @authors
  7 * Copyright (C) 2020 Anoxinon e.V.
  8 *
  9 * @copyright 
 10 * This file is part of xmppc.
 11 *
 12 * xmppc is free software: you can redistribute it and/or modify
 13 * it under the terms of the GNU General Public License as published by
 14 * the Free Software Foundation, either version 3 of the License, or
 15 * (at your option) any later version.
 16 *
 17 * xmppc is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20 * GNU General Public License for more details.
 21 *
 22 * You should have received a copy of the GNU General Public License
 23 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 24 *
 25 * German
 26 *
 27 * Diese Datei ist Teil von xmppc.
 28 *
 29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
 30 * der GNU General Public License, wie von der Free Software Foundation,
 31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
 32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
 33 *
 34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
 35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
 36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
 37 * Siehe die GNU General Public License für weitere Details.
 38 *
 39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
 40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
 41 */
 42
 43#define _GNU_SOURCE
 44
 45#include "pgp.h"
 46#include "string.h"
 47
 48#include <locale.h>
 49#include <stdlib.h>
 50#include <gpgme.h>
 51
 52#define PGP_BEGIN "-----BEGIN PGP MESSAGE-----"
 53#define PGP_END   "-----END PGP MESSAGE-----"
 54
 55static char* _pgp_encrypt_message(xmppc_t *xmppc, char* recipient, char* message);
 56static char* _pgp_remove_PGP_MESSAGE_comment(const char* message);
 57
 58static void _pgp_send_text(xmppc_t *xmppc, char* to, char* text);
 59
 60void pgp_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
 61  if(argc > 0) {
 62    if(strcmp("chat", argv[0]) == 0) {
 63      _pgp_send_text(xmppc, argv[1], argv[2]);
 64    } else {
 65      logError(xmppc, "Unbekanner Befehl: %s\n", argv[0]);
 66    }
 67  }
 68  sleep(10);
 69  xmpp_disconnect(xmppc->conn);
 70}
 71
 72void _pgp_send_text(xmppc_t *xmppc, char* to, char* text) {
 73  xmpp_conn_t *conn = xmppc->conn;
 74  xmpp_stanza_t *message;
 75  char* id = xmpp_uuid_gen(xmppc->ctx);
 76  message = xmpp_message_new(xmpp_conn_get_context(conn), "chat", to, id);
 77  int res = xmpp_message_set_body(message, "This message is *encrypted* with PGP (See :XEP:`27`)");
 78  if(res == 0) {
 79    xmpp_stanza_t *x = xmpp_stanza_new(xmppc->ctx);
 80    xmpp_stanza_set_name(x, "x");
 81    xmpp_stanza_set_ns(x, "jabber:x:encrypted");
 82    xmpp_stanza_t *b = xmpp_stanza_new(xmppc->ctx);
 83    char* encrypt_text = _pgp_encrypt_message(xmppc, to,text);
 84    if(encrypt_text == NULL) {
 85      logError(xmppc,"Encrypting of message failed.\n");
 86      return;
 87    }
 88    xmpp_stanza_set_text(b,encrypt_text);
 89    xmpp_stanza_add_child(x, b);
 90    xmpp_stanza_add_child(message, x);
 91    xmpp_stanza_t *e = xmpp_stanza_new(xmppc->ctx);
 92    xmpp_stanza_set_name(e, "encryption");
 93    xmpp_stanza_set_ns(e, "urn:xmpp:eme:0");
 94    xmpp_stanza_set_attribute(e, "namespace", "jabber:x:encrypted");
 95    xmpp_stanza_add_child(message, e);
 96    xmpp_send(conn, message); 
 97  }
 98}
 99
100char* _pgp_encrypt_message(xmppc_t *xmppc, char* recipient, char* message) {
101  setlocale (LC_ALL, "");
102  gpgme_check_version (NULL);
103  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
104  gpgme_ctx_t ctx;
105  gpgme_error_t error = gpgme_new (&ctx);
106  if(GPG_ERR_NO_ERROR != error ) {
107    printf("gpgme_new: %d\n", error);
108    return NULL;
109  }
110  error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
111  if(error != 0) {
112    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
113  }
114  gpgme_set_armor(ctx,1);
115  gpgme_set_textmode(ctx,1);
116  gpgme_set_offline(ctx,1);
117  gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
118
119  gpgme_key_t recp[3];
120
121  // Key for sender
122  const char *jid = xmpp_conn_get_jid(xmppc->conn);
123  logInfo(xmppc, "Looking up pgp key for %s\n", jid);
124  error = gpgme_get_key(ctx, jid, &(recp[0]), 0);
125  if(error != 0) {
126    logError(xmppc,"Public key not found for %s. GpgME Error: %s\n", jid, gpgme_strerror(error));
127    return NULL;
128  }
129
130  // Key for recipient
131  logInfo(xmppc, "Looking up pgp key for %s\n", recipient);
132  error = gpgme_get_key(ctx, recipient, &(recp[1]), 0);
133  if(error != 0) {
134    logError(xmppc,"Key not found for %s. GpgME Error: %s\n", recipient, gpgme_strerror(error));
135    return NULL;
136  }
137  recp[2] = NULL;
138  
139  logInfo(xmppc, "%s <%s>\n", recp[0]->uids->name, recp[0]->uids->email);
140  logInfo(xmppc, "%s <%s>\n", recp[1]->uids->name, recp[1]->uids->email);
141
142  gpgme_encrypt_flags_t flags = 0;
143  gpgme_data_t plain;
144  gpgme_data_t cipher;
145
146  error = gpgme_data_new (&plain);
147  if(error != 0) {
148    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
149  }
150
151  error = gpgme_data_new_from_mem(&plain, message, strlen(message),0);
152  if(error != 0) {
153    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
154  }
155  error = gpgme_data_new (&cipher);  
156  if(error != 0) {
157    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
158  }
159
160  error = gpgme_op_encrypt ( ctx, recp, flags, plain, cipher);
161  if(error != 0) {
162    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
163  }
164  size_t len;
165  char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
166  char* result = NULL;
167  if(len > 0 ){
168    result = _pgp_remove_PGP_MESSAGE_comment(cipher_str);
169  }
170
171  gpgme_key_release (recp[0]);
172  gpgme_key_release (recp[1]);
173  gpgme_release (ctx);
174  return result;
175}
176
177static char* _pgp_remove_PGP_MESSAGE_comment(const char* message) {
178  char* tmp = strndupa(message, strlen(message) - (strlen(PGP_END)+1));
179  tmp = tmp+((strlen(PGP_BEGIN) +1) * sizeof(char));
180  char* result = malloc(strlen(tmp)+1);
181  strcpy(result, tmp);
182  return result;
183}