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