comparison mcabber/src/pgp.c @ 1041:d4b97a2423eb

Introduce PGP utility functions
author Mikael Berthe <mikael@lilotux.net>
date Sun, 26 Nov 2006 10:30:52 +0100
parents
children 0dc104d51dce
comparison
equal deleted inserted replaced
1040:b6c6734a781b 1041:d4b97a2423eb
1 /*
2 * pgp.c -- PGP utility functions
3 *
4 * Copyright (C) 2006 Mikael Berthe <bmikael@lists.lilotux.net>
5 * Some parts inspired by centericq (impgp.cc)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or (at
10 * your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 */
22
23 #include <config.h>
24
25 #ifdef HAVE_GPGME
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <locale.h>
31 #include <sys/mman.h>
32 #include <glib.h>
33
34 #include "pgp.h"
35 #include "logprint.h"
36
37 #define MIN_GPGME_VERSION "1.0.0"
38
39 static struct gpg_struct
40 {
41 int enabled;
42 char *private_key;
43 char *passphrase;
44 } gpg;
45
46
47 // gpg_init(priv_key, passphrase)
48 // Initialize the GPG sub-systems. This function must be invoked early.
49 // Note: priv_key & passphrase are optional, they can be set later.
50 // This function returns 0 if gpgme is available and initialized;
51 // if not it returns the gpgme error code.
52 int gpg_init(const char *priv_key, const char *passphrase)
53 {
54 gpgme_error_t err;
55
56 // Check for version and OpenPGP protocol support.
57 if (!gpgme_check_version(MIN_GPGME_VERSION)) {
58 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
59 "GPGME initialization error: %s", gpgme_strerror(err));
60 return -1;
61 }
62
63 err = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
64 if (err) {
65 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
66 "GPGME initialization error: %s", gpgme_strerror(err));
67 return err;
68 }
69
70 // Set the locale information.
71 gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL));
72 gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL));
73
74 // Store private data.
75 gpg_set_private_key(priv_key);
76 gpg_set_passphrase(passphrase);
77
78 gpg.enabled = 1;
79 return 0;
80 }
81
82 // gpg_terminate()
83 // Destroy data and free memory.
84 void gpg_terminate(void)
85 {
86 gpg.enabled = 0;
87 gpg_set_passphrase(NULL);
88 gpg_set_private_key(NULL);
89 }
90
91 // gpg_set_passphrase(passphrase)
92 // Set the current passphrase (use NULL to erase it).
93 void gpg_set_passphrase(const char *passphrase)
94 {
95 // Remove current passphrase
96 if (gpg.passphrase) {
97 ssize_t len = strlen(gpg.passphrase);
98 memset(gpg.passphrase, 0, len);
99 munlock(gpg.passphrase, len);
100 g_free(gpg.passphrase);
101 }
102 if (passphrase) {
103 gpg.passphrase = g_strdup(passphrase);
104 mlock(gpg.passphrase, strlen(gpg.passphrase));
105 } else {
106 gpg.passphrase = NULL;
107 }
108 }
109
110 // gpg_set_private_key(keyid)
111 // Set the current private key id (use NULL to unset it).
112 void gpg_set_private_key(const char *priv_keyid)
113 {
114 g_free(gpg.private_key);
115 if (priv_keyid)
116 gpg.private_key = g_strdup(priv_keyid);
117 else
118 gpg.private_key = NULL;
119 }
120
121 // strip_header_footer(data)
122 // Remove PGP header & footer from data.
123 // Return a new string, or NULL.
124 // The string must be freed by the caller with g_free() when no longer needed.
125 static char *strip_header_footer(const char *data)
126 {
127 char *p, *q;
128
129 if (!data)
130 return NULL;
131
132 // p: beginning of real data
133 // q: end of real data
134
135 // Strip header (to the first empty line)
136 p = strstr(data, "\n\n");
137 if (!p)
138 return g_strdup(data);
139
140 // Strip footer
141 // We want to remove the last lines, until the line beginning with a '-'
142 p += 2;
143 for (q = p ; *q; q++) ;
144 // (q is at the end of data now)
145 for (q--; q > p && (*q != '\n' || *(q+1) != '-'); q--) ;
146
147 if (q <= p)
148 return NULL; // Shouldn't happen...
149
150 return g_strndup(p, q-p);
151 }
152
153 // passphrase_cb()
154 // GPGME passphrase callback function.
155 static gpgme_error_t passphrase_cb(void *hook, const char *uid_hint,
156 const char *passphrase_info, int prev_was_bad, int fd)
157 {
158 ssize_t len;
159
160 // Abort if we do not have the password.
161 if (!gpg.passphrase) {
162 write(fd, "\n", 1);
163 return gpg_error(GPG_ERR_CANCELED);
164 }
165
166 // Write the passphrase to the file descriptor.
167 len = strlen(gpg.passphrase);
168 if (write(fd, gpg.passphrase, len) != len)
169 return gpg_error(GPG_ERR_CANCELED);
170 if (write(fd, "\n", 1) != 1)
171 return gpg_error(GPG_ERR_CANCELED);
172
173 return 0; // Success
174 }
175
176 // gpg_verify(gpg_data, text, *sigsum)
177 // Verify that gpg_data is a correct signature for text.
178 // Return the key id (or fingerprint), and set *sigsum to
179 // the gpgme signature summary value.
180 // The returned string must be freed with g_free() after use.
181 char *gpg_verify(const char *gpg_data, const char *text,
182 gpgme_sigsum_t *sigsum)
183 {
184 gpgme_ctx_t ctx;
185 gpgme_data_t data_sign, data_text;
186 char *data;
187 char *verified_key = NULL;
188 gpgme_key_t key;
189 gpgme_error_t err;
190 const char prefix[] = "-----BEGIN PGP SIGNATURE-----\n\n";
191 const char suffix[] = "\n-----END PGP SIGNATURE-----\n";
192
193 // Reset the summary.
194 *sigsum = 0;
195
196 if (!gpg.enabled)
197 return NULL;
198
199 err = gpgme_new(&ctx);
200 if (err) {
201 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
202 "GPGME error: %s", gpgme_strerror(err));
203 return NULL;
204 }
205
206 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
207
208 // Surround the given data with the prefix & suffix
209 data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data));
210 strcpy(data, prefix);
211 strcat(data, gpg_data);
212 strcat(data, suffix);
213
214 err = gpgme_data_new_from_mem(&data_sign, data, strlen(data), 0);
215 if (!err) {
216 err = gpgme_data_new_from_mem(&data_text, text, strlen(text), 0);
217 if (!err) {
218 err = gpgme_op_verify(ctx, data_sign, data_text, 0);
219 if (!err) {
220 gpgme_verify_result_t vr = gpgme_op_verify_result(ctx);
221 if (vr && vr->signatures) {
222 char *r = vr->signatures->fpr;
223 // Found the fingerprint. Let's try to get the key id.
224 if(!gpgme_get_key(ctx, r, &key, 0)) {
225 r = key->subkeys->keyid;
226 gpgme_key_release(key);
227 }
228 // r is a static variable, let's copy it.
229 verified_key = g_strdup(r);
230 *sigsum = vr->signatures->summary;
231 }
232 }
233 gpgme_data_release(data_text);
234 }
235 gpgme_data_release(data_sign);
236 }
237 if (err)
238 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
239 "GPGME error: %s", gpgme_strerror(err));
240 gpgme_release(ctx);
241 g_free(data);
242 return verified_key;
243 }
244
245 // gpg_sign(gpg_data)
246 // Return a signature of gpg_data (or NULL).
247 // The returned string must be freed with g_free() after use.
248 char *gpg_sign(const char *gpg_data)
249 {
250 gpgme_ctx_t ctx;
251 gpgme_data_t in, out;
252 char *p;
253 char *signed_data = NULL;
254 size_t nread;
255 gpgme_key_t key;
256 gpgme_error_t err;
257
258 if (!gpg.enabled || !gpg.private_key)
259 return NULL;
260
261 err = gpgme_new(&ctx);
262 if (err) {
263 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
264 "GPGME error: %s", gpgme_strerror(err));
265 return NULL;
266 }
267
268 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
269 gpgme_set_textmode(ctx, 0);
270 gpgme_set_armor(ctx, 1);
271
272 p = getenv("GPG_AGENT_INFO");
273 if (!(p && strchr(p, ':')))
274 gpgme_set_passphrase_cb(ctx, passphrase_cb, 0);
275
276 err = gpgme_get_key(ctx, gpg.private_key, &key, 1);
277 if (!err) {
278 gpgme_signers_clear(ctx);
279 gpgme_signers_add(ctx, key);
280 gpgme_key_release(key);
281 err = gpgme_data_new_from_mem(&in, gpg_data, strlen(gpg_data), 0);
282 }
283 if (!err) {
284 err = gpgme_data_new(&out);
285 if (!err) {
286 err = gpgme_op_sign(ctx, in, out, GPGME_SIG_MODE_DETACH);
287 if (!err) {
288 signed_data = gpgme_data_release_and_get_mem(out, &nread);
289 if (signed_data) {
290 // We need to add a trailing NULL
291 char *dd = g_strndup(signed_data, nread);
292 free(signed_data);
293 signed_data = strip_header_footer(dd);
294 g_free(dd);
295 }
296 } else {
297 gpgme_data_release(out);
298 }
299 }
300 gpgme_data_release(in);
301 }
302 if (err && err != GPG_ERR_CANCELED)
303 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
304 "GPGME error: %s", gpgme_strerror(err));
305 gpgme_release(ctx);
306 return signed_data;
307 }
308
309 // gpg_decrypt(gpg_data)
310 // Return decrypted gpg_data (or NULL).
311 // The returned string must be freed with g_free() after use.
312 char *gpg_decrypt(const char *gpg_data)
313 {
314 gpgme_ctx_t ctx;
315 gpgme_data_t in, out;
316 char *p, *data;
317 char *decrypted_data = NULL;
318 size_t nread;
319 gpgme_error_t err;
320 const char prefix[] = "-----BEGIN PGP MESSAGE-----\n\n";
321 const char suffix[] = "\n-----END PGP MESSAGE-----\n";
322
323 if (!gpg.enabled)
324 return NULL;
325
326 err = gpgme_new(&ctx);
327 if (err) {
328 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
329 "GPGME error: %s", gpgme_strerror(err));
330 return NULL;
331 }
332
333 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
334
335 p = getenv("GPG_AGENT_INFO");
336 if (!(p && strchr(p, ':')))
337 gpgme_set_passphrase_cb(ctx, passphrase_cb, 0);
338
339 // Surround the given data with the prefix & suffix
340 data = g_new(char, sizeof(prefix) + sizeof(suffix) + strlen(gpg_data));
341 strcpy(data, prefix);
342 strcat(data, gpg_data);
343 strcat(data, suffix);
344
345 err = gpgme_data_new_from_mem(&in, data, strlen(data), 0);
346 if (!err) {
347 err = gpgme_data_new(&out);
348 if (!err) {
349 err = gpgme_op_decrypt(ctx, in, out);
350 if (!err) {
351 decrypted_data = gpgme_data_release_and_get_mem(out, &nread);
352 if (decrypted_data) {
353 // We need to add a trailing NULL
354 char *dd = g_strndup(decrypted_data, nread);
355 free(decrypted_data);
356 decrypted_data = dd;
357 }
358 } else {
359 gpgme_data_release(out);
360 }
361 }
362 gpgme_data_release(in);
363 }
364 if (err && err != GPG_ERR_CANCELED)
365 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
366 "GPGME error: %s", gpgme_strerror(err));
367 gpgme_release(ctx);
368 g_free(data);
369 return decrypted_data;
370 }
371
372 // gpg_encrypt(gpg_data, keyid)
373 // Return encrypted gpg_data with the key keyid (or NULL).
374 // The returned string must be freed with g_free() after use.
375 char *gpg_encrypt(const char *gpg_data, const char *keyid)
376 {
377 gpgme_ctx_t ctx;
378 gpgme_data_t in, out;
379 char *encrypted_data = NULL, *edata;
380 size_t nread;
381 gpgme_key_t key;
382 gpgme_error_t err;
383
384 if (!gpg.enabled)
385 return NULL;
386
387 err = gpgme_new(&ctx);
388 if (err) {
389 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
390 "GPGME error: %s", gpgme_strerror(err));
391 return NULL;
392 }
393
394 gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP);
395 gpgme_set_textmode(ctx, 0);
396 gpgme_set_armor(ctx, 1);
397
398 err = gpgme_get_key(ctx, keyid, &key, 0);
399 if (!err) {
400 gpgme_key_t keys[] = { key, 0 };
401 err = gpgme_data_new_from_mem(&in, gpg_data, strlen(gpg_data), 0);
402 if (!err) {
403 err = gpgme_data_new(&out);
404 if (!err) {
405 err = gpgme_op_encrypt(ctx, keys, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
406 if (!err)
407 encrypted_data = gpgme_data_release_and_get_mem(out, &nread);
408 else
409 gpgme_data_release(out);
410 }
411 gpgme_data_release(in);
412 }
413 gpgme_key_release(key);
414 }
415 if (err && err != GPG_ERR_CANCELED)
416 scr_LogPrint(LPRINT_LOGNORM|LPRINT_NOTUTF8,
417 "GPGME error: %s", gpgme_strerror(err));
418 gpgme_release(ctx);
419 edata = strip_header_footer(encrypted_data);
420 if (encrypted_data)
421 free(encrypted_data);
422 return edata;
423 }
424
425 // gpg_test_passphrase()
426 // Test the current gpg.passphrase with gpg.private_key.
427 // If the test doesn't succeed, the passphrase is cleared and a non-null
428 // value is returned.
429 int gpg_test_passphrase(void)
430 {
431 char *s;
432
433 if (!gpg.private_key)
434 return -1; // No private key...
435
436 s = gpg_sign("test");
437 if (s) {
438 free(s);
439 return 0; // Ok, test successful
440 }
441 // The passphrase is wrong (if provided)
442 gpg_set_passphrase(NULL);
443 return -1;
444 }
445
446 inline int gpg_enabled(void)
447 {
448 return gpg.enabled;
449 }
450
451 #else /* not HAVE_GPGME */
452
453 inline int gpg_enabled(void)
454 {
455 return 0;
456 }
457
458 #endif /* HAVE_GPGME */
459
460 /* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */