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/X509TrustEngine.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 X509TrustEngine
41 ExplicitKeyTrustEngine(const DOMElement* e) : X509TrustEngine(e) {}
42 virtual ~ExplicitKeyTrustEngine() {}
44 virtual bool validate(
46 TrustEngine::KeyInfoIterator& keyInfoSource,
47 const KeyResolver* keyResolver=NULL
49 virtual bool validate(
50 XSECCryptoX509* certEE,
51 const vector<XSECCryptoX509*>& certChain,
52 TrustEngine::KeyInfoIterator& keyInfoSource,
54 const KeyResolver* keyResolver=NULL
58 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
60 return new ExplicitKeyTrustEngine(e);
64 bool ExplicitKeyTrustEngine::validate(
66 TrustEngine::KeyInfoIterator& keyInfoSource,
67 const KeyResolver* keyResolver
73 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
75 if (!keyInfoSource.hasNext()) {
76 log.warn("unable to validate signature, no key information available for peer");
80 log.debug("attempting to validate signature with the key information for peer");
81 SignatureValidator sigValidator;
82 while (keyInfoSource.hasNext()) {
83 XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());
85 log.debug("attempting to validate signature with public key...");
87 sigValidator.setKey(key); // key now owned by validator
88 sigValidator.validate(&sig);
89 log.info("signature validated with public key");
92 catch (ValidationException& e) {
93 if (log.isDebugEnabled()) {
94 log.debug("public key did not validate signature: %s", e.what());
99 log.debug("key information does not resolve to a public key, skipping it");
103 log.error("no peer key information validated the signature");
107 bool ExplicitKeyTrustEngine::validate(
108 XSECCryptoX509* certEE,
109 const vector<XSECCryptoX509*>& certChain,
110 TrustEngine::KeyInfoIterator& keyInfoSource,
112 const KeyResolver* keyResolver
118 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
121 log.error("unable to validate, end-entity certificate was null");
124 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
125 log.error("only the OpenSSL XSEC provider is supported");
128 else if (!keyInfoSource.hasNext()) {
129 log.warn("unable to validate, no key information available for peer");
133 // The new "basic" trust implementation relies solely on certificates living within the
134 // role interface to verify the EE certificate.
136 log.debug("attempting to match key information from peer with end-entity certificate");
137 while (keyInfoSource.hasNext()) {
138 KeyResolver::ResolvedCertificates resolvedCerts;
139 if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {
140 log.debug("key information does not resolve to a certificate, skipping it");
144 log.debug("checking if certificates contained within key information match end-entity certificate");
145 if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
146 log.error("only the OpenSSL XSEC provider is supported");
149 else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {
150 log.info("end-entity certificate matches certificate from peer key information");
155 log.debug("no certificates within this peer's key information matched the given end-entity certificate");