- Fix reading of Traget::targetDistribution.
[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
28 ///////////////////////////////////////////////////////////////////
29 namespace zypp
30 { /////////////////////////////////////////////////////////////////
31
32   DEFINE_PTR_TYPE(KeyRing);
33
34   /** Callbacks from signature verification workflow.
35    *
36    * Per default all methods answer \c false.
37   */
38   struct KeyRingReport : public callback::ReportBase
39   {
40
41     virtual bool askUserToAcceptUnsignedFile( const std::string &file );
42
43     /**
44      * we DONT know the key, only its id, but we have never seen it, the difference
45      * with trust key is that if you dont have it, you can't import it later.
46      * The answer means continue yes or no?
47      */
48     virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id );
49
50     /**
51      * This basically means, we know the key, but it is not trusted, Continue
52      * yes or no?. Nothing else is performed (import, etc)
53      */
54     virtual bool askUserToTrustKey( const PublicKey &key);
55
56
57     /**
58      * Import the key.
59      * This means saving the key in the trusted database so next run it will appear as trusted.
60      * Nothing to do with trustKey, as you CAN trust a key without importing it,
61      * basically you will be asked every time again.
62      * There are programs who prefer to manage the trust keyring on their own and use trustKey
63      * without importing it into rpm.
64      */
65     virtual bool askUserToImportKey( const PublicKey &key);
66     virtual bool askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key );
67
68     public:
69       /** \name Query/change the default values.
70        * Per default all methods answer \c false.
71        */
72       //@{
73       enum DefaultAcceptBits
74       {
75         ACCEPT_NOTHING             = 0x0000,
76         ACCEPT_UNSIGNED_FILE       = 0x0001,
77         ACCEPT_UNKNOWNKEY          = 0x0002,
78         TRUST_KEY                  = 0x0004,
79         IMPORT_KEY                 = 0x0008,
80         ACCEPT_VERIFICATION_FAILED = 0x0010,
81       };
82       ZYPP_DECLARE_FLAGS(DefaultAccept,DefaultAcceptBits);
83
84       /** Get the active accept bits. */
85       static DefaultAccept defaultAccept();
86       /** Set the active accept bits. */
87       static void setDefaultAccept( DefaultAccept value_r );
88      //@}
89   };
90   ZYPP_DECLARE_OPERATORS_FOR_FLAGS(KeyRingReport::DefaultAccept);
91
92   struct KeyRingSignals : public callback::ReportBase
93   {
94     virtual void trustedKeyAdded( const PublicKey &/*key*/ )
95     {}
96     virtual void trustedKeyRemoved( const PublicKey &/*key*/ )
97     {}
98   };
99
100   class KeyRingException : public Exception
101    {
102      public:
103        /** Ctor taking message.
104       * Use \ref ZYPP_THROW to throw exceptions.
105         */
106        KeyRingException()
107        : Exception( "Bad Key Exception" )
108        {}
109        /** Ctor taking message.
110         * Use \ref ZYPP_THROW to throw exceptions.
111         */
112        KeyRingException( const std::string & msg_r )
113        : Exception( msg_r )
114        {}
115        /** Dtor. */
116        virtual ~KeyRingException() throw() {};
117    };
118
119   ///////////////////////////////////////////////////////////////////
120   //
121   //    CLASS NAME : KeyRing
122   //
123   /** Class that represent a text and multiple translations.
124   */
125   class KeyRing  : public base::ReferenceCounted, private base::NonCopyable
126   {
127     friend std::ostream & operator<<( std::ostream & str, const KeyRing & obj );
128
129   public:
130     /** Implementation  */
131     class Impl;
132
133   public:
134     /** Default ctor */
135     KeyRing(const Pathname &baseTmpDir);
136     //explicit
137     //KeyRing(const Pathname &general_kr, const Pathname &trusted_kr);
138
139     /**
140      * imports a key from a file.
141      * throw if key was not imported
142      */
143     void importKey( const PublicKey &key, bool trusted = false);
144
145     void dumpTrustedPublicKey( const std::string &id, std::ostream &stream )
146     { dumpPublicKey(id, true, stream); }
147
148     void dumpUntrustedPublicKey( const std::string &id, std::ostream &stream )
149     { dumpPublicKey(id, false, stream); }
150
151     void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
152
153     /**
154      * reads the public key id from a signature
155      */
156     std::string readSignatureKeyId( const Pathname &signature );
157
158     /**
159      * true if the key id is trusted
160      */
161     bool isKeyTrusted( const std::string &id);
162
163     /**
164      * true if the key id is knows, that means
165      * at least exist on the untrusted keyring
166      */
167     bool isKeyKnown( const std::string &id );
168
169     /**
170      * removes a key from the keyring.
171      * If trusted is true, Remove it from trusted keyring too.
172      */
173     void deleteKey( const std::string &id, bool trusted =  false);
174
175     /**
176      * Get a list of public keys in the keyring
177      */
178     std::list<PublicKey> publicKeys();
179
180     /**
181      * Get a list of trusted public keys in the keyring
182      */
183     std::list<PublicKey> trustedPublicKeys();
184
185     /**
186      * Get a list of public key ids in the keyring
187      */
188     std::list<std::string> publicKeyIds();
189
190     /**
191      * Get a list of trusted public key ids in the keyring
192      */
193     std::list<std::string> trustedPublicKeyIds();
194
195     /**
196      * Follows a signature verification interacting with the user.
197      * The bool returned depends on user decision to trust or not.
198      *
199      * To propagate user decisions, either connect to the \ref KeyRingReport
200      * or use its static methods to set the desired defaults.
201      *
202      * \code
203      * struct KeyRingReportReceive : public callback::ReceiveReport<KeyRingReport>
204      * {
205      *   KeyRingReportReceive() { connect(); }
206      *
207      *   // Overload the virtual methods to return the appropriate values.
208      *   virtual bool askUserToAcceptUnsignedFile( const std::string &file );
209      *   ...
210      * };
211      * \endcode
212      * \see \ref KeyRingReport
213      */
214     bool verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature);
215     bool verifyFileSignature( const Pathname &file, const Pathname &signature);
216     bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
217
218     /** Dtor */
219     ~KeyRing();
220
221   private:
222     /** Pointer to implementation */
223     RWCOW_pointer<Impl> _pimpl;
224   };
225   ///////////////////////////////////////////////////////////////////
226
227   /** \relates KeyRing Stream output */
228   inline std::ostream & operator<<( std::ostream & str, const KeyRing & /*obj*/ )
229   {
230     //return str << obj.asString();
231     return str;
232   }
233
234   ///////////////////////////////////////////////////////////////////
235
236   namespace target
237   {
238     namespace rpm
239     {
240       /** Internal connection to rpm database. Not for public use. */
241       struct KeyRingSignals : public ::zypp::KeyRingSignals
242       {};
243     }
244   }
245
246  /////////////////////////////////////////////////////////////////
247 } // namespace zypp
248 ///////////////////////////////////////////////////////////////////
249 #endif // ZYPP_KEYRING_H