2 decryptionresult.cpp - wraps a gpgme keygen result
3 Copyright (C) 2004 Klarälvdalens Datakonsult AB
4 2016 Bundesamt für Sicherheit in der Informationstechnik
5 Software engineering by Intevation GmbH
7 This file is part of GPGME++.
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.
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.
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.
29 #include <decryptionresult.h>
43 class GpgME::DecryptionResult::Private
46 explicit Private(const _gpgme_op_decrypt_result &r) : res(r)
48 if (res.unsupported_algorithm) {
49 res.unsupported_algorithm = strdup(res.unsupported_algorithm);
52 res.file_name = strdup(res.file_name);
54 if (res.symkey_algo) {
55 res.symkey_algo = strdup(res.symkey_algo);
57 //FIXME: copying gpgme_recipient_t objects invalidates the keyid member,
58 //thus we use _keyid for now (internal API)
59 for (gpgme_recipient_t r = res.recipients ; r ; r = r->next) {
60 recipients.push_back(*r);
66 if (res.unsupported_algorithm) {
67 std::free(res.unsupported_algorithm);
69 res.unsupported_algorithm = 0;
71 std::free(res.file_name);
74 if (res.symkey_algo) {
75 std::free(res.symkey_algo);
80 _gpgme_op_decrypt_result res;
81 std::vector<_gpgme_recipient> recipients;
84 GpgME::DecryptionResult::DecryptionResult(gpgme_ctx_t ctx, int error)
85 : GpgME::Result(error), d()
90 GpgME::DecryptionResult::DecryptionResult(gpgme_ctx_t ctx, const Error &error)
91 : GpgME::Result(error), d()
96 void GpgME::DecryptionResult::init(gpgme_ctx_t ctx)
101 gpgme_decrypt_result_t res = gpgme_op_decrypt_result(ctx);
105 d.reset(new Private(*res));
108 make_standard_stuff(DecryptionResult)
110 const char *GpgME::DecryptionResult::unsupportedAlgorithm() const
112 return d ? d->res.unsupported_algorithm : 0 ;
115 bool GpgME::DecryptionResult::isWrongKeyUsage() const
117 return d && d->res.wrong_key_usage;
120 bool GpgME::DecryptionResult::isDeVs() const
122 return d && d->res.is_de_vs;
125 const char *GpgME::DecryptionResult::fileName() const
127 return d ? d->res.file_name : 0 ;
130 unsigned int GpgME::DecryptionResult::numRecipients() const
132 return d ? d->recipients.size() : 0 ;
135 GpgME::DecryptionResult::Recipient GpgME::DecryptionResult::recipient(unsigned int idx) const
137 if (d && idx < d->recipients.size()) {
138 return Recipient(&d->recipients[idx]);
145 struct make_recipient {
146 GpgME::DecryptionResult::Recipient operator()(_gpgme_recipient &t)
148 return GpgME::DecryptionResult::Recipient(&t);
153 std::vector<GpgME::DecryptionResult::Recipient> GpgME::DecryptionResult::recipients() const
155 std::vector<Recipient> result;
157 result.reserve(d->recipients.size());
158 std::transform(d->recipients.begin(), d->recipients.end(),
159 std::back_inserter(result),
165 const char *GpgME::DecryptionResult::sessionKey() const
167 return d ? d->res.session_key : nullptr;
170 const char *GpgME::DecryptionResult::symkeyAlgo() const
172 return d ? d->res.symkey_algo : nullptr;
175 bool GpgME::DecryptionResult::isLegacyCipherNoMDC() const
177 return d && d->res.legacy_cipher_nomdc;
180 class GpgME::DecryptionResult::Recipient::Private : public _gpgme_recipient
183 Private(gpgme_recipient_t reci) : _gpgme_recipient(*reci) {}
186 GpgME::DecryptionResult::Recipient::Recipient()
192 GpgME::DecryptionResult::Recipient::Recipient(gpgme_recipient_t r)
196 d.reset(new Private(r));
200 bool GpgME::DecryptionResult::Recipient::isNull() const
205 const char *GpgME::DecryptionResult::Recipient::keyID() const
207 //_keyid is internal API, but the public keyid is invalid after copying (see above)
214 const char *GpgME::DecryptionResult::Recipient::shortKeyID() const
216 //_keyid is internal API, but the public keyid is invalid after copying (see above)
218 return d->_keyid + 8;
223 unsigned int GpgME::DecryptionResult::Recipient::publicKeyAlgorithm() const
226 return d->pubkey_algo;
231 const char *GpgME::DecryptionResult::Recipient::publicKeyAlgorithmAsString() const
234 return gpgme_pubkey_algo_name(d->pubkey_algo);
239 GpgME::Error GpgME::DecryptionResult::Recipient::status() const
242 return Error(d->status);
247 std::ostream &GpgME::operator<<(std::ostream &os, const DecryptionResult &result)
249 os << "GpgME::DecryptionResult(";
250 if (!result.isNull()) {
251 os << "\n error: " << result.error()
252 << "\n fileName: " << protect(result.fileName())
253 << "\n unsupportedAlgorithm: " << protect(result.unsupportedAlgorithm())
254 << "\n isWrongKeyUsage: " << result.isWrongKeyUsage()
255 << "\n isDeVs " << result.isDeVs()
256 << "\n legacyCipherNoMDC " << result.isLegacyCipherNoMDC()
257 << "\n symkeyAlgo: " << protect(result.symkeyAlgo())
258 << "\n recipients:\n";
259 const std::vector<DecryptionResult::Recipient> recipients = result.recipients();
260 std::copy(recipients.begin(), recipients.end(),
261 std::ostream_iterator<DecryptionResult::Recipient>(os, "\n"));
266 std::ostream &GpgME::operator<<(std::ostream &os, const DecryptionResult::Recipient &reci)
268 os << "GpgME::DecryptionResult::Recipient(";
269 if (!reci.isNull()) {
270 os << "\n keyID: " << protect(reci.keyID())
271 << "\n shortKeyID: " << protect(reci.shortKeyID())
272 << "\n publicKeyAlgorithm: " << protect(reci.publicKeyAlgorithmAsString())
273 << "\n status: " << reci.status();