libkdenetwork Library API Documentation

verificationresult.cpp

00001 /* verificationresult.cpp - wraps a gpgme verify result
00002    Copyright (C) 2004 Klarälvdalens Datakonsult AB
00003 
00004    This file is part of GPGME++.
00005  
00006    GPGME++ is free software; you can redistribute it and/or modify it
00007    under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010  
00011    GPGME++ is distributed in the hope that it will be useful, but
00012    WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with GPGME++; if not, write to the Free Software Foundation,
00018    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA.
00019 */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include <gpgmepp/verificationresult.h>
00026 #include "shared.h"
00027 #include "result_p.h"
00028 
00029 #include <gpgme.h>
00030 
00031 #include <algorithm>
00032 #include <cstring>
00033 #include <cstdlib>
00034 
00035 class GpgME::VerificationResult::Private : public GpgME::Shared {
00036 public:
00037   Private( const gpgme_verify_result_t r ) : Shared() {
00038     if ( !r )
00039       return;
00040     // copy recursively, using compiler-generated copy ctor.
00041     // We just need to handle the pointers in the structs:
00042     for ( gpgme_signature_t is = r->signatures ; is ; is = is->next ) {
00043       gpgme_signature_t scopy = new _gpgme_signature( *is );
00044       if ( is->fpr )
00045     scopy->fpr = strdup( is->fpr );
00046       scopy->next = 0;
00047       sigs.push_back( scopy );
00048       // copy notations:
00049       nota.push_back( std::vector<Nota>() );
00050       purls.push_back( 0 );
00051       for ( gpgme_sig_notation_t in = is->notations ; in ; in = in->next ) {
00052     if ( !in->name ) {
00053       if ( in->value )
00054         purls.back() = strdup( in->value ); // policy url
00055       continue;
00056     }
00057     Nota n = { 0, 0 };
00058     n.name = strdup( in->name );
00059     if ( in->value )
00060       n.value = strdup( in->value );
00061     nota.back().push_back( n );
00062       }
00063     }
00064   }
00065   ~Private() {
00066     for ( std::vector<gpgme_signature_t>::iterator it = sigs.begin() ; it != sigs.end() ; ++it ) {
00067       std::free( (*it)->fpr );
00068       delete *it; *it = 0;
00069     }
00070     for ( std::vector< std::vector<Nota> >::iterator it = nota.begin() ; it != nota.end() ; ++it )
00071       for ( std::vector<Nota>::iterator jt = it->begin() ; jt != it->end() ; ++jt ) {
00072     std::free( jt->name );  jt->name = 0;
00073     std::free( jt->value ); jt->value = 0;
00074       }
00075     std::for_each( purls.begin(), purls.end(), &std::free );
00076   }
00077 
00078   struct Nota {
00079     char * name;
00080     char * value;
00081   };
00082 
00083   std::vector<gpgme_signature_t> sigs;
00084   std::vector< std::vector<Nota> > nota;
00085   std::vector<char*> purls;
00086 };
00087 
00088 GpgME::VerificationResult::VerificationResult( gpgme_ctx_t ctx, int error )
00089   : GpgME::Result( error ), d( 0 )
00090 {
00091   if ( error || !ctx )
00092     return;
00093   gpgme_verify_result_t res = gpgme_op_verify_result( ctx );
00094   if ( !res )
00095     return;
00096   d = new Private( res );
00097   d->ref();
00098 }
00099 
00100 make_standard_stuff(VerificationResult)
00101 
00102 GpgME::Signature GpgME::VerificationResult::signature( unsigned int idx ) const {
00103   return Signature( d, idx );
00104 }
00105 
00106 std::vector<GpgME::Signature> GpgME::VerificationResult::signatures() const {
00107   if ( !d )
00108     return std::vector<Signature>();
00109   std::vector<Signature> result;
00110   result.reserve( d->sigs.size() );
00111   for ( unsigned int i = 0 ; i < d->sigs.size() ; ++i )
00112     result.push_back( Signature( d, i ) );
00113   return result;
00114 }
00115 
00116 
00117 
00118 
00119 
00120 
00121 GpgME::Signature::Signature( VerificationResult::Private * parent, unsigned int i )
00122   : d( parent ), idx( i )
00123 {
00124   if ( d )
00125     d->ref();
00126 }
00127 
00128 GpgME::Signature::Signature() : d( 0 ), idx( 0 ) {}
00129 
00130 GpgME::Signature::Signature( const Signature & other )
00131   : d( other.d ), idx( other.idx )
00132 {
00133   if ( d )
00134     d->ref();
00135 }
00136 
00137 GpgME::Signature::~Signature() {
00138   if ( d )
00139     d->unref();
00140 }
00141 
00142 const GpgME::Signature & GpgME::Signature::operator=( const Signature & other ) {
00143   if ( this->d != other.d ) {
00144     if ( other.d )
00145       other.d->ref();
00146     if ( this->d )
00147       this->d->unref();
00148     this->d = other.d;
00149   }
00150 
00151   this->idx = other.idx;
00152   return *this;
00153 }
00154 
00155 
00156 bool GpgME::Signature::isNull() const {
00157   return !d || idx >= d->sigs.size() ;
00158 }
00159 
00160 
00161 GpgME::Signature::Summary GpgME::Signature::summary() const {
00162   if ( isNull() )
00163     return None;
00164   gpgme_sigsum_t sigsum = d->sigs[idx]->summary;
00165   unsigned int result = 0;
00166   if ( sigsum & GPGME_SIGSUM_VALID       ) result |= Valid;
00167   if ( sigsum & GPGME_SIGSUM_GREEN       ) result |= Green;
00168   if ( sigsum & GPGME_SIGSUM_RED         ) result |= Red;
00169   if ( sigsum & GPGME_SIGSUM_KEY_REVOKED ) result |= KeyRevoked;
00170   if ( sigsum & GPGME_SIGSUM_KEY_EXPIRED ) result |= KeyExpired;
00171   if ( sigsum & GPGME_SIGSUM_SIG_EXPIRED ) result |= SigExpired;
00172   if ( sigsum & GPGME_SIGSUM_KEY_MISSING ) result |= KeyMissing;
00173   if ( sigsum & GPGME_SIGSUM_CRL_MISSING ) result |= CrlMissing;
00174   if ( sigsum & GPGME_SIGSUM_CRL_TOO_OLD ) result |= CrlTooOld;
00175   if ( sigsum & GPGME_SIGSUM_BAD_POLICY  ) result |= BadPolicy;
00176   if ( sigsum & GPGME_SIGSUM_SYS_ERROR   ) result |= SysError;
00177   return static_cast<Summary>( result );
00178 }
00179 
00180 const char * GpgME::Signature::fingerprint() const {
00181   return isNull() ? 0 : d->sigs[idx]->fpr ;
00182 }
00183 
00184 GpgME::Error GpgME::Signature::status() const {
00185   return isNull() ? 0 : d->sigs[idx]->status ;
00186 }
00187 
00188 time_t GpgME::Signature::creationTime() const {
00189   return static_cast<time_t>( isNull() ? 0 : d->sigs[idx]->timestamp );
00190 }
00191 
00192 time_t GpgME::Signature::expirationTime() const {
00193   return static_cast<time_t>( isNull() ? 0 : d->sigs[idx]->exp_timestamp );
00194 }
00195 
00196 bool GpgME::Signature::neverExpires() const {
00197   return expirationTime() == (time_t)0;
00198 }
00199 
00200 bool GpgME::Signature::wrongKeyUsage() const {
00201   return !isNull() && d->sigs[idx]->wrong_key_usage;
00202 }
00203 
00204 GpgME::Signature::Validity GpgME::Signature::validity() const {
00205   if ( isNull() )
00206     return Unknown;
00207   switch ( d->sigs[idx]->validity ) {
00208   default:
00209   case GPGME_VALIDITY_UNKNOWN:   return Unknown;
00210   case GPGME_VALIDITY_UNDEFINED: return Undefined;
00211   case GPGME_VALIDITY_NEVER:     return Never;
00212   case GPGME_VALIDITY_MARGINAL:  return Marginal;
00213   case GPGME_VALIDITY_FULL:      return Full;
00214   case GPGME_VALIDITY_ULTIMATE:  return Ultimate;
00215   }
00216 }
00217 
00218 
00219 char GpgME::Signature::validityAsString() const {
00220   if ( isNull() )
00221     return '?';
00222   switch ( d->sigs[idx]->validity ) {
00223   default:
00224   case GPGME_VALIDITY_UNKNOWN:   return '?';
00225   case GPGME_VALIDITY_UNDEFINED: return 'q';
00226   case GPGME_VALIDITY_NEVER:     return 'n';
00227   case GPGME_VALIDITY_MARGINAL:  return 'm';
00228   case GPGME_VALIDITY_FULL:      return 'f';
00229   case GPGME_VALIDITY_ULTIMATE:  return 'u';
00230   }
00231 }
00232 
00233 GpgME::Error GpgME::Signature::nonValidityReason() const {
00234   return isNull() ? 0 : d->sigs[idx]->validity_reason ;
00235 }
00236 
00237 
00238 GpgME::Signature::Notation GpgME::Signature::notation( unsigned int nidx ) const {
00239   return Notation( d, idx, nidx );
00240 }
00241 
00242 std::vector<GpgME::Signature::Notation> GpgME::Signature::notations() const {
00243   if ( isNull() )
00244     return std::vector<Notation>();
00245   std::vector<Notation> result;
00246   result.reserve( d->nota[idx].size() );
00247   for ( unsigned int i = 0 ; i < d->nota[idx].size() ; ++i )
00248     result.push_back( Notation( d, idx, i ) );
00249   return result;
00250 }
00251 
00252 
00253 GpgME::Signature::Notation::Notation( VerificationResult::Private * parent, unsigned int sindex, unsigned int nindex )
00254   : d( parent ), sidx( sindex ), nidx( nindex )
00255 {
00256   if ( d )
00257     d->ref();
00258 }
00259 
00260 GpgME::Signature::Notation::Notation()
00261   : d( 0 ), sidx( 0 ), nidx( 0 ) {}
00262 
00263 GpgME::Signature::Notation::Notation( const Notation & other )
00264   : d( other.d ), sidx( other.sidx ), nidx( other.nidx )
00265 {
00266   if ( d )
00267     d->ref();
00268 }
00269 
00270 GpgME::Signature::Notation::~Notation() {
00271   if ( d )
00272     d->unref();
00273 }
00274 
00275 const GpgME::Signature::Notation & GpgME::Signature::Notation::operator=( const Notation & other ) {
00276   if ( this->d != other.d ) {
00277     if ( other.d )
00278       other.d->ref();
00279     if ( this->d )
00280       this->d->ref();
00281     this->d = other.d;
00282   }
00283 
00284   sidx = other.sidx;
00285   nidx = other.nidx;
00286   return *this;
00287 }
00288 
00289 bool GpgME::Signature::Notation::isNull() const {
00290   return !d || sidx >= d->nota.size() || nidx >= d->nota[sidx].size() ;
00291 }
00292 
00293 
00294 const char * GpgME::Signature::Notation::name() const {
00295   return isNull() ? 0 : d->nota[sidx][nidx].name ;
00296 }
00297 
00298 const char * GpgME::Signature::Notation::value() const {
00299   return isNull() ? 0 : d->nota[sidx][nidx].value ;
00300 }
00301 
KDE Logo
This file is part of the documentation for libkdenetwork Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:21:41 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003