131f9a66ee6c36fc37021bc5260189b098359d20
[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     
74         /**
75          * Set key usage criteria.
76          * 
77          * @param usage the usage mask to set
78          */
79         void setUsage(unsigned int usage);
80
81         /**
82          * Get the peer name criteria.
83          * 
84          * @return the peer name
85          */
86         const char* getPeerName() const;
87     
88         /**
89          * Set the peer name criteria.
90          * 
91          * @param peerName peer name to set
92          */
93         void setPeerName(const char* peerName);
94     
95         /**
96          * Get the key algorithm criteria.
97          * 
98          * @return the key algorithm
99          */
100         const char* getKeyAlgorithm() const;
101     
102         /**
103          * Set the key algorithm criteria.
104          * 
105          * @param keyAlgorithm the key algorithm to set
106          */
107         void setKeyAlgorithm(const char* keyAlgorithm);
108
109         /**
110          * Get the key size criteria.
111          * <p>If a a maximum size is also set, this is treated as a minimum.
112          *
113          * @return  the key size, or 0
114          */
115         unsigned int getKeySize() const;
116
117         /**
118          * Set the key size criteria.
119          * <p>If a a maximum size is also set, this is treated as a minimum.
120          *
121          * @param keySize key size to set
122          */
123         void setKeySize(unsigned int keySize);
124
125         /**
126          * Get the maximum key size criteria.
127          *
128          * @return  the maximum key size, or 0
129          */
130         unsigned int getMaxKeySize() const;
131
132         /**
133          * Set the maximum key size criteria.
134          *
135          * @param keySize maximum key size to set
136          */
137         void setMaxKeySize(unsigned int keySize);
138
139         /**
140          * Set the key algorithm and size criteria based on an XML algorithm specifier.
141          *
142          * @param algorithm XML algorithm specifier
143          */
144         void setXMLAlgorithm(const XMLCh* algorithm);
145
146         /**
147          * Gets key name criteria.
148          * 
149          * @return an immutable set of key names
150          */
151         const std::set<std::string>& getKeyNames() const;
152
153         /**
154          * Gets key name criteria.
155          * 
156          * @return a mutable set of key names
157          */
158         std::set<std::string>& getKeyNames();
159
160         /**
161          * Returns the public key criteria.
162          * 
163          * @return  a public key
164          */
165         virtual XSECCryptoKey* getPublicKey() const;
166
167         /**
168          * Sets the public key criteria.
169          *
170          * <p>The lifetime of the key <strong>MUST</strong> extend
171          * for the lifetime of this object.
172          * 
173          * @param key a public key
174          */
175         void setPublicKey(XSECCryptoKey* key);
176
177         /**
178          * Bitmask constants controlling the kinds of criteria set automatically
179          * based on a KeyInfo object.
180          */
181         enum keyinfo_extraction_t {
182             KEYINFO_EXTRACTION_KEY = 1,
183             KEYINFO_EXTRACTION_KEYNAMES = 2
184         };
185
186         /**
187          * Gets the KeyInfo criteria.
188          * 
189          * @return the KeyInfo criteria
190          */
191         const xmlsignature::KeyInfo* getKeyInfo() const;
192     
193         /**
194          * Sets the KeyInfo criteria.
195          * 
196          * @param keyInfo       the KeyInfo criteria
197          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
198          */
199         virtual void setKeyInfo(const xmlsignature::KeyInfo* keyInfo, int extraction=0);
200
201         /**
202          * Gets the native KeyInfo criteria.
203          * 
204          * @return the native KeyInfo criteria
205          */
206         DSIGKeyInfoList* getNativeKeyInfo() const;
207
208         /**
209          * Sets the KeyInfo criteria.
210          * 
211          * @param keyInfo       the KeyInfo criteria
212          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
213          */
214         virtual void setNativeKeyInfo(DSIGKeyInfoList* keyInfo, int extraction=0);
215
216         /**
217          * Sets the KeyInfo criteria from an XML Signature.
218          * 
219          * @param sig           the Signature containing KeyInfo criteria
220          * @param extraction    bitmask of criteria to auto-extract from KeyInfo
221          */
222         void setSignature(const xmlsignature::Signature& sig, int extraction=0);
223
224         /**
225          * Resets object to a default state.
226          */
227         virtual void reset();
228
229     private:
230         unsigned int m_keyUsage;
231         unsigned int m_keySize,m_maxKeySize;
232         std::string m_peerName,m_keyAlgorithm;
233         std::set<std::string> m_keyNames;
234         XSECCryptoKey* m_key;
235         const xmlsignature::KeyInfo* m_keyInfo;
236         DSIGKeyInfoList* m_nativeKeyInfo;
237         Credential* m_credential;
238     };
239
240 #if defined (_MSC_VER)
241     #pragma warning( pop )
242 #endif
243 };
244
245 #endif /* __xmltooling_credcrit_h__ */