libzypp 17.38.15

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
22 namespace 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 enum class Ring : bool {
94 General = false,
95 Trusted = true,
96 };
97
98 zyppng::SignalProxy<void ( const PublicKey &/*key*/ )> sigTrustedKeyAdded()
99 { return _sigTrustedKeyAdded; }
100
101 zyppng::SignalProxy<void ( const PublicKey &/*key*/ )> sigTrustedKeyRemoved()
102 { return _sigTrustedKeyRemoved; }
103
104 public:
105 KeyRingImpl( const Pathname & baseTmpDir );
106
107 public:
108 void allowPreload( bool yesno_r ) //< General keyring may be preloaded with keys cached on the system.
109 { _allowPreload = yesno_r; }
110
115 void importKey ( const PublicKey & key, const Ring ring );
116 void importKeys( const std::list<PublicKey> & keys, const Ring ring );
117
118 PublicKey exportKey( const std::string & id, const Ring ring ) const
119 { return exportKey( id, keyRingPath( ring ) ); }
120 PublicKey exportKey( const PublicKeyData & keyData, const Ring ring ) const
121 { return exportKey( keyData, keyRingPath( ring ) ); }
122 PublicKey exportKey( const PublicKey & key, const Ring ring ) const
123 { return exportKey( key.keyData(), ring ); }
124
125 void deleteKey( const std::string & id, const Ring ring );
126
127 void dumpPublicKey( const std::string & id, const Ring ring, std::ostream & stream )
128 { dumpPublicKey( id, keyRingPath( ring ), stream ); }
129
130 bool verifyFile( const Pathname & file, const Pathname & signature, const Ring ring )
131 { return verifyFile( file, signature, keyRingPath( ring ) ); }
132
133 std::string readSignatureKeyId( const Pathname & signature );
134
140 void multiKeyImport( const Pathname & keyfile_r, const Ring ring );
141
142 public:
143 enum class MustUpdate {
144 Present = 0,
145 Missing = 1,
146 Update = 2,
147 };
148
149 MustUpdate mustUpdateData( const PublicKeyData & keyData, const Ring ring ) const
150 {
151 PublicKeyData ringKeyData { publicKeyData( keyData, ring ) };
152 if ( not ringKeyData ) {
153 return MustUpdate::Missing;
154 }
155 if ( keyData.isUpdateFor( ringKeyData ) ) {
156 return MustUpdate::Update;
157 }
158 return MustUpdate::Present;
159 }
160
161 bool isKeyTrusted( const std::string & id ) const
162 { return bool(publicKeyData( id, Ring::Trusted )); }
163
164 bool isKeyKnown( const std::string & id ) const
165 { return publicKeyData( id, Ring::Trusted ) || publicKeyData( id, Ring::General ); }
166
167 PublicKeyData publicKeyData( const std::string & id, const Ring ring ) const
168 { return publicKeyData( id, keyRingPath( ring ) ); }
169 PublicKeyData publicKeyData( const PublicKeyData & keyData, const Ring ring ) const
170 { return publicKeyData( keyData.id(), ring ); }
171 PublicKeyData publicKeyData( const PublicKey & key, const Ring ring ) const
172 { return publicKeyData( key.keyData().id(), ring ); }
173
174 const std::list<PublicKeyData> & publicKeyData( const Ring ring ) const
175 { return publicKeyData( keyRingPath( ring ) ); }
176
177 std::list<PublicKey> publicKeys( const Ring ring ) const
178 { return publicKeys( keyRingPath( ring ) ); }
179
180
181 private:
183 class SendKeys
184 {
185 public:
187 SendKeys( KeyRingImpl & keyRing, const Pathname & source, const Pathname & target );
189 SendKeys( KeyRingImpl & keyRing, const Ring source, const Ring target );
190 SendKeys( const SendKeys & ) = delete;
191 SendKeys( SendKeys && ) = delete;
192 SendKeys & operator=( const SendKeys & ) = delete;
193 SendKeys & operator=( SendKeys && ) = delete;
194
198 void operator()( const std::string & id );
199
200 private:
201 KeyManagerCtx _source;
202 CachedPublicKeyData::Manip _target;
203 };
204
205 private:
206 const Pathname keyRingPath( const Ring ring ) const
207 { return ring == Ring::General ? _general_tmp_dir.path() : _trusted_tmp_dir.path(); }
208
209 void importKey( const Pathname & keyfile, const Pathname & keyring );
210
211 PublicKey exportKey( const std::string & id, const Pathname & keyring ) const;
212 PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring ) const;
213
214 void deleteKey( const std::string & id, const Pathname & keyring );
215
216 PublicKeyData publicKeyData( const std::string & id, const Pathname & keyring ) const;
217
218 const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring ) const
219 {
220 if ( _allowPreload && keyring == keyRingPath( Ring::General ) ) {
221 KeyRingImpl * lazyinit = const_cast<KeyRingImpl*>(this);
222 lazyinit->_allowPreload = false;
223 lazyinit->preloadCachedKeys();
224 }
225 return cachedPublicKeyData( keyring );
226 }
227
228 std::list<PublicKey> publicKeys( const Pathname & keyring ) const;
229
230 void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream ) const;
231 filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring ) const;
232
233 bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
234
235 private:
237 void preloadCachedKeys();
238
240 CachedPublicKeyData::Manip keyRingManip( const Pathname & keyring )
241 { return cachedPublicKeyData.manip( keyring ); }
242
243 private:
244 // Used for trusted and untrusted keyrings
245 filesystem::TmpDir _trusted_tmp_dir;
246 filesystem::TmpDir _general_tmp_dir;
247 Pathname _base_dir;
248 bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
249
255 CachedPublicKeyData cachedPublicKeyData;
256
257 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyAdded;
258 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyRemoved;
259 };
260
261 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::Ring ring )
262 { return str << ( ring == KeyRingImpl::Ring::General ? "generalKeyRing" : "trustedKeyRing" ); }
263
264 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::MustUpdate flag )
265 { return str << ( flag == KeyRingImpl::MustUpdate::Present ? "=" : flag == KeyRingImpl::MustUpdate::Missing ? "+" : ">" ); }
266}
267
268
269#endif
KeyManager.h
bool
scoped_ptr
zypp::KeyManagerCtx
Definition KeyManager.h:32
zypp::KeyRingImpl::SendKeys::operator()
void operator()(const std::string &id)
Transfer key with id from source to target keyring.
Definition keyring_p.cc:121
zypp::KeyRingImpl::SendKeys::_source
KeyManagerCtx _source
Definition keyring_p.h:201
zypp::KeyRingImpl::SendKeys::operator=
SendKeys & operator=(const SendKeys &)=delete
zypp::KeyRingImpl::SendKeys::SendKeys
SendKeys(SendKeys &&)=delete
zypp::KeyRingImpl::SendKeys::operator=
SendKeys & operator=(SendKeys &&)=delete
zypp::KeyRingImpl::SendKeys::SendKeys
SendKeys(KeyRingImpl &keyRing, const Pathname &source, const Pathname &target)
Ctor creates the context for reading source and writing target.
Definition keyring_p.cc:112
zypp::KeyRingImpl::SendKeys::SendKeys
SendKeys(const SendKeys &)=delete
zypp::KeyRingImpl::SendKeys::_target
CachedPublicKeyData::Manip _target
Definition keyring_p.h:202
zypp::KeyRingImpl::_sigTrustedKeyAdded
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
Definition keyring_p.h:257
zypp::KeyRingImpl::publicKeys
std::list< PublicKey > publicKeys(const Ring ring) const
Definition keyring_p.h:177
zypp::KeyRingImpl::MustUpdate
MustUpdate
Definition keyring_p.h:143
zypp::KeyRingImpl::MustUpdate::Update
@ Update
old version of Key is in Ring
Definition keyring_p.h:146
zypp::KeyRingImpl::MustUpdate::Missing
@ Missing
Key is not in Ring.
Definition keyring_p.h:145
zypp::KeyRingImpl::MustUpdate::Present
@ Present
Key is in Ring.
Definition keyring_p.h:144
zypp::KeyRingImpl::importKey
void importKey(const PublicKey &key, const Ring ring)
Import PublicKeys into a Ring.
Definition keyring_p.cc:140
zypp::KeyRingImpl::publicKeyData
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring) const
Definition keyring_p.h:218
zypp::KeyRingImpl::sigTrustedKeyRemoved
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyRemoved()
Definition keyring_p.h:101
zypp::KeyRingImpl::dumpPublicKeyToTmp
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring) const
Definition keyring_p.cc:320
zypp::KeyRingImpl::publicKeyData
PublicKeyData publicKeyData(const PublicKeyData &keyData, const Ring ring) const
Definition keyring_p.h:169
zypp::KeyRingImpl::mustUpdateData
MustUpdate mustUpdateData(const PublicKeyData &keyData, const Ring ring) const
Helper computing PublicKeyData's status in a Ring.
Definition keyring_p.h:149
zypp::KeyRingImpl::isKeyKnown
bool isKeyKnown(const std::string &id) const
Definition keyring_p.h:164
zypp::KeyRingImpl::exportKey
PublicKey exportKey(const PublicKey &key, const Ring ring) const
Definition keyring_p.h:122
zypp::KeyRingImpl::allowPreload
void allowPreload(bool yesno_r)
Definition keyring_p.h:108
zypp::KeyRingImpl::_general_tmp_dir
filesystem::TmpDir _general_tmp_dir
Definition keyring_p.h:246
zypp::KeyRingImpl::verifyFile
bool verifyFile(const Pathname &file, const Pathname &signature, const Ring ring)
Definition keyring_p.h:130
zypp::KeyRingImpl::KeyRingImpl
KeyRingImpl(const Pathname &baseTmpDir)
Definition keyring_p.cc:132
zypp::KeyRingImpl::_allowPreload
bool _allowPreload
Definition keyring_p.h:248
zypp::KeyRingImpl::publicKeyData
PublicKeyData publicKeyData(const std::string &id, const Ring ring) const
Definition keyring_p.h:167
zypp::KeyRingImpl::preloadCachedKeys
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
Definition keyring_p.cc:351
zypp::KeyRingImpl::multiKeyImport
void multiKeyImport(const Pathname &keyfile_r, const Ring ring)
Used by RpmDB to import the trusted keys.
Definition keyring_p.cc:224
zypp::KeyRingImpl::_sigTrustedKeyRemoved
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyRemoved
Definition keyring_p.h:258
zypp::KeyRingImpl::keyRingPath
const Pathname keyRingPath(const Ring ring) const
Definition keyring_p.h:206
zypp::KeyRingImpl::exportKey
PublicKey exportKey(const PublicKeyData &keyData, const Ring ring) const
Definition keyring_p.h:120
zypp::KeyRingImpl::deleteKey
void deleteKey(const std::string &id, const Ring ring)
Definition keyring_p.cc:230
zypp::KeyRingImpl::exportKey
PublicKey exportKey(const std::string &id, const Ring ring) const
Definition keyring_p.h:118
zypp::KeyRingImpl::Ring
Ring
Definition keyring_p.h:93
zypp::KeyRingImpl::_base_dir
Pathname _base_dir
Definition keyring_p.h:247
zypp::KeyRingImpl::publicKeyData
const std::list< PublicKeyData > & publicKeyData(const Ring ring) const
Definition keyring_p.h:174
zypp::KeyRingImpl::readSignatureKeyId
std::string readSignatureKeyId(const Pathname &signature)
Definition keyring_p.cc:331
zypp::KeyRingImpl::importKeys
void importKeys(const std::list< PublicKey > &keys, const Ring ring)
Definition keyring_p.cc:217
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:240
zypp::KeyRingImpl::isKeyTrusted
bool isKeyTrusted(const std::string &id) const
Definition keyring_p.h:161
zypp::KeyRingImpl::dumpPublicKey
void dumpPublicKey(const std::string &id, const Ring ring, std::ostream &stream)
Definition keyring_p.h:127
zypp::KeyRingImpl::publicKeyData
PublicKeyData publicKeyData(const PublicKey &key, const Ring ring) const
Definition keyring_p.h:171
zypp::KeyRingImpl::sigTrustedKeyAdded
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyAdded()
Definition keyring_p.h:98
zypp::KeyRingImpl::_trusted_tmp_dir
filesystem::TmpDir _trusted_tmp_dir
Definition keyring_p.h:245
zypp::KeyRingImpl::cachedPublicKeyData
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition keyring_p.h:255
zypp::PublicKeyData
Class representing one GPG Public Keys data.
Definition PublicKey.h:201
zypp::PublicKeyData::isUpdateFor
bool isUpdateFor(const PublicKeyData &rhs) const
Whether this could replace rhs in a keyring.
Definition PublicKey.h:287
zypp::PublicKeyData::id
std::string id() const
Key ID.
Definition PublicKey.cc:412
zypp::PublicKey
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition PublicKey.h:378
zypp::PublicKey::keyData
const PublicKeyData & keyData() const
The public keys data (.
Definition PublicKey.cc:640
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
zypp::filesystem::TmpPath::path
Pathname path() const
Definition TmpPath.cc:124
zyppng::SignalProxy
Definition signals.h:325
zyppng::Signal
Definition signals.h:151
expected.h
str
String related utilities and Regular expression matching.
zypp::base::NonCopyable
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
zypp::keyring
Definition KeyRingContexts.cc:18
zypp::target
Definition KeyRing.h:325
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::Ring
KeyRingImpl::Ring Ring
Definition KeyRing.cc:172
zypp::ResolverFocus::Update
@ Update
Focus on updating requested packages and their dependencies as much as possible.
Definition ResolverFocus.h:28
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
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:64
zypp::CachedPublicKeyData::Cache::assertCache
void assertCache(const Pathname &keyring_r)
Definition keyring_p.cc:70
zypp::CachedPublicKeyData::Cache::hasChanged
bool hasChanged() const
Definition keyring_p.cc:79
zypp::CachedPublicKeyData::Cache::_data
std::list< PublicKeyData > _data
Definition keyring_p.h:70
zypp::CachedPublicKeyData::Cache::Cache
Cache()
Definition keyring_p.cc:62
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:51
zypp::CachedPublicKeyData::Manip::Manip
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
Definition keyring_p.cc:46
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:89
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:86
zypp::CachedPublicKeyData::getData
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Definition keyring_p.cc:94
zypp::CachedPublicKeyData::manip
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition keyring_p.cc:92
zypp::CachedPublicKeyData::CacheMap
std::map< Pathname, Cache > CacheMap
Definition keyring_p.h:78
TmpPath.h