2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * FilesystemCredentialResolver.cpp
20 * Supplies credentials from local files
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"
30 #include "util/XMLHelper.h"
32 #include <sys/types.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>
41 using namespace xmlsignature;
42 using namespace xmltooling;
43 using namespace log4cpp;
46 // OpenSSL password callback...
47 static int passwd_callback(char* buf, int len, int verify, void* passwd)
51 if(passwd && len > strlen(reinterpret_cast<char*>(passwd)))
53 strcpy(buf,reinterpret_cast<char*>(passwd));
60 namespace xmltooling {
62 #if defined (_MSC_VER)
63 #pragma warning( push )
64 #pragma warning( disable : 4250 )
67 class XMLTOOL_DLLLOCAL FilesystemCredentialResolver;
68 class XMLTOOL_DLLLOCAL FilesystemCredential : public OpenSSLCredential, public BasicX509Credential
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);
77 virtual ~FilesystemCredential() {
80 void addKeyNames(const DOMElement* e);
82 void attach(SSL_CTX* ctx) const;
85 FilesystemCredentialResolver* m_resolver;
88 #if defined (_MSC_VER)
89 #pragma warning( pop )
92 class XMLTOOL_DLLLOCAL FilesystemCredentialResolver : public CredentialResolver
95 FilesystemCredentialResolver(const DOMElement* e);
96 virtual ~FilesystemCredentialResolver() {
98 for_each(m_certs.begin(),m_certs.end(),X509_free);
101 Lockable* lock() { return this; }
104 const Credential* resolve(const CredentialCriteria* criteria=NULL) const {
105 return (criteria ? (criteria->matches(*m_credential) ? m_credential : NULL) : m_credential);
108 virtual vector<const Credential*>::size_type resolve(
109 vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
111 if (!criteria || criteria->matches(*m_credential)) {
112 results.push_back(m_credential);
118 void attach(SSL_CTX* ctx) const;
121 XSECCryptoKey* loadKey();
123 enum format_t { PEM=SSL_FILETYPE_PEM, DER=SSL_FILETYPE_ASN1, _PKCS12, UNKNOWN };
125 format_t getEncodingFormat(BIO* in) const;
126 string formatToString(format_t format) const;
127 format_t xmlFormatToFormat(const XMLCh* format_xml) const;
129 format_t m_keyformat;
130 string m_keypath,m_keypass;
131 vector<X509*> m_certs;
132 FilesystemCredential* m_credential;
135 CredentialResolver* XMLTOOL_DLLLOCAL FilesystemCredentialResolverFactory(const DOMElement* const & e)
137 return new FilesystemCredentialResolver(e);
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);
149 FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e) : m_credential(NULL)
152 NDC ndc("FilesystemCredentialResolver");
154 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER);
156 const DOMElement* root=e;
158 XSECCryptoKey* key=NULL;
159 vector<XSECCryptoX509*> xseccerts;
162 const XMLCh* format_xml=NULL;
166 const DOMElement* keynode=XMLHelper::getFirstChildElement(root,Key);
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);
173 const XMLCh* password_xml=keynode->getAttributeNS(NULL,password);
175 auto_ptr_char kp(password_xml);
179 e=XMLHelper::getFirstChildElement(keynode,Path);
180 if (e && e->hasChildNodes()) {
181 const XMLCh* s=e->getFirstChild()->getNodeValue();
182 auto_ptr_char kpath(s);
184 struct _stat stat_buf;
185 if (_stat(kpath.get(), &stat_buf) != 0)
187 struct stat stat_buf;
188 if (stat(kpath.get(), &stat_buf) != 0)
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()));
194 m_keypath=kpath.get();
197 log.error("Path element missing inside Key element");
198 throw XMLSecurityException("FilesystemCredentialResolver can't access key file, no Path element specified.");
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;
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()));
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());
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()));
232 // Check for Certificate
233 e=XMLHelper::getFirstChildElement(root,Certificate);
235 m_credential = new FilesystemCredential(this,key,xseccerts);
236 m_credential->addKeyNames(keynode);
239 auto_ptr_char certpass(e->getAttributeNS(NULL,password));
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");
245 throw XMLSecurityException("FilesystemCredentialResolver can't access certificate file, missing or empty Path element.");
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());
256 throw XMLSecurityException("FilesystemCredentialResolver configuration contains unknown certificate encoding format ($1)",params(1,unknown.get()));
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());
273 while (x=PEM_read_bio_X509(in,NULL,passwd_callback,const_cast<char*>(certpass.get())))
274 m_certs.push_back(x);
278 x=d2i_X509_bio(in,NULL);
280 m_certs.push_back(x);
284 throw XMLSecurityException("FilesystemCredentialResolver unable to load DER certificate from file ($1)",params(1,certpath.get()));
289 p12=d2i_PKCS12_bio(in,NULL);
291 PKCS12_parse(p12, certpass.get(), NULL, &x, NULL);
295 m_certs.push_back(x);
300 throw XMLSecurityException("FilesystemCredentialResolver unable to load PKCS12 certificate from file ($1)",params(1,certpath.get()));
311 throw XMLSecurityException("FilesystemCredentialResolver unable to load certificate(s) from file ($1)",params(1,certpath.get()));
319 throw XMLSecurityException("FilesystemCredentialResolver unable to load any certificate(s)");
321 // Load any extra CA files.
322 const DOMElement* extra=XMLHelper::getFirstChildElement(e,CAPath);
324 if (!extra->hasChildNodes()) {
325 log.warn("skipping empty CAPath element");
326 extra = XMLHelper::getNextSiblingElement(extra,CAPath);
329 auto_ptr_char capath(extra->getFirstChild()->getNodeValue());
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());
342 while (x=PEM_read_bio_X509(in,NULL,passwd_callback,const_cast<char*>(certpass.get())))
343 m_certs.push_back(x);
347 x=d2i_X509_bio(in,NULL);
349 m_certs.push_back(x);
353 throw XMLSecurityException("FilesystemCredentialResolver unable to load DER CA certificate from file ($1)",params(1,capath.get()));
358 p12 = d2i_PKCS12_bio(in, NULL);
360 PKCS12_parse(p12, certpass.get(), NULL, &x, NULL);
364 m_certs.push_back(x);
370 throw XMLSecurityException("FilesystemCredentialResolver unable to load PKCS12 CA certificate from file ($1)",params(1,capath.get()));
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()));
385 extra = XMLHelper::getNextSiblingElement(extra,CAPath);
388 catch (XMLToolingException&) {
390 for_each(m_certs.begin(), m_certs.end(), X509_free);
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);
403 XSECCryptoKey* FilesystemCredentialResolver::loadKey()
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) {
415 pkey=PEM_read_bio_PrivateKey(in, NULL, passwd_callback, const_cast<char*>(m_keypass.c_str()));
419 pkey=d2i_PrivateKey_bio(in, NULL);
423 PKCS12* p12 = d2i_PKCS12_bio(in, NULL);
425 PKCS12_parse(p12, const_cast<char*>(m_keypass.c_str()), &pkey, NULL, NULL);
434 // Now map it to an XSEC wrapper.
436 XSECCryptoKey* ret=NULL;
437 switch (pkey->type) {
439 ret=new OpenSSLCryptoKeyRSA(pkey);
443 ret=new OpenSSLCryptoKeyDSA(pkey);
447 Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER).error("unsupported private key type");
455 throw XMLSecurityException("FilesystemCredentialResolver unable to load private key from file.");
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
465 const int READSIZE = 1;
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");
485 // This is a slight variation of the Java code by Chad La Joie.
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.
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 ) {
503 if ( BIO_seek(in, mark) < 0 ) {
505 throw XMLSecurityException("getEncodingFormat: BIO_seek() can't reset the file position");
512 // Convert key/cert format_t types to a human-meaningful string for debug output
513 string FilesystemCredentialResolver::formatToString(format_t format) const
527 // Convert key/cert raw XML format attribute (XMLCh[]) to format_t type
528 FilesystemCredentialResolver::format_t FilesystemCredentialResolver::xmlFormatToFormat(const XMLCh* format_xml) const
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 };
535 if (!XMLString::compareString(format_xml,cPEM))
537 else if (!XMLString::compareString(format_xml,cDER))
539 else if (!XMLString::compareString(format_xml,cPKCS12))
547 void FilesystemCredentialResolver::attach(SSL_CTX* ctx) const
553 if (m_keypath.empty())
554 throw XMLSecurityException("No key available, unable to attach private key to SSL context.");
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()));
561 switch (m_keyformat) {
563 ret=SSL_CTX_use_PrivateKey_file(ctx, m_keypath.c_str(), m_keyformat);
567 ret=SSL_CTX_use_RSAPrivateKey_file(ctx, m_keypath.c_str(), m_keyformat);
571 BIO* in=BIO_new(BIO_s_file_internal());
572 if (in && BIO_read_filename(in,m_keypath.c_str())>0) {
574 PKCS12* p12 = d2i_PKCS12_bio(in, NULL);
576 PKCS12_parse(p12, const_cast<char*>(m_keypass.c_str()), &pkey, NULL, NULL);
579 ret=SSL_CTX_use_PrivateKey(ctx, pkey);
591 throw XMLSecurityException("Unable to attach private key to SSL context.");
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) {
599 throw XMLSecurityException("Unable to attach client certificate to SSL context.");
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) {
608 throw XMLSecurityException("Unable to attach CA certificate to SSL context.");
614 void FilesystemCredential::addKeyNames(const DOMElement* e)
616 e = e ? XMLHelper::getFirstChildElement(e, Name) : NULL;
618 if (e->hasChildNodes()) {
619 auto_ptr_char n(e->getFirstChild()->getNodeValue());
620 if (n.get() && *n.get())
621 m_keyNames.insert(n.get());
623 e = XMLHelper::getNextSiblingElement(e, Name);
627 void FilesystemCredential::attach(SSL_CTX* ctx) const
629 return m_resolver->attach(ctx);