Algorithm and key size criteria, incoming signature algorithm extraction.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / CredentialCriteria.h
1 /*
2  *  Copyright 2001-2007 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/XMLToolingConfig.h>
27 #include <xmltooling/signature/KeyInfo.h>
28 #include <xmltooling/signature/Signature.h>
29
30 #include <string>
31 #include <xsec/dsig/DSIGKeyInfoList.hpp>
32 #include <xsec/dsig/DSIGKeyInfoName.hpp>
33
34 namespace xmltooling {
35
36     /**
37      * Class for specifying criteria by which a CredentialResolver should resolve credentials.
38      */
39     class XMLTOOL_API CredentialCriteria
40     {
41         MAKE_NONCOPYABLE(CredentialCriteria);
42     public:
43         CredentialCriteria() : m_keyUsage(UNSPECIFIED_CREDENTIAL), m_keySize(0), m_keyInfo(NULL), m_nativeKeyInfo(NULL) {}
44         virtual ~CredentialCriteria() {}
45
46         enum UsageType {
47             UNSPECIFIED_CREDENTIAL,
48             SIGNING_CREDENTIAL,
49             TLS_CREDENTIAL,
50             ENCRYPTION_CREDENTIAL
51         };
52         
53         /**
54          * Get the key usage criteria.
55          * 
56          * @return the usage.
57          */
58         UsageType getUsage() const {
59             return m_keyUsage;
60         }
61     
62         /**
63          * Set the key usage criteria.
64          * 
65          * @param usage the usage to set
66          */
67         void setUsage(UsageType usage) {
68             m_keyUsage = usage;
69         }
70
71         /**
72          * Get the peer name criteria.
73          * 
74          * @return the peer name
75          */
76         const char* getPeerName() const {
77             return m_peerName.c_str();
78         }
79     
80         /**
81          * Set the peer name criteria.
82          * 
83          * @param peerName peer name to set
84          */
85         void setPeerName(const char* peerName) {
86             m_peerName.erase();
87             if (peerName)
88                 m_peerName = peerName;
89         }
90     
91         /**
92          * Get the key algorithm criteria.
93          * 
94          * @return the key algorithm
95          */
96         const char* getKeyAlgorithm() const {
97             return m_keyAlgorithm.c_str();
98         }
99     
100         /**
101          * Set the key algorithm criteria.
102          * 
103          * @param keyAlgorithm The key algorithm to set
104          */
105         void setKeyAlgorithm(const char* keyAlgorithm) {
106             m_keyAlgorithm.erase();
107             if (keyAlgorithm)
108                 m_keyAlgorithm = keyAlgorithm;
109         }
110
111         /**
112          * Get the key size criteria.
113          *
114          * @return  the key size, or 0
115          */
116         unsigned int getKeySize() const {
117             return m_keySize;
118         }
119
120         /**
121          * Set the key size criteria.
122          *
123          * @param keySize Key size to set
124          */
125         void setKeySize(unsigned int keySize) {
126             m_keySize = keySize;
127         }
128     
129         /**
130          * Set the key algorithm and size criteria based on an XML algorithm specifier.
131          *
132          * @param algorithm XML algorithm specifier
133          */
134         void setXMLAlgorithm(const XMLCh* algorithm) {
135             if (algorithm) {
136                 std::pair<const char*,unsigned int> mapped =
137                     XMLToolingConfig::getConfig().mapXMLAlgorithmToKeyAlgorithm(algorithm);
138                 setKeyAlgorithm(mapped.first);
139                 setKeySize(mapped.second);
140             }
141             else {
142                 setKeyAlgorithm(NULL);
143                 setKeySize(0);
144             }
145         }
146
147         /**
148          * Get the key name criteria.
149          * 
150          * @return the key name
151          */
152         const char* getKeyName() const {
153             return m_keyName.c_str();
154         }
155     
156         /**
157          * Set the key name criteria.
158          * 
159          * @param keyName key name to set
160          */
161         void setKeyName(const char* keyName) {
162             m_keyName.erase();
163             if (keyName)
164                 m_keyName = keyName;
165         }
166         
167         /**
168          * Gets the KeyInfo criteria.
169          * 
170          * @return the KeyInfo criteria
171          */
172         const xmlsignature::KeyInfo* getKeyInfo() const {
173             return m_keyInfo;
174         }
175     
176         /**
177          * Sets the KeyInfo criteria.
178          * 
179          * @param keyInfo   the KeyInfo criteria
180          */
181         void setKeyInfo(const xmlsignature::KeyInfo* keyInfo) {
182             m_keyInfo = keyInfo;
183         } 
184
185         /**
186          * Gets the native KeyInfo criteria.
187          * 
188          * @return the native KeyInfo criteria
189          */
190         DSIGKeyInfoList* getNativeKeyInfo() const {
191             return m_nativeKeyInfo;
192         }
193
194         /**
195          * Sets the KeyInfo criteria.
196          * 
197          * @param keyInfo   the KeyInfo criteria
198          */
199         void setNativeKeyInfo(DSIGKeyInfoList* keyInfo) {
200             m_nativeKeyInfo = keyInfo;
201         }
202
203         void setSignature(const xmlsignature::Signature& sig) {
204             setXMLAlgorithm(sig.getSignatureAlgorithm());
205             xmlsignature::KeyInfo* k = sig.getKeyInfo();
206             if (k)
207                 return setKeyInfo(k);
208             DSIGSignature* dsig = sig.getXMLSignature();
209             if (dsig)
210                 setNativeKeyInfo(dsig->getKeyInfoList());
211         }
212
213     private:
214         UsageType m_keyUsage;
215         unsigned int m_keySize;
216         std::string m_peerName,m_keyAlgorithm,m_keyName;
217         const xmlsignature::KeyInfo* m_keyInfo;
218         DSIGKeyInfoList* m_nativeKeyInfo;
219     };
220 };
221
222 #endif /* __xmltooling_credcrit_h__ */