9270b1fcc15bc8c216ae3d070e5890a09d8b346d
[shibboleth/cpp-xmltooling.git] / xmltooling / signature / KeyResolver.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file KeyResolver.h\r
19  * \r
20  * Resolves public keys and certificates based on KeyInfo information or\r
21  * external factors. \r
22  */\r
23 \r
24 #if !defined(__xmltooling_keyres_h__) && !defined(XMLTOOLING_NO_XMLSEC)\r
25 #define __xmltooling_keyres_h__\r
26 \r
27 #include <xmltooling/security/XSECCryptoX509CRL.h>\r
28 \r
29 #include <xsec/dsig/DSIGKeyInfoList.hpp>\r
30 #include <xsec/enc/XSECCryptoKey.hpp>\r
31 #include <xsec/enc/XSECCryptoX509.hpp>\r
32 \r
33 #include <algorithm>\r
34 #include <vector>\r
35 \r
36 namespace xmlsignature {\r
37     class XMLTOOL_API KeyInfo;\r
38     class XMLTOOL_API Signature;\r
39 \r
40     /**\r
41      * An API for resolving keys. The default/simple implementation\r
42      * allows a hard-wired key to be supplied. This is mostly\r
43      * useful for testing, or to adapt another mechanism for supplying\r
44      * keys to this interface.\r
45      */\r
46     class XMLTOOL_API KeyResolver {\r
47         MAKE_NONCOPYABLE(KeyResolver);\r
48     public:\r
49         /**\r
50          * Constructor based on a single externally supplied key.\r
51          * The key will be destroyed when the resolver is. \r
52          * \r
53          * @param key   external key\r
54          */\r
55         KeyResolver(XSECCryptoKey* key=NULL) : m_key(key) {}\r
56         \r
57         virtual ~KeyResolver() {\r
58             delete m_key;\r
59         }\r
60         \r
61         /**\r
62          * Returns a key based on the supplied KeyInfo information.\r
63          * The caller must delete the key when done with it.\r
64          * \r
65          * @param keyInfo   the key information\r
66          * @return  the resolved key\r
67          */\r
68         virtual XSECCryptoKey* resolveKey(const KeyInfo* keyInfo) const {\r
69             return m_key ? m_key->clone() : NULL;\r
70         }\r
71 \r
72         /**\r
73          * Returns a key based on the supplied KeyInfo information.\r
74          * The caller must delete the key when done with it.\r
75          * \r
76          * @param keyInfo   the key information\r
77          * @return  the resolved key\r
78          */\r
79         virtual XSECCryptoKey* resolveKey(DSIGKeyInfoList* keyInfo) const {\r
80             return m_key ? m_key->clone() : NULL;\r
81         }\r
82 \r
83         /**\r
84          * Returns a key based on the supplied KeyInfo information.\r
85          * The caller must delete the key when done with it.\r
86          * \r
87          * @param sig   signature containing the key information\r
88          * @return  the resolved key\r
89          */\r
90         XSECCryptoKey* resolveKey(const Signature* sig) const;\r
91 \r
92         /**\r
93          * A wrapper that handles disposal of certificates when required.\r
94          */\r
95         class XMLTOOL_API ResolvedCertificates {\r
96             MAKE_NONCOPYABLE(ResolvedCertificates);\r
97             bool m_owned;\r
98             std::vector<XSECCryptoX509*> m_certs;\r
99         public:\r
100             ResolvedCertificates() : m_owned(false) {}\r
101             ~ResolvedCertificates() {\r
102                 if (m_owned) {\r
103                     std::for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());\r
104                 }\r
105             }\r
106             const std::vector<XSECCryptoX509*>& v() const {\r
107                 return m_certs;\r
108             }\r
109             friend class XMLTOOL_API KeyResolver;\r
110         };\r
111 \r
112         /**\r
113          * Returns a set of certificates based on the supplied KeyInfo information.\r
114          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
115          * \r
116          * @param keyInfo   the key information\r
117          * @param certs     reference to object to hold certificates\r
118          * @return  number of certificates returned\r
119          */\r
120         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
121             const KeyInfo* keyInfo, ResolvedCertificates& certs\r
122             ) const;\r
123         \r
124         /**\r
125          * Returns a set of certificates based on the supplied KeyInfo information.\r
126          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
127          * \r
128          * @param keyInfo   the key information\r
129          * @param certs     reference to object to hold certificates\r
130          * @return  number of certificates returned\r
131          */\r
132         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
133             DSIGKeyInfoList* keyInfo, ResolvedCertificates& certs \r
134             ) const;\r
135 \r
136         /**\r
137          * Returns a set of certificates based on the supplied KeyInfo information.\r
138          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
139          * \r
140          * @param sig   signature containing the key information\r
141          * @param certs     reference to object to hold certificates\r
142          * @return  number of certificates returned\r
143          */\r
144         std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
145             const Signature* sig, ResolvedCertificates& certs\r
146             ) const;\r
147 \r
148         /**\r
149          * Returns a CRL based on the supplied KeyInfo information.\r
150          * The caller must delete the CRL when done with it.\r
151          * \r
152          * @param keyInfo   the key information\r
153          * @return  the resolved CRL\r
154          */\r
155         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(const KeyInfo* keyInfo) const;\r
156         \r
157         /**\r
158          * Returns a CRL based on the supplied KeyInfo information.\r
159          * The caller must delete the CRL when done with it.\r
160          * \r
161          * @param keyInfo   the key information\r
162          * @return  the resolved CRL\r
163          */\r
164         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(DSIGKeyInfoList* keyInfo) const;\r
165 \r
166         /**\r
167          * Returns a CRL based on the supplied KeyInfo information.\r
168          * The caller must delete the CRL when done with it.\r
169          * \r
170          * @param sig   signature containing the key information\r
171          * @return  the resolved CRL\r
172          */\r
173         xmltooling::XSECCryptoX509CRL* resolveCRL(const Signature* sig) const;\r
174 \r
175     protected:\r
176         XSECCryptoKey* m_key;\r
177 \r
178         /**\r
179          * Accessor for certificate vector from derived KeyResolver classes.\r
180          *\r
181          * @param certs certificate wrapper to access\r
182          * @return modifiable reference to vector inside wrapper\r
183          */\r
184         std::vector<XSECCryptoX509*>& accessCertificates(ResolvedCertificates& certs) const {\r
185             return certs.m_certs;\r
186         }\r
187 \r
188         /**\r
189          * Accessor for certificate ownership flag from derived KeyResolver classes.\r
190          *\r
191          * @param certs certificate wrapper to access\r
192          * @return modifiable reference to ownership flag inside wrapper\r
193          */\r
194         bool& accessOwned(ResolvedCertificates& certs) const {\r
195             return certs.m_owned;\r
196         }\r
197     };\r
198 \r
199     /**\r
200      * Registers KeyResolver classes into the runtime.\r
201      */\r
202     void XMLTOOL_API registerKeyResolvers();\r
203 \r
204     /** KeyResolver based on hard-wired key */\r
205     #define FILESYSTEM_KEY_RESOLVER  "org.opensaml.xmlooling.FilesystemKeyResolver"\r
206 \r
207     /** KeyResolver based on extracting information directly out of a KeyInfo */\r
208     #define INLINE_KEY_RESOLVER  "org.opensaml.xmlooling.InlineKeyResolver"\r
209 };\r
210 \r
211 #endif /* __xmltooling_keyres_h__ */\r