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 * AbstractPKIXTrustEngine.cpp
20 * A trust engine that uses X.509 trust anchors and CRLs associated with a KeyInfoSource
21 * to perform PKIX validation of signatures and certificates.
26 #include "security/AbstractPKIXTrustEngine.h"
27 #include "signature/KeyInfo.h"
29 #include <openssl/x509_vfy.h>
30 #include <openssl/x509v3.h>
31 #include <xmltooling/security/CredentialCriteria.h>
32 #include <xmltooling/security/CredentialResolver.h>
33 #include <xmltooling/security/KeyInfoResolver.h>
34 #include <xmltooling/security/OpenSSLCryptoX509CRL.h>
35 #include <xmltooling/security/X509Credential.h>
36 #include <xmltooling/signature/SignatureValidator.h>
37 #include <xmltooling/util/NDC.h>
38 #include <xercesc/util/XMLUniDefs.hpp>
39 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
41 using namespace xmlsignature;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
48 static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
51 Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
55 static bool XMLTOOL_DLLLOCAL validate(
57 STACK_OF(X509)* untrusted,
58 AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo,
60 const vector<XSECCryptoX509CRL*>* inlineCRLs=NULL
63 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
65 // First we build a stack of CA certs. These objects are all referenced in place.
66 log.debug("supplying PKIX Validation information");
68 // We need this for CRL support.
69 X509_STORE* store=X509_STORE_new();
75 STACK_OF(X509)* CAstack = sk_X509_new_null();
77 // This contains the state of the validate operation.
81 const vector<XSECCryptoX509*>& CAcerts = pkixInfo->getTrustAnchors();
82 for (vector<XSECCryptoX509*>::const_iterator i=CAcerts.begin(); i!=CAcerts.end(); ++i) {
83 if ((*i)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
84 sk_X509_push(CAstack,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
89 log.debug("supplied (%d) CA certificate(s)", count);
91 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
94 for (vector<XSECCryptoX509CRL*>::const_iterator j=inlineCRLs->begin(); j!=inlineCRLs->end(); ++j) {
95 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
97 X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
102 const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
103 for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
104 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
106 X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
110 log.debug("supplied (%d) CRL(s)", count);
112 X509_STORE_set_flags(store, fullCRLChain ? (X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL) : (X509_V_FLAG_CRL_CHECK));
114 if ((inlineCRLs && !inlineCRLs->empty()) || !pkixInfo->getCRLs().empty()) {
115 log.warn("OpenSSL versions < 0.9.7 do not support CRL checking");
119 // AFAICT, EE and untrusted are passed in but not owned by the ctx.
120 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
121 if (X509_STORE_CTX_init(&ctx,store,EE,untrusted)!=1) {
123 log.error("unable to initialize X509_STORE_CTX");
124 sk_X509_free(CAstack);
125 X509_STORE_free(store);
129 X509_STORE_CTX_init(&ctx,store,EE,untrusted);
132 // Seems to be most efficient to just pass in the CA stack.
133 X509_STORE_CTX_trusted_stack(&ctx,CAstack);
134 X509_STORE_CTX_set_depth(&ctx,100); // we check the depth down below
135 X509_STORE_CTX_set_verify_cb(&ctx,error_callback);
137 int ret=X509_verify_cert(&ctx);
139 // Now see if the depth was acceptable by counting the number of intermediates.
140 int depth=sk_X509_num(ctx.chain)-2;
141 if (pkixInfo->getVerificationDepth() < depth) {
143 "certificate chain was too long (%d intermediates, only %d allowed)",
144 (depth==-1) ? 0 : depth,
145 pkixInfo->getVerificationDepth()
152 X509_STORE_CTX_cleanup(&ctx);
153 X509_STORE_free(store);
154 sk_X509_free(CAstack);
157 log.debug("successfully validated certificate chain");
165 AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) : TrustEngine(e), m_fullCRLChain(false)
167 static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n);
168 const XMLCh* flag = e ? e->getAttributeNS(NULL, fullCRLChain) : NULL;
169 m_fullCRLChain = (flag && (*flag == xercesc::chLatin_t || *flag == xercesc::chDigit_1));
172 bool AbstractPKIXTrustEngine::checkEntityNames(
173 X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
176 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
178 // We resolve to a set of trusted credentials.
179 vector<const Credential*> creds;
180 credResolver.resolve(creds,&criteria);
182 // Build a list of acceptable names.
183 set<string> trustednames;
184 trustednames.insert(criteria.getPeerName());
185 for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred)
186 trustednames.insert((*cred)->getKeyNames().begin(), (*cred)->getKeyNames().end());
189 X509_NAME* subject=X509_get_subject_name(certEE);
191 // One way is a direct match to the subject DN.
192 // Seems that the way to do the compare is to write the X509_NAME into a BIO.
193 BIO* b = BIO_new(BIO_s_mem());
194 BIO* b2 = BIO_new(BIO_s_mem());
195 BIO_set_mem_eof_return(b, 0);
196 BIO_set_mem_eof_return(b2, 0);
197 // The flags give us LDAP order instead of X.500, with a comma separator.
198 int len=X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
199 string subjectstr,subjectstr2;
201 while ((len = BIO_read(b, buf, 255)) > 0) {
205 log.debugStream() << "certificate subject: " << subjectstr << logging::eol;
206 // The flags give us LDAP order instead of X.500, with a comma plus space separator.
207 len=X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
209 while ((len = BIO_read(b2, buf, 255)) > 0) {
214 // Check each keyname.
215 for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
216 #ifdef HAVE_STRCASECMP
217 if (!strcasecmp(n->c_str(),subjectstr.c_str()) || !strcasecmp(n->c_str(),subjectstr2.c_str())) {
219 if (!stricmp(n->c_str(),subjectstr.c_str()) || !stricmp(n->c_str(),subjectstr2.c_str())) {
221 log.debug("matched full subject DN to a key name (%s)", n->c_str());
230 log.debug("unable to match DN, trying TLS subjectAltName match");
231 STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, NULL, NULL);
233 int numalts = sk_GENERAL_NAME_num(altnames);
234 for (int an=0; an<numalts; an++) {
235 const GENERAL_NAME* check = sk_GENERAL_NAME_value(altnames, an);
236 if (check->type==GEN_DNS || check->type==GEN_URI) {
237 const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
238 const int altlen = ASN1_STRING_length(check->d.ia5);
239 for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
240 #ifdef HAVE_STRCASECMP
241 if ((check->type==GEN_DNS && !strncasecmp(altptr,n->c_str(),altlen))
243 if ((check->type==GEN_DNS && !strnicmp(altptr,n->c_str(),altlen))
245 || (check->type==GEN_URI && !strncmp(altptr,n->c_str(),altlen))) {
246 log.debug("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
247 GENERAL_NAMES_free(altnames);
254 GENERAL_NAMES_free(altnames);
256 log.debug("unable to match subjectAltName, trying TLS CN match");
257 memset(buf,0,sizeof(buf));
258 if (X509_NAME_get_text_by_NID(subject,NID_commonName,buf,255)>0) {
259 for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
260 #ifdef HAVE_STRCASECMP
261 if (!strcasecmp(buf,n->c_str())) {
263 if (!stricmp(buf,n->c_str())) {
265 log.debug("matched subject CN to a key name (%s)", n->c_str());
271 log.warn("no common name in certificate subject");
274 log.error("certificate has no subject?!");
279 bool AbstractPKIXTrustEngine::validateWithCRLs(
281 STACK_OF(X509)* certChain,
282 const CredentialResolver& credResolver,
283 CredentialCriteria* criteria,
284 const std::vector<XSECCryptoX509CRL*>* inlineCRLs
288 NDC ndc("validateWithCRLs");
290 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
293 log.error("X.509 credential was NULL, unable to perform validation");
297 if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
298 log.debug("checking that the certificate name is acceptable");
299 if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
300 criteria->setUsage(Credential::SIGNING_CREDENTIAL);
301 if (!checkEntityNames(certEE,credResolver,*criteria)) {
302 log.error("certificate name was not acceptable");
307 log.debug("performing certificate path validation...");
309 auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria));
310 while (pkix->next()) {
311 if (::validate(certEE,certChain,pkix.get(),m_fullCRLChain,inlineCRLs)) {
316 log.debug("failed to validate certificate chain using supplied PKIX information");
320 bool AbstractPKIXTrustEngine::validate(
322 STACK_OF(X509)* certChain,
323 const CredentialResolver& credResolver,
324 CredentialCriteria* criteria
327 return validateWithCRLs(certEE,certChain,credResolver,criteria);
330 bool AbstractPKIXTrustEngine::validate(
331 XSECCryptoX509* certEE,
332 const vector<XSECCryptoX509*>& certChain,
333 const CredentialResolver& credResolver,
334 CredentialCriteria* criteria
341 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("X.509 credential was NULL, unable to perform validation");
344 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
345 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("only the OpenSSL XSEC provider is supported");
349 STACK_OF(X509)* untrusted=sk_X509_new_null();
350 for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i)
351 sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
353 bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
354 sk_X509_free(untrusted);
358 bool AbstractPKIXTrustEngine::validate(
360 const CredentialResolver& credResolver,
361 CredentialCriteria* criteria
367 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
369 const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
371 inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
372 if (!inlineResolver) {
373 log.error("unable to perform PKIX validation, no KeyInfoResolver available");
377 // Pull the certificate chain out of the signature.
378 X509Credential* x509cred;
379 auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS));
380 if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
381 log.error("unable to perform PKIX validation, signature does not contain any certificates");
384 const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
386 log.error("unable to perform PKIX validation, signature does not contain any certificates");
390 log.debug("validating signature using certificate from within the signature");
392 // Find and save off a pointer to the certificate that unlocks the object.
393 // Most of the time, this will be the first one anyway.
394 XSECCryptoX509* certEE=NULL;
395 SignatureValidator keyValidator;
396 for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
398 auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
399 keyValidator.setKey(key.get());
400 keyValidator.validate(&sig);
401 log.debug("signature verified with key inside signature, attempting certificate validation...");
404 catch (ValidationException& ex) {
405 log.debug(ex.what());
410 log.debug("failed to verify signature with embedded certificates");
413 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
414 log.error("only the OpenSSL XSEC provider is supported");
418 STACK_OF(X509)* untrusted=sk_X509_new_null();
419 for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); i!=certs.end(); ++i)
420 sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
421 const vector<XSECCryptoX509CRL*>& crls = x509cred->getCRLs();
422 bool ret = validateWithCRLs(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria, &crls);
423 sk_X509_free(untrusted);
427 bool AbstractPKIXTrustEngine::validate(
428 const XMLCh* sigAlgorithm,
433 const CredentialResolver& credResolver,
434 CredentialCriteria* criteria
440 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
443 log.error("unable to perform PKIX validation, KeyInfo not present");
447 const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
449 inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
450 if (!inlineResolver) {
451 log.error("unable to perform PKIX validation, no KeyInfoResolver available");
455 // Pull the certificate chain out of the signature.
456 X509Credential* x509cred;
457 auto_ptr<Credential> cred(inlineResolver->resolve(keyInfo,X509Credential::RESOLVE_CERTS));
458 if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
459 log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
462 const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
464 log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
468 log.debug("validating signature using certificate from within KeyInfo");
470 // Find and save off a pointer to the certificate that unlocks the object.
471 // Most of the time, this will be the first one anyway.
472 XSECCryptoX509* certEE=NULL;
473 for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
475 auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
476 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
477 log.debug("signature verified with key inside signature, attempting certificate validation...");
481 catch (SignatureException& ex) {
482 log.debug(ex.what());
487 return validate(certEE,certs,credResolver,criteria);
489 log.debug("failed to verify signature with embedded certificates");