2 * Copyright 2006 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/OpenSSLCryptoX509CRL.h>
32 #include <xmltooling/signature/SignatureValidator.h>
33 #include <xmltooling/util/NDC.h>
34 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
36 using namespace xmlsignature;
37 using namespace xmltooling;
38 using namespace log4cpp;
41 AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const DOMElement* e) : OpenSSLTrustEngine(e), m_inlineResolver(NULL)
43 m_inlineResolver = XMLToolingConfig::getConfig().KeyResolverManager.newPlugin(INLINE_KEY_RESOLVER,NULL);
46 AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine()
48 delete m_inlineResolver;
52 static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
55 Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
59 static bool XMLTOOL_DLLLOCAL validate(
60 X509* EE, STACK_OF(X509)* untrusted, AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo
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("building CA list from PKIX Validation information");
68 // We need this for CRL support.
69 X509_STORE* store=X509_STORE_new();
74 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
75 X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);
78 STACK_OF(X509)* CAstack = sk_X509_new_null();
80 // This contains the state of the validate operation.
83 const vector<XSECCryptoX509*>& CAcerts = pkixInfo->getTrustAnchors();
84 for (vector<XSECCryptoX509*>::const_iterator i=CAcerts.begin(); i!=CAcerts.end(); ++i) {
85 if ((*i)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
86 sk_X509_push(CAstack,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
90 const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
91 for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
92 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
96 X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL())
101 // AFAICT, EE and untrusted are passed in but not owned by the ctx.
102 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
103 if (X509_STORE_CTX_init(&ctx,store,EE,untrusted)!=1) {
105 log.error("unable to initialize X509_STORE_CTX");
106 sk_X509_free(CAstack);
107 X509_STORE_free(store);
111 X509_STORE_CTX_init(&ctx,store,EE,untrusted);
114 // Seems to be most efficient to just pass in the CA stack.
115 X509_STORE_CTX_trusted_stack(&ctx,CAstack);
116 X509_STORE_CTX_set_depth(&ctx,100); // we check the depth down below
117 X509_STORE_CTX_set_verify_cb(&ctx,error_callback);
119 int ret=X509_verify_cert(&ctx);
121 // Now see if the depth was acceptable by counting the number of intermediates.
122 int depth=sk_X509_num(ctx.chain)-2;
123 if (pkixInfo->getVerificationDepth() < depth) {
125 "certificate chain was too long (%d intermediates, only %d allowed)",
126 (depth==-1) ? 0 : depth,
127 pkixInfo->getVerificationDepth()
134 X509_STORE_CTX_cleanup(&ctx);
135 X509_STORE_free(store);
136 sk_X509_free(CAstack);
139 log.info("successfully validated certificate chain");
147 bool AbstractPKIXTrustEngine::checkEntityNames(X509* certEE, const KeyInfoSource& keyInfoSource) const
149 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
151 // Build a list of acceptable names. Transcode the possible key "names" to UTF-8.
152 // For some simple cases, this should handle UTF-8 encoded DNs in certificates.
153 vector<string> keynames;
154 auto_ptr<KeyInfoIterator> keyInfoIter(keyInfoSource.getKeyInfoIterator());
155 while (keyInfoIter->hasNext()) {
156 const KeyInfo* keyInfo = keyInfoIter->next();
157 const vector<KeyName*>& knames=keyInfo->getKeyNames();
158 for (vector<KeyName*>::const_iterator kn_i=knames.begin(); kn_i!=knames.end(); ++kn_i) {
159 const XMLCh* n=(*kn_i)->getName();
162 keynames.push_back(kn);
168 string peername = keyInfoSource.getName();
169 if (!peername.empty())
170 keynames.push_back(peername);
173 X509_NAME* subject=X509_get_subject_name(certEE);
175 // One way is a direct match to the subject DN.
176 // Seems that the way to do the compare is to write the X509_NAME into a BIO.
177 BIO* b = BIO_new(BIO_s_mem());
178 BIO* b2 = BIO_new(BIO_s_mem());
179 BIO_set_mem_eof_return(b, 0);
180 BIO_set_mem_eof_return(b2, 0);
181 // The flags give us LDAP order instead of X.500, with a comma separator.
182 int len=X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
183 string subjectstr,subjectstr2;
185 while ((len = BIO_read(b, buf, 255)) > 0) {
189 log.infoStream() << "certificate subject: " << subjectstr << CategoryStream::ENDLINE;
190 // The flags give us LDAP order instead of X.500, with a comma plus space separator.
191 len=X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
193 while ((len = BIO_read(b2, buf, 255)) > 0) {
198 // Check each keyname.
199 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
200 #ifdef HAVE_STRCASECMP
201 if (!strcasecmp(n->c_str(),subjectstr.c_str()) || !strcasecmp(n->c_str(),subjectstr2.c_str())) {
203 if (!stricmp(n->c_str(),subjectstr.c_str()) || !stricmp(n->c_str(),subjectstr2.c_str())) {
205 log.info("matched full subject DN to a key name (%s)", n->c_str());
214 log.debug("unable to match DN, trying TLS subjectAltName match");
215 STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, NULL, NULL);
217 int numalts = sk_GENERAL_NAME_num(altnames);
218 for (int an=0; an<numalts; an++) {
219 const GENERAL_NAME* check = sk_GENERAL_NAME_value(altnames, an);
220 if (check->type==GEN_DNS || check->type==GEN_URI) {
221 const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
222 const int altlen = ASN1_STRING_length(check->d.ia5);
224 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
225 #ifdef HAVE_STRCASECMP
226 if ((check->type==GEN_DNS && !strncasecmp(altptr,n->c_str(),altlen))
228 if ((check->type==GEN_DNS && !strnicmp(altptr,n->c_str(),altlen))
230 || (check->type==GEN_URI && !strncmp(altptr,n->c_str(),altlen))) {
231 log.info("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
232 GENERAL_NAMES_free(altnames);
239 GENERAL_NAMES_free(altnames);
241 log.debug("unable to match subjectAltName, trying TLS CN match");
242 memset(buf,0,sizeof(buf));
243 if (X509_NAME_get_text_by_NID(subject,NID_commonName,buf,255)>0) {
244 for (vector<string>::const_iterator n=keynames.begin(); n!=keynames.end(); n++) {
245 #ifdef HAVE_STRCASECMP
246 if (!strcasecmp(buf,n->c_str())) {
248 if (!stricmp(buf,n->c_str())) {
250 log.info("matched subject CN to a key name (%s)", n->c_str());
256 log.warn("no common name in certificate subject");
259 log.error("certificate has no subject?!");
264 bool AbstractPKIXTrustEngine::validate(
266 STACK_OF(X509)* certChain,
267 const KeyInfoSource& keyInfoSource,
269 const KeyResolver* keyResolver
275 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
278 log.error("X.509 credential was NULL, unable to perform validation");
283 log.debug("checking that the certificate name is acceptable");
284 if (!checkEntityNames(certEE,keyInfoSource)) {
285 log.error("certificate name was not acceptable");
290 log.debug("performing certificate path validation...");
292 auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(keyInfoSource));
293 while (pkix->next()) {
294 if (::validate(certEE,certChain,pkix.get())) {
299 log.error("failed to validate certificate chain using supplied PKIX information");
303 bool AbstractPKIXTrustEngine::validate(
304 XSECCryptoX509* certEE,
305 const vector<XSECCryptoX509*>& certChain,
306 const KeyInfoSource& keyInfoSource,
308 const KeyResolver* keyResolver
315 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("X.509 credential was NULL, unable to perform validation");
318 else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
322 Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
326 STACK_OF(X509)* untrusted=sk_X509_new_null();
327 for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i) {
328 sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
331 bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(),untrusted,keyInfoSource,checkName,keyResolver);
332 sk_X509_free(untrusted);
336 bool AbstractPKIXTrustEngine::validate(
338 const KeyInfoSource& keyInfoSource,
339 const KeyResolver* keyResolver
345 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
347 // Pull the certificate chain out of the signature using an inline KeyResolver.
348 KeyResolver::ResolvedCertificates certs;
349 if (0==m_inlineResolver->resolveCertificates(&sig, certs)) {
350 log.error("unable to perform PKIX validation, signature does not contain any certificates");
354 log.debug("validating signature using certificate from within the signature");
356 // Find and save off a pointer to the certificate that unlocks the object.
357 // Most of the time, this will be the first one anyway.
358 XSECCryptoX509* certEE=NULL;
359 SignatureValidator keyValidator;
360 for (vector<XSECCryptoX509*>::const_iterator i=certs.v().begin(); !certEE && i!=certs.v().end(); ++i) {
362 keyValidator.setKey((*i)->clonePublicKey());
363 keyValidator.validate(&sig);
364 log.info("signature verified with key inside signature, attempting certificate validation...");
367 catch (ValidationException&) {
373 return validate(certEE,certs.v(),keyInfoSource,true,keyResolver);
375 log.error("failed to verify signature with embedded certificates");
379 bool AbstractPKIXTrustEngine::validate(
380 const XMLCh* sigAlgorithm,
385 const KeyInfoSource& keyInfoSource,
386 const KeyResolver* keyResolver
392 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
394 // Pull the certificate chain out of the KeyInfo using an inline KeyResolver.
395 KeyResolver::ResolvedCertificates certs;
396 if (!keyInfo || 0==m_inlineResolver->resolveCertificates(keyInfo, certs)) {
397 log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
401 log.debug("validating signature using certificate from within KeyInfo");
403 // Find and save off a pointer to the certificate that unlocks the object.
404 // Most of the time, this will be the first one anyway.
405 XSECCryptoX509* certEE=NULL;
406 SignatureValidator keyValidator;
407 for (vector<XSECCryptoX509*>::const_iterator i=certs.v().begin(); !certEE && i!=certs.v().end(); ++i) {
409 auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
410 if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
411 log.info("signature verified with key inside signature, attempting certificate validation...");
415 catch (SignatureException&) {
421 return validate(certEE,certs.v(),keyInfoSource,true,keyResolver);
423 log.error("failed to verify signature with embedded certificates");