LCOV - code coverage report
Current view: top level - lang/cpp/src - signingresult.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 128 22.7 %
Date: 2016-12-01 18:45:36 Functions: 7 33 21.2 %

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

Generated by: LCOV version 1.11