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