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
56 virtual bool validate(
57 const XMLCh* sigAlgorithm,
62 KeyInfoIterator& keyInfoSource,
63 const KeyResolver* keyResolver=NULL
67 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
69 return new ExplicitKeyTrustEngine(e);
73 bool ExplicitKeyTrustEngine::validate(
75 TrustEngine::KeyInfoIterator& keyInfoSource,
76 const KeyResolver* keyResolver
82 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
84 if (!keyInfoSource.hasNext()) {
85 log.warn("unable to validate signature, no key information available for peer");
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());
94 log.debug("attempting to validate signature with public key...");
96 sigValidator.setKey(key); // key now owned by validator
97 sigValidator.validate(&sig);
98 log.info("signature validated with public key");
101 catch (ValidationException& e) {
102 if (log.isDebugEnabled()) {
103 log.debug("public key did not validate signature: %s", e.what());
108 log.debug("key information does not resolve to a public key, skipping it");
112 log.error("no peer key information validated the signature");
116 bool ExplicitKeyTrustEngine::validate(
117 const XMLCh* sigAlgorithm,
122 KeyInfoIterator& keyInfoSource,
123 const KeyResolver* keyResolver
129 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
131 if (!keyInfoSource.hasNext()) {
132 log.warn("unable to validate signature, no key information available for peer");
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()));
140 log.debug("attempting to validate signature with public key...");
142 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
143 log.info("signature validated with public key");
147 catch (SignatureException& e) {
148 if (log.isDebugEnabled()) {
149 log.debug("public key did not validate signature: %s", e.what());
154 log.debug("key information does not resolve to a public key, skipping it");
158 log.error("no peer key information validated the signature");
162 bool ExplicitKeyTrustEngine::validate(
163 XSECCryptoX509* certEE,
164 const vector<XSECCryptoX509*>& certChain,
165 TrustEngine::KeyInfoIterator& keyInfoSource,
167 const KeyResolver* keyResolver
173 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
176 log.error("unable to validate, end-entity certificate was null");
179 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
180 log.error("only the OpenSSL XSEC provider is supported");
183 else if (!keyInfoSource.hasNext()) {
184 log.warn("unable to validate, no key information available for peer");
188 // The new "basic" trust implementation relies solely on certificates living within the
189 // role interface to verify the EE certificate.
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");
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");
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");
210 log.debug("no certificates within this peer's key information matched the given end-entity certificate");