a1ad0cb2a3390bbf154357559187b49eb99f0416
[platform/upstream/libzypp.git] / zypp / KeyRing.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/KeyRing.h
10  *
11 */
12 #ifndef ZYPP_KEYRING_H
13 #define ZYPP_KEYRING_H
14
15 #include <iosfwd>
16 #include <map>
17 #include <list>
18 #include <set>
19 #include <string>
20
21 #include "zypp/base/ReferenceCounted.h"
22 #include "zypp/base/Flags.h"
23 #include "zypp/Callback.h"
24 #include "zypp/base/PtrTypes.h"
25 #include "zypp/Locale.h"
26 #include "zypp/PublicKey.h"
27 #include "zypp/KeyContext.h"
28
29 ///////////////////////////////////////////////////////////////////
30 namespace zypp
31 { /////////////////////////////////////////////////////////////////
32
33   DEFINE_PTR_TYPE(KeyRing);
34
35   /** Callbacks from signature verification workflow.
36    *
37    * Per default all methods answer \c false. This may be canged
38    * by calling \ref KeyRing::setDefaultAccept.
39    * \code
40    *  KeyRing::setDefaultAccept( KeyRing::ACCEPT_UNSIGNED_FILE | KeyRing::ACCEPT_VERIFICATION_FAILED );
41    * \endcode
42    * \see \ref KeyRing
43   */
44   struct KeyRingReport : public callback::ReportBase
45   {
46     /**
47      * User reply options for the askUserToTrustKey callback.
48      *
49      * \param filedes Name of the file (repo alias) or filename if not available
50      */
51     enum KeyTrust
52     {
53       /**
54        * User has chosen not to trust the key.
55        */
56       KEY_DONT_TRUST = 0,
57       /**
58        * This basically means, we knew the key, but it was not trusted. User
59        * has chosen to continue, but not import the key.
60        */
61       KEY_TRUST_TEMPORARILY,
62       /**
63        * Import the key.
64        * This means saving the key in the trusted database so next run it will appear as trusted.
65        * Nothing to do with KEY_TRUST_TEMPORARILY, as you CAN trust a key without importing it,
66        * basically you will be asked every time again.
67        * There are programs who prefer to manage the trust keyring on their own and use trustKey
68        * without importing it into rpm.
69        */
70       KEY_TRUST_AND_IMPORT
71     };
72
73     /**
74      * Ask user to trust and/or import the key to trusted keyring.
75      * \see KeyTrust
76      */
77     virtual KeyTrust askUserToAcceptKey( const PublicKey &key, const KeyContext &keycontext = KeyContext() );
78
79     virtual bool askUserToAcceptUnsignedFile( const std::string &file, const KeyContext &keycontext = KeyContext() );
80
81     /**
82      * we DONT know the key, only its id, but we have never seen it, the difference
83      * with trust key is that if you dont have it, you can't import it later.
84      * The answer means continue yes or no?
85      *
86      */
87     virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id, const KeyContext &keycontext = KeyContext() );
88
89     /**
90      * The file \ref filedesc is signed but the verification failed
91      *
92      * \param filedesc Filename or its description.
93      */
94     virtual bool askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key, const KeyContext &keycontext = KeyContext() );
95
96   };
97
98   struct KeyRingSignals : public callback::ReportBase
99   {
100     virtual void trustedKeyAdded( const PublicKey &/*key*/ )
101     {}
102     virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
103     {}
104   };
105
106   class KeyRingException : public Exception
107    {
108      public:
109        /** Ctor taking message.
110       * Use \ref ZYPP_THROW to throw exceptions.
111         */
112        KeyRingException()
113        : Exception( "Bad Key Exception" )
114        {}
115        /** Ctor taking message.
116         * Use \ref ZYPP_THROW to throw exceptions.
117         */
118        KeyRingException( const std::string & msg_r )
119        : Exception( msg_r )
120        {}
121        /** Dtor. */
122        virtual ~KeyRingException() throw() {};
123    };
124
125   ///////////////////////////////////////////////////////////////////
126   //
127   //    CLASS NAME : KeyRing
128   //
129   /** Gpg key handling.
130    *
131   */
132   class KeyRing : public base::ReferenceCounted, private base::NonCopyable
133   {
134     friend std::ostream & operator<<( std::ostream & str, const KeyRing & obj );
135
136     public:
137       /** \name Default answers in verification workflow.
138        * Per default all answers are \c false.
139        */
140       //@{
141       /** \ref DefaultAccept flags (\see \ref base::Flags) are used to
142        *  define the default callback answers during signature verification.
143        * \code
144        *  KeyRing::setDefaultAccept( KeyRing::ACCEPT_UNSIGNED_FILE | ACCEPT_VERIFICATION_FAILED );
145        * \endcode
146        * \see \ref KeyRingReport.
147        */
148       enum DefaultAcceptBits
149       {
150         ACCEPT_NOTHING             = 0x0000,
151         ACCEPT_UNSIGNED_FILE       = 0x0001,
152         ACCEPT_UNKNOWNKEY          = 0x0002,
153         TRUST_KEY_TEMPORARILY      = 0x0004,
154         TRUST_AND_IMPORT_KEY       = 0x0008,
155         ACCEPT_VERIFICATION_FAILED = 0x0010,
156       };
157       ZYPP_DECLARE_FLAGS( DefaultAccept, DefaultAcceptBits );
158
159       /** Get the active accept bits. */
160       static DefaultAccept defaultAccept();
161
162       /** Set the active accept bits. */
163       static void setDefaultAccept( DefaultAccept value_r );
164      //@}
165
166   public:
167     /** Implementation  */
168     class Impl;
169
170   public:
171     /** Default ctor */
172     KeyRing(const Pathname &baseTmpDir);
173
174     /**
175      * imports a key from a file.
176      * throw if key was not imported
177      */
178     void importKey( const PublicKey &key, bool trusted = false);
179
180     /** Initial import from \ref RpmDb. */
181     void multiKeyImport( const Pathname & keyfile_r, bool trusted_r = false );
182
183     void dumpTrustedPublicKey( const std::string &id, std::ostream &stream )
184     { dumpPublicKey(id, true, stream); }
185
186     void dumpUntrustedPublicKey( const std::string &id, std::ostream &stream )
187     { dumpPublicKey(id, false, stream); }
188
189     void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
190
191     /** Export a public key identified by its key data. */
192     PublicKey exportPublicKey( const PublicKeyData & keyData );
193
194     /** Export a trusted public key identified by its key data. */
195     PublicKey exportTrustedPublicKey( const PublicKeyData & keyData );
196
197     /**
198      * reads the public key id from a signature
199      */
200     std::string readSignatureKeyId( const Pathname &signature );
201
202     /**
203      * true if the key id is trusted
204      */
205     bool isKeyTrusted( const std::string &id );
206
207     /**
208      * true if the key id is knows, that means
209      * at least exist on the untrusted keyring
210      */
211     bool isKeyKnown( const std::string &id );
212
213     /**
214      * removes a key from the keyring.
215      * If trusted is true, Remove it from trusted keyring too.
216      */
217     void deleteKey( const std::string &id, bool trusted =  false );
218
219     /**
220      * Get a list of public keys in the keyring (incl. ASCII armored keys in tmpfiles)
221      */
222     std::list<PublicKey> publicKeys();
223
224     /**
225      * Get a list of trusted public keys in the keyring (incl. ASCII armored keys in tmpfiles)
226      */
227     std::list<PublicKey> trustedPublicKeys();
228
229     /**
230      * Get a list of public key data in the keyring (key data only)
231      */
232     std::list<PublicKeyData> publicKeyData();
233
234     /**
235      * Get a list of trusted public key data in the keyring (key data only)
236      */
237     std::list<PublicKeyData> trustedPublicKeyData();
238
239     /**
240      * Get a list of public key ids in the keyring
241      * \deprecated \ref publicKeyData is cheaper to retrieve and contains more key data
242      */
243     std::list<std::string> publicKeyIds() ZYPP_DEPRECATED;
244
245     /**
246      * Get a list of trusted public key ids in the keyring
247      * \deprecated \ref trustedPublicKeyData is cheaper to retrieve and contains more key data
248      */
249     std::list<std::string> trustedPublicKeyIds() ZYPP_DEPRECATED;
250
251     /**
252      * Follows a signature verification interacting with the user.
253      * The bool returned depends on user decision to trust or not.
254      *
255      * To propagate user decisions, either connect to the \ref KeyRingReport
256      * or use its static methods to set the desired defaults.
257      *
258      * \code
259      * struct KeyRingReportReceive : public callback::ReceiveReport<KeyRingReport>
260      * {
261      *   KeyRingReportReceive() { connect(); }
262      *
263      *   // Overload the virtual methods to return the appropriate values.
264      *   virtual bool askUserToAcceptUnsignedFile( const std::string &file );
265      *   ...
266      * };
267      * \endcode
268      *
269      * \param file Path of the file to be verified
270      * \param filedesc Description of the file (to give the user some context)
271      * \param signature Signature to verify the file against
272      *
273      * \see \ref KeyRingReport
274      */
275     bool verifyFileSignatureWorkflow(
276         const Pathname &file,
277         const std::string filedesc,
278         const Pathname &signature,
279         const KeyContext &keycontext = KeyContext());
280
281
282     /**
283      * Verifies a file against a signature, with no user interaction
284      *
285      * \param file Path of the file to be verified
286      * \param signature Signature to verify the file against
287      */
288     bool verifyFileSignature( const Pathname &file, const Pathname &signature );
289
290     bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature );
291
292     /** Dtor */
293     ~KeyRing();
294
295   private:
296     /** Pointer to implementation */
297     RWCOW_pointer<Impl> _pimpl;
298   };
299   ///////////////////////////////////////////////////////////////////
300
301   /** \relates KeyRing Stream output */
302   inline std::ostream & operator<<( std::ostream & str, const KeyRing & /*obj*/ )
303   {
304     //return str << obj.asString();
305     return str;
306   }
307
308   /** \relates KeyRing::DefaultAccept  */
309   ZYPP_DECLARE_OPERATORS_FOR_FLAGS( KeyRing::DefaultAccept );
310
311   ///////////////////////////////////////////////////////////////////
312
313   namespace target
314   {
315     namespace rpm
316     {
317       /** Internal connection to rpm database. Not for public use. */
318       struct KeyRingSignals : public ::zypp::KeyRingSignals
319       {};
320     }
321   }
322
323  /////////////////////////////////////////////////////////////////
324 } // namespace zypp
325 ///////////////////////////////////////////////////////////////////
326 #endif // ZYPP_KEYRING_H