Line data Source code
1 : /* run-sign.c - Helper to perform a sign operation
2 : Copyright (C) 2009 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
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.
10 :
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.
15 :
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/>.
18 : */
19 :
20 : /* We need to include config.h so that we know whether we are building
21 : with large file system (LFS) support. */
22 : #ifdef HAVE_CONFIG_H
23 : #include <config.h>
24 : #endif
25 :
26 : #include <stdlib.h>
27 : #include <stdio.h>
28 : #include <string.h>
29 :
30 : #include <gpgme.h>
31 :
32 : #define PGM "run-sign"
33 :
34 : #include "run-support.h"
35 :
36 :
37 : static int verbose;
38 :
39 : static gpg_error_t
40 0 : status_cb (void *opaque, const char *keyword, const char *value)
41 : {
42 : (void)opaque;
43 0 : printf ("status_cb: %s %s\n", keyword, value);
44 0 : return 0;
45 : }
46 :
47 :
48 : static void
49 0 : print_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
50 : {
51 : gpgme_invalid_key_t invkey;
52 : gpgme_new_signature_t sig;
53 :
54 0 : for (invkey = result->invalid_signers; invkey; invkey = invkey->next)
55 0 : printf ("Signing key `%s' not used: %s <%s>\n",
56 0 : nonnull (invkey->fpr),
57 : gpg_strerror (invkey->reason), gpg_strsource (invkey->reason));
58 :
59 0 : for (sig = result->signatures; sig; sig = sig->next)
60 : {
61 0 : printf ("Key fingerprint: %s\n", nonnull (sig->fpr));
62 0 : printf ("Signature type : %d\n", sig->type);
63 0 : printf ("Public key algo: %d\n", sig->pubkey_algo);
64 0 : printf ("Hash algo .....: %d\n", sig->hash_algo);
65 0 : printf ("Creation time .: %ld\n", sig->timestamp);
66 0 : printf ("Sig class .....: 0x%u\n", sig->sig_class);
67 : }
68 0 : }
69 :
70 :
71 :
72 : static int
73 0 : show_usage (int ex)
74 : {
75 0 : fputs ("usage: " PGM " [options] FILE\n\n"
76 : "Options:\n"
77 : " --verbose run in verbose mode\n"
78 : " --status print status lines from the backend\n"
79 : " --openpgp use the OpenPGP protocol (default)\n"
80 : " --cms use the CMS protocol\n"
81 : " --uiserver use the UI server\n"
82 : " --loopback use a loopback pinentry\n"
83 : " --key NAME use key NAME for signing\n"
84 : , stderr);
85 0 : exit (ex);
86 : }
87 :
88 :
89 : int
90 0 : main (int argc, char **argv)
91 : {
92 0 : int last_argc = -1;
93 : gpgme_error_t err;
94 : gpgme_ctx_t ctx;
95 0 : const char *key_string = NULL;
96 0 : gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
97 0 : gpgme_sig_mode_t sigmode = GPGME_SIG_MODE_NORMAL;
98 : gpgme_data_t in, out;
99 : gpgme_sign_result_t result;
100 0 : int print_status = 0;
101 0 : int use_loopback = 0;
102 :
103 0 : if (argc)
104 0 : { argc--; argv++; }
105 :
106 0 : while (argc && last_argc != argc )
107 : {
108 0 : last_argc = argc;
109 0 : if (!strcmp (*argv, "--"))
110 : {
111 0 : argc--; argv++;
112 0 : break;
113 : }
114 0 : else if (!strcmp (*argv, "--help"))
115 0 : show_usage (0);
116 0 : else if (!strcmp (*argv, "--verbose"))
117 : {
118 0 : verbose = 1;
119 0 : argc--; argv++;
120 : }
121 0 : else if (!strcmp (*argv, "--status"))
122 : {
123 0 : print_status = 1;
124 0 : argc--; argv++;
125 : }
126 0 : else if (!strcmp (*argv, "--openpgp"))
127 : {
128 0 : protocol = GPGME_PROTOCOL_OpenPGP;
129 0 : argc--; argv++;
130 : }
131 0 : else if (!strcmp (*argv, "--cms"))
132 : {
133 0 : protocol = GPGME_PROTOCOL_CMS;
134 0 : argc--; argv++;
135 : }
136 0 : else if (!strcmp (*argv, "--uiserver"))
137 : {
138 0 : protocol = GPGME_PROTOCOL_UISERVER;
139 0 : argc--; argv++;
140 : }
141 0 : else if (!strcmp (*argv, "--key"))
142 : {
143 0 : argc--; argv++;
144 0 : if (!argc)
145 0 : show_usage (1);
146 0 : key_string = *argv;
147 0 : argc--; argv++;
148 : }
149 0 : else if (!strcmp (*argv, "--loopback"))
150 : {
151 0 : use_loopback = 1;
152 0 : argc--; argv++;
153 : }
154 0 : else if (!strncmp (*argv, "--", 2))
155 0 : show_usage (1);
156 :
157 : }
158 :
159 0 : if (argc != 1)
160 0 : show_usage (1);
161 :
162 0 : if (key_string && protocol == GPGME_PROTOCOL_UISERVER)
163 : {
164 0 : fprintf (stderr, PGM ": ignoring --key in UI-server mode\n");
165 0 : key_string = NULL;
166 : }
167 :
168 0 : init_gpgme (protocol);
169 :
170 0 : err = gpgme_new (&ctx);
171 0 : fail_if_err (err);
172 0 : gpgme_set_protocol (ctx, protocol);
173 0 : gpgme_set_armor (ctx, 1);
174 0 : if (print_status)
175 0 : gpgme_set_status_cb (ctx, status_cb, NULL);
176 0 : if (use_loopback)
177 0 : gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK);
178 :
179 0 : if (key_string)
180 : {
181 : gpgme_key_t akey;
182 :
183 0 : err = gpgme_get_key (ctx, key_string, &akey, 1);
184 0 : if (err)
185 : {
186 0 : exit (1);
187 : }
188 0 : err = gpgme_signers_add (ctx, akey);
189 0 : fail_if_err (err);
190 0 : gpgme_key_unref (akey);
191 : }
192 :
193 0 : err = gpgme_data_new_from_file (&in, *argv, 1);
194 0 : if (err)
195 : {
196 0 : fprintf (stderr, PGM ": error reading `%s': %s\n",
197 : *argv, gpg_strerror (err));
198 0 : exit (1);
199 : }
200 :
201 0 : err = gpgme_data_new (&out);
202 0 : fail_if_err (err);
203 :
204 0 : err = gpgme_op_sign (ctx, in, out, sigmode);
205 0 : result = gpgme_op_sign_result (ctx);
206 0 : if (result)
207 0 : print_result (result, sigmode);
208 0 : if (err)
209 : {
210 0 : fprintf (stderr, PGM ": signing failed: %s\n", gpg_strerror (err));
211 0 : exit (1);
212 : }
213 :
214 0 : fputs ("Begin Output:\n", stdout);
215 0 : print_data (out);
216 0 : fputs ("End Output.\n", stdout);
217 0 : gpgme_data_release (out);
218 :
219 0 : gpgme_data_release (in);
220 :
221 0 : gpgme_release (ctx);
222 0 : return 0;
223 : }
|