2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * ExplicitKeyTrustEngine.cpp
\r
20 * TrustEngine based on explicit knowledge of peer key information.
\r
23 #include "internal.h"
\r
24 #include "security/X509TrustEngine.h"
\r
25 #include "signature/SignatureValidator.h"
\r
26 #include "util/NDC.h"
\r
28 #include <log4cpp/Category.hh>
\r
29 #include <xercesc/util/XMLUniDefs.hpp>
\r
30 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
\r
32 using namespace xmlsignature;
\r
33 using namespace xmltooling;
\r
34 using namespace log4cpp;
\r
35 using namespace std;
\r
37 namespace xmltooling {
\r
38 class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public X509TrustEngine
\r
41 ExplicitKeyTrustEngine(const DOMElement* e) : X509TrustEngine(e) {}
\r
42 virtual ~ExplicitKeyTrustEngine() {}
\r
44 virtual bool validate(
\r
46 TrustEngine::KeyInfoIterator& keyInfoSource,
\r
47 const KeyResolver* keyResolver=NULL
\r
49 virtual bool validate(
\r
50 XSECCryptoX509* certEE,
\r
51 const vector<XSECCryptoX509*>& certChain,
\r
52 TrustEngine::KeyInfoIterator& keyInfoSource,
\r
53 bool checkName=true,
\r
54 const KeyResolver* keyResolver=NULL
\r
60 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
\r
62 return new ExplicitKeyTrustEngine(e);
\r
66 bool ExplicitKeyTrustEngine::validate(
\r
68 TrustEngine::KeyInfoIterator& keyInfoSource,
\r
69 const KeyResolver* keyResolver
\r
73 NDC ndc("validate");
\r
75 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
\r
77 if (!keyInfoSource.hasNext()) {
\r
78 log.warn("unable to validate signature, no key information available for peer");
\r
82 log.debug("attempting to validate signature with the key information for peer");
\r
83 SignatureValidator sigValidator;
\r
84 while (keyInfoSource.hasNext()) {
\r
85 XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoSource.next());
\r
87 log.debug("attempting to validate signature with public key...");
\r
89 sigValidator.setKey(key); // key now owned by validator
\r
90 sigValidator.validate(&sig);
\r
91 log.info("signature validated with public key");
\r
94 catch (ValidationException& e) {
\r
95 if (log.isDebugEnabled()) {
\r
96 log.debug("public key did not validate signature: %s", e.what());
\r
101 log.debug("key information does not resolve to a public key, skipping it");
\r
105 log.error("no peer key information validated the signature");
\r
109 bool ExplicitKeyTrustEngine::validate(
\r
110 XSECCryptoX509* certEE,
\r
111 const vector<XSECCryptoX509*>& certChain,
\r
112 TrustEngine::KeyInfoIterator& keyInfoSource,
\r
114 const KeyResolver* keyResolver
\r
118 NDC ndc("validate");
\r
120 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
\r
123 log.error("unable to validate, end-entity certificate was null");
\r
126 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
\r
127 log.error("only the OpenSSL XSEC provider is supported");
\r
130 else if (!keyInfoSource.hasNext()) {
\r
131 log.warn("unable to validate, no key information available for peer");
\r
135 // The new "basic" trust implementation relies solely on certificates living within the
\r
136 // role interface to verify the EE certificate.
\r
138 log.debug("attempting to match key information from peer with end-entity certificate");
\r
139 while (keyInfoSource.hasNext()) {
\r
140 KeyResolver::ResolvedCertificates resolvedCerts;
\r
141 if (0 == (keyResolver ? keyResolver : m_keyResolver)->resolveCertificates(keyInfoSource.next(),resolvedCerts)) {
\r
142 log.debug("key information does not resolve to a certificate, skipping it");
\r
146 log.debug("checking if certificates contained within key information match end-entity certificate");
\r
147 if (resolvedCerts.v().front()->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
\r
148 log.error("only the OpenSSL XSEC provider is supported");
\r
151 else if (!X509_cmp(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),static_cast<OpenSSLCryptoX509*>(resolvedCerts.v().front())->getOpenSSLX509())) {
\r
152 log.info("end-entity certificate matches certificate from peer key information");
\r
157 log.debug("no certificates within this peer's key information matched the given end-entity certificate");
\r