#include "zypp/KeyRing.h"
#include "zypp/ExternalProgram.h"
#include "zypp/TmpPath.h"
+#include "zypp/ZYppCallbacks.h" // JobReport::instance
using std::endl;
/// const std::list<PublicKeyData> & cachedPublicKeyData( const Pathname & keyring );
/// \endcode
///////////////////////////////////////////////////////////////////
- struct CachedPublicKeyData // : private base::NonCopyable - but KeyRing uses RWCOW though also NonCopyable :(
+ struct CachedPublicKeyData : private base::NonCopyable
{
const std::list<PublicKeyData> & operator()( const Pathname & keyring_r ) const
{ return getData( keyring_r ); }
+ void setDirty( const Pathname & keyring_r )
+ { _cacheMap[keyring_r].setDirty(); }
+
private:
struct Cache
{
- // Empty copy ctor to allow insert into std::map as
- // scoped_ptr is noncopyable.
Cache() {}
- Cache( const Cache & rhs ) {}
+
+ void setDirty()
+ {
+ _keyringK.reset();
+ _keyringP.reset();
+ }
void assertCache( const Pathname & keyring_r )
{
void KeyRing::Impl::importKey( const PublicKey & key, bool trusted )
{
importKey( key.path(), trusted ? trustedKeyRing() : generalKeyRing() );
+ MIL << "Imported key " << key << " to " << (trusted ? "trustedKeyRing" : "generalKeyRing" ) << endl;
if ( trusted )
- {
+ try {
callback::SendReport<target::rpm::KeyRingSignals> rpmdbEmitSignal;
- callback::SendReport<KeyRingSignals> emitSignal;
-
rpmdbEmitSignal->trustedKeyAdded( key );
+
+ callback::SendReport<KeyRingSignals> emitSignal;
emitSignal->trustedKeyAdded( key );
}
+ catch ( const Exception & excp )
+ {
+ ERR << "Could not import key into rpmdb: " << excp << endl;
+ // TODO: JobReport as hotfix for bsc#1057188; should bubble up and go through some callback
+ JobReport::error( excp.asUserHistory() );
+ }
}
void KeyRing::Impl::multiKeyImport( const Pathname & keyfile_r, bool trusted_r )
void KeyRing::Impl::deleteKey( const std::string & id, bool trusted )
{
- PublicKey key;
-
- if ( trusted )
+ PublicKeyData keyDataToDel( publicKeyExists( id, trusted ? trustedKeyRing() : generalKeyRing() ) );
+ if ( ! keyDataToDel )
{
- key = exportKey( id, trustedKeyRing() );
+ WAR << "Key to delete [" << id << "] is not in " << (trusted ? "trustedKeyRing" : "generalKeyRing" ) << endl;
+ return;
}
-
deleteKey( id, trusted ? trustedKeyRing() : generalKeyRing() );
+ MIL << "Deleted key [" << id << "] from " << (trusted ? "trustedKeyRing" : "generalKeyRing" ) << endl;
if ( trusted )
- {
- callback::SendReport<target::rpm::KeyRingSignals> rpmdbEmitSignal;
- callback::SendReport<KeyRingSignals> emitSignal;
+ try {
+ PublicKey key( keyDataToDel );
+ callback::SendReport<target::rpm::KeyRingSignals> rpmdbEmitSignal;
rpmdbEmitSignal->trustedKeyRemoved( key );
+
+ callback::SendReport<KeyRingSignals> emitSignal;
emitSignal->trustedKeyRemoved( key );
}
+ catch ( const Exception & excp )
+ {
+ ERR << "Could not delete key from rpmmdb: " << excp << endl;
+ // TODO: JobReport as hotfix for bsc#1057188; should bubble up and go through some callback
+ JobReport::error( excp.asUserHistory() );
+ }
}
PublicKeyData KeyRing::Impl::publicKeyExists( const std::string & id, const Pathname & keyring )
{
- MIL << "Searching key [" << id << "] in keyring " << keyring << endl;
- const std::list<PublicKeyData> & keys( publicKeyData( keyring ) );
- for_( it, keys.begin(), keys.end() )
+ PublicKeyData ret;
+ for ( const PublicKeyData & key : publicKeyData( keyring ) )
{
- if ( id == (*it).id() )
+ if ( key.providesKey( id ) )
{
- return *it;
+ ret = key;
+ break;
}
}
- return PublicKeyData();
+ MIL << (ret ? "Found" : "No") << " key [" << id << "] in keyring " << keyring << endl;
+ return ret;
}
PublicKey KeyRing::Impl::exportKey( const PublicKeyData & keyData, const Pathname & keyring )
return PublicKey( dumpPublicKeyToTmp( keyData.id(), keyring ), keyData );
// Here: key not found
- WAR << "No key " << id << " to export from " << keyring << endl;
+ WAR << "No key [" << id << "] to export from " << keyring << endl;
return PublicKey();
}
filesystem::TmpFile KeyRing::Impl::dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring )
{
filesystem::TmpFile tmpFile( _base_dir, "pubkey-"+id+"-" );
- MIL << "Going to export key " << id << " from " << keyring << " to " << tmpFile.path() << endl;
+ MIL << "Going to export key [" << id << "] from " << keyring << " to " << tmpFile.path() << endl;
std::ofstream os( tmpFile.path().c_str() );
dumpPublicKey( id, keyring, os );
if( signature.empty() || (!PathInfo( signature ).isExist()) )
{
bool res = report->askUserToAcceptUnsignedFile( filedesc, context );
- MIL << "User decision on unsigned file: " << res << endl;
+ MIL << "askUserToAcceptUnsignedFile: " << res << endl;
return res;
}
- // get the id of the signature
+ // get the id of the signature (it might be a subkey id!)
std::string id = readSignatureKeyId( signature );
- // doeskey exists in trusted keyring
+ // does key exists in trusted keyring
PublicKeyData trustedKeyData( publicKeyExists( id, trustedKeyRing() ) );
if ( trustedKeyData )
{
{
// bnc #393160: Comment #30: Compare at least the fingerprint
// in case an attacker created a key the the same id.
+ //
+ // FIXME: bsc#1008325: For keys using subkeys, we'd actually need
+ // to compare the subkey sets, to tell whether a key was updated.
+ // because created() remains unchanged if the primary key is not touched.
+ // For now we wait until a new subkey signs the data and treat it as a
+ // new key (else part below).
if ( trustedKeyData.fingerprint() == generalKeyData.fingerprint()
&& trustedKeyData.created() < generalKeyData.created() )
{
MIL << "Key was updated. Saving new version into trusted keyring: " << generalKeyData << endl;
importKey( exportKey( generalKeyData, generalKeyRing() ), true );
- trustedKeyData = generalKeyData = PublicKeyData(); // invalidated by import.
+ trustedKeyData = publicKeyExists( id, trustedKeyRing() ); // re-read: invalidated by import?
}
}
- if ( ! trustedKeyData ) // invalidated by previous import
- trustedKeyData = publicKeyExists( id, trustedKeyRing() );
+ // it exists, is trusted, does it validate?
report->infoVerify( filedesc, trustedKeyData, context );
-
- // it exists, is trusted, does it validates?
if ( verifyFile( file, signature, trustedKeyRing() ) )
{
return (sigValid_r=true); // signature is actually successfully validated!
}
else
{
- return report->askUserToAcceptVerificationFailed( filedesc, exportKey( trustedKeyData, trustedKeyRing() ), context );
+ bool res = report->askUserToAcceptVerificationFailed( filedesc, exportKey( trustedKeyData, trustedKeyRing() ), context );
+ MIL << "askUserToAcceptVerificationFailed: " << res << endl;
+ return res;
}
}
else
if ( generalKeyData )
{
PublicKey key( exportKey( generalKeyData, generalKeyRing() ) );
- MIL << "Exported key " << id << " to " << key.path() << endl;
- MIL << "Key " << id << " " << key.name() << " is not trusted" << endl;
+ MIL << "Key [" << id << "] " << key.name() << " is not trusted" << endl;
// ok the key is not trusted, ask the user to trust it or not
KeyRingReport::KeyTrust reply = report->askUserToAcceptKey( key, context );
if ( reply == KeyRingReport::KEY_TRUST_TEMPORARILY ||
reply == KeyRingReport::KEY_TRUST_AND_IMPORT )
{
- MIL << "User wants to trust key " << id << " " << key.name() << endl;
- //dumpFile( unKey.path() );
+ MIL << "User wants to trust key [" << id << "] " << key.name() << endl;
Pathname whichKeyring;
if ( reply == KeyRingReport::KEY_TRUST_AND_IMPORT )
{
- MIL << "User wants to import key " << id << " " << key.name() << endl;
+ MIL << "User wants to import key [" << id << "] " << key.name() << endl;
importKey( key, true );
whichKeyring = trustedKeyRing();
}
else
whichKeyring = generalKeyRing();
- // emit key added
+ // does it validate?
+ report->infoVerify( filedesc, generalKeyData, context );
if ( verifyFile( file, signature, whichKeyring ) )
{
- MIL << "File signature is verified" << endl;
return (sigValid_r=true); // signature is actually successfully validated!
}
else
{
- MIL << "File signature check fails" << endl;
- if ( report->askUserToAcceptVerificationFailed( filedesc, key, context ) )
- {
- MIL << "User continues anyway." << endl;
- return true;
- }
- else
- {
- MIL << "User does not want to continue" << endl;
- return false;
- }
+ bool res = report->askUserToAcceptVerificationFailed( filedesc, key, context );
+ MIL << "askUserToAcceptVerificationFailed: " << res << endl;
+ return res;
}
}
else
{
- MIL << "User does not want to trust key " << id << " " << key.name() << endl;
+ MIL << "User does not want to trust key [" << id << "] " << key.name() << endl;
return false;
}
}
else
{
- // unknown key...
+ // signed with an unknown key...
MIL << "File [" << file << "] ( " << filedesc << " ) signed with unknown key [" << id << "]" << endl;
- if ( report->askUserToAcceptUnknownKey( filedesc, id, context ) )
- {
- MIL << "User wants to accept unknown key " << id << endl;
- return true;
- }
- else
- {
- MIL << "User does not want to accept unknown key " << id << endl;
- return false;
- }
+ bool res = report->askUserToAcceptUnknownKey( filedesc, id, context );
+ MIL << "askUserToAcceptUnknownKey: " << res << endl;
+ return res;
}
}
return false;
NULL
};
+ cachedPublicKeyData.setDirty( keyring );
ExternalProgram prog( argv,ExternalProgram::Discard_Stderr, false, -1, true );
- prog.close();
+ if ( prog.close() )
+ ZYPP_THROW(KeyRingException(_("Failed to import key.")));
}
void KeyRing::Impl::deleteKey( const std::string & id, const Pathname & keyring )
NULL
};
+ cachedPublicKeyData.setDirty( keyring );
ExternalProgram prog( argv,ExternalProgram::Discard_Stderr, false, -1, true );
-
- int code = prog.close();
- if ( code )
- ZYPP_THROW(Exception(_("Failed to delete key.")));
- else
- MIL << "Deleted key " << id << " from keyring " << keyring << endl;
+ if ( prog.close() )
+ ZYPP_THROW(KeyRingException(_("Failed to delete key.")));
}
-
std::string KeyRing::Impl::readSignatureKeyId( const Pathname & signature )
{
if ( ! PathInfo( signature ).isFile() )
- ZYPP_THROW(Exception( str::Format(_("Signature file %s not found")) % signature.asString() ));
-
- MIL << "Determining key id if signature " << signature << endl;
- // HACK create a tmp keyring with no keys
- filesystem::TmpDir dir( _base_dir, "fake-keyring" );
- std::string tmppath( dir.path().asString() );
+ ZYPP_THROW(KeyRingException( str::Format(_("Signature file %s not found")) % signature.asString() ));
+ MIL << "Determining key id of signature " << signature << endl;
const char* argv[] =
{
GPG_BINARY,
- "--homedir", tmppath.c_str(),
- "--no-default-keyring",
- "--quiet",
- "--no-tty",
- "--no-greeting",
- "--batch",
- "--status-fd", "1",
+ "--list-packets",
signature.asString().c_str(),
NULL
};
-
- ExternalProgram prog( argv,ExternalProgram::Discard_Stderr, false, -1, true );
-
- std::string line;
- int count = 0;
-
- str::regex rxNoKey( "^\\[GNUPG:\\] NO_PUBKEY (.+)\n$" );
+ ExternalProgram prog( argv ,ExternalProgram::Discard_Stderr, false, -1, true );
+
+ // :signature packet: algo 1, keyid 1397BC53640DB551
+ // version 4, created 1501094968, md5len 0, sigclass 0x00
+ // digest algo 8, begin of digest 15 89
+ // hashed subpkt 2 len 4 (sig created 2017-07-26)
+ // subpkt 16 len 8 (issuer key ID 1397BC53640DB551)
+ // data: [4095 bits]
std::string id;
- for( line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
+ for( std::string line = prog.receiveLine(); !line.empty(); line = prog.receiveLine() )
{
- //MIL << "[" << line << "]" << endl;
- str::smatch what;
- if( str::regex_match( line, what, rxNoKey ) )
+ if ( id.empty() && str::startsWith( line, ":signature packet:" ) )
{
- if ( what.size() >= 1 )
- {
- id = what[1];
- break;
- }
- //dumpRegexpResults( what );
+ static const str::regex rxKeyId( " keyid +([0-9A-Z]+)" );
+ str::smatch what;
+ if( str::regex_match( line, what, rxKeyId ) )
+ id = what[1];
}
}
- if ( count == 0 )
- {
- MIL << "no output" << endl;
- }
-
MIL << "Determined key id [" << id << "] for signature " << signature << endl;
prog.close();
return id;