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