2 * Copyright 2001-2005 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * ExplicitKeyTrustEngine.cpp
20 * TrustEngine based on explicit knowledge of peer key information.
24 #include "security/OpenSSLTrustEngine.h"
25 #include "signature/SignatureValidator.h"
28 #include <log4cpp/Category.hh>
29 #include <xercesc/util/XMLUniDefs.hpp>
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
32 using namespace xmlsignature;
33 using namespace xmltooling;
34 using namespace log4cpp;
37 namespace xmltooling {
38 class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public OpenSSLTrustEngine
41 ExplicitKeyTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e) {}
42 virtual ~ExplicitKeyTrustEngine() {}
44 virtual bool validate(
46 const KeyInfoSource& keyInfoSource,
47 const KeyResolver* keyResolver=NULL
49 virtual bool validate(
50 const XMLCh* sigAlgorithm,
55 const KeyInfoSource& keyInfoSource,
56 const KeyResolver* keyResolver=NULL
58 virtual bool validate(
59 XSECCryptoX509* certEE,
60 const vector<XSECCryptoX509*>& certChain,
61 const KeyInfoSource& keyInfoSource,
63 const KeyResolver* keyResolver=NULL
65 virtual bool validate(
67 STACK_OF(X509)* certChain,
68 const KeyInfoSource& keyInfoSource,
70 const KeyResolver* keyResolver=NULL
74 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
76 return new ExplicitKeyTrustEngine(e);
80 bool ExplicitKeyTrustEngine::validate(
82 const KeyInfoSource& keyInfoSource,
83 const KeyResolver* keyResolver
89 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
91 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
92 if (!keyInfoIter->hasNext()) {
93 log.warn("unable to validate signature, no key information available for peer");
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());
102 log.debug("attempting to validate signature with public key...");
104 sigValidator.setKey(key); // key now owned by validator
105 sigValidator.validate(&sig);
106 log.info("signature validated with public key");
109 catch (ValidationException& e) {
110 if (log.isDebugEnabled()) {
111 log.debug("public key did not validate signature: %s", e.what());
116 log.debug("key information does not resolve to a public key, skipping it");
120 log.error("no peer key information validated the signature");
124 bool ExplicitKeyTrustEngine::validate(
125 const XMLCh* sigAlgorithm,
130 const KeyInfoSource& keyInfoSource,
131 const KeyResolver* keyResolver
137 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
139 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
140 if (!keyInfoIter->hasNext()) {
141 log.warn("unable to validate signature, no key information available for peer");
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()));
149 log.debug("attempting to validate signature with public key...");
151 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
152 log.info("signature validated with public key");
156 catch (SignatureException& e) {
157 if (log.isDebugEnabled()) {
158 log.debug("public key did not validate signature: %s", e.what());
163 log.debug("key information does not resolve to a public key, skipping it");
167 log.error("no peer key information validated the signature");
171 bool ExplicitKeyTrustEngine::validate(
172 XSECCryptoX509* certEE,
173 const vector<XSECCryptoX509*>& certChain,
174 const KeyInfoSource& keyInfoSource,
176 const KeyResolver* keyResolver
183 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("unable to validate, end-entity certificate was null");
186 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
190 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
194 return validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), NULL, keyInfoSource, checkName, keyResolver);
197 bool ExplicitKeyTrustEngine::validate(
199 STACK_OF(X509)* certChain,
200 const KeyInfoSource& keyInfoSource,
202 const KeyResolver* keyResolver
208 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
211 log.error("unable to validate, end-entity certificate was null");
215 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
216 if (!keyInfoIter->hasNext()) {
217 log.warn("unable to validate, no key information available for peer");
221 // The "basic" trust implementation relies solely on certificates living within the
222 // peer interface to verify the EE certificate.
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");
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");
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");
243 log.debug("no certificates within this peer's key information matched the given end-entity certificate");