1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/PublicKey.h
12 #ifndef ZYPP_PUBLICKEY_H
13 #define ZYPP_PUBLICKEY_H
21 #include "zypp/base/Iterable.h"
22 #include "zypp/base/PtrTypes.h"
23 #include "zypp/base/Exception.h"
24 #include "zypp/base/DrunkenBishop.h"
25 #include "zypp/Pathname.h"
26 #include "zypp/Edition.h"
27 #include "zypp/Date.h"
32 ///////////////////////////////////////////////////////////////////
34 { /////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////
44 /// \class BadKeyException
45 /// \brief Exception thrown when the supplied key is not a valid gpg key
46 ///////////////////////////////////////////////////////////////////
47 class BadKeyException : public Exception
50 /** Ctor taking message.
51 * Use \ref ZYPP_THROW to throw exceptions.
54 : Exception( "Bad Key Exception" )
57 Pathname keyFile() const
60 /** Ctor taking message.
61 * Use \ref ZYPP_THROW to throw exceptions.
63 BadKeyException( const std::string & msg_r, const Pathname &keyfile = Pathname() )
64 : Exception( msg_r ), _keyfile(keyfile)
67 virtual ~BadKeyException() throw() {};
71 ///////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////
74 /// \class PublicSubkeyData
75 /// \brief Class representing a GPG Public Keys subkeys.
76 /// \see \ref PublicKeyData.
77 ///////////////////////////////////////////////////////////////////
78 class PublicSubkeyData
81 /** Default constructed: empty data. */
86 /** Whether this contains valid data (not default constructed). */
87 explicit operator bool() const;
91 std::string id() const;
96 /** Expiry date, or \c Date() if the key never expires. */
99 /** Whether the key has expired. */
100 bool expired() const;
102 /** Number of days (24h) until the key expires (or since it exired).
103 * A value of \c 0 means the key will expire within the next 24h.
104 * Negative values indicate the key has expired less than \c N days ago.
105 * For keys without expiration date \c INT_MAX is returned.
107 int daysToLive() const;
109 /** Simple string representation.
110 * Encodes \ref id, \ref created and \ref expires
112 * 640DB551 2016-04-12 [expires: 2019-04-12]
115 std::string asString() const;
119 RWCOW_pointer<Impl> _pimpl;
120 friend class PublicKeyData;
121 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
122 PublicSubkeyData(const _gpgme_subkey *rawSubKeyData);
124 ///////////////////////////////////////////////////////////////////
126 /** \relates PublicSubkeyData Stream output */
127 inline std::ostream & operator<<( std::ostream & str, const PublicSubkeyData & obj )
128 { return str << obj.asString(); }
130 ///////////////////////////////////////////////////////////////////
131 /// \class PublicKeyData
132 /// \brief Class representing one GPG Public Keys data.
133 /// \ref PublicKeyData are provided e.g. by a \ref PublicKey or
134 /// a \ref KeyRing. \ref PublicKeyData are usually easier to
135 /// retrieve and sufficient unless you actually need an ASCII
136 /// armored version of the key placed in a tempfile. In this
137 /// case use \ref PublicKey.
138 ///////////////////////////////////////////////////////////////////
142 /** Default constructed: empty data. */
147 /** Whether this contains valid data (not default constructed). */
148 explicit operator bool() const;
152 std::string id() const;
155 std::string name() const;
157 /** Key fingerprint.*/
158 std::string fingerprint() const;
160 /** Creation / last modification date (latest selfsig). */
161 Date created() const;
163 /** Expiry date, or \c Date() if the key never expires. */
164 Date expires() const;
166 /** Whether the key has expired. */
167 bool expired() const;
169 /** Number of days (24h) until the key expires (or since it exired).
170 * A value of \c 0 means the key will expire within the next 24h.
171 * Negative values indicate the key has expired less than \c N days ago.
172 * For keys without expiration date \c INT_MAX is returned.
174 int daysToLive() const;
176 /** * Expiry info in a human readable form.
177 * The exipry daye plus an annotation if the key has expired, or will
178 * expire within 90 days.
181 * Tue May 11 13:37:33 CEST 2010
182 * Tue May 11 13:37:33 CEST 2010 (expires in 90 days)
183 * Tue May 11 13:37:33 CEST 2010 (expires in 1 day)
184 * Tue May 11 13:37:33 CEST 2010 (expires within 24h)
185 * Tue May 11 13:37:33 CEST 2010 (EXPIRED)
188 std::string expiresAsString() const;
190 /** Gpg-pubkey version as computed by rpm (trailing 8 byte \ref id) */
191 std::string gpgPubkeyVersion() const;
193 /** Gpg-pubkey release as computed by rpm (hexencoded \ref created) */
194 std::string gpgPubkeyRelease() const;
196 /** Gpg-pubkey name as computed by rpm*/
197 std::string rpmName () const;
199 /** Gpg-pubkey \ref Edition built from version and release.*/
200 Edition gpgPubkeyEdition() const
201 { return Edition( gpgPubkeyVersion(), gpgPubkeyRelease() ); }
203 /** Simple string representation.
204 * Encodes \ref id, \ref gpgPubkeyRelease, \ref name and \ref fingerprint.
206 * [E3A5C360307E3D54-4be01a65] [SuSE Package Signing Key <build@suse.de>] [4E98E67519D98DC7362A5990E3A5C360307E3D54]
209 std::string asString() const;
212 typedef const PublicSubkeyData * SubkeyIterator;
214 /** Whether \ref subkeys is not empty. */
215 bool hasSubkeys() const;
217 /** Iterate any subkeys. */
218 Iterable<SubkeyIterator> subkeys() const;
220 /** Whether \a id_r is the \ref id of the primary key or of a subkey. */
221 bool providesKey( const std::string & id_r ) const;
224 /** Random art fingerprint visualization type (\ref base::DrunkenBishop). */
225 typedef base::DrunkenBishop AsciiArt;
227 /** Random art fingerprint visualization (\ref base::DrunkenBishop).
230 * cout << key.asciiArt( PublicKey::AsciiArt::USE_COLOR ) << endl;
233 AsciiArt asciiArt() const;
237 RWCOW_pointer<Impl> _pimpl;
239 friend class KeyManagerCtx;
240 static PublicKeyData fromGpgmeKey(_gpgme_key *data);
242 PublicKeyData(shared_ptr<Impl> data);
243 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
245 ///////////////////////////////////////////////////////////////////
247 /** \relates PublicKeyData Stream output */
248 inline std::ostream & operator<<( std::ostream & str, const PublicKeyData & obj )
249 { return str << obj.asString(); }
251 /** \relates PublicKeyData Detailed stream output */
252 std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
254 /** \relates PublicKeyData Equal based on fingerprint anf creation date. */
255 bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs );
257 /** \relates PublicKeyData NotEqual. */
258 inline bool operator!=( const PublicKeyData & lhs, const PublicKeyData & rhs )
259 { return !( lhs == rhs ); }
261 ///////////////////////////////////////////////////////////////////
263 /// \brief Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
265 /// If you don't need the ASCII armored version of the key stored in
266 /// a tempfile, using \ref PublicKeyData might be sufficient.
268 /// \note In case the ASCII armored blob actually contains multiple
269 /// keys, the \b last keys data are made available via the API. The
270 /// additional keys data are made available via \ref hiddenKeys.
271 ///////////////////////////////////////////////////////////////////
275 /** Implementation */
282 /** Ctor taking the key from a file.
284 * This is quite expensive, as a copy of the file is created and
285 * used. If you can construct PublicKey from a \ref filesystem::TmpFile,
286 * this prevents copying.
288 * \throws when data does not make a key
290 explicit PublicKey( const Pathname & keyFile_r );
292 /** Ctor reading the key from a \ref TmpFile.
294 * PublicKey holds a reference on the TmpFile providing the key.
296 * \throws when data does not make a key
298 explicit PublicKey( const filesystem::TmpFile & sharedFile_r );
303 /** The public keys data (\see \ref PublicKeyData).*/
304 const PublicKeyData & keyData() const;
306 typedef PublicKeyData::SubkeyIterator SubkeyIterator;
309 { return ! ( id().empty() || fingerprint().empty() ); }
311 std::string id() const; //!< \see \ref PublicKeyData
312 std::string name() const; //!< \see \ref PublicKeyData
313 std::string fingerprint() const; //!< \see \ref PublicKeyData
314 Date created() const; //!< \see \ref PublicKeyData
315 Date expires() const; //!< \see \ref PublicKeyData
316 std::string expiresAsString() const; //!< \see \ref PublicKeyData
317 bool expired() const; //!< \see \ref PublicKeyData
318 int daysToLive() const; //!< \see \ref PublicKeyData
319 std::string gpgPubkeyVersion() const; //!< \see \ref PublicKeyData
320 std::string gpgPubkeyRelease() const; //!< \see \ref PublicKeyData
321 std::string asString() const; //!< \see \ref PublicKeyData
322 std::string rpmName () const;
324 Edition gpgPubkeyEdition() const ///!< \see \ref PublicKeyData
325 { return keyData().gpgPubkeyEdition(); }
327 bool hasSubkeys() const ///!< \see \ref PublicKeyData
328 { return keyData().hasSubkeys(); }
330 Iterable<SubkeyIterator> subkeys() const ///!< \see \ref PublicKeyData
331 { return keyData().subkeys(); }
333 bool providesKey( const std::string & id_r ) const ///!< \see \ref PublicKeyData
334 { return keyData().providesKey( id_r ); }
337 typedef PublicKeyData::AsciiArt AsciiArt; ///!< \see \ref PublicKeyData
339 AsciiArt asciiArt() const ///!< \see \ref PublicKeyData
340 { return keyData().asciiArt(); }
343 /** File containig the ASCII armored key. */
344 Pathname path() const;
346 /** Additional keys data in case the ASCII armored blob containes multiple keys. */
347 const std::list<PublicKeyData> & hiddenKeys() const;
350 bool operator==( const PublicKey & rhs ) const;
351 bool operator==( const std::string & sid ) const;
354 friend class KeyRing;
355 /** KeyRing ctor: No need to parse file if KeyRing already had valid KeyData. */
356 PublicKey( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r );
357 /** KeyRing ctor: Legacy callback APIs take PublicKey, but just need the PublicKeyData No need to export to file. */
358 explicit PublicKey( const PublicKeyData & keyData_r );
361 /** Pointer to implementation */
362 RWCOW_pointer<Impl> _pimpl;
364 ///////////////////////////////////////////////////////////////////
366 /** \relates PublicKey Stream output */
367 inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
368 { return str << obj.asString(); }
370 /** \relates PublicKey Detailed stream output */
371 std::ostream & dumpOn( std::ostream & str, const PublicKey & obj );
373 /////////////////////////////////////////////////////////////////
375 ///////////////////////////////////////////////////////////////////
376 #endif // ZYPP_PUBLICKEY_H