1 /* run-sign.c - Helper to perform a sign operation
2 * Copyright (C) 2009 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://gnu.org/licenses/>.
18 * SPDX-License-Identifier: LGPL-2.1-or-later
21 /* We need to include config.h so that we know whether we are building
22 with large file system (LFS) support. */
33 #define PGM "run-sign"
35 #include "run-support.h"
41 status_cb (void *opaque, const char *keyword, const char *value)
44 printf ("status_cb: %s %s\n", keyword, value);
50 print_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
52 gpgme_invalid_key_t invkey;
53 gpgme_new_signature_t sig;
57 for (invkey = result->invalid_signers; invkey; invkey = invkey->next)
58 printf ("Signing key `%s' not used: %s <%s>\n",
59 nonnull (invkey->fpr),
60 gpg_strerror (invkey->reason), gpg_strsource (invkey->reason));
62 for (sig = result->signatures; sig; sig = sig->next)
64 printf ("Key fingerprint: %s\n", nonnull (sig->fpr));
65 printf ("Signature type : %d\n", sig->type);
66 printf ("Public key algo: %d\n", sig->pubkey_algo);
67 printf ("Hash algo .....: %d\n", sig->hash_algo);
68 printf ("Creation time .: %ld\n", sig->timestamp);
69 printf ("Sig class .....: 0x%u\n", sig->sig_class);
78 fputs ("usage: " PGM " [options] FILE\n\n"
80 " --verbose run in verbose mode\n"
81 " --status print status lines from the backend\n"
82 " --openpgp use the OpenPGP protocol (default)\n"
83 " --cms use the CMS protocol\n"
84 " --uiserver use the UI server\n"
85 " --loopback use a loopback pinentry\n"
86 " --key NAME use key NAME for signing\n"
87 " --sender MBOX use MBOX as sender address\n"
88 " --include-key-block use this option with gpg\n"
95 main (int argc, char **argv)
100 const char *key_string = NULL;
101 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
102 gpgme_sig_mode_t sigmode = GPGME_SIG_MODE_NORMAL;
103 gpgme_data_t in, out;
104 gpgme_sign_result_t result;
105 int print_status = 0;
106 int use_loopback = 0;
107 int include_key_block = 0;
108 const char *sender = NULL;
114 while (argc && last_argc != argc )
117 if (!strcmp (*argv, "--"))
122 else if (!strcmp (*argv, "--help"))
124 else if (!strcmp (*argv, "--verbose"))
129 else if (!strcmp (*argv, "--status"))
134 else if (!strcmp (*argv, "--openpgp"))
136 protocol = GPGME_PROTOCOL_OpenPGP;
139 else if (!strcmp (*argv, "--cms"))
141 protocol = GPGME_PROTOCOL_CMS;
144 else if (!strcmp (*argv, "--uiserver"))
146 protocol = GPGME_PROTOCOL_UISERVER;
149 else if (!strcmp (*argv, "--key"))
157 else if (!strcmp (*argv, "--sender"))
165 else if (!strcmp (*argv, "--loopback"))
170 else if (!strcmp (*argv, "--include-key-block"))
172 include_key_block = 1;
175 else if (!strncmp (*argv, "--", 2))
183 if (key_string && protocol == GPGME_PROTOCOL_UISERVER)
185 fprintf (stderr, PGM ": ignoring --key in UI-server mode\n");
189 init_gpgme (protocol);
191 err = gpgme_new (&ctx);
193 gpgme_set_protocol (ctx, protocol);
194 gpgme_set_armor (ctx, 1);
196 gpgme_set_status_cb (ctx, status_cb, NULL);
198 gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
204 err = gpgme_get_key (ctx, key_string, &akey, 1);
207 fprintf (stderr, PGM ": get key '%s' failed: %s\n",
208 key_string, gpg_strerror (err));
211 err = gpgme_signers_add (ctx, akey);
213 gpgme_key_unref (akey);
218 err = gpgme_set_sender (ctx, sender);
222 if (include_key_block)
224 err = gpgme_set_ctx_flag (ctx, "include-key-block", "1");
227 fprintf (stderr, PGM ": error setting include-key-block: %s\n",
228 gpgme_strerror (err));
233 err = gpgme_data_new_from_file (&in, *argv, 1);
236 fprintf (stderr, PGM ": error reading `%s': %s\n",
237 *argv, gpg_strerror (err));
241 err = gpgme_data_new (&out);
244 err = gpgme_op_sign (ctx, in, out, sigmode);
245 result = gpgme_op_sign_result (ctx);
247 print_result (result, sigmode);
250 fprintf (stderr, PGM ": signing failed: %s\n", gpg_strerror (err));
254 if ((s = gpgme_get_ctx_flag (ctx, "redraw")) && *s)
255 fputs ("Screen redraw suggested\n", stdout);
257 fputs ("Begin Output:\n", stdout);
259 fputs ("End Output.\n", stdout);
260 gpgme_data_release (out);
262 gpgme_data_release (in);