4011bbade54faec16e38bec96208fb528a38c53d
[shibboleth/cpp-xmltooling.git] / xmltooling / security / CredentialCriteria.h
1 /*
2  *  Copyright 2001-2009 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 xmltooling/security/CredentialCriteria.h
19  * 
20  * Class for specifying criteria by which a CredentialResolver should resolve credentials.
21  */
22
23 #if !defined(__xmltooling_credcrit_h__) && !defined(XMLTOOLING_NO_XMLSEC)
24 #define __xmltooling_credcrit_h__
25
26 #include <xmltooling/XMLToolingConfig.h>
27 #include <xmltooling/security/KeyInfoResolver.h>
28 #include <xmltooling/security/Credential.h>
29 #include <xmltooling/signature/KeyInfo.h>
30 #include <xmltooling/signature/Signature.h>
31
32 #include <set>
33 #include <xsec/dsig/DSIGKeyInfoList.hpp>
34 #include <xsec/dsig/DSIGKeyInfoName.hpp>
35
36 namespace xmltooling {
37
38     /**
39      * Class for specifying criteria by which a CredentialResolver should resolve credentials.
40      */
41     class XMLTOOL_API CredentialCriteria
42     {
43         MAKE_NONCOPYABLE(CredentialCriteria);
44     public:
45         CredentialCriteria() : m_keyUsage(Credential::UNSPECIFIED_CREDENTIAL), m_keySize(0), m_key(NULL),
46             m_keyInfo(NULL), m_nativeKeyInfo(NULL), m_credential(NULL) {
47         }
48         virtual ~CredentialCriteria() {
49             delete m_credential;
50         }
51
52         /**
53          * Determines whether the supplied Credential matches this CredentialCriteria.
54          *
55          * @param credential    the Credential to evaluate
56          * @return true iff the Credential is consistent with this criteria
57          */
58         virtual bool matches(const Credential& credential) const;
59        
60         /**
61          * Get key usage criteria.
62          * 
63          * @return the usage mask
64          */
65         unsigned int getUsage() const {
66             return m_keyUsage;
67         }
68     
69         /**
70          * Set key usage criteria.
71          * 
72          * @param usage the usage mask to set
73          */
74         void setUsage(unsigned int usage) {
75             m_keyUsage = usage;
76         }
77
78         /**
79          * Get the peer name criteria.
80          * 
81          * @return the peer name
82          */
83         const char* getPeerName() const {
84             return m_peerName.c_str();
85         }
86     
87         /**
88          * Set the peer name criteria.
89          * 
90          * @param peerName peer name to set
91          */
92         void setPeerName(const char* peerName) {
93             m_peerName.erase();
94             if (peerName)
95                 m_peerName = peerName;
96         }
97     
98         /**
99          * Get the key algorithm criteria.
100          * 
101          * @return the key algorithm
102          */
103         const char* getKeyAlgorithm() const {
104             return m_keyAlgorithm.c_str();
105         }
106     
107         /**
108          * Set the key algorithm criteria.
109          * 
110          * @param keyAlgorithm The key algorithm to set
111          */
112         void setKeyAlgorithm(const char* keyAlgorithm) {
113             m_keyAlgorithm.erase();
114             if (keyAlgorithm)
115                 m_keyAlgorithm = keyAlgorithm;
116         }
117
118         /**
119          * Get the key size criteria.
120          *
121          * @return  the key size, or 0
122          */
123         unsigned int getKeySize() const {
124             return m_keySize;
125         }
126
127         /**
128          * Set the key size criteria.
129          *
130          * @param keySize Key size to set
131          */
132         void setKeySize(unsigned int keySize) {
133             m_keySize = keySize;
134         }
135     
136         /**
137          * Set the key algorithm and size criteria based on an XML algorithm specifier.
138          *
139          * @param algorithm XML algorithm specifier
140          */
141         void setXMLAlgorithm(const XMLCh* algorithm) {
142             if (algorithm) {
143                 std::pair<const char*,unsigned int> mapped =
144                     XMLToolingConfig::getConfig().mapXMLAlgorithmToKeyAlgorithm(algorithm);
145                 setKeyAlgorithm(mapped.first);
146                 setKeySize(mapped.second);
147             }
148             else {
149                 setKeyAlgorithm(NULL);
150                 setKeySize(0);
151             }
152         }
153
154         /**
155          * Gets key name criteria.
156          * 
157          * @return an immutable set of key names
158          */
159         const std::set<std::string>& getKeyNames() const {
160             return m_keyNames;
161         }
162
163         /**
164          * Gets key name criteria.
165          * 
166          * @return a mutable set of key names
167          */
168         std::set<std::string>& getKeyNames() {
169             return m_keyNames;
170         }
171
172         /**
173          * Returns the public key criteria.
174          * 
175          * @return  a public key
176          */
177         virtual XSECCryptoKey* getPublicKey() const {
178             return m_key;
179         }
180
181         /**
182          * Sets the public key criteria.
183          *
184          * <p>The lifetime of the key <strong>MUST</strong> extend
185          * for the lifetime of this object.
186          * 
187          * @param key a public key
188          */
189         void setPublicKey(XSECCryptoKey* key) {
190             m_key = key;
191         }
192
193         /**
194          * Bitmask constants controlling the kinds of criteria set automatically
195          * based on a KeyInfo object.
196          */
197         enum keyinfo_extraction_t {
198             KEYINFO_EXTRACTION_KEY = 1,
199             KEYINFO_EXTRACTION_KEYNAMES = 2
200         };
201
202         /**
203          * Gets the KeyInfo criteria.
204          * 
205          * @return the KeyInfo criteria
206          */
207         const xmlsignature::KeyInfo* getKeyInfo() const {
208             return m_keyInfo;
209         }
210     
211         /**
212          * Sets the KeyInfo criteria.
213          * 
214          * @param keyInfo       the KeyInfo criteria
215          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
216          */
217         virtual void setKeyInfo(const xmlsignature::KeyInfo* keyInfo, int extraction=0);
218
219         /**
220          * Gets the native KeyInfo criteria.
221          * 
222          * @return the native KeyInfo criteria
223          */
224         DSIGKeyInfoList* getNativeKeyInfo() const {
225             return m_nativeKeyInfo;
226         }
227
228         /**
229          * Sets the KeyInfo criteria.
230          * 
231          * @param keyInfo       the KeyInfo criteria
232          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
233          */
234         virtual void setNativeKeyInfo(DSIGKeyInfoList* keyInfo, int extraction=0);
235
236         /**
237          * Sets the KeyInfo criteria from an XML Signature.
238          * 
239          * @param sig           the Signature containing KeyInfo criteria
240          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
241          */
242         void setSignature(const xmlsignature::Signature& sig, int extraction=0);
243
244     private:
245         unsigned int m_keyUsage;
246         unsigned int m_keySize;
247         std::string m_peerName,m_keyAlgorithm;
248         std::set<std::string> m_keyNames;
249         XSECCryptoKey* m_key;
250         const xmlsignature::KeyInfo* m_keyInfo;
251         DSIGKeyInfoList* m_nativeKeyInfo;
252         Credential* m_credential;
253     };
254 };
255
256 #endif /* __xmltooling_credcrit_h__ */