libkdenetwork Library API Documentation

key.cpp

00001 /* key.cpp - wraps a gpgme key
00002    Copyright (C) 2003 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 #ifdef HAVE_CONFIG_H
00021 #include <config.h>
00022 #endif
00023 
00024 #include <gpgmepp/key.h>
00025 
00026 #include "util.h"
00027 
00028 #include <gpgme.h>
00029 
00030 #include <string.h>
00031 
00032 GpgME::Key GpgME::Key::null;
00033 
00034 namespace GpgME {
00035 
00036   using std::vector;
00037 
00038   struct Key::Private {
00039     Private( gpgme_key_t aKey, unsigned int aMode )
00040       : key( aKey ),
00041 #ifdef HAVE_GPGME_KEY_T_KEYLIST_MODE
00042     mode( 0 )
00043 #else
00044     mode( aMode )
00045 #endif
00046     {}
00047     gpgme_key_t key;
00048     unsigned int mode;
00049   };
00050 
00051   Key::Key() {
00052     d = new Private( 0, 0 );
00053   }
00054 
00055   Key::Key( gpgme_key_t key, bool ref, unsigned int mode ) {
00056     d = new Private( key, mode );
00057     if ( ref && d->key )
00058       gpgme_key_ref( d->key );
00059   }
00060 
00061   Key::Key( const Key & other ) {
00062     d = new Private( other.d->key, other.d->mode );
00063     if ( d->key )
00064       gpgme_key_ref( d->key );
00065   }
00066 
00067   Key::~Key() {
00068     if ( d->key )
00069       gpgme_key_unref( d->key );
00070     delete d; d = 0;
00071   }
00072 
00073   const Key & Key::operator=( const Key & other ) {
00074     if ( d == other.d ) return *this;
00075 
00076     if ( other.d->key )
00077       gpgme_key_ref( other.d->key );
00078     if ( d->key )
00079       gpgme_key_unref( d->key );
00080     *d = *other.d;
00081     return *this;
00082   }
00083 
00084   bool Key::isNull() const {
00085     return d->key == 0;
00086   }
00087 
00088   gpgme_key_t Key::impl() const {
00089     return d->key;
00090   }
00091 
00092 
00093 
00094   UserID Key::userID( unsigned int index ) const {
00095     return UserID( d->key, index );
00096   }
00097 
00098   Subkey Key::subkey( unsigned int index ) const {
00099     return Subkey( d->key, index );
00100   }
00101 
00102 
00103   unsigned int Key::numUserIDs() const {
00104     if ( !d->key )
00105       return 0;
00106     unsigned int count = 0;
00107     for ( gpgme_user_id_t uid = d->key->uids ; uid ; uid = uid->next )
00108       ++count;
00109     return count;
00110   }
00111 
00112   unsigned int Key::numSubkeys() const {
00113     if ( !d->key )
00114       return 0;
00115     unsigned int count = 0;
00116     for ( gpgme_sub_key_t subkey = d->key->subkeys ; subkey ; subkey = subkey->next )
00117       ++count;
00118     return count;
00119   }
00120 
00121   vector<UserID> Key::userIDs() const {
00122     if ( !d->key )
00123       return vector<UserID>();
00124 
00125     vector<UserID> v;
00126     v.reserve( numUserIDs() );
00127     for ( gpgme_user_id_t uid = d->key->uids ; uid ; uid = uid->next )
00128       v.push_back( UserID( d->key, uid ) );
00129     return v;
00130   }
00131 
00132   vector<Subkey> Key::subkeys() const {
00133     if ( !d->key )
00134       return vector<Subkey>();
00135 
00136     vector<Subkey> v;
00137     v.reserve( numSubkeys() );
00138     for ( gpgme_sub_key_t subkey = d->key->subkeys ; subkey ; subkey = subkey->next )
00139       v.push_back( Subkey( d->key, subkey ) );
00140     return v;
00141   }
00142 
00143   Key::OwnerTrust Key::ownerTrust() const {
00144     if ( !d->key )
00145       return Unknown;
00146     switch ( d->key->owner_trust ) {
00147     default:
00148     case GPGME_VALIDITY_UNKNOWN:   return Unknown;
00149     case GPGME_VALIDITY_UNDEFINED: return Undefined;
00150     case GPGME_VALIDITY_NEVER:     return Never;
00151     case GPGME_VALIDITY_MARGINAL:  return Marginal;
00152     case GPGME_VALIDITY_FULL:     return Full;
00153     case GPGME_VALIDITY_ULTIMATE: return Ultimate;
00154     }
00155   }
00156   char Key::ownerTrustAsString() const {
00157     if ( !d->key )
00158       return '?';
00159     switch ( d->key->owner_trust ) {
00160     default:
00161     case GPGME_VALIDITY_UNKNOWN:   return '?';
00162     case GPGME_VALIDITY_UNDEFINED: return 'q';
00163     case GPGME_VALIDITY_NEVER:     return 'n';
00164     case GPGME_VALIDITY_MARGINAL:  return 'm';
00165     case GPGME_VALIDITY_FULL:     return 'f';
00166     case GPGME_VALIDITY_ULTIMATE: return 'u';
00167     }
00168   }
00169 
00170   Context::Protocol Key::protocol() const {
00171     if ( !d->key )
00172       return Context::Unknown;
00173     switch ( d->key->protocol ) {
00174     case GPGME_PROTOCOL_CMS:     return Context::CMS;
00175     case GPGME_PROTOCOL_OpenPGP: return Context::OpenPGP;
00176     default:                     return Context::Unknown;
00177     }
00178   }
00179 
00180   const char * Key::protocolAsString() const {
00181     return d->key ? gpgme_get_protocol_name( d->key->protocol ) : 0 ;
00182   }
00183 
00184   bool Key::isRevoked() const {
00185     return d->key && d->key->revoked;
00186   }
00187 
00188   bool Key::isExpired() const {
00189     return d->key && d->key->expired;
00190   }
00191 
00192   bool Key::isDisabled() const {
00193     return d->key && d->key->disabled;
00194   }
00195 
00196   bool Key::isInvalid() const {
00197     return d->key && d->key->invalid;
00198   }
00199 
00200   bool Key::hasSecret() const {
00201     return d->key && d->key->secret;
00202   }
00203 
00204   bool Key::isRoot() const {
00205     return d->key && d->key->subkeys && d->key->subkeys->fpr && d->key->chain_id &&
00206       strcasecmp( d->key->subkeys->fpr, d->key->chain_id ) == 0;
00207   }
00208 
00209   bool Key::canEncrypt() const {
00210     return d->key && d->key->can_encrypt;
00211   }
00212 
00213   bool Key::canSign() const {
00214 #ifndef GPGME_CAN_SIGN_ON_SECRET_OPENPGP_KEYLISTING_NOT_BROKEN
00215     if ( d->key && d->key->protocol == GPGME_PROTOCOL_OpenPGP )
00216       return true;
00217 #endif
00218     return d->key && d->key->can_sign;
00219   }
00220 
00221   bool Key::canCertify() const {
00222     return d->key && d->key->can_certify;
00223   }
00224 
00225   bool Key::canAuthenticate() const {
00226     return d->key && d->key->can_authenticate;
00227   }
00228 
00229   const char * Key::issuerSerial() const {
00230     return d->key ? d->key->issuer_serial : 0 ;
00231   }
00232   const char * Key::issuerName() const {
00233     return d->key ? d->key->issuer_name : 0 ;
00234   }
00235   const char * Key::chainID() const {
00236     return d->key ? d->key->chain_id : 0 ;
00237   }
00238 
00239   const char * Key::keyID() const {
00240 #ifdef HAVE_GPGME_KEY_T_KEYID
00241     return d->key ? d->key->keyid : 0 ;
00242 #else
00243     if ( !d->key || !d->key->subkeys || !d->key->subkeys->fpr )
00244       return 0;
00245     const int len = strlen( d->key->subkeys->fpr );
00246     if ( len < 16 )
00247       return 0;
00248     return d->key->subkeys->fpr + len - 16; // return the last 8 bytes (in hex notation)
00249 #endif
00250   }
00251 
00252   const char * Key::shortKeyID() const {
00253     if ( const char * keyid = keyID() )
00254       return keyid + 8 ;
00255     else
00256       return 0;
00257   }
00258 
00259   const char * Key::primaryFingerprint() const {
00260 #ifdef HAVE_GPGME_KEY_T_FPR
00261     return d->key ? d->key->fpr : 0 ;
00262 #else
00263     return d->key && d->key->subkeys ? d->key->subkeys->fpr : 0 ;
00264 #endif
00265   }
00266 
00267   unsigned int Key::keyListMode() const {
00268 #ifdef HAVE_GPGME_KEY_T_KEYLIST_MODE
00269     return d->key ? convert_from_gpgme_keylist_mode_t( d->key->keylist_mode ) : 0 ;
00270 #else
00271     return d ? d->mode : 0 ;
00272 #endif
00273   }
00274 
00275   //
00276   //
00277   // class Subkey
00278   //
00279   //
00280 
00281   struct Subkey::Private {
00282     Private( gpgme_key_t aKey, unsigned int idx )
00283       : key( aKey ), subkey( 0 )
00284     {
00285       if ( key )
00286     for ( gpgme_sub_key_t s = key->subkeys ; s ; s = s->next, --idx )
00287       if ( idx == 0 ) {
00288         subkey = s;
00289         break;
00290       }
00291       if ( !subkey )
00292     key = 0;
00293     }
00294 
00295     Private( gpgme_key_t aKey, gpgme_sub_key_t aSubkey )
00296       : key( aKey ), subkey( 0 )
00297     {
00298       if ( key )
00299     for ( gpgme_sub_key_t s = key->subkeys ; s ; s = s->next )
00300       if ( s == aSubkey ) { // verify this subkey really belongs to this key
00301         subkey = aSubkey;
00302         break;
00303       }
00304       if ( !subkey )
00305     key = 0;
00306     }
00307 
00308     gpgme_key_t key;
00309     gpgme_sub_key_t subkey;
00310   };
00311 
00312   Subkey::Subkey( gpgme_key_t key, unsigned int idx ) {
00313     d = new Private( key, idx );
00314     if ( d->key )
00315       gpgme_key_ref( d->key );
00316   }
00317 
00318   Subkey::Subkey( gpgme_key_t key, gpgme_sub_key_t subkey ) {
00319     d = new Private( key, subkey );
00320     if ( d->key )
00321       gpgme_key_ref( d->key );
00322   }
00323 
00324   Subkey::Subkey( const Subkey & other ) {
00325     d = new Private( other.d->key, other.d->subkey );
00326     if ( d->key )
00327       gpgme_key_ref( d->key );
00328   }
00329 
00330   Subkey::~Subkey() {
00331     if ( d->key )
00332       gpgme_key_unref( d->key );
00333     delete d; d = 0;
00334   }
00335 
00336   const Subkey & Subkey::operator=( const Subkey & other ) {
00337     if ( &other == this ) return *this;
00338 
00339     if ( other.d->key )
00340       gpgme_key_ref( other.d->key );
00341     if ( d->key )
00342       gpgme_key_unref( d->key );
00343     *d = *other.d;
00344     return *this;
00345   }
00346 
00347   bool Subkey::isNull() const {
00348     return !d || !d->key || !d->subkey;
00349   }
00350 
00351   Key Subkey::parent() const {
00352     return Key( d->key, true );
00353   }
00354 
00355   const char * Subkey::keyID() const {
00356     return d->subkey ? d->subkey->keyid : 0 ;
00357   }
00358 
00359   const char * Subkey::fingerprint() const {
00360     return d->subkey ? d->subkey->fpr : 0 ;
00361   }
00362 
00363   unsigned int Subkey::publicKeyAlgorithm() const {
00364     return d->subkey ? d->subkey->pubkey_algo : 0 ;
00365   }
00366 
00367   const char * Subkey::publicKeyAlgorithmAsString() const {
00368     return gpgme_pubkey_algo_name( d->subkey ? d->subkey->pubkey_algo : (gpgme_pubkey_algo_t)0 );
00369   }
00370 
00371   bool Subkey::canEncrypt() const {
00372     return d->subkey && d->subkey->can_encrypt;
00373   }
00374 
00375   bool Subkey::canSign() const {
00376     return d->subkey && d->subkey->can_sign;
00377   }
00378 
00379   bool Subkey::canCertify() const {
00380     return d->subkey && d->subkey->can_certify;
00381   }
00382   
00383   bool Subkey::canAuthenticate() const {
00384     return d->subkey && d->subkey->can_authenticate;
00385   }
00386 
00387   unsigned int Subkey::length() const {
00388     return d->subkey ? d->subkey->length : 0 ;
00389   }
00390 
00391   time_t Subkey::creationTime() const {
00392     return static_cast<time_t>( d->subkey ? d->subkey->timestamp : 0 );
00393   }
00394 
00395   time_t Subkey::expirationTime() const {
00396     return static_cast<time_t>( d->subkey ? d->subkey->expires : 0 );
00397   }
00398 
00399   bool Subkey::neverExpires() const {
00400     return expirationTime() == time_t(0);
00401   }
00402 
00403   bool Subkey::isRevoked() const {
00404     return d->subkey && d->subkey->revoked;
00405   }
00406 
00407   bool Subkey::isInvalid() const {
00408     return d->subkey && d->subkey->invalid;
00409   }
00410 
00411   bool Subkey::isExpired() const {
00412     return d->subkey && d->subkey->expired;
00413   }
00414 
00415   bool Subkey::isDisabled() const {
00416     return d->subkey && d->subkey->disabled;
00417   }
00418 
00419   //
00420   //
00421   // class UserID
00422   //
00423   //
00424 
00425   struct UserID::Private {
00426     Private( gpgme_key_t aKey, unsigned int idx )
00427       : key( aKey ), uid( 0 )
00428     {
00429       if ( key )
00430     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next, --idx )
00431       if ( idx == 0 ) {
00432         uid = u;
00433         break;
00434       }
00435       if ( !uid )
00436     key = 0;
00437     }
00438 
00439     Private( gpgme_key_t aKey, gpgme_user_id_t aUid )
00440       : key( aKey ), uid( 0 )
00441     {
00442       if ( key )
00443     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next )
00444       if ( u == aUid ) {
00445         uid = u;
00446         break;
00447       }
00448       if ( !uid )
00449     key = 0;
00450     }
00451 
00452     gpgme_key_t key;
00453     gpgme_user_id_t uid;
00454   };
00455 
00456   UserID::UserID( gpgme_key_t key, gpgme_user_id_t uid ) {
00457     d = new Private( key, uid );
00458     if ( d->key )
00459       gpgme_key_ref( d->key );
00460   }
00461 
00462   UserID::UserID( gpgme_key_t key, unsigned int idx ) {
00463     d = new Private( key, idx );
00464     if ( d->key )
00465       gpgme_key_ref( d->key );
00466   }
00467 
00468   UserID::UserID( const UserID & other ) {
00469     d = new Private( other.d->key, other.d->uid );
00470     if ( d->key )
00471       gpgme_key_ref( d->key );
00472   }
00473 
00474   UserID::~UserID() {
00475     if ( d->key )
00476       gpgme_key_unref( d->key );
00477     delete d; d = 0;
00478   }
00479 
00480   const UserID & UserID::operator=( const UserID & other ) {
00481     if ( &other == this ) return *this;
00482 
00483     if ( other.d->key )
00484       gpgme_key_ref( other.d->key );
00485     if ( d->key )
00486       gpgme_key_unref( d->key );
00487     *d = *other.d;
00488     return *this;
00489   }
00490 
00491   bool UserID::isNull() const {
00492     return !d || !d->key || !d->uid;
00493   }
00494 
00495   Key UserID::parent() const {
00496     return Key( d->key, true );
00497   }
00498 
00499   UserID::Signature UserID::signature( unsigned int index ) const {
00500     return Signature( d->key, d->uid, index );
00501   }
00502 
00503   unsigned int UserID::numSignatures() const {
00504     if ( !d->uid )
00505       return 0;
00506     unsigned int count = 0;
00507     for ( gpgme_key_sig_t sig = d->uid->signatures ; sig ; sig = sig->next )
00508       ++count;
00509     return count;
00510   }
00511 
00512   vector<UserID::Signature> UserID::signatures() const {
00513     if ( !d->uid )
00514       return vector<Signature>();
00515 
00516     vector<Signature> v;
00517     v.reserve( numSignatures() );
00518     for ( gpgme_key_sig_t sig = d->uid->signatures ; sig ; sig = sig->next )
00519       v.push_back( Signature( d->key, d->uid, sig ) );
00520     return v;
00521   }
00522 
00523   const char * UserID::id() const {
00524     return d->uid ? d->uid->uid : 0 ;
00525   }
00526 
00527   const char * UserID::name() const {
00528     return d->uid ? d->uid->name : 0 ;
00529   }
00530 
00531   const char * UserID::email() const {
00532     return d->uid ? d->uid->email : 0 ;
00533   }
00534 
00535   const char * UserID::comment() const {
00536     return d->uid ? d->uid->comment : 0 ;
00537   }
00538 
00539   UserID::Validity UserID::validity() const {
00540     if ( !d->uid )
00541       return Unknown;
00542     switch ( d->uid->validity ) {
00543     default:
00544     case GPGME_VALIDITY_UNKNOWN:   return Unknown;
00545     case GPGME_VALIDITY_UNDEFINED: return Undefined;
00546     case GPGME_VALIDITY_NEVER:     return Never;
00547     case GPGME_VALIDITY_MARGINAL:  return Marginal;
00548     case GPGME_VALIDITY_FULL:      return Full;
00549     case GPGME_VALIDITY_ULTIMATE:  return Ultimate;
00550     }
00551   }
00552 
00553   char UserID::validityAsString() const {
00554     if ( !d->uid )
00555       return '?';
00556     switch ( d->uid->validity ) {
00557     default:
00558     case GPGME_VALIDITY_UNKNOWN:   return '?';
00559     case GPGME_VALIDITY_UNDEFINED: return 'q';
00560     case GPGME_VALIDITY_NEVER:     return 'n';
00561     case GPGME_VALIDITY_MARGINAL:  return 'm';
00562     case GPGME_VALIDITY_FULL:      return 'f';
00563     case GPGME_VALIDITY_ULTIMATE:  return 'u';
00564     }
00565   }
00566 
00567   bool UserID::isRevoked() const {
00568     return d->uid && d->uid->revoked;
00569   }
00570 
00571   bool UserID::isInvalid() const {
00572     return d->uid && d->uid->invalid;
00573   }
00574 
00575   //
00576   //
00577   // class Signature
00578   //
00579   //
00580 
00581   struct UserID::Signature::Private {
00582     Private( gpgme_key_t aKey, gpgme_user_id_t aUid, unsigned int idx )
00583       : key( aKey ), uid( 0 ), sig( 0 )
00584     {
00585       if ( key )
00586     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next )
00587       if ( u == aUid ) {
00588         uid = u;
00589         for ( gpgme_key_sig_t s = uid->signatures ; s ; s = s->next, --idx )
00590           if ( idx == 0 ) {
00591         sig = s;
00592         break;
00593           }
00594         break;
00595       }
00596       if ( !uid || !sig ) {
00597     uid = 0;
00598     sig = 0;
00599     key = 0;
00600       }
00601     }
00602       
00603     Private( gpgme_key_t aKey, gpgme_user_id_t aUid, gpgme_key_sig_t aSig )
00604       : key( aKey ), uid( 0 ), sig( 0 )
00605     {
00606       if ( key )
00607     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next )
00608       if ( u == aUid ) {
00609         uid = u;
00610         for ( gpgme_key_sig_t s = uid->signatures ; s ; s = s->next )
00611           if ( s == aSig ) {
00612         sig = s;
00613         break;
00614           }
00615         break;
00616       }
00617       if ( !uid || !sig ) {
00618     uid = 0;
00619     sig = 0;
00620     key = 0;
00621       }
00622     }
00623       
00624     gpgme_key_t key;
00625     gpgme_user_id_t uid;
00626     gpgme_key_sig_t sig;
00627   };
00628 
00629   UserID::Signature::Signature( gpgme_key_t key, gpgme_user_id_t uid, unsigned int idx ) {
00630     d = new Private( key, uid, idx );
00631     if ( d->key )
00632       gpgme_key_ref( d->key );
00633   }
00634 
00635   UserID::Signature::Signature( gpgme_key_t key, gpgme_user_id_t uid, gpgme_key_sig_t sig ) {
00636     d = new Private( key, uid, sig );
00637     if ( d->key )
00638       gpgme_key_ref( d->key );
00639   }
00640 
00641   UserID::Signature::Signature( const Signature & other ) {
00642     d = new Private( other.d->key, other.d->uid, other.d->sig );
00643     if ( d->key )
00644       gpgme_key_ref( d->key );
00645   }
00646 
00647   UserID::Signature::~Signature() {
00648     if ( d->key )
00649       gpgme_key_unref( d->key );
00650     delete d; d = 0;
00651   }
00652 
00653   const UserID::Signature & UserID::Signature::operator=( const Signature & other ) {
00654     if ( &other == this ) return *this;
00655 
00656     if ( other.d->key )
00657       gpgme_key_ref( other.d->key );
00658     if ( d->key )
00659       gpgme_key_unref( d->key );
00660     *d = *other.d;
00661     return *this;
00662   }
00663 
00664   bool UserID::Signature::isNull() const {
00665     return !d || !d->key || !d->uid || !d->sig;
00666   }
00667 
00668   UserID UserID::Signature::parent() const {
00669     return UserID( d->key, d->uid );
00670   }
00671 
00672   const char * UserID::Signature::signerKeyID() const {
00673     return d->sig ? d->sig->keyid : 0 ;
00674   }
00675 
00676   const char * UserID::Signature::algorithmAsString() const {
00677     return gpgme_pubkey_algo_name( d->sig ? d->sig->pubkey_algo : (gpgme_pubkey_algo_t)0 );
00678   }
00679 
00680   unsigned int UserID::Signature::algorithm() const {
00681     return d->sig ? d->sig->pubkey_algo : 0 ;
00682   }
00683 
00684   time_t UserID::Signature::creationTime() const {
00685     return static_cast<time_t>( d->sig ? d->sig->timestamp : 0 );
00686   }
00687 
00688   time_t UserID::Signature::expirationTime() const {
00689     return static_cast<time_t>( d->sig ? d->sig->expires : 0 );
00690   }
00691 
00692   bool UserID::Signature::neverExpires() const {
00693     return expirationTime() == time_t(0);
00694   }
00695 
00696   bool UserID::Signature::isRevokation() const {
00697     return d->sig && d->sig->revoked;
00698   }
00699 
00700   bool UserID::Signature::isInvalid() const {
00701     return d->sig && d->sig->invalid;
00702   }
00703 
00704   bool UserID::Signature::isExpired() const {
00705     return d->sig && d->sig->expired;
00706   }
00707 
00708   bool UserID::Signature::isExportable() const {
00709     return d->sig && d->sig->exportable;
00710   }
00711 
00712   const char * UserID::Signature::signerUserID() const {
00713     return d->sig ? d->sig->uid : 0 ;
00714   }
00715 
00716   const char * UserID::Signature::signerName() const {
00717     return d->sig ? d->sig->name : 0 ;
00718   }
00719 
00720   const char * UserID::Signature::signerEmail() const {
00721     return d->sig ? d->sig->email : 0 ;
00722   }
00723 
00724   const char * UserID::Signature::signerComment() const {
00725     return d->sig ? d->sig->comment : 0 ;
00726   }
00727 
00728   unsigned int UserID::Signature::certClass() const {
00729     return d->sig ? d->sig->sig_class : 0 ;
00730   }
00731 
00732   UserID::Signature::Status UserID::Signature::status() const {
00733     if ( !d->sig )
00734       return GeneralError;
00735     switch ( d->sig->status ) {
00736     case GPG_ERR_NO_ERROR:      return NoError;
00737     case GPG_ERR_SIG_EXPIRED:   return SigExpired;
00738     case GPG_ERR_KEY_EXPIRED:   return KeyExpired;
00739     case GPG_ERR_BAD_SIGNATURE: return BadSignature;
00740     case GPG_ERR_NO_PUBKEY:     return NoPublicKey;
00741     default:
00742     case GPG_ERR_GENERAL:       return GeneralError;
00743     }
00744   }
00745 
00746   const char * UserID::Signature::statusAsString() const {
00747     return d->sig ? gpgme_strerror( d->sig->status ) : 0 ;
00748   }
00749 
00750   UserID::Signature::Notation UserID::Signature::notation( unsigned int idx ) const {
00751     return Notation( d->key, d->uid, d->sig, idx );
00752   }
00753 
00754   unsigned int UserID::Signature::numNotations() const {
00755     if ( !d->sig )
00756       return 0;
00757     unsigned int count = 0;
00758 #ifdef HAVE_GPGME_KEY_SIG_NOTATIONS
00759     for ( gpgme_sig_notation_t nota = d->sig->notations ; nota ; nota = nota->next )
00760       if ( nota->name ) ++count; // others are policy URLs...
00761 #endif
00762     return count;
00763   }
00764       
00765   vector<UserID::Signature::Notation> UserID::Signature::notations() const {
00766     if ( !d->sig )
00767       return vector<Notation>();
00768     vector<Notation> v;
00769 #ifdef HAVE_GPGME_KEY_SIG_NOTATIONS
00770     v.reserve( numNotations() );
00771     for ( gpgme_sig_notation_t nota = d->sig->notations ; nota ; nota = nota->next )
00772       if ( nota->name )
00773     v.push_back( Notation( d->key, d->uid, d->sig, nota ) );
00774 #endif
00775     return v;
00776   }
00777 
00778   const char * UserID::Signature::policyURL() const {
00779 #ifdef HAVE_GPGME_KEY_SIG_NOTATIONS
00780     if ( !d->sig )
00781       return 0;
00782     for ( gpgme_sig_notation_t nota = d->sig->notations ; nota ; nota = nota->next )
00783       if ( !nota->name )
00784     return nota->value;
00785 #endif
00786     return 0;
00787   }
00788 
00789 
00790   
00791   //
00792   //
00793   // class Notation
00794   //
00795   //
00796 
00797   struct UserID::Signature::Notation::Private {
00798     Private( gpgme_key_t aKey, gpgme_user_id_t aUid,
00799          gpgme_key_sig_t aSig, unsigned int idx )
00800       : key( aKey ), uid( 0 ), sig( 0 ), nota( 0 )
00801     {
00802       if ( key )
00803     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next )
00804       if ( u == aUid ) {
00805         uid = u;
00806         for ( gpgme_key_sig_t s = uid->signatures ; s ; s = s->next )
00807           if ( s == aSig ) {
00808         sig = s;
00809 #ifdef HAVE_GPGME_KEY_SIG_NOTATIONS
00810         for ( gpgme_sig_notation_t n = sig->notations ; n ; n = n->next, --idx )
00811           if ( n == aNota ) {
00812             nota = n;
00813             break;
00814           }
00815 #else
00816         (void)idx;
00817 #endif
00818         break;
00819           }
00820         break;
00821       }
00822       if ( !uid || !sig || !nota ) {
00823     uid = 0;
00824     sig = 0;
00825     key = 0;
00826     nota = 0;
00827       }
00828     }
00829       
00830     Private( gpgme_key_t aKey, gpgme_user_id_t aUid,
00831          gpgme_key_sig_t aSig, gpgme_sig_notation_t aNota )
00832       : key( aKey ), uid( 0 ), sig( 0 ), nota( 0 )
00833     {
00834       if ( key )
00835     for ( gpgme_user_id_t u = key->uids ; u ; u = u->next )
00836       if ( u == aUid ) {
00837         uid = u;
00838         for ( gpgme_key_sig_t s = uid->signatures ; s ; s = s->next )
00839           if ( s == aSig ) {
00840         sig = s;
00841 #ifdef HAVE_GPGME_KEY_SIG_NOTATIONS
00842         for ( gpgme_sig_notation_t n = sig->notations ; n ; n = n->next )
00843           if ( n == aNota ) {
00844             nota = n;
00845             break;
00846           }
00847 #else
00848         (void)aNota;
00849 #endif
00850         break;
00851           }
00852         break;
00853       }
00854       if ( !uid || !sig || !nota ) {
00855     uid = 0;
00856     sig = 0;
00857     key = 0;
00858     nota = 0;
00859       }
00860     }
00861       
00862     gpgme_key_t key;
00863     gpgme_user_id_t uid;
00864     gpgme_key_sig_t sig;
00865     gpgme_sig_notation_t nota;
00866   };
00867 
00868   UserID::Signature::Notation::Notation( gpgme_key_t key, gpgme_user_id_t uid,
00869                      gpgme_key_sig_t sig, unsigned int idx ) {
00870     d = new Private( key, uid, sig, idx );
00871     if ( d->key )
00872       gpgme_key_ref( d->key );
00873   }
00874 
00875   UserID::Signature::Notation::Notation( gpgme_key_t key, gpgme_user_id_t uid,
00876                      gpgme_key_sig_t sig, gpgme_sig_notation_t nota ) {
00877     d = new Private( key, uid, sig, nota );
00878     if ( d->key )
00879       gpgme_key_ref( d->key );
00880   }
00881 
00882   UserID::Signature::Notation::Notation( const Notation & other ) {
00883     d = new Private( other.d->key, other.d->uid, other.d->sig, other.d->nota );
00884     if ( d->key )
00885       gpgme_key_ref( d->key );
00886   }
00887 
00888   UserID::Signature::Notation::~Notation() {
00889     if ( d->key )
00890       gpgme_key_unref( d->key );
00891     delete d; d = 0;
00892   }
00893 
00894   const UserID::Signature::Notation & UserID::Signature::Notation::operator=( const Notation & other ) {
00895     if ( &other == this ) return *this;
00896 
00897     if ( other.d->key )
00898       gpgme_key_ref( other.d->key );
00899     if ( d->key )
00900       gpgme_key_unref( d->key );
00901     *d = *other.d;
00902     return *this;
00903   }
00904 
00905   bool UserID::Signature::Notation::isNull() const {
00906     return !d || !d->key || !d->uid || !d->sig || !d->nota;
00907   }
00908 
00909   UserID::Signature UserID::Signature::Notation::parent() const {
00910     return Signature( d->key, d->uid, d->sig );
00911   }
00912 
00913   const char * UserID::Signature::Notation::name() const {
00914     return d->nota ? d->nota->name : 0 ;
00915   }
00916 
00917   const char * UserID::Signature::Notation::value() const {
00918     return d->nota ? d->nota->value : 0 ;
00919   }
00920 
00921 } // namespace GpgME
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 Mon Apr 4 04:44:05 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003