424382ccc345631053e42805c0bbeaf064f0f7ea
[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 #include <xmltooling/signature/KeyInfo.h>\r
29 \r
30 #include <xsec/dsig/DSIGKeyInfoList.hpp>\r
31 #include <xsec/enc/XSECCryptoKey.hpp>\r
32 #include <xsec/enc/XSECCryptoX509.hpp>\r
33 \r
34 #include <vector>\r
35 \r
36 namespace xmlsignature {\r
37     class XMLTOOL_API Signature;\r
38 \r
39     /**\r
40      * An API for resolving keys. The default/simple implementation\r
41      * allows a hard-wired key to be supplied. This is mostly\r
42      * useful for testing, or to adapt another mechanism for supplying\r
43      * keys to this interface.\r
44      */\r
45     class XMLTOOL_API KeyResolver {\r
46         MAKE_NONCOPYABLE(KeyResolver);\r
47     public:\r
48         /**\r
49          * Constructor based on a single externally supplied key.\r
50          * The key will be destroyed when the resolver is. \r
51          * \r
52          * @param key   external key\r
53          */\r
54         KeyResolver(XSECCryptoKey* key=NULL) : m_key(key) {}\r
55         \r
56         virtual ~KeyResolver() {\r
57             delete m_key;\r
58         }\r
59         \r
60         /**\r
61          * Returns a key based on the supplied KeyInfo information.\r
62          * The caller must delete the key when done with it.\r
63          * \r
64          * @param keyInfo   the key information\r
65          * @return  the resolved key\r
66          */\r
67         virtual XSECCryptoKey* resolveKey(const KeyInfo* keyInfo) const {\r
68             return m_key ? m_key->clone() : NULL;\r
69         }\r
70 \r
71         /**\r
72          * Returns a key based on the supplied KeyInfo information.\r
73          * The caller must delete the key when done with it.\r
74          * \r
75          * @param keyInfo   the key information\r
76          * @return  the resolved key\r
77          */\r
78         virtual XSECCryptoKey* resolveKey(DSIGKeyInfoList* keyInfo) const {\r
79             return m_key ? m_key->clone() : NULL;\r
80         }\r
81 \r
82         /**\r
83          * Returns a key based on the supplied KeyInfo information.\r
84          * The caller must delete the key when done with it.\r
85          * \r
86          * @param sig   signature containing the key information\r
87          * @return  the resolved key\r
88          */\r
89         XSECCryptoKey* resolveKey(const Signature* sig) const;\r
90 \r
91         /**\r
92          * A wrapper that handles disposal of certificates when required.\r
93          */\r
94         class XMLTOOL_API ResolvedCertificates {\r
95             MAKE_NONCOPYABLE(ResolvedCertificates);\r
96             bool m_owned;\r
97             std::vector<XSECCryptoX509*> m_certs;\r
98         public:\r
99             ResolvedCertificates() : m_owned(false) {}\r
100             ~ResolvedCertificates() {\r
101                 if (m_owned) {\r
102                     std::for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());\r
103                 }\r
104             }\r
105             const std::vector<XSECCryptoX509*>& v() const {\r
106                 return m_certs;\r
107             }\r
108             friend class XMLTOOL_API KeyResolver;\r
109         };\r
110 \r
111         /**\r
112          * Returns a set of certificates based on the supplied KeyInfo information.\r
113          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
114          * \r
115          * @param keyInfo   the key information\r
116          * @param certs     reference to object to hold certificates\r
117          * @return  number of certificates returned\r
118          */\r
119         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
120             const KeyInfo* keyInfo, ResolvedCertificates& certs\r
121             ) const;\r
122         \r
123         /**\r
124          * Returns a set of certificates based on the supplied KeyInfo information.\r
125          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
126          * \r
127          * @param keyInfo   the key information\r
128          * @param certs     reference to object to hold certificates\r
129          * @return  number of certificates returned\r
130          */\r
131         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
132             DSIGKeyInfoList* keyInfo, ResolvedCertificates& certs \r
133             ) const;\r
134 \r
135         /**\r
136          * Returns a set of certificates based on the supplied KeyInfo information.\r
137          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
138          * \r
139          * @param sig   signature containing the key information\r
140          * @param certs     reference to object to hold certificates\r
141          * @return  number of certificates returned\r
142          */\r
143         std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
144             const Signature* sig, ResolvedCertificates& certs\r
145             ) const;\r
146 \r
147         /**\r
148          * Returns a CRL based on the supplied KeyInfo information.\r
149          * The caller must delete the CRL when done with it.\r
150          * \r
151          * @param keyInfo   the key information\r
152          * @return  the resolved CRL\r
153          */\r
154         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(const KeyInfo* keyInfo) const;\r
155         \r
156         /**\r
157          * Returns a CRL based on the supplied KeyInfo information.\r
158          * The caller must delete the CRL when done with it.\r
159          * \r
160          * @param keyInfo   the key information\r
161          * @return  the resolved CRL\r
162          */\r
163         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(DSIGKeyInfoList* keyInfo) const;\r
164 \r
165         /**\r
166          * Returns a CRL based on the supplied KeyInfo information.\r
167          * The caller must delete the CRL when done with it.\r
168          * \r
169          * @param sig   signature containing the key information\r
170          * @return  the resolved CRL\r
171          */\r
172         xmltooling::XSECCryptoX509CRL* resolveCRL(const Signature* sig) const;\r
173 \r
174     protected:\r
175         XSECCryptoKey* m_key;\r
176 \r
177         /**\r
178          * Accessor for certificate vector from derived KeyResolver classes.\r
179          *\r
180          * @param certs certificate wrapper to access\r
181          * @return modifiable reference to vector inside wrapper\r
182          */\r
183         std::vector<XSECCryptoX509*>& accessCertificates(ResolvedCertificates& certs) const {\r
184             return certs.m_certs;\r
185         }\r
186 \r
187         /**\r
188          * Accessor for certificate ownership flag from derived KeyResolver classes.\r
189          *\r
190          * @param certs certificate wrapper to access\r
191          * @return modifiable reference to ownership flag inside wrapper\r
192          */\r
193         bool& accessOwned(ResolvedCertificates& certs) const {\r
194             return certs.m_owned;\r
195         }\r
196     };\r
197 \r
198     /**\r
199      * Registers KeyResolver classes into the runtime.\r
200      */\r
201     void XMLTOOL_API registerKeyResolvers();\r
202 \r
203     /** KeyResolver based on hard-wired key */\r
204     #define FILESYSTEM_KEY_RESOLVER  "org.opensaml.xmlooling.FilesystemKeyResolver"\r
205 \r
206     /** KeyResolver based on extracting information directly out of a KeyInfo */\r
207     #define INLINE_KEY_RESOLVER  "org.opensaml.xmlooling.InlineKeyResolver"\r
208 };\r
209 \r
210 #endif /* __xmltooling_keyres_h__ */\r