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