libzypp  17.37.5
KeyRing.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
13 #include "zypp_detail/keyring_p.h"
14 
15 #include <iostream>
16 #include <sys/file.h>
17 #include <unistd.h>
18 
19 #include <zypp/TmpPath.h>
20 #include <zypp/ZYppFactory.h>
21 #include <zypp/ZYpp.h>
22 
23 #include <zypp/base/LogTools.h>
24 #include <zypp/base/IOStream.h>
25 #include <zypp/base/String.h>
26 #include <zypp/base/Regex.h>
27 #include <zypp/base/Gettext.h>
28 #include <zypp-core/fs/WatchFile>
29 #include <zypp/PathInfo.h>
30 #include <zypp/ExternalProgram.h>
31 #include <zypp/TmpPath.h>
32 #include <zypp/ZYppCallbacks.h> // JobReport::instance
33 #include <zypp-common/KeyManager.h>
34 
36 
37 using std::endl;
38 
39 #undef ZYPP_BASE_LOGGER_LOGGROUP
40 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::KeyRing"
41 
43 namespace zypp
44 {
45 
47 
48  namespace
49  {
50  KeyRing::DefaultAccept _keyRingDefaultAccept( KeyRing::ACCEPT_NOTHING );
51  }
52 
53  KeyRing::DefaultAccept KeyRing::defaultAccept()
54  { return _keyRingDefaultAccept; }
55 
56  void KeyRing::setDefaultAccept( DefaultAccept value_r )
57  {
58  MIL << "Set new KeyRing::DefaultAccept: " << value_r << endl;
59  _keyRingDefaultAccept = value_r;
60  }
61 
62  void KeyRingReport::infoVerify( const std::string & file_r, const PublicKeyData & keyData_r, const KeyContext & keycontext )
63  {}
64 
65  bool KeyRingReport::askUserToAcceptUnsignedFile( const std::string & file, const KeyContext & keycontext )
66  { return _keyRingDefaultAccept.testFlag( KeyRing::ACCEPT_UNSIGNED_FILE ); }
67 
69  KeyRingReport::askUserToAcceptKey( const PublicKey & key, const KeyContext & keycontext )
70  {
71  if ( _keyRingDefaultAccept.testFlag( KeyRing::TRUST_KEY_TEMPORARILY ) )
72  return KEY_TRUST_TEMPORARILY;
73  if ( _keyRingDefaultAccept.testFlag( KeyRing::TRUST_AND_IMPORT_KEY ) )
74  return KEY_TRUST_AND_IMPORT;
75  return KEY_DONT_TRUST;
76  }
77 
78  bool KeyRingReport::askUserToAcceptUnknownKey( const std::string & file, const std::string & id, const KeyContext & keycontext )
79  { return _keyRingDefaultAccept.testFlag( KeyRing::ACCEPT_UNKNOWNKEY ); }
80 
81  bool KeyRingReport::askUserToAcceptVerificationFailed( const std::string & file, const PublicKey & key, const KeyContext & keycontext )
82  { return _keyRingDefaultAccept.testFlag( KeyRing::ACCEPT_VERIFICATION_FAILED ); }
83 
84  bool KeyRingReport::askUserToAcceptPackageKey(const PublicKey &key_r, const KeyContext &keycontext_r)
85  {
87  data.set("PublicKey", key_r);
88  data.set("KeyContext", keycontext_r);
89  report(data);
90 
91  if ( data.hasvalue("TrustKey") )
92  return data.get<bool>("TrustKey");
93  return false;
94  }
95 
96  void KeyRingReport::reportNonImportedKeys(const std::set<Edition> &keys_r)
97  {
99  data.set("Keys", keys_r);
100  report(data);
101  }
102 
103  void KeyRingReport::reportAutoImportKey( const std::list<PublicKeyData> & keyDataList_r,
104  const PublicKeyData & keySigning_r,
105  const KeyContext &keyContext_r )
106  {
108  data.set( "KeyDataList", keyDataList_r );
109  data.set( "KeySigning", keySigning_r );
110  data.set( "KeyContext", keyContext_r );
111  report( data );
112  }
113 
114  namespace
115  {
117  struct ImportKeyCBHelper
118  {
119  void operator()( const PublicKey & key_r )
120  {
121  try {
122  _rpmdbEmitSignal->trustedKeyAdded( key_r );
123  _emitSignal->trustedKeyAdded( key_r );
124  }
125  catch ( const Exception & excp )
126  {
127  ERR << "Could not import key into rpmdb: " << excp << endl;
128  // TODO: JobReport as hotfix for bsc#1057188; should bubble up and go through some callback
129  JobReport::error( excp.asUserHistory() );
130  }
131  }
132 
133  private:
134  callback::SendReport<target::rpm::KeyRingSignals> _rpmdbEmitSignal;
135  callback::SendReport<KeyRingSignals> _emitSignal;
136  };
137  } // namespace
138 
140  : KeyRingImpl( baseTmpDir )
141  {
142  MIL << "Current KeyRing::DefaultAccept: " << _keyRingDefaultAccept << std::endl;
143 
144  sigTrustedKeyAdded ().connect ([]( const PublicKey &key ){
145  ImportKeyCBHelper emit;
146  emit(key);
147  });
148 
149  sigTrustedKeyRemoved ().connect ([]( const PublicKey &key ){
150  try {
152  rpmdbEmitSignal->trustedKeyRemoved( key );
153 
155  emitSignal->trustedKeyRemoved( key );
156  }
157  catch ( const Exception & excp )
158  {
159  ERR << "Could not delete key from rpmmdb: " << excp << endl;
160  // TODO: JobReport as hotfix for bsc#1057188; should bubble up and go through some callback
162  }
163  });
164  }
165 
166 
168  //
169  // CLASS NAME : KeyRing
170  //
172 
173  KeyRing::KeyRing( const Pathname & baseTmpDir )
174  : _pimpl( new Impl( baseTmpDir ) )
175  {}
176 
178  {}
179 
181  {
182  return *_pimpl;
183  }
184 
185  void KeyRing::allowPreload( bool yesno_r )
186  { _pimpl->allowPreload( yesno_r ); }
187 
188 
189  void KeyRing::importKey( const PublicKey & key, bool trusted )
190  { _pimpl->importKey( key, trusted ); }
191 
192  void KeyRing::multiKeyImport( const Pathname & keyfile_r, bool trusted_r )
193  { _pimpl->multiKeyImport( keyfile_r, trusted_r ); }
194 
195  std::string KeyRing::readSignatureKeyId( const Pathname & signature )
196  { return _pimpl->readSignatureKeyId( signature ); }
197 
198  void KeyRing::deleteKey( const std::string & id, bool trusted )
199  { _pimpl->deleteKey( id, trusted ); }
200 
201  std::list<PublicKey> KeyRing::publicKeys()
202  { return _pimpl->publicKeys(); }
203 
204  std:: list<PublicKey> KeyRing::trustedPublicKeys()
205  { return _pimpl->trustedPublicKeys(); }
206 
207  std::list<PublicKeyData> KeyRing::publicKeyData()
208  { return _pimpl->publicKeyData(); }
209 
210  std::list<PublicKeyData> KeyRing::trustedPublicKeyData()
211  { return _pimpl->trustedPublicKeyData(); }
212 
213  PublicKeyData KeyRing::publicKeyData( const std::string &id_r )
214  { return _pimpl->publicKeyExists( id_r ); }
215 
216  PublicKeyData KeyRing::trustedPublicKeyData(const std::string &id_r)
217  { return _pimpl->trustedPublicKeyExists( id_r ); }
218 
219  bool KeyRing::verifyFileSignature( const Pathname & file, const Pathname & signature )
220  { return _pimpl->verifyFileSignature( file, signature ); }
221 
222  bool KeyRing::verifyFileTrustedSignature( const Pathname & file, const Pathname & signature )
223  { return _pimpl->verifyFileTrustedSignature( file, signature ); }
224  void KeyRing::dumpPublicKey( const std::string & id, bool trusted, std::ostream & stream )
225  { _pimpl->dumpPublicKey( id, trusted, stream ); }
226 
227  PublicKey KeyRing::exportPublicKey( const PublicKeyData & keyData )
228  { return _pimpl->exportPublicKey( keyData ); }
229 
230  PublicKey KeyRing::exportTrustedPublicKey( const PublicKeyData & keyData )
231  { return _pimpl->exportTrustedPublicKey( keyData ); }
232 
233  bool KeyRing::isKeyTrusted( const std::string & id )
234  { return _pimpl->isKeyTrusted( id ); }
235 
236  bool KeyRing::isKeyKnown( const std::string & id )
237  { return _pimpl->isKeyKnown( id ); }
238 
240 } // namespace zypp
void importKey(const PublicKey &key, bool trusted=false)
imports a key from a file.
Definition: KeyRing.cc:189
Interface to gettext.
#define MIL
Definition: Logger.h:100
PublicKey exportTrustedPublicKey(const PublicKeyData &keyData)
Export a trusted public key identified by its key data.
Definition: KeyRing.cc:230
zypp::KeyRing KeyRing
Definition: context.h:28
static bool error(const std::string &msg_r, const UserData &userData_r=UserData())
send error text
~KeyRing() override
Dtor.
Definition: KeyRing.cc:177
This basically means, we knew the key, but it was not trusted.
Definition: KeyRing.h:63
PublicKey exportPublicKey(const PublicKeyData &keyData)
Export a public key identified by its key data.
Definition: KeyRing.cc:227
void reportNonImportedKeys(const std::set< Edition > &keys_r)
Notify the user about keys that were not imported from the rpm key database into zypp keyring...
Definition: KeyRing.cc:96
void dumpPublicKey(const std::string &id, bool trusted, std::ostream &stream)
Definition: KeyRing.cc:224
std::list< PublicKey > trustedPublicKeys()
Get a list of trusted public keys in the keyring (incl.
Definition: KeyRing.cc:204
virtual bool askUserToAcceptUnsignedFile(const std::string &file, const KeyContext &keycontext=KeyContext())
Definition: KeyRing.cc:65
KeyRing(const Pathname &baseTmpDir)
Default ctor.
Definition: KeyRing.cc:173
KeyRing::Impl & pimpl()
Access to private functions for the KeyRingWorkflow implementations.
Definition: KeyRing.cc:180
std::list< PublicKeyData > trustedPublicKeyData()
Get a list of trusted public key data in the keyring (key data only)
Definition: KeyRing.cc:210
bool askUserToAcceptPackageKey(const PublicKey &key_r, const KeyContext &keycontext_r=KeyContext())
Ask user to trust and/or import the package key to trusted keyring, using ReportBase::report.
Definition: KeyRing.cc:84
virtual bool askUserToAcceptUnknownKey(const std::string &file, const std::string &id, const KeyContext &keycontext=KeyContext())
we DONT know the key, only its id, but we have never seen it, the difference with trust key is that i...
Definition: KeyRing.cc:78
#define ERR
Definition: Logger.h:102
callback::SendReport< target::rpm::KeyRingSignals > _rpmdbEmitSignal
Definition: KeyRing.cc:134
virtual void infoVerify(const std::string &file_r, const PublicKeyData &keyData_r, const KeyContext &keycontext=KeyContext())
Informal callback showing the trusted key that will be used for verification.
Definition: KeyRing.cc:62
virtual void report(const UserData &userData_r=UserData())
The most generic way of sending/receiving data.
Definition: Callback.h:155
KeyTrust
User reply options for the askUserToTrustKey callback.
Definition: KeyRing.h:53
bool trusted
Definition: keyringwf.cc:156
static void setDefaultAccept(DefaultAccept value_r)
Set the active accept bits.
Definition: KeyRing.cc:56
bool set(const std::string &key_r, AnyType val_r)
Set the value for key (nonconst version always returns true).
Definition: UserData.h:119
std::string asUserHistory() const
A single (multiline) string composed of asUserString and historyAsString.
Definition: Exception.cc:140
bool verifyFileSignature(const Pathname &file, const Pathname &signature) ZYPP_API
Verifies a file against a signature, with no user interaction.
Definition: KeyRing.cc:219
IMPL_PTR_TYPE(Application)
bool isKeyKnown(const std::string &id)
true if the key id is knows, that means at least exist on the untrusted keyring
Definition: KeyRing.cc:236
void multiKeyImport(const Pathname &keyfile_r, bool trusted_r=false)
Initial import from RpmDb.
Definition: KeyRing.cc:192
User has chosen not to trust the key.
Definition: KeyRing.h:58
Impl(const filesystem::Pathname &baseTmpDir)
Definition: KeyRing.cc:139
virtual KeyTrust askUserToAcceptKey(const PublicKey &key, const KeyContext &keycontext=KeyContext())
Ask user to trust and/or import the key to trusted keyring.
Definition: KeyRing.cc:69
static DefaultAccept defaultAccept()
Get the active accept bits.
Definition: KeyRing.cc:53
RW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: KeyRing.h:308
std::list< PublicKeyData > publicKeyData()
Get a list of public key data in the keyring (key data only)
Definition: KeyRing.cc:207
Base class for Exception.
Definition: Exception.h:152
const Tp & get(const std::string &key_r) const
Pass back a const Tp & reference to key_r value.
Definition: UserData.h:176
Typesafe passing of user data via callbacks.
Definition: UserData.h:39
void deleteKey(const std::string &id, bool trusted=false)
removes a key from the keyring.
Definition: KeyRing.cc:198
static constexpr const char * KEYS_NOT_IMPORTED_REPORT
Definition: KeyRing.h:132
void reportAutoImportKey(const std::list< PublicKeyData > &keyDataList_r, const PublicKeyData &keySigning_r, const KeyContext &keyContext_r)
Notify that a repository auto imported new package signing keys.
Definition: KeyRing.cc:103
bool hasvalue(const std::string &key_r) const
Whether key_r is in data and value is not empty.
Definition: UserData.h:102
bool isKeyTrusted(const std::string &id)
true if the key id is trusted
Definition: KeyRing.cc:233
static constexpr const char * REPORT_AUTO_IMPORT_KEY
Definition: KeyRing.h:151
void allowPreload(bool yesno_r)
The general keyring may be populated with known keys stored on the system.
Definition: KeyRing.cc:185
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
callback::SendReport< KeyRingSignals > _emitSignal
Definition: KeyRing.cc:135
std::string readSignatureKeyId(const Pathname &signature)
reads the public key id from a signature
Definition: KeyRing.cc:195
static constexpr const char * ACCEPT_PACKAGE_KEY_REQUEST
Definition: KeyRing.h:119
virtual bool askUserToAcceptVerificationFailed(const std::string &file, const PublicKey &key, const KeyContext &keycontext=KeyContext())
The file filedesc is signed but the verification failed.
Definition: KeyRing.cc:81
std::list< PublicKey > publicKeys()
Get a list of public keys in the keyring (incl.
Definition: KeyRing.cc:201
bool verifyFileTrustedSignature(const Pathname &file, const Pathname &signature) ZYPP_API
Definition: KeyRing.cc:222