Major revamp of credential and trust handling code, PKIX engine still needs work.
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / AbstractPKIXTrustEngine.cpp
1 /*
2  *  Copyright 2001-2007 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 "security/AbstractPKIXTrustEngine.h"
26 #include "signature/KeyInfo.h"
27
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>
39
40 using namespace xmlsignature;
41 using namespace xmltooling;
42 using namespace log4cpp;
43 using namespace std;
44
45
46 namespace {
47     static int XMLTOOL_DLLLOCAL error_callback(int ok, X509_STORE_CTX* ctx)
48     {
49         if (!ok)
50             Category::getInstance("OpenSSL").error("path validation failure: %s", X509_verify_cert_error_string(ctx->error));
51         return ok;
52     }
53
54     static bool XMLTOOL_DLLLOCAL validate(
55         X509* EE, STACK_OF(X509)* untrusted, AbstractPKIXTrustEngine::PKIXValidationInfoIterator* pkixInfo
56         )
57     {
58         Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
59     
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");
62     
63         // We need this for CRL support.
64         X509_STORE* store=X509_STORE_new();
65         if (!store) {
66             log_openssl();
67             return false;
68         }
69     #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
70         X509_STORE_set_flags(store,X509_V_FLAG_CRL_CHECK_ALL);
71     #endif
72     
73         STACK_OF(X509)* CAstack = sk_X509_new_null();
74         
75         // This contains the state of the validate operation.
76         X509_STORE_CTX ctx;
77         
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());
82             }
83         }
84
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) {
88                 // owned by store
89                 X509_STORE_add_crl(store, X509_CRL_dup(static_cast<OpenSSLCryptoX509CRL*>(*j)->getOpenSSLX509CRL()));
90             }
91         }
92      
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) {
96             log_openssl();
97             log.error("unable to initialize X509_STORE_CTX");
98             sk_X509_free(CAstack);
99             X509_STORE_free(store);
100             return false;
101         }
102     #else
103         X509_STORE_CTX_init(&ctx,store,EE,untrusted);
104     #endif
105     
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);
110         
111         int ret=X509_verify_cert(&ctx);
112         if (ret==1) {
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) {
116                 log.error(
117                     "certificate chain was too long (%d intermediates, only %d allowed)",
118                     (depth==-1) ? 0 : depth,
119                     pkixInfo->getVerificationDepth()
120                     );
121                 ret=0;
122             }
123         }
124         
125         // Clean up...
126         X509_STORE_CTX_cleanup(&ctx);
127         X509_STORE_free(store);
128         sk_X509_free(CAstack);
129     
130         if (ret==1) {
131             log.debug("successfully validated certificate chain");
132             return true;
133         }
134         
135         return false;
136     }
137 };
138
139 bool AbstractPKIXTrustEngine::checkEntityNames(
140     X509* certEE, const CredentialResolver& credResolver, const CredentialCriteria& criteria
141     ) const
142 {
143     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
144
145     vector<const Credential*> creds;
146     credResolver.resolve(creds,&criteria);
147
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);
152
153     char buf[256];
154     X509_NAME* subject=X509_get_subject_name(certEE);
155     if (subject) {
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;
165         BIO_flush(b);
166         while ((len = BIO_read(b, buf, 255)) > 0) {
167             buf[len] = '\0';
168             subjectstr+=buf;
169         }
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);
173         BIO_flush(b2);
174         while ((len = BIO_read(b2, buf, 255)) > 0) {
175             buf[len] = '\0';
176             subjectstr2+=buf;
177         }
178         
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())) {
183 #else
184             if (!stricmp(n->c_str(),subjectstr.c_str()) || !stricmp(n->c_str(),subjectstr2.c_str())) {
185 #endif
186                 log.debug("matched full subject DN to a key name (%s)", n->c_str());
187                 BIO_free(b);
188                 BIO_free(b2);
189                 return true;
190             }
191         }
192         BIO_free(b);
193         BIO_free(b2);
194
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);
197         if (altnames) {
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);
204                     
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))
208 #else
209                         if ((check->type==GEN_DNS && !strnicmp(altptr,n->c_str(),altlen))
210 #endif
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);
214                             return true;
215                         }
216                     }
217                 }
218             }
219         }
220         GENERAL_NAMES_free(altnames);
221             
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())) {
228 #else
229                 if (!stricmp(buf,n->c_str())) {
230 #endif
231                     log.debug("matched subject CN to a key name (%s)", n->c_str());
232                     return true;
233                 }
234             }
235         }
236         else
237             log.warn("no common name in certificate subject");
238     }
239     else
240         log.error("certificate has no subject?!");
241     
242     return false;
243 }
244
245 bool AbstractPKIXTrustEngine::validate(
246     X509* certEE,
247     STACK_OF(X509)* certChain,
248     const CredentialResolver& credResolver,
249     CredentialCriteria* criteria
250     ) const
251 {
252 #ifdef _DEBUG
253     NDC ndc("validate");
254 #endif
255     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
256
257     if (!certEE) {
258         log.error("X.509 credential was NULL, unable to perform validation");
259         return false;
260     }
261
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");
268             return false;
269         }
270     }
271     
272     log.debug("performing certificate path validation...");
273
274     auto_ptr<PKIXValidationInfoIterator> pkix(getPKIXValidationInfoIterator(credResolver, criteria, m_keyInfoResolver));
275     while (pkix->next()) {
276         if (::validate(certEE,certChain,pkix.get())) {
277             return true;
278         }
279     }
280
281     log.debug("failed to validate certificate chain using supplied PKIX information");
282     return false;
283 }
284
285 bool AbstractPKIXTrustEngine::validate(
286     XSECCryptoX509* certEE,
287     const vector<XSECCryptoX509*>& certChain,
288     const CredentialResolver& credResolver,
289     CredentialCriteria* criteria
290     ) const
291 {
292 #ifdef _DEBUG
293         NDC ndc("validate");
294 #endif
295     if (!certEE) {
296         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("X.509 credential was NULL, unable to perform validation");
297         return false;
298     }
299     else if (certEE->getProviderName()!=DSIGConstants::s_unicodeStrPROVOpenSSL) {
300         Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine").error("only the OpenSSL XSEC provider is supported");
301         return false;
302     }
303
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());
307
308     bool ret = validate(static_cast<OpenSSLCryptoX509*>(certEE)->getOpenSSLX509(), untrusted, credResolver, criteria);
309     sk_X509_free(untrusted);
310     return ret;
311 }
312
313 bool AbstractPKIXTrustEngine::validate(
314     Signature& sig,
315     const CredentialResolver& credResolver,
316     CredentialCriteria* criteria
317     ) const
318 {
319 #ifdef _DEBUG
320     NDC ndc("validate");
321 #endif
322     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
323
324     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
325     if (!inlineResolver)
326         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
327     if (!inlineResolver) {
328         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
329         return false;
330     }
331
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");
337         return false;
338     }
339     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
340     if (certs.empty()) {
341         log.error("unable to perform PKIX validation, signature does not contain any certificates");
342         return false;
343     }
344
345     log.debug("validating signature using certificate from within the signature");
346
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) {
352         try {
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...");
357             certEE=(*i);
358         }
359         catch (ValidationException& ex) {
360             log.debug(ex.what());
361         }
362     }
363     
364     if (certEE)
365         return validate(certEE,certs,credResolver,criteria);
366         
367     log.debug("failed to verify signature with embedded certificates");
368     return false;
369 }
370
371 bool AbstractPKIXTrustEngine::validate(
372     const XMLCh* sigAlgorithm,
373     const char* sig,
374     KeyInfo* keyInfo,
375     const char* in,
376     unsigned int in_len,
377     const CredentialResolver& credResolver,
378     CredentialCriteria* criteria
379     ) const
380 {
381 #ifdef _DEBUG
382     NDC ndc("validate");
383 #endif
384     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".TrustEngine");
385
386     if (!keyInfo) {
387         log.error("unable to perform PKIX validation, KeyInfo not present");
388         return false;
389     }
390
391     const KeyInfoResolver* inlineResolver = m_keyInfoResolver;
392     if (!inlineResolver)
393         inlineResolver = XMLToolingConfig::getConfig().getKeyInfoResolver();
394     if (!inlineResolver) {
395         log.error("unable to perform PKIX validation, no KeyInfoResolver available");
396         return false;
397     }
398
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");
404         return false;
405     }
406     const vector<XSECCryptoX509*>& certs = x509cred->getEntityCertificateChain();
407     if (certs.empty()) {
408         log.error("unable to perform PKIX validation, KeyInfo does not contain any certificates");
409         return false;
410     }
411
412     log.debug("validating signature using certificate from within KeyInfo");
413
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) {
418         try {
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...");
422                 certEE=(*i);
423             }
424         }
425         catch (SignatureException& ex) {
426             log.debug(ex.what());
427         }
428     }
429     
430     if (certEE)
431         return validate(certEE,certs,credResolver,criteria);
432         
433     log.debug("failed to verify signature with embedded certificates");
434     return false;
435 }