libzypp: zypp-common/private/keyring_p.h Source File

libzypp 17.38.5

keyring_p.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_PRIVATE_KEYRINGIMPL_H
13#define ZYPP_PRIVATE_KEYRINGIMPL_H
14
15#include <zypp-core/ng/base/signals.h>
16#include <zypp-core/fs/TmpPath.h>
17#include <zypp-core/fs/WatchFile>
18#include <zypp-common/KeyManager.h>
19#include <zypp-core/ng/pipelines/expected.h>
20#include <optional>
21
22namespace zypp {
23
24
32 struct CachedPublicKeyData : private base::NonCopyable
33 {
34 const std::list<PublicKeyData> & operator()( const Pathname & keyring_r ) const;
35
36 void setDirty( const Pathname & keyring_r );
37
44 struct Manip {
45 NON_COPYABLE_BUT_MOVE( Manip );
46 Manip( CachedPublicKeyData & cache_r, Pathname keyring_r );
47
48 KeyManagerCtx & keyManagerCtx();
49 private:
50 CachedPublicKeyData & _cache;
51 Pathname _keyring;
52 std::optional<KeyManagerCtx> _context;
53 };
54
55
57 Manip manip( Pathname keyring_r );
58
59 private:
60 struct Cache
61 {
62 Cache();
63
64 void setDirty();
65
66 void assertCache( const Pathname & keyring_r );
67
68 bool hasChanged() const;
69
70 std::list<PublicKeyData> _data;
71
72 private:
73
74 scoped_ptr<WatchFile> _keyringK;
75 scoped_ptr<WatchFile> _keyringP;
76 };
77
78 using CacheMap = std::map<Pathname, Cache>;
79
80 const std::list<PublicKeyData> & getData( const Pathname & keyring_r ) const;
81
82 const std::list<PublicKeyData> & getData( const Pathname & keyring_r, Cache & cache_r ) const;
83
84 mutable CacheMap _cacheMap;
85 };
86
90 class KeyRingImpl
91 {
92 public:
93 KeyRingImpl( const Pathname & baseTmpDir );
94
95 void importKey( const PublicKey & key, bool trusted = false );
96 void multiKeyImport( const Pathname & keyfile_r, bool trusted_r = false );
97 void deleteKey( const std::string & id, bool trusted );
98
99 std::string readSignatureKeyId( const Pathname & signature );
100
101 bool isKeyTrusted( const std::string & id )
102 { return bool(publicKeyExists( id, trustedKeyRing() )); }
103 bool isKeyKnown( const std::string & id )
104 { return publicKeyExists( id, trustedKeyRing() ) || publicKeyExists( id, generalKeyRing() ); }
105
106 std::list<PublicKey> trustedPublicKeys()
107 { return publicKeys( trustedKeyRing() ); }
108 std::list<PublicKey> publicKeys()
109 { return publicKeys( generalKeyRing() ); }
110
111 const std::list<PublicKeyData> & trustedPublicKeyData()
112 { return publicKeyData( trustedKeyRing() ); }
113 const std::list<PublicKeyData> & publicKeyData()
114 { return publicKeyData( generalKeyRing() ); }
115
116 void dumpPublicKey( const std::string & id, bool trusted, std::ostream & stream )
117 { dumpPublicKey( id, ( trusted ? trustedKeyRing() : generalKeyRing() ), stream ); }
118
119 PublicKey exportPublicKey( const PublicKeyData & keyData )
120 { return exportKey( keyData, generalKeyRing() ); }
121 PublicKey exportTrustedPublicKey( const PublicKeyData & keyData )
122 { return exportKey( keyData, trustedKeyRing() ); }
123
124 bool verifyFileSignature( const Pathname & file, const Pathname & signature )
125 { return verifyFile( file, signature, generalKeyRing() ); }
126 bool verifyFileTrustedSignature( const Pathname & file, const Pathname & signature )
127 { return verifyFile( file, signature, trustedKeyRing() ); }
128
129 PublicKeyData publicKeyExists( const std::string & id )
130 { return publicKeyExists(id, generalKeyRing());}
131 PublicKeyData trustedPublicKeyExists( const std::string & id )
132 { return publicKeyExists(id, trustedKeyRing());}
133
134 void allowPreload( bool yesno_r )
135 { _allowPreload = yesno_r; }
136
138 CachedPublicKeyData::Manip keyRingManip( const Pathname & keyring )
139 { return cachedPublicKeyData.manip( keyring ); }
140
141 bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
142 void importKey( const Pathname & keyfile, const Pathname & keyring );
143
144 PublicKey exportKey( const std::string & id, const Pathname & keyring );
145 PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring );
146 PublicKey exportKey( const PublicKey & key, const Pathname & keyring )
147 { return exportKey( key.keyData(), keyring ); }
148
149 void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream );
150 filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring );
151
152 void deleteKey( const std::string & id, const Pathname & keyring );
153
154 std::list<PublicKey> publicKeys( const Pathname & keyring);
155 const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring )
156 { return cachedPublicKeyData( keyring ); }
157
159 PublicKeyData publicKeyExists( const std::string & id, const Pathname & keyring );
161 void preloadCachedKeys();
162
163 const Pathname generalKeyRing() const
164 { return _general_tmp_dir.path(); }
165 const Pathname trustedKeyRing() const
166 { return _trusted_tmp_dir.path(); }
167
168 zyppng::SignalProxy<void ( const PublicKey &/*key*/ )> sigTrustedKeyAdded(){
169 return _sigTrustedKeyAdded;
170 }
171
172 zyppng::SignalProxy<void ( const PublicKey &/*key*/ )> sigTrustedKeyRemoved() {
173 return _sigTrustedKeyRemoved;
174 }
175
176 private:
177 // Used for trusted and untrusted keyrings
178 filesystem::TmpDir _trusted_tmp_dir;
179 filesystem::TmpDir _general_tmp_dir;
180 Pathname _base_dir;
181 bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
182
188 CachedPublicKeyData cachedPublicKeyData;
189
190 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyAdded;
191 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyRemoved;
192 };
193
194}
195
196
197#endif
KeyManager.h
bool
scoped_ptr
zypp::KeyManagerCtx
Definition KeyManager.h:31
zypp::KeyRingImpl::exportKey
PublicKey exportKey(const PublicKey &key, const Pathname &keyring)
Definition keyring_p.h:146
zypp::KeyRingImpl::_sigTrustedKeyAdded
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
Definition keyring_p.h:190
zypp::KeyRingImpl::publicKeyExists
PublicKeyData publicKeyExists(const std::string &id)
Definition keyring_p.h:129
zypp::KeyRingImpl::trustedPublicKeys
std::list< PublicKey > trustedPublicKeys()
Definition keyring_p.h:106
zypp::KeyRingImpl::publicKeyData
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring)
Definition keyring_p.h:155
zypp::KeyRingImpl::exportTrustedPublicKey
PublicKey exportTrustedPublicKey(const PublicKeyData &keyData)
Definition keyring_p.h:121
zypp::KeyRingImpl::sigTrustedKeyRemoved
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyRemoved()
Definition keyring_p.h:172
zypp::KeyRingImpl::importKey
void importKey(const PublicKey &key, bool trusted=false)
Definition keyring_p.cc:117
zypp::KeyRingImpl::exportPublicKey
PublicKey exportPublicKey(const PublicKeyData &keyData)
Definition keyring_p.h:119
zypp::KeyRingImpl::isKeyKnown
bool isKeyKnown(const std::string &id)
Definition keyring_p.h:103
zypp::KeyRingImpl::allowPreload
void allowPreload(bool yesno_r)
Definition keyring_p.h:134
zypp::KeyRingImpl::_general_tmp_dir
filesystem::TmpDir _general_tmp_dir
Definition keyring_p.h:179
zypp::KeyRingImpl::KeyRingImpl
KeyRingImpl(const Pathname &baseTmpDir)
Definition keyring_p.cc:110
zypp::KeyRingImpl::publicKeyData
const std::list< PublicKeyData > & publicKeyData()
Definition keyring_p.h:113
zypp::KeyRingImpl::multiKeyImport
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
Definition keyring_p.cc:138
zypp::KeyRingImpl::_allowPreload
bool _allowPreload
Definition keyring_p.h:181
zypp::KeyRingImpl::publicKeys
std::list< PublicKey > publicKeys()
Definition keyring_p.h:108
zypp::KeyRingImpl::verifyFileTrustedSignature
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
Definition keyring_p.h:126
zypp::KeyRingImpl::preloadCachedKeys
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
Definition keyring_p.cc:180
zypp::KeyRingImpl::dumpPublicKey
void dumpPublicKey(const std::string &id, bool trusted, std::ostream &stream)
Definition keyring_p.h:116
zypp::KeyRingImpl::_sigTrustedKeyRemoved
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyRemoved
Definition keyring_p.h:191
zypp::KeyRingImpl::trustedPublicKeyData
const std::list< PublicKeyData > & trustedPublicKeyData()
Definition keyring_p.h:111
zypp::KeyRingImpl::generalKeyRing
const Pathname generalKeyRing() const
Definition keyring_p.h:163
zypp::KeyRingImpl::verifyFileSignature
bool verifyFileSignature(const Pathname &file, const Pathname &signature)
Definition keyring_p.h:124
zypp::KeyRingImpl::_base_dir
Pathname _base_dir
Definition keyring_p.h:180
zypp::KeyRingImpl::trustedKeyRing
const Pathname trustedKeyRing() const
Definition keyring_p.h:165
zypp::KeyRingImpl::readSignatureKeyId
std::string readSignatureKeyId(const Pathname &signature)
Definition keyring_p.cc:301
zypp::KeyRingImpl::keyRingManip
CachedPublicKeyData::Manip keyRingManip(const Pathname &keyring)
Impl helper providing on demand a KeyManagerCtx to manip a cached keyring.
Definition keyring_p.h:138
zypp::KeyRingImpl::exportKey
PublicKey exportKey(const std::string &id, const Pathname &keyring)
Definition keyring_p.cc:239
zypp::KeyRingImpl::isKeyTrusted
bool isKeyTrusted(const std::string &id)
Definition keyring_p.h:101
zypp::KeyRingImpl::verifyFile
bool verifyFile(const Pathname &file, const Pathname &signature, const Pathname &keyring)
Definition keyring_p.cc:317
zypp::KeyRingImpl::deleteKey
void deleteKey(const std::string &id, bool trusted)
Definition keyring_p.cc:143
zypp::KeyRingImpl::sigTrustedKeyAdded
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyAdded()
Definition keyring_p.h:168
zypp::KeyRingImpl::trustedPublicKeyExists
PublicKeyData trustedPublicKeyExists(const std::string &id)
Definition keyring_p.h:131
zypp::KeyRingImpl::_trusted_tmp_dir
filesystem::TmpDir _trusted_tmp_dir
Definition keyring_p.h:178
zypp::KeyRingImpl::dumpPublicKeyToTmp
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring)
Definition keyring_p.cc:256
zypp::KeyRingImpl::cachedPublicKeyData
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition keyring_p.h:188
zypp::PublicKeyData
Class representing one GPG Public Keys data.
Definition PublicKey.h:201
zypp::PublicKey
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition PublicKey.h:358
zypp::PublicKey::keyData
const PublicKeyData & keyData() const
The public keys data (.
Definition PublicKey.cc:637
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::TmpDir
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:173
zypp::filesystem::TmpFile
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:118
zyppng::SignalProxy
Definition signals.h:325
zyppng::Signal
Definition signals.h:151
expected.h
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::keyring
Definition KeyRingContexts.cc:18
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
signals.h
zypp::CachedPublicKeyData::Cache
Definition keyring_p.h:61
zypp::CachedPublicKeyData::Cache::_keyringP
scoped_ptr< WatchFile > _keyringP
Definition keyring_p.h:75
zypp::CachedPublicKeyData::Cache::setDirty
void setDirty()
Definition keyring_p.cc:62
zypp::CachedPublicKeyData::Cache::assertCache
void assertCache(const Pathname &keyring_r)
Definition keyring_p.cc:68
zypp::CachedPublicKeyData::Cache::hasChanged
bool hasChanged() const
Definition keyring_p.cc:77
zypp::CachedPublicKeyData::Cache::_data
std::list< PublicKeyData > _data
Definition keyring_p.h:70
zypp::CachedPublicKeyData::Cache::Cache
Cache()
Definition keyring_p.cc:60
zypp::CachedPublicKeyData::Cache::_keyringK
scoped_ptr< WatchFile > _keyringK
Definition keyring_p.h:74
zypp::CachedPublicKeyData::Manip
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition keyring_p.h:44
zypp::CachedPublicKeyData::Manip::_context
std::optional< KeyManagerCtx > _context
Definition keyring_p.h:52
zypp::CachedPublicKeyData::Manip::_keyring
Pathname _keyring
Definition keyring_p.h:51
zypp::CachedPublicKeyData::Manip::keyManagerCtx
KeyManagerCtx & keyManagerCtx()
Definition keyring_p.cc:49
zypp::CachedPublicKeyData::Manip::Manip
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
Definition keyring_p.cc:44
zypp::CachedPublicKeyData::Manip::NON_COPYABLE_BUT_MOVE
NON_COPYABLE_BUT_MOVE(Manip)
zypp::CachedPublicKeyData::Manip::_cache
CachedPublicKeyData & _cache
Definition keyring_p.h:50
zypp::CachedPublicKeyData
Functor returning the keyrings data (cached).
Definition keyring_p.h:33
zypp::CachedPublicKeyData::setDirty
void setDirty(const Pathname &keyring_r)
Definition keyring_p.cc:87
zypp::CachedPublicKeyData::_cacheMap
CacheMap _cacheMap
Definition keyring_p.h:84
zypp::CachedPublicKeyData::operator()
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
Definition keyring_p.cc:84
zypp::CachedPublicKeyData::getData
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Definition keyring_p.cc:92
zypp::CachedPublicKeyData::manip
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition keyring_p.cc:90
zypp::CachedPublicKeyData::CacheMap
std::map< Pathname, Cache > CacheMap
Definition keyring_p.h:78
TmpPath.h