39 int eol =
output.indexOf(
'\n' );
41 int pos =
output.lastIndexOf(
' ', eol - 1 );
44 kDebug( 5326 ) <<
"found GnuPG" <<
mVersion;
58 return encsign( block, recipients, 0 );
71 const char *passphrase )
76 if(!recipients.isEmpty() && passphrase != 0)
77 cmd =
"--batch --armor --sign --encrypt --textmode";
78 else if(!recipients.isEmpty())
79 cmd =
"--batch --armor --encrypt --textmode";
80 else if(passphrase != 0)
81 cmd =
"--batch --escape-from --clearsign";
84 kDebug( 5326 ) <<
"kpgpbase: Neither recipients nor passphrase specified.";
91 if(!recipients.isEmpty())
93 cmd +=
" --set-filename stdin";
101 for( KeyIDList::ConstIterator it = recipients.begin();
102 it != recipients.end(); ++it ) {
110 exitStatus =
runGpg(cmd.data(), passphrase);
115 if( exitStatus != 0 )
118 errMsg = i18n(
"Unknown error." );
126 if(!recipients.isEmpty())
130 unsigned int num = 0;
131 QByteArray badkeys =
"";
139 while((index =
error.indexOf(
"skipped: ",index) ) != -1 )
142 index =
error.indexOf(
'\'',index);
143 int index2 =
error.indexOf(
'\'',index+1);
144 badkeys +=
error.mid(index, index2-index+1) +
", ";
150 if(num == recipients.count())
151 errMsg = i18n(
"Could not find public keys matching the userid(s)\n"
153 "the message is not encrypted.",
156 errMsg = i18n(
"Could not find public keys matching the userid(s)\n"
158 "these persons will not be able to read the message.",
165 if( passphrase != 0 )
179 if(
error.contains(
"bad passphrase") )
181 errMsg = i18n(
"Signing failed because the passphrase is wrong.");
186 else if(
error.contains(
"unusable secret key") )
188 errMsg = i18n(
"Signing failed because your secret key is unusable.");
213 exitStatus =
runGpg(
"--batch --decrypt", passphrase);
214 if( !
output.isEmpty() && ( !
error.contains(
"gpg: quoted printable" ) ) )
218 if(exitStatus == -1) {
219 errMsg = i18n(
"Error running gpg");
251 if(
error.contains(
"gpg: encrypted with" ) )
255 if(
error.contains(
"\ngpg: decryption failed" ) )
257 if( ( index =
error.indexOf(
"bad passphrase" ) ) != -1 )
259 if( passphrase != 0 )
261 errMsg = i18n(
"Bad passphrase; could not decrypt." );
262 kDebug( 5326 ) <<
"Base: passphrase is bad";
269 index2 =
error.lastIndexOf(
'"', index) - 1;
270 index =
error.lastIndexOf(
" \"", index2) + 7;
274 kDebug( 5326 ) <<
"Base: key needed is \"" << block.
requiredUserId() <<
"\"!";
277 else if(
error.contains(
"secret key not available" ) )
282 errMsg = i18n(
"You do not have the secret key needed to decrypt this message.");
283 kDebug( 5326 ) <<
"Base: no secret key for this message";
285 else if(
error.contains(
"cancelled by user" ) )
289 errMsg = i18n(
"The passphrase dialog was cancelled.");
297 index =
error.indexOf(
"can only be read by:");
300 index =
error.indexOf(
'\n',index);
301 int end =
error.indexOf(
"\n\n",index);
304 while( (index2 =
error.indexOf(
'\n',index+1)) <= end )
306 QByteArray item =
error.mid(index+1,index2-index-1);
308 mRecipients.append(item);
318 if( ( index =
error.indexOf(
"Signature made") ) != -1 )
324 index2 =
error.indexOf(
"using", index+15);
326 kDebug( 5326 ) <<
"Message was signed on '" << block.
signatureDate() <<
"'";
327 index2 =
error.indexOf(
"key ID ", index2) + 7;
329 kDebug( 5326 ) <<
"Message was signed with key '" << block.
signatureKeyId() <<
"'";
331 index =
error.indexOf(
'\n', index2)+1;
333 if ((
error.indexOf(
"Key matching expected", index) != -1 )
334 || (
error.indexOf(
"Can't check signature", index) != -1 ))
340 else if(
error.indexOf(
"Good signature", index) != -1 )
344 index =
error.indexOf(
'"',index);
345 index2 =
error.indexOf(
'\n',index+1);
346 index2 =
error.lastIndexOf(
'"', index2-1);
349 else if(
error.indexOf(
"BAD signature", index) != -1 )
354 index =
error.indexOf(
'"',index);
355 index2 =
error.indexOf(
'\n',index+1);
356 index2 =
error.lastIndexOf(
'"', index2-1);
359 else if(
error.indexOf(
"Can't find the right public key", index) != -1 )
382 const bool readTrust ,
389 exitStatus =
runGpg(
"--batch --list-public-keys --with-fingerprint --with-colons --fixed-list-mode 0x" + keyID, 0,
true );
391 exitStatus =
runGpg(
"--batch --list-public-keys --with-fingerprint --with-colons --fixed-list-mode --no-expensive-trust-checks 0x" + keyID, 0,
true );
393 if(exitStatus != 0) {
400 if( !strncmp(
output.data(),
"pub:", 4 ) )
403 offset =
output.indexOf(
"\npub:" );
410 key = parseKeyData(
output, offset, key );
423 QByteArray cmd =
"--batch --list-public-keys --with-fingerprint --with-colons "
424 "--fixed-list-mode --no-expensive-trust-checks";
425 for ( QStringList::ConstIterator it = patterns.begin();
426 it != patterns.end(); ++it ) {
428 cmd += KShell::quoteArg( *it ).toLocal8Bit();
431 exitStatus =
runGpg( cmd, 0,
true );
433 if(exitStatus != 0) {
442 std::sort( publicKeys.begin(), publicKeys.end(),
KeyCompare );
455 QByteArray cmd =
"--batch --list-secret-keys --with-fingerprint --with-colons "
457 for ( QStringList::ConstIterator it = patterns.begin();
458 it != patterns.end(); ++it ) {
460 cmd += KShell::quoteArg( *it ).toLocal8Bit();
463 exitStatus =
runGpg( cmd, 0,
true );
465 if(exitStatus != 0) {
474 std::sort( secretKeys.begin(), secretKeys.end(),
KeyCompare );
488 cmd +=
" --sign-key 0x";
492 exitStatus =
runGpg(cmd.data(), passphrase);
510 exitStatus =
runGpg(
"--batch --armor --export 0x" + keyID, 0,
true);
512 if(exitStatus != 0) {
522 BaseG::parseKeyData(
const QByteArray& output,
int& offset,
Key* key )
533 if( ( strncmp( output.data() + offset,
"pub:", 4 ) != 0 )
534 && ( strncmp( output.data() + offset,
"sec:", 4 ) != 0 ) ) {
544 bool firstKey =
true;
550 if( ( eol = output.indexOf(
'\n', index ) ) == -1 )
553 bool bIsPublicKey =
false;
554 if( ( bIsPublicKey = !strncmp( output.data() + index,
"pub:", 4 ) )
555 || !strncmp( output.data() + index,
"sec:", 4 ) )
566 Subkey *subkey =
new Subkey( QByteArray(), !bIsPublicKey );
569 int pos2 = output.indexOf(
':', pos );
570 for(
int field = 2; field <= 12; field++ )
577 switch( output[pos] )
582 subkey->setInvalid(
true );
586 subkey->setDisabled(
true );
590 subkey->setRevoked(
true );
594 subkey->setExpired(
true );
607 kDebug( 5326 ) <<
"Unknown trust value";
613 subkey->setKeyLength( output.mid( pos, pos2-pos ).toUInt() );
617 subkey->setKeyAlgorithm( output.mid( pos, pos2-pos ).toUInt() );
620 keyID = output.mid( pos, pos2-pos );
621 subkey->setKeyID( keyID );
625 subkey->setCreationDate( QString(QLatin1String(output.mid( pos, pos2-pos ))).toLong() );
629 subkey->setExpirationDate( QString(QLatin1String(output.mid( pos, pos2-pos ))).toLong() );
631 subkey->setExpirationDate( -1 );
639 for(
int i=pos; i<pos2; ++i )
643 subkey->setCanEncrypt(
true );
646 subkey->setCanSign(
true );
649 subkey->setCanCertify(
true );
661 kDebug( 5326 ) <<
"Unknown key capability";
666 pos2 = output.indexOf(
':', pos );
670 else if( !strncmp( output.data() + index,
"uid:", 4 ) )
674 UserID *userID =
new UserID( QLatin1String(
"") );
677 int pos2 = output.indexOf(
':', pos );
678 for(
int field=2; field <= 10; field++ )
685 switch( output[pos] )
688 userID->setInvalid(
true );
691 userID->setRevoked(
true );
710 kDebug( 5326 ) <<
"Unknown trust value";
723 QByteArray uid = output.mid( pos, pos2-pos );
727 for (
int idx = 0 ; (idx = uid.indexOf(
"\\x", idx ) != -1) ; ++idx ) {
729 str[0] = (char) QString( QLatin1String(uid.mid( idx + 2, 2 )) ).toShort( 0, 16 );
730 uid.replace( idx, 4, str );
732 QString uidString = QString::fromUtf8( uid.data() );
735 for (
int i = 0; i + 1 < uidString.length(); ++i ) {
736 if ( uidString[i].unicode() == 0xdbff &&
737 uidString[i+1].row() == 0xde ) {
746 kDebug( 5326 ) <<
"User Id '" << uid
747 <<
"' doesn't seem to be utf-8 encoded.";
751 int nonAsciiCount = 0, asciiCount = 0;
755 for(
signed char* ch = (
signed char*)uid.data();
756 *ch && ( *ch !=
'(' ) && ( *ch !=
'<' );
758 if( ( ( *ch >=
'A' ) && ( *ch <=
'Z' ) )
759 || ( ( *ch >=
'a' ) && ( *ch <=
'z' ) ) )
764 kDebug( 5326 ) <<
"ascii-nonAscii ratio :" << asciiCount
765 <<
":" << nonAsciiCount;
766 if( nonAsciiCount > asciiCount ) {
768 kDebug( 5326 ) <<
"Assume koi8-r encoding.";
769 QTextCodec *codec = QTextCodec::codecForName(
"KOI8-R");
770 uidString = codec->toUnicode( uid.data() );
778 if( ( uidString.length() >= 2 )
779 && ( uidString[0].toLower() == uidString[0] )
780 && ( uidString[1].toUpper() == uidString[1] ) ) {
783 kDebug( 5326 ) <<
"No, it doesn't seem to be koi8-r."
784 "Use CP 1251 instead.";
785 QTextCodec *codec = QTextCodec::codecForName(
"CP1251");
786 uidString = codec->toUnicode( uid.data() );
791 kDebug( 5326 ) <<
"Assume latin1 encoding.";
792 uidString = QString::fromLatin1( uid.data() );
795 userID->setText( uidString );
799 pos2 = output.indexOf(
':', pos );
805 else if( !strncmp( output.data() + index,
"fpr:", 4 ) )
814 for(
int i = 0; i < 8; ++i )
815 pos = output.indexOf(
':', pos ) + 1;
816 int pos2 = output.indexOf(
':', pos );
832 BaseG::parseKeyList(
const QByteArray& output,
bool secretKeys )
839 if( !strncmp( output.data(),
"pub:", 4 )
840 || !strncmp( output.data(),
"sec:", 4 ) )
844 offset = output.indexOf(
"\nsec:" );
846 offset = output.indexOf(
"\npub:" );
854 key = parseKeyData( output, offset );
void setCanCertify(const bool canCertify)
Sets the flag if the key can be used to certify keys to canCertify .
virtual KeyList publicKeys(const QStringList &patterns=QStringList())
Returns the list of public keys in the users public keyring.
void setError(const QByteArray &str)
void setFingerprint(const KeyID &keyID, const QByteArray &fpr)
Sets the fingerprint of the given subkey to fpr .
void setSignatureDate(const QByteArray &date)
virtual int signKey(const KeyID &keyID, const char *passphrase)
Signs the given key with the currently set user key.
const KeyID user() const
Returns the actual key ID of the currently set key.
virtual int encrypt(Block &block, const KeyIDList &recipients)
Encrypts the message with the given keys.
void setCanEncrypt(const bool canEncrypt)
Sets the flag if the key can be used to encrypt data to canEncrypt .
virtual KeyList secretKeys(const QStringList &patterns=QStringList())
Returns the list of secret keys in the users secret keyring.
virtual Key * readPublicKey(const KeyID &keyID, const bool readTrust=false, Key *key=0)
Reads the key data for the given key and returns it.
void setRequiredUserId(const QString &userId)
static Kpgp::Module * getKpgp()
return the actual pgp object
void setSecret(const bool secret)
Sets the flag if the key is a secret key to secret .
void setDisabled(const bool disabled)
Sets the flag if the key has been disabled to disabled .
bool KeyCompare(Key *left, Key *right)
virtual int decrypt(Block &block, const char *passphrase=0)
Decrypts the message.
void setSignatureUserId(const QString &userId)
void setInvalid(const bool invalid)
Sets the flag if the key is invalid to invalid .
void addUserID(const QString &uid, const Validity validity=KPGP_VALIDITY_UNKNOWN, const bool revoked=false, const bool invalid=false)
Adds a user ID with the given values to the key if uid isn't an empty string.
QByteArray signatureDate() const
date of the signature WARNING: Will most likely be changed to QDateTime
QByteArray signatureKeyId() const
keyID of signer
virtual int clearsign(Block &block, const char *passphrase)
Clearsigns the message with the currently set key.
void setSignatureKeyId(const QByteArray &keyId)
virtual int runGpg(const char *cmd, const char *passphrase=0, bool onlyReadFromGnuPG=false)
void addSubkey(const KeyID &keyID, const bool secret=false)
Adds a subkey with the given values to the key if keyID isn't an empty string.
void setExpired(const bool expired)
Sets the flag if the key has expired to expired .
void setProcessedText(const QByteArray &str)
void setRevoked(const bool revoked)
Sets the flag if the key has been revoked to revoked .
This class is used to store information about a PGP key.
virtual int encsign(Block &block, const KeyIDList &recipients, const char *passphrase=0)
Encrypts and signs the message with the given keys.
virtual QByteArray getAsciiPublicKey(const KeyID &keyID)
Returns the ascii armored data of the public key with the given key id.
void setCanSign(const bool canSign)
Sets the flag if the key can be used to sign data to canSign .
QString requiredUserId() const
void clear()
Clears/resets all key data.
void setStatus(const int status)