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.
25 #include "security/AbstractPKIXTrustEngine.h"
26 #include "signature/KeyInfo.h"
28 #include <log4cpp/Category.hh>
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 <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
40 using namespace xmlsignature;
41 using namespace xmltooling;
42 using namespace log4cpp;
47 static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
50 Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
54 static bool XMLTOOL_DLLLOCAL validate(
55 X509* EE, STACK_OF(X509)* untrusted, AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo
58 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
60 // First we build a stack of CA certs. These objects are all referenced in place.
61 log.debug("building CA list from PKIX Validation information");
63 // We need this for CRL support.
64 X509_STORE* store=X509_STORE_new();
69 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
70 X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);
73 STACK_OF(X509)* CAstack = sk_X509_new_null();
75 // This contains the state of the validate operation.
78 const vector<XSECCryptoX509*>& CAcerts = pkixInfo->getTrustAnchors();
79 for (vector<XSECCryptoX509*>::const_iterator i=CAcerts.begin(); i!=CAcerts.end(); ++i) {
80 if ((*i)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
81 sk_X509_push(CAstack,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
85 const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
86 for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
87 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
89 X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
93 // AFAICT, EE and untrusted are passed in but not owned by the ctx.
94 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
95 if (X509_STORE_CTX_init(&ctx,store,EE,untrusted)!=1) {
97 log.error("unable to initialize X509_STORE_CTX");
98 sk_X509_free(CAstack);
99 X509_STORE_free(store);
103 X509_STORE_CTX_init(&ctx,store,EE,untrusted);
106 // Seems to be most efficient to just pass in the CA stack.
107 X509_STORE_CTX_trusted_stack(&ctx,CAstack);
108 X509_STORE_CTX_set_depth(&ctx,100); // we check the depth down below
109 X509_STORE_CTX_set_verify_cb(&ctx,error_callback);
111 int ret=X509_verify_cert(&ctx);
113 // Now see if the depth was acceptable by counting the number of intermediates.
114 int depth=sk_X509_num(ctx.chain)-2;
115 if (pkixInfo->getVerificationDepth() < depth) {
117 "certificate chain was too long (%d intermediates, only %d allowed)",
118 (depth==-1) ? 0 : depth,
119 pkixInfo->getVerificationDepth()
126 X509_STORE_CTX_cleanup(&ctx);
127 X509_STORE_free(store);
128 sk_X509_free(CAstack);
131 log.debug("successfully validated certificate chain");
139 bool AbstractPKIXTrustEngine::checkEntityNames(
140 X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
143 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
145 vector<const Credential*> creds;
146 credResolver.resolve(creds,&criteria);
148 // Build a list of acceptable names.
149 vector<string> keynames(1,criteria.getPeerName());
150 for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred)
151 (*cred)->getKeyNames(keynames);
154 X509_NAME* subject=X509_get_subject_name(certEE);
156 // One way is a direct match to the subject DN.
157 // Seems that the way to do the compare is to write the X509_NAME into a BIO.
158 BIO* b = BIO_new(BIO_s_mem());
159 BIO* b2 = BIO_new(BIO_s_mem());
160 BIO_set_mem_eof_return(b, 0);
161 BIO_set_mem_eof_return(b2, 0);
162 // The flags give us LDAP order instead of X.500, with a comma separator.
163 int len=X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
164 string subjectstr,subjectstr2;
166 while ((len = BIO_read(b, buf, 255)) > 0) {
170 log.debugStream() << "certificate subject: " << subjectstr << CategoryStream::ENDLINE;
171 // The flags give us LDAP order instead of X.500, with a comma plus space separator.
172 len=X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
174 while ((len = BIO_read(b2, buf, 255)) > 0) {
179 // Check each keyname.
180 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
181 #ifdef HAVE_STRCASECMP
182 if (!strcasecmp(n->c_str(),subjectstr.c_str()) || !strcasecmp(n->c_str(),subjectstr2.c_str())) {
184 if (!stricmp(n->c_str(),subjectstr.c_str()) || !stricmp(n->c_str(),subjectstr2.c_str())) {
186 log.debug("matched full subject DN to a key name (%s)", n->c_str());
195 log.debug("unable to match DN, trying TLS subjectAltName match");
196 STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, NULL, NULL);
198 int numalts = sk_GENERAL_NAME_num(altnames);
199 for (int an=0; an<numalts; an++) {
200 const GENERAL_NAME* check = sk_GENERAL_NAME_value(altnames, an);
201 if (check->type==GEN_DNS || check->type==GEN_URI) {
202 const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
203 const int altlen = ASN1_STRING_length(check->d.ia5);
205 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
206 #ifdef HAVE_STRCASECMP
207 if ((check->type==GEN_DNS && !strncasecmp(altptr,n->c_str(),altlen))
209 if ((check->type==GEN_DNS && !strnicmp(altptr,n->c_str(),altlen))
211 || (check->type==GEN_URI && !strncmp(altptr,n->c_str(),altlen))) {
212 log.debug("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
213 GENERAL_NAMES_free(altnames);
220 GENERAL_NAMES_free(altnames);
222 log.debug("unable to match subjectAltName, trying TLS CN match");
223 memset(buf,0,sizeof(buf));
224 if (X509_NAME_get_text_by_NID(subject,NID_commonName,buf,255)>0) {
225 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
226 #ifdef HAVE_STRCASECMP
227 if (!strcasecmp(buf,n->c_str())) {
229 if (!stricmp(buf,n->c_str())) {
231 log.debug("matched subject CN to a key name (%s)", n->c_str());
237 log.warn("no common name in certificate subject");
240 log.error("certificate has no subject?!");
245 bool AbstractPKIXTrustEngine::validate(
247 STACK_OF(X509)* certChain,
248 const CredentialResolver& credResolver,
249 CredentialCriteria* criteria
255 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
258 log.error("X.509 credential was NULL, unable to perform validation");
262 if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
263 log.debug("checking that the certificate name is acceptable");
264 if (criteria->getUsage()==CredentialCriteria::UNSPECIFIED_CREDENTIAL)
265 criteria->setUsage(CredentialCriteria::SIGNING_CREDENTIAL);
266 if (!checkEntityNames(certEE,credResolver,*criteria)) {
267 log.error("certificate name was not acceptable");
272 log.debug("performing certificate path validation...");
274 auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria, m_keyInfoResolver));
275 while (pkix->next()) {
276 if (::validate(certEE,certChain,pkix.get())) {
281 log.debug("failed to validate certificate chain using supplied PKIX information");
285 bool AbstractPKIXTrustEngine::validate(
286 XSECCryptoX509* certEE,
287 const vector<XSECCryptoX509*>& certChain,
288 const CredentialResolver& credResolver,
289 CredentialCriteria* criteria
296 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("X.509 credential was NULL, unable to perform validation");
299 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
300 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
304 STACK_OF(X509)* untrusted=sk_X509_new_null();
305 for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i)
306 sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
308 bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
309 sk_X509_free(untrusted);
313 bool AbstractPKIXTrustEngine::validate(
315 const CredentialResolver& credResolver,
316 CredentialCriteria* criteria
322 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
324 const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
326 inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
327 if (!inlineResolver) {
328 log.error("unable to perform PKIX validation, no KeyInfoResolver available");
332 // Pull the certificate chain out of the signature.
333 X509Credential* x509cred;
334 auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS));
335 if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
336 log.error("unable to perform PKIX validation, signature does not contain any certificates");
339 const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
341 log.error("unable to perform PKIX validation, signature does not contain any certificates");
345 log.debug("validating signature using certificate from within the signature");
347 // Find and save off a pointer to the certificate that unlocks the object.
348 // Most of the time, this will be the first one anyway.
349 XSECCryptoX509* certEE=NULL;
350 SignatureValidator keyValidator;
351 for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
353 auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
354 keyValidator.setKey(key.get());
355 keyValidator.validate(&sig);
356 log.debug("signature verified with key inside signature, attempting certificate validation...");
359 catch (ValidationException& ex) {
360 log.debug(ex.what());
365 return validate(certEE,certs,credResolver,criteria);
367 log.debug("failed to verify signature with embedded certificates");
371 bool AbstractPKIXTrustEngine::validate(
372 const XMLCh* sigAlgorithm,
377 const CredentialResolver& credResolver,
378 CredentialCriteria* criteria
384 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
387 log.error("unable to perform PKIX validation, KeyInfo not present");
391 const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
393 inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
394 if (!inlineResolver) {
395 log.error("unable to perform PKIX validation, no KeyInfoResolver available");
399 // Pull the certificate chain out of the signature.
400 X509Credential* x509cred;
401 auto_ptr<Credential> cred(inlineResolver->resolve(keyInfo,X509Credential::RESOLVE_CERTS));
402 if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
403 log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
406 const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
408 log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
412 log.debug("validating signature using certificate from within KeyInfo");
414 // Find and save off a pointer to the certificate that unlocks the object.
415 // Most of the time, this will be the first one anyway.
416 XSECCryptoX509* certEE=NULL;
417 for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
419 auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
420 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
421 log.debug("signature verified with key inside signature, attempting certificate validation...");
425 catch (SignatureException& ex) {
426 log.debug(ex.what());
431 return validate(certEE,certs,credResolver,criteria);
433 log.debug("failed to verify signature with embedded certificates");