25 #include <kmessagebox.h>
26 #include <kconfigbase.h>
27 #include <kconfiggroup.h>
33 #include <QApplication>
48 #include <sys/socket.h>
49 #include <sys/types.h>
60 ModuleStatic() : kpgpObject( 0 ) {}
61 ~ModuleStatic() {
delete kpgpObject; }
65 K_GLOBAL_STATIC( ModuleStatic, s_module )
69 mPublicKeysCached(
false),
71 mSecretKeysCached(
false),
72 passphrase(0), passphrase_buffer_len(0), havePassPhrase(
false)
76 config =
new KConfig(QLatin1String(
"kpgprc"));
85 if (!s_module.isDestroyed() && s_module->kpgpObject ==
this)
86 s_module->kpgpObject = 0;
120 KConfigGroup grp(config, QString());
121 storePass = grp.readEntry(
"storePass",
false);
122 showEncryptionResult = grp.readEntry(
"showEncryptionResult",
true);
123 mShowKeyApprovalDlg = grp.readEntry(
"showKeysForApproval",
true );
128 flagEncryptToSelf = grp.readEntry(
"encryptToSelf",
true);
134 KConfigGroup grp(config, QString());
135 grp.writeEntry(
"storePass", storePass);
136 grp.writeEntry(
"showEncryptionResult", showEncryptionResult);
137 grp.writeEntry(
"showKeysForApproval", mShowKeyApprovalDlg );
139 grp.writeEntry(
"encryptToSelf", flagEncryptToSelf);
153 if (pgpUser != keyID) {
169 flagEncryptToSelf = flag;
175 return flagEncryptToSelf;
192 Module::prepare(
bool needPassPhrase,
Block* block )
194 if (0 == pgp) assignPGPBase();
198 errMsg = i18n(
"Could not find PGP executable.\n"
199 "Please check your PATH is set correctly.");
206 if(needPassPhrase && !havePassPhrase) {
207 if( (
tGPG ==
pgpType ) && ( 0 != getenv(
"GPG_AGENT_INFO") ) ) {
209 kDebug( 5326 ) <<
"user uses gpg-agent -> don't ask for passphrase";
211 setPassPhrase( QLatin1String(
"dummy") );
217 PassphraseDialog passdlg(0, i18n(
"OpenPGP Security Check"), ID);
219 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
221 int passdlgResult = passdlg.exec();
223 QApplication::restoreOverrideCursor();
225 if (passdlgResult == QDialog::Accepted) {
226 if (!setPassPhrase(passdlg.passphrase())) {
227 if ( passdlg.passphrase().length() >= 1024)
228 errMsg = i18n(
"Passphrase is too long, it must contain fewer than 1024 characters.");
230 errMsg = i18n(
"Out of memory.");
243 Module::wipePassPhrase(
bool freeMem)
246 if ( passphrase_buffer_len )
247 memset( passphrase, 0x00, passphrase_buffer_len );
249 kDebug( 5326 ) <<
"wipePassPhrase: passphrase && !passphrase_buffer_len ???";
253 if ( freeMem && passphrase ) {
256 passphrase_buffer_len = 0;
258 havePassPhrase =
false;
266 if (0 == pgp) assignPGPBase();
269 if( !prepare(
false, &block ) )
272 retval = pgp->
verify( block );
287 if (0 == pgp) assignPGPBase();
292 if( prepare(
true, &block ) != 1 )
295 retval = pgp->
decrypt( block, passphrase );
300 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
302 int ret = KMessageBox::warningContinueCancel(0,
303 i18n(
"You just entered an invalid passphrase.\n"
304 "Do you want to try again, or "
305 "cancel and view the message undecrypted?"),
306 i18n(
"PGP Warning"), KGuiItem(i18n(
"&Retry")));
308 QApplication::restoreOverrideCursor();
310 if ( ret == KMessageBox::Cancel )
break;
328 const KeyID& keyId,
const QByteArray& charset )
330 return encrypt( block, QStringList(), keyId,
true, charset );
335 const QStringList& receivers,
const KeyID& keyId,
336 bool sign,
const QByteArray& charset )
340 errMsg = QLatin1String(
"");
342 if( 0 == pgp ) assignPGPBase();
346 if( !receivers.empty() ) {
354 status =
doEncSign( block, encryptionKeyIds, sign );
362 QString str = i18n(
"You entered an invalid passphrase.\n"
363 "Do you want to try again, continue and leave the "
364 "message unsigned, or cancel sending the message?");
366 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
368 int ret = KMessageBox::warningYesNoCancel( 0, str,
370 KGuiItem(i18n(
"&Retry")),
371 KGuiItem(i18n(
"Send &Unsigned")) );
373 QApplication::restoreOverrideCursor();
375 if( ret == KMessageBox::Cancel ) {
378 if( ret == KMessageBox::No ) {
380 if( encryptionKeyIds.isEmpty() ) {
389 status =
doEncSign( block, encryptionKeyIds, sign );
394 QString str = i18nc(
"%1 = 'signing failed' error message",
395 "%1\nDo you want to send the message unsigned, "
396 "or cancel sending the message?",
399 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
401 int ret = KMessageBox::warningContinueCancel( 0, str,
403 KGuiItem(i18n(
"Send &Unsigned")) );
405 QApplication::restoreOverrideCursor();
407 if( ret == KMessageBox::Cancel ) {
411 status =
doEncSign( block, encryptionKeyIds, sign );
416 QString str = i18nc(
"%1 = 'bad keys' error message",
417 "%1\nDo you want to encrypt anyway, leave the "
418 "message as-is, or cancel sending the message?",
422 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
424 int ret = KMessageBox::warningYesNoCancel( 0, str,
426 KGuiItem(i18n(
"Send &Encrypted")),
427 KGuiItem(i18n(
"Send &Unencrypted")) );
429 QApplication::restoreOverrideCursor();
431 if( ret == KMessageBox::Cancel ) {
434 if( ret == KMessageBox::No ) {
447 QString str = i18nc(
"%1 = 'missing keys' error message",
448 "%1\nDo you want to leave the message as-is, "
449 "or cancel sending the message?",
452 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
454 int ret = KMessageBox::warningContinueCancel( 0, str,
456 KGuiItem(i18n(
"&Send As-Is")) );
458 QApplication::restoreOverrideCursor();
460 if( ret == KMessageBox::Cancel ) {
469 errMsg = i18n(
"The following error occurred:\n%1" ,
471 QString details = i18n(
"This is the error message of %1:\n%2" ,
472 (
pgpType ==
tGPG ) ? QLatin1String(
"GnuPG") : QLatin1String(
"PGP") ,
473 QLatin1String(block.
error().data()) );
475 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
477 KMessageBox::detailedSorry( 0, errMsg, details );
479 QApplication::restoreOverrideCursor();
488 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
490 bool result = ( cipherTextDlg->exec() == QDialog::Accepted );
492 QApplication::restoreOverrideCursor();
494 delete cipherTextDlg;
502 const KeyIDList& recipientKeyIds,
bool sign )
506 if( 0 == pgp ) assignPGPBase();
509 if( !havePgp )
return OK;
512 int result = prepare(
true, &block );
519 retval = pgp->
encsign( block, recipientKeyIds, passphrase );
522 if( !prepare(
false, &block ) )
return Kpgp::ERROR;
523 retval = pgp->
encrypt( block, recipientKeyIds );
533 const QStringList& recipients,
536 if( recipients.empty() ) {
537 encryptionKeyIds.clear();
543 QVector<KeyIDList> recipientKeyIds( recipients.count() + 1 );
551 bool showKeysForApproval =
false;
553 for( QStringList::ConstIterator it = recipients.constBegin();
554 it != recipients.constEnd(); ++it, ++i ) {
557 showKeysForApproval =
true;
560 if( keyIds.isEmpty() ) {
561 showKeysForApproval =
true;
563 recipientKeyIds[i] = keyIds;
566 kDebug( 5326 ) <<
"recipientKeyIds = (";
567 QVector<KeyIDList>::const_iterator kit;
568 for( kit = recipientKeyIds.constBegin(); kit != recipientKeyIds.constEnd(); ++kit ) {
569 kDebug( 5326 ) <<
"( 0x" << (*kit).
toStringList().join(QLatin1String(
", 0x") )
572 kDebug( 5326 ) <<
")";
574 if( showKeysForApproval || mShowKeyApprovalDlg ) {
589 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
591 int ret = dlg.exec();
593 if( ret == QDialog::Rejected ) {
595 QApplication::restoreOverrideCursor();
600 recipientKeyIds = dlg.
keys();
602 QApplication::restoreOverrideCursor();
607 int emptyListCount = 0;
608 for( QVector<KeyIDList>::const_iterator it = recipientKeyIds.constBegin();
609 it != recipientKeyIds.constEnd(); ++it ) {
610 if( (*it).isEmpty() ) {
612 if( it != recipientKeyIds.constBegin() ) {
617 for( KeyIDList::ConstIterator kit = (*it).constBegin();
618 kit != (*it).constEnd(); kit++ ) {
619 encryptionKeyIds.append( *kit );
628 if( recipientKeyIds.size() == emptyListCount + 1 ) {
629 QString str = ( recipients.count() == 1 )
630 ? i18n(
"You did not select an encryption key for the "
631 "recipient of this message; therefore, the message "
632 "will not be encrypted.")
633 : i18n(
"You did not select an encryption key for any of the "
634 "recipients of this message; therefore, the message "
635 "will not be encrypted.");
637 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
639 int ret = KMessageBox::warningContinueCancel( 0, str,
641 KGuiItem(i18n(
"Send &Unencrypted")) );
643 QApplication::restoreOverrideCursor();
645 if( ret == KMessageBox::Cancel ) {
649 encryptionKeyIds.clear();
651 else if( emptyListCount > 0 ) {
652 QString str = ( emptyListCount == 1 )
653 ? i18n(
"You did not select an encryption key for one of "
654 "the recipients; this person will not be able to "
655 "decrypt the message if you encrypt it.")
656 : i18n(
"You did not select encryption keys for some of "
657 "the recipients; these persons will not be able to "
658 "decrypt the message if you encrypt it." );
660 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
662 int ret = KMessageBox::warningYesNoCancel( 0, str,
664 KGuiItem(i18n(
"Send &Encrypted")),
665 KGuiItem(i18n(
"Send &Unencrypted")) );
667 QApplication::restoreOverrideCursor();
669 if( ret == KMessageBox::Cancel ) {
672 else if( ret == KMessageBox::No ) {
674 encryptionKeyIds.clear();
684 if( 0 == pgp ) assignPGPBase();
689 if( recipients.empty() )
692 int noKey = 0, never = 0, unknown = 0, always = 0, aip = 0, ask = 0,
694 for( QStringList::ConstIterator it = recipients.constBegin();
695 it != recipients.constEnd(); ++it) {
696 if( haveTrustedEncryptionKey( *it ) ) {
724 if( ( always+aip > 0 ) && ( never+unknown+ask+askwp+noKey == 0 ) ) {
728 if( ( unknown+ask+askwp > 0 ) && ( never+noKey == 0 ) ) {
732 if( ( never+noKey > 0 ) && ( always+ask == 0 ) ) {
742 if (0 == pgp) assignPGPBase();
744 if( prepare(
true ) != 1 )
758 if (0 == pgp) assignPGPBase();
760 if (!prepare())
return KeyList();
762 if( !mPublicKeysCached ) {
773 if (0 == pgp) assignPGPBase();
775 if (!prepare())
return KeyList();
777 if( !mSecretKeysCached ) {
790 foreach (
Key* key, mPublicKeys ) {
804 foreach (
Key* key, mPublicKeys ) {
817 foreach (
Key* key, mSecretKeys ) {
868 if( 0 == pgp ) assignPGPBase();
875 if( ( 0 == oldKey ) && ( 0 != newKey ) )
877 KeyList::Iterator it = std::lower_bound( mPublicKeys.begin(), mPublicKeys.end(), newKey,
KeyCompare );
878 mPublicKeys.insert( it, newKey );
879 kDebug( 5326 ) <<
"New public key 0x" << newKey->
primaryKeyID() <<
" ("
882 else if( ( 0 != oldKey ) && ( 0 == newKey ) )
884 kDebug( 5326 ) <<
"Public key 0x" << oldKey->
primaryKeyID() <<
" ("
886 mPublicKeys.removeAll( oldKey );
895 if (0 == pgp) assignPGPBase();
901 bool Module::setPassPhrase(
const QString& aPass)
909 size_t newlen = aPass.length();
910 if ( newlen >= 1024 ) {
916 if ( passphrase_buffer_len < newlen + 1 ) {
921 passphrase_buffer_len = (newlen + 1 + 15) & ~0xF;
922 passphrase = (
char*)malloc( passphrase_buffer_len );
924 passphrase_buffer_len = 0;
928 memcpy( passphrase, aPass.toLocal8Bit().data(), newlen + 1 );
929 havePassPhrase =
true;
938 KMessageBox::information(0,i18n(
"This feature is\nstill missing"));
946 wipePassPhrase(
true);
964 showEncryptionResult = flag;
970 return showEncryptionResult;
986 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
987 "or you chose not to use GnuPG/PGP.") );
994 const QString& text ,
995 const KeyID& oldKeyId ,
996 const QString& address ,
997 const unsigned int allowedKeys )
1006 if( address.isEmpty() ) {
1007 keyId = selectKey(
publicKeys(), title, text, oldKeyId, allowedKeys );
1010 bool rememberChoice;
1011 keyId = selectKey( rememberChoice,
publicKeys(), title, text, oldKeyId,
1013 if( !keyId.isEmpty() && rememberChoice ) {
1014 setKeysForAddress( address,
KeyIDList( keyId ) );
1021 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
1022 "or you chose not to use GnuPG/PGP.") );
1030 const QString& text ,
1032 const QString& address ,
1033 const unsigned int allowedKeys )
1042 if( address.isEmpty() ) {
1043 keyIds = selectKeys(
publicKeys(), title, text, oldKeyIds, allowedKeys );
1046 bool rememberChoice;
1047 keyIds = selectKeys( rememberChoice,
publicKeys(), title, text,
1048 oldKeyIds, allowedKeys );
1049 if( !keyIds.isEmpty() && rememberChoice ) {
1050 setKeysForAddress( address, keyIds );
1057 KMessageBox::sorry( 0, i18n(
"You either do not have GnuPG/PGP installed "
1058 "or you chose not to use GnuPG/PGP.") );
1069 if (!s_module->kpgpObject)
1071 s_module->kpgpObject =
new Module();
1073 return s_module->kpgpObject;
1094 nonPgpBlocks.clear();
1098 nonPgpBlocks.append(
"" );
1102 if( !strncmp( msg.data(),
"-----BEGIN PGP ", 15 ) )
1106 start = msg.indexOf(
"\n-----BEGIN PGP" ) + 1;
1109 nonPgpBlocks.append( msg );
1114 while( start != -1 )
1116 int nextEnd, nextStart;
1119 if( !strncmp( msg.data() + start + 15,
"SIGNED", 6 ) )
1124 nextEnd = msg.indexOf(
"\n-----END PGP", start + 15 );
1127 nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
1130 nextStart = msg.indexOf(
"\n-----BEGIN PGP", start + 15 );
1132 if( ( nextStart == -1 ) || ( nextEnd < nextStart ) ||
1136 nonPgpBlocks.append( msg.mid( lastEnd+1, start-lastEnd-1 ) );
1137 lastEnd = msg.indexOf(
"\n", nextEnd + 14 );
1140 pgpBlocks.append(
Block( msg.mid( start ) ) );
1141 nonPgpBlocks.append(
"" );
1146 pgpBlocks.append(
Block( msg.mid( start, lastEnd+1-start ) ) );
1147 if( ( nextStart != -1 ) && ( nextEnd > nextStart ) )
1148 nextStart = msg.indexOf(
"\n-----BEGIN PGP", lastEnd+1 );
1154 nonPgpBlocks.append( msg.mid( lastEnd+1 ) );
1159 return ( !pgpBlocks.isEmpty() );
1166 Module::haveTrustedEncryptionKey(
const QString& person )
1168 if( 0 == pgp ) assignPGPBase();
1170 if( !
usePGP() )
return false;
1174 QString address = canonicalAddress( person ).toLower();
1177 KeyIDList keyIds = keysForAddress( address );
1178 if( !keyIds.isEmpty() ) {
1180 for( KeyIDList::ConstIterator it = keyIds.constBegin();
1181 it != keyIds.constEnd(); ++it ) {
1192 KeyList::Iterator it = mPublicKeys.begin();
1195 for(; it != mPublicKeys.end(); ++it ) {
1197 if( (*it)->matchesUserID( person,
false ) ) {
1200 if( ( (*it)->isValidEncryptionKey() ) &&
1209 for( it = mPublicKeys.begin(); it != mPublicKeys.end(); ++it ) {
1211 if( (*it)->matchesUserID( address,
false ) ) {
1214 if( ( (*it)->isValidEncryptionKey() ) &&
1228 if( 0 == pgp ) assignPGPBase();
1230 if( !
usePGP() )
return KeyIDList();
1234 QString address = canonicalAddress( person ).toLower();
1248 KeyIDList keyIds = keysForAddress( address );
1249 if( !keyIds.isEmpty() ) {
1250 kDebug( 5326 ) <<
"Using encryption keys 0x"
1251 << keyIds.toStringList().join( QLatin1String(
", 0x") )
1255 for( KeyIDList::ConstIterator it = keyIds.constBegin();
1256 it != keyIds.constEnd(); ++it ) {
1260 if( !( key && ( key->isValidEncryptionKey() ) &&
1268 bool rememberChoice;
1269 keyIds = selectKeys( rememberChoice, mPublicKeys,
1270 i18n(
"Encryption Key Selection"),
1271 i18nc(
"if in your language something like "
1272 "'key(s)' isn't possible please "
1273 "use the plural in the translation",
1274 "There is a problem with the "
1275 "encryption key(s) for \"%1\".\n\n"
1276 "Please re-select the key(s) which should "
1277 "be used for this recipient."
1281 if( !keyIds.isEmpty() ) {
1282 if( rememberChoice ) {
1283 setKeysForAddress( person, keyIds );
1291 KeyList::Iterator it = mPublicKeys.begin();
1295 kDebug( 5326 ) <<
"Looking for keys matching" << person <<
" ...";
1296 for( ; it != mPublicKeys.end(); ++it ) {
1298 if( (*it)->matchesUserID( person,
false ) ) {
1301 if( ( (*it)->isValidEncryptionKey() ) &&
1303 kDebug( 5326 ) <<
"Matching trusted key found:"
1304 << (*it)->primaryKeyID();
1305 matchingKeys.append( *it );
1312 kDebug( 5326 ) <<
"Looking for keys matching" << address <<
" ...";
1313 if( matchingKeys.isEmpty() ) {
1314 for ( it = mPublicKeys.begin(); it != mPublicKeys.end(); ++it ) {
1316 if( (*it)->matchesUserID( address,
false ) ) {
1319 if( ( (*it)->isValidEncryptionKey() ) &&
1321 kDebug( 5326 ) <<
"Matching trusted key found:"
1322 << (*it)->primaryKeyID();
1323 matchingKeys.append( *it );
1330 if( matchingKeys.isEmpty() ) {
1332 bool rememberChoice;
1333 KeyIDList keyIds = selectKeys( rememberChoice, mPublicKeys,
1334 i18n(
"Encryption Key Selection"),
1335 i18nc(
"if in your language something like "
1336 "'key(s)' isn't possible please "
1337 "use the plural in the translation",
1338 "No valid and trusted OpenPGP key was "
1339 "found for \"%1\".\n\n"
1340 "Select the key(s) which should "
1341 "be used for this recipient."
1345 if( !keyIds.isEmpty() ) {
1346 if( rememberChoice ) {
1347 setKeysForAddress( person, keyIds );
1353 else if( matchingKeys.count() == 1 ) {
1354 return KeyIDList( matchingKeys.first()->primaryKeyID() );
1358 bool rememberChoice;
1359 KeyIDList keyIds = selectKeys( rememberChoice, matchingKeys,
1360 i18n(
"Encryption Key Selection"),
1361 i18nc(
"if in your language something like "
1362 "'key(s)' isn't possible please "
1363 "use the plural in the translation",
1364 "More than one key matches \"%1\".\n\n"
1365 "Select the key(s) which should "
1366 "be used for this recipient."
1370 if( !keyIds.isEmpty() ) {
1371 if( rememberChoice ) {
1372 setKeysForAddress( person, keyIds );
1384 Module::checkForPGP(
void)
1388 QStringList pSearchPaths;
1392 path = QString::fromLocal8Bit( getenv(
"PATH") );
1393 pSearchPaths = path.split( QLatin1Char(KPATH_SEPARATOR), QString::SkipEmptyParts );
1398 foreach(
const QString& curPath, pSearchPaths )
1401 path += QLatin1String(
"/gpg");
1402 if ( QFileInfo(path).isExecutable() )
1404 kDebug( 5326 ) <<
"Kpgp: gpg found";
1413 foreach(
const QString& curPath, pSearchPaths )
1416 path += QLatin1String(
"/pgpe");
1417 if ( QFileInfo(path).isExecutable() )
1419 kDebug( 5326 ) <<
"Kpgp: pgp 5 found";
1428 foreach(
const QString& curPath, pSearchPaths )
1431 path += QLatin1String(
"/pgp");
1432 if ( QFileInfo(path).isExecutable() )
1434 kDebug( 5326 ) <<
"Kpgp: pgp 2 or 6 found";
1443 kDebug( 5326 ) <<
"Kpgp: no pgp found";
1450 Module::assignPGPBase(
void)
1460 kDebug( 5326 ) <<
"Kpgp: assign pgp - gpg";
1465 kDebug( 5326 ) <<
"Kpgp: assign pgp - pgp 2";
1470 kDebug( 5326 ) <<
"Kpgp: assign pgp - pgp 5";
1475 kDebug( 5326 ) <<
"Kpgp: assign pgp - pgp 6";
1481 kDebug( 5326 ) <<
"Kpgp: pgpBase is dummy";
1486 kDebug( 5326 ) <<
"Kpgp: assign pgp - auto";
1489 kDebug( 5326 ) <<
"Kpgp: assign pgp - default";
1492 kDebug( 5326 ) <<
"Kpgp: pgpBase is gpg";
1498 kDebug( 5326 ) <<
"Kpgp: pgpBase is pgp 5";
1504 Base6 *pgp_v6 =
new Base6();
1505 if (!pgp_v6->isVersion6())
1507 kDebug( 5326 ) <<
"Kpgp: pgpBase is pgp 2";
1514 kDebug( 5326 ) <<
"Kpgp: pgpBase is pgp 6";
1524 kDebug( 5326 ) <<
"Kpgp: pgpBase is dummy";
1531 Module::canonicalAddress(
const QString& _adress )
1535 QString address = _adress.simplified();
1536 address = address.trimmed();
1539 if((index = address.indexOf(QLatin1String(
"<"))) != -1)
1540 if((index2 = address.indexOf(QLatin1String(
"@"),index+1)) != -1)
1541 if((index2 = address.indexOf(QLatin1String(
">"),index2+1)) != -1)
1542 return address.mid(index,index2-index+1);
1544 if((index = address.indexOf(QLatin1String(
"@"))) == -1)
1550 return QLatin1Char(
'<') + address + QLatin1String(
"@localdomain>");
1554 int index1 = address.lastIndexOf(QLatin1String(
" "),index);
1555 int index2 = address.indexOf(QLatin1String(
" "),index);
1556 if(index2 == -1) index2 = address.length();
1557 return QLatin1Char(
'<') + address.mid(index1+1 ,index2-index1-1) + QLatin1Char(
'>');
1564 if( 0 == pgp ) assignPGPBase();
1568 qDeleteAll( mPublicKeys );
1569 mPublicKeys.clear();
1570 mPublicKeysCached =
false;
1574 if( !mPublicKeysCached || reread )
1576 if( mPublicKeys.isEmpty() )
1587 for( KeyList::Iterator it = newPublicKeyList.begin(); it != newPublicKeyList.end(); ++it )
1596 qDeleteAll( mPublicKeys );
1597 mPublicKeys = newPublicKeyList;
1600 mPublicKeysCached =
true;
1607 if( 0 == pgp ) assignPGPBase();
1611 qDeleteAll( mSecretKeys );
1612 mSecretKeys.clear();
1613 mSecretKeysCached =
false;
1617 if( mSecretKeys.isEmpty() || reread )
1619 if( mSecretKeys.isEmpty() )
1630 for( KeyList::Iterator it = newSecretKeyList.begin(); it != newSecretKeyList.end(); ++it )
1639 qDeleteAll( mSecretKeys );
1640 mSecretKeys = newSecretKeyList;
1643 mSecretKeysCached =
true;
1648 Module::selectKey(
const KeyList& keys,
1649 const QString& title,
1650 const QString& text ,
1651 const KeyID& keyId ,
1652 const unsigned int allowedKeys )
1656 #ifndef QT_NO_TREEWIDGET
1658 allowedKeys,
false );
1660 #ifndef QT_NO_CURSOR
1661 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
1663 bool rej = ( dlg.exec() == QDialog::Rejected );
1664 #ifndef QT_NO_CURSOR
1665 QApplication::restoreOverrideCursor();
1677 Module::selectKeys(
const KeyList& keys,
1678 const QString& title,
1679 const QString& text ,
1680 const KeyIDList& keyIds ,
1681 const unsigned int allowedKeys )
1683 KeyIDList retval = KeyIDList();
1685 #ifndef QT_NO_TREEWIDGET
1686 KeySelectionDialog dlg( keys, title, text, keyIds,
false, allowedKeys,
1689 #ifndef QT_NO_CURSOR
1690 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
1692 bool rej = ( dlg.exec() == QDialog::Rejected );
1693 #ifndef QT_NO_CURSOR
1694 QApplication::restoreOverrideCursor();
1698 retval = dlg.keys();
1707 Module::selectKey(
bool& rememberChoice,
1709 const QString& title,
1710 const QString& text ,
1711 const KeyID& keyId ,
1712 const unsigned int allowedKeys )
1716 #ifndef QT_NO_TREEWIDGET
1717 KeySelectionDialog dlg( keys, title, text, KeyIDList( keyId ),
false,
1718 allowedKeys,
false );
1720 #ifndef QT_NO_CURSOR
1721 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
1723 bool rej = ( dlg.exec() == QDialog::Rejected );
1724 #ifndef QT_NO_CURSOR
1725 QApplication::restoreOverrideCursor();
1730 rememberChoice = dlg.rememberSelection();
1733 rememberChoice =
false;
1741 Module::selectKeys(
bool& rememberChoice,
1743 const QString& title,
1744 const QString& text ,
1745 const KeyIDList& keyIds ,
1746 const unsigned int allowedKeys )
1748 KeyIDList retval = KeyIDList();
1750 #ifndef QT_NO_TREEWIDGET
1751 KeySelectionDialog dlg( keys, title, text, keyIds,
true, allowedKeys,
1754 #ifndef QT_NO_CURSOR
1755 QApplication::setOverrideCursor( QCursor(Qt::ArrowCursor) );
1757 bool rej = ( dlg.exec() == QDialog::Rejected );
1758 #ifndef QT_NO_CURSOR
1759 QApplication::restoreOverrideCursor();
1763 retval = dlg.keys();
1764 rememberChoice = dlg.rememberSelection();
1767 rememberChoice =
false;
1775 Module::keysForAddress(
const QString& address )
1777 if( address.isEmpty() ) {
1780 QString addr = canonicalAddress( address ).toLower();
1781 if( addressDataDict.contains( addr ) ) {
1782 return addressDataDict[addr].keyIds;
1790 Module::setKeysForAddress(
const QString& address,
const KeyIDList& keyIds )
1792 if( address.isEmpty() ) {
1795 QString addr = canonicalAddress( address ).toLower();
1796 if( addressDataDict.contains( addr ) ) {
1797 addressDataDict[addr].keyIds = keyIds;
1802 data.keyIds = keyIds;
1803 addressDataDict.insert( addr, data );
1810 Module::readAddressData()
1815 KConfigGroup general( config,
"General" );
1816 int num = general.readEntry(
"addressEntries", 0 );
1818 addressDataDict.clear();
1819 for(
int i=1; i<=num; ++i ) {
1820 KConfigGroup addrGroup( config, QString::fromLatin1(
"Address #%1").arg(i) );
1821 address = addrGroup.readEntry(
"Address" );
1823 data.encrPref = (
EncryptPref) addrGroup.readEntry(
"EncryptionPreference",
1828 if ( !address.isEmpty() ) {
1829 addressDataDict.insert( address, data );
1835 Module::writeAddressData()
1837 KConfigGroup general( config,
"General" );
1838 general.writeEntry(
"addressEntries", addressDataDict.count() );
1841 AddressDataDict::Iterator it;
1842 for ( i=1, it = addressDataDict.begin();
1843 it != addressDataDict.end();
1845 KConfigGroup addrGroup( config, QString::fromLatin1(
"Address #%1").arg(i));
1846 addrGroup.writeEntry(
"Address", it.key() );
1847 addrGroup.writeEntry(
"Key IDs", it.value().keyIds.toStringList() );
1848 addrGroup.writeEntry(
"EncryptionPreference", (
int)it.value().encrPref );
1857 QString addr = canonicalAddress( address ).toLower();
1858 if( addressDataDict.contains( addr ) ) {
1859 return addressDataDict[addr].encrPref;
1870 if( address.isEmpty() ) {
1873 QString addr = canonicalAddress( address ).toLower();
1874 if( addressDataDict.contains( addr ) ) {
1875 addressDataDict[addr].encrPref = pref;
1879 data.encrPref = pref;
1880 addressDataDict.insert( addr, data );
bool isValidEncryptionKey() const
Returns true if the key is a valid encryption key.
int encryptionPossible(const QStringList &recipients)
checks if encrypting to the given list of persons is possible and desired, i.e.
Validity
These are the possible validity values for a PGP user id and for the owner trust. ...
static KeyIDList fromStringList(const QStringList &)
Converts from a QStringList to a KeyIDList.
virtual KeyList publicKeys(const QStringList &=QStringList())
Returns the list of public keys in the users public keyring.
Key * secretKey(const KeyID &keyID)
Returns the secret key with the given key ID or null if no matching key is found. ...
virtual int encsign(Block &, const KeyIDList &, const char *=0)
Encrypts and signs the message with the given keys.
bool encryptToSelf(void) const
virtual int decrypt(Block &, const char *=0)
Decrypts the message.
static bool prepareMessageForDecryption(const QByteArray &msg, QList< Block > &pgpBlocks, QList< QByteArray > &nonPgpBlocks)
Parses the given message and splits it into OpenPGP blocks and Non-OpenPGP blocks.
virtual int verify(Block &block)
Verifies the message.
const KeyID user() const
Returns the actual key ID of the currently set key.
virtual QString lastErrorMessage() const
Returns an error message if an error occurred during the last operation.
void readPublicKeys(bool reread=false)
Reads the list of public keys if necessary or if reread is true.
void setUser(const KeyID &keyID)
set a user identity to use (if you have more than one...) by default, pgp uses the identity which was...
void setEncryptionPreference(const QString &address, const EncryptPref pref)
Writes the given encryption preference for the given address to the config file.
bool showCipherText(void) const
const QString lastErrorMsg(void) const
returns the last error that occurred
Kpgp::Result encrypt(Block &block, const QStringList &receivers, const KeyID &keyId, bool sign, const QByteArray &charset=0)
encrypts the given OpenPGP block for a list of persons.
virtual void readConfig()
the following virtual function form the interface to the application using Kpgp
KeyID selectSecretKey(const QString &title, const QString &text=QString(), const KeyID &keyId=KeyID())
Shows a key selection dialog with all secret keys and the given title and the (optional) text...
Key * rereadKey(const KeyID &keyID, const bool readTrust=true)
Rereads the key data for the given key and returns the reread data.
void setStorePassPhrase(bool)
store passphrase in pgp object Problem: passphrase stays in memory.
const KeyList publicKeys()
get the list of cached public keys.
static Kpgp::Module * getKpgp()
return the actual pgp object
const KeyList secretKeys()
get the list of cached secret keys.
virtual Key * readPublicKey(const KeyID &, const bool=false, Key *=0)
Reads the key data for the given key and returns it.
bool KeyCompare(Key *left, Key *right)
virtual int signKey(const KeyID &, const char *)
Signs the given key with the currently set user key.
KeyIDList selectPublicKeys(const QString &title, const QString &text=QString(), const KeyIDList &oldKeyIds=KeyIDList(), const QString &address=QString(), const unsigned int allowedKeys=AllKeys)
Shows a key selection dialog with all public keys and the given title and the (optional) text...
virtual QByteArray getAsciiPublicKey(const KeyID &)
Returns the ascii armored data of the public key with the given key id.
enum Kpgp::Module::PGPType pgpType
void setShowCipherText(const bool flag)
QString primaryUserID() const
Returns the primary user ID or a null string if there are no user IDs.
Validity keyTrust() const
Returns the trust value of this key.
Kpgp::Result clearsign(Block &block, const KeyID &keyId, const QByteArray &charset=0)
clearsigns the given OpenPGP block with the key corresponding to the given key id.
int doEncSign(Block &block, const KeyIDList &recipientKeyIds, bool sign)
void reset()
Resets all information about this OpenPGP block.
QVector< KeyIDList > keys() const
void setEncryptToSelf(bool flag)
always encrypt message to oneself?
EncryptPref encryptionPreference(const QString &address)
Reads the encryption preference for the given address from the config file.
bool verify(Block &block)
Tries to verify the given OpenPGP block.
virtual void writeConfig(bool sync)
bool changePassPhrase()
Request the change of the passphrase of the actual secret key.
Validity keyTrust(const KeyID &keyID)
Returns the trust value for the given key.
QByteArray primaryFingerprint() const
Returns the fingerprint of the primary key or a null string if there are no subkeys.
EncryptPref
These are the possible preferences for encryption.
virtual int encrypt(Block &, const KeyIDList &)
Encrypts the message with the given keys.
This class is used to store information about a PGP key.
KeyID selectPublicKey(const QString &title, const QString &text=QString(), const KeyID &oldKeyId=KeyID(), const QString &address=QString(), const unsigned int allowedKeys=AllKeys)
Shows a key selection dialog with all public keys and the given title and the (optional) text...
void clear(const bool erasePassPhrase=false)
clears everything from memory
Kpgp::Result getEncryptionKeys(KeyIDList &encryptionKeyIds, const QStringList &recipients, const KeyID &keyId)
Determines the keys which should be used for encrypting the message to the given list of recipients...
QByteArray getAsciiPublicKey(const KeyID &keyID)
try to get an ascii armored key block for the given public key
QStringList toStringList() const
Converts from a KeyIDList to a QStringList.
bool isTrusted(const KeyID &keyID)
Returns TRUE if the given key is at least trusted marginally.
Key * publicKey(const KeyID &keyID)
Returns the public key with the given key ID or null if no matching key is found. ...
static KConfig * getConfig()
get the kpgp config object
void readSecretKeys(bool reread=false)
Reads the list of secret keys if necessary or if reread is true.
QString requiredUserId() const
bool matchesUserID(const QString &str, bool cs=true)
Returns true if the given string matches one of the user IDs.
bool storePassPhrase(void) const
bool signKey(const KeyID &keyID)
sign a key in the keyring with users signature.
virtual KeyList secretKeys(const QStringList &=QStringList())
Returns the list of secret keys in the users secret keyring.
void cloneKeyTrust(const Key *key)
Set the validity values for the user ids to the validity values of the given key. ...
bool usePGP(void) const
Should PGP/GnuPG be used?
bool decrypt(Block &block)
decrypts the given OpenPGP block if the passphrase is good.
KeyID primaryKeyID() const
Returns the key ID of the primary key or a null string if there are no subkeys.