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