a20f0349348fbf70cd9b79d399557fcb8c55caa4
[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::AbstractPKIXTrustEngine(const xercesc::DOMElement* e) : TrustEngine(e), m_fullCRLChain(false)
167 {
168     static XMLCh fullCRLChain[] = UNICODE_LITERAL_12(f,u,l,l,C,R,L,C,h,a,i,n);
169     const XMLCh* flag = e ? e->getAttributeNS(NULL, fullCRLChain) : NULL;
170     m_fullCRLChain = (flag && (*flag == xercesc::chLatin_t || *flag == xercesc::chDigit_1));
171 }
172
173 bool AbstractPKIXTrustEngine::checkEntityNames(
174     X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
175     ) const
176 {
177     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
178
179     // We resolve to a set of trusted credentials.
180     vector<const Credential*> creds;
181     credResolver.resolve(creds,&criteria);
182
183     // Build a list of acceptable names.
184     set<string> trustednames;
185     trustednames.insert(criteria.getPeerName());
186     for (vector<const Credential*>::const_iterator cred = creds.begin(); cred!=creds.end(); ++cred)
187         trustednames.insert((*cred)->getKeyNames().begin(), (*cred)->getKeyNames().end());
188
189     X509_NAME* subject=X509_get_subject_name(certEE);
190     if (subject) {
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         // The flags give us LDAP order instead of X.500, with a comma separator.
196         X509_NAME_print_ex(b,subject,0,XN_FLAG_RFC2253);
197         BIO_flush(b);
198         // The flags give us LDAP order instead of X.500, with a comma plus space separator.
199         X509_NAME_print_ex(b2,subject,0,XN_FLAG_RFC2253 + XN_FLAG_SEP_CPLUS_SPC - XN_FLAG_SEP_COMMA_PLUS);
200         BIO_flush(b2);
201
202         BUF_MEM* bptr=NULL;
203         BUF_MEM* bptr2=NULL;
204         BIO_get_mem_ptr(b, &bptr);
205         BIO_get_mem_ptr(b2, &bptr2);
206
207         if (bptr && bptr->length > 0 && log.isDebugEnabled()) {
208             string subjectstr(bptr->data, bptr->length);
209             log.debug("certificate subject: %s", subjectstr.c_str());
210         }
211         
212         // Check each keyname.
213         for (set<string>::const_iterator n=trustednames.begin(); bptr && bptr2 && n!=trustednames.end(); n++) {
214 #ifdef HAVE_STRCASECMP
215             if ((n->length() == bptr->length && !strncasecmp(n->c_str(), bptr->data, bptr->length)) ||
216                 (n->length() == bptr2->length && !strncasecmp(n->c_str(), bptr2->data, bptr2->length))) {
217 #else
218             if ((n->length() == bptr->length && !strnicmp(n->c_str(), bptr->data, bptr->length)) ||
219                 (n->length() == bptr2->length && !strnicmp(n->c_str(), bptr2->data, bptr2->length))) {
220 #endif
221                 log.debug("matched full subject DN to a key name (%s)", n->c_str());
222                 BIO_free(b);
223                 BIO_free(b2);
224                 return true;
225             }
226         }
227         BIO_free(b);
228         BIO_free(b2);
229
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);
232         if (altnames) {
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 && n->length()==altlen && !strncasecmp(altptr,n->c_str(),altlen))
242 #else
243                         if ((check->type==GEN_DNS && n->length()==altlen && !strnicmp(altptr,n->c_str(),altlen))
244 #endif
245                                 || (check->type==GEN_URI && n->length()==altlen && !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);
248                             return true;
249                         }
250                     }
251                 }
252             }
253         }
254         GENERAL_NAMES_free(altnames);
255             
256         log.debug("unable to match subjectAltName, trying TLS CN match");
257
258         // Fetch the last CN RDN.
259         char* peer_CN = NULL;
260         int j,i = -1;
261         while ((j=X509_NAME_get_index_by_NID(subject, NID_commonName, i)) >= 0)
262             i = j;
263         if (i >= 0) {
264             ASN1_STRING* tmp = X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i));
265             // Copied in from libcurl.
266             /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input
267                is already UTF-8 encoded. We check for this case and copy the raw
268                string manually to avoid the problem. */
269             if(tmp && ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) {
270                 j = ASN1_STRING_length(tmp);
271                 if(j >= 0) {
272                     peer_CN = (char*)OPENSSL_malloc(j + 1);
273                     memcpy(peer_CN, ASN1_STRING_data(tmp), j);
274                     peer_CN[j] = '\0';
275                 }
276             }
277             else /* not a UTF8 name */ {
278                 j = ASN1_STRING_to_UTF8(reinterpret_cast<unsigned char**>(&peer_CN), tmp);
279             }
280
281             for (set<string>::const_iterator n=trustednames.begin(); n!=trustednames.end(); n++) {
282 #ifdef HAVE_STRCASECMP
283                 if (n->length() == j && !strncasecmp(peer_CN, n->c_str(), j)) {
284 #else
285                 if (n->length() == j && !strnicmp(peer_CN, n->c_str(), j)) {
286 #endif
287                     log.debug("matched subject CN to a key name (%s)", n->c_str());
288                     if(peer_CN)
289                         OPENSSL_free(peer_CN);
290                     return true;
291                 }
292             }
293             if(peer_CN)
294                 OPENSSL_free(peer_CN);
295         }
296         else {
297             log.warn("no common name in certificate subject");
298         }
299     }
300     else {
301         log.error("certificate has no subject?!");
302     }
303     
304     return false;
305 }
306
307 bool AbstractPKIXTrustEngine::validateWithCRLs(
308     X509* certEE,
309     STACK_OF(X509)* certChain,
310     const CredentialResolver& credResolver,
311     CredentialCriteria* criteria,
312     const std::vector<XSECCryptoX509CRL*>* inlineCRLs
313     ) const
314 {
315 #ifdef _DEBUG
316     NDC ndc("validateWithCRLs");
317 #endif
318     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
319
320     if (!certEE) {
321         log.error("X.509 credential was NULL, unable to perform validation");
322         return false;
323     }
324
325     if (criteria && criteria->getPeerName() && *(criteria->getPeerName())) {
326         log.debug("checking that the certificate name is acceptable");
327         if (criteria->getUsage()==Credential::UNSPECIFIED_CREDENTIAL)
328             criteria->setUsage(Credential::SIGNING_CREDENTIAL);
329         if (!checkEntityNames(certEE,credResolver,*criteria)) {
330             log.error("certificate name was not acceptable");
331             return false;
332         }
333     }
334     
335     log.debug("performing certificate path validation...");
336
337     auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria));
338     while (pkix->next()) {
339         if (::validate(certEE,certChain,pkix.get(),m_fullCRLChain,inlineCRLs)) {
340             return true;
341         }
342     }
343
344     log.debug("failed to validate certificate chain using supplied PKIX information");
345     return false;
346 }
347
348 bool AbstractPKIXTrustEngine::validate(
349     X509* certEE,
350     STACK_OF(X509)* certChain,
351     const CredentialResolver& credResolver,
352     CredentialCriteria* criteria
353     ) const
354 {
355     return validateWithCRLs(certEE,certChain,credResolver,criteria);
356 }
357
358 bool AbstractPKIXTrustEngine::validate(
359     XSECCryptoX509* certEE,
360     const vector<XSECCryptoX509*>& certChain,
361     const CredentialResolver& credResolver,
362     CredentialCriteria* criteria
363     ) const
364 {
365 #ifdef _DEBUG
366         NDC ndc("validate");
367 #endif
368     if (!certEE) {
369         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("X.509 credential was NULL, unable to perform validation");
370         return false;
371     }
372     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
373         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX").error("only the OpenSSL XSEC provider is supported");
374         return false;
375     }
376
377     STACK_OF(X509)* untrusted=sk_X509_new_null();
378     for (vector<XSECCryptoX509*>::const_iterator i=certChain.begin(); i!=certChain.end(); ++i)
379         sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
380
381     bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
382     sk_X509_free(untrusted);
383     return ret;
384 }
385
386 bool AbstractPKIXTrustEngine::validate(
387     Signature& sig,
388     const CredentialResolver& credResolver,
389     CredentialCriteria* criteria
390     ) const
391 {
392 #ifdef _DEBUG
393     NDC ndc("validate");
394 #endif
395     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
396
397     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
398     if (!inlineResolver)
399         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
400     if (!inlineResolver) {
401         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
402         return false;
403     }
404
405     // Pull the certificate chain out of the signature.
406     X509Credential* x509cred;
407     auto_ptr<Credential> cred(inlineResolver->resolve(&sig,X509Credential::RESOLVE_CERTS|X509Credential::RESOLVE_CRLS));
408     if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
409         log.error("unable to perform PKIX validation, signature does not contain any certificates");
410         return false;
411     }
412     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
413     if (certs.empty()) {
414         log.error("unable to perform PKIX validation, signature does not contain any certificates");
415         return false;
416     }
417
418     log.debug("validating signature using certificate from within the signature");
419
420     // Find and save off a pointer to the certificate that unlocks the object.
421     // Most of the time, this will be the first one anyway.
422     XSECCryptoX509* certEE=NULL;
423     SignatureValidator keyValidator;
424     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
425         try {
426             auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
427             keyValidator.setKey(key.get());
428             keyValidator.validate(&sig);
429             log.debug("signature verified with key inside signature, attempting certificate validation...");
430             certEE=(*i);
431         }
432         catch (ValidationException& ex) {
433             log.debug(ex.what());
434         }
435     }
436     
437     if (!certEE) {
438         log.debug("failed to verify signature with embedded certificates");
439         return false;
440     }
441     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
442         log.error("only the OpenSSL XSEC provider is supported");
443         return false;
444     }
445
446     STACK_OF(X509)* untrusted=sk_X509_new_null();
447     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); i!=certs.end(); ++i)
448         sk_X509_push(untrusted,static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
449     const vector<XSECCryptoX509CRL*>& crls = x509cred->getCRLs();
450     bool ret = validateWithCRLs(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria, &crls);
451     sk_X509_free(untrusted);
452     return ret;
453 }
454
455 bool AbstractPKIXTrustEngine::validate(
456     const XMLCh* sigAlgorithm,
457     const char* sig,
458     KeyInfo* keyInfo,
459     const char* in,
460     unsigned int in_len,
461     const CredentialResolver& credResolver,
462     CredentialCriteria* criteria
463     ) const
464 {
465 #ifdef _DEBUG
466     NDC ndc("validate");
467 #endif
468     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine.PKIX");
469
470     if (!keyInfo) {
471         log.error("unable to perform PKIX validation, KeyInfo not present");
472         return false;
473     }
474
475     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
476     if (!inlineResolver)
477         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
478     if (!inlineResolver) {
479         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
480         return false;
481     }
482
483     // Pull the certificate chain out of the signature.
484     X509Credential* x509cred;
485     auto_ptr<Credential> cred(inlineResolver->resolve(keyInfo,X509Credential::RESOLVE_CERTS));
486     if (!cred.get() || !(x509cred=dynamic_cast<X509Credential*>(cred.get()))) {
487         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
488         return false;
489     }
490     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
491     if (certs.empty()) {
492         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
493         return false;
494     }
495
496     log.debug("validating signature using certificate from within KeyInfo");
497
498     // Find and save off a pointer to the certificate that unlocks the object.
499     // Most of the time, this will be the first one anyway.
500     XSECCryptoX509* certEE=NULL;
501     for (vector<XSECCryptoX509*>::const_iterator i=certs.begin(); !certEE && i!=certs.end(); ++i) {
502         try {
503             auto_ptr<XSECCryptoKey> key((*i)->clonePublicKey());
504             if (Signature::verifyRawSignature(key.get(), sigAlgorithm, sig, in, in_len)) {
505                 log.debug("signature verified with key inside signature, attempting certificate validation...");
506                 certEE=(*i);
507             }
508         }
509         catch (SignatureException& ex) {
510             log.debug(ex.what());
511         }
512     }
513     
514     if (certEE)
515         return validate(certEE,certs,credResolver,criteria);
516         
517     log.debug("failed to verify signature with embedded certificates");
518     return false;
519 }