Unified trust engines w/ KeyInfoSource interface, first cut at SOAP transport layer.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / ExplicitKeyTrustEngine.cpp
1 /*
2  *  Copyright 2001-2005 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  * ExplicitKeyTrustEngine.cpp
19  * 
20  * TrustEngine based on explicit knowledge of peer key information.
21  */
22
23 #include "internal.h"
24 #include "security/OpenSSLTrustEngine.h"
25 #include "signature/SignatureValidator.h"
26 #include "util/NDC.h"
27
28 #include <log4cpp/Category.hh>
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
31
32 using namespace xmlsignature;
33 using namespace xmltooling;
34 using namespace log4cpp;
35 using namespace std;
36
37 namespace xmltooling {
38     class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public OpenSSLTrustEngine
39     {
40     public:
41         ExplicitKeyTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e) {}
42         virtual ~ExplicitKeyTrustEngine() {}
43
44         virtual bool validate(
45             Signature& sig,
46             const KeyInfoSource& keyInfoSource,
47             const KeyResolver* keyResolver=NULL
48             ) const;
49         virtual bool validate(
50             const XMLCh* sigAlgorithm,
51             const char* sig,
52             KeyInfo* keyInfo,
53             const char* in,
54             unsigned int in_len,
55             const KeyInfoSource& keyInfoSource,
56             const KeyResolver* keyResolver=NULL
57             ) const;
58         virtual bool validate(
59             XSECCryptoX509* certEE,
60             const vector<XSECCryptoX509*>& certChain,
61             const KeyInfoSource& keyInfoSource,
62             bool checkName=true,
63             const KeyResolver* keyResolver=NULL
64             ) const;
65         virtual bool validate(
66             X509* certEE,
67             STACK_OF(X509)* certChain,
68             const KeyInfoSource& keyInfoSource,
69             bool checkName=true,
70             const KeyResolver* keyResolver=NULL
71             ) const;
72     };
73
74     TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
75     {
76         return new ExplicitKeyTrustEngine(e);
77     }
78 };
79
80 bool ExplicitKeyTrustEngine::validate(
81     Signature& sig,
82     const KeyInfoSource& keyInfoSource,
83     const KeyResolver* keyResolver
84     ) const
85 {
86 #ifdef _DEBUG
87     NDC ndc("validate");
88 #endif
89     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
90     
91     auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
92     if (!keyInfoIter->hasNext()) {
93         log.warn("unable to validate signature, no key information available for peer");
94         return false;
95     }
96     
97     log.debug("attempting to validate signature with the key information for peer");
98     SignatureValidator sigValidator;
99     while (keyInfoIter->hasNext()) {
100         XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoIter->next());
101         if (key) {
102             log.debug("attempting to validate signature with public key...");
103             try {
104                 sigValidator.setKey(key);   // key now owned by validator
105                 sigValidator.validate(&sig);
106                 log.info("signature validated with public key");
107                 return true;
108             }
109             catch (ValidationException& e) {
110                 if (log.isDebugEnabled()) {
111                     log.debug("public key did not validate signature: %s", e.what());
112                 }
113             }
114         }
115         else {
116             log.debug("key information does not resolve to a public key, skipping it");
117         }
118     }
119
120     log.error("no peer key information validated the signature");
121     return false;
122 }
123
124 bool ExplicitKeyTrustEngine::validate(
125     const XMLCh* sigAlgorithm,
126     const char* sig,
127     KeyInfo* keyInfo,
128     const char* in,
129     unsigned int in_len,
130     const KeyInfoSource& keyInfoSource,
131     const KeyResolver* keyResolver
132     ) const
133 {
134 #ifdef _DEBUG
135     NDC ndc("validate");
136 #endif
137     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
138     
139     auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
140     if (!keyInfoIter->hasNext()) {
141         log.warn("unable to validate signature, no key information available for peer");
142         return false;
143     }
144     
145     log.debug("attempting to validate signature with the key information for peer");
146     while (keyInfoIter->hasNext()) {
147         auto_ptr<XSECCryptoKey> key((keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoIter->next()));
148         if (key.get()) {
149             log.debug("attempting to validate signature with public key...");
150             try {
151                 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
152                     log.info("signature validated with public key");
153                     return true;
154                 }
155             }
156             catch (SignatureException& e) {
157                 if (log.isDebugEnabled()) {
158                     log.debug("public key did not validate signature: %s", e.what());
159                 }
160             }
161         }
162         else {
163             log.debug("key information does not resolve to a public key, skipping it");
164         }
165     }
166
167     log.error("no peer key information validated the signature");
168     return false;
169 }
170
171 bool ExplicitKeyTrustEngine::validate(
172     XSECCryptoX509* certEE,
173     const vector<XSECCryptoX509*>& certChain,
174     const KeyInfoSource& keyInfoSource,
175     bool checkName,
176     const KeyResolver* keyResolver
177     ) const
178 {
179     if (!certEE) {
180 #ifdef _DEBUG
181         NDC ndc("validate");
182 #endif
183         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("unable to validate, end-entity certificate was null");
184         return false;
185     }
186     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
187 #ifdef _DEBUG
188         NDC ndc("validate");
189 #endif
190         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
191         return false;
192     }
193
194     return validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), NULL, keyInfoSource, checkName, keyResolver);
195 }
196
197 bool ExplicitKeyTrustEngine::validate(
198     X509* certEE,
199     STACK_OF(X509)* certChain,
200     const KeyInfoSource& keyInfoSource,
201     bool checkName,
202     const KeyResolver* keyResolver
203     ) const
204 {
205 #ifdef _DEBUG
206     NDC ndc("validate");
207 #endif
208     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
209     
210     if (!certEE) {
211         log.error("unable to validate, end-entity certificate was null");
212         return false;
213     }
214
215     auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
216     if (!keyInfoIter->hasNext()) {
217         log.warn("unable to validate, no key information available for peer");
218         return false;
219     }
220
221     // The "basic" trust implementation relies solely on certificates living within the
222     // peer interface to verify the EE certificate.
223
224     log.debug("attempting to match key information from peer with end-entity certificate");
225     while (keyInfoIter->hasNext()) {
226         KeyResolver::ResolvedCertificates resolvedCerts;
227         if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoIter->next(),resolvedCerts)) {
228             log.debug("key information does not resolve to a certificate, skipping it");
229             continue;
230         }
231
232         log.debug("checking if certificates contained within key information match end-entity certificate");
233         if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
234             log.error("only the OpenSSL XSEC provider is supported");
235             continue;
236         }
237         else if (!X509_cmp(certEE,static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {
238             log.info("end-entity certificate matches certificate from peer key information");
239             return true;
240         }
241     }
242
243     log.debug("no certificates within this peer's key information matched the given end-entity certificate");
244     return false;
245 }