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/Pathname.h"
25 #include "zypp/Date.h"
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////
38 /// \class BadKeyException
39 /// \brief Exception thrown when the supplied key is not a valid gpg key
40 ///////////////////////////////////////////////////////////////////
41 class BadKeyException : public Exception
44 /** Ctor taking message.
45 * Use \ref ZYPP_THROW to throw exceptions.
48 : Exception( "Bad Key Exception" )
51 Pathname keyFile() const
54 /** Ctor taking message.
55 * Use \ref ZYPP_THROW to throw exceptions.
57 BadKeyException( const std::string & msg_r, const Pathname &keyfile = Pathname() )
58 : Exception( msg_r ), _keyfile(keyfile)
61 virtual ~BadKeyException() throw() {};
65 ///////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////
68 /// \class PublicSubkeyData
69 /// \brief Class representing a GPG Public Keys subkeys.
70 /// \see \ref PublicKeyData.
71 ///////////////////////////////////////////////////////////////////
72 class PublicSubkeyData
75 /** Default constructed: empty data. */
80 /** Whether this contains valid data (not default constructed). */
81 explicit operator bool() const;
85 std::string id() const;
90 /** Expiry date, or \c Date() if the key never expires. */
93 /** Whether the key has expired. */
96 /** Number of days (24h) until the key expires (or since it exired).
97 * A value of \c 0 means the key will expire within the next 24h.
98 * Negative values indicate the key has expired less than \c N days ago.
99 * For keys without expiration date \c INT_MAX is returned.
101 int daysToLive() const;
103 /** Simple string representation.
104 * Encodes \ref id, \ref created and \ref expires
106 * 640DB551 2016-04-12 [expires: 2019-04-12]
109 std::string asString() const;
113 RWCOW_pointer<Impl> _pimpl;
114 friend class PublicKeyScanner;
115 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
117 ///////////////////////////////////////////////////////////////////
119 /** \relates PublicSubkeyData Stream output */
120 inline std::ostream & operator<<( std::ostream & str, const PublicSubkeyData & obj )
121 { return str << obj.asString(); }
123 ///////////////////////////////////////////////////////////////////
124 /// \class PublicKeyData
125 /// \brief Class representing one GPG Public Keys data.
126 /// \ref PublicKeyData are provided e.g. by a \ref PublicKey or
127 /// a \ref KeyRing. \ref PublicKeyData are usually easier to
128 /// retrieve and sufficient unless you actually need an ASCII
129 /// armored version of the key placed in a tempfile. In this
130 /// case use \ref PublicKey.
131 ///////////////////////////////////////////////////////////////////
135 /** Default constructed: empty data. */
140 /** Whether this contains valid data (not default constructed). */
141 explicit operator bool() const;
145 std::string id() const;
148 std::string name() const;
150 /** Key fingerprint.*/
151 std::string fingerprint() const;
153 /** Creation / last modification date (latest selfsig). */
154 Date created() const;
156 /** Expiry date, or \c Date() if the key never expires. */
157 Date expires() const;
159 /** Whether the key has expired. */
160 bool expired() const;
162 /** Number of days (24h) until the key expires (or since it exired).
163 * A value of \c 0 means the key will expire within the next 24h.
164 * Negative values indicate the key has expired less than \c N days ago.
165 * For keys without expiration date \c INT_MAX is returned.
167 int daysToLive() const;
169 /** * Expiry info in a human readable form.
170 * The exipry daye plus an annotation if the key has expired, or will
171 * expire within 90 days.
174 * Tue May 11 13:37:33 CEST 2010
175 * Tue May 11 13:37:33 CEST 2010 (expires in 90 days)
176 * Tue May 11 13:37:33 CEST 2010 (expires in 1 day)
177 * Tue May 11 13:37:33 CEST 2010 (expires within 24h)
178 * Tue May 11 13:37:33 CEST 2010 (EXPIRED)
181 std::string expiresAsString() const;
183 /** Gpg-pubkey version as computed by rpm (trailing 8 byte \ref id) */
184 std::string gpgPubkeyVersion() const;
186 /** Gpg-pubkey release as computed by rpm (hexencoded \ref created) */
187 std::string gpgPubkeyRelease() const;
189 /** Simple string representation.
190 * Encodes \ref id, \ref gpgPubkeyRelease, \ref name and \ref fingerprint.
192 * [E3A5C360307E3D54-4be01a65] [SuSE Package Signing Key <build@suse.de>] [4E98E67519D98DC7362A5990E3A5C360307E3D54]
195 std::string asString() const;
198 typedef const PublicSubkeyData * SubkeyIterator;
200 /** Whether \ref subkeys is not empty. */
201 bool hasSubkeys() const;
203 /** Iterate any subkeys. */
204 Iterable<SubkeyIterator> subkeys() const;
206 /** Whether \a id_r is the \ref id of the primary key or of a subkey. */
207 bool providesKey( const std::string & id_r ) const;
211 RWCOW_pointer<Impl> _pimpl;
212 friend class PublicKeyScanner;
213 friend std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
215 ///////////////////////////////////////////////////////////////////
217 /** \relates PublicKeyData Stream output */
218 inline std::ostream & operator<<( std::ostream & str, const PublicKeyData & obj )
219 { return str << obj.asString(); }
221 /** \relates PublicKeyData Detailed stream output */
222 std::ostream & dumpOn( std::ostream & str, const PublicKeyData & obj );
224 /** \relates PublicKeyData Equal based on fingerprint anf creation date. */
225 bool operator==( const PublicKeyData & lhs, const PublicKeyData & rhs );
227 /** \relates PublicKeyData NotEqual. */
228 inline bool operator!=( const PublicKeyData & lhs, const PublicKeyData & rhs )
229 { return !( lhs == rhs ); }
231 ///////////////////////////////////////////////////////////////////
232 /// \class PublicKeyScanner
233 /// \brief Scan abstract from 'gpg --with-colons' key listings.
234 /// Feed gpg output line by line into \ref scan. The collected \ref PublicKeyData
235 /// contain the keys data (fingerprint, uid,...) but not the key itself (ASCII
236 /// armored stored in a file).
238 /// std::list<PublicKeyData> result;
240 /// PublicKeyScanner scanner;
241 /// for ( std::string line = prog.receiveLine(); !line.empty(); line = prog.receiveLine() )
242 /// scanner.scan( line );
243 /// result.swap( scanner._keys );
246 /// \relates PublicKeyData
247 ///////////////////////////////////////////////////////////////////
248 struct PublicKeyScanner
253 /** Feed gpg output line by line into \ref scan. */
254 void scan( std::string line_r );
256 /** Extracted keys. */
257 std::list<PublicKeyData> _keys;
261 RW_pointer<Impl, rw_pointer::Scoped<Impl> > _pimpl;
263 ///////////////////////////////////////////////////////////////////
266 ///////////////////////////////////////////////////////////////////
268 /// \brief Class representing one GPG Public Key (PublicKeyData + ASCII armored in a tempfile).
270 /// If you don't need the ASCII armored version of the key stored in
271 /// a tempfile, using \ref PublicKeyData might be sufficient.
273 /// \note In case the ASCII armored blob actually contains multiple
274 /// keys, the \b last keys data are made available via the API. The
275 /// additional keys data are made available via \ref hiddenKeys.
276 ///////////////////////////////////////////////////////////////////
280 /** Implementation */
287 /** Ctor taking the key from a file.
289 * This is quite expensive, as a copy of the file is created and
290 * used. If you can construct PublicKey from a \ref filesystem::TmpFile,
291 * this prevents copying.
293 * \throws when data does not make a key
295 explicit PublicKey( const Pathname & keyFile_r );
297 /** Ctor reading the key from a \ref TmpFile.
299 * PublicKey holds a reference on the TmpFile providing the key.
301 * \throws when data does not make a key
303 explicit PublicKey( const filesystem::TmpFile & sharedFile_r );
308 /** The public keys data (\see \ref PublicKeyData).*/
309 const PublicKeyData & keyData() const;
311 typedef PublicKeyData::SubkeyIterator SubkeyIterator;
314 { return ! ( id().empty() || fingerprint().empty() ); }
316 std::string id() const; //!< \see \ref PublicKeyData
317 std::string name() const; //!< \see \ref PublicKeyData
318 std::string fingerprint() const; //!< \see \ref PublicKeyData
319 Date created() const; //!< \see \ref PublicKeyData
320 Date expires() const; //!< \see \ref PublicKeyData
321 std::string expiresAsString() const; //!< \see \ref PublicKeyData
322 bool expired() const; //!< \see \ref PublicKeyData
323 int daysToLive() const; //!< \see \ref PublicKeyData
324 std::string gpgPubkeyVersion() const; //!< \see \ref PublicKeyData
325 std::string gpgPubkeyRelease() const; //!< \see \ref PublicKeyData
326 std::string asString() const; //!< \see \ref PublicKeyData
328 bool hasSubkeys() const ///!< \see \ref PublicKeyData
329 { return keyData().hasSubkeys(); }
331 Iterable<SubkeyIterator> subkeys() const ///!< \see \ref PublicKeyData
332 { return keyData().subkeys(); }
334 bool providesKey( const std::string & id_r ) const ///!< \see \ref PublicKeyData
335 { return keyData().providesKey( id_r ); }
338 /** File containig the ASCII armored key. */
339 Pathname path() const;
341 /** Additional keys data in case the ASCII armored blob containes multiple keys. */
342 const std::list<PublicKeyData> & hiddenKeys() const;
345 bool operator==( const PublicKey & rhs ) const;
346 bool operator==( const std::string & sid ) const;
349 friend class KeyRing;
350 /** KeyRing ctor: No need to parse file if KeyRing already had valid KeyData. */
351 PublicKey( const filesystem::TmpFile & sharedFile_r, const PublicKeyData & keyData_r );
354 /** Pointer to implementation */
355 RWCOW_pointer<Impl> _pimpl;
357 ///////////////////////////////////////////////////////////////////
359 /** \relates PublicKey Stream output */
360 inline std::ostream & operator<<( std::ostream & str, const PublicKey & obj )
361 { return str << obj.asString(); }
363 /** \relates PublicKey Detailed stream output */
364 std::ostream & dumpOn( std::ostream & str, const PublicKey & obj );
366 /////////////////////////////////////////////////////////////////
368 ///////////////////////////////////////////////////////////////////
369 #endif // ZYPP_PUBLICKEY_H