db630818d024951df98a16f46dc84954839076b2
[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/X509TrustEngine.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 X509TrustEngine
39     {
40     public:
41         ExplicitKeyTrustEngine(const DOMElement* e) : X509TrustEngine(e) {}
42         virtual ~ExplicitKeyTrustEngine() {}
43
44         virtual bool validate(
45             Signature& sig,
46             TrustEngine::KeyInfoIterator& keyInfoSource,
47             const KeyResolver* keyResolver=NULL
48             ) const;
49         virtual bool validate(
50             XSECCryptoX509* certEE,
51             const vector<XSECCryptoX509*>& certChain,
52             TrustEngine::KeyInfoIterator& keyInfoSource,
53             bool checkName=true,
54             const KeyResolver* keyResolver=NULL
55             ) const;
56         virtual bool validate(
57             const XMLCh* sigAlgorithm,
58             const char* sig,
59             KeyInfo* keyInfo,
60             const char* in,
61             unsigned int in_len,
62             KeyInfoIterator& keyInfoSource,
63             const KeyResolver* keyResolver=NULL
64             ) const;
65     };
66
67     TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
68     {
69         return new ExplicitKeyTrustEngine(e);
70     }
71 };
72
73 bool ExplicitKeyTrustEngine::validate(
74     Signature& sig,
75     TrustEngine::KeyInfoIterator& keyInfoSource,
76     const KeyResolver* keyResolver
77     ) const
78 {
79 #ifdef _DEBUG
80     NDC ndc("validate");
81 #endif
82     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
83     
84     if (!keyInfoSource.hasNext()) {
85         log.warn("unable to validate signature, no key information available for peer");
86         return false;
87     }
88     
89     log.debug("attempting to validate signature with the key information for peer");
90     SignatureValidator sigValidator;
91     while (keyInfoSource.hasNext()) {
92         XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());
93         if (key) {
94             log.debug("attempting to validate signature with public key...");
95             try {
96                 sigValidator.setKey(key);   // key now owned by validator
97                 sigValidator.validate(&sig);
98                 log.info("signature validated with public key");
99                 return true;
100             }
101             catch (ValidationException& e) {
102                 if (log.isDebugEnabled()) {
103                     log.debug("public key did not validate signature: %s", e.what());
104                 }
105             }
106         }
107         else {
108             log.debug("key information does not resolve to a public key, skipping it");
109         }
110     }
111
112     log.error("no peer key information validated the signature");
113     return false;
114 }
115
116 bool ExplicitKeyTrustEngine::validate(
117     const XMLCh* sigAlgorithm,
118     const char* sig,
119     KeyInfo* keyInfo,
120     const char* in,
121     unsigned int in_len,
122     KeyInfoIterator& keyInfoSource,
123     const KeyResolver* keyResolver
124     ) const
125 {
126 #ifdef _DEBUG
127     NDC ndc("validate");
128 #endif
129     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
130     
131     if (!keyInfoSource.hasNext()) {
132         log.warn("unable to validate signature, no key information available for peer");
133         return false;
134     }
135     
136     log.debug("attempting to validate signature with the key information for peer");
137     while (keyInfoSource.hasNext()) {
138         auto_ptr<XSECCryptoKey> key((keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next()));
139         if (key.get()) {
140             log.debug("attempting to validate signature with public key...");
141             try {
142                 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
143                     log.info("signature validated with public key");
144                     return true;
145                 }
146             }
147             catch (SignatureException& e) {
148                 if (log.isDebugEnabled()) {
149                     log.debug("public key did not validate signature: %s", e.what());
150                 }
151             }
152         }
153         else {
154             log.debug("key information does not resolve to a public key, skipping it");
155         }
156     }
157
158     log.error("no peer key information validated the signature");
159     return false;
160 }
161
162 bool ExplicitKeyTrustEngine::validate(
163     XSECCryptoX509* certEE,
164     const vector<XSECCryptoX509*>& certChain,
165     TrustEngine::KeyInfoIterator& keyInfoSource,
166     bool checkName,
167     const KeyResolver* keyResolver
168     ) const
169 {
170 #ifdef _DEBUG
171     NDC ndc("validate");
172 #endif
173     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
174     
175     if (!certEE) {
176         log.error("unable to validate, end-entity certificate was null");
177         return false;
178     }
179     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
180         log.error("only the OpenSSL XSEC provider is supported");
181         return false;
182     }
183     else if (!keyInfoSource.hasNext()) {
184         log.warn("unable to validate, no key information available for peer");
185         return false;
186     }
187
188     // The new "basic" trust implementation relies solely on certificates living within the
189     // role interface to verify the EE certificate.
190
191     log.debug("attempting to match key information from peer with end-entity certificate");
192     while (keyInfoSource.hasNext()) {
193         KeyResolver::ResolvedCertificates resolvedCerts;
194         if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {
195             log.debug("key information does not resolve to a certificate, skipping it");
196             continue;
197         }
198
199         log.debug("checking if certificates contained within key information match end-entity certificate");
200         if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
201             log.error("only the OpenSSL XSEC provider is supported");
202             continue;
203         }
204         else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {
205             log.info("end-entity certificate matches certificate from peer key information");
206             return true;
207         }
208     }
209
210     log.debug("no certificates within this peer's key information matched the given end-entity certificate");
211     return false;
212 }