openpgp.c

  1/*!
  2 * @file openpgp.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 "openpgp.h"
 46#include "string.h"
 47
 48#include <locale.h>
 49#include <stdlib.h>
 50#include <glib.h>
 51#include <gpgme.h>
 52
 53static void _openpgp_send_text(xmppc_t *xmppc, char* to, char* text);
 54static xmpp_stanza_t* _openpgp_signcrypt(xmppc_t *xmppc, char* to, char* text);
 55static char* _openpgp_gpg_signcrypt(xmppc_t *xmppc, char* recipient, char* message); 
 56static gpgme_error_t _openpgp_lookup_key(xmppc_t *xmppc, char* name, gpgme_ctx_t* ctx, gpgme_key_t* key);
 57
 58void openpgp_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
 59  if(argc > 0) {
 60    if(strcmp("signcrypt", argv[0]) == 0) {
 61      _openpgp_send_text(xmppc, argv[1], argv[2]);
 62    } else {
 63      logError(xmppc, "Unbekanner Befehl: %s\n", argv[0]);
 64    }
 65  }
 66  xmpp_disconnect(xmppc->conn);
 67}
 68
 69void _openpgp_send_text(xmppc_t *xmppc, char* to, char* text) {
 70  xmpp_conn_t *conn = xmppc->conn;
 71  xmpp_stanza_t *message;
 72  char* id = xmpp_uuid_gen(xmppc->ctx);
 73  message = xmpp_message_new(xmpp_conn_get_context(conn), NULL, to, id);
 74  xmpp_message_set_body(message, "This message is *encrypted* with OpenPGP (See :XEP:`0373`)");
 75    xmpp_stanza_t *openpgp = xmpp_stanza_new(xmppc->ctx);
 76    xmpp_stanza_set_name(openpgp, "openpgp");
 77    xmpp_stanza_set_ns(openpgp, "urn:xmpp:openpgp:0");
 78    
 79    xmpp_stanza_t * signcrypt = _openpgp_signcrypt(xmppc, to, text);
 80    char* c;
 81    size_t s;
 82    xmpp_stanza_to_text(signcrypt, &c,&s);
 83    char* signcrypt_e = _openpgp_gpg_signcrypt(xmppc,to, c);
 84    if( signcrypt_e == NULL ) {
 85      logError(xmppc, "Message not signcrypted.\n");
 86      return;
 87    } 
 88    // BASE64_OPENPGP_MESSAGE
 89    xmpp_stanza_t* base64_openpgp_message = xmpp_stanza_new(xmppc->ctx);
 90    xmpp_stanza_set_text(base64_openpgp_message,signcrypt_e);
 91    xmpp_stanza_add_child(openpgp, base64_openpgp_message);
 92    xmpp_stanza_add_child(message, openpgp);
 93    
 94    xmpp_stanza_to_text(message, &c,&s);
 95    
 96    xmpp_send(conn, message); 
 97   
 98}
 99
100xmpp_stanza_t* _openpgp_signcrypt(xmppc_t *xmppc, char* to, char* text) {
101
102  time_t now = time(NULL);
103  struct tm* tm = localtime(&now);
104  char buf[255];
105  strftime(buf, sizeof(buf), "%FT%T%z", tm);
106    int randnr = rand() % 5;
107    char rpad_data[randnr];
108    for(int i = 0; i < randnr-1; i++) {
109      rpad_data[i] = 'c';
110    }
111    rpad_data[randnr-1] = '\0';
112    
113    // signcrypt
114    xmpp_stanza_t *signcrypt = xmpp_stanza_new(xmppc->ctx);
115    xmpp_stanza_set_name(signcrypt, "signcrypt");
116    xmpp_stanza_set_ns(signcrypt, "urn:xmpp:openpgp:0");
117    // to
118    xmpp_stanza_t *s_to = xmpp_stanza_new(xmppc->ctx);
119    xmpp_stanza_set_name(s_to, "to");
120    xmpp_stanza_set_attribute(s_to, "jid", to);
121    // time
122    xmpp_stanza_t *time = xmpp_stanza_new(xmppc->ctx);
123    xmpp_stanza_set_name(time, "time");
124    xmpp_stanza_set_attribute(time, "stamp", buf);
125    xmpp_stanza_set_name(time, "time");
126    // rpad
127    xmpp_stanza_t *rpad = xmpp_stanza_new(xmppc->ctx);
128    xmpp_stanza_set_name(rpad, "rpad");
129    xmpp_stanza_t *rpad_text = xmpp_stanza_new(xmppc->ctx);
130    xmpp_stanza_set_text(rpad_text, rpad_data);
131    // payload
132    xmpp_stanza_t *payload= xmpp_stanza_new(xmppc->ctx);
133    xmpp_stanza_set_name(payload, "payload");
134    // body
135    xmpp_stanza_t *body = xmpp_stanza_new(xmppc->ctx);
136    xmpp_stanza_set_name(body, "body");
137    xmpp_stanza_set_ns(body, "jabber:client");
138    // text
139    xmpp_stanza_t *body_text = xmpp_stanza_new(xmppc->ctx);
140    xmpp_stanza_set_text(body_text, text);
141    xmpp_stanza_add_child(signcrypt,s_to);
142    xmpp_stanza_add_child(signcrypt,time);
143    xmpp_stanza_add_child(signcrypt,rpad);
144    xmpp_stanza_add_child(rpad,rpad_text);
145    xmpp_stanza_add_child(signcrypt,payload);
146    xmpp_stanza_add_child(payload, body);
147    xmpp_stanza_add_child(body, body_text);
148
149    return signcrypt;
150}
151
152char* _openpgp_gpg_signcrypt(xmppc_t *xmppc, char* recipient, char* message) {
153  setlocale (LC_ALL, "");
154  gpgme_check_version (NULL);
155  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
156  gpgme_ctx_t ctx;
157  gpgme_error_t error = gpgme_new (&ctx);
158  if(GPG_ERR_NO_ERROR != error ) {
159    printf("gpgme_new: %d\n", error);
160    return NULL;
161  }
162  error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
163  if(error != 0) {
164    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
165  }
166  gpgme_set_armor(ctx,0);
167  gpgme_set_textmode(ctx,0);
168  gpgme_set_offline(ctx,1);
169  gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
170  if(error != 0) {
171    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
172  }
173
174  gpgme_key_t recp[3];
175  recp[0] = NULL,
176  recp[1] = NULL;
177  const char *jid = xmpp_conn_get_jid(xmppc->conn);
178  
179  char* xmpp_jid_me = alloca( (strlen(jid)+6) * sizeof(char) );
180  char* xmpp_jid_recipient =  alloca( (strlen(recipient)+6) * sizeof(char) );
181
182  strcpy(xmpp_jid_me, "xmpp:");
183  strcpy(xmpp_jid_recipient, "xmpp:");
184  strcat(xmpp_jid_me, jid);
185  strcat(xmpp_jid_recipient,recipient);
186
187  gpgme_signers_clear(ctx);
188
189  // lookup own key
190  error = _openpgp_lookup_key(xmppc,xmpp_jid_me, &ctx, &recp[0]);
191  if(error != 0) {
192    logError(xmppc,"Key not found for %s. GpgME Error: %s\n", xmpp_jid_me, gpgme_strerror(error));
193    return NULL;
194  }
195
196  error = gpgme_signers_add(ctx,recp[0]);
197  if(error != 0) {
198    logError(xmppc,"gpgme_signers_add %s. GpgME Error: %s\n", xmpp_jid_me, gpgme_strerror(error));
199    return NULL;
200  }
201
202
203  // lookup key of recipient
204  error = _openpgp_lookup_key(xmppc,xmpp_jid_recipient, &ctx, &recp[1]);
205  if(error != 0) {
206    logError(xmppc,"Key not found for %s. GpgME Error: %s\n", xmpp_jid_recipient, gpgme_strerror(error));
207    return NULL;
208  }
209  recp[2] = NULL;
210  logInfo(xmppc, "%s <%s>\n", recp[0]->uids->name, recp[0]->uids->email);
211  logInfo(xmppc, "%s <%s>\n", recp[1]->uids->name, recp[1]->uids->email);
212
213#ifdef XMPPC_DEVELOPMENT
214  gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST; 
215#else 
216  gpgme_encrypt_flags_t flags = 0;
217#endif
218
219  gpgme_data_t plain;
220  gpgme_data_t cipher;
221
222  error = gpgme_data_new (&plain);
223  if(error != 0) {
224    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
225    return NULL;
226  }
227
228  error = gpgme_data_new_from_mem(&plain, message, strlen(message),0);
229  if(error != 0) {
230    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
231    return NULL;
232  }
233  error = gpgme_data_new (&cipher);  
234  if(error != 0) {
235    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
236    return NULL;
237  }
238
239  error = gpgme_op_encrypt_sign ( ctx, recp, flags, plain, cipher);
240  if(error != 0) {
241    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
242    return NULL;
243  }
244
245  size_t len;
246  char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
247  char* result = g_base64_encode( (unsigned char*) cipher_str,len);
248  gpgme_key_release (recp[0]);
249  gpgme_key_release (recp[1]);
250  gpgme_release (ctx);
251  return result;
252}
253
254gpgme_error_t _openpgp_lookup_key(xmppc_t *xmppc,char* name, gpgme_ctx_t* ctx, gpgme_key_t* key) {
255  logDebug(xmppc, "Looking for key: %s ...\n", name);
256  gpgme_error_t error = gpgme_op_keylist_start (*ctx, NULL, 0);
257  while (!error) {
258    error = gpgme_op_keylist_next (*ctx, key);
259    if(!error) {
260      gpgme_user_id_t uids = (*key)->uids;
261      while (uids) {
262        if(strcmp(uids->name, name) == 0) {
263         logDebug(xmppc, "Key found: %s ...\n", uids->name);
264         return error; 
265        } 
266        uids=uids->next;
267      }
268    } else {
269      gpgme_key_release((*key));
270    }
271  }
272  return error;
273}
274