libzypp  17.35.16
keyring_p.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_ZYPP_DETAIL_KEYRINGIMPL_H
13 #define ZYPP_ZYPP_DETAIL_KEYRINGIMPL_H
14 
15 #include "zypp-core/fs/TmpPath.h"
16 #include <zypp-core/fs/WatchFile>
17 #include <zypp/KeyManager.h>
18 #include <zypp/KeyRing.h>
19 
20 #include <optional>
21 
22 namespace zypp {
23 
24 
33  {
34  const std::list<PublicKeyData> & operator()( const Pathname & keyring_r ) const;
35 
36  void setDirty( const Pathname & keyring_r );
37 
44  struct Manip {
46  Manip( CachedPublicKeyData & cache_r, Pathname keyring_r );
47 
49  private:
52  std::optional<KeyManagerCtx> _context;
53  };
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 
85  };
86 
87 
88 
90  //
91  // CLASS NAME : KeyRing::Impl
92  //
95  {
96  Impl( const Pathname & baseTmpDir );
97 
98  void importKey( const PublicKey & key, bool trusted = false );
99  void multiKeyImport( const Pathname & keyfile_r, bool trusted_r = false );
100  void deleteKey( const std::string & id, bool trusted );
101 
102  std::string readSignatureKeyId( const Pathname & signature );
103 
104  bool isKeyTrusted( const std::string & id )
105  { return bool(publicKeyExists( id, trustedKeyRing() )); }
106  bool isKeyKnown( const std::string & id )
107  { return publicKeyExists( id, trustedKeyRing() ) || publicKeyExists( id, generalKeyRing() ); }
108 
109  std::list<PublicKey> trustedPublicKeys()
110  { return publicKeys( trustedKeyRing() ); }
111  std::list<PublicKey> publicKeys()
112  { return publicKeys( generalKeyRing() ); }
113 
114  const std::list<PublicKeyData> & trustedPublicKeyData()
115  { return publicKeyData( trustedKeyRing() ); }
116  const std::list<PublicKeyData> & publicKeyData()
117  { return publicKeyData( generalKeyRing() ); }
118 
119  void dumpPublicKey( const std::string & id, bool trusted, std::ostream & stream )
120  { dumpPublicKey( id, ( trusted ? trustedKeyRing() : generalKeyRing() ), stream ); }
121 
123  { return exportKey( keyData, generalKeyRing() ); }
125  { return exportKey( keyData, trustedKeyRing() ); }
126 
127  bool verifyFileSignature( const Pathname & file, const Pathname & signature )
128  { return verifyFile( file, signature, generalKeyRing() ); }
129  bool verifyFileTrustedSignature( const Pathname & file, const Pathname & signature )
130  { return verifyFile( file, signature, trustedKeyRing() ); }
131 
132  PublicKeyData publicKeyExists( const std::string & id )
133  { return publicKeyExists(id, generalKeyRing());}
134  PublicKeyData trustedPublicKeyExists( const std::string & id )
135  { return publicKeyExists(id, trustedKeyRing());}
136 
137  void allowPreload( bool yesno_r )
138  { _allowPreload = yesno_r; }
139 
142  { return cachedPublicKeyData.manip( keyring ); }
143 
144  bool verifyFile( const Pathname & file, const Pathname & signature, const Pathname & keyring );
145  void importKey( const Pathname & keyfile, const Pathname & keyring );
146 
147  PublicKey exportKey( const std::string & id, const Pathname & keyring );
148  PublicKey exportKey( const PublicKeyData & keyData, const Pathname & keyring );
149  PublicKey exportKey( const PublicKey & key, const Pathname & keyring )
150  { return exportKey( key.keyData(), keyring ); }
151 
152  void dumpPublicKey( const std::string & id, const Pathname & keyring, std::ostream & stream );
153  filesystem::TmpFile dumpPublicKeyToTmp( const std::string & id, const Pathname & keyring );
154 
155  void deleteKey( const std::string & id, const Pathname & keyring );
156 
157  std::list<PublicKey> publicKeys( const Pathname & keyring);
158  const std::list<PublicKeyData> & publicKeyData( const Pathname & keyring )
159  { return cachedPublicKeyData( keyring ); }
160 
162  PublicKeyData publicKeyExists( const std::string & id, const Pathname & keyring );
164  void preloadCachedKeys();
165 
166  const Pathname generalKeyRing() const
167  { return _general_tmp_dir.path(); }
168  const Pathname trustedKeyRing() const
169  { return _trusted_tmp_dir.path(); }
170 
171  private:
172  // Used for trusted and untrusted keyrings
176  bool _allowPreload = false; //< General keyring may be preloaded with keys cached on the system.
177 
184  };
185 
186 }
187 
188 
189 #endif
void allowPreload(bool yesno_r)
Definition: keyring_p.h:137
const std::list< PublicKeyData > & publicKeyData()
Definition: keyring_p.h:116
PublicKey exportPublicKey(const PublicKeyData &keyData)
Definition: keyring_p.h:122
void dumpPublicKey(const std::string &id, bool trusted, std::ostream &stream)
Definition: keyring_p.h:119
void deleteKey(const std::string &id, bool trusted)
Definition: KeyRing.cc:247
PublicKey exportKey(const std::string &id, const Pathname &keyring)
Definition: KeyRing.cc:355
bool isKeyTrusted(const std::string &id)
Definition: keyring_p.h:104
const std::list< PublicKeyData > & trustedPublicKeyData()
Definition: keyring_p.h:114
const PublicKeyData & keyData() const
The public keys data (.
Definition: PublicKey.cc:643
Functor returning the keyrings data (cached).
Definition: keyring_p.h:32
PublicKey exportKey(const PublicKey &key, const Pathname &keyring)
Definition: keyring_p.h:149
Manip(CachedPublicKeyData &cache_r, Pathname keyring_r)
Definition: KeyRing.cc:119
Class representing one GPG Public Keys data.
Definition: PublicKey.h:207
bool isKeyKnown(const std::string &id)
Definition: keyring_p.h:106
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
Definition: KeyRing.cc:242
bool verifyFile(const Pathname &file, const Pathname &signature, const Pathname &keyring)
Definition: KeyRing.cc:433
Pathname path() const
Definition: TmpPath.cc:152
Manip manip(Pathname keyring_r)
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition: KeyRing.cc:165
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:127
CachedPublicKeyData cachedPublicKeyData
Functor returning the keyrings data (cached).
Definition: keyring_p.h:183
PublicKey exportTrustedPublicKey(const PublicKeyData &keyData)
Definition: keyring_p.h:124
bool trusted
Definition: keyringwf.cc:135
std::map< Pathname, Cache > CacheMap
Definition: keyring_p.h:78
PublicKeyData trustedPublicKeyExists(const std::string &id)
Definition: keyring_p.h:134
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition: TmpPath.h:181
filesystem::TmpDir _trusted_tmp_dir
Definition: keyring_p.h:173
bool verifyFileSignature(const Pathname &file, const Pathname &signature)
Definition: keyring_p.h:127
CachedPublicKeyData & _cache
Definition: keyring_p.h:50
const Pathname generalKeyRing() const
Definition: keyring_p.h:166
filesystem::TmpFile dumpPublicKeyToTmp(const std::string &id, const Pathname &keyring)
Definition: KeyRing.cc:372
scoped_ptr< WatchFile > _keyringK
Definition: keyring_p.h:74
KeyRing implementation.
Definition: keyring_p.h:94
std::list< PublicKey > trustedPublicKeys()
Definition: keyring_p.h:109
const std::list< PublicKeyData > & getData(const Pathname &keyring_r) const
Definition: KeyRing.cc:167
void importKey(const PublicKey &key, bool trusted=false)
Definition: KeyRing.cc:220
KeyManagerCtx & keyManagerCtx()
Definition: KeyRing.cc:124
Impl(const Pathname &baseTmpDir)
Definition: KeyRing.cc:212
Pathname _base_dir
Definition: keyring_p.h:175
void assertCache(const Pathname &keyring_r)
Definition: KeyRing.cc:143
void preloadCachedKeys()
Load key files cached on the system into the generalKeyRing.
Definition: KeyRing.cc:296
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:364
const Pathname trustedKeyRing() const
Definition: keyring_p.h:168
std::list< PublicKey > publicKeys()
Definition: keyring_p.h:111
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature)
Definition: keyring_p.h:129
const std::list< PublicKeyData > & operator()(const Pathname &keyring_r) const
Definition: KeyRing.cc:159
const std::list< PublicKeyData > & publicKeyData(const Pathname &keyring)
Definition: keyring_p.h:158
std::list< PublicKeyData > _data
Definition: keyring_p.h:70
void setDirty(const Pathname &keyring_r)
Definition: KeyRing.cc:162
std::optional< KeyManagerCtx > _context
Definition: keyring_p.h:52
scoped_ptr< WatchFile > _keyringP
Definition: keyring_p.h:75
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
CachedPublicKeyData::Manip keyRingManip(const Pathname &keyring)
Impl helper providing on demand a KeyManagerCtx to manip a cached keyring.
Definition: keyring_p.h:141
PublicKeyData publicKeyExists(const std::string &id)
Definition: keyring_p.h:132
std::string readSignatureKeyId(const Pathname &signature)
Definition: KeyRing.cc:417
Helper providing on demand a KeyManagerCtx to manip the cached keyring.
Definition: keyring_p.h:44
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
filesystem::TmpDir _general_tmp_dir
Definition: keyring_p.h:174