1f0f8fca275e23cf65d3bd0f07a9479c7fd039c4
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / AbstractPKIXTrustEngine.cpp
1 /*
2  *  Copyright 2001-2009 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * AbstractPKIXTrustEngine.cpp
19  * 
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.
22  */
23
24 #include "internal.h"
25 #include "logging.h"
26 #include "security/AbstractPKIXTrustEngine.h"
27 #include "signature/KeyInfo.h"
28 #include "signature/Signature.h"
29
30 #include <openssl/x509_vfy.h>
31 #include <openssl/x509v3.h>
32 #include <xmltooling/security/CredentialCriteria.h>
33 #include <xmltooling/security/CredentialResolver.h>
34 #include <xmltooling/security/KeyInfoResolver.h>
35 #include <xmltooling/security/OpenSSLCryptoX509CRL.h>
36 #include <xmltooling/security/X509Credential.h>
37 #include <xmltooling/signature/SignatureValidator.h>
38 #include <xmltooling/util/NDC.h>
39 #include <xercesc/util/XMLUniDefs.hpp>
40 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
41
42 using namespace xmlsignature;
43 using namespace xmltooling::logging;
44 using namespace xmltooling;
45 using namespace std;
46
47
48 namespace {
49     static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
50     {
51         if (!ok)
52             Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
53         return ok;
54     }
55
56     static bool XMLTOOL_DLLLOCAL validate(
57         X509* EE,
58         STACK_OF(X509)* untrusted,
59         AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo,
60         bool fullCRLChain,
61         const vector<XSECCryptoX509CRL*>* inlineCRLs=NULL
62         )
63     {
64         Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
65     
66         // First we build a stack of CA certs. These objects are all referenced in place.
67         log.debug("supplying PKIX Validation information");
68     
69         // We need this for CRL support.
70         X509_STORE* store=X509_STORE_new();
71         if (!store) {
72             log_openssl();
73             return false;
74         }
75     
76         STACK_OF(X509)* CAstack = sk_X509_new_null();
77         
78         // This contains the state of the validate operation.
79         int count=0;
80         X509_STORE_CTX ctx;
81         
82         const vector<XSECCryptoX509*>& CAcerts = pkixInfo->getTrustAnchors();
83         for (vector<XSECCryptoX509*>::const_iterator i=CAcerts.begin(); i!=CAcerts.end(); ++i) {
84             if ((*i)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
85                 sk_X509_push(CAstack,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
86                 ++count;
87             }
88         }
89
90         log.debug("supplied (%d) CA certificate(s)", count);
91
92 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
93         count=0;
94         if (inlineCRLs) {
95             for (vector<XSECCryptoX509CRL*>::const_iterator j=inlineCRLs->begin(); j!=inlineCRLs->end(); ++j) {
96                 if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
97                     // owned by store
98                     X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
99                     ++count;
100                 }
101             }
102         }
103         const vector<XSECCryptoX509CRL*>& crls = pkixInfo->getCRLs();
104         for (vector<XSECCryptoX509CRL*>::const_iterator j=crls.begin(); j!=crls.end(); ++j) {
105             if ((*j)->getProviderName()==DSIGConstants::s_unicodeStrPROVOpenSSL) {
106                 // owned by store
107                 X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
108                 ++count;
109             }
110         }
111         log.debug("supplied (%d) CRL(s)", count);
112         if (count > 0)
113             X509_STORE_set_flags(store, fullCRLChain ? (X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL) : (X509_V_FLAG_CRL_CHECK));
114 #else
115         if ((inlineCRLs && !inlineCRLs->empty()) || !pkixInfo->getCRLs().empty()) {
116             log.warn("OpenSSL versions < 0.9.7 do not support CRL checking");
117         }
118 #endif
119
120         // AFAICT, EE and untrusted are passed in but not owned by the ctx.
121 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
122         if (X509_STORE_CTX_init(&ctx,store,EE,untrusted)!=1) {
123             log_openssl();
124             log.error("unable to initialize X509_STORE_CTX");
125             sk_X509_free(CAstack);
126             X509_STORE_free(store);
127             return false;
128         }
129 #else
130         X509_STORE_CTX_init(&ctx,store,EE,untrusted);
131 #endif
132     
133         // Seems to be most efficient to just pass in the CA stack.
134         X509_STORE_CTX_trusted_stack(&ctx,CAstack);
135         X509_STORE_CTX_set_depth(&ctx,100);    // we check the depth down below
136         X509_STORE_CTX_set_verify_cb(&ctx,error_callback);
137         
138         int ret=X509_verify_cert(&ctx);
139         if (ret==1) {
140             // Now see if the depth was acceptable by counting the number of intermediates.
141             int depth=sk_X509_num(ctx.chain)-2;
142             if (pkixInfo->getVerificationDepth() < depth) {
143                 log.error(
144                     "certificate chain was too long (%d intermediates, only %d allowed)",
145                     (depth==-1) ? 0 : depth,
146                     pkixInfo->getVerificationDepth()
147                     );
148                 ret=0;
149             }
150         }
151         
152         // Clean up...
153         X509_STORE_CTX_cleanup(&ctx);
154         X509_STORE_free(store);
155         sk_X509_free(CAstack);
156     
157         if (ret==1) {
158             log.debug("successfully validated certificate chain");
159             return true;
160         }
161         
162         return false;
163     }
164 };
165
166 AbstractPKIXTrustEngine::PKIXValidationInfoIterator::PKIXValidationInfoIterator()
167 {
168 }
169
170 AbstractPKIXTrustEngine::PKIXValidationInfoIterator::~PKIXValidationInfoIterator()
171 {
172 }
173
174 AbstractPKIXTrustEngine::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) : TrustEngine(e), m_fullCRLChain(false)
175 {
176     static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n);
177     const XMLCh* flag = e ? e->getAttributeNS(NULL, fullCRLChain) : NULL;
178     m_fullCRLChain = (flag && (*flag == xercesc::chLatin_t || *flag == xercesc::chDigit_1));
179 }
180
181 AbstractPKIXTrustEngine::~AbstractPKIXTrustEngine()
182 {
183 }
184
185 bool AbstractPKIXTrustEngine::checkEntityNames(
186     X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
187     ) const
188 {
189     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
190
191     // We resolve to a set of trusted credentials.
192     vector<const Credential*> creds;
193     credResolver.resolve(creds,&criteria);
194
195     // Build a list of acceptable names.
196     set<string> trustednames;
197     trustednames.insert(criteria.getPeerName());
198     for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred)
199         trustednames.insert((*cred)->getKeyNames().begin(), (*cred)->getKeyNames().end());
200
201     X509_NAME* subject=X509_get_subject_name(certEE);
202     if (subject) {
203         // One way is a direct match to the subject DN.
204         // Seems that the way to do the compare is to write the X509_NAME into a BIO.
205         BIO* b = BIO_new(BIO_s_mem());
206         BIO* b2 = BIO_new(BIO_s_mem());
207         // The flags give us LDAP order instead of X.500, with a comma separator.
208         X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
209         BIO_flush(b);
210         // The flags give us LDAP order instead of X.500, with a comma plus space separator.
211         X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
212         BIO_flush(b2);
213
214         BUF_MEM* bptr=NULL;
215         BUF_MEM* bptr2=NULL;
216         BIO_get_mem_ptr(b, &bptr);
217         BIO_get_mem_ptr(b2, &bptr2);
218
219         if (bptr && bptr->length > 0 && log.isDebugEnabled()) {
220             string subjectstr(bptr->data, bptr->length);
221             log.debug("certificate subject: %s", subjectstr.c_str());
222         }
223         
224         // Check each keyname.
225         for (set<string>::const_iterator n=trustednames.begin(); bptr && bptr2 && n!=trustednames.end(); n++) {
226 #ifdef HAVE_STRCASECMP
227             if ((n->length() == bptr->length && !strncasecmp(n->c_str(), bptr->data, bptr->length)) ||
228                 (n->length() == bptr2->length && !strncasecmp(n->c_str(), bptr2->data, bptr2->length))) {
229 #else
230             if ((n->length() == bptr->length && !strnicmp(n->c_str(), bptr->data, bptr->length)) ||
231                 (n->length() == bptr2->length && !strnicmp(n->c_str(), bptr2->data, bptr2->length))) {
232 #endif
233                 log.debug("matched full subject DN to a key name (%s)", n->c_str());
234                 BIO_free(b);
235                 BIO_free(b2);
236                 return true;
237             }
238         }
239         BIO_free(b);
240         BIO_free(b2);
241
242         log.debug("unable to match DN, trying TLS subjectAltName match");
243         STACK_OF(GENERAL_NAME)* altnames=(STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(certEE, NID_subject_alt_name, NULL, NULL);
244         if (altnames) {
245             int numalts = sk_GENERAL_NAME_num(altnames);
246             for (int an=0; an<numalts; an++) {
247                 const GENERAL_NAME* check = sk_GENERAL_NAME_value(altnames, an);
248                 if (check->type==GEN_DNS || check->type==GEN_URI) {
249                     const char* altptr = (char*)ASN1_STRING_data(check->d.ia5);
250                     const int altlen = ASN1_STRING_length(check->d.ia5);
251                     for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
252 #ifdef HAVE_STRCASECMP
253                         if ((check->type==GEN_DNS && n->length()==altlen && !strncasecmp(altptr,n->c_str(),altlen))
254 #else
255                         if ((check->type==GEN_DNS && n->length()==altlen && !strnicmp(altptr,n->c_str(),altlen))
256 #endif
257                                 || (check->type==GEN_URI && n->length()==altlen && !strncmp(altptr,n->c_str(),altlen))) {
258                             log.debug("matched DNS/URI subjectAltName to a key name (%s)", n->c_str());
259                             GENERAL_NAMES_free(altnames);
260                             return true;
261                         }
262                     }
263                 }
264             }
265         }
266         GENERAL_NAMES_free(altnames);
267             
268         log.debug("unable to match subjectAltName, trying TLS CN match");
269
270         // Fetch the last CN RDN.
271         char* peer_CN = NULL;
272         int j,i = -1;
273         while ((j=X509_NAME_get_index_by_NID(subject, NID_commonName, i)) >= 0)
274             i = j;
275         if (i >= 0) {
276             ASN1_STRING* tmp = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i));
277             // Copied in from libcurl.
278             /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
279                is already UTF-8 encoded. We check for this case and copy the raw
280                string manually to avoid the problem. */
281             if(tmp && ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
282                 j = ASN1_STRING_length(tmp);
283                 if(j >= 0) {
284                     peer_CN = (char*)OPENSSL_malloc(j + 1);
285                     memcpy(peer_CN, ASN1_STRING_data(tmp), j);
286                     peer_CN[j] = '\0';
287                 }
288             }
289             else /* not a UTF8 name */ {
290                 j = ASN1_STRING_to_UTF8(reinterpret_cast<unsigned char**>(&peer_CN), tmp);
291             }
292
293             for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
294 #ifdef HAVE_STRCASECMP
295                 if (n->length() == j && !strncasecmp(peer_CN, n->c_str(), j)) {
296 #else
297                 if (n->length() == j && !strnicmp(peer_CN, n->c_str(), j)) {
298 #endif
299                     log.debug("matched subject CN to a key name (%s)", n->c_str());
300                     if(peer_CN)
301                         OPENSSL_free(peer_CN);
302                     return true;
303                 }
304             }
305             if(peer_CN)
306                 OPENSSL_free(peer_CN);
307         }
308         else {
309             log.warn("no common name in certificate subject");
310         }
311     }
312     else {
313         log.error("certificate has no subject?!");
314     }
315     
316     return false;
317 }
318
319 bool AbstractPKIXTrustEngine::validateWithCRLs(
320     X509* certEE,
321     STACK_OF(X509)* certChain,
322     const CredentialResolver& credResolver,
323     CredentialCriteria* criteria,
324     const std::vector<XSECCryptoX509CRL*>* inlineCRLs
325     ) const
326 {
327 #ifdef _DEBUG
328     NDC ndc("validateWithCRLs");
329 #endif
330     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
331
332     if (!certEE) {
333         log.error("X.509 credential was NULL, unable to perform validation");
334         return false;
335     }
336
337     if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
338         log.debug("checking that the certificate name is acceptable");
339         if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
340             criteria->setUsage(Credential::SIGNING_CREDENTIAL);
341         if (!checkEntityNames(certEE,credResolver,*criteria)) {
342             log.error("certificate name was not acceptable");
343             return false;
344         }
345     }
346     
347     log.debug("performing certificate path validation...");
348
349     auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria));
350     while (pkix->next()) {
351         if (::validate(certEE,certChain,pkix.get(),m_fullCRLChain,inlineCRLs)) {
352             return true;
353         }
354     }
355
356     log.debug("failed to validate certificate chain using supplied PKIX information");
357     return false;
358 }
359
360 bool AbstractPKIXTrustEngine::validate(
361     X509* certEE,
362     STACK_OF(X509)* certChain,
363     const CredentialResolver& credResolver,
364     CredentialCriteria* criteria
365     ) const
366 {
367     return validateWithCRLs(certEE,certChain,credResolver,criteria);
368 }
369
370 bool AbstractPKIXTrustEngine::validate(
371     XSECCryptoX509* certEE,
372     const vector<XSECCryptoX509*>& certChain,
373     const CredentialResolver& credResolver,
374     CredentialCriteria* criteria
375     ) const
376 {
377 #ifdef _DEBUG
378         NDC ndc("validate");
379 #endif
380     if (!certEE) {
381         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("X.509 credential was NULL, unable to perform validation");
382         return false;
383     }
384     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
385         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("only the OpenSSL XSEC provider is supported");
386         return false;
387     }
388
389     STACK_OF(X509)* untrusted=sk_X509_new_null();
390     for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i)
391         sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
392
393     bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
394     sk_X509_free(untrusted);
395     return ret;
396 }
397
398 bool AbstractPKIXTrustEngine::validate(
399     Signature& sig,
400     const CredentialResolver& credResolver,
401     CredentialCriteria* criteria
402     ) const
403 {
404 #ifdef _DEBUG
405     NDC ndc("validate");
406 #endif
407     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
408
409     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
410     if (!inlineResolver)
411         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
412     if (!inlineResolver) {
413         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
414         return false;
415     }
416
417     // Pull the certificate chain out of the signature.
418     X509Credential* x509cred;
419     auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS));
420     if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
421         log.error("unable to perform PKIX validation, signature does not contain any certificates");
422         return false;
423     }
424     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
425     if (certs.empty()) {
426         log.error("unable to perform PKIX validation, signature does not contain any certificates");
427         return false;
428     }
429
430     log.debug("validating signature using certificate from within the signature");
431
432     // Find and save off a pointer to the certificate that unlocks the object.
433     // Most of the time, this will be the first one anyway.
434     XSECCryptoX509* certEE=NULL;
435     SignatureValidator keyValidator;
436     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
437         try {
438             auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
439             keyValidator.setKey(key.get());
440             keyValidator.validate(&sig);
441             log.debug("signature verified with key inside signature, attempting certificate validation...");
442             certEE=(*i);
443         }
444         catch (ValidationException& ex) {
445             log.debug(ex.what());
446         }
447     }
448     
449     if (!certEE) {
450         log.debug("failed to verify signature with embedded certificates");
451         return false;
452     }
453     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
454         log.error("only the OpenSSL XSEC provider is supported");
455         return false;
456     }
457
458     STACK_OF(X509)* untrusted=sk_X509_new_null();
459     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); i!=certs.end(); ++i)
460         sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
461     const vector<XSECCryptoX509CRL*>& crls = x509cred->getCRLs();
462     bool ret = validateWithCRLs(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria, &crls);
463     sk_X509_free(untrusted);
464     return ret;
465 }
466
467 bool AbstractPKIXTrustEngine::validate(
468     const XMLCh* sigAlgorithm,
469     const char* sig,
470     KeyInfo* keyInfo,
471     const char* in,
472     unsigned int in_len,
473     const CredentialResolver& credResolver,
474     CredentialCriteria* criteria
475     ) const
476 {
477 #ifdef _DEBUG
478     NDC ndc("validate");
479 #endif
480     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
481
482     if (!keyInfo) {
483         log.error("unable to perform PKIX validation, KeyInfo not present");
484         return false;
485     }
486
487     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
488     if (!inlineResolver)
489         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
490     if (!inlineResolver) {
491         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
492         return false;
493     }
494
495     // Pull the certificate chain out of the signature.
496     X509Credential* x509cred;
497     auto_ptr<Credential> cred(inlineResolver->resolve(keyInfo,X509Credential::RESOLVE_CERTS));
498     if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
499         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
500         return false;
501     }
502     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
503     if (certs.empty()) {
504         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
505         return false;
506     }
507
508     log.debug("validating signature using certificate from within KeyInfo");
509
510     // Find and save off a pointer to the certificate that unlocks the object.
511     // Most of the time, this will be the first one anyway.
512     XSECCryptoX509* certEE=NULL;
513     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
514         try {
515             auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
516             if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
517                 log.debug("signature verified with key inside signature, attempting certificate validation...");
518                 certEE=(*i);
519             }
520         }
521         catch (SignatureException& ex) {
522             log.debug(ex.what());
523         }
524     }
525     
526     if (certEE)
527         return validate(certEE,certs,credResolver,criteria);
528         
529     log.debug("failed to verify signature with embedded certificates");
530     return false;
531 }