libzypp  17.32.5
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 <map>
17 #include <list>
18 #include <set>
19 #include <string>
20 #include <utility>
21 
22 #include <zypp/base/Iterable.h>
23 #include <zypp/base/PtrTypes.h>
24 #include <zypp/base/Exception.h>
26 #include <zypp/Pathname.h>
27 #include <zypp/Edition.h>
28 #include <zypp/Date.h>
29 
30 struct _gpgme_key;
31 struct _gpgme_subkey;
32 struct _gpgme_key_sig;
33 
35 namespace zypp
36 {
37 
38  namespace filesystem
39  {
40  class TmpFile;
41  }
42  class PublicKeyData;
43  class KeyManagerCtx;
44 
49  class BadKeyException : public Exception
50  {
51  public:
56  : Exception( "Bad Key Exception" )
57  {}
58 
59  Pathname keyFile() const
60  { return _keyfile; }
61 
65  BadKeyException( const std::string & msg_r, Pathname keyfile = Pathname() )
66  : Exception( msg_r ), _keyfile(std::move(keyfile))
67  {}
69  ~BadKeyException() throw() override {};
70  private:
72  };
74 
81  {
82  public:
85 
87 
89  explicit operator bool() const;
90 
91  public:
93  std::string id() const;
94 
96  Date created() const;
97 
99  Date expires() const;
100 
102  bool expired() const;
103 
109  int daysToLive() const;
110 
117  std::string asString() const;
118 
119  private:
120  struct Impl;
122  friend class PublicKeyData;
123  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
124  PublicSubkeyData(const _gpgme_subkey *rawSubKeyData);
125  };
127 
129  inline std::ostream & operator<<( std::ostream & str, const PublicSubkeyData & obj )
130  { return str << obj.asString(); }
131 
138  {
139  public:
142 
144 
146  explicit operator bool() const;
147 
148  public:
150  std::string id() const;
151 
153  std::string name() const;
154 
156  Date created() const;
157 
159  Date expires() const;
160 
162  bool expired() const;
163 
169  int daysToLive() const;
170 
172  bool inTrustedRing() const;
173 
175  bool inKnownRing() const;
176 
183  std::string asString() const;
184 
185  private:
186  struct Impl;
188  friend class PublicKeyData;
189  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
190  PublicKeySignatureData(const _gpgme_key_sig *rawKeySignatureData);
191  };
193 
195  inline std::ostream & operator<<( std::ostream & str, const PublicKeySignatureData & obj )
196  { return str << obj.asString(); }
197 
208  {
209  public:
211  PublicKeyData();
212 
213  ~PublicKeyData();
214 
215  PublicKeyData(const PublicKeyData &) = default;
216  PublicKeyData(PublicKeyData &&) noexcept = default;
217  PublicKeyData &operator=(const PublicKeyData &) = default;
218  PublicKeyData &operator=(PublicKeyData &&) noexcept = default;
219 
221  explicit operator bool() const;
222 
223  public:
225  std::string id() const;
226 
228  std::string name() const;
229 
231  std::string fingerprint() const;
232 
234  std::string algoName() const;
235 
237  Date created() const;
238 
240  Date expires() const;
241 
243  bool expired() const;
244 
250  int daysToLive() const;
251 
264  std::string expiresAsString() const;
265 
267  std::string gpgPubkeyVersion() const;
268 
270  std::string gpgPubkeyRelease() const;
271 
273  std::string rpmName () const;
274 
277  { return Edition( gpgPubkeyVersion(), gpgPubkeyRelease() ); }
278 
285  std::string asString() const;
286 
287  public:
290 
292  bool hasSubkeys() const;
293 
296 
299 
303  bool providesKey( const std::string & id_r ) const;
304 
308  static bool isSafeKeyId( const std::string & id_r )
309  { return id_r.size() >= 16; }
310 
311  public:
313  bool hasSignatures() const;
314 
315  public:
318 
325  AsciiArt asciiArt() const;
326 
327  private:
328  struct Impl;
330 
331  friend class KeyManagerCtx;
332  static PublicKeyData fromGpgmeKey(_gpgme_key *data);
333 
334  PublicKeyData(shared_ptr<Impl> data);
335  friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
336  };
338 
340  inline std::ostream & operator<<( std::ostream & str, const PublicKeyData & obj )
341  { return str << obj.asString(); }
342 
344  std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
345 
347  bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs );
348 
350  inline bool operator!=( const PublicKeyData & lhs, const PublicKeyData & rhs )
351  { return !( lhs == rhs ); }
352 
364  class PublicKey
365  {
366  public:
368  struct Impl;
369 
370  public:
372  PublicKey();
373 
382  explicit PublicKey( const Pathname & keyFile_r );
383 
390  explicit PublicKey( const filesystem::TmpFile & sharedFile_r );
391 
392  ~PublicKey();
393 
395  static PublicKey noThrow( const Pathname & keyFile_r );
396 
397  public:
399  const PublicKeyData & keyData() const;
400 
402 
403  bool isValid() const
404  { return ! ( id().empty() || fingerprint().empty() ); }
405 
406  std::string id() const;
407  std::string name() const;
408  std::string fingerprint() const;
409  std::string algoName() const;
410  Date created() const;
411  Date expires() const;
412  std::string expiresAsString() const;
413  bool expired() const;
414  int daysToLive() const;
415  std::string gpgPubkeyVersion() const;
416  std::string gpgPubkeyRelease() const;
417  std::string asString() const;
418  std::string rpmName () const;
419 
421  { return keyData().gpgPubkeyEdition(); }
422 
423  bool hasSubkeys() const
424  { return keyData().hasSubkeys(); }
425 
427  { return keyData().subkeys(); }
428 
429  bool providesKey( const std::string & id_r ) const
430  { return keyData().providesKey( id_r ); }
431 
432  static bool isSafeKeyId( const std::string & id_r )
433  { return PublicKeyData::isSafeKeyId(id_r); }
434 
435  public:
437 
439  { return keyData().asciiArt(); }
440 
441  public:
443  Pathname path() const;
444 
446  const std::list<PublicKeyData> & hiddenKeys() const;
447 
451  bool fileProvidesKey( const std::string & id_r ) const;
452 
453  public:
454  bool operator==( const PublicKey & rhs ) const;
455  bool operator!=( const PublicKey & rhs ) const
456  { return not operator==( rhs ); }
457  bool operator==( const std::string & sid ) const;
458  bool operator!=( const std::string & sid ) const
459  { return not operator==( sid ); }
460 
461  private:
462  friend class KeyRing;
464  PublicKey( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r );
466  explicit PublicKey( const PublicKeyData & keyData_r );
467 
468  private:
471  };
473 
475  inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
476  { return str << obj.asString(); }
477 
479  std::ostream & dumpOn( std::ostream & str, const PublicKey & obj );
480 
482 } // namespace zypp
484 #endif // ZYPP_PUBLICKEY_H
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:196
PublicKeySignatureData()
Default constructed: empty data.
Definition: PublicKey.cc:238
bool hasSignatures() const
Whether signatures is not empty.
static bool isSafeKeyId(const std::string &id_r)
Whether this is a long id (64bit/16byte) or even better a fingerprint.
Definition: PublicKey.h:308
int daysToLive() const
Number of days (24h) until the key expires (or since it exired).
Definition: PublicKey.cc:193
const std::list< PublicKeyData > & hiddenKeys() const
Additional keys data in case the ASCII armored blob contains multiple keys.
Definition: PublicKey.cc:649
const PublicKeyData & keyData() const
The public keys data (.
Definition: PublicKey.cc:643
std::string name() const
Definition: PublicKey.cc:666
Iterable< KeySignatureIterator > signatures() const
Iterate all key signatures.
Definition: PublicKey.cc:472
Gpg key handling.
Definition: KeyRing.h:186
bool operator!=(const std::string &sid) const
Definition: PublicKey.h:458
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:120
std::string gpgPubkeyRelease() const
Gpg-pubkey release as computed by rpm (hexencoded created)
Definition: PublicKey.cc:448
PublicSubkeyData implementation.
Definition: PublicKey.cc:132
bool fileProvidesKey(const std::string &id_r) const
Extends providesKey to look at the hidden keys too.
Definition: PublicKey.cc:652
Edition gpgPubkeyEdition() const
!<
Definition: PublicKey.h:420
Class representing one GPG Public Keys data.
Definition: PublicKey.h:207
bool expired() const
Whether the key has expired.
Definition: PublicKey.cc:190
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:454
Exception thrown when the supplied key is not a valid gpg key.
Definition: PublicKey.h:49
std::string algoName() const
Key algorithm string like RSA 2048
Definition: PublicKey.cc:427
PublicSubkeyData()
Default constructed: empty data.
Definition: PublicKey.cc:163
std::string id() const
Definition: PublicKey.cc:663
base::DrunkenBishop AsciiArt
Random art fingerprint visualization type (base::DrunkenBishop).
Definition: PublicKey.h:317
Iterable< SubkeyIterator > subkeys() const
!<
Definition: PublicKey.h:426
std::string name() const
Key name.
Definition: PublicKey.cc:421
String related utilities and Regular expression matching.
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:186
Definition: Arch.h:363
std::string asString() const
Simple string representation.
Definition: PublicKey.cc:275
Date created() const
Creation date.
Definition: PublicKey.cc:184
Edition represents [epoch:]version[-release]
Definition: Edition.h:60
int daysToLive() const
Definition: PublicKey.cc:684
bool hasSubkeys() const
!<
Definition: PublicKey.h:423
std::ostream & operator<<(std::ostream &str, const PublicKeySignatureData &obj)
Definition: PublicKey.h:195
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
Provide a new empty temporary file and delete it when no longer needed.
Definition: TmpPath.h:127
Iterable< SubkeyIterator > subkeys() const
Iterate any subkeys.
Definition: PublicKey.cc:469
Date expires() const
Expiry date, or Date() if the key never expires.
Definition: PublicKey.cc:266
PublicKeyData()
Default constructed: empty data.
Definition: PublicKey.cc:401
PublicKeySignatureData implementation.
Definition: PublicKey.cc:206
bool inTrustedRing() const
Whether the signature is trusted in rpmdb.
Definition: PublicKey.cc:290
AsciiArt asciiArt() const
!<
Definition: PublicKey.h:438
std::ostream & operator<<(std::ostream &str, const PublicKeyData &obj)
Definition: PublicKey.h:340
std::string expiresAsString() const
Definition: PublicKey.cc:687
std::string gpgPubkeyVersion() const
Gpg-pubkey version as computed by rpm (trailing 8 byte id)
Definition: PublicKey.cc:445
std::string id() const
Subkey ID.
Definition: PublicKey.cc:181
Date expires() const
Definition: PublicKey.cc:678
bool operator!=(const PublicKey &rhs) const
Definition: PublicKey.h:455
bool operator==(const PublicKey &rhs) const
Definition: PublicKey.cc:702
std::string expiresAsString() const
Definition: PublicKey.cc:442
const PublicSubkeyData * SubkeyIterator
Definition: PublicKey.h:288
int daysToLive() const
Number of days (24h) until the key expires (or since it exired).
Definition: PublicKey.cc:439
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: PublicKey.h:470
std::string rpmName() const
Gpg-pubkey name as computed by rpm.
Definition: PublicKey.cc:451
Store and operate on date (time_t).
Definition: Date.h:32
Date created() const
Creation / last modification date (latest selfsig).
Definition: PublicKey.cc:430
bool expired() const
Whether the key has expired.
Definition: PublicKey.cc:269
std::string name() const
The user ID associated with this key, if present.
Definition: PublicKey.cc:260
std::string gpgPubkeyVersion() const
Definition: PublicKey.cc:690
std::string rpmName() const
Definition: PublicKey.cc:699
bool providesKey(const std::string &id_r) const
!<
Definition: PublicKey.h:429
bool inKnownRing() const
Whether the key has been seen before.
Definition: PublicKey.cc:293
BadKeyException()
Ctor taking message.
Definition: PublicKey.h:55
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
Definition: Capability.cc:580
friend std::ostream & dumpOn(std::ostream &str, const PublicKeyData &obj)
Definition: PublicKey.cc:489
int daysToLive() const
Number of days (24h) until the key expires (or since it expired).
Definition: PublicKey.cc:272
Pathname keyFile() const
Definition: PublicKey.h:59
std::ostream & operator<<(std::ostream &str, const PublicSubkeyData &obj)
Definition: PublicKey.h:129
PublicKey()
Default ctor.
Definition: PublicKey.cc:617
Date created() const
Creation date.
Definition: PublicKey.cc:263
PublicKey implementation.
Definition: PublicKey.cc:512
std::string fingerprint() const
Key fingerprint.
Definition: PublicKey.cc:424
bool expired() const
Whether the key has expired.
Definition: PublicKey.cc:436
~BadKeyException() override
Dtor.
Definition: PublicKey.h:69
std::string gpgPubkeyRelease() const
Definition: PublicKey.cc:693
Class representing a GPG Public Keys subkeys.
Definition: PublicKey.h:80
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition: PublicKey.h:364
std::ostream & operator<<(std::ostream &str, const PublicKey &obj)
Definition: PublicKey.h:475
Date expires() const
Expiry date, or Date() if the key never expires.
Definition: PublicKey.cc:187
AsciiArt asciiArt() const
Random art fingerprint visualization (base::DrunkenBishop).
Definition: PublicKey.cc:486
Date created() const
Definition: PublicKey.cc:675
Base class for Exception.
Definition: Exception.h:146
static bool isSafeKeyId(const std::string &id_r)
!<
Definition: PublicKey.h:432
Pathname path() const
File containing the ASCII armored key.
Definition: PublicKey.cc:646
std::string id() const
Key ID.
Definition: PublicKey.cc:418
Edition gpgPubkeyEdition() const
Gpg-pubkey Edition built from version and release.
Definition: PublicKey.h:276
RWCOW_pointer< Impl > _pimpl
Definition: PublicKey.h:328
std::string fingerprint() const
Definition: PublicKey.cc:669
BadKeyException(const std::string &msg_r, Pathname keyfile=Pathname())
Ctor taking message.
Definition: PublicKey.h:65
std::string asString() const
Definition: PublicKey.cc:696
bool expired() const
Definition: PublicKey.cc:681
PublicKeyData & operator=(const PublicKeyData &)=default
PublicKeyData implementation.
Definition: PublicKey.cc:301
friend std::ostream & dumpOn(std::ostream &str, const PublicKeyData &obj)
Definition: PublicKey.cc:489
static PublicKey noThrow(const Pathname &keyFile_r)
Static ctor returning an empty PublicKey rather than throwing.
Definition: PublicKey.cc:640
bool operator!=(const PublicKeyData &lhs, const PublicKeyData &rhs)
Definition: PublicKey.h:350
friend std::ostream & dumpOn(std::ostream &str, const PublicKeyData &obj)
Definition: PublicKey.cc:489
bool isValid() const
Definition: PublicKey.h:403
static PublicKeyData fromGpgmeKey(_gpgme_key *data)
Definition: PublicKey.cc:412
Easy-to use interface to the ZYPP dependency resolver.
Definition: Application.cc:19
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:475
Class representing a signature on a GPG Public Key.
Definition: PublicKey.h:137
Date expires() const
Expiry date, or Date() if the key never expires.
Definition: PublicKey.cc:433
std::string algoName() const
Definition: PublicKey.cc:672
std::string id() const
The key ID of key used to create the signature.
Definition: PublicKey.cc:257
bool hasSubkeys() const
Whether subkeys is not empty.
Definition: PublicKey.cc:466
Random art fingerprint visualization Visualize fingerprint data on a [17x9] (SSH) or [19x11] (GPG) or...
Definition: DrunkenBishop.h:61