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:
Private(const gpgme_sign_result_t r)46 Private(const gpgme_sign_result_t r)
47 {
48 if (!r) {
49 return;
50 }
51 for (gpgme_new_signature_t is = r->signatures ; is ; is = is->next) {
52 gpgme_new_signature_t copy = new _gpgme_new_signature(*is);
53 if (is->fpr) {
54 copy->fpr = strdup(is->fpr);
55 }
56 copy->next = nullptr;
57 created.push_back(copy);
58 }
59 for (gpgme_invalid_key_t ik = r->invalid_signers ; ik ; ik = ik->next) {
60 gpgme_invalid_key_t copy = new _gpgme_invalid_key(*ik);
61 if (ik->fpr) {
62 copy->fpr = strdup(ik->fpr);
63 }
64 copy->next = nullptr;
65 invalid.push_back(copy);
66 }
67 }
~Private()68 ~Private()
69 {
70 for (std::vector<gpgme_new_signature_t>::iterator it = created.begin() ; it != created.end() ; ++it) {
71 std::free((*it)->fpr);
72 delete *it; *it = nullptr;
73 }
74 for (std::vector<gpgme_invalid_key_t>::iterator it = invalid.begin() ; it != invalid.end() ; ++it) {
75 std::free((*it)->fpr);
76 delete *it; *it = nullptr;
77 }
78 }
79
80 std::vector<gpgme_new_signature_t> created;
81 std::vector<gpgme_invalid_key_t> invalid;
82 };
83
SigningResult(gpgme_ctx_t ctx,int error)84 GpgME::SigningResult::SigningResult(gpgme_ctx_t ctx, int error)
85 : GpgME::Result(error), d()
86 {
87 init(ctx);
88 }
89
SigningResult(gpgme_ctx_t ctx,const Error & error)90 GpgME::SigningResult::SigningResult(gpgme_ctx_t ctx, const Error &error)
91 : GpgME::Result(error), d()
92 {
93 init(ctx);
94 }
95
init(gpgme_ctx_t ctx)96 void GpgME::SigningResult::init(gpgme_ctx_t ctx)
97 {
98 if (!ctx) {
99 return;
100 }
101 gpgme_sign_result_t res = gpgme_op_sign_result(ctx);
102 if (!res) {
103 return;
104 }
105 d.reset(new Private(res));
106 }
107
make_standard_stuff(SigningResult)108 make_standard_stuff(SigningResult)
109
110 GpgME::CreatedSignature GpgME::SigningResult::createdSignature(unsigned int idx) const
111 {
112 return CreatedSignature(d, idx);
113 }
114
createdSignatures() const115 std::vector<GpgME::CreatedSignature> GpgME::SigningResult::createdSignatures() const
116 {
117 if (!d) {
118 return std::vector<CreatedSignature>();
119 }
120 std::vector<CreatedSignature> result;
121 result.reserve(d->created.size());
122 for (unsigned int i = 0 ; i < d->created.size() ; ++i) {
123 result.push_back(CreatedSignature(d, i));
124 }
125 return result;
126 }
127
invalidSigningKey(unsigned int idx) const128 GpgME::InvalidSigningKey GpgME::SigningResult::invalidSigningKey(unsigned int idx) const
129 {
130 return InvalidSigningKey(d, idx);
131 }
132
invalidSigningKeys() const133 std::vector<GpgME::InvalidSigningKey> GpgME::SigningResult::invalidSigningKeys() const
134 {
135 if (!d) {
136 return std::vector<GpgME::InvalidSigningKey>();
137 }
138 std::vector<GpgME::InvalidSigningKey> result;
139 result.reserve(d->invalid.size());
140 for (unsigned int i = 0 ; i < d->invalid.size() ; ++i) {
141 result.push_back(InvalidSigningKey(d, i));
142 }
143 return result;
144 }
145
InvalidSigningKey(const std::shared_ptr<SigningResult::Private> & parent,unsigned int i)146 GpgME::InvalidSigningKey::InvalidSigningKey(const std::shared_ptr<SigningResult::Private> &parent, unsigned int i)
147 : d(parent), idx(i)
148 {
149
150 }
151
InvalidSigningKey()152 GpgME::InvalidSigningKey::InvalidSigningKey() : d(), idx(0) {}
153
isNull() const154 bool GpgME::InvalidSigningKey::isNull() const
155 {
156 return !d || idx >= d->invalid.size() ;
157 }
158
fingerprint() const159 const char *GpgME::InvalidSigningKey::fingerprint() const
160 {
161 return isNull() ? nullptr : d->invalid[idx]->fpr ;
162 }
163
reason() const164 GpgME::Error GpgME::InvalidSigningKey::reason() const
165 {
166 return Error(isNull() ? 0 : d->invalid[idx]->reason);
167 }
168
CreatedSignature(const std::shared_ptr<SigningResult::Private> & parent,unsigned int i)169 GpgME::CreatedSignature::CreatedSignature(const std::shared_ptr<SigningResult::Private> &parent, unsigned int i)
170 : d(parent), idx(i)
171 {
172
173 }
174
CreatedSignature()175 GpgME::CreatedSignature::CreatedSignature() : d(), idx(0) {}
176
isNull() const177 bool GpgME::CreatedSignature::isNull() const
178 {
179 return !d || idx >= d->created.size() ;
180 }
181
fingerprint() const182 const char *GpgME::CreatedSignature::fingerprint() const
183 {
184 return isNull() ? nullptr : d->created[idx]->fpr ;
185 }
186
creationTime() const187 time_t GpgME::CreatedSignature::creationTime() const
188 {
189 return static_cast<time_t>(isNull() ? 0 : d->created[idx]->timestamp);
190 }
191
mode() const192 GpgME::SignatureMode GpgME::CreatedSignature::mode() const
193 {
194 if (isNull()) {
195 return NormalSignatureMode;
196 }
197 switch (d->created[idx]->type) {
198 default:
199 case GPGME_SIG_MODE_NORMAL: return NormalSignatureMode;
200 case GPGME_SIG_MODE_DETACH: return Detached;
201 case GPGME_SIG_MODE_CLEAR: return Clearsigned;
202 }
203 }
204
publicKeyAlgorithm() const205 unsigned int GpgME::CreatedSignature::publicKeyAlgorithm() const
206 {
207 return isNull() ? 0 : d->created[idx]->pubkey_algo ;
208 }
209
publicKeyAlgorithmAsString() const210 const char *GpgME::CreatedSignature::publicKeyAlgorithmAsString() const
211 {
212 return gpgme_pubkey_algo_name(isNull() ? (gpgme_pubkey_algo_t)0 : d->created[idx]->pubkey_algo);
213 }
214
hashAlgorithm() const215 unsigned int GpgME::CreatedSignature::hashAlgorithm() const
216 {
217 return isNull() ? 0 : d->created[idx]->hash_algo ;
218 }
219
hashAlgorithmAsString() const220 const char *GpgME::CreatedSignature::hashAlgorithmAsString() const
221 {
222 return gpgme_hash_algo_name(isNull() ? (gpgme_hash_algo_t)0 : d->created[idx]->hash_algo);
223 }
224
signatureClass() const225 unsigned int GpgME::CreatedSignature::signatureClass() const
226 {
227 return isNull() ? 0 : d->created[idx]->sig_class ;
228 }
229
operator <<(std::ostream & os,const SigningResult & result)230 std::ostream &GpgME::operator<<(std::ostream &os, const SigningResult &result)
231 {
232 os << "GpgME::SigningResult(";
233 if (!result.isNull()) {
234 os << "\n error: " << result.error()
235 << "\n createdSignatures:\n";
236 const std::vector<CreatedSignature> cs = result.createdSignatures();
237 std::copy(cs.begin(), cs.end(),
238 std::ostream_iterator<CreatedSignature>(os, "\n"));
239 os << " invalidSigningKeys:\n";
240 const std::vector<InvalidSigningKey> isk = result.invalidSigningKeys();
241 std::copy(isk.begin(), isk.end(),
242 std::ostream_iterator<InvalidSigningKey>(os, "\n"));
243 }
244 return os << ')';
245 }
246
operator <<(std::ostream & os,const CreatedSignature & sig)247 std::ostream &GpgME::operator<<(std::ostream &os, const CreatedSignature &sig)
248 {
249 os << "GpgME::CreatedSignature(";
250 if (!sig.isNull()) {
251 os << "\n fingerprint: " << protect(sig.fingerprint())
252 << "\n creationTime: " << sig.creationTime()
253 << "\n mode: " << sig.mode()
254 << "\n publicKeyAlgorithm: " << protect(sig.publicKeyAlgorithmAsString())
255 << "\n hashAlgorithm: " << protect(sig.hashAlgorithmAsString())
256 << "\n signatureClass: " << sig.signatureClass()
257 << '\n';
258 }
259 return os << ')';
260 }
261
operator <<(std::ostream & os,const InvalidSigningKey & key)262 std::ostream &GpgME::operator<<(std::ostream &os, const InvalidSigningKey &key)
263 {
264 os << "GpgME::InvalidSigningKey(";
265 if (!key.isNull()) {
266 os << "\n fingerprint: " << protect(key.fingerprint())
267 << "\n reason: " << key.reason()
268 << '\n';
269 }
270 return os << ')';
271 }
272