fix loging an doc
[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     typedef enum
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     KeyTrust;
73
74     /**
75      * Ask user to trust and/or import the key to trusted keyring.
76      * \see KeyTrust
77      */
78     virtual KeyTrust askUserToAcceptKey( const PublicKey &key, const KeyContext &keycontext = KeyContext() );
79
80     virtual bool askUserToAcceptUnsignedFile( const std::string &file, const KeyContext &keycontext = KeyContext() );
81
82     /**
83      * we DONT know the key, only its id, but we have never seen it, the difference
84      * with trust key is that if you dont have it, you can't import it later.
85      * The answer means continue yes or no?
86      *
87      */
88     virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id, const KeyContext &keycontext = KeyContext() );
89
90     /**
91      * The file \ref filedesc is signed but the verification failed
92      *
93      * \param filedesc Filename or its description.
94      */
95     virtual bool askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key, const KeyContext &keycontext = KeyContext() );
96
97   };
98
99   struct KeyRingSignals : public callback::ReportBase
100   {
101     virtual void trustedKeyAdded( const PublicKey &/*key*/ )
102     {}
103     virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
104     {}
105   };
106
107   class KeyRingException : public Exception
108    {
109      public:
110        /** Ctor taking message.
111       * Use \ref ZYPP_THROW to throw exceptions.
112         */
113        KeyRingException()
114        : Exception( "Bad Key Exception" )
115        {}
116        /** Ctor taking message.
117         * Use \ref ZYPP_THROW to throw exceptions.
118         */
119        KeyRingException( const std::string & msg_r )
120        : Exception( msg_r )
121        {}
122        /** Dtor. */
123        virtual ~KeyRingException() throw() {};
124    };
125
126   ///////////////////////////////////////////////////////////////////
127   //
128   //    CLASS NAME : KeyRing
129   //
130   /** Gpg key handling.
131    *
132   */
133   class KeyRing : public base::ReferenceCounted, private base::NonCopyable
134   {
135     friend std::ostream & operator<<( std::ostream & str, const KeyRing & obj );
136
137     public:
138       /** \name Default answers in verification workflow.
139        * Per default all answers are \c false.
140        */
141       //@{
142       /** \ref DefaultAccept flags (\see \ref base::Flags) are used to
143        *  define the default callback answers during signature verification.
144        * \code
145        *  KeyRing::setDefaultAccept( KeyRing::ACCEPT_UNSIGNED_FILE | ACCEPT_VERIFICATION_FAILED );
146        * \endcode
147        * \see \ref KeyRingReport.
148        */
149       enum DefaultAcceptBits
150       {
151         ACCEPT_NOTHING             = 0x0000,
152         ACCEPT_UNSIGNED_FILE       = 0x0001,
153         ACCEPT_UNKNOWNKEY          = 0x0002,
154         TRUST_KEY_TEMPORARILY      = 0x0004,
155         TRUST_AND_IMPORT_KEY       = 0x0008,
156         ACCEPT_VERIFICATION_FAILED = 0x0010,
157       };
158       ZYPP_DECLARE_FLAGS( DefaultAccept, DefaultAcceptBits );
159
160       /** Get the active accept bits. */
161       static DefaultAccept defaultAccept();
162
163       /** Set the active accept bits. */
164       static void setDefaultAccept( DefaultAccept value_r );
165      //@}
166
167   public:
168     /** Implementation  */
169     class Impl;
170
171   public:
172     /** Default ctor */
173     KeyRing(const Pathname &baseTmpDir);
174     //explicit
175     //KeyRing(const Pathname &general_kr, const Pathname &trusted_kr);
176
177     /**
178      * imports a key from a file.
179      * throw if key was not imported
180      */
181     void importKey( const PublicKey &key, bool trusted = 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     /**
192      * reads the public key id from a signature
193      */
194     std::string readSignatureKeyId( const Pathname &signature );
195
196     /**
197      * true if the key id is trusted
198      */
199     bool isKeyTrusted( const std::string &id);
200
201     /**
202      * true if the key id is knows, that means
203      * at least exist on the untrusted keyring
204      */
205     bool isKeyKnown( const std::string &id );
206
207     /**
208      * removes a key from the keyring.
209      * If trusted is true, Remove it from trusted keyring too.
210      */
211     void deleteKey( const std::string &id, bool trusted =  false);
212
213     /**
214      * Get a list of public keys in the keyring
215      */
216     std::list<PublicKey> publicKeys();
217
218     /**
219      * Get a list of trusted public keys in the keyring
220      */
221     std::list<PublicKey> trustedPublicKeys();
222
223     /**
224      * Get a list of public key ids in the keyring
225      */
226     std::list<std::string> publicKeyIds();
227
228     /**
229      * Get a list of trusted public key ids in the keyring
230      */
231     std::list<std::string> trustedPublicKeyIds();
232
233     /**
234      * Follows a signature verification interacting with the user.
235      * The bool returned depends on user decision to trust or not.
236      *
237      * To propagate user decisions, either connect to the \ref KeyRingReport
238      * or use its static methods to set the desired defaults.
239      *
240      * \code
241      * struct KeyRingReportReceive : public callback::ReceiveReport<KeyRingReport>
242      * {
243      *   KeyRingReportReceive() { connect(); }
244      *
245      *   // Overload the virtual methods to return the appropriate values.
246      *   virtual bool askUserToAcceptUnsignedFile( const std::string &file );
247      *   ...
248      * };
249      * \endcode
250      *
251      * \param file Path of the file to be verified
252      * \param filedesc Description of the file (to give the user some context)
253      * \param signature Signature to verify the file against
254      *
255      * \see \ref KeyRingReport
256      */
257     bool verifyFileSignatureWorkflow(
258         const Pathname &file,
259         const std::string filedesc,
260         const Pathname &signature,
261         const KeyContext &keycontext = KeyContext());
262
263
264     /**
265      * Verifies a file against a signature, with no user interaction
266      *
267      * \param file Path of the file to be verified
268      * \param signature Signature to verify the file against
269      */
270     bool verifyFileSignature( const Pathname &file, const Pathname &signature);
271
272     bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
273
274     /** Dtor */
275     ~KeyRing();
276
277   private:
278     /** Pointer to implementation */
279     RWCOW_pointer<Impl> _pimpl;
280   };
281   ///////////////////////////////////////////////////////////////////
282
283   /** \relates KeyRing Stream output */
284   inline std::ostream & operator<<( std::ostream & str, const KeyRing & /*obj*/ )
285   {
286     //return str << obj.asString();
287     return str;
288   }
289
290   /** \relates KeyRing::DefaultAccept  */
291   ZYPP_DECLARE_OPERATORS_FOR_FLAGS( KeyRing::DefaultAccept );
292
293   ///////////////////////////////////////////////////////////////////
294
295   namespace target
296   {
297     namespace rpm
298     {
299       /** Internal connection to rpm database. Not for public use. */
300       struct KeyRingSignals : public ::zypp::KeyRingSignals
301       {};
302     }
303   }
304
305  /////////////////////////////////////////////////////////////////
306 } // namespace zypp
307 ///////////////////////////////////////////////////////////////////
308 #endif // ZYPP_KEYRING_H