8e29e8de730b07fb3b3525846a0e6d86539ab7e5
[shibboleth/cpp-xmltooling.git] / xmltooling / security / impl / FilesystemCredentialResolver.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  * FilesystemCredentialResolver.cpp
19  * 
20  * Supplies credentials from local files
21  */
22
23 #include "internal.h"
24 #include "security/BasicX509Credential.h"
25 #include "security/CredentialCriteria.h"
26 #include "security/CredentialResolver.h"
27 #include "security/KeyInfoResolver.h"
28 #include "security/OpenSSLCredential.h"
29 #include "util/NDC.h"
30 #include "util/XMLHelper.h"
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <openssl/pkcs12.h>
35 #include <log4cpp/Category.hh>
36 #include <xercesc/util/XMLUniDefs.hpp>
37 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
38 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyRSA.hpp>
39 #include <xsec/enc/OpenSSL/OpenSSLCryptoKeyDSA.hpp>
40
41 using namespace xmlsignature;
42 using namespace xmltooling;
43 using namespace log4cpp;
44 using namespace std;
45
46 // OpenSSL password callback...
47 static int passwd_callback(char* buf, int len, int verify, void* passwd)
48 {
49     if(!verify)
50     {
51         if(passwd && len > strlen(reinterpret_cast<char*>(passwd)))
52         {
53             strcpy(buf,reinterpret_cast<char*>(passwd));
54             return strlen(buf);
55         }
56     }  
57     return 0;
58 }
59
60 namespace xmltooling {
61
62 #if defined (_MSC_VER)
63     #pragma warning( push )
64     #pragma warning( disable : 4250 )
65 #endif
66
67     class XMLTOOL_DLLLOCAL FilesystemCredentialResolver;
68     class XMLTOOL_DLLLOCAL FilesystemCredential : public OpenSSLCredential, public BasicX509Credential
69     {
70     public:
71         FilesystemCredential(FilesystemCredentialResolver* resolver, XSECCryptoKey* key, const std::vector<XSECCryptoX509*>& xseccerts)
72                 : BasicX509Credential(key, xseccerts), m_resolver(resolver) {
73             if (!m_xseccerts.empty())
74                 extractNames(m_xseccerts.front(), m_keyNames);
75             initKeyInfo();
76         }
77         virtual ~FilesystemCredential() {
78         }
79
80         void addKeyNames(const DOMElement* e);
81
82         void attach(SSL_CTX* ctx) const;
83     
84     private:
85         FilesystemCredentialResolver* m_resolver;
86     };
87
88 #if defined (_MSC_VER)
89     #pragma warning( pop )
90 #endif
91
92     class XMLTOOL_DLLLOCAL FilesystemCredentialResolver : public CredentialResolver
93     {
94     public:
95         FilesystemCredentialResolver(const DOMElement* e);
96         virtual ~FilesystemCredentialResolver() {
97             delete m_credential;
98             for_each(m_certs.begin(),m_certs.end(),X509_free);
99         }
100
101         Lockable* lock() { return this; }
102         void unlock() {}
103         
104         const Credential* resolve(const CredentialCriteria* criteria=NULL) const {
105             return (criteria ? (criteria->matches(*m_credential) ? m_credential : NULL) : m_credential);
106         }
107
108         virtual vector<const Credential*>::size_type resolve(
109             vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
110             ) const {
111             if (!criteria || criteria->matches(*m_credential)) {
112                 results.push_back(m_credential);
113                 return 1;
114             }
115             return 0;
116         }
117
118         void attach(SSL_CTX* ctx) const;
119
120     private:
121         XSECCryptoKey* loadKey();
122         
123         enum format_t { PEM=SSL_FILETYPE_PEM, DER=SSL_FILETYPE_ASN1, _PKCS12, UNKNOWN };
124     
125         format_t getEncodingFormat(BIO* in) const;
126         string formatToString(format_t format) const;
127         format_t xmlFormatToFormat(const XMLCh* format_xml) const;
128     
129         format_t m_keyformat;
130         string m_keypath,m_keypass;
131         vector<X509*> m_certs;
132         FilesystemCredential* m_credential;
133     };
134
135     CredentialResolver* XMLTOOL_DLLLOCAL FilesystemCredentialResolverFactory(const DOMElement* const & e)
136     {
137         return new FilesystemCredentialResolver(e);
138     }
139
140     static const XMLCh CAPath[] =           UNICODE_LITERAL_6(C,A,P,a,t,h);
141     static const XMLCh Certificate[] =      UNICODE_LITERAL_11(C,e,r,t,i,f,i,c,a,t,e);
142     static const XMLCh format[] =           UNICODE_LITERAL_6(f,o,r,m,a,t);
143     static const XMLCh Key[] =              UNICODE_LITERAL_3(K,e,y);
144     static const XMLCh Name[] =             UNICODE_LITERAL_4(N,a,m,e);
145     static const XMLCh password[] =         UNICODE_LITERAL_8(p,a,s,s,w,o,r,d);
146     static const XMLCh Path[] =             UNICODE_LITERAL_4(P,a,t,h);
147 };
148
149 FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) : m_credential(NULL)
150 {
151 #ifdef _DEBUG
152     NDC ndc("FilesystemCredentialResolver");
153 #endif
154     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER);
155
156     const DOMElement* root=e;
157
158     XSECCryptoKey* key=NULL;
159     vector<XSECCryptoX509*> xseccerts;
160
161     format_t fformat;
162     const XMLCh* format_xml=NULL;
163     BIO* in = NULL;
164     
165     // Move to Key
166     const DOMElement* keynode=XMLHelper::getFirstChildElement(root,Key);
167     if (keynode) {
168
169         // Get raw format attrib value, but defer processing til later since may need to 
170         // determine format dynamically, and we need the Path for that.
171         format_xml=keynode->getAttributeNS(NULL,format);
172             
173         const XMLCh* password_xml=keynode->getAttributeNS(NULL,password);
174         if (password_xml) {
175             auto_ptr_char kp(password_xml);
176             m_keypass=kp.get();
177         }
178         
179         e=XMLHelper::getFirstChildElement(keynode,Path);
180         if (e && e->hasChildNodes()) {
181             const XMLCh* s=e->getFirstChild()->getNodeValue();
182             auto_ptr_char kpath(s);
183 #ifdef WIN32
184             struct _stat stat_buf;
185             if (_stat(kpath.get(), &stat_buf) != 0)
186 #else
187             struct stat stat_buf;
188             if (stat(kpath.get(), &stat_buf) != 0)
189 #endif
190             {
191                 log.error("key file (%s) can't be opened", kpath.get());
192                 throw XMLSecurityException("FilesystemCredentialResolver can't access key file ($1)",params(1,kpath.get()));
193             }
194             m_keypath=kpath.get();
195         }
196         else {
197             log.error("Path element missing inside Key element");
198             throw XMLSecurityException("FilesystemCredentialResolver can't access key file, no Path element specified.");
199         }
200
201         // Determine the key encoding format dynamically, if not explicitly specified
202         if (format_xml && *format_xml) {
203             fformat = xmlFormatToFormat(format_xml);
204             if (fformat != UNKNOWN) {
205                 m_keyformat = fformat;
206             }
207             else {
208                 auto_ptr_char unknown(format_xml);
209                 log.error("configuration specifies unknown key encoding format (%s)", unknown.get());
210                 throw XMLSecurityException("FilesystemCredentialResolver configuration contains unknown key encoding format ($1)",params(1,unknown.get()));
211             }
212         }
213         else {
214             in=BIO_new(BIO_s_file_internal());
215             if (in && BIO_read_filename(in,m_keypath.c_str())>0) {
216                 m_keyformat = getEncodingFormat(in);
217                 log.debug("key encoding format for (%s) dynamically resolved as (%s)", m_keypath.c_str(), formatToString(m_keyformat).c_str());
218             }
219             else {
220                 log.error("key file (%s) can't be read to determine encoding format", m_keypath.c_str());
221                 throw XMLSecurityException("FilesystemCredentialResolver can't read key file ($1) to determine encoding format",params(1,m_keypath.c_str()));
222             }
223             if (in)
224                 BIO_free(in);
225             in = NULL;    
226         }
227         
228         // Load the key.
229         key = loadKey();
230     }
231         
232     // Check for Certificate
233     e=XMLHelper::getFirstChildElement(root,Certificate);
234     if (!e) {
235         m_credential = new FilesystemCredential(this,key,xseccerts);
236         m_credential->addKeyNames(keynode);
237         return;
238     }
239     auto_ptr_char certpass(e->getAttributeNS(NULL,password));
240     
241     const DOMElement* ep=XMLHelper::getFirstChildElement(e,Path);
242     if (!ep || !ep->hasChildNodes()) {
243         log.error("Path element missing inside Certificate element or is empty");
244         delete key;
245         throw XMLSecurityException("FilesystemCredentialResolver can't access certificate file, missing or empty Path element.");
246     }
247     
248     auto_ptr_char certpath(ep->getFirstChild()->getNodeValue());
249     format_xml=e->getAttributeNS(NULL,format);
250     if (format_xml && *format_xml) {
251         fformat = xmlFormatToFormat(format_xml);
252         if (fformat == UNKNOWN) {
253             auto_ptr_char unknown(format_xml);
254             log.error("configuration specifies unknown certificate encoding format (%s)", unknown.get());
255             delete key;
256             throw XMLSecurityException("FilesystemCredentialResolver configuration contains unknown certificate encoding format ($1)",params(1,unknown.get()));
257         }
258     }
259     
260     try {
261         X509* x=NULL;
262         PKCS12* p12=NULL;
263         in=BIO_new(BIO_s_file_internal());
264         if (in && BIO_read_filename(in,certpath.get())>0) {
265             if (!format_xml || !*format_xml) {
266                 // Determine the cert encoding format dynamically, if not explicitly specified
267                 fformat = getEncodingFormat(in);
268                 log.debug("certificate encoding format for (%s) dynamically resolved as (%s)", certpath.get(), formatToString(fformat).c_str());
269             }
270
271             switch(fformat) {
272                 case PEM:
273                     while (x=PEM_read_bio_X509(in,NULL,passwd_callback,const_cast<char*>(certpass.get())))
274                         m_certs.push_back(x);
275                     break;
276                                 
277                 case DER:
278                     x=d2i_X509_bio(in,NULL);
279                     if (x)
280                         m_certs.push_back(x);
281                     else {
282                         log_openssl();
283                         BIO_free(in);
284                         throw XMLSecurityException("FilesystemCredentialResolver unable to load DER certificate from file ($1)",params(1,certpath.get()));
285                     }
286                     break;
287
288                 case _PKCS12:
289                     p12=d2i_PKCS12_bio(in,NULL);
290                     if (p12) {
291                         PKCS12_parse(p12, certpass.get(), NULL, &x, NULL);
292                         PKCS12_free(p12);
293                     }
294                     if (x) {
295                         m_certs.push_back(x);
296                         x=NULL;
297                     } else {
298                         log_openssl();
299                         BIO_free(in);
300                         throw XMLSecurityException("FilesystemCredentialResolver unable to load PKCS12 certificate from file ($1)",params(1,certpath.get()));
301                     }
302                     break;
303             } // end switch
304
305         } else {
306             log_openssl();
307             if (in) {
308                 BIO_free(in);
309                 in=NULL;
310             }
311             throw XMLSecurityException("FilesystemCredentialResolver unable to load certificate(s) from file ($1)",params(1,certpath.get()));
312         }
313         if (in) {
314             BIO_free(in);
315             in=NULL;
316         }
317
318         if (m_certs.empty())
319             throw XMLSecurityException("FilesystemCredentialResolver unable to load any certificate(s)");
320
321         // Load any extra CA files.
322         const DOMElement* extra=XMLHelper::getFirstChildElement(e,CAPath);
323         while (extra) {
324             if (!extra->hasChildNodes()) {
325                 log.warn("skipping empty CAPath element");
326                 extra = XMLHelper::getNextSiblingElement(extra,CAPath);
327                 continue;
328             }
329             auto_ptr_char capath(extra->getFirstChild()->getNodeValue());
330             x=NULL;
331             p12=NULL;
332             in=BIO_new(BIO_s_file_internal());
333             if (in && BIO_read_filename(in,capath.get())>0) {
334                 if (!format_xml || !*format_xml) {
335                     // Determine the cert encoding format dynamically, if not explicitly specified
336                     fformat = getEncodingFormat(in);
337                     log.debug("CA certificate encoding format for (%s) dynamically resolved as (%s)", certpath.get(), formatToString(fformat).c_str());
338                 }
339
340                 switch (fformat) {
341                     case PEM:
342                         while (x=PEM_read_bio_X509(in,NULL,passwd_callback,const_cast<char*>(certpass.get())))
343                             m_certs.push_back(x);
344                         break;
345
346                     case DER:
347                         x=d2i_X509_bio(in,NULL);
348                         if (x)
349                             m_certs.push_back(x);
350                         else {
351                             log_openssl();
352                             BIO_free(in);
353                             throw XMLSecurityException("FilesystemCredentialResolver unable to load DER CA certificate from file ($1)",params(1,capath.get()));
354                         }
355                         break;
356
357                     case _PKCS12:
358                         p12 = d2i_PKCS12_bio(in, NULL);
359                         if (p12) {
360                             PKCS12_parse(p12, certpass.get(), NULL, &x, NULL);
361                             PKCS12_free(p12);
362                         }
363                         if (x) {
364                             m_certs.push_back(x);
365                             x=NULL;
366                         }
367                         else {
368                             log_openssl();
369                             BIO_free(in);
370                             throw XMLSecurityException("FilesystemCredentialResolver unable to load PKCS12 CA certificate from file ($1)",params(1,capath.get()));
371                         }
372                         break;
373                 } //end switch
374
375                 BIO_free(in);
376             }
377             else {
378                 if (in)
379                     BIO_free(in);
380                 log_openssl();
381                 log.error("CA file (%s) can't be opened", capath.get());
382                 throw XMLSecurityException("FilesystemCredentialResolver can't open CA file ($1)",params(1,capath.get()));
383             }
384             
385             extra = XMLHelper::getNextSiblingElement(extra,CAPath);
386         }
387     }
388     catch (XMLToolingException&) {
389         delete key;
390         for_each(m_certs.begin(), m_certs.end(), X509_free);
391         throw;
392     }
393
394     // Reflect certs over to XSEC form and wrap with credential object.
395     for (vector<X509*>::iterator j=m_certs.begin(); j!=m_certs.end(); j++)
396         xseccerts.push_back(new OpenSSLCryptoX509(*j));
397     if (!key && !xseccerts.empty())
398         key = xseccerts.front()->clonePublicKey();
399     m_credential = new FilesystemCredential(this, key, xseccerts);
400     m_credential->addKeyNames(keynode);
401 }
402
403 XSECCryptoKey* FilesystemCredentialResolver::loadKey()
404 {
405 #ifdef _DEBUG
406     NDC ndc("loadKey");
407 #endif
408
409     // Get a EVP_PKEY.
410     EVP_PKEY* pkey=NULL;
411     BIO* in=BIO_new(BIO_s_file_internal());
412     if (in && BIO_read_filename(in,m_keypath.c_str())>0) {
413         switch (m_keyformat) {
414             case PEM:
415                 pkey=PEM_read_bio_PrivateKey(in, NULL, passwd_callback, const_cast<char*>(m_keypass.c_str()));
416                 break;
417             
418             case DER:
419                 pkey=d2i_PrivateKey_bio(in, NULL);
420                 break;
421                 
422             default: {
423                 PKCS12* p12 = d2i_PKCS12_bio(in, NULL);
424                 if (p12) {
425                     PKCS12_parse(p12, const_cast<char*>(m_keypass.c_str()), &pkey, NULL, NULL);
426                     PKCS12_free(p12);
427                 }
428             }
429         }
430     }
431     if (in)
432         BIO_free(in);
433     
434     // Now map it to an XSEC wrapper.
435     if (pkey) {
436         XSECCryptoKey* ret=NULL;
437         switch (pkey->type) {
438             case EVP_PKEY_RSA:
439                 ret=new OpenSSLCryptoKeyRSA(pkey);
440                 break;
441                 
442             case EVP_PKEY_DSA:
443                 ret=new OpenSSLCryptoKeyDSA(pkey);
444                 break;
445             
446             default:
447                 Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER).error("unsupported private key type");
448         }
449         EVP_PKEY_free(pkey);
450         if (ret)
451             return ret;
452     }
453
454     log_openssl();
455     throw XMLSecurityException("FilesystemCredentialResolver unable to load private key from file."); 
456 }
457
458 // Used to determine the encoding format of credentials files
459 // dynamically. Supports: PEM, DER, PKCS12.
460 FilesystemCredentialResolver::format_t FilesystemCredentialResolver::getEncodingFormat(BIO* in) const
461 {
462     PKCS12* p12 = NULL;
463     format_t format;
464
465     const int READSIZE = 1;
466     char buf[READSIZE];
467     char b1;
468     int mark;
469
470     try {
471         if ( (mark = BIO_tell(in)) < 0 ) 
472             throw XMLSecurityException("getEncodingFormat: BIO_tell() can't get the file position");
473         if ( BIO_read(in, buf, READSIZE) <= 0 ) 
474             throw XMLSecurityException("getEncodingFormat: BIO_read() can't read from the stream");
475         if ( BIO_seek(in, mark) < 0 ) 
476             throw XMLSecurityException("getEncodingFormat: BIO_seek() can't reset the file position");
477     }
478     catch (...) {
479         log_openssl();
480         throw;
481     }
482
483     b1 = buf[0];
484
485     // This is a slight variation of the Java code by Chad La Joie.
486     //
487     // Check the first byte of the file.  If it's some kind of
488     // DER-encoded structure (including PKCS12), it will begin with ASCII 048.
489     // Otherwise, assume it's PEM.
490     if (b1 !=  48) {
491         format = PEM;
492     } else {
493         // Here we know it's DER-encoded, now try to parse it as a PKCS12
494         // ASN.1 structure.  If it fails, must be another kind of DER-encoded
495         // key/cert structure.  A little inefficient...but it works.
496         if ( (p12=d2i_PKCS12_bio(in,NULL)) == NULL ) {
497             format = DER;
498         } else {
499             format = _PKCS12;
500         }
501         if (p12)
502             PKCS12_free(p12);    
503         if ( BIO_seek(in, mark) < 0 ) {
504             log_openssl();
505             throw XMLSecurityException("getEncodingFormat: BIO_seek() can't reset the file position");
506         }
507     }
508
509     return format;
510 }
511
512 // Convert key/cert format_t types to a human-meaningful string for debug output
513 string FilesystemCredentialResolver::formatToString(format_t format) const
514 {
515     switch(format) {
516         case PEM:
517             return "PEM";
518         case DER:
519             return "DER";
520         case _PKCS12:
521             return "PKCS12";
522         default:
523             return "UNKNOWN";
524     }
525 }
526
527 // Convert key/cert raw XML format attribute (XMLCh[]) to format_t type
528 FilesystemCredentialResolver::format_t FilesystemCredentialResolver::xmlFormatToFormat(const XMLCh* format_xml) const
529 {
530     static const XMLCh cPEM[] = UNICODE_LITERAL_3(P,E,M);
531     static const XMLCh cDER[] = UNICODE_LITERAL_3(D,E,R);
532     static const XMLCh cPKCS12[] = { chLatin_P, chLatin_K, chLatin_C, chLatin_S, chDigit_1, chDigit_2, chNull };
533     format_t format;
534
535     if (!XMLString::compareString(format_xml,cPEM))
536         format=PEM;
537     else if (!XMLString::compareString(format_xml,cDER))
538         format=DER;
539     else if (!XMLString::compareString(format_xml,cPKCS12))
540         format=_PKCS12;
541     else
542         format=UNKNOWN;
543
544     return format;
545 }
546
547 void FilesystemCredentialResolver::attach(SSL_CTX* ctx) const
548 {
549 #ifdef _DEBUG
550     NDC ndc("attach");
551 #endif
552
553     if (m_keypath.empty())
554         throw XMLSecurityException("No key available, unable to attach private key to SSL context.");
555
556     // Attach key.
557     SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
558     SSL_CTX_set_default_passwd_cb_userdata(ctx, const_cast<char*>(m_keypass.c_str()));
559
560     int ret=0;
561     switch (m_keyformat) {
562         case PEM:
563             ret=SSL_CTX_use_PrivateKey_file(ctx, m_keypath.c_str(), m_keyformat);
564             break;
565             
566         case DER:
567             ret=SSL_CTX_use_RSAPrivateKey_file(ctx, m_keypath.c_str(), m_keyformat);
568             break;
569             
570         default: {
571             BIO* in=BIO_new(BIO_s_file_internal());
572             if (in && BIO_read_filename(in,m_keypath.c_str())>0) {
573                 EVP_PKEY* pkey=NULL;
574                 PKCS12* p12 = d2i_PKCS12_bio(in, NULL);
575                 if (p12) {
576                     PKCS12_parse(p12, const_cast<char*>(m_keypass.c_str()), &pkey, NULL, NULL);
577                     PKCS12_free(p12);
578                     if (pkey) {
579                         ret=SSL_CTX_use_PrivateKey(ctx, pkey);
580                         EVP_PKEY_free(pkey);
581                     }
582                 }
583             }
584             if (in)
585                 BIO_free(in);
586         }
587     }
588     
589     if (ret!=1) {
590         log_openssl();
591         throw XMLSecurityException("Unable to attach private key to SSL context.");
592     }
593
594     // Attach certs.
595     for (vector<X509*>::const_iterator i=m_certs.begin(); i!=m_certs.end(); i++) {
596         if (i==m_certs.begin()) {
597             if (SSL_CTX_use_certificate(ctx, *i) != 1) {
598                 log_openssl();
599                 throw XMLSecurityException("Unable to attach client certificate to SSL context.");
600             }
601         }
602         else {
603             // When we add certs, they don't get ref counted, so we need to duplicate them.
604             X509* dup = X509_dup(*i);
605             if (SSL_CTX_add_extra_chain_cert(ctx, dup) != 1) {
606                 X509_free(dup);
607                 log_openssl();
608                 throw XMLSecurityException("Unable to attach CA certificate to SSL context.");
609             }
610         }
611     }
612 }
613
614 void FilesystemCredential::addKeyNames(const DOMElement* e)
615 {
616     e = e ? XMLHelper::getFirstChildElement(e, Name) : NULL;
617     while (e) {
618         if (e->hasChildNodes()) {
619             auto_ptr_char n(e->getFirstChild()->getNodeValue());
620             if (n.get() && *n.get())
621                 m_keyNames.insert(n.get());
622         }
623         e = XMLHelper::getNextSiblingElement(e, Name);
624     }
625 }
626
627 void FilesystemCredential::attach(SSL_CTX* ctx) const
628 {
629     return m_resolver->attach(ctx);
630 }