Address certificate object lifetime with wrapper class.
[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          * A wrapper that handles disposal of certificates when required.\r
83          */\r
84         class XMLTOOL_API ResolvedCertificates {\r
85             MAKE_NONCOPYABLE(ResolvedCertificates);\r
86             bool m_owned;\r
87             std::vector<XSECCryptoX509*> m_certs;\r
88         public:\r
89             ResolvedCertificates() : m_owned(false) {}\r
90             ~ResolvedCertificates() {\r
91                 if (m_owned) {\r
92                     std::for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());\r
93                 }\r
94             }\r
95             const std::vector<XSECCryptoX509*>& v() const {\r
96                 return m_certs;\r
97             }\r
98             friend class XMLTOOL_API KeyResolver;\r
99         };\r
100 \r
101         /**\r
102          * Returns a set of certificates based on the supplied KeyInfo information.\r
103          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
104          * \r
105          * @param keyInfo   the key information\r
106          * @param certs     reference to object to hold certificates\r
107          * @return  number of certificates returned\r
108          */\r
109         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
110             const KeyInfo* keyInfo, ResolvedCertificates& certs\r
111             ) const;\r
112         \r
113         /**\r
114          * Returns a set of certificates based on the supplied KeyInfo information.\r
115          * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.\r
116          * \r
117          * @param keyInfo   the key information\r
118          * @param certs     reference to object to hold certificates\r
119          * @return  number of certificates returned\r
120          */\r
121         virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(\r
122             DSIGKeyInfoList* keyInfo, ResolvedCertificates& certs \r
123             ) const;\r
124 \r
125         /**\r
126          * Returns a CRL based on the supplied KeyInfo information.\r
127          * The caller must delete the CRL when done with it.\r
128          * \r
129          * @param keyInfo   the key information\r
130          * @return  the resolved CRL\r
131          */\r
132         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(const KeyInfo* keyInfo) const;\r
133         \r
134         /**\r
135          * Returns a CRL based on the supplied KeyInfo information.\r
136          * The caller must delete the CRL when done with it.\r
137          * \r
138          * @param keyInfo   the key information\r
139          * @return  the resolved CRL\r
140          */\r
141         virtual xmltooling::XSECCryptoX509CRL* resolveCRL(DSIGKeyInfoList* keyInfo) const;\r
142 \r
143     protected:\r
144         XSECCryptoKey* m_key;\r
145 \r
146         /**\r
147          * Accessor for certificate vector from derived KeyResolver classes.\r
148          *\r
149          * @param certs certificate wrapper to access\r
150          * @return modifiable reference to vector inside wrapper\r
151          */\r
152         std::vector<XSECCryptoX509*>& accessCertificates(ResolvedCertificates& certs) const {\r
153             return certs.m_certs;\r
154         }\r
155 \r
156         /**\r
157          * Accessor for certificate ownership flag from derived KeyResolver classes.\r
158          *\r
159          * @param certs certificate wrapper to access\r
160          * @return modifiable reference to ownership flag inside wrapper\r
161          */\r
162         bool& accessOwned(ResolvedCertificates& certs) const {\r
163             return certs.m_owned;\r
164         }\r
165     };\r
166 \r
167     /**\r
168      * Registers KeyResolver classes into the runtime.\r
169      */\r
170     void XMLTOOL_API registerKeyResolvers();\r
171 \r
172     /** KeyResolver based on hard-wired key */\r
173     #define FILESYSTEM_KEY_RESOLVER  "org.opensaml.xmlooling.FilesystemKeyResolver"\r
174 \r
175     /** KeyResolver based on extracting information directly out of a KeyInfo */\r
176     #define INLINE_KEY_RESOLVER  "org.opensaml.xmlooling.InlineKeyResolver"\r
177 };\r
178 \r
179 #endif /* __xmltooling_keyres_h__ */\r