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.
25 #include "security/Credential.h"
26 #include "security/CredentialCriteria.h"
27 #include "security/CredentialResolver.h"
28 #include "security/OpenSSLTrustEngine.h"
29 #include "security/SignatureTrustEngine.h"
30 #include "signature/SignatureValidator.h"
33 #include <xercesc/util/XMLUniDefs.hpp>
34 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyDSA.hpp>
35 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
36 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
38 using namespace xmlsignature;
39 using namespace xmltooling::logging;
40 using namespace xmltooling;
43 using xercesc::DOMElement;
45 namespace xmltooling {
46 class XMLTOOL_DLLLOCAL ExplicitKeyTrustEngine : public SignatureTrustEngine, public OpenSSLTrustEngine
49 ExplicitKeyTrustEngine(const DOMElement* e) : TrustEngine(e) {}
50 virtual ~ExplicitKeyTrustEngine() {}
52 virtual bool validate(
54 const CredentialResolver& credResolver,
55 CredentialCriteria* criteria=NULL
57 virtual bool validate(
58 const XMLCh* sigAlgorithm,
63 const CredentialResolver& credResolver,
64 CredentialCriteria* criteria=NULL
66 virtual bool validate(
67 XSECCryptoX509* certEE,
68 const vector<XSECCryptoX509*>& certChain,
69 const CredentialResolver& credResolver,
70 CredentialCriteria* criteria=NULL
72 virtual bool validate(
74 STACK_OF(X509)* certChain,
75 const CredentialResolver& credResolver,
76 CredentialCriteria* criteria=NULL
80 TrustEngine* XMLTOOL_DLLLOCAL ExplicitKeyTrustEngineFactory(const DOMElement* const & e)
82 return new ExplicitKeyTrustEngine(e);
86 bool ExplicitKeyTrustEngine::validate(
88 const CredentialResolver& credResolver,
89 CredentialCriteria* criteria
95 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."EXPLICIT_KEY_TRUSTENGINE);
97 vector<const Credential*> credentials;
99 criteria->setUsage(Credential::SIGNING_CREDENTIAL);
100 criteria->setSignature(sig, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
101 credResolver.resolve(credentials,criteria);
104 CredentialCriteria cc;
105 cc.setUsage(Credential::SIGNING_CREDENTIAL);
106 cc.setSignature(sig, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
107 credResolver.resolve(credentials,&cc);
109 if (credentials.empty()) {
110 log.debug("unable to validate signature, no credentials available from peer");
114 log.debug("attempting to validate signature with the peer's credentials");
115 SignatureValidator sigValidator;
116 for (vector<const Credential*>::const_iterator c=credentials.begin(); c!=credentials.end(); ++c) {
117 sigValidator.setCredential(*c);
119 sigValidator.validate(&sig);
120 log.debug("signature validated with credential");
123 catch (ValidationException& e) {
124 log.debug("public key did not validate signature: %s", e.what());
128 log.debug("no peer credentials validated the signature");
132 bool ExplicitKeyTrustEngine::validate(
133 const XMLCh* sigAlgorithm,
138 const CredentialResolver& credResolver,
139 CredentialCriteria* criteria
145 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."EXPLICIT_KEY_TRUSTENGINE);
147 vector<const Credential*> credentials;
149 criteria->setUsage(Credential::SIGNING_CREDENTIAL);
150 criteria->setKeyInfo(keyInfo, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
151 criteria->setXMLAlgorithm(sigAlgorithm);
152 credResolver.resolve(credentials,criteria);
155 CredentialCriteria cc;
156 cc.setUsage(Credential::SIGNING_CREDENTIAL);
157 cc.setKeyInfo(keyInfo, CredentialCriteria::KEYINFO_EXTRACTION_KEY);
158 cc.setXMLAlgorithm(sigAlgorithm);
159 credResolver.resolve(credentials,&cc);
161 if (credentials.empty()) {
162 log.debug("unable to validate signature, no credentials available from peer");
166 log.debug("attempting to validate signature with the peer's credentials");
167 for (vector<const Credential*>::const_iterator c=credentials.begin(); c!=credentials.end(); ++c) {
168 if ((*c)->getPublicKey()) {
170 if (Signature::verifyRawSignature((*c)->getPublicKey(), sigAlgorithm, sig, in, in_len)) {
171 log.debug("signature validated with public key");
175 catch (SignatureException& e) {
176 if (log.isDebugEnabled()) {
177 log.debug("public key did not validate signature: %s", e.what());
183 log.debug("no peer credentials validated the signature");
187 bool ExplicitKeyTrustEngine::validate(
188 XSECCryptoX509* certEE,
189 const vector<XSECCryptoX509*>& certChain,
190 const CredentialResolver& credResolver,
191 CredentialCriteria* criteria
198 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."EXPLICIT_KEY_TRUSTENGINE).error("unable to validate, end-entity certificate was null");
201 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
202 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."EXPLICIT_KEY_TRUSTENGINE).error("only the OpenSSL XSEC provider is supported");
206 return validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), NULL, credResolver, criteria);
209 bool ExplicitKeyTrustEngine::validate(
211 STACK_OF(X509)* certChain,
212 const CredentialResolver& credResolver,
213 CredentialCriteria* criteria
219 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine."EXPLICIT_KEY_TRUSTENGINE);
222 log.error("unable to validate, end-entity certificate was null");
226 vector<const Credential*> credentials;
228 if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
229 criteria->setUsage(Credential::SIGNING_CREDENTIAL);
230 credResolver.resolve(credentials,criteria);
233 CredentialCriteria cc;
234 cc.setUsage(Credential::SIGNING_CREDENTIAL);
235 credResolver.resolve(credentials,&cc);
237 if (credentials.empty()) {
238 log.debug("unable to validate certificate, no credentials available from peer");
242 // The "explicit" trust implementation relies solely on keys living within the
243 // peer resolver to verify the EE certificate.
245 log.debug("attempting to match credentials from peer with end-entity certificate");
246 for (vector<const Credential*>::const_iterator c=credentials.begin(); c!=credentials.end(); ++c) {
247 XSECCryptoKey* key = (*c)->getPublicKey();
249 if (key->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
250 log.error("only the OpenSSL XSEC provider is supported");
253 switch (key->getKeyType()) {
254 case XSECCryptoKey::KEY_RSA_PUBLIC:
256 RSA* rsa = static_cast<OpenSSLCryptoKeyRSA*>(key)->getOpenSSLRSA();
257 EVP_PKEY* evp = X509_PUBKEY_get(X509_get_X509_PUBKEY(certEE));
258 if (rsa && evp && evp->type == EVP_PKEY_RSA &&
259 BN_cmp(rsa->n,evp->pkey.rsa->n) == 0 && BN_cmp(rsa->e,evp->pkey.rsa->e) == 0) {
262 log.debug("end-entity certificate matches peer RSA key information");
270 case XSECCryptoKey::KEY_DSA_PUBLIC:
272 DSA* dsa = static_cast<OpenSSLCryptoKeyDSA*>(key)->getOpenSSLDSA();
273 EVP_PKEY* evp = X509_PUBKEY_get(X509_get_X509_PUBKEY(certEE));
274 if (dsa && evp && evp->type == EVP_PKEY_DSA && BN_cmp(dsa->pub_key,evp->pkey.dsa->pub_key) == 0) {
277 log.debug("end-entity certificate matches peer DSA key information");
286 log.warn("unknown peer key type, skipping...");
291 log.debug("no keys within this peer's key information matched the given end-entity certificate");