libzypp: zypp-common/PublicKey.h Source File

libzypp 17.38.9

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
20#include <zypp-common/base/DrunkenBishop.h>
21
22#include <zypp-core/base/Iterable.h>
23#include <zypp-core/base/PtrTypes.h>
24#include <zypp-core/base/Exception.h>
25#include <zypp-core/Pathname.h>
26#include <zypp/Edition.h>
27#include <zypp-core/Date.h>
28
29struct _gpgme_key;
30struct _gpgme_subkey;
31struct _gpgme_key_sig;
32
34namespace zypp
35{
36
37 namespace filesystem
38 {
39 class TmpFile;
40 }
41 class PublicKeyData;
42 class KeyManagerCtx;
43
48 class ZYPP_API BadKeyException : public Exception
49 {
50 public:
54 BadKeyException()
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 {}
67
68 ~BadKeyException() throw() override {};
69 private:
70 Pathname _keyfile;
71 };
72
73
79 class ZYPP_API PublicSubkeyData
80 {
81 public:
83 PublicSubkeyData();
84
85 ~PublicSubkeyData();
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;
120 RWCOW_pointer<Impl> _pimpl;
121 friend class PublicKeyData;
122 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
123 PublicSubkeyData(const _gpgme_subkey *rawSubKeyData);
124 };
125
126
128 inline std::ostream & operator<<( std::ostream & str, const PublicSubkeyData & obj )
129 { return str << obj.asString(); }
130
136 class ZYPP_API PublicKeySignatureData
137 {
138 public:
140 PublicKeySignatureData();
141
142 ~PublicKeySignatureData();
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;
180 RWCOW_pointer<Impl> _pimpl;
181 friend class PublicKeyData;
182 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
183 PublicKeySignatureData(const _gpgme_key_sig *rawKeySignatureData);
184 };
185
186
188 inline std::ostream & operator<<( std::ostream & str, const PublicKeySignatureData & obj )
189 { return str << obj.asString(); }
190
200 class ZYPP_API PublicKeyData
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
269 Edition gpgPubkeyEdition() const
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:
298 using SubkeyIterator = const PublicSubkeyData *;
299 using KeySignatureIterator = const PublicKeySignatureData *;
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:
327 using AsciiArt = base::DrunkenBishop;
328
335 AsciiArt asciiArt() const;
336
337 private:
338 struct Impl;
339 RWCOW_pointer<Impl> _pimpl;
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 };
347
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
374 class ZYPP_API PublicKey
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
411 using SubkeyIterator = PublicKeyData::SubkeyIterator;
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
430 Edition gpgPubkeyEdition() const
431 { return keyData().gpgPubkeyEdition(); }
432
433 bool hasSubkeys() const
434 { return keyData().hasSubkeys(); }
435
436 Iterable<SubkeyIterator> subkeys() const
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:
446 using AsciiArt = PublicKeyData::AsciiArt;
447
448 AsciiArt asciiArt() const
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:
483 RWCOW_pointer<Impl> _pimpl;
484 };
485
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
Date.h
DrunkenBishop.h
Edition.h
Exception.h
ZYPP_API
#define ZYPP_API
Definition Globals.h:69
Iterable.h
Pathname.h
PtrTypes.h
bool
shared_ptr
zypp::BadKeyException::keyFile
Pathname keyFile() const
Definition PublicKey.h:58
zypp::BadKeyException::BadKeyException
BadKeyException(const std::string &msg_r, Pathname keyfile=Pathname())
Ctor taking message.
Definition PublicKey.h:64
zypp::BadKeyException::~BadKeyException
~BadKeyException() override
Dtor.
Definition PublicKey.h:68
zypp::BadKeyException::_keyfile
Pathname _keyfile
Definition PublicKey.h:70
zypp::BadKeyException::BadKeyException
BadKeyException()
Ctor taking message.
Definition PublicKey.h:54
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Edition
Edition represents [epoch:]version[-release].
Definition Edition.h:60
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:94
zypp::Iterable
Definition Iterable.h:37
zypp::KeyManagerCtx
Definition KeyManager.h:31
zypp::PublicKeyData
Class representing one GPG Public Keys data.
Definition PublicKey.h:201
zypp::PublicKeyData::signatures
Iterable< KeySignatureIterator > signatures() const
Iterate all key signatures.
Definition PublicKey.cc:466
zypp::PublicKeyData::created
Date created() const
Creation / last modification date (latest selfsig).
Definition PublicKey.cc:424
zypp::PublicKeyData::expired
bool expired() const
Whether the key has expired.
Definition PublicKey.cc:430
zypp::PublicKeyData::name
std::string name() const
Key name.
Definition PublicKey.cc:415
zypp::PublicKeyData::hasSignatures
bool hasSignatures() const
Whether signatures is not empty.
zypp::PublicKeyData::subkeys
Iterable< SubkeyIterator > subkeys() const
Iterate any subkeys.
Definition PublicKey.cc:463
zypp::PublicKeyData::daysToLive
int daysToLive() const
Number of days (24h) until the key expires (or since it exired).
Definition PublicKey.cc:433
zypp::PublicKeyData::KeyManagerCtx
friend class KeyManagerCtx
Definition PublicKey.h:341
zypp::PublicKeyData::rpmName
std::string rpmName() const
Gpg-pubkey name as computed by rpm.
Definition PublicKey.cc:445
zypp::PublicKeyData::hasSubkeys
bool hasSubkeys() const
Whether subkeys is not empty.
Definition PublicKey.cc:460
zypp::PublicKeyData::isUpdateFor
bool isUpdateFor(const PublicKeyData &rhs) const
Whether this could replace rhs in a keyring.
Definition PublicKey.h:284
zypp::PublicKeyData::PublicKeyData
PublicKeyData()
Default constructed: empty data.
Definition PublicKey.cc:395
zypp::PublicKeyData::SubkeyIterator
const PublicSubkeyData * SubkeyIterator
Definition PublicKey.h:298
zypp::PublicKeyData::expires
Date expires() const
Expiry date, or Date() if the key never expires.
Definition PublicKey.cc:427
zypp::PublicKeyData::algoName
std::string algoName() const
Key algorithm string like RSA 2048.
Definition PublicKey.cc:421
zypp::PublicKeyData::PublicKeyData
PublicKeyData(const PublicKeyData &)=default
zypp::PublicKeyData::isSafeKeyId
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
zypp::PublicKeyData::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition PublicKey.h:339
zypp::PublicKeyData::providesKey
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
zypp::PublicKeyData::fingerprint
std::string fingerprint() const
Key fingerprint.
Definition PublicKey.cc:418
zypp::PublicKeyData::gpgPubkeyRelease
std::string gpgPubkeyRelease() const
Gpg-pubkey release as computed by rpm (hexencoded created).
Definition PublicKey.cc:442
zypp::PublicKeyData::gpgPubkeyVersion
std::string gpgPubkeyVersion() const
Gpg-pubkey version as computed by rpm (trailing 8 byte id).
Definition PublicKey.cc:439
zypp::PublicKeyData::PublicKeyData
PublicKeyData(PublicKeyData &&) noexcept=default
zypp::PublicKeyData::fromGpgmeKey
static PublicKeyData fromGpgmeKey(_gpgme_key *data)
Definition PublicKey.cc:406
zypp::PublicKeyData::expiresAsString
std::string expiresAsString() const
Definition PublicKey.cc:436
zypp::PublicKeyData::asciiArt
AsciiArt asciiArt() const
Random art fingerprint visualization (base::DrunkenBishop).
Definition PublicKey.cc:480
zypp::PublicKeyData::asString
std::string asString() const
Simple string representation.
Definition PublicKey.cc:448
zypp::PublicKeyData::AsciiArt
base::DrunkenBishop AsciiArt
Random art fingerprint visualization type (base::DrunkenBishop).
Definition PublicKey.h:327
zypp::PublicKeyData::gpgPubkeyEdition
Edition gpgPubkeyEdition() const
Gpg-pubkey Edition built from version and release.
Definition PublicKey.h:269
zypp::PublicKeyData::KeySignatureIterator
const PublicKeySignatureData * KeySignatureIterator
Definition PublicKey.h:299
zypp::PublicKeySignatureData
Class representing a signature on a GPG Public Key.
Definition PublicKey.h:137
zypp::PublicKeySignatureData::created
Date created() const
Creation date.
Definition PublicKey.cc:263
zypp::PublicKeySignatureData::asString
std::string asString() const
Simple string representation.
Definition PublicKey.cc:275
zypp::PublicKeySignatureData::PublicKeySignatureData
PublicKeySignatureData()
Default constructed: empty data.
Definition PublicKey.cc:238
zypp::PublicKeySignatureData::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition PublicKey.h:180
zypp::PublicKeySignatureData::daysToLive
int daysToLive() const
Number of days (24h) until the key expires (or since it expired).
Definition PublicKey.cc:272
zypp::PublicKeySignatureData::expired
bool expired() const
Whether the key has expired.
Definition PublicKey.cc:269
zypp::PublicKeySignatureData::id
std::string id() const
The key ID of key used to create the signature.
Definition PublicKey.cc:257
zypp::PublicKeySignatureData::name
std::string name() const
The user ID associated with this key, if present.
Definition PublicKey.cc:260
zypp::PublicKeySignatureData::expires
Date expires() const
Expiry date, or Date() if the key never expires.
Definition PublicKey.cc:266
zypp::PublicKeySignatureData::PublicKeyData
friend class PublicKeyData
Definition PublicKey.h:181
zypp::PublicKey
Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
Definition PublicKey.h:375
zypp::PublicKey::gpgPubkeyEdition
Edition gpgPubkeyEdition() const
!<
Definition PublicKey.h:430
zypp::PublicKey::AsciiArt
PublicKeyData::AsciiArt AsciiArt
Definition PublicKey.h:446
zypp::PublicKey::asciiArt
AsciiArt asciiArt() const
!<
Definition PublicKey.h:448
zypp::PublicKey::_pimpl
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition PublicKey.h:483
zypp::PublicKey::PublicKey
PublicKey()
Default ctor.
Definition PublicKey.cc:611
zypp::PublicKey::hiddenKeys
const std::list< PublicKeyData > & hiddenKeys() const
Additional keys data in case the ASCII armored blob contains multiple keys.
Definition PublicKey.cc:643
zypp::PublicKey::isSafeKeyId
static bool isSafeKeyId(const std::string &id_r)
!<
Definition PublicKey.h:442
zypp::PublicKey::keyData
const PublicKeyData & keyData() const
The public keys data (.
Definition PublicKey.cc:637
zypp::PublicKey::asString
std::string asString() const
Definition PublicKey.cc:690
zypp::PublicKey::fingerprint
std::string fingerprint() const
Definition PublicKey.cc:663
zypp::PublicKey::id
std::string id() const
Definition PublicKey.cc:657
zypp::PublicKey::operator!=
bool operator!=(const std::string &sid) const
Definition PublicKey.h:471
zypp::PublicKey::operator!=
bool operator!=(const PublicKey &rhs) const
Definition PublicKey.h:468
zypp::PublicKey::SubkeyIterator
PublicKeyData::SubkeyIterator SubkeyIterator
Definition PublicKey.h:411
zypp::PublicKey::hasHiddenKeys
bool hasHiddenKeys() const
Definition PublicKey.h:455
zypp::PublicKey::KeyRingImpl
friend class KeyRingImpl
Definition PublicKey.h:475
zypp::PublicKey::noThrow
static PublicKey noThrow(const Pathname &keyFile_r)
Static ctor returning an empty PublicKey rather than throwing.
Definition PublicKey.cc:634
zypp::PublicKey::hasSubkeys
bool hasSubkeys() const
!<
Definition PublicKey.h:433
zypp::PublicKey::providesKey
bool providesKey(const std::string &id_r) const
!<
Definition PublicKey.h:439
zypp::PublicKey::subkeys
Iterable< SubkeyIterator > subkeys() const
!<
Definition PublicKey.h:436
zypp::PublicKey::isValid
bool isValid() const
Definition PublicKey.h:413
zypp::PublicSubkeyData
Class representing a GPG Public Keys subkeys.
Definition PublicKey.h:80
zypp::PublicSubkeyData::_pimpl
RWCOW_pointer< Impl > _pimpl
Definition PublicKey.h:120
zypp::PublicSubkeyData::id
std::string id() const
Subkey ID.
Definition PublicKey.cc:181
zypp::PublicSubkeyData::PublicSubkeyData
PublicSubkeyData()
Default constructed: empty data.
Definition PublicKey.cc:163
zypp::PublicSubkeyData::daysToLive
int daysToLive() const
Number of days (24h) until the key expires (or since it exired).
Definition PublicKey.cc:193
zypp::PublicSubkeyData::expires
Date expires() const
Expiry date, or Date() if the key never expires.
Definition PublicKey.cc:187
zypp::PublicSubkeyData::PublicKeyData
friend class PublicKeyData
Definition PublicKey.h:121
zypp::PublicSubkeyData::asString
std::string asString() const
Simple string representation.
Definition PublicKey.cc:196
zypp::PublicSubkeyData::created
Date created() const
Creation date.
Definition PublicKey.cc:184
zypp::PublicSubkeyData::expired
bool expired() const
Whether the key has expired.
Definition PublicKey.cc:190
zypp::base::DrunkenBishop
Random art fingerprint visualization Visualize fingerprint data on a [17x9] (SSH) or [19x11] (GPG) or...
Definition DrunkenBishop.h:62
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::TmpFile
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:118
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::filesystem
Types and functions for filesystem operations.
Definition Glob.cc:24
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::operator!=
bool operator!=(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:313
zypp::operator==
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:309
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173
zypp::dumpOn
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
relates: Capability Detailed stream output
Definition Capability.cc:589
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::PublicKeyData::Impl
PublicKeyData implementation.
Definition PublicKey.cc:296
zypp::PublicKeySignatureData::Impl
PublicKeySignatureData implementation.
Definition PublicKey.cc:207
zypp::PublicKey::Impl
PublicKey implementation.
Definition PublicKey.cc:507
zypp::PublicSubkeyData::Impl
PublicSubkeyData implementation.
Definition PublicKey.cc:133
zypp::RWCOW_pointer
RW_pointer supporting 'copy on write' functionality.
Definition PtrTypes.h:469