52 return encsign( block, recipients, 0 );
65 const char *passphrase )
73 bool signonly =
false;
75 if(!recipients.isEmpty() && passphrase != 0)
76 cmd =
"pgpe +batchmode -afts ";
77 else if(!recipients.isEmpty())
78 cmd =
"pgpe +batchmode -aft ";
79 else if(passphrase != 0)
81 cmd =
"pgps +batchmode -abft ";
86 errMsg = i18n(
"Neither recipients nor passphrase specified.");
93 if(!recipients.isEmpty())
100 KeyIDList::ConstIterator end( recipients.constEnd() );
102 for( KeyIDList::ConstIterator it = recipients.constBegin();
120 exitStatus =
run(cmd.data(), passphrase);
127 if(
error.contains(
"Cannot unlock private key") )
129 errMsg = i18n(
"The passphrase you entered is invalid.");
137 while((index =
error.indexOf(
"WARNING: The above key",index+1)) != -1 )
139 int index2 =
error.indexOf(
"But you previously",index);
140 int index3 =
error.indexOf(
"WARNING: The above key",index+1);
141 if(index2 == -1 || (index2 > index3 && index3 != -1))
145 index2 =
error.indexOf(
'\n',index);
146 index3 =
error.indexOf(
'\n',index2+1);
147 aStr +=
error.mid(index2+1, index3-index2-1);
153 aStr.truncate(aStr.length()-2);
154 if(
error.contains(
"No valid keys found") )
155 errMsg = i18n(
"The key(s) you want to encrypt your message "
156 "to are not trusted. No encryption done.");
158 errMsg = i18n(
"The following key(s) are not trusted:\n%1\n"
159 "Their owner(s) will not be able to decrypt the message.",
160 QString::fromLocal8Bit( aStr ));
165 if((index =
error.indexOf(
"No encryption keys found for")) != -1 )
167 index =
error.indexOf(
':',index);
168 int index2 =
error.indexOf(
'\n',index);
170 errMsg = i18n(
"Missing encryption key(s) for:\n%1",
171 QString::fromLocal8Bit(
error.mid(index,index2-index)));
182 for (
int idx = 0 ; (idx =
input.indexOf(
"\n-", idx)) != -1 ; idx += 4 )
183 input.replace(idx, 2,
"\n- -");
199 int exitStatus =
run(
"pgpv -f +batchmode=1", passphrase);
204 if(exitStatus == -1) {
205 errMsg = i18n(
"Error running PGP");
212 int index =
error.indexOf(
"Cannot decrypt message");
220 if(
error.contains(
"Need a pass phrase") )
224 errMsg = i18n(
"Bad passphrase; could not decrypt.");
225 kDebug( 5326 ) <<
"Base: passphrase is bad";
235 errMsg = i18n(
"You do not have the secret key needed to decrypt this message.");
236 kDebug( 5326 ) <<
"Base: no secret key for this message";
242 index =
error.indexOf(
"can only be decrypted by:");
245 index =
error.indexOf(
'\n',index);
246 int end =
error.indexOf(
"\n\n",index);
250 while( (index2 =
error.indexOf(
'\n',index+1)) <= end )
252 QByteArray item =
error.mid(index+1,index2-index-1);
254 mRecipients.append(item);
260 index =
error.indexOf(
"Good signature");
268 index =
error.indexOf(
"Key ID ", index) + 7;
272 index =
error.indexOf(
'"',index) + 1;
273 int index2 =
error.indexOf(
'"', index);
279 index =
error.indexOf(
"BAD signature");
287 index =
error.indexOf(
"Key ID ", index) + 7;
291 index =
error.indexOf(
'"',index) + 1;
292 int index2 =
error.indexOf(
'"', index);
298 index =
error.indexOf(
"Signature by unknown key");
301 index =
error.indexOf(
"keyid: 0x",index) + 9;
322 int exitStatus =
run(
"pgpk -ll 0x" + keyId, 0,
true );
324 if(exitStatus != 0) {
329 key = parseSingleKey(
output, key );
338 exitStatus =
run(
"pgpk -c 0x" + keyId, 0,
true );
340 if(exitStatus != 0) {
345 parseTrustDataForKey( key,
output );
357 QByteArray cmd =
"pgpk -ll";
359 QStringList::ConstIterator end( patterns.end() );
362 for ( QStringList::ConstIterator it = patterns.constBegin();
365 cmd += KShell::quoteArg( *it ).toLocal8Bit();
368 exitStatus =
run( cmd, 0,
true );
370 if(exitStatus != 0) {
379 std::sort( keys.begin(), keys.end(),
KeyCompare );
388 QByteArray cmd =
"pgpk -ll";
389 QStringList::ConstIterator end( patterns.constEnd() );
390 for ( QStringList::ConstIterator it = patterns.constBegin();
393 cmd += KShell::quoteArg( *it ).toLocal8Bit();
396 int exitStatus =
run( cmd, 0,
true );
398 if(exitStatus != 0) {
407 std::sort( keys.begin(), keys.end(),
KeyCompare );
419 int exitStatus =
run(
"pgpk -xa 0x" + keyID, 0,
true );
421 if(exitStatus != 0) {
433 if(passphrase == 0)
return false;
436 cmd =
"pgpk -s -f +batchmode=1 0x";
441 int exitStatus =
run(cmd.data(), passphrase);
452 Base5::parseKeyData(
const QByteArray& output,
int& offset,
Key* key )
459 if( ( strncmp( output.data() + offset,
"pub", 3 ) != 0 ) &&
460 ( strncmp( output.data() + offset,
"sec", 3 ) != 0 ) )
462 kDebug( 5326 ) <<
"Unknown key type or corrupt key data.";
472 bool primaryKey =
true;
479 eol = output.indexOf(
'\n', offset );
480 if( ( eol == -1 ) || ( eol == offset ) )
485 if( !strncmp( output.data() + offset,
"pub", 3 ) ||
486 !strncmp( output.data() + offset,
"sec", 3 ) ||
487 !strncmp( output.data() + offset,
"sub", 3 ) )
492 subkey =
new Subkey(
"",
false );
504 switch( output[offset+3] )
509 subkey->setDisabled(
true );
519 while( output[pos] ==
' ' )
521 pos2 = output.indexOf(
' ', pos );
522 subkey->setKeyLength( output.mid( pos, pos2-pos ).toUInt() );
527 while( output[pos] ==
' ' )
530 pos2 = output.indexOf(
' ', pos );
531 subkey->setKeyID( output.mid( pos, pos2-pos ) );
536 while( output[pos] ==
' ' )
538 pos2 = output.indexOf(
' ', pos );
539 int year = output.mid( pos, 4 ).toInt();
540 int month = output.mid( pos+5, 2 ).toInt();
541 int day = output.mid( pos+8, 2 ).toInt();
542 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
543 QDateTime epoch( QDate( 1970, 01, 01 ), QTime( 00, 00 ) );
548 subkey->setCreationDate( epoch.secsTo( dt ) );
552 if( primaryKey || !key->
revoked() )
555 while( output[pos] ==
' ' )
557 pos2 = output.indexOf(
' ', pos );
558 if( output[pos] ==
'-' )
560 subkey->setExpirationDate( -1 );
562 else if( !strncmp( output.data() + pos,
"*REVOKED*", 9 ) )
564 subkey->setRevoked(
true );
569 int year = output.mid( pos, 4 ).toInt();
570 int month = output.mid( pos+5, 2 ).toInt();
571 int day = output.mid( pos+8, 2 ).toInt();
572 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
573 subkey->setCreationDate( epoch.secsTo( dt ) );
575 if( QDateTime::currentDateTime() >= dt )
577 subkey->setExpired(
true );
583 subkey->setRevoked(
true );
589 while( output[pos] ==
' ' )
591 pos2 = output.indexOf(
' ', pos );
592 if( !strncmp( output.data() + pos,
"RSA", 3 ) )
597 else if( !strncmp( output.data() + pos,
"DSS", 3 ) )
599 else if( !strncmp( output.data() + pos,
"Diffie-Hellman", 14 ) )
602 kDebug( 5326 )<<
"Unknown key algorithm";
605 subkey->setCanEncrypt( encr );
606 subkey->setCanSign( sign );
607 subkey->setCanCertify( sign );
612 bool canSign =
false;
613 bool canEncr =
false;
615 while( output[pos] ==
' ' )
618 if( !strncmp( output.data() + pos,
"Sign & Encrypt", 14 ) )
623 else if( !strncmp( output.data() + pos,
"Sign only", 9 ) )
625 else if( !strncmp( output.data() + pos,
"Encrypt only", 12 ) )
628 kDebug( 5326 )<<
"Unknown key capability";
641 else if( !strncmp( output.data() + offset,
"f16", 3 ) ||
642 !strncmp( output.data() + offset,
"f20", 3 ) )
649 int pos = output.indexOf(
'=', offset+3 ) + 2;
650 QByteArray fingerprint = output.mid( pos, eol-pos );
652 for (
int idx = 0 ; (idx = fingerprint.indexOf(
' ', idx)) != -1 ; )
653 fingerprint.replace( idx, 1,
"" );
654 assert( subkey != 0 );
655 subkey->setFingerprint( fingerprint );
658 else if( !strncmp( output.data() + offset,
"uid", 3 ) )
661 QByteArray uid = output.mid( pos, eol-pos );
671 else if ( !strncmp( output.data() + offset,
"sig", 3 ) ||
672 !strncmp( output.data() + offset,
"SIG", 3 ) ||
673 !strncmp( output.data() + offset,
"ret", 3 ) )
687 Base5::parseSingleKey(
const QByteArray& output, Key* key )
692 if( !strncmp( output.data(),
"Type Bits", 9 ) )
696 offset = output.indexOf(
"\nType Bits" ) + 1;
702 offset = output.indexOf(
'\n', offset ) + 1;
706 key = parseKeyData( output, offset, key );
715 Base5::parseKeyList(
const QByteArray& output,
bool onlySecretKeys )
722 if( !strncmp( output.data(),
"Type Bits", 9 ) )
726 offset = output.indexOf(
"\nType Bits" ) + 1;
732 offset = output.indexOf(
'\n', offset ) + 1;
738 key = parseKeyData( output, offset );
742 if( !onlySecretKeys || !key->secret() )
757 Base5::parseTrustDataForKey( Key* key,
const QByteArray& str )
759 if( ( key == 0 ) || str.isEmpty() )
762 QByteArray keyID =
"0x" + key->primaryKeyID();
766 int offset = str.indexOf(
"\n\n KeyID" ) + 9;
767 if( offset == -1 + 9 )
770 offset = str.indexOf(
'\n', offset ) + 1;
771 if( offset == -1 + 1 )
774 bool ultimateTrust =
false;
775 if( !strncmp( str.data() + offset+13,
"ultimate", 8 ) )
776 ultimateTrust =
true;
784 if( ( eol = str.indexOf(
'\n', offset ) ) == -1 )
787 if( str[offset+23] !=
' ' )
792 if( !strncmp( str.data() + offset+23,
"complete", 8 ) )
797 else if( !strncmp( str.data() + offset+23,
"marginal", 8 ) )
799 else if( !strncmp( str.data() + offset+23,
"invalid", 7 ) )
803 int pos = offset + 33;
804 QString uid = QLatin1String(str.mid( pos, eol-pos ));
807 for( UserIDList::Iterator it = userIDs.begin(); it != userIDs.end(); ++it )
808 if( (*it)->text() == uid )
810 kDebug( 5326 )<<
"Setting the validity of"<<uid<<
" to"<<validity;
811 (*it)->setValidity( validity );
Validity
These are the possible validity values for a PGP user id and for the owner trust. ...
void setCanCertify(const bool canCertify)
Sets the flag if the key can be used to certify keys to canCertify .
void setError(const QByteArray &str)
void setSignatureDate(const QByteArray &date)
virtual KeyList publicKeys(const QStringList &patterns=QStringList())
Returns the list of public keys in the users public keyring.
bool encryptToSelf(void) const
const KeyID user() const
Returns the actual key ID of the currently set key.
void setCanEncrypt(const bool canEncrypt)
Sets the flag if the key can be used to encrypt data to canEncrypt .
virtual Key * readPublicKey(const KeyID &keyID, const bool readTrust=false, Key *key=0)
Reads the key data for the given key and returns it.
virtual KeyList secretKeys(const QStringList &patterns=QStringList())
Returns the list of secret keys in the users secret keyring.
virtual QByteArray getAsciiPublicKey(const KeyID &keyID)
Returns the ascii armored data of the public key with the given key id.
static Kpgp::Module * getKpgp()
return the actual pgp object
virtual int signKey(const KeyID &keyID, const char *passphrase)
Signs the given key with the currently set user key.
void setDisabled(const bool disabled)
Sets the flag if the key has been disabled to disabled .
bool revoked() const
Returns true if the key has been revoked.
bool KeyCompare(Key *left, Key *right)
virtual int run(const char *cmd, const char *passphrase=0, bool onlyReadFromPGP=false)
void setSignatureUserId(const QString &userId)
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.
bool expired() const
Returns true if the key has expired.
QList< UserID * > UserIDList
virtual int clearsign(Block &block, const char *passphrase)
Clearsigns the message with the currently set key.
virtual int encsign(Block &block, const KeyIDList &recipients, const char *passphrase=0)
Encrypts and signs the message with the given keys.
void setSignatureKeyId(const QByteArray &keyId)
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 .
virtual int encrypt(Block &block, const KeyIDList &recipients)
Encrypts the message with the given keys.
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 decrypt(Block &block, const char *passphrase=0)
Decrypts the message.
void setCanSign(const bool canSign)
Sets the flag if the key can be used to sign data to canSign .
void clear()
Clears/resets all key data.
void setStatus(const int status)