31 #include <zypp-core/fs/WatchFile> 42 #undef ZYPP_BASE_LOGGER_LOGGROUP 43 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::KeyRing" 57 {
return _keyRingDefaultAccept; }
61 MIL <<
"Set new KeyRing::DefaultAccept: " << value_r << endl;
62 _keyRingDefaultAccept = value_r;
90 data.
set(
"PublicKey", key_r);
91 data.
set(
"KeyContext", keycontext_r);
95 return data.
get<
bool>(
"TrustKey");
102 data.
set(
"Keys", keys_r);
111 data.set(
"KeyDataList", keyDataList_r );
112 data.set(
"KeySigning", keySigning_r );
113 data.set(
"KeyContext", keyContext_r );
121 , _keyring { std::move(keyring_r) }
125 if ( not _context ) {
131 _cache.setDirty( _keyring );
132 return _context.value();
154 bool k = _keyringK->hasChanged();
155 bool p = _keyringP->hasChanged();
160 {
return getData( keyring_r ); }
172 return getData( keyring_r, cache );
179 MIL <<
"Found keys: " << cache_r.
_data << std::endl;
181 return cache_r.
_data;
190 struct ImportKeyCBHelper
198 catch (
const Exception & excp )
200 ERR <<
"Could not import key into rpmdb: " << excp << endl;
213 : _trusted_tmp_dir( baseTmpDir,
"zypp-trusted-kr" )
214 , _general_tmp_dir( baseTmpDir,
"zypp-general-kr" )
215 , _base_dir( baseTmpDir )
217 MIL <<
"Current KeyRing::DefaultAccept: " << _keyRingDefaultAccept << std::endl;
223 MIL <<
"Imported key " << key <<
" to " << (
trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
227 ImportKeyCBHelper emitSignal;
235 emitSignal( exportKey( key, trustedKeyRing() ) );
237 emitSignal( exportKey( hkey, trustedKeyRing() ) );
244 importKey( keyfile_r, trusted_r ? trustedKeyRing() : generalKeyRing() );
249 PublicKeyData keyDataToDel( publicKeyExists(
id,
trusted ? trustedKeyRing() : generalKeyRing() ) );
250 if ( ! keyDataToDel )
252 WAR <<
"Key to delete [" <<
id <<
"] is not in " << (
trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
256 MIL <<
"Deleted key [" <<
id <<
"] from " << (
trusted ?
"trustedKeyRing" :
"generalKeyRing" ) << endl;
263 rpmdbEmitSignal->trustedKeyRemoved( key );
266 emitSignal->trustedKeyRemoved( key );
270 ERR <<
"Could not delete key from rpmmdb: " << excp << endl;
278 if ( _allowPreload && keyring == generalKeyRing() ) {
279 _allowPreload =
false;
286 if ( key.providesKey(
id ) )
292 DBG << (ret ?
"Found" :
"No") <<
" key [" <<
id <<
"] in keyring " << keyring << endl;
298 MIL <<
"preloadCachedKeys into general keyring..." << endl;
304 std::set<Pathname> cachedirs;
306 cachedirs.insert( conf.pubkeyCachePath() );
307 cachedirs.insert(
"/usr/lib/rpm/gnupg/keys" );
308 if (
Pathname r = conf.systemRoot(); r !=
"/" && not r.
empty() ) {
309 cachedirs.insert( r / conf.pubkeyCachePath() );
310 cachedirs.insert( r /
"/usr/lib/rpm/gnupg/keys" );
312 if (
Pathname r = conf.repoManagerRoot(); r !=
"/" && not r.
empty() ) {
313 cachedirs.insert( r / conf.pubkeyCachePath() );
314 cachedirs.insert( r /
"/usr/lib/rpm/gnupg/keys" );
317 std::map<std::string,Pathname> keyCandidates;
318 const str::regex rx {
"^gpg-pubkey-([[:xdigit:]]{8,})(-[[:xdigit:]]{8,})?\\.(asc|key)$" };
319 for (
const auto & cache : cachedirs ) {
321 [&rx,&keyCandidates](
const Pathname & dir_r,
const char *
const file_r )->
bool {
324 Pathname & remember { keyCandidates[what[1]] };
325 if ( remember.empty() ) {
326 remember = dir_r / file_r;
334 for (
const auto & p : keyCandidates ) {
337 const std::string &
id { p.first };
341 if ( manip.keyManagerCtx().importKey( path ) ) {
342 DBG <<
"preload key file " << path << endl;
345 WAR <<
"Skipping: Can't preload key file " << path << endl;
352 return PublicKey( dumpPublicKeyToTmp( keyData.
id(), keyring ), keyData );
359 return PublicKey( dumpPublicKeyToTmp( keyData.
id(), keyring ), keyData );
362 WAR <<
"No key [" <<
id <<
"] to export from " << keyring << endl;
375 MIL <<
"Going to export key [" <<
id <<
"] from " << keyring <<
" to " << tmpFile.
path() << endl;
377 std::ofstream os( tmpFile.
path().
c_str() );
385 const std::list<PublicKeyData> & keys(
publicKeyData( keyring ) );
386 std::list<PublicKey> ret;
388 for_( it, keys.begin(), keys.end() )
390 PublicKey key( exportKey( *it, keyring ) );
391 ret.push_back( key );
392 MIL <<
"Found key " << key << endl;
399 if ( !
PathInfo( keyfile ).isExist() )
406 if ( ! manip.keyManagerCtx().importKey( keyfile ) )
413 if ( ! manip.keyManagerCtx().deleteKey(
id ) )
419 if ( !
PathInfo( signature ).isFile() )
422 MIL <<
"Determining key id of signature " << signature << endl;
425 if ( ! fprs.empty() ) {
426 std::string &
id = fprs.back();
427 MIL <<
"Determined key id [" <<
id <<
"] for signature " << signature << endl;
430 return std::string();
void allowPreload(bool yesno_r)
void importKey(const PublicKey &key, bool trusted=false)
imports a key from a file.
const std::list< PublicKeyData > & publicKeyData()
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 contains multiple keys.
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
const std::list< PublicKeyData > & trustedPublicKeyData()
Functor returning the keyrings data (cached).
static ZConfig & instance()
Singleton ctor.
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.
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
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)
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
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)
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
virtual bool askUserToAcceptUnsignedFile(const std::string &file, const KeyContext &keycontext=KeyContext())
const char * c_str() const
String representation.
KeyRing(const Pathname &baseTmpDir)
Default ctor.
KeyRing::Impl & pimpl()
Access to private functions for the KeyRingWorkflow implementations.
std::list< PublicKeyData > trustedPublicKeyData()
Get a list of trusted public key data in the keyring (key data only)
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.
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
bool verify(const Pathname &file, const Pathname &signature)
Tries to verify file using signature, returns true on success.
static KeyManagerCtx createForOpenPGP()
Creates a new KeyManagerCtx for PGP using a volatile temp.
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...
callback::SendReport< target::rpm::KeyRingSignals > _rpmdbEmitSignal
bool exportKey(const std::string &id, std::ostream &stream)
Exports the key with id into the given stream, returns true on success.
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)
virtual void report(const UserData &userData_r=UserData())
The most generic way of sending/receiving data.
KeyTrust
User reply options for the askUserToTrustKey callback.
bool empty() const
Test for an empty path.
PublicKeyData trustedPublicKeyExists(const std::string &id)
static void setDefaultAccept(DefaultAccept value_r)
Set the active accept bits.
bool verifyFileSignature(const Pathname &file, const Pathname &signature)
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.
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring)
Interim helper class to collect global options and settings.
IMPL_PTR_TYPE(Application)
std::list< PublicKey > trustedPublicKeys()
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
void importKey(const PublicKey &key, bool trusted=false)
KeyManagerCtx & keyManagerCtx()
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.
User has chosen not to trust the key.
void assertCache(const Pathname &keyring_r)
virtual KeyTrust askUserToAcceptKey(const PublicKey &key, const KeyContext &keycontext=KeyContext())
Ask user to trust and/or import the key to trusted keyring.
std::list< PublicKeyData > listKeys()
Returns a list of all public keys found in the current keyring.
static DefaultAccept defaultAccept()
Get the active accept bits.
RW_pointer< Impl > _pimpl
Pointer to implementation.
Regular expression match result.
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
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 containing 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.
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.
static constexpr const char * KEYS_NOT_IMPORTED_REPORT
void reportAutoImportKey(const std::list< PublicKeyData > &keyDataList_r, const PublicKeyData &keySigning_r, const KeyContext &keyContext_r)
Notify that a repository auto imported new package signing keys.
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
Wrapper class for ::stat/::lstat.
bool regex_match(const std::string &s, smatch &matches, const regex ®ex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
std::list< std::string > readSignatureFingerprints(const Pathname &signature)
Reads all fingerprints from the signature file , returns a list of all found fingerprints.
std::list< PublicKeyData > _data
void setDirty(const Pathname &keyring_r)
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
static constexpr const char * REPORT_AUTO_IMPORT_KEY
void allowPreload(bool yesno_r)
The general keyring may be populated with known keys stored on the system.
Easy-to use interface to the ZYPP dependency resolver.
callback::SendReport< KeyRingSignals > _emitSignal
PublicKeyData publicKeyExists(const std::string &id)
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.
static constexpr const char * ACCEPT_PACKAGE_KEY_REQUEST
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.
Helper providing on demand a KeyManagerCtx to manip the cached keyring.