51 return encsign( block, recipients, 0 );
64 const char *passphrase )
69 if(!recipients.isEmpty() && passphrase != 0)
70 cmd =
PGP2 " +batchmode +language=en +verbose=1 -seat";
71 else if(!recipients.isEmpty())
72 cmd =
PGP2 " +batchmode +language=en +verbose=1 -eat";
73 else if(passphrase != 0)
74 cmd =
PGP2 " +batchmode +language=en +verbose=1 -sat";
77 kDebug( 5326 ) <<
"kpgpbase: Neither recipients nor passphrase specified.";
84 if(!recipients.isEmpty()) {
90 KeyIDList::ConstIterator end( recipients.end() );
91 for( KeyIDList::ConstIterator it = recipients.constBegin();
101 exitStatus =
run(cmd.data(), passphrase);
113 if(!recipients.isEmpty())
117 unsigned int num = 0;
118 QByteArray badkeys =
"";
119 if (
error.contains(
"Cannot find the public key") )
123 while((index =
error.indexOf(
"Cannot find the public key",index))
127 index =
error.indexOf(
'\'',index);
128 int index2 =
error.indexOf(
'\'',index+1);
131 badkeys +=
error.mid(index, index2-index+1);
136 if(num == recipients.count())
137 errMsg = i18n(
"Could not find public keys matching the userid(s)\n"
139 "the message is not encrypted.",
142 errMsg = i18n(
"Could not find public keys matching the userid(s)\n"
144 "these persons will not be able to read the message.",
150 if (
error.contains(
"skipping userid") )
154 while((index =
error.indexOf(
"skipping userid",index))
158 int index2 =
error.indexOf(
'\n',index+16);
161 badkeys +=
error.mid(index+16, index2-index-16);
167 if(num == recipients.count())
168 errMsg = i18n(
"Public keys not certified with trusted signature "
171 "The message is not encrypted.",
174 errMsg = i18n(
"Public keys not certified with trusted signature "
177 "these persons will not be able to read the message.",
188 if(
error.contains(
"Pass phrase is good") )
193 if(
error.contains(
"Bad pass phrase") )
195 errMsg = i18n(
"Bad passphrase; could not sign.");
201 if (
error.contains(
"Signature error") )
203 errMsg = i18n(
"Signing failed: please check your PGP User Identity, "
204 "the PGP setup, and the key rings.");
209 if (
error.contains(
"Encryption error") )
211 errMsg = i18n(
"Encryption failed: please check your PGP setup "
212 "and the key rings.");
232 exitStatus =
run(
PGP2 " +batchmode +language=en -f", passphrase);
239 if(
error.contains(
"ASCII armor corrupted.") )
241 kDebug( 5326 ) <<
"removing ASCII armor header";
242 int index1 =
input.indexOf(
"-----BEGIN PGP SIGNED MESSAGE-----");
244 index1 =
input.indexOf(
"-----BEGIN PGP SIGNATURE-----", index1);
246 index1 =
input.indexOf(
"-----BEGIN PGP MESSAGE-----");
247 index1 =
input.indexOf(
'\n', index1);
248 index2 =
input.indexOf(
"\n\n", index1);
249 input.remove(index1, index2 - index1);
250 exitStatus =
run(
PGP2 " +batchmode +language=en -f", passphrase);
256 if(exitStatus == -1) {
257 errMsg = i18n(
"error running PGP");
283 if(
error.contains(
"File is encrypted.") )
287 if((index =
error.indexOf(
"Key for user ID:")) != -1 )
291 index2 =
error.indexOf(
'\n', index);
295 if((passphrase != 0) && (
error.contains(
"Bad pass phrase") ))
297 errMsg = i18n(
"Bad passphrase; could not decrypt.");
298 kDebug( 5326 ) <<
"Base: passphrase is bad";
308 errMsg = i18n(
"You do not have the secret key needed to decrypt this message.");
309 kDebug( 5326 ) <<
"Base: no secret key for this message";
315 index =
error.indexOf(
"can only be read by:");
318 index =
error.indexOf(
'\n',index);
319 int end =
error.indexOf(
"\n\n",index);
322 while( (index2 =
error.indexOf(
'\n',index+1)) <= end )
324 QByteArray item =
error.mid(index+1,index2-index-1);
326 mRecipients.append(item);
393 if((index =
error.indexOf(
"File has signature")) != -1 )
396 index =
error.indexOf(
'\n', index+18) + 1;
400 if ((index2 =
error.indexOf(
"Signature made", index)) != -1 ) {
402 int index3 =
error.indexOf(
"using", index2);
404 kDebug( 5326 ) <<
"Message was signed on '" << block.
signatureDate() <<
"'";
405 index3 =
error.indexOf(
"key ID ", index3) + 7;
407 kDebug( 5326 ) <<
"Message was signed with key '" << block.
signatureKeyId() <<
"'";
416 if( ( index2 =
error.indexOf(
"Key matching expected", index) ) != -1 )
420 int index3 =
error.indexOf(
"Key ID ", index2) + 7;
424 else if( (index2 =
error.indexOf(
"Good signature from", index)) != -1 )
428 index =
error.indexOf(
'"',index2+19);
429 index2 =
error.indexOf(
'"', index+1);
432 else if( (index2 =
error.indexOf(
"Bad signature from", index)) != -1 )
436 index =
error.indexOf(
'"',index2+19);
437 index2 =
error.indexOf(
'"', index+1);
440 else if(
error.indexOf(
"Keyring file", index) != -1 )
446 index =
error.indexOf(
'\'', index) + 1;
447 index2 =
error.indexOf(
'\'', index);
449 "Please check your PGP setup.", QString::fromLatin1(
error.mid(index, index2-index)) ) );
465 const bool readTrust ,
471 exitStatus =
run(
PGP2 " +batchmode +language=en +verbose=0 -kvc -f 0x" +
474 if(exitStatus != 0) {
479 key = parsePublicKeyData(
output, key );
488 exitStatus =
run(
PGP2 " +batchmode +language=en +verbose=0 -kc -f",
491 if(exitStatus != 0) {
496 parseTrustDataForKey( key,
error );
517 if ( patterns.isEmpty() ) {
518 exitStatus =
run( cmd, 0,
true );
520 if ( exitStatus != 0 ) {
529 typedef QMap<QByteArray, Key*> KeyMap;
531 QStringList::ConstIterator end( patterns.constEnd() );
532 for ( QStringList::ConstIterator it = patterns.constBegin();
534 exitStatus =
run( cmd +
' ' + KShell::quoteArg( *it ).toLocal8Bit(),
537 if ( exitStatus != 0 ) {
546 while ( !publicKeys.isEmpty() ) {
547 Key * key = publicKeys.takeFirst();
555 KeyMap::ConstIterator endKeyMap( map.constEnd() );
556 for ( KeyMap::ConstIterator it = map.constBegin(); it != endKeyMap; ++it ) {
557 publicKeys.append( it.value() );
562 std::sort( publicKeys.begin(), publicKeys.end(),
KeyCompare );
580 cmd =
PGP2 " +batchmode +language=en -ks -f ";
582 cmd +=
" 0x" + keyID;
585 exitStatus =
run(cmd.data(),passphrase);
600 int exitStatus =
run(
PGP2 " +batchmode +force +language=en -kxaf 0x" + keyID,
603 if(exitStatus != 0) {
613 Base2::parsePublicKeyData(
const QByteArray& output,
Key* key )
619 if( !strncmp( output.data(),
"pub", 3 ) ||
620 !strncmp( output.data(),
"sec", 3 ) )
629 index = output.indexOf(
"\npub" );
641 if( ( index2 = output.indexOf(
'\n', index ) ) == -1 )
644 if( !strncmp( output.data() + index,
"pub", 3 ) ||
645 !strncmp( output.data() + index,
"sec", 3 ) )
669 subkey =
new Subkey(
"",
false );
679 switch( output[index+3] )
692 kDebug( 5326 ) <<
"Unknown key flag.";
697 while( output[pos] ==
' ' )
699 pos2 = output.indexOf(
'/', pos );
700 subkey->
setKeyLength( output.mid( pos, pos2-pos ).toUInt() );
704 pos2 = output.indexOf(
' ', pos );
705 subkey->
setKeyID( output.mid( pos, pos2-pos ) );
709 while( output[pos] ==
' ' )
711 pos2 = output.indexOf(
' ', pos );
712 int year = output.mid( pos, 4 ).toInt();
713 int month = output.mid( pos+5, 2 ).toInt();
714 int day = output.mid( pos+8, 2 ).toInt();
715 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
716 QDateTime epoch( QDate( 1970, 01, 01 ), QTime( 00, 00 ) );
725 while( output[pos] ==
' ' )
727 QByteArray uid = output.mid( pos, index2-pos );
728 if( uid !=
"*** KEY REVOKED ***" )
736 else if( output[index] ==
' ' )
741 assert( subkey != 0 );
744 while( output[pos] ==
' ' )
747 if( !strncmp( output.data() + pos,
"Key fingerprint = ", 18 ) )
752 QByteArray fingerprint = output.mid( pos, index2-pos );
754 for (
int idx = 0 ; (idx = fingerprint.indexOf(
' ', idx)) != -1 ; )
755 fingerprint.replace( idx, 1,
"" );
759 else if( !strncmp( output.data() + pos,
"Expire: ", 8 ) ||
760 !strncmp( output.data() + pos,
"no expire ", 10 ) )
767 if( output[pos] ==
'E' )
771 int year = output.mid( pos, 4 ).toInt();
772 int month = output.mid( pos+5, 2 ).toInt();
773 int day = output.mid( pos+8, 2 ).toInt();
774 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
775 QDateTime epoch( QDate( 1970, 01, 01 ), QTime( 00, 00 ) );
786 if( !strncmp( output.data() + pos,
"SIGNature only", 14 ) )
791 else if( !strncmp( output.data() + pos,
"ENCRyption only", 15 ) )
805 key->
addUserID( QLatin1String(output.mid( pos, index2-pos )) );
818 Base2::parseTrustDataForKey( Key* key,
const QByteArray& str )
820 if( ( key == 0 ) || str.isEmpty() )
827 int index = str.indexOf(
'\n' ) + 1;
828 while( ( index > 0 ) &&
829 ( strncmp( str.data() + index+2, keyID.data(), 8 ) != 0 ) )
830 index = str.indexOf(
'\n', index ) + 1;
835 bool ultimateTrust =
false;
836 if( !strncmp( str.data() + index+11,
"ultimate", 8 ) )
837 ultimateTrust =
true;
839 bool firstLine =
true;
846 if( ( index2 = str.indexOf(
'\n', index ) ) == -1 )
850 if( !firstLine && ( str[index+2] !=
' ' ) )
853 if( str[index+21] !=
' ' )
858 if( !strncmp( str.data() + index+21,
"complete", 8 ) )
863 else if( !strncmp( str.data() + index+21,
"marginal", 8 ) )
865 else if( !strncmp( str.data() + index+21,
"never", 5 ) )
867 else if( !strncmp( str.data() + index+21,
"undefined", 9 ) )
871 int pos = index + 31;
872 if( str[index+2] ==
' ' )
874 QString uid = QLatin1String(str.mid( pos, index2-pos ));
877 for( UserIDList::Iterator it = userIDs.begin(); it != userIDs.end(); ++it )
878 if( (*it)->text() == uid )
880 kDebug( 5326 )<<
"Setting the validity of"<<uid<<
" to"<<validity;
881 (*it)->setValidity( validity );
895 kDebug( 5326 ) <<
"Kpgp::Base2::parseKeyList()";
902 if( !strncmp( output.data(),
"pub", 3 ) ||
903 !strncmp( output.data(),
"sec", 3 ) )
908 index = output.indexOf(
"\nsec" );
910 index = output.indexOf(
"\npub" );
922 if( ( index2 = output.indexOf(
'\n', index ) ) == -1 )
925 if( !strncmp( output.data() + index,
"pub", 3 ) ||
926 !strncmp( output.data() + index,
"sec", 3 ) )
949 subkey =
new Subkey(
"", secretKeys );
959 switch( output[index+3] )
972 kDebug( 5326 ) <<
"Unknown key flag.";
977 while( output[pos] ==
' ' )
979 pos2 = output.indexOf(
'/', pos );
980 subkey->
setKeyLength( output.mid( pos, pos2-pos ).toUInt() );
984 pos2 = output.indexOf(
' ', pos );
985 subkey->
setKeyID( output.mid( pos, pos2-pos ) );
989 while( output[pos] ==
' ' )
991 pos2 = output.indexOf(
' ', pos );
992 int year = output.mid( pos, 4 ).toInt();
993 int month = output.mid( pos+5, 2 ).toInt();
994 int day = output.mid( pos+8, 2 ).toInt();
995 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
996 QDateTime epoch( QDate( 1970, 01, 01 ), QTime( 00, 00 ) );
1005 while( output[pos] ==
' ' )
1007 QByteArray uid = output.mid( pos, index2-pos );
1008 if( uid !=
"*** KEY REVOKED ***" )
1016 else if( output[index] ==
' ' )
1022 int pos = index + 1;
1023 while( output[pos] ==
' ' )
1026 if( !strncmp( output.data() + pos,
"Key fingerprint = ", 18 ) )
1031 QByteArray fingerprint = output.mid( pos, index2-pos );
1033 for (
int idx = 0 ; (idx = fingerprint.indexOf(
' ', idx)) != -1 ; )
1034 fingerprint.replace( idx, 1,
"" );
1038 else if( !strncmp( output.data() + pos,
"Expire: ", 8 ) ||
1039 !strncmp( output.data() + pos,
"no expire ", 10 ) )
1046 if( output[pos] ==
'E' )
1050 int year = output.mid( pos, 4 ).toInt();
1051 int month = output.mid( pos+5, 2 ).toInt();
1052 int day = output.mid( pos+8, 2 ).toInt();
1053 QDateTime dt( QDate( year, month, day ), QTime( 00, 00 ) );
1054 QDateTime epoch( QDate( 1970, 01, 01 ), QTime( 00, 00 ) );
1065 if( !strncmp( output.data() + pos,
"SIGNature only", 14 ) )
1070 else if( !strncmp( output.data() + pos,
"ENCRyption only", 15 ) )
1084 key->
addUserID( QLatin1String(output.mid( pos, index2-pos )) );
void setRevoked(const bool revoked)
Sets the flag if the subkey has been revoked to revoked .
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 setDisabled(const bool disabled)
Sets the flag if the subkey has been disabled to disabled .
void setSignatureDate(const QByteArray &date)
void setExpired(const bool expired)
Sets the flag if the subkey has expired to expired .
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 .
void setKeyLength(const unsigned int keyLen)
Sets the key length of the subkey to keyLen bits.
virtual KeyList secretKeys(const QStringList &patterns=QStringList())
Returns the list of secret keys in the users secret keyring.
void setRequiredUserId(const QString &userId)
static Kpgp::Module * getKpgp()
return the actual pgp object
void setCanSign(const bool canSign)
Sets the flag if the subkey can be used to sign data to canSign .
virtual int clearsign(Block &block, const char *passphrase)
Clearsigns the message with the currently set key.
void setKeyID(const KeyID &keyID)
Sets the key ID of the subkey to keyID .
virtual int signKey(const KeyID &keyID, const char *passphrase)
Signs the given key with the currently set user key.
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 run(const char *cmd, const char *passphrase=0, bool onlyReadFromPGP=false)
void setSignatureUserId(const QString &userId)
This class is used to store information about a subkey of a PGP key.
virtual int encrypt(Block &block, const KeyIDList &recipients)
Encrypts the message with the given keys.
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.
KeyList doGetPublicKeys(const QByteArray &cmd, const QStringList &patterns)
virtual int decrypt(Block &block, const char *passphrase=0)
Decrypts the message.
QList< UserID * > UserIDList
void setExpirationDate(const time_t expirationDate)
Sets the expiration date of the subkey to expirationDate seconds since Epoch.
QByteArray signatureDate() const
date of the signature WARNING: Will most likely be changed to QDateTime
QByteArray signatureKeyId() const
keyID of signer
virtual int encsign(Block &block, const KeyIDList &recipients, const char *passphrase=0)
Encrypts and signs the message with the given keys.
virtual KeyList parseKeyList(const QByteArray &, bool)
void setSignatureKeyId(const QByteArray &keyId)
void setCanCertify(const bool canCertify)
Sets the flag if the subkey can be used to certify keys to canCertify .
QByteArray primaryFingerprint() const
Returns the fingerprint of the primary key or a null string if there are no subkeys.
void setFingerprint(const QByteArray &fingerprint)
Sets the fingerprint of the subkey to fingerprint .
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 KeyList publicKeys(const QStringList &patterns=QStringList())
Returns the list of public keys in the users public 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 setCanEncrypt(const bool canEncrypt)
Sets the flag if the subkey can be used to encrypt data to canEncrypt .
void setCanSign(const bool canSign)
Sets the flag if the key can be used to sign data to canSign .
virtual QByteArray getAsciiPublicKey(const KeyID &keyID)
Returns the ascii armored data of the public key with the given key id.
void setCreationDate(const time_t creationDate)
Sets the creation date of the subkey to creationDate seconds since Epoch.
void clear()
Clears/resets all key data.
void setStatus(const int status)
KeyID primaryKeyID() const
Returns the key ID of the primary key or a null string if there are no subkeys.