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
 53#define PGP_BEGIN "-----BEGIN PGP MESSAGE-----"
 54#define PGP_END   "-----END PGP MESSAGE-----"
 55
 56static char* _pgp_encrypt_message(xmppc_t *xmppc, char* recipient, char* message);
 57static char* _pgp_remove_PGP_MESSAGE_comment(const char* message);
 58
 59static void _pgp_send_text(xmppc_t *xmppc, char* to, char* text);
 60
 61void pgp_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
 62  if(argc > 0) {
 63    if(strcmp("chat", argv[0]) == 0) {
 64      _pgp_send_text(xmppc, argv[1], argv[2]);
 65    } else {
 66      logError(xmppc, "Unbekanner Befehl: %s\n", argv[0]);
 67    }
 68  }
 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(text == NULL) {
 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  gpgme_set_armor(ctx,1);
111  gpgme_set_textmode(ctx,1);
112  gpgme_set_offline(ctx,1);
113  gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
114
115  gpgme_key_t recp[3];
116  const char *jid = xmpp_conn_get_jid(xmppc->conn);
117  error = gpgme_get_key(ctx, jid, &(recp[0]), 0);
118  error = gpgme_get_key(ctx, recipient, &(recp[1]), 0);
119  recp[2] = NULL;
120  
121  logInfo(xmppc, "%s <%s>\n", recp[0]->uids->name, recp[0]->uids->email);
122  logInfo(xmppc, "%s <%s>\n", recp[1]->uids->name, recp[1]->uids->email);
123
124  gpgme_encrypt_flags_t flags = 0;
125  gpgme_data_t plain;
126  gpgme_data_t cipher;
127
128  error = gpgme_data_new (&plain);
129
130  error = gpgme_data_new_from_mem(&plain, message, strlen(message),0);
131  error = gpgme_data_new (&cipher);  
132
133  error = gpgme_op_encrypt ( ctx, recp, flags, plain, cipher);
134  size_t len;
135  char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
136  char* result = NULL;
137  if(len > 0 ){
138    result = _pgp_remove_PGP_MESSAGE_comment(cipher_str);
139  }
140
141  gpgme_key_release (recp[0]);
142  gpgme_key_release (recp[1]);
143  gpgme_release (ctx);
144  return result;
145}
146
147static char* _pgp_remove_PGP_MESSAGE_comment(const char* message) {
148  char* tmp = strndupa(message, strlen(message) - (strlen(PGP_END)+1));
149  tmp = tmp+((strlen(PGP_BEGIN) +1) * sizeof(char));
150  char* result = malloc(strlen(tmp)+1);
151  strcpy(result, tmp);
152  return result;
153}