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