certmanager/lib Library API Documentation

cryptplugwrapper.cpp

Go to the documentation of this file.
00001 
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033 
00034 #include "cryptplugwrapper.h"
00035 #include "cryptplug.h"
00036 
00037 #include <backends/qgpgme/qgpgmekeylistjob.h>
00038 #include <backends/qgpgme/qgpgmeencryptjob.h>
00039 #include <backends/qgpgme/qgpgmedecryptjob.h>
00040 #include <backends/qgpgme/qgpgmesignjob.h>
00041 #include <backends/qgpgme/qgpgmeverifydetachedjob.h>
00042 #include <backends/qgpgme/qgpgmeverifyopaquejob.h>
00043 #include <backends/qgpgme/qgpgmekeygenerationjob.h>
00044 #include <backends/qgpgme/qgpgmeimportjob.h>
00045 #include <backends/qgpgme/qgpgmeexportjob.h>
00046 #include <backends/qgpgme/qgpgmesecretkeyexportjob.h>
00047 #include <backends/qgpgme/qgpgmedownloadjob.h>
00048 #include <backends/qgpgme/qgpgmedeletejob.h>
00049 #include <backends/qgpgme/qgpgmesignencryptjob.h>
00050 #include <backends/qgpgme/qgpgmedecryptverifyjob.h>
00051 #include <backends/qgpgme/qgpgmecryptoconfig.h>
00052 #include <backends/qgpgme/qgpgmerefreshkeysjob.h>
00053 
00054 // qgpgme
00055 #include <qgpgme/dataprovider.h>
00056 
00057 // gpgme++
00058 #include <gpgmepp/data.h>
00059 #include <gpgmepp/importresult.h>
00060 #include <gpgmepp/keygenerationresult.h>
00061 
00062 // kde
00063 #include <kdebug.h>
00064 #include <kapplication.h>
00065 #include <klocale.h>
00066 #include <kglobal.h>
00067 #include <kconfig.h>
00068 
00069 // other
00070 #include <memory>
00071 
00072 #include <assert.h>
00073 #include <stdlib.h>
00074 #include <stdio.h>
00075 
00076 
00077 
00078 
00079 /*
00080  *
00081  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00082  *                                                                    *
00083  *  This file's source comments - as well as those in interface file  *
00084  *  cryptplugwrapper.h - are optimized for processing by Doxygen.     *
00085  *                                                                    *
00086  *  To obtain best results please get an updated version of Doxygen,  *
00087  *  for sources and binaries goto http://www.doxygen.org/index.html   *
00088  *                                                                    *
00089   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00090                                                                       *
00091                                                                       */
00092 
00093 
00094 
00121 // a little helper class for reordering of DN attributes
00122 class DNBeautifier {
00123 public:
00124   enum UnknownAttrsHandling { unknownAttrsHide,
00125                               unknownAttrsPrefix,
00126                               unknownAttrsPostfix,
00127                               unknownAttrsInfix };
00128   // infix: at the position of "_X_", if any, else Postfix
00129 
00130   DNBeautifier()
00131   {
00132     // the attrOrder is defaulted to an empty string automatically
00133     _unknownAttrsHandling = unknownAttrsInfix;
00134     _unknownAttrsHandlingChar = "INFIX";
00135   }
00136   DNBeautifier( KConfig* config,
00137                 const QString& cfgGroup,
00138                 const QString& cfgAttributeOrderEntry,
00139                 const QString& cfgUnknownAttrsEntry,
00140                 const QStringList& fallbackAttrOrder = QStringList(),
00141                 UnknownAttrsHandling fallbackUnknowAttrsHandling = unknownAttrsInfix )
00142   {
00143     _unknownAttrsHandling = unknownAttrsInfix;
00144     _unknownAttrsHandlingChar = "INFIX";
00145     if( config ){
00146       const QString oldGroup( config->group() );
00147       config->setGroup( cfgGroup );                             // e.g. "General"
00148       _attrOrder =
00149         config->readListEntry( cfgAttributeOrderEntry );        // e.g. "DNAttributeOrder"
00150       _unknownAttrsHandlingChar =
00151         config->readEntry( cfgUnknownAttrsEntry ).upper().latin1(); // e.g. "DNUnknownAttributes"
00152       config->setGroup( oldGroup );
00153       if( _unknownAttrsHandlingChar == "HIDE" )
00154         _unknownAttrsHandling = unknownAttrsHide;
00155       else if( _unknownAttrsHandlingChar == "PREFIX" )
00156         _unknownAttrsHandling = unknownAttrsPrefix;
00157       else if( _unknownAttrsHandlingChar == "POSTFIX" )
00158         _unknownAttrsHandling = unknownAttrsPostfix;
00159       else if( _unknownAttrsHandlingChar == "INFIX" )
00160         _unknownAttrsHandling = unknownAttrsInfix;
00161       else
00162         _unknownAttrsHandlingChar = "INFIX";
00163     }
00164     if( _attrOrder.isEmpty() && ! fallbackAttrOrder.isEmpty() )
00165       _attrOrder = fallbackAttrOrder;
00166 
00167     if( _attrOrder.isEmpty() ){
00168       _attrOrderChar = 0;
00169     }else{
00170       _attrOrderChar = new char*[ _attrOrder.count()+1 ];
00171       int i=0;
00172       for( QStringList::ConstIterator itOrder = _attrOrder.begin();
00173            itOrder != _attrOrder.end();
00174            ++itOrder ){
00175         _attrOrderChar[ i ] = (char*)malloc( ((*itOrder).length()+1)*sizeof(char) );
00176         strcpy( _attrOrderChar[ i ], (*itOrder).latin1() );
00177         ++i;
00178       }
00179       _attrOrderChar[ i ] = NULL;
00180     }
00181   }
00182   ~DNBeautifier()
00183   {
00184     int i=0;
00185     for( QStringList::ConstIterator itOrder = _attrOrder.begin();
00186          itOrder != _attrOrder.end();
00187          ++itOrder ){
00188       free( _attrOrderChar[ i ] );
00189       ++i;
00190     }
00191     delete[] _attrOrderChar;
00192   }
00193 
00194   QStringList attrOrder() const
00195   {
00196     return _attrOrder;
00197   }
00198   char** attrOrderChar()
00199   {
00200     return _attrOrderChar;
00201   }
00202 
00203   UnknownAttrsHandling unknownAttrsHandling() const
00204   {
00205     return _unknownAttrsHandling;
00206   }
00207   const char* unknownAttrsHandlingChar() const
00208   {
00209     return _unknownAttrsHandlingChar;
00210   }
00211 
00212   QValueList< QPair<QString,QString> > reorder( const QValueList< QPair<QString,QString> > & dn ) const
00213   {
00214     return reorder( dn, _attrOrder, _unknownAttrsHandling );
00215   }
00216 
00217 
00218   static QValueList< QPair<QString,QString> > reorder(
00219     const QValueList< QPair<QString,QString> > & dn,
00220     QStringList attrOrder,
00221     UnknownAttrsHandling unknownAttrsHandling )
00222   {
00223     if( !attrOrder.isEmpty() ){
00224       QPtrList<   QPair<QString,QString> > unknownEntries;
00225       QValueList< QPair<QString,QString> > dnNew;
00226 
00227       QPair<QString,QString>* unknownEntry;
00228       QStringList::ConstIterator itOrder;
00229       QValueList< QPair<QString,QString> >::ConstIterator itDN;
00230       bool bFound;
00231 
00232       if( unknownAttrsHandling != unknownAttrsHide ){
00233         // find all unknown entries in their order of appearance
00234         for( itDN = dn.begin(); itDN != dn.end(); ++itDN ){
00235           bFound = false;
00236           for( itOrder = attrOrder.begin(); itOrder != attrOrder.end(); ++itOrder ){
00237             if( (*itOrder) == (*itDN).first ){
00238               bFound = true;
00239               break;
00240             }
00241           }
00242           if( !bFound )
00243             unknownEntries.append( &(*itDN) );
00244         }
00245       }
00246 
00247       // prepend the unknown attrs (if desired)
00248       if( unknownAttrsHandling == unknownAttrsPrefix ){
00249         for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
00250           dnNew << *unknownEntry;
00251         }
00252       }
00253 
00254       // process the known attrs in the desired order
00255       bool b_X_declared = false;
00256       for( itOrder = attrOrder.begin(); itOrder != attrOrder.end(); ++itOrder ){
00257         if( (*itOrder) == "_X_" ){
00258           b_X_declared = true;
00259           // insert the unknown attrs (if desired)
00260           if( unknownAttrsHandling == unknownAttrsInfix ){
00261             for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
00262               dnNew << *unknownEntry;
00263             }
00264           }
00265         }else{
00266           for( itDN = dn.begin(); itDN != dn.end(); ++itDN ){
00267             if( (*itOrder) == (*itDN).first ){
00268               dnNew << *itDN;
00269               //kdDebug(5150) << QString((*itDN).first) <<" = " << QString((*itDN).second) << endl;;
00270             }
00271           }
00272         }
00273       }
00274 
00275       // append the unknown attrs (if desired)
00276       if( unknownAttrsHandling == unknownAttrsPostfix ||
00277           ( unknownAttrsHandling == unknownAttrsInfix && ! b_X_declared ) ){
00278         for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
00279           dnNew << *unknownEntry;
00280         }
00281       }
00282 
00283       return dnNew;
00284     }
00285     return dn;
00286   }
00287 
00288 private:
00289   QStringList _attrOrder;
00290   char**      _attrOrderChar;
00291   UnknownAttrsHandling _unknownAttrsHandling;
00292   QCString    _unknownAttrsHandlingChar;
00293 };
00294 
00295 
00296 
00297 /* special helper class to be used by signing/encrypting functions *******/
00298 
00299 
00300 
00301 StructuringInfoWrapper::StructuringInfoWrapper( CryptPlugWrapper* wrapper )
00302   : _initDone( false ), _wrapper( wrapper )
00303 {
00304     initMe();
00305 }
00306 StructuringInfoWrapper::~StructuringInfoWrapper()
00307 {
00308     freeMe();
00309 }
00310 void StructuringInfoWrapper::reset()
00311 {
00312     freeMe();
00313     initMe();
00314 }
00315 void StructuringInfoWrapper::initMe()
00316 {
00317     if ( _wrapper && _wrapper->cryptPlug() ) {
00318       _wrapper->cryptPlug()->init_StructuringInfo( &data );
00319       _initDone = true;
00320     }
00321 }
00322 void StructuringInfoWrapper::freeMe()
00323 {
00324     if( _wrapper && _wrapper->cryptPlug() && _initDone ) {
00325       _wrapper->cryptPlug()->free_StructuringInfo( &data );
00326       _initDone = false;
00327     }
00328 }
00329 
00330 class CryptPlugWrapper::Config {
00331 public:
00332   Config( gpgme_protocol_t proto );
00333   ~Config();
00334 
00335   const char*             signatureKeyCertificate;
00336   SignatureAlgorithm      signatureAlgorithm;
00337   SignatureCompoundMode   signatureCompoundMode;
00338   SendCertificates        sendCertificates;
00339   bool                    saveSentSignatures;
00340   bool                    warnNoCertificate;
00341   bool                    signatureUseCRLs;
00342   EncryptionAlgorithm     encryptionAlgorithm;
00343   EncryptEmail            encryptEmail;
00344   bool                    saveMessagesEncrypted;
00345   bool                    encryptionUseCRLs;
00346   bool                    encryptionCRLExpiryNearWarning;
00347   int                     encryptionCRLNearExpiryInterval;
00348   CertificateSource       certificateSource;
00349   bool                    warnSendUnsigned;
00350   bool                    signatureCertificateExpiryNearWarning;
00351   int                     signatureCertificateExpiryNearInterval;
00352   bool                    cACertificateExpiryNearWarning;
00353   int                     cACertificateExpiryNearInterval;
00354   bool                    rootCertificateExpiryNearWarning;
00355   int                     rootCertificateExpiryNearInterval;
00356   bool                    warnSendUnencrypted;
00357   bool                    checkCertificatePath;
00358   bool                    receiverCertificateExpiryNearWarning;
00359   int                     receiverCertificateExpiryNearWarningInterval;
00360   bool                    certificateInChainExpiryNearWarning;
00361   int                     certificateInChainExpiryNearWarningInterval;
00362   bool                    receiverEmailAddressNotInCertificateWarning;
00363   const char* libVersion; /* a statically allocated string with the GPGME Version used */
00364 };
00365 
00366 static const int NEAR_EXPIRY = 14;
00367 
00368 CryptPlugWrapper::Config::Config( gpgme_protocol_t proto )
00369 {
00370   signatureAlgorithm                   = SignAlg_SHA1;
00371   if ( proto == GPGME_PROTOCOL_CMS )
00372     signatureCompoundMode              = SignatureCompoundMode_Opaque;
00373   else
00374     signatureCompoundMode              = SignatureCompoundMode_Detached;
00375   sendCertificates                     = SendCert_SendChainWithRoot;
00376   saveSentSignatures                   = true;
00377   warnNoCertificate                    = true;
00378   signatureUseCRLs                     = true;
00379   encryptionAlgorithm                  = EncryptAlg_RSA;
00380   encryptEmail                         = EncryptEmail_Ask;
00381   saveMessagesEncrypted                = true;
00382   encryptionUseCRLs                    = true;
00383   encryptionCRLExpiryNearWarning       = false;
00384   encryptionCRLNearExpiryInterval      = NEAR_EXPIRY;
00385   certificateSource                    = CertSrc_Server;
00386   warnSendUnsigned                             = true;
00387   signatureCertificateExpiryNearWarning        = true;
00388   signatureCertificateExpiryNearInterval       = NEAR_EXPIRY;
00389   cACertificateExpiryNearWarning               = true;
00390   cACertificateExpiryNearInterval              = NEAR_EXPIRY;
00391   rootCertificateExpiryNearWarning             = true;
00392   rootCertificateExpiryNearInterval            = NEAR_EXPIRY;
00393   warnSendUnencrypted                          = false;
00394   checkCertificatePath                         = true;
00395   receiverCertificateExpiryNearWarning         = true;
00396   receiverCertificateExpiryNearWarningInterval = NEAR_EXPIRY;
00397   certificateInChainExpiryNearWarning          = true;
00398   certificateInChainExpiryNearWarningInterval  = NEAR_EXPIRY;
00399   receiverEmailAddressNotInCertificateWarning  = true;
00400   libVersion = gpgme_check_version (NULL);
00401 }
00402 
00403 CryptPlugWrapper::Config::~Config() {
00404 }
00405 
00406 const char* CryptPlugWrapper::bugURL(){ return "http://www.gnupg.org/aegypten/"; }
00407 
00408 
00409 void CryptPlugWrapper::setSignatureAlgorithm( SignatureAlgorithm sigAlg )
00410 {
00411   _config->signatureAlgorithm = sigAlg;
00412 }
00413 
00414 SignatureAlgorithm CryptPlugWrapper::signatureAlgorithm()
00415 {
00416   return _config->signatureAlgorithm;
00417 }
00418 
00419 
00420 
00421 
00422 void CryptPlugWrapper::setWarnSendUnsigned( bool flag )
00423 {
00424   _config->warnSendUnsigned = flag;
00425 }
00426 
00427 bool CryptPlugWrapper::warnSendUnsigned()
00428 {
00429   return _config->warnSendUnsigned;
00430 }
00431 
00432 
00433 
00434 
00435 
00436 
00437 
00438 
00439 
00440 void CryptPlugWrapper::setSignatureCertificateExpiryNearWarning( bool flag )
00441 {
00442   _config->signatureCertificateExpiryNearWarning = flag;
00443 }
00444 
00445 bool CryptPlugWrapper::signatureCertificateExpiryNearWarning( void )
00446 {
00447   return _config->signatureCertificateExpiryNearWarning;
00448 }
00449 
00450 void CryptPlugWrapper::setSignatureCertificateExpiryNearInterval( int interval )
00451 {
00452   _config->signatureCertificateExpiryNearInterval = interval;
00453 }
00454 
00455 int CryptPlugWrapper::signatureCertificateExpiryNearInterval( void )
00456 {
00457   return _config->signatureCertificateExpiryNearInterval;
00458 }
00459 
00460 void CryptPlugWrapper::setCACertificateExpiryNearWarning( bool flag )
00461 {
00462   _config->cACertificateExpiryNearWarning = flag;
00463 }
00464 
00465 bool CryptPlugWrapper::caCertificateExpiryNearWarning( void )
00466 {
00467   return _config->cACertificateExpiryNearWarning;
00468 }
00469 
00470 void CryptPlugWrapper::setCACertificateExpiryNearInterval( int interval )
00471 {
00472   _config->cACertificateExpiryNearInterval = interval;
00473 }
00474 
00475 int CryptPlugWrapper::caCertificateExpiryNearInterval( void )
00476 {
00477   return _config->cACertificateExpiryNearInterval;
00478 }
00479 
00480 void CryptPlugWrapper::setRootCertificateExpiryNearWarning( bool flag )
00481 {
00482   _config->rootCertificateExpiryNearWarning = flag;
00483 }
00484 
00485 bool CryptPlugWrapper::rootCertificateExpiryNearWarning( void )
00486 {
00487   return _config->rootCertificateExpiryNearWarning;
00488 }
00489 
00490 void CryptPlugWrapper::setRootCertificateExpiryNearInterval( int interval )
00491 {
00492   _config->rootCertificateExpiryNearInterval = interval;
00493 }
00494 
00495 int CryptPlugWrapper::rootCertificateExpiryNearInterval( void )
00496 {
00497   return _config->rootCertificateExpiryNearInterval;
00498 }
00499 
00500 
00501 
00502 
00503 
00504 void CryptPlugWrapper::setEncryptionAlgorithm( EncryptionAlgorithm cryptAlg )
00505 {
00506   _config->encryptionAlgorithm = cryptAlg;
00507 }
00508 
00509 EncryptionAlgorithm CryptPlugWrapper::encryptionAlgorithm()
00510 {
00511   return _config->encryptionAlgorithm;
00512 }
00513 
00514 void CryptPlugWrapper::setEncryptEmail( EncryptEmail cryptMode )
00515 {
00516   _config->encryptEmail = cryptMode;
00517 }
00518 
00519 EncryptEmail CryptPlugWrapper::encryptEmail()
00520 {
00521   return _config->encryptEmail;
00522 }
00523 
00524 
00525 
00526 
00527 
00528 
00529 void CryptPlugWrapper::setWarnSendUnencrypted( bool flag )
00530 {
00531   _config->warnSendUnencrypted = flag;
00532 }
00533 
00534 bool CryptPlugWrapper::warnSendUnencrypted()
00535 {
00536   return _config->warnSendUnencrypted;
00537 }
00538 
00539 
00540 
00541 
00542 
00543 
00544 
00545 
00546 
00547 void CryptPlugWrapper::setSaveMessagesEncrypted( bool flag )
00548 {
00549   _config->saveMessagesEncrypted = flag;
00550 }
00551 
00552 bool CryptPlugWrapper::saveMessagesEncrypted()
00553 {
00554   return _config->saveMessagesEncrypted;
00555 }
00556 
00557 
00558 
00559 
00560 
00561 
00562 
00563 void CryptPlugWrapper::setCheckCertificatePath( bool flag )
00564 {
00565   _config->checkCertificatePath = flag;
00566 }
00567 
00568 bool CryptPlugWrapper::checkCertificatePath()
00569 {
00570   return _config->checkCertificatePath;
00571 }
00572 
00573 
00574 
00575 
00576 
00577 
00578 
00579 
00580 
00581 
00582 
00583 
00584 void CryptPlugWrapper::setReceiverCertificateExpiryNearWarning( bool flag )
00585 {
00586   _config->receiverCertificateExpiryNearWarning = flag;
00587 }
00588 
00589 bool CryptPlugWrapper::receiverCertificateExpiryNearWarning()
00590 {
00591   return _config->receiverCertificateExpiryNearWarning;
00592 }
00593 
00594 
00595 void CryptPlugWrapper::setReceiverCertificateExpiryNearWarningInterval( int interval )
00596 {
00597   _config->receiverCertificateExpiryNearWarningInterval = interval;
00598 }
00599 
00600 int CryptPlugWrapper::receiverCertificateExpiryNearWarningInterval()
00601 {
00602   return _config->receiverCertificateExpiryNearWarningInterval;
00603 }
00604 
00605 void CryptPlugWrapper::setCertificateInChainExpiryNearWarning( bool flag )
00606 {
00607   _config->certificateInChainExpiryNearWarning = flag;
00608 }
00609 
00610 bool CryptPlugWrapper::certificateInChainExpiryNearWarning()
00611 {
00612   return _config->certificateInChainExpiryNearWarning;
00613 }
00614 
00615 
00616 void CryptPlugWrapper::setCertificateInChainExpiryNearWarningInterval( int interval )
00617 {
00618   _config->certificateInChainExpiryNearWarningInterval = interval;
00619 }
00620 
00621 int CryptPlugWrapper::certificateInChainExpiryNearWarningInterval()
00622 {
00623   return _config->certificateInChainExpiryNearWarningInterval;
00624 }
00625 
00626 void CryptPlugWrapper::setReceiverEmailAddressNotInCertificateWarning( bool flag )
00627 {
00628   _config->receiverEmailAddressNotInCertificateWarning = flag;
00629 }
00630 
00631 bool CryptPlugWrapper::receiverEmailAddressNotInCertificateWarning()
00632 {
00633   return _config->receiverEmailAddressNotInCertificateWarning;
00634 }
00635 
00636 
00637 
00638 
00639 
00640 
00641 
00642 
00643 void CryptPlugWrapper::setEncryptionUseCRLs( bool flag )
00644 {
00645   _config->encryptionUseCRLs = flag;
00646 
00647   /* PENDING(g10) Store this setting in gpgme and use it. If true,
00648      every certificate used for encryption should be checked against
00649      applicable CRLs.
00650   */
00651 }
00652 
00653 bool CryptPlugWrapper::encryptionUseCRLs()
00654 {
00655   return _config->encryptionUseCRLs;
00656 }
00657 
00658 
00659 void CryptPlugWrapper::setEncryptionCRLExpiryNearWarning( bool flag )
00660 {
00661   _config->encryptionCRLExpiryNearWarning = flag;
00662 }
00663 
00664 bool CryptPlugWrapper::encryptionCRLExpiryNearWarning()
00665 {
00666   return _config->encryptionCRLExpiryNearWarning;
00667 }
00668 
00669 void CryptPlugWrapper::setEncryptionCRLNearExpiryInterval( int interval )
00670 {
00671   _config->encryptionCRLNearExpiryInterval = interval;
00672 }
00673 
00674 int CryptPlugWrapper::encryptionCRLNearExpiryInterval()
00675 {
00676   return _config->encryptionCRLNearExpiryInterval;
00677 }
00678 
00679 
00680 void CryptPlugWrapper::setCertificateSource( CertificateSource source )
00681 {
00682   _config->certificateSource = source;
00683 }
00684 
00685 CertificateSource CryptPlugWrapper::certificateSource()
00686 {
00687   return _config->certificateSource;
00688 }
00689 
00690 
00691 
00692 
00693 
00694 QString CryptPlugWrapper::libVersion() const {
00695   return _config && _config->libVersion ? QString::fromUtf8( _config->libVersion ) : QString::null ;
00696 }
00697 
00698 /* Some multi purpose functions ******************************************/
00699 
00700 QString CryptPlugWrapper::errorIdToText( int errId, bool & isPassphraseError ) {
00701   const GpgME::Error err( errId );
00702   isPassphraseError = err.isCanceled()
00703     || gpgme_err_code( errId ) == GPG_ERR_NO_SECKEY ; // FIXME: more?
00704   return QString::fromLocal8Bit( err.asString() );
00705 #if 0
00706   switch( errId ){
00707   case /*GPGME_EOF                = */-1:
00708     return(i18n("End of File reached during operation."));
00709   case /*GPGME_No_Error           = */0:
00710     return(i18n("No error."));
00711   case /*GPGME_General_Error      = */1:
00712     return(i18n("General error."));
00713   case /*GPGME_Out_Of_Core        = */2:
00714     return(i18n("Out of core."));
00715   case /*GPGME_Invalid_Value      = */3:
00716     return(i18n("Invalid value."));
00717   case /*GPGME_Busy               = */4:
00718     return(i18n("Engine is busy."));
00719   case /*GPGME_No_Request         = */5:
00720     return(i18n("No request."));
00721   case /*GPGME_Exec_Error         = */6:
00722     return(i18n("Execution error."));
00723   case /*GPGME_Too_Many_Procs     = */7:
00724     return(i18n("Too many processes."));
00725   case /*GPGME_Pipe_Error         = */8:
00726     return(i18n("Pipe error."));
00727   case /*GPGME_No_Recipients      = */9:
00728     return(i18n("No recipients."));
00729   case /*GPGME_No_Data            = */10:
00730     return(i18n("No data."));
00731   case /*GPGME_Conflict           = */11:
00732     return(i18n("Conflict."));
00733   case /*GPGME_Not_Implemented    = */12:
00734     return(i18n("Not implemented."));
00735   case /*GPGME_Read_Error         = */13:
00736     return(i18n("Read error."));
00737   case /*GPGME_Write_Error        = */14:
00738     return(i18n("Write error."));
00739   case /*GPGME_Invalid_Type       = */15:
00740     return(i18n("Invalid type."));
00741   case /*GPGME_Invalid_Mode       = */16:
00742     return(i18n("Invalid mode."));
00743   case /*GPGME_File_Error         = */17:  // errno is set in this case.
00744     return(i18n("File error."));
00745   case /*GPGME_Decryption_Failed  = */18:
00746     return(i18n("Decryption failed."));
00747   case /*GPGME_No_Passphrase      = */19:
00748     isPassphraseError = true;
00749     return(i18n("No passphrase."));
00750   case /*GPGME_Canceled           = */20:
00751     isPassphraseError = true;
00752     return(i18n("Canceled."));
00753   case /*GPGME_Invalid_Key        = */21:
00754     isPassphraseError = true; // ### ???
00755     return(i18n("Invalid key."));
00756   case /*GPGME_Invalid_Engine     = */22:
00757     return(i18n("Invalid engine."));
00758   case /*GPGME_Invalid_Recipients = */23:
00759     return(i18n("Invalid recipients."));
00760   default:
00761     return(i18n("Unknown error."));
00762   }
00763 #endif
00764 }
00765 
00766 /* some special functions ************************************************/
00767 
00768 
00769 CryptPlugWrapper::CryptPlugWrapper( const QString& name,
00770                                     const QString& libName,
00771                                     const QString& update,
00772                                     bool           active )
00773   : Kleo::CryptoBackend::Protocol(),
00774     _name( name ),
00775     _libName( libName ),
00776     _updateURL( update ),
00777     _active(  active  ),
00778     _initStatus( InitStatus_undef ),
00779     _cp( 0 ),
00780     _config( 0 ),
00781     _cryptoConfig( 0 )
00782 {
00783   const bool ok = initialize( 0, 0 );
00784   assert( ok );
00785 }
00786 
00787 
00788 CryptPlugWrapper::~CryptPlugWrapper()
00789 {
00790     deinitialize();
00791 }
00792 
00793 
00794 void CryptPlugWrapper::setActive( bool active )
00795 {
00796     _active = active;
00797 }
00798 
00799 
00800 bool CryptPlugWrapper::active() const
00801 {
00802     return _active;
00803 }
00804 
00805 
00806 
00807 bool CryptPlugWrapper::setLibName( const QString& libName )
00808 {
00809     bool bOk = ! _cp;           // Changing the lib name is only allowed
00810     if( bOk )                   // when either no initialization took
00811         _libName = libName;     // place or 'deinitialize()' has been
00812     return bOk;                 // called afterwards.
00813 }
00814 
00815 QString CryptPlugWrapper::libName() const
00816 {
00817     return _libName;
00818 }
00819 
00820 QString CryptPlugWrapper::protocol() const
00821 {
00822   if ( _libName.contains( "smime" ) )
00823     return "smime";
00824   if ( _libName.contains( "openpgp" ) )
00825     return "openpgp";
00826   return QString::null;
00827 }
00828 
00829 void CryptPlugWrapper::setDisplayName( const QString& name )
00830 {
00831     _name = name;
00832 }
00833 
00834 
00835 QString CryptPlugWrapper::displayName() const
00836 {
00837     if ( !_name.isEmpty() )
00838       return _name;
00839     if ( _libName.contains( "smime" ) )
00840       return "gpgsm";
00841     if ( _libName.contains( "openpgp" ) )
00842       return "gpg";
00843     return i18n("(Unknown Protocol)");
00844 }
00845 
00846 
00847 void CryptPlugWrapper::setUpdateURL( const QString& url )
00848 {
00849     _updateURL = url;
00850 }
00851 
00852 
00853 QString CryptPlugWrapper::updateURL() const
00854 {
00855     return _updateURL;
00856 }
00857 
00858 bool CryptPlugWrapper::alwaysEncryptToSelf() {
00859   return true;
00860 }
00861 
00862 
00863 void CryptPlugWrapper::setAlwaysEncryptToSelf( bool enc ) {
00864   mAlwaysEncryptToSelf = enc;
00865 }
00866 
00867 bool CryptPlugWrapper::initialize( InitStatus* initStatus, QString* errorMsg )
00868 {
00869     if ( _cp )
00870       return true;
00871 
00872     _initStatus = InitStatus_undef;
00873     /* make sure we have a lib name */
00874     if ( _libName.isEmpty() ) {
00875       _initStatus = InitStatus_NoLibName;
00876       kdDebug(5150) << "No library name was given.\n" << endl;
00877     } else {
00878       if ( _libName.contains( "smime" ) ) {
00879     _cp = new SMIMECryptPlug();
00880     _config = new Config( GPGME_PROTOCOL_CMS );
00881       } else if ( _libName.contains( "openpgp" ) ) {
00882     _cp = new OpenPGPCryptPlug();
00883     _config = new Config( GPGME_PROTOCOL_OpenPGP );
00884       } else {
00885     _cp = 0;
00886     _config = 0;
00887       }
00888 
00889       if ( !_cp ) {
00890     _initStatus = InitStatus_LoadError;
00891     kdDebug(5150) << "Couldn't create '" << _libName.latin1() << "'" << endl;
00892       } else {
00893     /* now call the init function */
00894     if( !_cp->initialize() ) {
00895       _initStatus = InitStatus_InitError;
00896       kdDebug(5150) << "Error while executing function 'initialize' on plugin " << _libName << endl;
00897       _lastError = i18n("Error while initializing plugin \"%1\"").arg( _libName );
00898       if ( errorMsg )
00899         *errorMsg = _lastError;
00900       delete _cp; _cp = 0;
00901       delete _config; _config = 0;
00902     } else {
00903       _initStatus  = InitStatus_Ok;
00904     }
00905       }
00906     }
00907     if( initStatus )
00908         *initStatus = _initStatus;
00909     return _initStatus == InitStatus_Ok;
00910 }
00911 
00912 
00913 
00914 void CryptPlugWrapper::deinitialize()
00915 {
00916     delete _cp; _cp = 0;
00917     delete _config; _config = 0;
00918     delete _cryptoConfig; _cryptoConfig = 0;
00919 }
00920 
00921 
00922 CryptPlugWrapper::InitStatus CryptPlugWrapper::initStatus( QString* errorMsg ) const
00923 {
00924     if( errorMsg )
00925         *errorMsg = _lastError;
00926     return _initStatus;
00927 }
00928 
00929 
00930 bool CryptPlugWrapper::hasFeature( Feature flag )
00931 {
00932   return _cp && _cp->hasFeature( flag );
00933 }
00934 
00935 
00936 /* normal functions ******************************************************/
00937 
00938 
00939 bool CryptPlugWrapper::signMessage( const char* cleartext,
00940                                     char** ciphertext,
00941                                     const size_t* cipherLen,
00942                                     const char* certificate,
00943                                     StructuringInfoWrapper& structuring,
00944                                     int* errId,
00945                                     char** errTxt )
00946 {
00947   return _cp && _cp->signMessage( cleartext, ciphertext, cipherLen, certificate,
00948                   &structuring.data, errId, errTxt,
00949                   _config->sendCertificates, _config->signatureCompoundMode );
00950 }
00951 
00952 
00953 bool CryptPlugWrapper::checkMessageSignature( char** cleartext,
00954                                               const char* signaturetext,
00955                                               bool signatureIsBinary,
00956                                               int signatureLen,
00957                                               CryptPlug::SignatureMetaData* sigmeta )
00958 {
00959   DNBeautifier dnBeautifier( kapp->config(),
00960                              "DN",
00961                              "AttributeOrder",
00962                              "UnknownAttributes" );
00963   return _cp && _cp->checkMessageSignature( cleartext,
00964                                             signaturetext,
00965                                             signatureIsBinary,
00966                                             signatureLen,
00967                                             sigmeta,
00968                                             dnBeautifier.attrOrderChar(),
00969                                             dnBeautifier.unknownAttrsHandlingChar() );
00970 }
00971 
00972 
00973 bool CryptPlugWrapper::storeCertificatesFromMessage( const char* ciphertext )
00974 {
00975     return _cp && _cp->storeCertificatesFromMessage( ciphertext );
00976 }
00977 
00978 
00979 bool CryptPlugWrapper::findCertificates( const char*  addressee,
00980                                          char** certificates,
00981                                          int* newSize,
00982                                          bool secretOnly )
00983 {
00984   DNBeautifier dnBeautifier( kapp->config(),
00985                              "DN",
00986                              "AttributeOrder",
00987                              "UnknownAttributes" );
00988   return _cp && _cp->findCertificates( addressee,
00989                                        certificates,
00990                                        newSize,
00991                                        secretOnly,
00992                                        dnBeautifier.attrOrderChar(),
00993                                        dnBeautifier.unknownAttrsHandlingChar() );
00994 }
00995 
00996 bool CryptPlugWrapper::encryptMessage( const char* cleartext,
00997                                        const char** ciphertext,
00998                                        const size_t* cipherLen,
00999                                        const char* addressee,
01000                                        StructuringInfoWrapper& structuring,
01001                                        int* errId,
01002                                        char** errTxt )
01003 {
01004   return _cp && _cp->encryptMessage( cleartext, ciphertext, cipherLen, addressee,
01005                      &structuring.data, errId, errTxt );
01006 }
01007 
01008 
01009 bool CryptPlugWrapper::encryptAndSignMessage( const char* cleartext,
01010                                               const char** ciphertext,
01011                                               const char* certificate,
01012                                               StructuringInfoWrapper& structuring )
01013 {
01014   return _cp && _cp->encryptAndSignMessage( cleartext, ciphertext, certificate,
01015                         &structuring.data );
01016 }
01017 
01018 
01019 bool CryptPlugWrapper::decryptMessage( const char* ciphertext,
01020                                        bool        cipherIsBinary,
01021                                        int         cipherLen,
01022                                        char**      cleartext,
01023                                        const char* certificate,
01024                                        int* errId,
01025                                        char** errTxt )
01026 {
01027   return _cp && _cp->decryptMessage( ciphertext, cipherIsBinary, cipherLen,
01028                      (const char**)cleartext, certificate, errId, errTxt );
01029 }
01030 
01031 
01032 bool CryptPlugWrapper::decryptAndCheckMessage(
01033                             const char*  ciphertext,
01034                             bool         cipherIsBinary,
01035                             int          cipherLen,
01036                             char**       cleartext,
01037                             const char*  certificate,
01038                             bool*        signatureFound,
01039                             CryptPlug::SignatureMetaData* sigmeta,
01040                             int*   errId,
01041                             char** errTxt )
01042 {
01043   DNBeautifier dnBeautifier( kapp->config(),
01044                              "DN",
01045                              "AttributeOrder",
01046                              "UnknownAttributes" );
01047   return _cp && _cp->decryptAndCheckMessage( ciphertext,
01048                                              cipherIsBinary,
01049                                              cipherLen,
01050                                              (const char**)cleartext,
01051                                              certificate,
01052                                              signatureFound,
01053                                              sigmeta,
01054                                              errId,
01055                                              errTxt,
01056                                              dnBeautifier.attrOrderChar(),
01057                                              dnBeautifier.unknownAttrsHandlingChar() );
01058 }
01059 
01060 
01061 
01062 
01063 void CryptPlugWrapper::freeSignatureMetaData( CryptPlug::SignatureMetaData* sigmeta )
01064 {
01065     if ( !sigmeta )
01066       return;
01067     free( sigmeta->status );
01068     for( int i = 0; i < sigmeta->extended_info_count; ++i ) {
01069         free( sigmeta->extended_info[i].creation_time );
01070         free( (void*)sigmeta->extended_info[i].status_text );
01071         free( (void*)sigmeta->extended_info[i].keyid );
01072         free( (void*)sigmeta->extended_info[i].fingerprint );
01073         free( (void*)sigmeta->extended_info[i].algo );
01074         free( (void*)sigmeta->extended_info[i].userid );
01075         free( (void*)sigmeta->extended_info[i].name );
01076         free( (void*)sigmeta->extended_info[i].comment );
01077         if( sigmeta->extended_info[i].emailCount ){
01078             for( int j=0; j < sigmeta->extended_info[i].emailCount; ++j)
01079                 if( sigmeta->extended_info[i].emailList[j] )
01080                     free( (void*)sigmeta->extended_info[i].emailList[j] );
01081             free( (void*)sigmeta->extended_info[i].emailList );
01082         }
01083     }
01084     free( sigmeta->extended_info );
01085 }
01086 
01087 CryptPlugWrapper::CertificateInfoList CryptPlugWrapper::listKeys( const QString& pattern,
01088   bool remote,
01089   bool* truncated )
01090 {
01091     CertificateInfoList result;
01092     if ( truncated ) *truncated = false;
01093     if ( !_cp )
01094       return result;
01095 
01096     CryptPlug::CertIterator * it = _cp->startListCertificates( pattern.utf8().data(), remote );
01097     if ( !it )
01098       return result;
01099 
01100     DNBeautifier dnBeautifier( kapp->config(),
01101                                "DN",
01102                                "AttributeOrder",
01103                                "UnknownAttributes" );
01104 
01105     while ( true ) {
01106       CryptPlug::CertificateInfo* info = 0;
01107       if ( _cp->nextCertificate( it,
01108                                  &info,
01109                                  dnBeautifier.attrOrderChar(),
01110                                  dnBeautifier.unknownAttrsHandlingChar() ) != 0 ) {
01111         kdDebug(5150) << "error" << endl;
01112         break;
01113       }
01114       if ( !info )
01115         break;
01116 
01117       CryptPlugWrapper::CertificateInfo cpwinfo;
01118       for ( char** ptr = info->userid; *ptr; ++ptr )
01119         cpwinfo.userid << QString::fromUtf8( *ptr );
01120       cpwinfo.userid_0_org = QString::fromUtf8( info->userid_0_org );
01121 
01122       kdDebug(5150) << "CryptPlugWrapper got " << cpwinfo.userid[0] << endl;
01123       cpwinfo.serial = QString::fromUtf8( info->serial );
01124       kdDebug(5150) << "fpr=" << info->fingerprint << endl;
01125       cpwinfo.fingerprint = QString::fromUtf8( info->fingerprint );
01126       kdDebug(5150) << "Done getting fpr" << endl;
01127 
01128       cpwinfo.issuer_org   = QString::fromUtf8( info->issuer_org );
01129       cpwinfo.issuer_reord = QString::fromUtf8( info->issuer_reord );
01130       cpwinfo.chainid = QString::fromUtf8( info->chainid );
01131       QString caps = QString::fromUtf8( info->caps );
01132 
01133       cpwinfo.sign = caps.contains('s');
01134       cpwinfo.encrypt = caps.contains('e');
01135       cpwinfo.certify = caps.contains('c');
01136 
01137       cpwinfo.created.setTime_t(info->created );
01138       cpwinfo.expire.setTime_t( info->expire );
01139 
01140       cpwinfo.secret = info->secret;
01141       cpwinfo.invalid = info->invalid;
01142       cpwinfo.expired = info->expired;
01143       cpwinfo.disabled = info->disabled;
01144 
01145       for ( CryptPlug::DnPair * a = info->dnarray ; a && a->key&& a->value ; ++a )
01146         //kdDebug(5150) << "CryptPlugWrapper::listKeys() " << a->key << " = " << a->value << endl;
01147         cpwinfo.dn.push_back( qMakePair( QString::fromUtf8( a->key ), QString::fromUtf8( a->value ) ) );
01148 
01149       //cpwinfo.dn = dnBeautifier.reorder( cpwinfo.dn );
01150 
01151       result.push_back( cpwinfo );
01152     }
01153 
01154     if ( _cp->endListCertificates( it ) != 0 )
01155       if ( truncated ) *truncated = true;
01156 
01157     return result;
01158 }
01159 
01160 GpgME::ImportResult CryptPlugWrapper::importCertificate( const char* data, size_t length )
01161 {
01162     if ( !_cp )
01163       return GpgME::ImportResult();
01164 
01165 
01166    return _cp->importCertificateFromMem( data, length );
01167 }
01168 
01169 Kleo::KeyListJob * CryptPlugWrapper::keyListJob( bool remote, bool includeSigs, bool validate ) const {
01170   if ( !_cp )
01171     return 0;
01172 
01173   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01174   if ( !context )
01175     return 0;
01176 
01177   unsigned int mode = context->keyListMode();
01178   if ( remote ) {
01179     mode |= GpgME::Context::Extern;
01180     mode &= ~GpgME::Context::Local;
01181   } else {
01182     mode |= GpgME::Context::Local;
01183     mode &= ~GpgME::Context::Extern;
01184   }
01185   if ( includeSigs ) mode |= GpgME::Context::Signatures;
01186   if ( validate ) mode |= GpgME::Context::Validate;
01187   context->setKeyListMode( mode );
01188   return new Kleo::QGpgMEKeyListJob( context );
01189 }
01190 
01191 Kleo::EncryptJob * CryptPlugWrapper::encryptJob( bool armor, bool textmode ) const {
01192   if ( !_cp )
01193     return 0;
01194 
01195   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01196   if ( !context )
01197     return 0;
01198 
01199   context->setArmor( armor );
01200   context->setTextMode( textmode );
01201   return new Kleo::QGpgMEEncryptJob( context );
01202 }
01203 
01204 Kleo::DecryptJob * CryptPlugWrapper::decryptJob() const {
01205   if ( !_cp )
01206     return 0;
01207 
01208   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01209   if ( !context )
01210     return 0;
01211 
01212   return new Kleo::QGpgMEDecryptJob( context );
01213 }
01214 
01215 Kleo::SignJob * CryptPlugWrapper::signJob( bool armor, bool textMode ) const {
01216   if ( !_cp )
01217     return 0;
01218 
01219   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01220   if ( !context )
01221     return 0;
01222 
01223   context->setArmor( armor );
01224   context->setTextMode( textMode );
01225 
01226   return new Kleo::QGpgMESignJob( context );
01227 }
01228 
01229 Kleo::VerifyDetachedJob * CryptPlugWrapper::verifyDetachedJob( bool textMode ) const {
01230   if ( !_cp )
01231     return 0;
01232 
01233   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01234   if ( !context )
01235     return 0;
01236 
01237   context->setTextMode( textMode );
01238 
01239   return new Kleo::QGpgMEVerifyDetachedJob( context );
01240 }
01241 
01242 Kleo::VerifyOpaqueJob * CryptPlugWrapper::verifyOpaqueJob( bool textMode ) const {
01243   if ( !_cp )
01244     return 0;
01245 
01246   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01247   if ( !context )
01248     return 0;
01249 
01250   context->setTextMode( textMode );
01251 
01252   return new Kleo::QGpgMEVerifyOpaqueJob( context );
01253 }
01254 
01255 Kleo::KeyGenerationJob * CryptPlugWrapper::keyGenerationJob() const {
01256   if ( !_cp )
01257     return 0;
01258 
01259   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01260   if ( !context )
01261     return 0;
01262 
01263   return new Kleo::QGpgMEKeyGenerationJob( context );
01264 }
01265 
01266 Kleo::ImportJob * CryptPlugWrapper::importJob() const {
01267   if ( !_cp )
01268     return 0;
01269 
01270   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01271   if ( !context )
01272     return 0;
01273 
01274   return new Kleo::QGpgMEImportJob( context );
01275 }
01276 
01277 Kleo::ExportJob * CryptPlugWrapper::publicKeyExportJob( bool armor ) const {
01278   if ( !_cp )
01279     return 0;
01280 
01281   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01282   if ( !context )
01283     return 0;
01284 
01285   context->setArmor( armor );
01286   return new Kleo::QGpgMEExportJob( context );
01287 }
01288 
01289 Kleo::ExportJob * CryptPlugWrapper::secretKeyExportJob( bool armor ) const {
01290   if ( !_cp || _cp->mProtocol != GpgME::Context::CMS ) // fixme: add support for gpg, too
01291     return 0;
01292 
01293   // this operation is not supported by gpgme, so we have to call gpgsm ourselves:
01294   return new Kleo::QGpgMESecretKeyExportJob( armor );
01295 }
01296 
01297 Kleo::RefreshKeysJob * CryptPlugWrapper::refreshKeysJob() const {
01298   if ( !_cp || _cp->mProtocol != GpgME::Context::CMS ) // fixme: add support for gpg, too
01299     return 0;
01300 
01301   // this operation is not supported by gpgme, so we have to call gpgsm ourselves:
01302   return new Kleo::QGpgMERefreshKeysJob();
01303 }
01304 
01305 Kleo::DownloadJob * CryptPlugWrapper::downloadJob( bool armor ) const {
01306   if ( !_cp )
01307     return 0;
01308 
01309   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01310   if ( !context )
01311     return 0;
01312 
01313   context->setArmor( armor );
01314   // this is the hackish interface for downloading from keyserers currently:
01315   context->setKeyListMode( GpgME::Context::Extern );
01316 
01317   return new Kleo::QGpgMEDownloadJob( context );
01318 }
01319 
01320 Kleo::DeleteJob * CryptPlugWrapper::deleteJob() const {
01321   if ( !_cp )
01322     return 0;
01323 
01324   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01325   if ( !context )
01326     return 0;
01327 
01328   return new Kleo::QGpgMEDeleteJob( context );
01329 }
01330 
01331 Kleo::SignEncryptJob * CryptPlugWrapper::signEncryptJob( bool armor, bool textMode ) const {
01332   if ( !_cp )
01333     return 0;
01334 
01335   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01336   if ( !context )
01337     return 0;
01338 
01339   context->setArmor( armor );
01340   context->setTextMode( textMode );
01341 
01342   return new Kleo::QGpgMESignEncryptJob( context );
01343 }
01344 
01345 Kleo::DecryptVerifyJob * CryptPlugWrapper::decryptVerifyJob( bool textMode ) const {
01346   if ( !_cp )
01347     return 0;
01348 
01349   GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
01350   if ( !context )
01351     return 0;
01352 
01353   context->setTextMode( textMode );
01354 
01355   return new Kleo::QGpgMEDecryptVerifyJob( context );
01356 }
KDE Logo
This file is part of the documentation for certmanager/lib Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 22:40:25 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003