b75ab2be4893347bbf7bf8a5e0672baeecefee14
[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 \r
38     /**\r
39      * An API for resolving keys. The default/simple implementation\r
40      * allows a hard-wired key to be supplied. This is mostly\r
41      * useful for testing, or to adapt another mechanism for supplying\r
42      * keys to this interface.\r
43      */\r
44     class XMLTOOL_API KeyResolver {\r
45         MAKE_NONCOPYABLE(KeyResolver);\r
46     public:\r
47         /**\r
48          * Constructor based on a single externally supplied key.\r
49          * The key will be destroyed when the resolver is. \r
50          * \r
51          * @param key   external key\r
52          */\r
53         KeyResolver(XSECCryptoKey* key=NULL) : m_key(key) {}\r
54         \r
55         virtual ~KeyResolver() {\r
56             delete m_key;\r
57         }\r
58         \r
59         /**\r
60          * Returns a key based on the supplied KeyInfo information.\r
61          * The caller must delete the key when done with it.\r
62          * \r
63          * @param keyInfo   the key information\r
64          * @return  the resolved key\r
65          */\r
66         virtual XSECCryptoKey* resolveKey(const KeyInfo* keyInfo) const {\r
67             return m_key ? m_key->clone() : NULL;\r
68         }\r
69 \r
70         /**\r
71          * Returns a key based on the supplied KeyInfo information.\r
72          * The caller must delete the key when done with it.\r
73          * \r
74          * @param keyInfo   the key information\r
75          * @return  the resolved key\r
76          */\r
77         virtual XSECCryptoKey* resolveKey(DSIGKeyInfoList* keyInfo) const {\r
78             return m_key ? m_key->clone() : NULL;\r
79         }\r
80 \r
81         /**\r
82          * Returns a set of certificates based on the supplied KeyInfo information.\r
83          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
84          * \r
85          * @param keyInfo   the key information\r
86          * @param certs     reference to vector to store certificates\r
87          * @return  number of certificates returned\r
88          */\r
89         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
90             const KeyInfo* keyInfo, std::vector<XSECCryptoX509*>& certs\r
91             ) const;\r
92         \r
93         /**\r
94          * Returns a set of certificates based on the supplied KeyInfo information.\r
95          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
96          * \r
97          * @param keyInfo   the key information\r
98          * @param certs     reference to vector to store certificates\r
99          * @return  number of certificates returned\r
100          */\r
101         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
102             DSIGKeyInfoList* keyInfo, std::vector<XSECCryptoX509*>& certs \r
103             ) const;\r
104 \r
105         /**\r
106          * Returns a CRL based on the supplied KeyInfo information.\r
107          * The caller must delete the CRL when done with it.\r
108          * \r
109          * @param keyInfo   the key information\r
110          * @return  the resolved CRL\r
111          */\r
112         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(const KeyInfo* keyInfo) const;\r
113         \r
114         /**\r
115          * Returns a CRL based on the supplied KeyInfo information.\r
116          * The caller must delete the CRL when done with it.\r
117          * \r
118          * @param keyInfo   the key information\r
119          * @return  the resolved CRL\r
120          */\r
121         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(DSIGKeyInfoList* keyInfo) const;\r
122 \r
123     protected:\r
124         XSECCryptoKey* m_key;\r
125     };\r
126 \r
127     /**\r
128      * Registers KeyResolver classes into the runtime.\r
129      */\r
130     void XMLTOOL_API registerKeyResolvers();\r
131 \r
132     /** KeyResolver based on hard-wired key */\r
133     #define FILESYSTEM_KEY_RESOLVER  "org.opensaml.xmlooling.FilesystemKeyResolver"\r
134 \r
135     /** KeyResolver based on extracting information directly out of a KeyInfo */\r
136     #define INLINE_KEY_RESOLVER  "org.opensaml.xmlooling.InlineKeyResolver"\r
137 };\r
138 \r
139 #endif /* __xmltooling_keyres_h__ */\r