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