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