1 /* encrypt_cb.c
2 *
3 * Copyright 2011 Hans Alves <alves.h88@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21
22 #include "geanypg.h"
23
geanypg_encrypt(encrypt_data * ed,gpgme_key_t * recp,int sign,int flags)24 static void geanypg_encrypt(encrypt_data * ed, gpgme_key_t * recp, int sign, int flags)
25 { /* FACTORIZE */
26 gpgme_data_t plain, cipher;
27 gpgme_error_t err;
28 FILE * tempfile;
29 tempfile = tmpfile();
30 if (!(tempfile))
31 {
32 g_warning("%s: %s.", _("couldn't create tempfile"), strerror(errno));
33 return ;
34 }
35 gpgme_data_new_from_stream(&cipher, tempfile);
36 gpgme_data_set_encoding(cipher, GPGME_DATA_ENCODING_ARMOR);
37
38 geanypg_load_buffer(&plain);
39
40 /* do the actual encryption */
41 if (sign)
42 err = gpgme_op_encrypt_sign(ed->ctx, recp, flags, plain, cipher);
43 else
44 err = gpgme_op_encrypt(ed->ctx, recp, flags, plain, cipher);
45 if (err != GPG_ERR_NO_ERROR && gpgme_err_code(err) != GPG_ERR_CANCELED)
46 geanypg_show_err_msg(err);
47 else if(gpgme_err_code(err) != GPG_ERR_CANCELED)
48 {
49 rewind(tempfile);
50 geanypg_write_file(tempfile);
51 }
52
53 fclose(tempfile);
54 /* release buffers */
55 gpgme_data_release(plain);
56 gpgme_data_release(cipher);
57 }
58
geanypg_encrypt_cb(GtkMenuItem * menuitem,gpointer user_data)59 void geanypg_encrypt_cb(GtkMenuItem * menuitem, gpointer user_data)
60 {
61 int sign;
62 encrypt_data ed;
63 gpgme_error_t err;
64 geanypg_init_ed(&ed);
65 err = gpgme_new(&ed.ctx);
66 if (err && geanypg_show_err_msg(err))
67 return;
68 gpgme_set_armor(ed.ctx, 1);
69 gpgme_set_passphrase_cb(ed.ctx, geanypg_passphrase_cb, NULL);
70 if (geanypg_get_keys(&ed) && geanypg_get_secret_keys(&ed))
71 {
72 gpgme_key_t * recp = NULL;
73 if (geanypg_encrypt_selection_dialog(&ed, &recp, &sign))
74 {
75 int flags = 0;
76 int stop = 0;
77 gpgme_key_t * key = recp;
78 while (*key)
79 {
80 if ((*key)->owner_trust != GPGME_VALIDITY_ULTIMATE &&
81 (*key)->owner_trust != GPGME_VALIDITY_FULL &&
82 (*key)->owner_trust != GPGME_VALIDITY_MARGINAL)
83 {
84 if (dialogs_show_question(_("The key with user ID \"%s\" has validity \"%s\".\n\n"
85 "WARNING: It is NOT certain that the key belongs to the person named in the user ID.\n\n"
86 "Are you *really* sure you want to use this key anyway?"),
87 (*key)->uids->uid, geanypg_validity((*key)->owner_trust)))
88 flags = GPGME_ENCRYPT_ALWAYS_TRUST;
89 else
90 stop = 1;
91 }
92 ++key;
93 }
94 if (*recp && !stop)
95 geanypg_encrypt(&ed, recp, sign, flags);
96 else if (!stop && dialogs_show_question(_("No recipients were selected,\nuse symmetric cipher?")))
97 geanypg_encrypt(&ed, NULL, sign, flags);
98 }
99 if (recp)
100 free(recp);
101 }
102 geanypg_release_keys(&ed);
103 gpgme_release(ed.ctx);
104 }
105