libzypp  17.38.7
PublicKey.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_PUBLICKEY_H
13 #define ZYPP_PUBLICKEY_H
14 
15 #include <iosfwd>
16 #include <list>
17 #include <string>
18 #include <utility>
19 
21 
25 #include <zypp-core/Pathname.h>
26 #include <zypp/Edition.h>
27 #include <zypp-core/Date.h>
28 
29 struct _gpgme_key;
30 struct _gpgme_subkey;
31 struct _gpgme_key_sig;
32 
34 namespace zypp
35 {
36 
37  namespace filesystem
38  {
39  class TmpFile;
40  }
41  class PublicKeyData;
42  class KeyManagerCtx;
43 
49  {
50  public:
55  : Exception( "Bad Key Exception" )
56  {}
57 
58  Pathname keyFile() const
59  { return _keyfile; }
60 
64  BadKeyException( const std::string & msg_r, Pathname keyfile = Pathname() )
65  : Exception( msg_r ), _keyfile(std::move(keyfile))
66  {}
68  ~BadKeyException() throw() override {};
69  private:
70  Pathname _keyfile;
71  };
73 
80  {
81  public:
84 
86 
88  explicit operator bool() const;
89 
90  public:
92  std::string id() const;
93 
95  Date created() const;
96 
98  Date expires() const;
99 
101  bool expired() const;
102 
108  int daysToLive() const;
109 
116  std::string asString() const;
117 
118  private:
119  struct Impl;
121  friend class PublicKeyData;
122  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
123  PublicSubkeyData(const _gpgme_subkey *rawSubKeyData);
124  };
126 
128  inline std::ostream & operator<<( std::ostream & str, const PublicSubkeyData & obj )
129  { return str << obj.asString(); }
130 
137  {
138  public:
141 
143 
145  explicit operator bool() const;
146 
147  public:
149  std::string id() const;
150 
152  std::string name() const;
153 
155  Date created() const;
156 
158  Date expires() const;
159 
161  bool expired() const;
162 
168  int daysToLive() const;
169 
176  std::string asString() const;
177 
178  private:
179  struct Impl;
181  friend class PublicKeyData;
182  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
183  PublicKeySignatureData(const _gpgme_key_sig *rawKeySignatureData);
184  };
186 
188  inline std::ostream & operator<<( std::ostream & str, const PublicKeySignatureData & obj )
189  { return str << obj.asString(); }
190 
201  {
202  public:
204  PublicKeyData();
205 
206  ~PublicKeyData();
207 
208  PublicKeyData(const PublicKeyData &) = default;
209  PublicKeyData(PublicKeyData &&) noexcept = default;
210  PublicKeyData &operator=(const PublicKeyData &) = default;
211  PublicKeyData &operator=(PublicKeyData &&) noexcept = default;
212 
214  explicit operator bool() const;
215 
216  public:
218  std::string id() const;
219 
221  std::string name() const;
222 
224  std::string fingerprint() const;
225 
227  std::string algoName() const;
228 
230  Date created() const;
231 
233  Date expires() const;
234 
236  bool expired() const;
237 
243  int daysToLive() const;
244 
257  std::string expiresAsString() const;
258 
260  std::string gpgPubkeyVersion() const;
261 
263  std::string gpgPubkeyRelease() const;
264 
266  std::string rpmName () const;
267 
270  { return Edition( gpgPubkeyVersion(), gpgPubkeyRelease() ); }
271 
278  std::string asString() const;
279 
284  bool isUpdateFor( const PublicKeyData & rhs ) const
285  {
286  // bnc #393160: Comment #30: Compare at least the fingerprint
287  // in case an attacker created a key the the same id.
288  //
289  // bsc#1008325: For keys using subkeys, we'd actually need to compare
290  // the subkey sets, to tell whether a key was updated. because created()
291  // remains unchanged if the primary key is not touched.
292  // For now we wait until a new subkey signs any metadata and treat it as
293  // a new key then (see VerifyFileSignatureLogic)
294  return not rhs || ( fingerprint() == rhs.fingerprint() && created() > rhs.created() );
295  }
296 
297  public:
300 
302  bool hasSubkeys() const;
303 
305  Iterable<SubkeyIterator> subkeys() const;
306 
308  Iterable<KeySignatureIterator> signatures() const;
309 
313  bool providesKey( const std::string & id_r ) const;
314 
318  static bool isSafeKeyId( const std::string & id_r )
319  { return id_r.size() >= 16; }
320 
321  public:
323  bool hasSignatures() const;
324 
325  public:
328 
335  AsciiArt asciiArt() const;
336 
337  private:
338  struct Impl;
340 
341  friend class KeyManagerCtx;
342  static PublicKeyData fromGpgmeKey(_gpgme_key *data);
343 
344  PublicKeyData(shared_ptr<Impl> data);
345  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
346  };
348 
350  inline std::ostream & operator<<( std::ostream & str, const PublicKeyData & obj )
351  { return str << obj.asString(); }
352 
354  std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj ) ZYPP_API;
355 
357  bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs ) ZYPP_API;
358 
360  inline bool operator!=( const PublicKeyData & lhs, const PublicKeyData & rhs )
361  { return !( lhs == rhs ); }
362 
375  {
376  public:
378  struct Impl;
379 
380  public:
382  PublicKey();
383 
392  explicit PublicKey( const Pathname & keyFile_r );
393 
400  explicit PublicKey( const filesystem::TmpFile & sharedFile_r );
401 
402  ~PublicKey();
403 
405  static PublicKey noThrow( const Pathname & keyFile_r );
406 
407  public:
409  const PublicKeyData & keyData() const;
410 
412 
413  bool isValid() const
414  { return ! ( id().empty() || fingerprint().empty() ); }
415 
416  std::string id() const;
417  std::string name() const;
418  std::string fingerprint() const;
419  std::string algoName() const;
420  Date created() const;
421  Date expires() const;
422  std::string expiresAsString() const;
423  bool expired() const;
424  int daysToLive() const;
425  std::string gpgPubkeyVersion() const;
426  std::string gpgPubkeyRelease() const;
427  std::string asString() const;
428  std::string rpmName () const;
429 
431  { return keyData().gpgPubkeyEdition(); }
432 
433  bool hasSubkeys() const
434  { return keyData().hasSubkeys(); }
435 
437  { return keyData().subkeys(); }
438 
439  bool providesKey( const std::string & id_r ) const
440  { return keyData().providesKey( id_r ); }
441 
442  static bool isSafeKeyId( const std::string & id_r )
443  { return PublicKeyData::isSafeKeyId(id_r); }
444 
445  public:
447 
449  { return keyData().asciiArt(); }
450 
451  public:
453  Pathname path() const;
454 
455  bool hasHiddenKeys() const
456  { return not hiddenKeys().empty(); }
457 
459  const std::list<PublicKeyData> & hiddenKeys() const;
460 
464  bool fileProvidesKey( const std::string & id_r ) const;
465 
466  public:
467  bool operator==( const PublicKey & rhs ) const;
468  bool operator!=( const PublicKey & rhs ) const
469  { return not operator==( rhs ); }
470  bool operator==( const std::string & sid ) const;
471  bool operator!=( const std::string & sid ) const
472  { return not operator==( sid ); }
473 
474  private:
475  friend class KeyRingImpl;
477  PublicKey( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r );
479  explicit PublicKey( const PublicKeyData & keyData_r );
480 
481  private:
484  };
486 
488  inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
489  { return str << obj.asString(); }
490 
492  std::ostream & dumpOn( std::ostream & str, const PublicKey & obj ) ZYPP_API;
493 
495 } // namespace zypp
497 #endif // ZYPP_PUBLICKEY_H
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:196
static bool isSafeKeyId(const std::string &id_r)
Whether this is a long id (64bit/16byte) or even better a fingerprint.
Definition: PublicKey.h:318
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:309
bool hasHiddenKeys() const
Definition: PublicKey.h:455
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition: Patch.cc:122
bool operator!=(const std::string &sid) const
Definition: PublicKey.h:471
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:119
PublicSubkeyData implementation.
Definition: PublicKey.cc:132
KeyRing implementation, shared between zyppng and zypp.
Definition: keyring_p.h:90
Edition gpgPubkeyEdition() const
!<
Definition: PublicKey.h:430
Class representing one GPG Public Keys data.
Definition: PublicKey.h:200
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:448
Exception thrown when the supplied key is not a valid gpg key.
Definition: PublicKey.h:48
base::DrunkenBishop AsciiArt
Random art fingerprint visualization type (base::DrunkenBishop).
Definition: PublicKey.h:327
Iterable< SubkeyIterator > subkeys() const
!<
Definition: PublicKey.h:436
String related utilities and Regular expression matching.
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:179
Definition: ansi.h:854
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:275
Edition represents [epoch:]version[-release]
Definition: Edition.h:59
bool hasSubkeys() const
!<
Definition: PublicKey.h:433
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:117
Iterable< SubkeyIterator > subkeys() const
Iterate any subkeys.
Definition: PublicKey.cc:463
PublicKeySignatureData implementation.
Definition: PublicKey.cc:206
AsciiArt asciiArt() const
!<
Definition: PublicKey.h:448
bool operator!=(const PublicKey &rhs) const
Definition: PublicKey.h:468
const PublicSubkeyData * SubkeyIterator
Definition: PublicKey.h:298
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: PublicKey.h:483
Store and operate on date (time_t).
Definition: Date.h:32
Date created() const
Creation / last modification date (latest selfsig).
Definition: PublicKey.cc:424
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition: Arch.h:173
bool isUpdateFor(const PublicKeyData &rhs) const
Whether this could replace rhs in a keyring.
Definition: PublicKey.h:284
bool operator!=(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition: Capability.h:313
bool providesKey(const std::string &id_r) const
!<
Definition: PublicKey.h:439
BadKeyException()
Ctor taking message.
Definition: PublicKey.h:54
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition: Capability.cc:589
Pathname keyFile() const
Definition: PublicKey.h:58
PublicKey implementation.
Definition: PublicKey.cc:506
std::string fingerprint() const
Key fingerprint.
Definition: PublicKey.cc:418
~BadKeyException() override
Dtor.
Definition: PublicKey.h:68
Class representing a GPG Public Keys subkeys.
Definition: PublicKey.h:79
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:374
AsciiArt asciiArt() const
Random art fingerprint visualization (base::DrunkenBishop).
Definition: PublicKey.cc:480
Base class for Exception.
Definition: Exception.h:152
static bool isSafeKeyId(const std::string &id_r)
!<
Definition: PublicKey.h:442
Edition gpgPubkeyEdition() const
Gpg-pubkey Edition built from version and release.
Definition: PublicKey.h:269
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:338
BadKeyException(const std::string &msg_r, Pathname keyfile=Pathname())
Ctor taking message.
Definition: PublicKey.h:64
std::string asString() const
Definition: PublicKey.cc:690
PublicKeyData implementation.
Definition: PublicKey.cc:295
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition: Capabilities.cc:65
bool isValid() const
Definition: PublicKey.h:413
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
bool providesKey(const std::string &id_r) const
Whether id_r is the id or fingerprint of the primary key or of a subkey.
Definition: PublicKey.cc:469
Class representing a signature on a GPG Public Key.
Definition: PublicKey.h:136
bool hasSubkeys() const
Whether subkeys is not empty.
Definition: PublicKey.cc:460
Random art fingerprint visualization Visualize fingerprint data on a [17x9] (SSH) or [19x11] (GPG) or...
Definition: DrunkenBishop.h:61