Line data Source code
1 : /* run-decrypt.c - Helper to perform a verify operation
2 : Copyright (C) 2009 g10 Code GmbH
3 : 2016 Intevation GmbH
4 :
5 : This file is part of GPGME.
6 :
7 : GPGME is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU Lesser General Public License as
9 : published by the Free Software Foundation; either version 2.1 of
10 : the License, or (at your option) any later version.
11 :
12 : GPGME is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public
18 : License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : /* We need to include config.h so that we know whether we are building
22 : with large file system (LFS) support. */
23 : #ifdef HAVE_CONFIG_H
24 : #include <config.h>
25 : #endif
26 :
27 : #include <stdlib.h>
28 : #include <stdio.h>
29 : #include <string.h>
30 :
31 : #include <gpgme.h>
32 :
33 : #define PGM "run-decrypt"
34 :
35 : #include "run-support.h"
36 :
37 :
38 : static int verbose;
39 :
40 : static gpg_error_t
41 0 : status_cb (void *opaque, const char *keyword, const char *value)
42 : {
43 : (void)opaque;
44 0 : fprintf (stderr, "status_cb: %s %s\n", keyword, value);
45 0 : return 0;
46 : }
47 :
48 :
49 : static void
50 0 : print_result (gpgme_decrypt_result_t result)
51 : {
52 : gpgme_recipient_t recp;
53 0 : int count = 0;
54 0 : printf ("Original file name: %s\n", nonnull(result->file_name));
55 0 : printf ("Wrong key usage: %i\n", result->wrong_key_usage);
56 0 : printf ("Unsupported algorithm: %s\n ", nonnull(result->unsupported_algorithm));
57 :
58 0 : for (recp = result->recipients; recp->next; recp = recp->next)
59 : {
60 0 : printf ("recipient %d\n", count++);
61 0 : printf (" status ....: %s\n", gpgme_strerror (recp->status));
62 0 : printf (" keyid: %s\n", nonnull (recp->keyid));
63 0 : printf (" algo ...: %s\n", gpgme_pubkey_algo_name (recp->pubkey_algo));
64 : }
65 0 : }
66 :
67 :
68 : static int
69 0 : show_usage (int ex)
70 : {
71 0 : fputs ("usage: " PGM " [options] FILE\n\n"
72 : "Options:\n"
73 : " --verbose run in verbose mode\n"
74 : " --status print status lines from the backend\n"
75 : " --openpgp use the OpenPGP protocol (default)\n"
76 : " --cms use the CMS protocol\n"
77 : , stderr);
78 0 : exit (ex);
79 : }
80 :
81 :
82 : int
83 0 : main (int argc, char **argv)
84 : {
85 0 : int last_argc = -1;
86 : gpgme_error_t err;
87 : gpgme_ctx_t ctx;
88 0 : gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
89 0 : FILE *fp_in = NULL;
90 0 : gpgme_data_t in = NULL;
91 0 : gpgme_data_t out = NULL;
92 : gpgme_decrypt_result_t result;
93 0 : int print_status = 0;
94 :
95 0 : if (argc)
96 0 : { argc--; argv++; }
97 :
98 0 : while (argc && last_argc != argc )
99 : {
100 0 : last_argc = argc;
101 0 : if (!strcmp (*argv, "--"))
102 : {
103 0 : argc--; argv++;
104 0 : break;
105 : }
106 0 : else if (!strcmp (*argv, "--help"))
107 0 : show_usage (0);
108 0 : else if (!strcmp (*argv, "--verbose"))
109 : {
110 0 : verbose = 1;
111 0 : argc--; argv++;
112 : }
113 0 : else if (!strcmp (*argv, "--status"))
114 : {
115 0 : print_status = 1;
116 0 : argc--; argv++;
117 : }
118 0 : else if (!strcmp (*argv, "--openpgp"))
119 : {
120 0 : protocol = GPGME_PROTOCOL_OpenPGP;
121 0 : argc--; argv++;
122 : }
123 0 : else if (!strcmp (*argv, "--cms"))
124 : {
125 0 : protocol = GPGME_PROTOCOL_CMS;
126 0 : argc--; argv++;
127 : }
128 0 : else if (!strncmp (*argv, "--", 2))
129 0 : show_usage (1);
130 :
131 : }
132 :
133 0 : if (argc < 1 || argc > 2)
134 0 : show_usage (1);
135 :
136 0 : fp_in = fopen (argv[0], "rb");
137 0 : if (!fp_in)
138 : {
139 0 : err = gpgme_error_from_syserror ();
140 0 : fprintf (stderr, PGM ": can't open `%s': %s\n",
141 : argv[0], gpgme_strerror (err));
142 0 : exit (1);
143 : }
144 :
145 0 : init_gpgme (protocol);
146 :
147 0 : err = gpgme_new (&ctx);
148 0 : fail_if_err (err);
149 0 : gpgme_set_protocol (ctx, protocol);
150 0 : if (print_status)
151 : {
152 0 : gpgme_set_status_cb (ctx, status_cb, NULL);
153 0 : gpgme_set_ctx_flag (ctx, "full-status", "1");
154 : }
155 :
156 0 : err = gpgme_data_new_from_stream (&in, fp_in);
157 0 : if (err)
158 : {
159 0 : fprintf (stderr, PGM ": error allocating data object: %s\n",
160 : gpgme_strerror (err));
161 0 : exit (1);
162 : }
163 :
164 0 : err = gpgme_data_new (&out);
165 0 : if (err)
166 : {
167 0 : fprintf (stderr, PGM ": error allocating data object: %s\n",
168 : gpgme_strerror (err));
169 0 : exit (1);
170 : }
171 :
172 0 : err = gpgme_op_decrypt (ctx, in, out);
173 0 : result = gpgme_op_decrypt_result (ctx);
174 0 : if (err)
175 : {
176 0 : fprintf (stderr, PGM ": decrypt failed: %s\n", gpgme_strerror (err));
177 0 : exit (1);
178 : }
179 0 : if (result) {
180 0 : print_result (result);
181 0 : print_data (out);
182 : }
183 :
184 0 : gpgme_data_release (out);
185 0 : gpgme_data_release (in);
186 :
187 0 : gpgme_release (ctx);
188 0 : return 0;
189 : }
|