Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/pgp.c @ 1197:6f602d3270a4
Add /pgp [-]force
With this command it becomes possible to enforce PGP encryption without
checking if the remote client has PGP support. It can be used to send
encrypted offline messages too.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Fri, 27 Apr 2007 00:37:57 +0200 |
parents | 265e1d873d3e |
children | 2de8f8ba1f34 |
rev | line source |
---|---|
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 * |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net> |
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" |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 #include "logprint.h" |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 #define MIN_GPGME_VERSION "1.0.0" |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 static struct gpg_struct |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 int enabled; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 char *private_key; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 char *passphrase; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 } gpg; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 // gpg_init(priv_key, passphrase) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 // Initialize the GPG sub-systems. This function must be invoked early. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 // Note: priv_key & passphrase are optional, they can be set later. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 // This function returns 0 if gpgme is available and initialized; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 // if not it returns the gpgme error code. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 int gpg_init(const char *priv_key, const char *passphrase) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 gpgme_error_t err; |
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 // Check for version and OpenPGP protocol support. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 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
|
58 scr_LogPrint(LPRINT_LOGNORM, |
4cdf19d9c74e
PGP: Fix a small bug in initialization
Mikael Berthe <mikael@lilotux.net>
parents:
1054
diff
changeset
|
59 "GPGME initialization error: Bad library version"); |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 return -1; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 if (err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 "GPGME initialization error: %s", gpgme_strerror(err)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 return err; |
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 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 // Set the locale information. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 // Store private data. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 gpg_set_private_key(priv_key); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 gpg_set_passphrase(passphrase); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 gpg.enabled = 1; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 return 0; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 // gpg_terminate() |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 // Destroy data and free memory. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 void gpg_terminate(void) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 gpg.enabled = 0; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 gpg_set_passphrase(NULL); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 gpg_set_private_key(NULL); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 // gpg_set_passphrase(passphrase) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 // Set the current passphrase (use NULL to erase it). |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 void gpg_set_passphrase(const char *passphrase) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 // Remove current passphrase |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 if (gpg.passphrase) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 ssize_t len = strlen(gpg.passphrase); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 memset(gpg.passphrase, 0, len); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 munlock(gpg.passphrase, len); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 g_free(gpg.passphrase); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 if (passphrase) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 gpg.passphrase = g_strdup(passphrase); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 mlock(gpg.passphrase, strlen(gpg.passphrase)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 } else { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 gpg.passphrase = NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 // gpg_set_private_key(keyid) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 // Set the current private key id (use NULL to unset it). |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 void gpg_set_private_key(const char *priv_keyid) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 g_free(gpg.private_key); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 if (priv_keyid) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 gpg.private_key = g_strdup(priv_keyid); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 else |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 gpg.private_key = NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 // strip_header_footer(data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 // Remove PGP header & footer from data. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 // Return a new string, or NULL. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 // 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
|
125 static char *strip_header_footer(const char *data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 char *p, *q; |
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 if (!data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 // p: beginning of real data |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 // q: end of real data |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
134 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 // Strip header (to the first empty line) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 p = strstr(data, "\n\n"); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 if (!p) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 return g_strdup(data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 // Strip footer |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 // 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
|
142 p += 2; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 for (q = p ; *q; q++) ; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 // (q is at the end of data now) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 for (q--; q > p && (*q != '\n' || *(q+1) != '-'); q--) ; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 if (q <= p) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
148 return NULL; // Shouldn't happen... |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
149 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 return g_strndup(p, q-p); |
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 // passphrase_cb() |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
154 // GPGME passphrase callback function. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 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
|
156 const char *passphrase_info, int prev_was_bad, int fd) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
158 ssize_t len; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
159 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
160 // Abort if we do not have the password. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 if (!gpg.passphrase) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 write(fd, "\n", 1); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 return gpg_error(GPG_ERR_CANCELED); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
166 // Write the passphrase to the file descriptor. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
167 len = strlen(gpg.passphrase); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
168 if (write(fd, gpg.passphrase, len) != len) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
169 return gpg_error(GPG_ERR_CANCELED); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
170 if (write(fd, "\n", 1) != 1) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
171 return gpg_error(GPG_ERR_CANCELED); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
172 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
173 return 0; // Success |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
174 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
175 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
176 // gpg_verify(gpg_data, text, *sigsum) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
177 // Verify that gpg_data is a correct signature for text. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
178 // Return the key id (or fingerprint), and set *sigsum to |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
179 // the gpgme signature summary value. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
180 // The returned string must be freed with g_free() after use. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
181 char *gpg_verify(const char *gpg_data, const char *text, |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
182 gpgme_sigsum_t *sigsum) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
183 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
184 gpgme_ctx_t ctx; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
185 gpgme_data_t data_sign, data_text; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
186 char *data; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
187 char *verified_key = NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
188 gpgme_key_t key; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
189 gpgme_error_t err; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
190 const char prefix[] = "-----BEGIN PGP SIGNATURE-----\n\n"; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
191 const char suffix[] = "\n-----END PGP SIGNATURE-----\n"; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
192 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
193 // Reset the summary. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
194 *sigsum = 0; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
195 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
196 if (!gpg.enabled) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
197 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
198 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
199 err = gpgme_new(&ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
200 if (err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
201 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
202 "GPGME error: %s", gpgme_strerror(err)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
203 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
204 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
205 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
206 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
207 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
208 // Surround the given data with the prefix & suffix |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
209 data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
210 strcpy(data, prefix); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
211 strcat(data, gpg_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
212 strcat(data, suffix); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
213 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
214 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
|
215 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
216 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
|
217 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
218 err = gpgme_op_verify(ctx, data_sign, data_text, 0); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
219 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
220 gpgme_verify_result_t vr = gpgme_op_verify_result(ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
221 if (vr && vr->signatures) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
222 char *r = vr->signatures->fpr; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
223 // Found the fingerprint. Let's try to get the key id. |
1105
265e1d873d3e
PGP: Improve checks for missing keys
Mikael Berthe <mikael@lilotux.net>
parents:
1057
diff
changeset
|
224 if (!gpgme_get_key(ctx, r, &key, 0) && key) { |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
225 r = key->subkeys->keyid; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
226 gpgme_key_release(key); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
227 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
228 // r is a static variable, let's copy it. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
229 verified_key = g_strdup(r); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
230 *sigsum = vr->signatures->summary; |
1054
082d55152a25
PGP: Improve pgp_verify()
Mikael Berthe <mikael@lilotux.net>
parents:
1049
diff
changeset
|
231 // For some reason summary could be 0 when status is 0 too, |
082d55152a25
PGP: Improve pgp_verify()
Mikael Berthe <mikael@lilotux.net>
parents:
1049
diff
changeset
|
232 // which means the signature is valid... |
082d55152a25
PGP: Improve pgp_verify()
Mikael Berthe <mikael@lilotux.net>
parents:
1049
diff
changeset
|
233 if (!*sigsum && !vr->signatures->status) |
082d55152a25
PGP: Improve pgp_verify()
Mikael Berthe <mikael@lilotux.net>
parents:
1049
diff
changeset
|
234 *sigsum = GPGME_SIGSUM_GREEN; |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
235 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
236 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
237 gpgme_data_release(data_text); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
238 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
239 gpgme_data_release(data_sign); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
240 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
241 if (err) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
242 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
1047
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
243 "GPGME verification error: %s", gpgme_strerror(err)); |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
244 gpgme_release(ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
245 g_free(data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
246 return verified_key; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
247 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
248 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
249 // gpg_sign(gpg_data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
250 // Return a signature of gpg_data (or NULL). |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
251 // The returned string must be freed with g_free() after use. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
252 char *gpg_sign(const char *gpg_data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
253 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
254 gpgme_ctx_t ctx; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
255 gpgme_data_t in, out; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
256 char *p; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
257 char *signed_data = NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
258 size_t nread; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
259 gpgme_key_t key; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
260 gpgme_error_t err; |
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 if (!gpg.enabled || !gpg.private_key) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
263 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
264 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
265 err = gpgme_new(&ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
266 if (err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
267 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
268 "GPGME error: %s", gpgme_strerror(err)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
269 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
270 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
271 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
272 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
273 gpgme_set_textmode(ctx, 0); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
274 gpgme_set_armor(ctx, 1); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
275 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
276 p = getenv("GPG_AGENT_INFO"); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
277 if (!(p && strchr(p, ':'))) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
278 gpgme_set_passphrase_cb(ctx, passphrase_cb, 0); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
279 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
280 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
|
281 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
|
282 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
|
283 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
|
284 return NULL; |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
285 } |
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
|
286 |
ad5de4d1ee56
PGP: Give a more useful error message when the private key isn't found
Mikael Berthe <mikael@lilotux.net>
parents:
1047
diff
changeset
|
287 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
|
288 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
|
289 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
|
290 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
|
291 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
292 err = gpgme_data_new(&out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
293 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
294 err = gpgme_op_sign(ctx, in, out, GPGME_SIG_MODE_DETACH); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
295 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
296 signed_data = gpgme_data_release_and_get_mem(out, &nread); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
297 if (signed_data) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
298 // We need to add a trailing NULL |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
299 char *dd = g_strndup(signed_data, nread); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
300 free(signed_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
301 signed_data = strip_header_footer(dd); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
302 g_free(dd); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
303 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
304 } else { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
305 gpgme_data_release(out); |
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 gpgme_data_release(in); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
309 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
310 if (err && err != GPG_ERR_CANCELED) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
311 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
1047
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
312 "GPGME signature error: %s", gpgme_strerror(err)); |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
313 gpgme_release(ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
314 return signed_data; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
315 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
316 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
317 // gpg_decrypt(gpg_data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
318 // Return decrypted gpg_data (or NULL). |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
319 // The returned string must be freed with g_free() after use. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
320 char *gpg_decrypt(const char *gpg_data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
321 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
322 gpgme_ctx_t ctx; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
323 gpgme_data_t in, out; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
324 char *p, *data; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
325 char *decrypted_data = NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
326 size_t nread; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
327 gpgme_error_t err; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
328 const char prefix[] = "-----BEGIN PGP MESSAGE-----\n\n"; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
329 const char suffix[] = "\n-----END PGP MESSAGE-----\n"; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
330 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
331 if (!gpg.enabled) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
332 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
333 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
334 err = gpgme_new(&ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
335 if (err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
336 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
337 "GPGME error: %s", gpgme_strerror(err)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
338 return NULL; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
339 } |
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 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
342 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
343 p = getenv("GPG_AGENT_INFO"); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
344 if (!(p && strchr(p, ':'))) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
345 gpgme_set_passphrase_cb(ctx, passphrase_cb, 0); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
346 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
347 // Surround the given data with the prefix & suffix |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
348 data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data)); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
349 strcpy(data, prefix); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
350 strcat(data, gpg_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
351 strcat(data, suffix); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
352 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
353 err = gpgme_data_new_from_mem(&in, data, strlen(data), 0); |
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_data_new(&out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
356 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
357 err = gpgme_op_decrypt(ctx, in, out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
358 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
359 decrypted_data = gpgme_data_release_and_get_mem(out, &nread); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
360 if (decrypted_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(decrypted_data, nread); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
363 free(decrypted_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
364 decrypted_data = dd; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
365 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
366 } else { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
367 gpgme_data_release(out); |
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 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
370 gpgme_data_release(in); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
371 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
372 if (err && err != GPG_ERR_CANCELED) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
373 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
1047
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
374 "GPGME decryption error: %s", gpgme_strerror(err)); |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
375 gpgme_release(ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
376 g_free(data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
377 return decrypted_data; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
378 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
379 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
380 // gpg_encrypt(gpg_data, keyid) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
381 // Return encrypted gpg_data with the key keyid (or NULL). |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
382 // The returned string must be freed with g_free() after use. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
383 char *gpg_encrypt(const char *gpg_data, const char *keyid) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
384 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
385 gpgme_ctx_t ctx; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
386 gpgme_data_t in, out; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
387 char *encrypted_data = NULL, *edata; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
388 size_t nread; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
389 gpgme_key_t key; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
390 gpgme_error_t err; |
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 gpgme_set_textmode(ctx, 0); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
404 gpgme_set_armor(ctx, 1); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
405 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
406 err = gpgme_get_key(ctx, keyid, &key, 0); |
1105
265e1d873d3e
PGP: Improve checks for missing keys
Mikael Berthe <mikael@lilotux.net>
parents:
1057
diff
changeset
|
407 if (!err && key) { |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
408 gpgme_key_t keys[] = { key, 0 }; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
409 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
|
410 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
411 err = gpgme_data_new(&out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
412 if (!err) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
413 err = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
414 if (!err) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
415 encrypted_data = gpgme_data_release_and_get_mem(out, &nread); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
416 else |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
417 gpgme_data_release(out); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
418 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
419 gpgme_data_release(in); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
420 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
421 gpgme_key_release(key); |
1047
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
422 } else { |
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
423 scr_LogPrint(LPRINT_LOGNORM, "GPGME encryption error: key not found"); |
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
424 err = 0; |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
425 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
426 if (err && err != GPG_ERR_CANCELED) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
427 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8, |
1047
0dc104d51dce
PGP: Better error messages
Mikael Berthe <mikael@lilotux.net>
parents:
1041
diff
changeset
|
428 "GPGME encryption error: %s", gpgme_strerror(err)); |
1041
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
429 gpgme_release(ctx); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
430 edata = strip_header_footer(encrypted_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
431 if (encrypted_data) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
432 free(encrypted_data); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
433 return edata; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
434 } |
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 // gpg_test_passphrase() |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
437 // Test the current gpg.passphrase with gpg.private_key. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
438 // 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
|
439 // value is returned. |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
440 int gpg_test_passphrase(void) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
441 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
442 char *s; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
443 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
444 if (!gpg.private_key) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
445 return -1; // No private key... |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
446 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
447 s = gpg_sign("test"); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
448 if (s) { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
449 free(s); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
450 return 0; // Ok, test successful |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
451 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
452 // The passphrase is wrong (if provided) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
453 gpg_set_passphrase(NULL); |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
454 return -1; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
455 } |
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 inline int gpg_enabled(void) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
458 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
459 return gpg.enabled; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
460 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
461 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
462 #else /* not HAVE_GPGME */ |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
463 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
464 inline int gpg_enabled(void) |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
465 { |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
466 return 0; |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
467 } |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
468 |
d4b97a2423eb
Introduce PGP utility functions
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
469 #endif /* HAVE_GPGME */ |
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 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |