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    // BASE64_OPENPGP_MESSAGE
 85    xmpp_stanza_t* base64_openpgp_message = xmpp_stanza_new(xmppc->ctx);
 86    xmpp_stanza_set_text(base64_openpgp_message,signcrypt_e);
 87    xmpp_stanza_add_child(openpgp, base64_openpgp_message);
 88    xmpp_stanza_add_child(message, openpgp);
 89    
 90    xmpp_stanza_to_text(message, &c,&s);
 91    
 92    xmpp_send(conn, message); 
 93   
 94}
 95
 96xmpp_stanza_t* _openpgp_signcrypt(xmppc_t *xmppc, char* to, char* text) {
 97
 98  time_t now = time(NULL);
 99  struct tm* tm = localtime(&now);
100  char buf[255];
101  strftime(buf, sizeof(buf), "%FT%T%z", tm);
102  printf("%s\n",buf);
103
104    int randnr = rand() % 5;
105    char rpad_data[randnr];
106    for(int i = 0; i < randnr-1; i++) {
107      rpad_data[i] = 'c';
108    }
109    rpad_data[randnr-1] = '\0';
110    
111
112    xmpp_stanza_t *signcrypt = xmpp_stanza_new(xmppc->ctx);
113    xmpp_stanza_set_name(signcrypt, "signcrypt");
114    xmpp_stanza_set_ns(signcrypt, "urn:xmpp:openpgp:0");
115    xmpp_stanza_t *s_to = xmpp_stanza_new(xmppc->ctx);
116    xmpp_stanza_set_name(s_to, "to");
117    xmpp_stanza_set_attribute(s_to, "jid", to);
118    xmpp_stanza_t *time = xmpp_stanza_new(xmppc->ctx);
119    xmpp_stanza_set_name(time, "time");
120    xmpp_stanza_set_attribute(time, "stamp", buf);
121    xmpp_stanza_set_name(time, "time");
122    xmpp_stanza_t *rpad = xmpp_stanza_new(xmppc->ctx);
123    xmpp_stanza_set_name(rpad, "rpad");
124    xmpp_stanza_t *rpad_text = xmpp_stanza_new(xmppc->ctx);
125    xmpp_stanza_set_text(rpad_text, rpad_data);
126    xmpp_stanza_t *payload= xmpp_stanza_new(xmppc->ctx);
127    xmpp_stanza_set_name(payload, "payload");
128    xmpp_stanza_t *body = xmpp_stanza_new(xmppc->ctx);
129    xmpp_stanza_set_name(body, "body");
130    xmpp_stanza_set_ns(body, "jabber:client");
131    xmpp_stanza_t *body_text = xmpp_stanza_new(xmppc->ctx);
132    xmpp_stanza_set_text(body_text, text);
133    xmpp_stanza_add_child(signcrypt,s_to);
134    xmpp_stanza_add_child(signcrypt,time);
135    xmpp_stanza_add_child(signcrypt,rpad);
136    xmpp_stanza_add_child(rpad,rpad_text);
137    xmpp_stanza_add_child(signcrypt,payload);
138    xmpp_stanza_add_child(payload, body);
139    xmpp_stanza_add_child(body, body_text);
140    return signcrypt;
141
142}
143
144char* _openpgp_gpg_signcrypt(xmppc_t *xmppc, char* recipient, char* message) {
145  setlocale (LC_ALL, "");
146  gpgme_check_version (NULL);
147  gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL));
148  gpgme_ctx_t ctx;
149  gpgme_error_t error = gpgme_new (&ctx);
150  if(GPG_ERR_NO_ERROR != error ) {
151    printf("gpgme_new: %d\n", error);
152    return NULL;
153  }
154  error = gpgme_set_protocol(ctx, GPGME_PROTOCOL_OPENPGP);
155  if(error != 0) {
156    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
157  }
158  gpgme_set_armor(ctx,0);
159  gpgme_set_textmode(ctx,0);
160  gpgme_set_offline(ctx,1);
161  gpgme_set_keylist_mode(ctx, GPGME_KEYLIST_MODE_LOCAL);
162  if(error != 0) {
163    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
164  }
165
166  gpgme_key_t recp[3];
167  recp[0] = NULL,
168  recp[1] = NULL;
169  const char *jid = xmpp_conn_get_jid(xmppc->conn);
170  
171  char* xmpp_jid_me = alloca( (strlen(jid)+6) * sizeof(char) );
172  char* xmpp_jid_recipient =  alloca( (strlen(recipient)+6) * sizeof(char) );
173
174  strcpy(xmpp_jid_me, "xmpp:");
175  strcpy(xmpp_jid_recipient, "xmpp:");
176  strcat(xmpp_jid_me, jid);
177  strcat(xmpp_jid_recipient,recipient);
178
179  error = _openpgp_lookup_key(xmppc,xmpp_jid_me, &ctx, &recp[0]);
180  if(error != 0) {
181    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
182  }
183
184  error = _openpgp_lookup_key(xmppc,xmpp_jid_recipient, &ctx, &recp[1]);
185  if(error != 0) {
186    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
187  }
188  recp[2] = NULL;
189  logInfo(xmppc, "%s <%s>\n", recp[0]->uids->name, recp[0]->uids->email);
190  logInfo(xmppc, "%s <%s>\n", recp[1]->uids->name, recp[1]->uids->email);
191
192#ifdef XMPPC_DEVELOPMENT
193  gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST; 
194#else 
195  gpgme_encrypt_flags_t flags = 0;
196#endif
197
198  gpgme_data_t plain;
199  gpgme_data_t cipher;
200
201  error = gpgme_data_new (&plain);
202  if(error != 0) {
203    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
204  }
205
206  error = gpgme_data_new_from_mem(&plain, message, strlen(message),0);
207  if(error != 0) {
208    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
209  }
210  error = gpgme_data_new (&cipher);  
211  if(error != 0) {
212    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
213  }
214
215  error = gpgme_op_encrypt_sign ( ctx, recp, flags, plain, cipher);
216  if(error != 0) {
217    logError(xmppc,"GpgME Error: %s\n", gpgme_strerror(error));  
218  }
219
220  size_t len;
221  char *cipher_str = gpgme_data_release_and_get_mem(cipher, &len);
222  char* result = g_base64_encode( (unsigned char*) cipher_str,len);
223  gpgme_key_release (recp[0]);
224  gpgme_key_release (recp[1]);
225  gpgme_release (ctx);
226  return result;
227}
228
229gpgme_error_t _openpgp_lookup_key(xmppc_t *xmppc,char* name, gpgme_ctx_t* ctx, gpgme_key_t* key) {
230  logDebug(xmppc, "Looking for key: %s ...\n", name);
231  gpgme_error_t error = gpgme_op_keylist_start (*ctx, NULL, 0);
232  while (!error) {
233    error = gpgme_op_keylist_next (*ctx, key);
234    if(strcmp((*key)->uids->name, name) == 0) {
235      logDebug(xmppc, "Key found: %s ...\n", (*key)->uids->name);
236      return error;
237    }
238    else {
239      gpgme_key_release((*key));
240    }
241  }
242  return error;
243}
244