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

libzypp 17.38.11

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 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:
182 const Pathname keyRingPath( const Ring ring ) const
183 { return ring == Ring::General ? _general_tmp_dir.path() : _trusted_tmp_dir.path(); }
184
185 void importKey( const Pathname & keyfile, const Pathname & keyring );
186
187 PublicKey exportKey( const std::string & id, const Pathname & keyring ) const;
188 PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring ) const;
189
190 void deleteKey( const std::string & id, const Pathname & keyring );
191
192 PublicKeyData publicKeyData( const std::string & id, const Pathname & keyring ) const;
193
194 const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring ) const
195 {
196 if ( _allowPreload && keyring == keyRingPath( Ring::General ) ) {
197 KeyRingImpl * lazyinit = const_cast<KeyRingImpl*>(this);
198 lazyinit->_allowPreload = false;
199 lazyinit->preloadCachedKeys();
200 }
201 return cachedPublicKeyData( keyring );
202 }
203
204 std::list<PublicKey> publicKeys( const Pathname & keyring ) const;
205
206 void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream ) const;
207 filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring ) const;
208
209 bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
210
211 private:
213 void preloadCachedKeys();
214
216 CachedPublicKeyData::Manip keyRingManip( const Pathname & keyring )
217 { return cachedPublicKeyData.manip( keyring ); }
218
219 private:
220 // Used for trusted and untrusted keyrings
221 filesystem::TmpDir _trusted_tmp_dir;
222 filesystem::TmpDir _general_tmp_dir;
223 Pathname _base_dir;
224 bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
225
231 CachedPublicKeyData cachedPublicKeyData;
232
233 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyAdded;
234 zyppng::Signal<void ( const PublicKey &/*key*/ )> _sigTrustedKeyRemoved;
235 };
236
237 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::Ring ring )
238 { return str << ( ring == KeyRingImpl::Ring::General ? "generalKeyRing" : "trustedKeyRing" ); }
239
240 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::MustUpdate flag )
241 { return str << ( flag == KeyRingImpl::MustUpdate::Present ? "=" : flag == KeyRingImpl::MustUpdate::Missing ? "+" : ">" ); }
242}
243
244
245#endif
KeyManager.h
bool
scoped_ptr
zypp::KeyManagerCtx
Definition KeyManager.h:31
zypp::KeyRingImpl::_sigTrustedKeyAdded
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
Definition keyring_p.h:233
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:120
zypp::KeyRingImpl::publicKeyData
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring) const
Definition keyring_p.h:194
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:299
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:222
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:112
zypp::KeyRingImpl::_allowPreload
bool _allowPreload
Definition keyring_p.h:224
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:330
zypp::KeyRingImpl::multiKeyImport
void multiKeyImport(const Pathname &keyfile_r, const Ring ring)
Used by RpmDB to import the trusted keys.
Definition keyring_p.cc:203
zypp::KeyRingImpl::_sigTrustedKeyRemoved
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyRemoved
Definition keyring_p.h:234
zypp::KeyRingImpl::keyRingPath
const Pathname keyRingPath(const Ring ring) const
Definition keyring_p.h:182
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:209
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::Ring::General
@ General
Definition keyring_p.h:94
zypp::KeyRingImpl::Ring::Trusted
@ Trusted
Definition keyring_p.h:95
zypp::KeyRingImpl::_base_dir
Pathname _base_dir
Definition keyring_p.h:223
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:310
zypp::KeyRingImpl::importKeys
void importKeys(const std::list< PublicKey > &keys, const Ring ring)
Definition keyring_p.cc:196
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:216
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:221
zypp::KeyRingImpl::cachedPublicKeyData
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition keyring_p.h:231
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:284
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:375
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
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
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::Ring
KeyRingImpl::Ring Ring
Definition KeyRing.cc:172
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