2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * Resolves public keys and certificates based on KeyInfo information or
24 #if !defined(__xmltooling_keyres_h__) && !defined(XMLTOOLING_NO_XMLSEC)
25 #define __xmltooling_keyres_h__
27 #include <xmltooling/security/XSECCryptoX509CRL.h>
29 #include <xsec/dsig/DSIGKeyInfoList.hpp>
30 #include <xsec/enc/XSECCryptoKey.hpp>
31 #include <xsec/enc/XSECCryptoX509.hpp>
36 namespace xmlsignature {
37 class XMLTOOL_API KeyInfo;
38 class XMLTOOL_API Signature;
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.
46 class XMLTOOL_API KeyResolver {
47 MAKE_NONCOPYABLE(KeyResolver);
50 * Constructor based on a single externally supplied key.
51 * The key will be destroyed when the resolver is.
53 * @param key external key
55 KeyResolver(XSECCryptoKey* key=NULL) : m_key(key) {}
57 virtual ~KeyResolver() {
62 * Returns a key based on the supplied KeyInfo information.
63 * The caller must delete the key when done with it.
65 * @param keyInfo the key information
66 * @return the resolved key
68 virtual XSECCryptoKey* resolveKey(const KeyInfo* keyInfo) const {
69 return m_key ? m_key->clone() : NULL;
73 * Returns a key based on the supplied KeyInfo information.
74 * The caller must delete the key when done with it.
76 * @param keyInfo the key information
77 * @return the resolved key
79 virtual XSECCryptoKey* resolveKey(DSIGKeyInfoList* keyInfo) const {
80 return m_key ? m_key->clone() : NULL;
84 * Returns a key based on the supplied KeyInfo information.
85 * The caller must delete the key when done with it.
87 * @param sig signature containing the key information
88 * @return the resolved key
90 XSECCryptoKey* resolveKey(const Signature* sig) const;
93 * A wrapper that handles disposal of certificates when required.
95 class XMLTOOL_API ResolvedCertificates {
96 MAKE_NONCOPYABLE(ResolvedCertificates);
98 std::vector<XSECCryptoX509*> m_certs;
100 ResolvedCertificates() : m_owned(false) {}
102 ~ResolvedCertificates() {
107 * Empties the container and frees any held resources.
111 std::for_each(m_certs.begin(), m_certs.end(), xmltooling::cleanup<XSECCryptoX509>());
118 * Transfers ownership of certificates outside wrapper.
120 * @param writeTo a container into which to move the certificates
121 * @return true iff the certificates must be freed by caller
123 bool release(std::vector<XSECCryptoX509*>& writeTo) {
124 writeTo.assign(m_certs.begin(),m_certs.end());
134 * Accesses the underlying array of certificates.
136 * @return reference to certificate container
138 const std::vector<XSECCryptoX509*>& v() const {
142 friend class XMLTOOL_API KeyResolver;
146 * Returns a set of certificates based on the supplied KeyInfo information.
147 * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.
149 * @param keyInfo the key information
150 * @param certs reference to object to hold certificates
151 * @return number of certificates returned
153 virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(
154 const KeyInfo* keyInfo, ResolvedCertificates& certs
158 * Returns a set of certificates based on the supplied KeyInfo information.
159 * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.
161 * @param keyInfo the key information
162 * @param certs reference to object to hold certificates
163 * @return number of certificates returned
165 virtual std::vector<XSECCryptoX509*>::size_type resolveCertificates(
166 DSIGKeyInfoList* keyInfo, ResolvedCertificates& certs
170 * Returns a set of certificates based on the supplied KeyInfo information.
171 * The certificates must be cloned if kept beyond the lifetime of the KeyInfo source.
173 * @param sig signature containing the key information
174 * @param certs reference to object to hold certificates
175 * @return number of certificates returned
177 std::vector<XSECCryptoX509*>::size_type resolveCertificates(
178 const Signature* sig, ResolvedCertificates& certs
182 * Returns a CRL based on the supplied KeyInfo information.
183 * The caller must delete the CRL when done with it.
185 * @param keyInfo the key information
186 * @return the resolved CRL
188 virtual xmltooling::XSECCryptoX509CRL* resolveCRL(const KeyInfo* keyInfo) const;
191 * Returns a CRL based on the supplied KeyInfo information.
192 * The caller must delete the CRL when done with it.
194 * @param keyInfo the key information
195 * @return the resolved CRL
197 virtual xmltooling::XSECCryptoX509CRL* resolveCRL(DSIGKeyInfoList* keyInfo) const;
200 * Returns a CRL based on the supplied KeyInfo information.
201 * The caller must delete the CRL when done with it.
203 * @param sig signature containing the key information
204 * @return the resolved CRL
206 xmltooling::XSECCryptoX509CRL* resolveCRL(const Signature* sig) const;
209 /** Stores an explicit key. */
210 XSECCryptoKey* m_key;
213 * Accessor for certificate vector from derived KeyResolver classes.
215 * @param certs certificate wrapper to access
216 * @return modifiable reference to vector inside wrapper
218 std::vector<XSECCryptoX509*>& accessCertificates(ResolvedCertificates& certs) const {
219 return certs.m_certs;
223 * Accessor for certificate ownership flag from derived KeyResolver classes.
225 * @param certs certificate wrapper to access
226 * @return modifiable reference to ownership flag inside wrapper
228 bool& accessOwned(ResolvedCertificates& certs) const {
229 return certs.m_owned;
234 * Registers KeyResolver classes into the runtime.
236 void XMLTOOL_API registerKeyResolvers();
238 /** KeyResolver based on hard-wired key */
239 #define FILESYSTEM_KEY_RESOLVER "org.opensaml.xmlooling.FilesystemKeyResolver"
241 /** KeyResolver based on extracting information directly out of a KeyInfo */
242 #define INLINE_KEY_RESOLVER "org.opensaml.xmlooling.InlineKeyResolver"
245 #endif /* __xmltooling_keyres_h__ */