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 <http://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-sign"
34 #include "run-support.h"
40 status_cb (void *opaque, const char *keyword, const char *value)
43 printf ("status_cb: %s %s\n", keyword, value);
49 print_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
51 gpgme_invalid_key_t invkey;
52 gpgme_new_signature_t sig;
56 for (invkey = result->invalid_signers; invkey; invkey = invkey->next)
57 printf ("Signing key `%s' not used: %s <%s>\n",
58 nonnull (invkey->fpr),
59 gpg_strerror (invkey->reason), gpg_strsource (invkey->reason));
61 for (sig = result->signatures; sig; sig = sig->next)
63 printf ("Key fingerprint: %s\n", nonnull (sig->fpr));
64 printf ("Signature type : %d\n", sig->type);
65 printf ("Public key algo: %d\n", sig->pubkey_algo);
66 printf ("Hash algo .....: %d\n", sig->hash_algo);
67 printf ("Creation time .: %ld\n", sig->timestamp);
68 printf ("Sig class .....: 0x%u\n", sig->sig_class);
77 fputs ("usage: " PGM " [options] FILE\n\n"
79 " --verbose run in verbose mode\n"
80 " --status print status lines from the backend\n"
81 " --openpgp use the OpenPGP protocol (default)\n"
82 " --cms use the CMS protocol\n"
83 " --uiserver use the UI server\n"
84 " --loopback use a loopback pinentry\n"
85 " --key NAME use key NAME for signing\n"
92 main (int argc, char **argv)
97 const char *key_string = NULL;
98 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
99 gpgme_sig_mode_t sigmode = GPGME_SIG_MODE_NORMAL;
100 gpgme_data_t in, out;
101 gpgme_sign_result_t result;
102 int print_status = 0;
103 int use_loopback = 0;
108 while (argc && last_argc != argc )
111 if (!strcmp (*argv, "--"))
116 else if (!strcmp (*argv, "--help"))
118 else if (!strcmp (*argv, "--verbose"))
123 else if (!strcmp (*argv, "--status"))
128 else if (!strcmp (*argv, "--openpgp"))
130 protocol = GPGME_PROTOCOL_OpenPGP;
133 else if (!strcmp (*argv, "--cms"))
135 protocol = GPGME_PROTOCOL_CMS;
138 else if (!strcmp (*argv, "--uiserver"))
140 protocol = GPGME_PROTOCOL_UISERVER;
143 else if (!strcmp (*argv, "--key"))
151 else if (!strcmp (*argv, "--loopback"))
156 else if (!strncmp (*argv, "--", 2))
164 if (key_string && protocol == GPGME_PROTOCOL_UISERVER)
166 fprintf (stderr, PGM ": ignoring --key in UI-server mode\n");
170 init_gpgme (protocol);
172 err = gpgme_new (&ctx);
174 gpgme_set_protocol (ctx, protocol);
175 gpgme_set_armor (ctx, 1);
177 gpgme_set_status_cb (ctx, status_cb, NULL);
179 gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
185 err = gpgme_get_key (ctx, key_string, &akey, 1);
190 err = gpgme_signers_add (ctx, akey);
192 gpgme_key_unref (akey);
195 err = gpgme_data_new_from_file (&in, *argv, 1);
198 fprintf (stderr, PGM ": error reading `%s': %s\n",
199 *argv, gpg_strerror (err));
203 err = gpgme_data_new (&out);
206 err = gpgme_op_sign (ctx, in, out, sigmode);
207 result = gpgme_op_sign_result (ctx);
209 print_result (result, sigmode);
212 fprintf (stderr, PGM ": signing failed: %s\n", gpg_strerror (err));
216 fputs ("Begin Output:\n", stdout);
218 fputs ("End Output.\n", stdout);
219 gpgme_data_release (out);
221 gpgme_data_release (in);