37 #undef ZYPP_BASE_LOGGER_LOGGROUP 38 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::KeyRing" 52 {
return _keyRingDefaultAccept; }
56 MIL <<
"Set new KeyRing::DefaultAccept: " << value_r << endl;
57 _keyRingDefaultAccept = value_r;
70 return KEY_TRUST_TEMPORARILY;
72 return KEY_TRUST_AND_IMPORT;
73 return KEY_DONT_TRUST;
84 UserData data(ACCEPT_PACKAGE_KEY_REQUEST);
85 data.
set(
"PublicKey", key_r);
86 data.
set(
"KeyContext", keycontext_r);
90 return data.
get<
bool>(
"TrustKey");
96 UserData data(KEYS_NOT_IMPORTED_REPORT);
97 data.
set(
"Keys", keys_r);
112 const std::list<PublicKeyData> & operator()(
const Pathname & keyring_r )
const 113 {
return getData( keyring_r ); }
115 void setDirty(
const Pathname & keyring_r )
129 void assertCache(
const Pathname & keyring_r )
138 bool hasChanged()
const 152 typedef std::map<Pathname,Cache> CacheMap;
154 const std::list<PublicKeyData> & getData(
const Pathname & keyring_r )
const 158 cache.assertCache( keyring_r );
159 return getData( keyring_r, cache );
162 const std::list<PublicKeyData> & getData(
const Pathname & keyring_r, Cache & cache_r )
const 164 if ( cache_r.hasChanged() ) {
167 if (ctx->setHomedir(keyring_r)) {
168 std::list<PublicKeyData> foundKeys = ctx->listKeys();
169 cache_r._data.swap(foundKeys);
172 MIL <<
"Found keys: " << cache_r._data << endl;
174 return cache_r._data;
190 : _trusted_tmp_dir( baseTmpDir,
"zypp-trusted-kr" )
191 , _general_tmp_dir( baseTmpDir,
"zypp-general-kr" )
192 , _base_dir( baseTmpDir )
194 MIL <<
"Current KeyRing::DefaultAccept: " << _keyRingDefaultAccept << endl;
199 void deleteKey(
const std::string &
id,
bool trusted );
204 {
return bool(publicKeyExists(
id, trustedKeyRing() )); }
206 {
return publicKeyExists(
id, trustedKeyRing() ) || publicKeyExists(
id, generalKeyRing() ); }
218 void dumpPublicKey(
const std::string &
id,
bool trusted, std::ostream & stream )
219 {
dumpPublicKey(
id, ( trusted ? trustedKeyRing() : generalKeyRing() ), stream ); }
222 {
return exportKey( keyData, generalKeyRing() ); }
224 {
return exportKey( keyData, trustedKeyRing() ); }
229 {
return verifyFile( file, signature, generalKeyRing() ); }
231 {
return verifyFile( file, signature, trustedKeyRing() ); }
234 {
return publicKeyExists(
id, trustedKeyRing());}
243 {
return exportKey( key.
keyData(), keyring ); }
252 {
return cachedPublicKeyData( keyring ); }
258 {
return _general_tmp_dir.path(); }
260 {
return _trusted_tmp_dir.path(); }
280 struct ImportKeyCBHelper
282 void operator()(
const PublicKey & key_r )
290 ERR <<
"Could not import key into rpmdb: " << excp << endl;
305 importKey( key.
path(), trusted ? trustedKeyRing() : generalKeyRing() );
306 MIL <<
"Imported key " << key <<
" to " << (trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
310 ImportKeyCBHelper emitSignal;
318 emitSignal( exportKey( key, trustedKeyRing() ) );
320 emitSignal( exportKey( hkey, trustedKeyRing() ) );
327 importKey( keyfile_r, trusted_r ? trustedKeyRing() : generalKeyRing() );
332 PublicKeyData keyDataToDel( publicKeyExists(
id, trusted ? trustedKeyRing() : generalKeyRing() ) );
333 if ( ! keyDataToDel )
335 WAR <<
"Key to delete [" <<
id <<
"] is not in " << (trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
338 deleteKey(
id, trusted ? trustedKeyRing() : generalKeyRing() );
339 MIL <<
"Deleted key [" <<
id <<
"] from " << (trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
346 rpmdbEmitSignal->trustedKeyRemoved( key );
349 emitSignal->trustedKeyRemoved( key );
353 ERR <<
"Could not delete key from rpmmdb: " << excp << endl;
364 if ( key.providesKey(
id ) )
370 MIL << (ret ?
"Found" :
"No") <<
" key [" <<
id <<
"] in keyring " << keyring << endl;
376 return PublicKey( dumpPublicKeyToTmp( keyData.
id(), keyring ), keyData );
383 return PublicKey( dumpPublicKeyToTmp( keyData.
id(), keyring ), keyData );
386 WAR <<
"No key [" <<
id <<
"] to export from " << keyring << endl;
394 if (!ctx || !ctx->setHomedir(keyring))
396 ctx->exportKey(
id, stream);
402 MIL <<
"Going to export key [" <<
id <<
"] from " << keyring <<
" to " << tmpFile.
path() << endl;
404 std::ofstream os( tmpFile.
path().
c_str() );
415 MIL <<
"Going to verify signature for " << filedesc <<
" ( " << file <<
" ) with " << signature << endl;
420 bool res = report->askUserToAcceptUnsignedFile( filedesc, context );
421 MIL <<
"askUserToAcceptUnsignedFile: " << res << endl;
429 PublicKeyData trustedKeyData( publicKeyExists(
id, trustedKeyRing() ) );
430 if ( trustedKeyData )
432 MIL <<
"Key is trusted: " << trustedKeyData << endl;
436 PublicKeyData generalKeyData( publicKeyExists(
id, generalKeyRing() ) );
437 if ( generalKeyData )
450 MIL <<
"Key was updated. Saving new version into trusted keyring: " << generalKeyData << endl;
451 importKey( exportKey( generalKeyData, generalKeyRing() ),
true );
452 trustedKeyData = publicKeyExists(
id, trustedKeyRing() );
457 report->infoVerify( filedesc, trustedKeyData, context );
458 if ( verifyFile( file, signature, trustedKeyRing() ) )
460 return (sigValid_r=
true);
464 bool res = report->askUserToAcceptVerificationFailed( filedesc, exportKey( trustedKeyData, trustedKeyRing() ), context );
465 MIL <<
"askUserToAcceptVerificationFailed: " << res << endl;
471 PublicKeyData generalKeyData( publicKeyExists(
id, generalKeyRing() ) );
472 if ( generalKeyData )
474 PublicKey key( exportKey( generalKeyData, generalKeyRing() ) );
475 MIL <<
"Key [" <<
id <<
"] " << key.
name() <<
" is not trusted" << endl;
482 MIL <<
"User wants to trust key [" <<
id <<
"] " << key.
name() << endl;
487 MIL <<
"User wants to import key [" <<
id <<
"] " << key.
name() << endl;
489 whichKeyring = trustedKeyRing();
492 whichKeyring = generalKeyRing();
495 report->infoVerify( filedesc, generalKeyData, context );
496 if ( verifyFile( file, signature, whichKeyring ) )
498 return (sigValid_r=
true);
502 bool res = report->askUserToAcceptVerificationFailed( filedesc, key, context );
503 MIL <<
"askUserToAcceptVerificationFailed: " << res << endl;
509 MIL <<
"User does not want to trust key [" <<
id <<
"] " << key.
name() << endl;
516 MIL <<
"File [" << file <<
"] ( " << filedesc <<
" ) signed with unknown key [" <<
id <<
"]" << endl;
517 bool res = report->askUserToAcceptUnknownKey( filedesc,
id, context );
518 MIL <<
"askUserToAcceptUnknownKey: " << res << endl;
527 const std::list<PublicKeyData> & keys(
publicKeyData( keyring ) );
528 std::list<PublicKey> ret;
530 for_( it, keys.begin(), keys.end() )
532 PublicKey key( exportKey( *it, keyring ) );
533 ret.push_back( key );
534 MIL <<
"Found key " << key << endl;
541 if ( !
PathInfo( keyfile ).isExist() )
548 if(!ctx || !ctx->setHomedir(keyring))
551 cachedPublicKeyData.setDirty( keyring );
552 if(!ctx->importKey(keyfile))
563 if(!ctx->setHomedir(keyring)) {
567 if(!ctx->deleteKey(
id)){
571 cachedPublicKeyData.setDirty( keyring );
576 if ( !
PathInfo( signature ).isFile() )
579 MIL <<
"Determining key id of signature " << signature << endl;
583 return std::string();
586 std::list<std::string> fprs = ctx->readSignatureFingerprints(signature);
588 std::string &
id = fprs.back();
589 MIL <<
"Determined key id [" <<
id <<
"] for signature " << signature << endl;
592 return std::string();
598 if (!ctx || !ctx->setHomedir(keyring))
601 return ctx->verify(file, signature);
void importKey(const PublicKey &key, bool trusted=false)
imports a key from a file.
const std::list< PublicKeyData > & publicKeyData()
static Ptr createForOpenPGP()
Creates a new KeyManagerCtx for PGP.
PublicKey exportTrustedPublicKey(const PublicKeyData &keyData)
Export a trusted public key identified by its key data.
PublicKey exportPublicKey(const PublicKeyData &keyData)
void dumpPublicKey(const std::string &id, bool trusted, std::ostream &stream)
void deleteKey(const std::string &id, bool trusted)
PublicKey exportKey(const std::string &id, const Pathname &keyring)
static bool error(const std::string &msg_r, const UserData &userData_r=UserData())
send error text
bool isKeyTrusted(const std::string &id)
const std::list< PublicKeyData > & hiddenKeys() const
Additional keys data in case the ASCII armored blob containes multiple keys.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
const std::list< PublicKeyData > & trustedPublicKeyData()
const PublicKeyData & keyData() const
The public keys data (.
PublicKey exportKey(const PublicKey &key, const Pathname &keyring)
This basically means, we knew the key, but it was not trusted.
PublicKey exportPublicKey(const PublicKeyData &keyData)
Export a public key identified by its key data.
Class representing one GPG Public Keys data.
bool isKeyKnown(const std::string &id)
void reportNonImportedKeys(const std::set< Edition > &keys_r)
Notify the user about keys that were not imported from the rpm key database into zypp keyring...
void dumpPublicKey(const std::string &id, bool trusted, std::ostream &stream)
PublicKeyData publicKeyExists(const std::string &id, const Pathname &keyring)
Get PublicKeyData for ID (false if ID is not found).
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
std::list< PublicKey > trustedPublicKeys()
Get a list of trusted public keys in the keyring (incl.
bool verifyFile(const Pathname &file, const Pathname &signature, const Pathname &keyring)
virtual bool askUserToAcceptUnsignedFile(const std::string &file, const KeyContext &keycontext=KeyContext())
const char * c_str() const
String representation.
KeyRing(const Pathname &baseTmpDir)
Default ctor.
std::list< PublicKeyData > trustedPublicKeyData()
Get a list of trusted public key data in the keyring (key data only)
bool verifyFileSignatureWorkflow(const Pathname &file, const std::string &filedesc, const Pathname &signature, bool &sigValid_r, const KeyContext &keycontext=KeyContext())
Follows a signature verification interacting with the user.
bool askUserToAcceptPackageKey(const PublicKey &key_r, const KeyContext &keycontext_r=KeyContext())
Ask user to trust and/or import the package key to trusted keyring, using ReportBase::report.
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Provide a new empty temporary file and delete it when no longer needed.
virtual bool askUserToAcceptUnknownKey(const std::string &file, const std::string &id, const KeyContext &keycontext=KeyContext())
we DONT know the key, only its id, but we have never seen it, the difference with trust key is that i...
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
callback::SendReport< target::rpm::KeyRingSignals > _rpmdbEmitSignal
Remember a files attributes to detect content changes.
virtual void infoVerify(const std::string &file_r, const PublicKeyData &keyData_r, const KeyContext &keycontext=KeyContext())
Informal callback showing the trusted key that will be used for verification.
PublicKey exportTrustedPublicKey(const PublicKeyData &keyData)
KeyTrust
User reply options for the askUserToTrustKey callback.
bool empty() const
Test for an empty path.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Date created() const
Creation / last modification date (latest selfsig).
PublicKeyData trustedPublicKeyExists(const std::string &id)
static void setDefaultAccept(DefaultAccept value_r)
Set the active accept bits.
Provide a new empty temporary directory and recursively delete it when no longer needed.
filesystem::TmpDir _trusted_tmp_dir
bool verifyFileSignature(const Pathname &file, const Pathname &signature)
std::list< PublicKeyData > _data
bool set(const std::string &key_r, AnyType val_r)
Set the value for key (nonconst version always returns true).
const std::string & asString() const
String representation.
bool verifyFileSignatureWorkflow(const Pathname &file, const std::string &filedesc, const Pathname &signature, bool &sigValid_r, const KeyContext &keycontext=KeyContext())
bool isExist() const
Return whether valid stat info exists.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
const Pathname generalKeyRing() const
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring)
IMPL_PTR_TYPE(Application)
std::list< PublicKey > trustedPublicKeys()
scoped_ptr< WatchFile > _keyringP
void importKey(const PublicKey &key, bool trusted=false)
Impl(const Pathname &baseTmpDir)
bool isKeyKnown(const std::string &id)
true if the key id is knows, that means at least exist on the untrusted keyring
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
Initial import from RpmDb.
std::string fingerprint() const
Key fingerprint.
virtual KeyTrust askUserToAcceptKey(const PublicKey &key, const KeyContext &keycontext=KeyContext())
Ask user to trust and/or import the key to trusted keyring.
scoped_ptr< WatchFile > _keyringK
static DefaultAccept defaultAccept()
Get the active accept bits.
RW_pointer< Impl > _pimpl
Pointer to implementation.
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
std::list< PublicKeyData > publicKeyData()
Get a list of public key data in the keyring (key data only)
Base class for Exception.
Pathname path() const
File containig the ASCII armored key.
std::string id() const
Key ID.
const Tp & get(const std::string &key_r) const
Pass back a const Tp & reference to key_r value.
const Pathname trustedKeyRing() const
std::list< PublicKey > publicKeys()
Typesafe passing of user data via callbacks.
void deleteKey(const std::string &id, bool trusted=false)
removes a key from the keyring.
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
shared_ptr< KeyManagerCtx > Ptr
Wrapper class for ::stat/::lstat.
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring)
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
bool isKeyTrusted(const std::string &id)
true if the key id is trusted
Easy-to use interface to the ZYPP dependency resolver.
callback::SendReport< KeyRingSignals > _emitSignal
std::string readSignatureKeyId(const Pathname &signature)
reads the public key id from a signature
bool verifyFileSignature(const Pathname &file, const Pathname &signature)
Verifies a file against a signature, with no user interaction.
std::string readSignatureKeyId(const Pathname &signature)
virtual bool askUserToAcceptVerificationFailed(const std::string &file, const PublicKey &key, const KeyContext &keycontext=KeyContext())
The file filedesc is signed but the verification failed.
std::list< PublicKey > publicKeys()
Get a list of public keys in the keyring (incl.
filesystem::TmpDir _general_tmp_dir