Line data Source code
1 : /*
2 : run-keylist.cpp
3 :
4 : This file is part of GpgMEpp's test suite.
5 : Copyright (c) 2018 Intevation GmbH
6 :
7 : QGpgME is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU General Public License,
9 : version 2, as published by the Free Software Foundation.
10 :
11 : QGpgME is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program; if not, write to the Free Software
18 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : In addition, as a special exception, the copyright holders give
21 : permission to link the code of this program with any edition of
22 : the Qt library by Trolltech AS, Norway (or with modified versions
23 : of Qt that use the same license as Qt), and distribute linked
24 : combinations including the two. You must obey the GNU General
25 : Public License in all respects for all of the code used other than
26 : Qt. If you modify this file, you may extend this exception to
27 : your version of the file, but you are not obligated to do so. If
28 : you do not wish to do so, delete this exception statement from
29 : your version.
30 : */
31 : #ifdef HAVE_CONFIG_H
32 : #include "config.h"
33 : #endif
34 :
35 : #include "context.h"
36 : #include "key.h"
37 : #include "data.h"
38 : #include "verificationresult.h"
39 :
40 : #include <memory>
41 : #include <sstream>
42 : #include <iostream>
43 :
44 : using namespace GpgME;
45 : static int
46 0 : show_usage (int ex)
47 : {
48 : fputs ("usage: run-verify [options] [DETACHEDSIGFILE] FILE\n\n"
49 : "Options:\n"
50 : " --verbose run in verbose mode\n"
51 : " --openpgp use the OpenPGP protocol (default)\n"
52 : " --cms use the CMS protocol\n"
53 : " --sender MBOX use MBOX as sender address\n"
54 : " --repeat N repeat it N times\n"
55 : " --list-key list the signing key afterwards\n"
56 0 : , stderr);
57 0 : exit (ex);
58 : }
59 :
60 : int
61 0 : main (int argc, char **argv)
62 : {
63 0 : int last_argc = -1;
64 0 : Protocol protocol = OpenPGP;
65 0 : std::string sender;
66 0 : int repeats = 1;
67 0 : bool verbose = false;
68 0 : bool list_key = false;
69 :
70 0 : if (argc)
71 0 : { argc--; argv++; }
72 :
73 0 : while (argc && last_argc != argc )
74 : {
75 0 : last_argc = argc;
76 0 : if (!strcmp (*argv, "--"))
77 : {
78 0 : argc--; argv++;
79 0 : break;
80 : }
81 0 : else if (!strcmp (*argv, "--help"))
82 0 : show_usage (0);
83 0 : else if (!strcmp (*argv, "--verbose"))
84 : {
85 0 : verbose = true;
86 0 : argc--; argv++;
87 : }
88 0 : else if (!strcmp (*argv, "--list-key"))
89 : {
90 0 : list_key = true;
91 0 : argc--; argv++;
92 : }
93 0 : else if (!strcmp (*argv, "--openpgp"))
94 : {
95 0 : protocol = OpenPGP;
96 0 : argc--; argv++;
97 : }
98 0 : else if (!strcmp (*argv, "--cms"))
99 : {
100 0 : protocol = CMS;
101 0 : argc--; argv++;
102 : }
103 0 : else if (!strcmp (*argv, "--sender"))
104 : {
105 0 : argc--; argv++;
106 0 : if (!argc)
107 0 : show_usage (1);
108 0 : sender = *argv;
109 0 : argc--; argv++;
110 : }
111 0 : else if (!strcmp (*argv, "--repeat"))
112 : {
113 0 : argc--; argv++;
114 0 : if (!argc)
115 0 : show_usage (1);
116 0 : repeats = atoi (*argv);
117 0 : argc--; argv++;
118 : }
119 0 : else if (!strncmp (*argv, "--", 2))
120 0 : show_usage (1);
121 : }
122 :
123 0 : if (argc < 1 || argc > 2)
124 0 : show_usage (1);
125 :
126 0 : GpgME::initializeLibrary();
127 :
128 0 : for (int i = 0; i < repeats; i++) {
129 0 : std::cout << "Starting run: " << i << std::endl;
130 0 : auto ctx = std::unique_ptr<Context> (Context::createForProtocol(protocol));
131 0 : if (!ctx) {
132 0 : std::cerr << "Failed to get Context";
133 0 : return -1;
134 : }
135 :
136 0 : std::FILE *fp_sig = fopen (argv[0], "rb");
137 0 : if (!fp_sig) {
138 0 : std::cerr << "Failed to open sig file";
139 0 : exit (1);
140 : }
141 :
142 0 : std::FILE *fp_msg = nullptr;
143 0 : if (argc > 1)
144 : {
145 0 : fp_msg = fopen (argv[1], "rb");
146 0 : if (!fp_msg) {
147 0 : std::cerr << "Failed to open msg file";
148 0 : exit (1);
149 : }
150 : }
151 0 : Data dSig(fp_sig);
152 0 : Data dMsg;
153 0 : bool is_opaque = true;
154 0 : if (fp_msg) {
155 0 : dMsg = Data(fp_msg);
156 0 : is_opaque = false;
157 : }
158 :
159 0 : if (!sender.empty()) {
160 0 : ctx->setSender(sender.c_str());
161 : }
162 :
163 0 : Data output;
164 0 : VerificationResult result;
165 0 : if (is_opaque) {
166 0 : result = ctx->verifyOpaqueSignature(dSig, output);
167 : } else {
168 0 : result = ctx->verifyDetachedSignature(dSig, dMsg);
169 : }
170 :
171 0 : Signature sig;
172 0 : if (result.numSignatures()) {
173 0 : sig = result.signature(0);
174 : }
175 :
176 0 : if (list_key && !sig.isNull()) {
177 0 : sig.key(true, false);
178 : }
179 :
180 0 : if (verbose) {
181 0 : std::cout << "Result: " << result << std::endl;
182 : } else {
183 0 : std::cout << "Err:" << result.error() << std::endl;
184 : }
185 : }
186 0 : }
|