Add missing class declaration.
[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     /**
41      * Class for specifying criteria by which a CredentialResolver should resolve credentials.
42      */
43     class XMLTOOL_API CredentialCriteria
44     {
45         MAKE_NONCOPYABLE(CredentialCriteria);
46     public:
47         /** Default constructor. */
48         CredentialCriteria();
49
50         virtual ~CredentialCriteria();
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
143         /**
144          * Gets key name criteria.
145          * 
146          * @return an immutable set of key names
147          */
148         const std::set<std::string>& getKeyNames() const {
149             return m_keyNames;
150         }
151
152         /**
153          * Gets key name criteria.
154          * 
155          * @return a mutable set of key names
156          */
157         std::set<std::string>& getKeyNames() {
158             return m_keyNames;
159         }
160
161         /**
162          * Returns the public key criteria.
163          * 
164          * @return  a public key
165          */
166         virtual XSECCryptoKey* getPublicKey() const {
167             return m_key;
168         }
169
170         /**
171          * Sets the public key criteria.
172          *
173          * <p>The lifetime of the key <strong>MUST</strong> extend
174          * for the lifetime of this object.
175          * 
176          * @param key a public key
177          */
178         void setPublicKey(XSECCryptoKey* key) {
179             m_key = key;
180         }
181
182         /**
183          * Bitmask constants controlling the kinds of criteria set automatically
184          * based on a KeyInfo object.
185          */
186         enum keyinfo_extraction_t {
187             KEYINFO_EXTRACTION_KEY = 1,
188             KEYINFO_EXTRACTION_KEYNAMES = 2
189         };
190
191         /**
192          * Gets the KeyInfo criteria.
193          * 
194          * @return the KeyInfo criteria
195          */
196         const xmlsignature::KeyInfo* getKeyInfo() const {
197             return m_keyInfo;
198         }
199     
200         /**
201          * Sets the KeyInfo criteria.
202          * 
203          * @param keyInfo       the KeyInfo criteria
204          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
205          */
206         virtual void setKeyInfo(const xmlsignature::KeyInfo* keyInfo, int extraction=0);
207
208         /**
209          * Gets the native KeyInfo criteria.
210          * 
211          * @return the native KeyInfo criteria
212          */
213         DSIGKeyInfoList* getNativeKeyInfo() const {
214             return m_nativeKeyInfo;
215         }
216
217         /**
218          * Sets the KeyInfo criteria.
219          * 
220          * @param keyInfo       the KeyInfo criteria
221          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
222          */
223         virtual void setNativeKeyInfo(DSIGKeyInfoList* keyInfo, int extraction=0);
224
225         /**
226          * Sets the KeyInfo criteria from an XML Signature.
227          * 
228          * @param sig           the Signature containing KeyInfo criteria
229          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
230          */
231         void setSignature(const xmlsignature::Signature& sig, int extraction=0);
232
233     private:
234         unsigned int m_keyUsage;
235         unsigned int m_keySize;
236         std::string m_peerName,m_keyAlgorithm;
237         std::set<std::string> m_keyNames;
238         XSECCryptoKey* m_key;
239         const xmlsignature::KeyInfo* m_keyInfo;
240         DSIGKeyInfoList* m_nativeKeyInfo;
241         Credential* m_credential;
242     };
243 };
244
245 #endif /* __xmltooling_credcrit_h__ */