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
17#include <zypp-core/fs/WatchFile>
20#include <functional>
21#include <optional>
22
23namespace zypp {
24
25
34 {
35 const std::list<PublicKeyData> & operator()( const Pathname & keyring_r ) const;
36
37 void setDirty( const Pathname & keyring_r );
38
45 struct Manip {
47 Manip( CachedPublicKeyData & cache_r, Pathname keyring_r );
48
50 private:
53 std::optional<KeyManagerCtx> _context;
54 };
55
56
58 Manip manip( Pathname keyring_r );
59
60 private:
61 struct Cache
62 {
63 Cache();
64
65 void setDirty();
66
67 void assertCache( const Pathname & keyring_r );
68
69 bool hasChanged() const;
70
71 std::list<PublicKeyData> _data;
72
73 private:
74
77 };
78
79 using CacheMap = std::map<Pathname, Cache>;
80
81 const std::list<PublicKeyData> & getData( const Pathname & keyring_r ) const;
82
83 const std::list<PublicKeyData> & getData( const Pathname & keyring_r, Cache & cache_r ) const;
84
86 };
87
92 {
93 public:
94 enum class Ring : bool {
95 General = false,
96 Trusted = true,
97 };
98
101
104
105 public:
106 KeyRingImpl( const Pathname & baseTmpDir );
107
108 public:
109 void allowPreload( bool yesno_r ) //< General keyring may be preloaded with keys cached on the system.
110 { _allowPreload = yesno_r; }
111
112 void setTrustedKeyRingInit( std::function<void()> init_r ) //< Executed on first use of the trusted keyring.
113 { _trustedRingInit = std::move(init_r); }
114
119 void importKey ( const PublicKey & key, const Ring ring );
120 void importKeys( const std::list<PublicKey> & keys, const Ring ring );
121
122 PublicKey exportKey( const std::string & id, const Ring ring ) const
123 { return exportKey( id, keyRingPath( ring ) ); }
124 PublicKey exportKey( const PublicKeyData & keyData, const Ring ring ) const
125 { return exportKey( keyData, keyRingPath( ring ) ); }
126 PublicKey exportKey( const PublicKey & key, const Ring ring ) const
127 { return exportKey( key.keyData(), ring ); }
128
129 void deleteKey( const std::string & id, const Ring ring );
130
131 void dumpPublicKey( const std::string & id, const Ring ring, std::ostream & stream )
132 { dumpPublicKey( id, keyRingPath( ring ), stream ); }
133
134 bool verifyFile( const Pathname & file, const Pathname & signature, const Ring ring )
135 { return verifyFile( file, signature, keyRingPath( ring ) ); }
136
137 std::string readSignatureKeyId( const Pathname & signature );
138
144 void multiKeyImport( const Pathname & keyfile_r, const Ring ring );
145
146 public:
147 enum class MustUpdate {
150 Update = 2,
151 };
152
153 MustUpdate mustUpdateData( const PublicKeyData & keyData, const Ring ring ) const
154 {
155 PublicKeyData ringKeyData { publicKeyData( keyData, ring ) };
156 if ( not ringKeyData ) {
157 return MustUpdate::Missing;
158 }
159 if ( keyData.isUpdateFor( ringKeyData ) ) {
160 return MustUpdate::Update;
161 }
162 return MustUpdate::Present;
163 }
164
165 bool isKeyTrusted( const std::string & id ) const
166 { return bool(publicKeyData( id, Ring::Trusted )); }
167
168 bool isKeyKnown( const std::string & id ) const
169 { return publicKeyData( id, Ring::Trusted ) || publicKeyData( id, Ring::General ); }
170
171 PublicKeyData publicKeyData( const std::string & id, const Ring ring ) const
172 { return publicKeyData( id, keyRingPath( ring ) ); }
173 PublicKeyData publicKeyData( const PublicKeyData & keyData, const Ring ring ) const
174 { return publicKeyData( keyData.id(), ring ); }
175 PublicKeyData publicKeyData( const PublicKey & key, const Ring ring ) const
176 { return publicKeyData( key.keyData().id(), ring ); }
177
178 const std::list<PublicKeyData> & publicKeyData( const Ring ring ) const
179 { return publicKeyData( keyRingPath( ring ) ); }
180
181 std::list<PublicKey> publicKeys( const Ring ring ) const
182 { return publicKeys( keyRingPath( ring ) ); }
183
184
185 private:
188 {
189 public:
191 SendKeys( KeyRingImpl & keyRing, const Pathname & source, const Pathname & target );
193 SendKeys( KeyRingImpl & keyRing, const Ring source, const Ring target );
194 SendKeys( const SendKeys & ) = delete;
195 SendKeys( SendKeys && ) = delete;
196 SendKeys & operator=( const SendKeys & ) = delete;
197 SendKeys & operator=( SendKeys && ) = delete;
198
202 void operator()( const std::string & id );
203
204 private:
207 };
208
209 private:
210 const Pathname keyRingPath( const Ring ring ) const
211 {
212 if ( ring == Ring::General )
213 return _general_tmp_dir.path();
214 if ( _trustedRingInit ) {
215 // Reset before executing: the initializer populates the ring and
216 // will ask for this path again.
217 KeyRingImpl * lazyinit = const_cast<KeyRingImpl*>(this);
218 std::function<void()> init;
219 init.swap( lazyinit->_trustedRingInit );
220 init();
221 }
222 return _trusted_tmp_dir.path();
223 }
224
225 void importKey( const Pathname & keyfile, const Pathname & keyring );
226
227 PublicKey exportKey( const std::string & id, const Pathname & keyring ) const;
228 PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring ) const;
229
230 void deleteKey( const std::string & id, const Pathname & keyring );
231
232 PublicKeyData publicKeyData( const std::string & id, const Pathname & keyring ) const;
233
234 const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring ) const
235 {
236 if ( _allowPreload && keyring == keyRingPath( Ring::General ) ) {
237 KeyRingImpl * lazyinit = const_cast<KeyRingImpl*>(this);
238 lazyinit->_allowPreload = false;
239 lazyinit->preloadCachedKeys();
240 }
242 }
243
244 std::list<PublicKey> publicKeys( const Pathname & keyring ) const;
245
246 void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream ) const;
247 filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring ) const;
248
249 bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
250
251 private:
253 void preloadCachedKeys();
254
258
259 private:
260 // Used for trusted and untrusted keyrings
264 bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
265 std::function<void()> _trustedRingInit; //< Executed on first use of the trusted keyring.
266
273
276 };
277
278 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::Ring ring )
279 { return str << ( ring == KeyRingImpl::Ring::General ? "generalKeyRing" : "trustedKeyRing" ); }
280
281 inline std::ostream & operator<<( std::ostream & str, KeyRingImpl::MustUpdate flag )
282 { return str << ( flag == KeyRingImpl::MustUpdate::Present ? "=" : flag == KeyRingImpl::MustUpdate::Missing ? "+" : ">" ); }
283}
284
285
286#endif
void operator()(const std::string &id)
Transfer key with id from source to target keyring.
Definition keyring_p.cc:121
SendKeys & operator=(const SendKeys &)=delete
SendKeys(SendKeys &&)=delete
SendKeys & operator=(SendKeys &&)=delete
SendKeys(KeyRingImpl &keyRing, const Pathname &source, const Pathname &target)
Ctor creates the context for reading source and writing target.
Definition keyring_p.cc:112
SendKeys(const SendKeys &)=delete
CachedPublicKeyData::Manip _target
Definition keyring_p.h:206
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyAdded
Definition keyring_p.h:274
std::list< PublicKey > publicKeys(const Ring ring) const
Definition keyring_p.h:181
@ Update
old version of Key is in Ring
Definition keyring_p.h:150
@ Missing
Key is not in Ring.
Definition keyring_p.h:149
void importKey(const PublicKey &key, const Ring ring)
Import PublicKeys into a Ring.
Definition keyring_p.cc:140
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring) const
Definition keyring_p.h:234
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyRemoved()
Definition keyring_p.h:102
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring) const
Definition keyring_p.cc:320
PublicKeyData publicKeyData(const PublicKeyData &keyData, const Ring ring) const
Definition keyring_p.h:173
MustUpdate mustUpdateData(const PublicKeyData &keyData, const Ring ring) const
Helper computing PublicKeyData's status in a Ring.
Definition keyring_p.h:153
bool isKeyKnown(const std::string &id) const
Definition keyring_p.h:168
PublicKey exportKey(const PublicKey &key, const Ring ring) const
Definition keyring_p.h:126
void allowPreload(bool yesno_r)
Definition keyring_p.h:109
filesystem::TmpDir _general_tmp_dir
Definition keyring_p.h:262
bool verifyFile(const Pathname &file, const Pathname &signature, const Ring ring)
Definition keyring_p.h:134
void setTrustedKeyRingInit(std::function< void()> init_r)
Definition keyring_p.h:112
KeyRingImpl(const Pathname &baseTmpDir)
Definition keyring_p.cc:132
PublicKeyData publicKeyData(const std::string &id, const Ring ring) const
Definition keyring_p.h:171
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
Definition keyring_p.cc:351
void multiKeyImport(const Pathname &keyfile_r, const Ring ring)
Used by RpmDB to import the trusted keys.
Definition keyring_p.cc:224
zyppng::Signal< void(const PublicKey &)> _sigTrustedKeyRemoved
Definition keyring_p.h:275
const Pathname keyRingPath(const Ring ring) const
Definition keyring_p.h:210
PublicKey exportKey(const PublicKeyData &keyData, const Ring ring) const
Definition keyring_p.h:124
void deleteKey(const std::string &id, const Ring ring)
Definition keyring_p.cc:230
PublicKey exportKey(const std::string &id, const Ring ring) const
Definition keyring_p.h:122
Pathname _base_dir
Definition keyring_p.h:263
const std::list< PublicKeyData > & publicKeyData(const Ring ring) const
Definition keyring_p.h:178
std::string readSignatureKeyId(const Pathname &signature)
Definition keyring_p.cc:331
void importKeys(const std::list< PublicKey > &keys, const Ring ring)
Definition keyring_p.cc:217
CachedPublicKeyData::Manip keyRingManip(const Pathname &keyring)
Impl helper providing on demand a KeyManagerCtx to manip a cached keyring.
Definition keyring_p.h:256
bool isKeyTrusted(const std::string &id) const
Definition keyring_p.h:165
void dumpPublicKey(const std::string &id, const Ring ring, std::ostream &stream)
Definition keyring_p.h:131
PublicKeyData publicKeyData(const PublicKey &key, const Ring ring) const
Definition keyring_p.h:175
std::function< void()> _trustedRingInit
Definition keyring_p.h:265
zyppng::SignalProxy< void(const PublicKey &)> sigTrustedKeyAdded()
Definition keyring_p.h:99
filesystem::TmpDir _trusted_tmp_dir
Definition keyring_p.h:261
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition keyring_p.h:272
Class representing one GPG Public Keys data.
Definition PublicKey.h:201
bool isUpdateFor(const PublicKeyData &rhs) const
Whether this could replace rhs in a keyring.
Definition PublicKey.h:287
std::string id() const
Key ID.
Definition PublicKey.cc:412
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition PublicKey.h:378
const PublicKeyData & keyData() const
The public keys data (.
Definition PublicKey.cc:640
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:173
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:118
void
Definition curl_dl.cc:94
String related utilities and Regular expression matching.
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
Easy-to use interface to the ZYPP dependency resolver.
KeyRingImpl::Ring Ring
Definition KeyRing.cc:172
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
scoped_ptr< WatchFile > _keyringP
Definition keyring_p.h:76
void assertCache(const Pathname &keyring_r)
Definition keyring_p.cc:70
std::list< PublicKeyData > _data
Definition keyring_p.h:71
scoped_ptr< WatchFile > _keyringK
Definition keyring_p.h:75
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition keyring_p.h:45
std::optional< KeyManagerCtx > _context
Definition keyring_p.h:53
KeyManagerCtx & keyManagerCtx()
Definition keyring_p.cc:51
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
Definition keyring_p.cc:46
CachedPublicKeyData & _cache
Definition keyring_p.h:51
Functor returning the keyrings data (cached).
Definition keyring_p.h:34
void setDirty(const Pathname &keyring_r)
Definition keyring_p.cc:89
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
Definition keyring_p.cc:86
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Definition keyring_p.cc:94
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition keyring_p.cc:92
std::map< Pathname, Cache > CacheMap
Definition keyring_p.h:79