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  xmpp_disconnect(xmppc->conn);
 69}
 70
 71void _pgp_send_text(xmppc_t *xmppc, char* to, char* text) {
 72  xmpp_conn_t *conn = xmppc->conn;
 73  xmpp_stanza_t *message;
 74  char* id = xmpp_uuid_gen(xmppc->ctx);
 75  message = xmpp_message_new(xmpp_conn_get_context(conn), "chat", to, id);
 76  int res = xmpp_message_set_body(message, "This message is *encrypted* with PGP (See :XEP:`27`)");
 77  if(res == 0) {
 78    xmpp_stanza_t *x = xmpp_stanza_new(xmppc->ctx);
 79    xmpp_stanza_set_name(x, "x");
 80    xmpp_stanza_set_ns(x, "jabber:x:encrypted");
 81    xmpp_stanza_t *b = xmpp_stanza_new(xmppc->ctx);
 82    char* encrypt_text = _pgp_encrypt_message(xmppc, to,text);
 83    if(encrypt_text == NULL) {
 84      logError(xmppc,"Encrypting of message failed.\n");
 85      return;
 86    }
 87    xmpp_stanza_set_text(b,encrypt_text);
 88    xmpp_stanza_add_child(x, b);
 89    xmpp_stanza_add_child(message, x);
 90    xmpp_stanza_t *e = xmpp_stanza_new(xmppc->ctx);
 91    xmpp_stanza_set_name(e, "encryption");
 92    xmpp_stanza_set_ns(e, "urn:xmpp:eme:0");
 93    xmpp_stanza_set_attribute(e, "namespace", "jabber:x:encrypted");
 94    xmpp_stanza_add_child(message, e);
 95    xmpp_send(conn, message); 
 96  }
 97}
 98
 99char* _pgp_encrypt_message(xmppc_t *xmppc, char* recipient, char* message) {
100  setlocale (LC_ALL, "");
101  gpgme_check_version (NULL);
102  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
103  gpgme_ctx_t ctx;
104  gpgme_error_t error = gpgme_new (&ctx);
105  if(GPG_ERR_NO_ERROR != error ) {
106    printf("gpgme_new: %d\n", error);
107    return NULL;
108  }
109  error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
110  if(error != 0) {
111    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
112  }
113  gpgme_set_armor(ctx,1);
114  gpgme_set_textmode(ctx,1);
115  gpgme_set_offline(ctx,1);
116  gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
117
118  gpgme_key_t recp[3];
119
120  // Key for sender
121  const char *jid = xmpp_conn_get_jid(xmppc->conn);
122  logInfo(xmppc, "Looking up pgp key for %s\n", jid);
123  error = gpgme_get_key(ctx, jid, &(recp[0]), 0);
124  if(error != 0) {
125    logError(xmppc,"Public key not found for %s. GpgME Error: %s\n", jid, gpgme_strerror(error));
126    return NULL;
127  }
128
129  // Key for recipient
130  logInfo(xmppc, "Looking up pgp key for %s\n", recipient);
131  error = gpgme_get_key(ctx, recipient, &(recp[1]), 0);
132  if(error != 0) {
133    logError(xmppc,"Key not found for %s. GpgME Error: %s\n", recipient, gpgme_strerror(error));
134    return NULL;
135  }
136  recp[2] = NULL;
137  
138  logInfo(xmppc, "%s <%s>\n", recp[0]->uids->name, recp[0]->uids->email);
139  logInfo(xmppc, "%s <%s>\n", recp[1]->uids->name, recp[1]->uids->email);
140
141  gpgme_encrypt_flags_t flags = 0;
142  gpgme_data_t plain;
143  gpgme_data_t cipher;
144
145  error = gpgme_data_new (&plain);
146  if(error != 0) {
147    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
148  }
149
150  error = gpgme_data_new_from_mem(&plain, message, strlen(message),0);
151  if(error != 0) {
152    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
153  }
154  error = gpgme_data_new (&cipher);  
155  if(error != 0) {
156    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
157  }
158
159  error = gpgme_op_encrypt ( ctx, recp, flags, plain, cipher);
160  if(error != 0) {
161    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
162  }
163  size_t len;
164  char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
165  char* result = NULL;
166  if(len > 0 ){
167    result = _pgp_remove_PGP_MESSAGE_comment(cipher_str);
168  }
169
170  gpgme_key_release (recp[0]);
171  gpgme_key_release (recp[1]);
172  gpgme_release (ctx);
173  return result;
174}
175
176static char* _pgp_remove_PGP_MESSAGE_comment(const char* message) {
177  char* tmp = strndupa(message, strlen(message) - (strlen(PGP_END)+1));
178  tmp = tmp+((strlen(PGP_BEGIN) +1) * sizeof(char));
179  char* result = malloc(strlen(tmp)+1);
180  strcpy(result, tmp);
181  return result;
182}