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