mcabber/mcabber/pgp.c
author Holger Weiß <holger@zedat.fu-berlin.de>
Sat, 21 Nov 2015 17:44:55 +0100
changeset 2233 90c25a29fb35
parent 2212 778280b01bcb
child 2234 232c26383fc4
permissions -rw-r--r--
New option: 'gpg_path' The path to the gpg binary can now be specified in order to force the use of GnuPG version 1 on systems which have both versions installed.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 * pgp.c        -- PGP utility functions
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
     4
 * Copyright (C) 2006-2015 Mikael Berthe <mikael@lilotux.net>
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * Some parts inspired by centericq (impgp.cc)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 *
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * This program is free software; you can redistribute it and/or modify
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 * it under the terms of the GNU General Public License as published by
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * the Free Software Foundation; either version 2 of the License, or (at
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * your option) any later version.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 *
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * General Public License for more details.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 *
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 * along with this program; if not, write to the Free Software
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
 * USA
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
 */
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
#include <config.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
#ifdef HAVE_GPGME
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
#include <stdlib.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
#include <string.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
#include <unistd.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
#include <locale.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
#include <sys/mman.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
#include <glib.h>
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
#include "pgp.h"
2233
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    35
#include "settings.h"
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
#include "logprint.h"
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
2233
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    38
#define MIN_GPGME_VERSION "1.1.0"
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
static struct gpg_struct
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
{
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    42
  int   enabled;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    43
  int   version1;
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
  char *private_key;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
  char *passphrase;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
} gpg;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
//  gpg_init(priv_key, passphrase)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
// Initialize the GPG sub-systems.  This function must be invoked early.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
// Note: priv_key & passphrase are optional, they can be set later.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
// This function returns 0 if gpgme is available and initialized;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
// if not it returns the gpgme error code.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
int gpg_init(const char *priv_key, const char *passphrase)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
  gpgme_error_t err;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    58
  gpgme_ctx_t ctx;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    59
  gpgme_engine_info_t info;
2233
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    60
  const char *gpg_path;
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    61
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
  // Check for version and OpenPGP protocol support.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
  if (!gpgme_check_version(MIN_GPGME_VERSION)) {
1057
4cdf19d9c74e PGP: Fix a small bug in initialization
Mikael Berthe <mikael@lilotux.net>
parents: 1054
diff changeset
    64
    scr_LogPrint(LPRINT_LOGNORM,
4cdf19d9c74e PGP: Fix a small bug in initialization
Mikael Berthe <mikael@lilotux.net>
parents: 1054
diff changeset
    65
                 "GPGME initialization error: Bad library version");
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
    return -1;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
  err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
  if (err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
                 "GPGME initialization error: %s", gpgme_strerror(err));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
    return err;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
  // Set the locale information.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
  gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
  gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
2233
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    80
  // The path to the gpg binary can be specified in order to force
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    81
  // version 1, for example.
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    82
  gpg_path = settings_opt_get("gpg_path");
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    83
  if (gpg_path) {
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    84
    err = gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, gpg_path, NULL);
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    85
    if (err) return -1;
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    86
  }
90c25a29fb35 New option: 'gpg_path'
Holger Weiß <holger@zedat.fu-berlin.de>
parents: 2212
diff changeset
    87
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
  // Store private data.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
  gpg_set_private_key(priv_key);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
  gpg_set_passphrase(passphrase);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    92
  err = gpgme_new(&ctx);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    93
  if (err) return -1;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    94
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    95
  // Check OpenPGP engine version; with version 2+ the agent is mandatory
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    96
  // and we do not manage the passphrase.
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    97
  gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    98
  if (err) return -1;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
    99
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   100
  err = gpgme_get_engine_info (&info);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   101
  if (!err) {
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   102
    while (info && info->protocol != gpgme_get_protocol (ctx))
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   103
      info = info->next;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   104
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   105
    if (info && info->version) {
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   106
      if (!strncmp(info->version, "1.", 2))
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   107
        gpg.version1 = TRUE;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   108
      scr_log_print(LPRINT_DEBUG, "GPGME: Engine version is '%s'.",
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   109
                    info->version);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   110
    }
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   111
  }
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   112
2212
778280b01bcb Fix a few memory leaks
Mikael Berthe <mikael@lilotux.net>
parents: 2208
diff changeset
   113
  gpgme_release(ctx);
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
  gpg.enabled = 1;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
  return 0;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   118
//  gpg_is_version1()
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   119
// Return TRUE if the GnuPG OpenPGP engine version is 1.x
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   120
int gpg_is_version1(void)
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   121
{
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   122
  return gpg.version1;
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   123
}
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   124
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
//  gpg_terminate()
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
// Destroy data and free memory.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   127
void gpg_terminate(void)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   128
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   129
  gpg.enabled = 0;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   130
  gpg_set_passphrase(NULL);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   131
  gpg_set_private_key(NULL);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   132
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   133
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   134
//  gpg_set_passphrase(passphrase)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   135
// Set the current passphrase (use NULL to erase it).
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   136
void gpg_set_passphrase(const char *passphrase)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   137
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   138
  // Remove current passphrase
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   139
  if (gpg.passphrase) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   140
    ssize_t len = strlen(gpg.passphrase);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   141
    memset(gpg.passphrase, 0, len);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   142
    munlock(gpg.passphrase, len);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   143
    g_free(gpg.passphrase);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   144
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   145
  if (passphrase) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   146
    gpg.passphrase = g_strdup(passphrase);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   147
    mlock(gpg.passphrase, strlen(gpg.passphrase));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   148
  } else {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   149
    gpg.passphrase = NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   150
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   151
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   152
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   153
//  gpg_set_private_key(keyid)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   154
// Set the current private key id (use NULL to unset it).
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   155
void gpg_set_private_key(const char *priv_keyid)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   156
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   157
  g_free(gpg.private_key);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   158
  if (priv_keyid)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   159
    gpg.private_key = g_strdup(priv_keyid);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   160
  else
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   161
    gpg.private_key = NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   162
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   163
2188
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   164
//  gpg_get_private_key_id()
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   165
// Return the current private key id (static string).
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   166
const char *gpg_get_private_key_id(void)
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   167
{
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   168
  return gpg.private_key;
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   169
}
84252c616919 PGP: Encrypt messages with our own PGP key when carbons are enabled
Mikael Berthe <mikael@lilotux.net>
parents: 2187
diff changeset
   170
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   171
//  strip_header_footer(data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   172
// Remove PGP header & footer from data.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   173
// Return a new string, or NULL.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   174
// The string must be freed by the caller with g_free() when no longer needed.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   175
static char *strip_header_footer(const char *data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   176
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   177
  char *p, *q;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   178
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   179
  if (!data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   180
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   181
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   182
  // p: beginning of real data
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   183
  // q: end of real data
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   184
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   185
  // Strip header (to the first empty line)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   186
  p = strstr(data, "\n\n");
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   187
  if (!p)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   188
    return g_strdup(data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   189
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   190
  // Strip footer
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   191
  // We want to remove the last lines, until the line beginning with a '-'
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   192
  p += 2;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   193
  for (q = p ; *q; q++) ;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   194
  // (q is at the end of data now)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   195
  for (q--; q > p && (*q != '\n' || *(q+1) != '-'); q--) ;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   196
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   197
  if (q <= p)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   198
    return NULL; // Shouldn't happen...
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   199
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   200
  return g_strndup(p, q-p);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   201
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   202
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1414
diff changeset
   203
// GCC ignores casts to void, thus we need to hack around that
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1414
diff changeset
   204
static inline void ignore(void*x) {}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1414
diff changeset
   205
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   206
//  passphrase_cb()
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   207
// GPGME passphrase callback function.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   208
static gpgme_error_t passphrase_cb(void *hook, const char *uid_hint,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   209
                       const char *passphrase_info, int prev_was_bad, int fd)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   210
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   211
  ssize_t len;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   212
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   213
  // Abort if we do not have the password.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   214
  if (!gpg.passphrase) {
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1414
diff changeset
   215
    ignore((void*)write(fd, "\n", 1)); // We have an error anyway, thus it does
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1414
diff changeset
   216
                                       // not matter if we fail again.
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   217
    return gpg_error(GPG_ERR_CANCELED);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   218
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   219
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   220
  // Write the passphrase to the file descriptor.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   221
  len = strlen(gpg.passphrase);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   222
  if (write(fd, gpg.passphrase, len) != len)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   223
    return gpg_error(GPG_ERR_CANCELED);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   224
  if (write(fd, "\n", 1) != 1)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   225
    return gpg_error(GPG_ERR_CANCELED);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   226
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   227
  return 0; // Success
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   228
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   229
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   230
//  gpg_verify(gpg_data, text, *sigsum)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   231
// Verify that gpg_data is a correct signature for text.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   232
// Return the key id (or fingerprint), and set *sigsum to
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   233
// the gpgme signature summary value.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   234
// The returned string must be freed with g_free() after use.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   235
char *gpg_verify(const char *gpg_data, const char *text,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   236
                 gpgme_sigsum_t *sigsum)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   237
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   238
  gpgme_ctx_t ctx;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   239
  gpgme_data_t data_sign, data_text;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   240
  char *data;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   241
  char *verified_key = NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   242
  gpgme_key_t key;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   243
  gpgme_error_t err;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   244
  const char prefix[] = "-----BEGIN PGP SIGNATURE-----\n\n";
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   245
  const char suffix[] = "\n-----END PGP SIGNATURE-----\n";
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   246
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   247
  // Reset the summary.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   248
  *sigsum = 0;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   249
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   250
  if (!gpg.enabled)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   251
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   252
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   253
  err = gpgme_new(&ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   254
  if (err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   255
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   256
                 "GPGME error: %s", gpgme_strerror(err));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   257
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   258
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   259
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   260
  gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   261
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   262
  // Surround the given data with the prefix & suffix
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   263
  data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   264
  strcpy(data, prefix);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   265
  strcat(data, gpg_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   266
  strcat(data, suffix);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   267
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   268
  err = gpgme_data_new_from_mem(&data_sign, data, strlen(data), 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   269
  if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   270
    err = gpgme_data_new_from_mem(&data_text, text, strlen(text), 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   271
    if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   272
      err = gpgme_op_verify(ctx, data_sign, data_text, 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   273
      if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   274
        gpgme_verify_result_t vr = gpgme_op_verify_result(ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   275
        if (vr && vr->signatures) {
2200
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   276
            gpgme_signature_t s = NULL;
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   277
            // check all signatures and stop if the first could be verified
2208
7c843b29571b Fix changeset fec172dbacc7
Mikael Berthe <mikael@lilotux.net>
parents: 2202
diff changeset
   278
            for (s = vr->signatures; s && !verified_key; s = s->next) {
2200
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   279
                // Found the fingerprint.  Let's try to get the key id.
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   280
                if (NULL != s->fpr) {
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   281
                    if (!gpgme_get_key(ctx, s->fpr, &key, 0)) {
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   282
                        if (key) {
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   283
                            verified_key = g_strdup(key->subkeys->keyid);
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   284
                            gpgme_key_release(key);
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   285
                        }
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   286
                    }
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   287
                }
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   288
                *sigsum = s->summary;
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   289
                // For some reason summary could be 0 when status is 0 too,
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   290
                // which means the signature is valid...
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   291
                if ((!*sigsum) && (!s->status))
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   292
                    *sigsum = GPGME_SIGSUM_GREEN;
fec172dbacc7 Check all signatures and stop immediately if one could be verified
Sven Gaerner <sgaerner@gmx.net>
parents: 2196
diff changeset
   293
            }
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   294
        }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   295
      }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   296
      gpgme_data_release(data_text);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   297
    }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   298
    gpgme_data_release(data_sign);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   299
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   300
  if (err)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   301
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
1047
0dc104d51dce PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents: 1041
diff changeset
   302
                 "GPGME verification error: %s", gpgme_strerror(err));
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   303
  gpgme_release(ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   304
  g_free(data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   305
  return verified_key;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   306
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   307
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   308
//  gpg_sign(gpg_data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   309
// Return a signature of gpg_data (or NULL).
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   310
// The returned string must be freed with g_free() after use.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   311
char *gpg_sign(const char *gpg_data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   312
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   313
  gpgme_ctx_t ctx;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   314
  gpgme_data_t in, out;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   315
  char *signed_data = NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   316
  size_t nread;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   317
  gpgme_key_t key;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   318
  gpgme_error_t err;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   319
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   320
  if (!gpg.enabled || !gpg.private_key)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   321
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   322
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   323
  err = gpgme_new(&ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   324
  if (err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   325
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   326
                 "GPGME error: %s", gpgme_strerror(err));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   327
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   328
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   329
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   330
  gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   331
  gpgme_set_textmode(ctx, 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   332
  gpgme_set_armor(ctx, 1);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   333
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   334
  if (gpg.version1) {
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   335
    // GPG_AGENT_INFO isn't used by GnuPG version 2+
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   336
    char *p = getenv("GPG_AGENT_INFO");
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   337
    if (!(p && strchr(p, ':')))
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   338
      gpgme_set_passphrase_cb(ctx, passphrase_cb, 0);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   339
  }
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   340
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   341
  err = gpgme_get_key(ctx, gpg.private_key, &key, 1);
1105
265e1d873d3e PGP: Improve checks for missing keys
Mikael Berthe <mikael@lilotux.net>
parents: 1057
diff changeset
   342
  if (err || !key) {
1049
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   343
    scr_LogPrint(LPRINT_LOGNORM, "GPGME error: private key not found");
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   344
    gpgme_release(ctx);
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   345
    return NULL;
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   346
  }
1049
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   347
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   348
  gpgme_signers_clear(ctx);
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   349
  gpgme_signers_add(ctx, key);
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   350
  gpgme_key_release(key);
ad5de4d1ee56 PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents: 1047
diff changeset
   351
  err = gpgme_data_new_from_mem(&in, gpg_data, strlen(gpg_data), 0);
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   352
  if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   353
    err = gpgme_data_new(&out);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   354
    if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   355
      err = gpgme_op_sign(ctx, in, out, GPGME_SIG_MODE_DETACH);
2202
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   356
      if (err) {
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   357
        gpgme_data_release(out);
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   358
      } else {
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   359
        signed_data = gpgme_data_release_and_get_mem(out, &nread);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   360
        if (signed_data) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   361
          // We need to add a trailing NULL
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   362
          char *dd = g_strndup(signed_data, nread);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   363
          free(signed_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   364
          signed_data = strip_header_footer(dd);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   365
          g_free(dd);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   366
        }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   367
      }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   368
    }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   369
    gpgme_data_release(in);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   370
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   371
  if (err && err != GPG_ERR_CANCELED)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   372
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
1047
0dc104d51dce PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents: 1041
diff changeset
   373
                 "GPGME signature error: %s", gpgme_strerror(err));
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   374
  gpgme_release(ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   375
  return signed_data;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   376
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   377
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   378
//  gpg_decrypt(gpg_data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   379
// Return decrypted gpg_data (or NULL).
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   380
// The returned string must be freed with g_free() after use.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   381
char *gpg_decrypt(const char *gpg_data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   382
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   383
  gpgme_ctx_t ctx;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   384
  gpgme_data_t in, out;
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   385
  char *data;
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   386
  char *decrypted_data = NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   387
  size_t nread;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   388
  gpgme_error_t err;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   389
  const char prefix[] = "-----BEGIN PGP MESSAGE-----\n\n";
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   390
  const char suffix[] = "\n-----END PGP MESSAGE-----\n";
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   391
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   392
  if (!gpg.enabled)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   393
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   394
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   395
  err = gpgme_new(&ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   396
  if (err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   397
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   398
                 "GPGME error: %s", gpgme_strerror(err));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   399
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   400
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   401
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   402
  gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   403
2196
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   404
  if (gpg.version1) {
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   405
    // GPG_AGENT_INFO isn't used by GnuPG version 2+
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   406
    char *p = getenv("GPG_AGENT_INFO");
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   407
    if (!(p && strchr(p, ':')))
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   408
      gpgme_set_passphrase_cb(ctx, passphrase_cb, 0);
8811fe9d6ef0 Improve support for GnuPG v2+
Mikael Berthe <mikael@lilotux.net>
parents: 2195
diff changeset
   409
  }
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   410
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   411
  // Surround the given data with the prefix & suffix
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   412
  data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   413
  strcpy(data, prefix);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   414
  strcat(data, gpg_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   415
  strcat(data, suffix);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   416
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   417
  err = gpgme_data_new_from_mem(&in, data, strlen(data), 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   418
  if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   419
    err = gpgme_data_new(&out);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   420
    if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   421
      err = gpgme_op_decrypt(ctx, in, out);
2202
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   422
      if (err) {
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   423
        gpgme_data_release(out);
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   424
      } else {
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   425
        decrypted_data = gpgme_data_release_and_get_mem(out, &nread);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   426
        if (decrypted_data) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   427
          // We need to add a trailing NULL
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   428
          char *dd = g_strndup(decrypted_data, nread);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   429
          free(decrypted_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   430
          decrypted_data = dd;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   431
        }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   432
      }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   433
    }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   434
    gpgme_data_release(in);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   435
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   436
  if (err && err != GPG_ERR_CANCELED)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   437
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
1047
0dc104d51dce PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents: 1041
diff changeset
   438
                 "GPGME decryption error: %s", gpgme_strerror(err));
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   439
  gpgme_release(ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   440
  g_free(data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   441
  return decrypted_data;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   442
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   443
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   444
//  gpg_encrypt(gpg_data, keyids[], n)
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   445
// Return encrypted gpg_data with the n keys from the keyids array (or NULL).
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   446
// The returned string must be freed with g_free() after use.
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   447
char *gpg_encrypt(const char *gpg_data, const char *keyids[], size_t nkeys)
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   448
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   449
  gpgme_ctx_t ctx;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   450
  gpgme_data_t in, out;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   451
  char *encrypted_data = NULL, *edata;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   452
  size_t nread;
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   453
  gpgme_key_t *keys;
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   454
  gpgme_error_t err;
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   455
  unsigned i;
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   456
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   457
  if (!gpg.enabled)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   458
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   459
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   460
  if (!keyids || !nkeys) {
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   461
    return NULL;
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   462
  }
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   463
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   464
  err = gpgme_new(&ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   465
  if (err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   466
    scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   467
                 "GPGME error: %s", gpgme_strerror(err));
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   468
    return NULL;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   469
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   470
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   471
  gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   472
  gpgme_set_textmode(ctx, 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   473
  gpgme_set_armor(ctx, 1);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   474
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   475
  keys = g_new0(gpgme_key_t, 1+nkeys);
2195
40ddaebeb81e PGP: Add a memory allocation check and compilation directives
Mikael Berthe <mikael@lilotux.net>
parents: 2188
diff changeset
   476
  if (!keys) {
40ddaebeb81e PGP: Add a memory allocation check and compilation directives
Mikael Berthe <mikael@lilotux.net>
parents: 2188
diff changeset
   477
    gpgme_release(ctx);
40ddaebeb81e PGP: Add a memory allocation check and compilation directives
Mikael Berthe <mikael@lilotux.net>
parents: 2188
diff changeset
   478
    return NULL;
40ddaebeb81e PGP: Add a memory allocation check and compilation directives
Mikael Berthe <mikael@lilotux.net>
parents: 2188
diff changeset
   479
  }
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   480
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   481
  for (i = 0; i < nkeys; i++) {
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   482
    err = gpgme_get_key(ctx, keyids[i], &keys[i], 0);
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   483
    if (err || !keys[i]) {
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   484
      scr_LogPrint(LPRINT_LOGNORM, "GPGME encryption error: cannot use key %s",
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   485
                   keyids[i]);
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   486
      // We need to have err not null to ensure we won't try to encrypt
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   487
      // without this key.
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   488
      if (!err) err = GPG_ERR_UNKNOWN_ERRNO;
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   489
      break;
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   490
    }
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   491
  }
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   492
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   493
  if (!err) {
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   494
    err = gpgme_data_new_from_mem(&in, gpg_data, strlen(gpg_data), 0);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   495
    if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   496
      err = gpgme_data_new(&out);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   497
      if (!err) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   498
        err = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
2202
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   499
        if (err) {
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   500
          gpgme_data_release(out);
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   501
        } else {
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   502
          encrypted_data = gpgme_data_release_and_get_mem(out, &nread);
2202
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   503
          if (encrypted_data) {
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   504
            // We need to add a trailing NULL
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   505
            char *dd = g_strndup(encrypted_data, nread);
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   506
            free(encrypted_data);
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   507
            encrypted_data = dd;
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   508
          }
ff18feb75a6d PGP: Add a trailing NULL after a message is PGP-encrypted
Mikael Berthe <mikael@lilotux.net>
parents: 2200
diff changeset
   509
        }
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   510
      }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   511
      gpgme_data_release(in);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   512
    }
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   513
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   514
    if (err && err != GPG_ERR_CANCELED) {
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   515
      scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   516
                   "GPGME encryption error: %s", gpgme_strerror(err));
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   517
    }
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   518
  }
2187
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   519
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   520
  for (i = 0; keys[i]; i++)
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   521
    gpgme_key_release(keys[i]);
e3b66c8ead4f PGP: Change gpg_encrypt() so that several encryption keys can be used
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   522
  g_free(keys);
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   523
  gpgme_release(ctx);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   524
  edata = strip_header_footer(encrypted_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   525
  if (encrypted_data)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   526
    free(encrypted_data);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   527
  return edata;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   528
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   529
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   530
//  gpg_test_passphrase()
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   531
// Test the current gpg.passphrase with gpg.private_key.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   532
// If the test doesn't succeed, the passphrase is cleared and a non-null
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   533
// value is returned.
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   534
int gpg_test_passphrase(void)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   535
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   536
  char *s;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   537
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   538
  if (!gpg.private_key)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   539
    return -1; // No private key...
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   540
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   541
  s = gpg_sign("test");
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   542
  if (s) {
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   543
    free(s);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   544
    return 0; // Ok, test successful
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   545
  }
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   546
  // The passphrase is wrong (if provided)
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   547
  gpg_set_passphrase(NULL);
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   548
  return -1;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   549
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   550
1413
f89844a0448a Remove useless inlines
Mikael Berthe <mikael@lilotux.net>
parents: 1238
diff changeset
   551
int gpg_enabled(void)
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   552
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   553
  return gpg.enabled;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   554
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   555
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   556
#else  /* not HAVE_GPGME */
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   557
1413
f89844a0448a Remove useless inlines
Mikael Berthe <mikael@lilotux.net>
parents: 1238
diff changeset
   558
int gpg_enabled(void)
1041
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   559
{
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   560
  return 0;
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   561
}
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   562
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   563
#endif /* HAVE_GPGME */
d4b97a2423eb Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   564
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1668
diff changeset
   565
/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */