Line data Source code
1 : /*
2 : signingresult.cpp - wraps a gpgme verify result
3 : Copyright (C) 2004 Klarälvdalens Datakonsult AB
4 : 2016 Bundesamt für Sicherheit in der Informationstechnik
5 : Software engineering by Intevation GmbH
6 :
7 : This file is part of GPGME++.
8 :
9 : GPGME++ is free software; you can redistribute it and/or
10 : modify it under the terms of the GNU Library General Public
11 : License as published by the Free Software Foundation; either
12 : version 2 of the License, or (at your option) any later version.
13 :
14 : GPGME++ is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Library General Public License for more details.
18 :
19 : You should have received a copy of the GNU Library General Public License
20 : along with GPGME++; see the file COPYING.LIB. If not, write to the
21 : Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 : Boston, MA 02110-1301, USA.
23 : */
24 :
25 : #ifdef HAVE_CONFIG_H
26 : #include "config.h"
27 : #endif
28 :
29 : #include <signingresult.h>
30 : #include "result_p.h"
31 : #include "util.h"
32 :
33 : #include <gpgme.h>
34 :
35 : #include <cstring>
36 : #include <cstdlib>
37 : #include <algorithm>
38 : #include <istream>
39 : #include <iterator>
40 :
41 : #include <string.h>
42 :
43 : class GpgME::SigningResult::Private
44 : {
45 : public:
46 7 : Private(const gpgme_sign_result_t r)
47 7 : {
48 7 : if (!r) {
49 0 : return;
50 : }
51 14 : for (gpgme_new_signature_t is = r->signatures ; is ; is = is->next) {
52 7 : gpgme_new_signature_t copy = new _gpgme_new_signature(*is);
53 7 : if (is->fpr) {
54 7 : copy->fpr = strdup(is->fpr);
55 : }
56 7 : copy->next = 0;
57 7 : created.push_back(copy);
58 : }
59 7 : for (gpgme_invalid_key_t ik = r->invalid_signers ; ik ; ik = ik->next) {
60 0 : gpgme_invalid_key_t copy = new _gpgme_invalid_key(*ik);
61 0 : if (ik->fpr) {
62 0 : copy->fpr = strdup(ik->fpr);
63 : }
64 0 : copy->next = 0;
65 0 : invalid.push_back(copy);
66 : }
67 : }
68 7 : ~Private()
69 7 : {
70 14 : for (std::vector<gpgme_new_signature_t>::iterator it = created.begin() ; it != created.end() ; ++it) {
71 7 : std::free((*it)->fpr);
72 7 : delete *it; *it = 0;
73 : }
74 7 : for (std::vector<gpgme_invalid_key_t>::iterator it = invalid.begin() ; it != invalid.end() ; ++it) {
75 0 : std::free((*it)->fpr);
76 0 : delete *it; *it = 0;
77 : }
78 7 : }
79 :
80 : std::vector<gpgme_new_signature_t> created;
81 : std::vector<gpgme_invalid_key_t> invalid;
82 : };
83 :
84 0 : GpgME::SigningResult::SigningResult(gpgme_ctx_t ctx, int error)
85 0 : : GpgME::Result(error), d()
86 : {
87 0 : init(ctx);
88 0 : }
89 :
90 7 : GpgME::SigningResult::SigningResult(gpgme_ctx_t ctx, const Error &error)
91 7 : : GpgME::Result(error), d()
92 : {
93 7 : init(ctx);
94 7 : }
95 :
96 7 : void GpgME::SigningResult::init(gpgme_ctx_t ctx)
97 : {
98 7 : if (!ctx) {
99 0 : return;
100 : }
101 7 : gpgme_sign_result_t res = gpgme_op_sign_result(ctx);
102 7 : if (!res) {
103 0 : return;
104 : }
105 7 : d.reset(new Private(res));
106 : }
107 :
108 14 : make_standard_stuff(SigningResult)
109 :
110 0 : GpgME::CreatedSignature GpgME::SigningResult::createdSignature(unsigned int idx) const
111 : {
112 0 : return CreatedSignature(d, idx);
113 : }
114 :
115 0 : std::vector<GpgME::CreatedSignature> GpgME::SigningResult::createdSignatures() const
116 : {
117 0 : if (!d) {
118 0 : return std::vector<CreatedSignature>();
119 : }
120 0 : std::vector<CreatedSignature> result;
121 0 : result.reserve(d->created.size());
122 0 : for (unsigned int i = 0 ; i < d->created.size() ; ++i) {
123 0 : result.push_back(CreatedSignature(d, i));
124 : }
125 0 : return result;
126 : }
127 :
128 0 : GpgME::InvalidSigningKey GpgME::SigningResult::invalidSigningKey(unsigned int idx) const
129 : {
130 0 : return InvalidSigningKey(d, idx);
131 : }
132 :
133 0 : std::vector<GpgME::InvalidSigningKey> GpgME::SigningResult::invalidSigningKeys() const
134 : {
135 0 : if (!d) {
136 0 : return std::vector<GpgME::InvalidSigningKey>();
137 : }
138 0 : std::vector<GpgME::InvalidSigningKey> result;
139 0 : result.reserve(d->invalid.size());
140 0 : for (unsigned int i = 0 ; i < d->invalid.size() ; ++i) {
141 0 : result.push_back(InvalidSigningKey(d, i));
142 : }
143 0 : return result;
144 : }
145 :
146 0 : GpgME::InvalidSigningKey::InvalidSigningKey(const std::shared_ptr<SigningResult::Private> &parent, unsigned int i)
147 0 : : d(parent), idx(i)
148 : {
149 :
150 0 : }
151 :
152 0 : GpgME::InvalidSigningKey::InvalidSigningKey() : d(), idx(0) {}
153 :
154 0 : bool GpgME::InvalidSigningKey::isNull() const
155 : {
156 0 : return !d || idx >= d->invalid.size() ;
157 : }
158 :
159 0 : const char *GpgME::InvalidSigningKey::fingerprint() const
160 : {
161 0 : return isNull() ? 0 : d->invalid[idx]->fpr ;
162 : }
163 :
164 0 : GpgME::Error GpgME::InvalidSigningKey::reason() const
165 : {
166 0 : return Error(isNull() ? 0 : d->invalid[idx]->reason);
167 : }
168 :
169 0 : GpgME::CreatedSignature::CreatedSignature(const std::shared_ptr<SigningResult::Private> &parent, unsigned int i)
170 0 : : d(parent), idx(i)
171 : {
172 :
173 0 : }
174 :
175 0 : GpgME::CreatedSignature::CreatedSignature() : d(), idx(0) {}
176 :
177 0 : bool GpgME::CreatedSignature::isNull() const
178 : {
179 0 : return !d || idx >= d->created.size() ;
180 : }
181 :
182 0 : const char *GpgME::CreatedSignature::fingerprint() const
183 : {
184 0 : return isNull() ? 0 : d->created[idx]->fpr ;
185 : }
186 :
187 0 : time_t GpgME::CreatedSignature::creationTime() const
188 : {
189 0 : return static_cast<time_t>(isNull() ? 0 : d->created[idx]->timestamp);
190 : }
191 :
192 0 : GpgME::SignatureMode GpgME::CreatedSignature::mode() const
193 : {
194 0 : if (isNull()) {
195 0 : return NormalSignatureMode;
196 : }
197 0 : switch (d->created[idx]->type) {
198 : default:
199 0 : case GPGME_SIG_MODE_NORMAL: return NormalSignatureMode;
200 0 : case GPGME_SIG_MODE_DETACH: return Detached;
201 0 : case GPGME_SIG_MODE_CLEAR: return Clearsigned;
202 : }
203 : }
204 :
205 0 : unsigned int GpgME::CreatedSignature::publicKeyAlgorithm() const
206 : {
207 0 : return isNull() ? 0 : d->created[idx]->pubkey_algo ;
208 : }
209 :
210 0 : const char *GpgME::CreatedSignature::publicKeyAlgorithmAsString() const
211 : {
212 0 : return gpgme_pubkey_algo_name(isNull() ? (gpgme_pubkey_algo_t)0 : d->created[idx]->pubkey_algo);
213 : }
214 :
215 0 : unsigned int GpgME::CreatedSignature::hashAlgorithm() const
216 : {
217 0 : return isNull() ? 0 : d->created[idx]->hash_algo ;
218 : }
219 :
220 0 : const char *GpgME::CreatedSignature::hashAlgorithmAsString() const
221 : {
222 0 : return gpgme_hash_algo_name(isNull() ? (gpgme_hash_algo_t)0 : d->created[idx]->hash_algo);
223 : }
224 :
225 0 : unsigned int GpgME::CreatedSignature::signatureClass() const
226 : {
227 0 : return isNull() ? 0 : d->created[idx]->sig_class ;
228 : }
229 :
230 0 : std::ostream &GpgME::operator<<(std::ostream &os, const SigningResult &result)
231 : {
232 0 : os << "GpgME::SigningResult(";
233 0 : if (!result.isNull()) {
234 0 : os << "\n error: " << result.error()
235 0 : << "\n createdSignatures:\n";
236 0 : const std::vector<CreatedSignature> cs = result.createdSignatures();
237 : std::copy(cs.begin(), cs.end(),
238 0 : std::ostream_iterator<CreatedSignature>(os, "\n"));
239 0 : os << " invalidSigningKeys:\n";
240 0 : const std::vector<InvalidSigningKey> isk = result.invalidSigningKeys();
241 : std::copy(isk.begin(), isk.end(),
242 0 : std::ostream_iterator<InvalidSigningKey>(os, "\n"));
243 : }
244 0 : return os << ')';
245 : }
246 :
247 0 : std::ostream &GpgME::operator<<(std::ostream &os, const CreatedSignature &sig)
248 : {
249 0 : os << "GpgME::CreatedSignature(";
250 0 : if (!sig.isNull()) {
251 : os << "\n fingerprint: " << protect(sig.fingerprint())
252 0 : << "\n creationTime: " << sig.creationTime()
253 0 : << "\n mode: " << sig.mode()
254 : << "\n publicKeyAlgorithm: " << protect(sig.publicKeyAlgorithmAsString())
255 : << "\n hashAlgorithm: " << protect(sig.hashAlgorithmAsString())
256 0 : << "\n signatureClass: " << sig.signatureClass()
257 0 : << '\n';
258 : }
259 0 : return os << ')';
260 : }
261 :
262 0 : std::ostream &GpgME::operator<<(std::ostream &os, const InvalidSigningKey &key)
263 : {
264 0 : os << "GpgME::InvalidSigningKey(";
265 0 : if (!key.isNull()) {
266 : os << "\n fingerprint: " << protect(key.fingerprint())
267 0 : << "\n reason: " << key.reason()
268 0 : << '\n';
269 : }
270 0 : return os << ')';
271 24 : }
|