1 /* run-encrypt.c - Helper to perform an encrypt operation
2 * Copyright (C) 2016 g10 Code GmbH
4 * This file is part of GPGME.
6 * GPGME is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * GPGME is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
20 /* We need to include config.h so that we know whether we are building
21 with large file system (LFS) support. */
32 #define PGM "run-encrypt"
34 #include "run-support.h"
41 xstrdup (const char *string)
43 char *p = strdup (string);
46 fprintf (stderr, "strdup failed\n");
54 status_cb (void *opaque, const char *keyword, const char *value)
57 fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value));
63 progress_cb (void *opaque, const char *what, int type, int current, int total)
69 fprintf (stderr, "progress for '%s' %u%% (%d of %d)\n",
71 (unsigned)(((double)current / total) * 100), current, total);
73 fprintf (stderr, "progress for '%s' %d\n", nonnull(what), current);
79 print_result (gpgme_encrypt_result_t result)
81 gpgme_invalid_key_t invkey;
83 for (invkey = result->invalid_recipients; invkey; invkey = invkey->next)
84 printf ("Encryption key `%s' not used: %s <%s>\n",
85 nonnull (invkey->fpr),
86 gpg_strerror (invkey->reason), gpg_strsource (invkey->reason));
94 fputs ("usage: " PGM " [options] FILE\n\n"
96 " --verbose run in verbose mode\n"
97 " --status print status lines from the backend\n"
98 " --progress print progress info\n"
99 " --openpgp use the OpenPGP protocol (default)\n"
100 " --cms use the CMS protocol\n"
101 " --uiserver use the UI server\n"
102 " --loopback use a loopback pinentry\n"
103 " --key NAME encrypt to key NAME\n"
104 " --keystring NAMES encrypt to ';' delimited NAMES\n"
105 " --throw-keyids use this option\n"
106 " --no-symkey-cache disable the use of that cache\n"
107 " --wrap assume input is valid OpenPGP message\n"
108 " --symmetric encrypt symmetric (OpenPGP only)\n"
115 main (int argc, char **argv)
120 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
121 gpgme_data_t in, out;
122 gpgme_encrypt_result_t result;
123 int print_status = 0;
124 int print_progress = 0;
125 int use_loopback = 0;
127 gpgme_key_t keys[10+1];
129 char *keystring = NULL;
131 gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST;
133 int no_symkey_cache = 0;
138 if (DIM(keys) != DIM(keyargs)+1)
141 while (argc && last_argc != argc )
144 if (!strcmp (*argv, "--"))
149 else if (!strcmp (*argv, "--help"))
151 else if (!strcmp (*argv, "--verbose"))
156 else if (!strcmp (*argv, "--status"))
161 else if (!strcmp (*argv, "--progress"))
166 else if (!strcmp (*argv, "--openpgp"))
168 protocol = GPGME_PROTOCOL_OpenPGP;
171 else if (!strcmp (*argv, "--cms"))
173 protocol = GPGME_PROTOCOL_CMS;
176 else if (!strcmp (*argv, "--uiserver"))
178 protocol = GPGME_PROTOCOL_UISERVER;
181 else if (!strcmp (*argv, "--key"))
186 if (keycount == DIM (keyargs))
188 keyargs[keycount++] = *argv;
191 else if (!strcmp (*argv, "--keystring"))
196 keystring = xstrdup (*argv);
197 for (i=0; keystring[i]; i++)
198 if (keystring[i] == ';')
202 else if (!strcmp (*argv, "--throw-keyids"))
204 flags |= GPGME_ENCRYPT_THROW_KEYIDS;
207 else if (!strcmp (*argv, "--wrap"))
209 flags |= GPGME_ENCRYPT_WRAP;
212 else if (!strcmp (*argv, "--loopback"))
217 else if (!strcmp (*argv, "--symmetric"))
219 flags |= GPGME_ENCRYPT_SYMMETRIC;
222 else if (!strcmp (*argv, "--no-symkey-cache"))
227 else if (!strncmp (*argv, "--", 2))
235 init_gpgme (protocol);
237 err = gpgme_new (&ctx);
239 gpgme_set_protocol (ctx, protocol);
240 gpgme_set_armor (ctx, 1);
243 gpgme_set_status_cb (ctx, status_cb, NULL);
244 gpgme_set_ctx_flag (ctx, "full-status", "1");
247 gpgme_set_progress_cb (ctx, progress_cb, NULL);
250 gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
251 gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
255 err = gpgme_set_ctx_flag (ctx, "no-symkey-cache", "1");
258 fprintf (stderr, PGM ": error setting no-symkey-cache: %s\n",
259 gpgme_strerror (err));
264 for (i=0; i < keycount; i++)
266 err = gpgme_get_key (ctx, keyargs[i], &keys[i], 0);
271 err = gpgme_data_new_from_file (&in, *argv, 1);
274 fprintf (stderr, PGM ": error reading `%s': %s\n",
275 *argv, gpg_strerror (err));
278 offset = gpgme_data_seek (in, 0, SEEK_END);
279 if (offset == (gpgme_off_t)(-1))
281 err = gpg_error_from_syserror ();
282 fprintf (stderr, PGM ": error seeking `%s': %s\n",
283 *argv, gpg_strerror (err));
286 if (gpgme_data_seek (in, 0, SEEK_SET) == (gpgme_off_t)(-1))
288 err = gpg_error_from_syserror ();
289 fprintf (stderr, PGM ": error seeking `%s': %s\n",
290 *argv, gpg_strerror (err));
297 p = numbuf + sizeof numbuf;
301 *--p = '0' + (offset % 10);
305 err = gpgme_data_set_flag (in, "size-hint", p);
308 fprintf (stderr, PGM ": error setting size-hint for `%s': %s\n",
309 *argv, gpg_strerror (err));
314 err = gpgme_data_new (&out);
317 err = gpgme_op_encrypt_ext (ctx, keycount ? keys : NULL, keystring,
319 result = gpgme_op_encrypt_result (ctx);
321 print_result (result);
324 fprintf (stderr, PGM ": encrypting failed: %s\n", gpg_strerror (err));
328 fputs ("Begin Output:\n", stdout);
330 fputs ("End Output.\n", stdout);
331 gpgme_data_release (out);
333 gpgme_data_release (in);
335 for (i=0; i < keycount; i++)
336 gpgme_key_unref (keys[i]);