2 * Copyright 2001-2007 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/OpenSSLCryptoKeyDSA.hpp>
31 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
32 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
34 using namespace xmlsignature;
35 using namespace xmltooling;
36 using namespace log4cpp;
39 namespace xmltooling {
40 class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public OpenSSLTrustEngine
43 ExplicitKeyTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e) {}
44 virtual ~ExplicitKeyTrustEngine() {}
46 virtual bool validate(
48 const KeyInfoSource& keyInfoSource,
49 const KeyResolver* keyResolver=NULL
51 virtual bool validate(
52 const XMLCh* sigAlgorithm,
57 const KeyInfoSource& keyInfoSource,
58 const KeyResolver* keyResolver=NULL
60 virtual bool validate(
61 XSECCryptoX509* certEE,
62 const vector<XSECCryptoX509*>& certChain,
63 const KeyInfoSource& keyInfoSource,
65 const KeyResolver* keyResolver=NULL
67 virtual bool validate(
69 STACK_OF(X509)* certChain,
70 const KeyInfoSource& keyInfoSource,
72 const KeyResolver* keyResolver=NULL
76 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
78 return new ExplicitKeyTrustEngine(e);
82 bool ExplicitKeyTrustEngine::validate(
84 const KeyInfoSource& keyInfoSource,
85 const KeyResolver* keyResolver
91 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
93 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
94 if (!keyInfoIter->hasNext()) {
95 log.warn("unable to validate signature, no key information available for peer");
99 log.debug("attempting to validate signature with the key information for peer");
100 SignatureValidator sigValidator;
101 while (keyInfoIter->hasNext()) {
102 XSECCryptoKey* key = (keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoIter->next());
104 log.debug("attempting to validate signature with public key...");
106 sigValidator.setKey(key); // key now owned by validator
107 sigValidator.validate(&sig);
108 log.info("signature validated with public key");
111 catch (ValidationException& e) {
112 if (log.isDebugEnabled()) {
113 log.debug("public key did not validate signature: %s", e.what());
118 log.debug("key information does not resolve to a public key, skipping it");
122 log.error("no peer key information validated the signature");
126 bool ExplicitKeyTrustEngine::validate(
127 const XMLCh* sigAlgorithm,
132 const KeyInfoSource& keyInfoSource,
133 const KeyResolver* keyResolver
139 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
141 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
142 if (!keyInfoIter->hasNext()) {
143 log.warn("unable to validate signature, no key information available for peer");
147 log.debug("attempting to validate signature with the key information for peer");
148 while (keyInfoIter->hasNext()) {
149 auto_ptr<XSECCryptoKey> key((keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoIter->next()));
151 log.debug("attempting to validate signature with public key...");
153 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
154 log.info("signature validated with public key");
158 catch (SignatureException& e) {
159 if (log.isDebugEnabled()) {
160 log.debug("public key did not validate signature: %s", e.what());
165 log.debug("key information does not resolve to a public key, skipping it");
169 log.error("no peer key information validated the signature");
173 bool ExplicitKeyTrustEngine::validate(
174 XSECCryptoX509* certEE,
175 const vector<XSECCryptoX509*>& certChain,
176 const KeyInfoSource& keyInfoSource,
178 const KeyResolver* keyResolver
185 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("unable to validate, end-entity certificate was null");
188 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
192 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
196 return validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), NULL, keyInfoSource, checkName, keyResolver);
199 bool ExplicitKeyTrustEngine::validate(
201 STACK_OF(X509)* certChain,
202 const KeyInfoSource& keyInfoSource,
204 const KeyResolver* keyResolver
210 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
213 log.error("unable to validate, end-entity certificate was null");
217 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
218 if (!keyInfoIter->hasNext()) {
219 log.warn("unable to validate, no key information available for peer");
223 // The "explicit" trust implementation relies solely on keys living within the
224 // peer interface to verify the EE certificate.
226 log.debug("attempting to match key information from peer with end-entity certificate");
227 while (keyInfoIter->hasNext()) {
228 auto_ptr<XSECCryptoKey> key((keyResolver ? keyResolver : m_keyResolver)->resolveKey(keyInfoIter->next()));
230 log.debug("checking if peer key matches end-entity certificate");
231 if (key->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
232 log.error("only the OpenSSL XSEC provider is supported");
235 switch (key->getKeyType()) {
236 case XSECCryptoKey::KEY_RSA_PUBLIC:
238 RSA* rsa = static_cast<OpenSSLCryptoKeyRSA*>(key.get())->getOpenSSLRSA();
239 EVP_PKEY* evp = certEE->cert_info->key->pkey;
240 if (rsa && evp && evp->type == EVP_PKEY_RSA &&
241 BN_cmp(rsa->n,evp->pkey.rsa->n) == 0 && BN_cmp(rsa->e,evp->pkey.rsa->e) != 0) {
242 log.info("end-entity certificate matches peer RSA key information");
248 case XSECCryptoKey::KEY_DSA_PUBLIC:
250 DSA* dsa = static_cast<OpenSSLCryptoKeyDSA*>(key.get())->getOpenSSLDSA();
251 EVP_PKEY* evp = certEE->cert_info->key->pkey;
252 if (dsa && evp && evp->type == EVP_PKEY_DSA && BN_cmp(dsa->pub_key,evp->pkey.dsa->pub_key) == 0) {
253 log.info("end-entity certificate matches peer DSA key information");
260 log.warn("unknown peer key type, skipping...");
264 log.debug("key information does not resolve to a public key, skipping it");
268 log.debug("no keys within this peer's key information matched the given end-entity certificate");