- Create the cache directly from the schema (installed) file.
[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/Callback.h"
23 #include "zypp/base/PtrTypes.h"
24 #include "zypp/Locale.h"
25 #include "zypp/PublicKey.h"
26
27 ///////////////////////////////////////////////////////////////////
28 namespace zypp
29 { /////////////////////////////////////////////////////////////////
30
31   DEFINE_PTR_TYPE(KeyRing);
32
33   struct KeyRingReport : public callback::ReportBase
34   {
35     
36     virtual bool askUserToAcceptUnsignedFile( const std::string &file );
37     
38     /**
39      * we DONT know the key, only its id, but we have never seen it, the difference
40      * with trust key is that if you dont have it, you can't import it later.
41      * The answer means continue yes or no?
42      */
43     virtual bool askUserToAcceptUnknownKey( const std::string &file, const std::string &id );
44     
45     /**
46      * This basically means, we know the key, but it is not trusted, Continue
47      * yes ir no?. Nothing else is performed (import, etc)
48      */
49     virtual bool askUserToTrustKey( const PublicKey &key);
50     
51     
52     /**
53      * Import the key.
54      * This means saving the key in the trusted database so next run it will appear as trusted.
55      * Nothing to do with trustKey, as you CAN trust a key without importing it, 
56      * basically you will be asked every time again. 
57      * There are programs who prefer to manage the trust keyring on their own and use trustKey 
58      * without importing it into rpm.
59      */
60     virtual bool askUserToImportKey( const PublicKey &key);
61     virtual bool askUserToAcceptVerificationFailed( const std::string &file, const PublicKey &key );
62   };
63   
64   struct KeyRingSignals : public callback::ReportBase
65   {
66     virtual void trustedKeyAdded( const KeyRing &/*keyring*/, const PublicKey &/*key*/ )
67     {}
68     virtual void trustedKeyRemoved( const KeyRing &/*keyring*/, const PublicKey &/*key*/ )
69     {}
70   };
71
72   class KeyRingException : public Exception
73    {
74      public:
75        /** Ctor taking message.
76       * Use \ref ZYPP_THROW to throw exceptions.
77         */
78        KeyRingException()
79        : Exception( "Bad Key Exception" )
80        {}
81        /** Ctor taking message.
82         * Use \ref ZYPP_THROW to throw exceptions.
83         */
84        KeyRingException( const std::string & msg_r )
85        : Exception( msg_r )
86        {}
87        /** Dtor. */
88        virtual ~KeyRingException() throw() {};
89    };
90   
91   ///////////////////////////////////////////////////////////////////
92   //
93   //    CLASS NAME : KeyRing
94   //
95   /** Class that represent a text and multiple translations.
96   */
97   class KeyRing  : public base::ReferenceCounted, private base::NonCopyable
98   {
99     friend std::ostream & operator<<( std::ostream & str, const KeyRing & obj );
100
101   public:
102     /** Implementation  */
103     class Impl;
104
105   public:
106     /** Default ctor */
107     KeyRing(const Pathname &baseTmpDir);
108     //explicit
109     //KeyRing(const Pathname &general_kr, const Pathname &trusted_kr);
110
111     /**
112      * imports a key from a file.
113      * throw if key was not imported
114      */
115     void importKey( const PublicKey &key, bool trusted = false);
116     
117     void dumpTrustedPublicKey( const std::string &id, std::ostream &stream )
118     { dumpPublicKey(id, true, stream); }
119     
120     void dumpUntrustedPublicKey( const std::string &id, std::ostream &stream )
121     { dumpPublicKey(id, false, stream); }
122     
123     void dumpPublicKey( const std::string &id, bool trusted, std::ostream &stream );
124     
125     /**
126      * reads the public key id from a signature
127      */
128     std::string readSignatureKeyId( const Pathname &signature );
129     
130     /**
131      * true if the key id is trusted
132      */
133     bool isKeyTrusted( const std::string &id);
134     
135     /**
136      * true if the key id is knows, that means
137      * at least exist on the untrusted keyring
138      */
139     bool isKeyKnown( const std::string &id );
140     
141     /**
142      * removes a key from the keyring.
143      * If trusted is true, Remove it from trusted keyring too.
144      */
145     void deleteKey( const std::string &id, bool trusted =  false);
146
147     std::list<PublicKey> publicKeys();
148     std::list<PublicKey> trustedPublicKeys();
149
150     /**
151      * Follows a signature verification interacting with the user.
152      * The boolr eturned depends on user desicion to trust or not.
153      */
154     bool verifyFileSignatureWorkflow( const Pathname &file, const std::string filedesc, const Pathname &signature);
155     bool verifyFileSignature( const Pathname &file, const Pathname &signature);
156     bool verifyFileTrustedSignature( const Pathname &file, const Pathname &signature);
157
158 /** Dtor */
159     ~KeyRing();
160
161   public:
162
163     /** Synonym for \ref text */
164     //std::string asString() const
165     //{}
166
167   private:
168     /** Pointer to implementation */
169     RWCOW_pointer<Impl> _pimpl;
170   };
171   ///////////////////////////////////////////////////////////////////
172
173   /** \relates KeyRing Stream output */
174   inline std::ostream & operator<<( std::ostream & str, const KeyRing & /*obj*/ )
175   {
176     //return str << obj.asString();
177     return str;
178   }
179
180   /////////////////////////////////////////////////////////////////
181 } // namespace zypp
182 ///////////////////////////////////////////////////////////////////
183 #endif // ZYPP_KEYRING_H