37 #include <backends/qgpgme/qgpgmekeylistjob.h>
38 #include <backends/qgpgme/qgpgmeencryptjob.h>
39 #include <backends/qgpgme/qgpgmedecryptjob.h>
40 #include <backends/qgpgme/qgpgmesignjob.h>
41 #include <backends/qgpgme/qgpgmeverifydetachedjob.h>
42 #include <backends/qgpgme/qgpgmeverifyopaquejob.h>
43 #include <backends/qgpgme/qgpgmekeygenerationjob.h>
44 #include <backends/qgpgme/qgpgmeimportjob.h>
45 #include <backends/qgpgme/qgpgmeexportjob.h>
46 #include <backends/qgpgme/qgpgmesecretkeyexportjob.h>
47 #include <backends/qgpgme/qgpgmedownloadjob.h>
48 #include <backends/qgpgme/qgpgmedeletejob.h>
49 #include <backends/qgpgme/qgpgmesignencryptjob.h>
50 #include <backends/qgpgme/qgpgmedecryptverifyjob.h>
51 #include <backends/qgpgme/qgpgmecryptoconfig.h>
52 #include <backends/qgpgme/qgpgmerefreshkeysjob.h>
55 #include <qgpgme/dataprovider.h>
58 #include <gpgmepp/data.h>
59 #include <gpgmepp/importresult.h>
60 #include <gpgmepp/keygenerationresult.h>
64 #include <tdeapplication.h>
65 #include <tdelocale.h>
66 #include <tdeglobal.h>
67 #include <tdeconfig.h>
124 enum UnknownAttrsHandling { unknownAttrsHide,
133 _unknownAttrsHandling = unknownAttrsInfix;
134 _unknownAttrsHandlingChar =
"INFIX";
136 DNBeautifier( TDEConfig* config,
137 const TQString& cfgGroup,
138 const TQString& cfgAttributeOrderEntry,
139 const TQString& cfgUnknownAttrsEntry,
140 const TQStringList& fallbackAttrOrder = TQStringList(),
141 UnknownAttrsHandling fallbackUnknowAttrsHandling = unknownAttrsInfix )
143 _unknownAttrsHandling = unknownAttrsInfix;
144 _unknownAttrsHandlingChar =
"INFIX";
146 const TQString oldGroup( config->group() );
147 config->setGroup( cfgGroup );
149 config->readListEntry( cfgAttributeOrderEntry );
150 _unknownAttrsHandlingChar =
151 config->readEntry( cfgUnknownAttrsEntry ).upper().latin1();
152 config->setGroup( oldGroup );
153 if( _unknownAttrsHandlingChar ==
"HIDE" )
154 _unknownAttrsHandling = unknownAttrsHide;
155 else if( _unknownAttrsHandlingChar ==
"PREFIX" )
156 _unknownAttrsHandling = unknownAttrsPrefix;
157 else if( _unknownAttrsHandlingChar ==
"POSTFIX" )
158 _unknownAttrsHandling = unknownAttrsPostfix;
159 else if( _unknownAttrsHandlingChar ==
"INFIX" )
160 _unknownAttrsHandling = unknownAttrsInfix;
162 _unknownAttrsHandlingChar =
"INFIX";
164 if( _attrOrder.isEmpty() && ! fallbackAttrOrder.isEmpty() )
165 _attrOrder = fallbackAttrOrder;
167 if( _attrOrder.isEmpty() ){
170 _attrOrderChar =
new char*[ _attrOrder.count()+1 ];
172 for( TQStringList::ConstIterator itOrder = _attrOrder.begin();
173 itOrder != _attrOrder.end();
175 _attrOrderChar[ i ] = (
char*)malloc( ((*itOrder).length()+1)*
sizeof(char) );
176 strcpy( _attrOrderChar[ i ], (*itOrder).latin1() );
179 _attrOrderChar[ i ] = NULL;
185 for( TQStringList::ConstIterator itOrder = _attrOrder.begin();
186 itOrder != _attrOrder.end();
188 free( _attrOrderChar[ i ] );
191 delete[] _attrOrderChar;
194 TQStringList attrOrder()
const
198 char** attrOrderChar()
200 return _attrOrderChar;
203 UnknownAttrsHandling unknownAttrsHandling()
const
205 return _unknownAttrsHandling;
207 const char* unknownAttrsHandlingChar()
const
209 return _unknownAttrsHandlingChar;
212 TQValueList< TQPair<TQString,TQString> > reorder(
const TQValueList< TQPair<TQString,TQString> > & dn )
const
214 return reorder( dn, _attrOrder, _unknownAttrsHandling );
218 static TQValueList< TQPair<TQString,TQString> > reorder(
219 const TQValueList< TQPair<TQString,TQString> > & dn,
220 TQStringList attrOrder,
221 UnknownAttrsHandling unknownAttrsHandling )
223 if( !attrOrder.isEmpty() ){
224 TQPtrList< TQPair<TQString,TQString> > unknownEntries;
225 TQValueList< TQPair<TQString,TQString> > dnNew;
227 TQPair<TQString,TQString>* unknownEntry;
228 TQStringList::ConstIterator itOrder;
229 TQValueList< TQPair<TQString,TQString> >::ConstIterator itDN;
232 if( unknownAttrsHandling != unknownAttrsHide ){
234 for( itDN = dn.begin(); itDN != dn.end(); ++itDN ){
236 for( itOrder = attrOrder.begin(); itOrder != attrOrder.end(); ++itOrder ){
237 if( (*itOrder) == (*itDN).first ){
243 unknownEntries.append( &(*itDN) );
248 if( unknownAttrsHandling == unknownAttrsPrefix ){
249 for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
250 dnNew << *unknownEntry;
255 bool b_X_declared =
false;
256 for( itOrder = attrOrder.begin(); itOrder != attrOrder.end(); ++itOrder ){
257 if( (*itOrder) ==
"_X_" ){
260 if( unknownAttrsHandling == unknownAttrsInfix ){
261 for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
262 dnNew << *unknownEntry;
266 for( itDN = dn.begin(); itDN != dn.end(); ++itDN ){
267 if( (*itOrder) == (*itDN).first ){
276 if( unknownAttrsHandling == unknownAttrsPostfix ||
277 ( unknownAttrsHandling == unknownAttrsInfix && ! b_X_declared ) ){
278 for( unknownEntry = unknownEntries.first(); unknownEntry; unknownEntry = unknownEntries.next() ){
279 dnNew << *unknownEntry;
289 TQStringList _attrOrder;
290 char** _attrOrderChar;
291 UnknownAttrsHandling _unknownAttrsHandling;
292 TQCString _unknownAttrsHandlingChar;
302 : _initDone( false ), _wrapper( wrapper )
306 StructuringInfoWrapper::~StructuringInfoWrapper()
310 void StructuringInfoWrapper::reset()
315 void StructuringInfoWrapper::initMe()
317 if ( _wrapper && _wrapper->cryptPlug() ) {
318 _wrapper->cryptPlug()->init_StructuringInfo( &data );
322 void StructuringInfoWrapper::freeMe()
324 if( _wrapper && _wrapper->cryptPlug() && _initDone ) {
325 _wrapper->cryptPlug()->free_StructuringInfo( &data );
330 class CryptPlugWrapper::Config {
332 Config( gpgme_protocol_t proto );
335 const char* signatureKeyCertificate;
336 SignatureAlgorithm signatureAlgorithm;
337 SignatureCompoundMode signatureCompoundMode;
338 SendCertificates sendCertificates;
339 bool saveSentSignatures;
340 bool warnNoCertificate;
341 bool signatureUseCRLs;
342 EncryptionAlgorithm encryptionAlgorithm;
343 EncryptEmail encryptEmail;
344 bool saveMessagesEncrypted;
345 bool encryptionUseCRLs;
346 bool encryptionCRLExpiryNearWarning;
347 int encryptionCRLNearExpiryInterval;
348 CertificateSource certificateSource;
349 bool warnSendUnsigned;
350 bool signatureCertificateExpiryNearWarning;
351 int signatureCertificateExpiryNearInterval;
352 bool cACertificateExpiryNearWarning;
353 int cACertificateExpiryNearInterval;
354 bool rootCertificateExpiryNearWarning;
355 int rootCertificateExpiryNearInterval;
356 bool warnSendUnencrypted;
357 bool checkCertificatePath;
358 bool receiverCertificateExpiryNearWarning;
359 int receiverCertificateExpiryNearWarningInterval;
360 bool certificateInChainExpiryNearWarning;
361 int certificateInChainExpiryNearWarningInterval;
362 bool receiverEmailAddressNotInCertificateWarning;
363 const char* libVersion;
366 static const int NEAR_EXPIRY = 14;
368 CryptPlugWrapper::Config::Config( gpgme_protocol_t proto )
370 signatureAlgorithm = SignAlg_SHA1;
371 if ( proto == GPGME_PROTOCOL_CMS )
372 signatureCompoundMode = SignatureCompoundMode_Opaque;
374 signatureCompoundMode = SignatureCompoundMode_Detached;
375 sendCertificates = SendCert_SendChainWithRoot;
376 saveSentSignatures =
true;
377 warnNoCertificate =
true;
378 signatureUseCRLs =
true;
379 encryptionAlgorithm = EncryptAlg_RSA;
380 encryptEmail = EncryptEmail_Ask;
381 saveMessagesEncrypted =
true;
382 encryptionUseCRLs =
true;
383 encryptionCRLExpiryNearWarning =
false;
384 encryptionCRLNearExpiryInterval = NEAR_EXPIRY;
385 certificateSource = CertSrc_Server;
386 warnSendUnsigned =
true;
387 signatureCertificateExpiryNearWarning =
true;
388 signatureCertificateExpiryNearInterval = NEAR_EXPIRY;
389 cACertificateExpiryNearWarning =
true;
390 cACertificateExpiryNearInterval = NEAR_EXPIRY;
391 rootCertificateExpiryNearWarning =
true;
392 rootCertificateExpiryNearInterval = NEAR_EXPIRY;
393 warnSendUnencrypted =
false;
394 checkCertificatePath =
true;
395 receiverCertificateExpiryNearWarning =
true;
396 receiverCertificateExpiryNearWarningInterval = NEAR_EXPIRY;
397 certificateInChainExpiryNearWarning =
true;
398 certificateInChainExpiryNearWarningInterval = NEAR_EXPIRY;
399 receiverEmailAddressNotInCertificateWarning =
true;
400 libVersion = gpgme_check_version (NULL);
403 CryptPlugWrapper::Config::~Config() {
408 TQString CryptPlugWrapper::errorIdToText(
int errId,
bool & isPassphraseError ) {
409 const GpgME::Error err( errId );
410 isPassphraseError = err.isCanceled()
411 || gpgme_err_code( errId ) == GPG_ERR_NO_SECKEY ;
412 return TQString::fromLocal8Bit( err.asString() );
419 const TQString& libName,
420 const TQString& update,
422 : Kleo::CryptoBackend::Protocol(),
425 _updateURL( update ),
427 _iniStatus( IniStatus_undef ),
432 const bool ok = initialize( 0, 0 );
469 TQString CryptPlugWrapper::protocol()
const
471 if ( _libName.contains(
"smime" ) )
473 if ( _libName.contains(
"openpgp" ) )
486 if ( !_name.isEmpty() )
488 if ( _libName.contains(
"smime" ) )
490 if ( _libName.contains(
"openpgp" ) )
492 return i18n(
"(Unknown Protocol)");
495 bool CryptPlugWrapper::initialize( IniStatus* iniStatus, TQString* errorMsg )
500 _iniStatus = IniStatus_undef;
502 if ( _libName.isEmpty() ) {
503 _iniStatus = IniStatus_NoLibName;
504 kdDebug(5150) <<
"No library name was given.\n" << endl;
506 if ( _libName.contains(
"smime" ) ) {
507 _cp =
new SMIMECryptPlug();
508 _config =
new Config( GPGME_PROTOCOL_CMS );
509 }
else if ( _libName.contains(
"openpgp" ) ) {
510 _cp =
new OpenPGPCryptPlug();
511 _config =
new Config( GPGME_PROTOCOL_OpenPGP );
518 _iniStatus = IniStatus_LoadError;
519 kdDebug(5150) <<
"Couldn't create '" << _libName.latin1() <<
"'" << endl;
522 if( !_cp->initialize() ) {
523 _iniStatus = IniStatus_InitError;
524 kdDebug(5150) <<
"Error while executing function 'initialize' on plugin " << _libName << endl;
525 _lastError = i18n(
"Error while initializing plugin \"%1\"").arg( _libName );
527 *errorMsg = _lastError;
529 delete _config; _config = 0;
531 _iniStatus = IniStatus_Ok;
537 return _iniStatus == IniStatus_Ok;
545 delete _config; _config = 0;
546 delete _cryptoConfig; _cryptoConfig = 0;
553 *errorMsg = _lastError;
560 return _cp && _cp->hasFeature( flag );
567 const char* signaturetext,
568 bool signatureIsBinary,
570 CryptPlug::SignatureMetaData* sigmeta )
572 DNBeautifier dnBeautifier( kapp->config(),
575 "UnknownAttributes" );
576 return _cp && _cp->checkMessageSignature( cleartext,
581 dnBeautifier.attrOrderChar(),
582 dnBeautifier.unknownAttrsHandlingChar() );
590 const char* certificate,
594 return _cp && _cp->decryptMessage( ciphertext, cipherIsBinary, cipherLen,
595 (
const char**)cleartext, certificate, errId, errTxt );
600 const char* ciphertext,
604 const char* certificate,
605 bool* signatureFound,
606 CryptPlug::SignatureMetaData* sigmeta,
610 DNBeautifier dnBeautifier( kapp->config(),
613 "UnknownAttributes" );
614 return _cp && _cp->decryptAndCheckMessage( ciphertext,
617 (
const char**)cleartext,
623 dnBeautifier.attrOrderChar(),
624 dnBeautifier.unknownAttrsHandlingChar() );
630 void CryptPlugWrapper::freeSignatureMetaData( CryptPlug::SignatureMetaData* sigmeta )
634 free( sigmeta->status );
635 for(
int i = 0; i < sigmeta->extended_info_count; ++i ) {
636 free( sigmeta->extended_info[i].creation_time );
637 free( (
void*)sigmeta->extended_info[i].status_text );
638 free( (
void*)sigmeta->extended_info[i].keyid );
639 free( (
void*)sigmeta->extended_info[i].fingerprint );
640 free( (
void*)sigmeta->extended_info[i].algo );
641 free( (
void*)sigmeta->extended_info[i].userid );
642 free( (
void*)sigmeta->extended_info[i].name );
643 free( (
void*)sigmeta->extended_info[i].comment );
644 if( sigmeta->extended_info[i].emailCount ){
645 for(
int j=0; j < sigmeta->extended_info[i].emailCount; ++j)
646 if( sigmeta->extended_info[i].emailList[j] )
647 free( (
void*)sigmeta->extended_info[i].emailList[j] );
648 free( (
void*)sigmeta->extended_info[i].emailList );
651 free( sigmeta->extended_info );
654 GpgME::ImportResult CryptPlugWrapper::importCertificate(
const char* data,
size_t length )
657 return GpgME::ImportResult();
660 return _cp->importCertificateFromMem( data, length );
663 Kleo::KeyListJob * CryptPlugWrapper::keyListJob(
bool remote,
bool includeSigs,
bool validate )
const {
667 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
671 unsigned int mode = context->keyListMode();
673 mode |= GpgME::Context::Extern;
674 mode &= ~GpgME::Context::Local;
676 mode |= GpgME::Context::Local;
677 mode &= ~GpgME::Context::Extern;
679 if ( includeSigs ) mode |= GpgME::Context::Signatures;
680 if ( validate ) mode |= GpgME::Context::Validate;
681 context->setKeyListMode( mode );
682 return new Kleo::QGpgMEKeyListJob( context );
685 Kleo::EncryptJob * CryptPlugWrapper::encryptJob(
bool armor,
bool textmode )
const {
689 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
693 context->setArmor( armor );
694 context->setTextMode( textmode );
695 return new Kleo::QGpgMEEncryptJob( context );
702 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
706 return new Kleo::QGpgMEDecryptJob( context );
709 Kleo::SignJob * CryptPlugWrapper::signJob(
bool armor,
bool textMode )
const {
713 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
717 context->setArmor( armor );
718 context->setTextMode( textMode );
720 return new Kleo::QGpgMESignJob( context );
727 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
731 context->setTextMode( textMode );
733 return new Kleo::QGpgMEVerifyDetachedJob( context );
740 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
744 context->setTextMode( textMode );
746 return new Kleo::QGpgMEVerifyOpaqueJob( context );
753 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
757 return new Kleo::QGpgMEKeyGenerationJob( context );
764 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
768 return new Kleo::QGpgMEImportJob( context );
771 Kleo::ExportJob * CryptPlugWrapper::publicKeyExportJob(
bool armor )
const {
775 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
779 context->setArmor( armor );
780 return new Kleo::QGpgMEExportJob( context );
783 Kleo::ExportJob * CryptPlugWrapper::secretKeyExportJob(
bool armor,
const TQString& charset )
const {
784 if ( !_cp || _cp->mProtocol != GpgME::Context::CMS )
788 return new Kleo::QGpgMESecretKeyExportJob( armor, charset );
792 if ( !_cp || _cp->mProtocol != GpgME::Context::CMS )
796 return new Kleo::QGpgMERefreshKeysJob();
803 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
807 context->setArmor( armor );
809 context->setKeyListMode( GpgME::Context::Extern );
811 return new Kleo::QGpgMEDownloadJob( context );
818 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
822 return new Kleo::QGpgMEDeleteJob( context );
829 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
833 context->setArmor( armor );
834 context->setTextMode( textMode );
836 return new Kleo::QGpgMESignEncryptJob( context );
843 GpgME::Context * context = GpgME::Context::createForProtocol( _cp->mProtocol );
847 context->setTextMode( textMode );
849 return new Kleo::QGpgMEDecryptVerifyJob( context );
This class provides C++ access to the CRYPTPLUG API.
An abstract base class for asynchronous decrypters.
An abstract base class for asynchronous combined decrypters and verifiers.
An abstract base class for asynchronous deleters.
An abstract base class for asynchronous downloaders.
An abstract base class for asynchronous encrypters.
An abstract base class for asynchronous exporters.
An abstract base class for asynchronous importers.
An abstract base class for asynchronous key generation.
An abstract base class for asynchronous key listers.
An abstract base class for asynchronous key refreshers.
An abstract base class for asynchronous combined signing and encrypting.
An abstract base class for asynchronous signing.
An abstract base class for asynchronous verification of detached signatures.
An abstract base class for asynchronous verification of opaque signatures.
Common API header for CRYPTPLUG.
C++ wrapper for the CRYPTPLUG library API.
TQString libName() const
Returns the CRYPTPLUG library name.
~CryptPlugWrapper()
Destructor of CRYPTPLUG wrapper class.
bool setLibName(const TQString &libName)
Set the CRYPTPLUG library name.
void setActive(bool active)
Set this CRYPTPLUG wrapper's internal active flag.
CryptPlugWrapper(const TQString &name=TQString(), const TQString &libName=TQString(), const TQString &update=TQString(), bool active=false)
Constructor of CRYPTPLUG wrapper class.
bool active() const
Returns this CRYPTPLUG wrapper's internal active flag.
TQString displayName() const
Returns the external name.
void setDisplayName(const TQString &name)
Specifies the external name that is visible in lists, messages, etc.
bool decryptMessage(const char *ciphertext, bool cipherIsBinary, int cipherLen, char **cleartext, const char *certificate, int *errId, char **errTxt)
Tries to decrypt an email message ciphertext and returns the decrypted message in cleartext.
bool decryptAndCheckMessage(const char *ciphertext, bool cipherIsBinary, int cipherLen, char **cleartext, const char *certificate, bool *signatureFound, CryptPlug::SignatureMetaData *sigmeta, int *errId, char **errTxt)
Combines the functionality of checkMessageSignature() and decryptMessage().
IniStatus
Current initialization state.
IniStatus iniStatus(TQString *errorMsg) const
Returns this CRYPTPLUG wrapper's initialization state.
void deinitialize()
This function unloads the lib and frees all internal structures.
bool hasFeature(::Feature)
This function returns true if the specified feature is available in the plugin, and false otherwise.
bool checkMessageSignature(char **cleartext, const char *signaturetext, bool signatureIsBinary, int signatureLen, CryptPlug::SignatureMetaData *sigmeta)
Checks whether the signature of a message is valid.