2 * Copyright 2001-2008 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
25 #include "security/BasicX509Credential.h"
26 #include "security/CredentialCriteria.h"
27 #include "security/CredentialResolver.h"
28 #include "security/KeyInfoResolver.h"
29 #include "security/OpenSSLCredential.h"
30 #include "security/SecurityHelper.h"
32 #include "util/PathResolver.h"
33 #include "util/XMLHelper.h"
35 #include <sys/types.h>
37 #include <openssl/pkcs12.h>
38 #include <xercesc/util/XMLUniDefs.hpp>
39 #include <xsec/enc/OpenSSL/OpenSSLCryptoX509.hpp>
41 using namespace xmlsignature;
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
46 using xercesc::DOMElement;
47 using xercesc::chLatin_f;
48 using xercesc::chDigit_0;
50 namespace xmltooling {
52 // The ManagedResource classes handle memory management, loading of the files
53 // and staleness detection. A copy of the active objects is always stored in
56 class XMLTOOL_DLLLOCAL ManagedResource {
58 ManagedResource() : local(true), reloadChanges(true), filestamp(0), reloadInterval(0) {}
61 SOAPTransport* getTransport() {
62 SOAPTransport::Address addr("FilesystemCredentialResolver", source.c_str(), source.c_str());
63 string scheme(addr.m_endpoint, strchr(addr.m_endpoint,':') - addr.m_endpoint);
64 return XMLToolingConfig::getConfig().SOAPTransportManager.newPlugin(scheme.c_str(), addr);
68 bool stale(Category& log, RWLock* lock=NULL) {
71 struct _stat stat_buf;
72 if (_stat(source.c_str(), &stat_buf) != 0)
76 if (stat(source.c_str(), &stat_buf) != 0)
79 if (filestamp >= stat_buf.st_mtime)
82 // If necessary, elevate lock and recheck.
84 log.debug("timestamp of local resource changed, elevating to a write lock");
87 if (filestamp >= stat_buf.st_mtime) {
88 // Somebody else handled it, just downgrade.
89 log.debug("update of local resource handled by another thread, downgrading lock");
96 // Update the timestamp regardless. No point in repeatedly trying.
97 filestamp = stat_buf.st_mtime;
98 log.info("change detected, reloading local resource...");
101 time_t now = time(NULL);
104 if (now - filestamp < reloadInterval)
107 // If necessary, elevate lock and recheck.
109 log.debug("reload interval for remote resource elapsed, elevating to a write lock");
112 if (now - filestamp < reloadInterval) {
113 // Somebody else handled it, just downgrade.
114 log.debug("update of remote resource handled by another thread, downgrading lock");
122 log.info("reloading remote resource...");
127 bool local,reloadChanges;
128 string format,source,backing;
129 time_t filestamp,reloadInterval;
132 class XMLTOOL_DLLLOCAL ManagedKey : public ManagedResource {
134 ManagedKey() : key(NULL) {}
135 ~ManagedKey() { delete key; }
136 void load(Category& log, const char* password) {
139 XSECCryptoKey* nkey=NULL;
141 nkey = SecurityHelper::loadKeyFromFile(source.c_str(), format.c_str(), password);
144 auto_ptr<SOAPTransport> t(getTransport());
145 log.info("loading private key from URL (%s)", source.c_str());
146 nkey = SecurityHelper::loadKeyFromURL(*t.get(), backing.c_str(), format.c_str(), password);
151 format = SecurityHelper::guessEncodingFormat(local ? source.c_str() : backing.c_str());
157 class XMLTOOL_DLLLOCAL ManagedCert : public ManagedResource {
160 ~ManagedCert() { for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>()); }
161 void load(Category& log, const char* password) {
164 vector<XSECCryptoX509*> ncerts;
166 SecurityHelper::loadCertificatesFromFile(ncerts, source.c_str(), format.c_str(), password);
169 auto_ptr<SOAPTransport> t(getTransport());
170 log.info("loading certificate(s) from URL (%s)", source.c_str());
171 SecurityHelper::loadCertificatesFromURL(ncerts, *t.get(), backing.c_str(), format.c_str(), password);
173 for_each(certs.begin(), certs.end(), xmltooling::cleanup<XSECCryptoX509>());
176 format = SecurityHelper::guessEncodingFormat(local ? source.c_str() : backing.c_str());
178 vector<XSECCryptoX509*> certs;
181 class XMLTOOL_DLLLOCAL ManagedCRL : public ManagedResource {
184 ~ManagedCRL() { for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>()); }
185 void load(Category& log) {
188 vector<XSECCryptoX509CRL*> ncrls;
190 SecurityHelper::loadCRLsFromFile(ncrls, source.c_str(), format.c_str());
193 auto_ptr<SOAPTransport> t(getTransport());
194 log.info("loading CRL(s) from URL (%s)", source.c_str());
195 SecurityHelper::loadCRLsFromURL(ncrls, *t.get(), backing.c_str(), format.c_str());
197 for_each(crls.begin(), crls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
200 format = SecurityHelper::guessEncodingFormat(local ? source.c_str() : backing.c_str());
202 vector<XSECCryptoX509CRL*> crls;
205 class XMLTOOL_DLLLOCAL FilesystemCredential;
206 class XMLTOOL_DLLLOCAL FilesystemCredentialResolver : public CredentialResolver
208 friend class XMLTOOL_DLLLOCAL FilesystemCredential;
210 FilesystemCredentialResolver(const DOMElement* e);
211 virtual ~FilesystemCredentialResolver();
218 const Credential* resolve(const CredentialCriteria* criteria=NULL) const;
220 virtual vector<const Credential*>::size_type resolve(
221 vector<const Credential*>& results, const CredentialCriteria* criteria=NULL
225 Credential* getCredential();
228 Credential* m_credential;
229 string m_keypass,m_certpass;
230 unsigned int m_keyinfomask,m_usage;
231 vector<string> m_keynames;
234 vector<ManagedCert> m_certs;
235 vector<ManagedCRL> m_crls;
238 #if defined (_MSC_VER)
239 #pragma warning( push )
240 #pragma warning( disable : 4250 )
243 class XMLTOOL_DLLLOCAL FilesystemCredential : public OpenSSLCredential, public BasicX509Credential
246 FilesystemCredential(
247 FilesystemCredentialResolver* resolver, XSECCryptoKey* key, const vector<XSECCryptoX509*>& xseccerts, const vector<XSECCryptoX509CRL*>& crls
248 ) : BasicX509Credential(key ? key : (xseccerts.empty() ? NULL : xseccerts.front()->clonePublicKey()), xseccerts, crls), m_resolver(resolver) {
250 m_keyNames.insert(m_resolver->m_keynames.begin(), m_resolver->m_keynames.end());
253 virtual ~FilesystemCredential() {
256 unsigned int getUsage() const {
257 return m_resolver->m_usage;
260 void initKeyInfo(unsigned int types=0) {
261 BasicX509Credential::initKeyInfo(types);
264 void attach(SSL_CTX* ctx) const;
267 FilesystemCredentialResolver* m_resolver;
270 #if defined (_MSC_VER)
271 #pragma warning( pop )
274 CredentialResolver* XMLTOOL_DLLLOCAL FilesystemCredentialResolverFactory(const DOMElement* const & e)
276 return new FilesystemCredentialResolver(e);
279 static const XMLCh backingFilePath[] = UNICODE_LITERAL_15(b,a,c,k,i,n,g,F,i,l,e,P,a,t,h);
280 static const XMLCh _CredentialResolver[] = UNICODE_LITERAL_18(C,r,e,d,e,n,t,i,a,l,R,e,s,o,l,v,e,r);
281 static const XMLCh CAPath[] = UNICODE_LITERAL_6(C,A,P,a,t,h);
282 static const XMLCh Certificate[] = UNICODE_LITERAL_11(C,e,r,t,i,f,i,c,a,t,e);
283 static const XMLCh _certificate[] = UNICODE_LITERAL_11(c,e,r,t,i,f,i,c,a,t,e);
284 static const XMLCh CRL[] = UNICODE_LITERAL_3(C,R,L);
285 static const XMLCh _format[] = UNICODE_LITERAL_6(f,o,r,m,a,t);
286 static const XMLCh Key[] = UNICODE_LITERAL_3(K,e,y);
287 static const XMLCh _key[] = UNICODE_LITERAL_3(k,e,y);
288 static const XMLCh keyInfoMask[] = UNICODE_LITERAL_11(k,e,y,I,n,f,o,M,a,s,k);
289 static const XMLCh keyName[] = UNICODE_LITERAL_7(k,e,y,N,a,m,e);
290 static const XMLCh Name[] = UNICODE_LITERAL_4(N,a,m,e);
291 static const XMLCh password[] = UNICODE_LITERAL_8(p,a,s,s,w,o,r,d);
292 static const XMLCh Path[] = UNICODE_LITERAL_4(P,a,t,h);
293 static const XMLCh _reloadChanges[] = UNICODE_LITERAL_13(r,e,l,o,a,d,C,h,a,n,g,e,s);
294 static const XMLCh _reloadInterval[] = UNICODE_LITERAL_14(r,e,l,o,a,d,I,n,t,e,r,v,a,l);
295 static const XMLCh _URL[] = UNICODE_LITERAL_3(U,R,L);
296 static const XMLCh _use[] = UNICODE_LITERAL_3(u,s,e);
299 FilesystemCredentialResolver::FilesystemCredentialResolver(const DOMElement* e)
300 : m_lock(NULL), m_credential(NULL), m_usage(Credential::UNSPECIFIED_CREDENTIAL)
303 NDC ndc("FilesystemCredentialResolver");
305 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER);
307 // Default to disable X509IssuerSerial due to schema validation issues.
309 Credential::KEYINFO_KEY_NAME |
310 Credential::KEYINFO_KEY_VALUE |
311 X509Credential::KEYINFO_X509_CERTIFICATE |
312 X509Credential::KEYINFO_X509_SUBJECTNAME;
313 if (e && e->hasAttributeNS(NULL,keyInfoMask))
314 m_keyinfomask = XMLString::parseInt(e->getAttributeNS(NULL,keyInfoMask));
316 if (e && (e->hasAttributeNS(NULL,_certificate) || e->hasAttributeNS(NULL,_key))) {
317 // Dummy up a simple file resolver config using these attributes.
318 DOMElement* dummy = e->getOwnerDocument()->createElementNS(NULL,_CredentialResolver);
321 if (e->hasAttributeNS(NULL,_key)) {
322 child = e->getOwnerDocument()->createElementNS(NULL,Key);
323 dummy->appendChild(child);
324 path = e->getOwnerDocument()->createElementNS(NULL,Path);
325 child->appendChild(path);
326 path->appendChild(e->getOwnerDocument()->createTextNode(e->getAttributeNS(NULL,_key)));
327 if (e->hasAttributeNS(NULL,password))
328 child->setAttributeNS(NULL,password,e->getAttributeNS(NULL,password));
329 if (e->hasAttributeNS(NULL,keyName)) {
330 path = e->getOwnerDocument()->createElementNS(NULL,Name);
331 child->appendChild(path);
332 path->appendChild(e->getOwnerDocument()->createTextNode(e->getAttributeNS(NULL,keyName)));
335 if (e->hasAttributeNS(NULL,_certificate)) {
336 child = e->getOwnerDocument()->createElementNS(NULL,Certificate);
337 dummy->appendChild(child);
338 path = e->getOwnerDocument()->createElementNS(NULL,Path);
339 child->appendChild(path);
340 path->appendChild(e->getOwnerDocument()->createTextNode(e->getAttributeNS(NULL,_certificate)));
342 e = dummy; // reset "root" to the dummy config element
346 const DOMElement* root = e;
348 // Save off usage flags.
349 const XMLCh* usage = root->getAttributeNS(NULL,_use);
350 if (usage && *usage) {
351 auto_ptr_char u(usage);
352 if (!strcmp(u.get(), "signing"))
353 m_usage = Credential::SIGNING_CREDENTIAL | Credential::TLS_CREDENTIAL;
354 else if (!strcmp(u.get(), "TLS"))
355 m_usage = Credential::TLS_CREDENTIAL;
356 else if (!strcmp(u.get(), "encryption"))
357 m_usage = Credential::ENCRYPTION_CREDENTIAL;
361 const DOMElement* keynode = XMLHelper::getFirstChildElement(root,Key);
363 prop = keynode->getAttributeNS(NULL,_format);
365 auto_ptr_char f(prop);
366 m_key.format = f.get();
369 prop = keynode->getAttributeNS(NULL,password);
371 auto_ptr_char kp(prop);
372 m_keypass = kp.get();
375 if ((e=XMLHelper::getFirstChildElement(keynode,Path)) && e->hasChildNodes()) {
376 prop = e->getFirstChild()->getNodeValue();
377 auto_ptr_char kpath(prop);
378 m_key.source = kpath.get();
379 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_key.source, PathResolver::XMLTOOLING_CFG_FILE);
381 prop = e->getAttributeNS(NULL,_reloadChanges);
382 if (prop && (*prop==chLatin_f) || (*prop==chDigit_0))
383 m_key.reloadChanges = false;
385 else if ((e=XMLHelper::getFirstChildElement(keynode,_URL)) && e->hasChildNodes()) {
386 prop = e->getFirstChild()->getNodeValue();
387 auto_ptr_char kpath(prop);
388 m_key.source = kpath.get();
390 prop = e->getAttributeNS(NULL,backingFilePath);
392 throw XMLSecurityException("FilesystemCredentialResolver can't access key, backingFilePath missing from URL element.");
393 auto_ptr_char b(prop);
394 m_key.backing = b.get();
395 XMLToolingConfig::getConfig().getPathResolver()->resolve(m_key.backing, PathResolver::XMLTOOLING_RUN_FILE);
396 prop = e->getAttributeNS(NULL,_reloadInterval);
398 m_key.reloadInterval = XMLString::parseInt(prop);
401 log.error("Path/URL element missing inside Key element");
402 throw XMLSecurityException("FilesystemCredentialResolver can't access key, no Path or URL element specified.");
405 e = XMLHelper::getFirstChildElement(keynode, Name);
407 if (e->hasChildNodes()) {
408 auto_ptr_char n(e->getFirstChild()->getNodeValue());
409 if (n.get() && *n.get())
410 m_keynames.push_back(n.get());
412 e = XMLHelper::getNextSiblingElement(e, Name);
417 const DOMElement* crlnode = XMLHelper::getFirstChildElement(root,CRL);
419 const XMLCh* crlformat = crlnode->getAttributeNS(NULL,_format);
420 e = XMLHelper::getFirstChildElement(crlnode,Path);
422 if (e->hasChildNodes()) {
423 m_crls.push_back(ManagedCRL());
424 ManagedResource& crl = m_crls.back();
425 if (crlformat && *crlformat) {
426 auto_ptr_char f(crlformat);
427 crl.format = f.get();
429 prop = e->getFirstChild()->getNodeValue();
430 auto_ptr_char crlpath(prop);
431 crl.source = crlpath.get();
432 XMLToolingConfig::getConfig().getPathResolver()->resolve(crl.source, PathResolver::XMLTOOLING_CFG_FILE);
434 prop = e->getAttributeNS(NULL,_reloadChanges);
435 if (prop && (*prop==chLatin_f) || (*prop==chDigit_0))
436 crl.reloadChanges = false;
438 e = XMLHelper::getNextSiblingElement(e,Path);
441 e=XMLHelper::getFirstChildElement(crlnode,_URL);
443 if (e->hasChildNodes()) {
444 m_crls.push_back(ManagedCRL());
445 ManagedResource& crl = m_crls.back();
446 if (crlformat && *crlformat) {
447 auto_ptr_char f(crlformat);
448 crl.format = f.get();
450 prop = e->getFirstChild()->getNodeValue();
451 auto_ptr_char crlpath(prop);
452 crl.source = crlpath.get();
454 prop = e->getAttributeNS(NULL,backingFilePath);
456 throw XMLSecurityException("FilesystemCredentialResolver can't access CRL, backingFilePath missing from URL element.");
457 auto_ptr_char b(prop);
458 crl.backing = b.get();
459 XMLToolingConfig::getConfig().getPathResolver()->resolve(crl.backing, PathResolver::XMLTOOLING_RUN_FILE);
460 prop = e->getAttributeNS(NULL,_reloadInterval);
462 crl.reloadInterval = XMLString::parseInt(prop);
464 e = XMLHelper::getNextSiblingElement(e,_URL);
466 if (m_crls.empty()) {
467 log.error("Path/URL element missing inside CRL element");
468 throw XMLSecurityException("FilesystemCredentialResolver can't access CRL, no Path or URL element specified.");
472 // Check for Certificate
473 DOMElement* certnode = XMLHelper::getFirstChildElement(root,Certificate);
475 prop = certnode->getAttributeNS(NULL,password);
477 auto_ptr_char certpass(prop);
478 m_certpass = certpass.get();
481 const XMLCh* certformat = certnode->getAttributeNS(NULL,_format);
483 e = XMLHelper::getFirstChildElement(certnode);
485 if (e->hasChildNodes() && (XMLString::equals(e->getLocalName(), Path) || XMLString::equals(e->getLocalName(), CAPath))) {
486 m_certs.push_back(ManagedCert());
487 ManagedResource& cert = m_certs.back();
488 if (certformat && *certformat) {
489 auto_ptr_char f(certformat);
490 cert.format = f.get();
492 prop = e->getFirstChild()->getNodeValue();
493 auto_ptr_char certpath(prop);
494 cert.source = certpath.get();
495 XMLToolingConfig::getConfig().getPathResolver()->resolve(cert.source, PathResolver::XMLTOOLING_CFG_FILE);
497 prop = e->getAttributeNS(NULL,_reloadChanges);
498 if (prop && (*prop==chLatin_f) || (*prop==chDigit_0))
499 cert.reloadChanges = false;
501 else if (e->hasChildNodes() && XMLString::equals(e->getLocalName(), _URL)) {
502 m_certs.push_back(ManagedCert());
503 ManagedResource& cert = m_certs.back();
504 if (certformat && *certformat) {
505 auto_ptr_char f(certformat);
506 cert.format = f.get();
508 prop = e->getFirstChild()->getNodeValue();
509 auto_ptr_char certpath(prop);
510 cert.source = certpath.get();
512 prop = e->getAttributeNS(NULL,backingFilePath);
514 throw XMLSecurityException("FilesystemCredentialResolver can't access certificate, backingFilePath missing from URL element.");
515 auto_ptr_char b(prop);
516 cert.backing = b.get();
517 XMLToolingConfig::getConfig().getPathResolver()->resolve(cert.backing, PathResolver::XMLTOOLING_RUN_FILE);
518 prop = e->getAttributeNS(NULL,_reloadInterval);
520 cert.reloadInterval = XMLString::parseInt(prop);
522 e = XMLHelper::getNextSiblingElement(e);
524 if (m_certs.empty()) {
525 log.error("Path/URL element missing inside Certificate element");
526 throw XMLSecurityException("FilesystemCredentialResolver can't access certificate, no Path or URL element specified.");
530 // Do an initial load of all the objects. If anything blows up here, whatever's
531 // been loaded should be freed during teardown of the embedded objects.
532 time_t now = time(NULL);
533 m_key.filestamp = now;
534 m_key.load(log, m_keypass.c_str());
535 for (vector<ManagedCert>::iterator i = m_certs.begin(); i != m_certs.end(); ++i) {
536 i->load(log, (i==m_certs.begin()) ? m_certpass.c_str() : NULL);
539 for (vector<ManagedCRL>::iterator j = m_crls.begin(); j != m_crls.end(); ++j) {
544 // Load it all into a credential object and then create the lock.
545 auto_ptr<Credential> credential(getCredential());
546 m_lock = RWLock::create();
547 m_credential = credential.release();
550 FilesystemCredentialResolver::~FilesystemCredentialResolver()
556 Credential* FilesystemCredentialResolver::getCredential()
558 // First, verify that the key and certificate match.
559 if (m_key.key && !m_certs.empty()) {
560 auto_ptr<XSECCryptoKey> temp(m_certs.front().certs.front()->clonePublicKey());
561 if (!SecurityHelper::matches(m_key.key, temp.get()))
562 throw XMLSecurityException("FilesystemCredentialResolver given mismatched key/certificate, check for consistency.");
565 // We (unfortunately) need to duplicate all the objects and put them in one set of arrays
566 // in order to create the credential wrapper.
567 FilesystemCredential* credential=NULL;
568 auto_ptr<XSECCryptoKey> xseckey(m_key.key ? m_key.key->clone() : NULL);
569 vector<XSECCryptoX509*> xseccerts;
570 vector<XSECCryptoX509CRL*> xseccrls;
572 for (vector<ManagedCert>::iterator i = m_certs.begin(); i != m_certs.end(); ++i) {
573 for (vector<XSECCryptoX509*>::const_iterator y = i->certs.begin(); y != i->certs.end(); ++y)
574 xseccerts.push_back(new OpenSSLCryptoX509(static_cast<OpenSSLCryptoX509*>(*y)->getOpenSSLX509()));
576 for (vector<ManagedCRL>::iterator j = m_crls.begin(); j != m_crls.end(); ++j) {
577 for (vector<XSECCryptoX509CRL*>::const_iterator z = j->crls.begin(); z != j->crls.end(); ++z)
578 xseccrls.push_back((*z)->clone());
580 credential = new FilesystemCredential(this, xseckey.get(), xseccerts, xseccrls);
584 for_each(xseccerts.begin(), xseccerts.end(), xmltooling::cleanup<XSECCryptoX509>());
585 for_each(xseccrls.begin(), xseccrls.end(), xmltooling::cleanup<XSECCryptoX509CRL>());
589 // At this point the copies are owned by the credential.
591 credential->initKeyInfo(m_keyinfomask);
601 Lockable* FilesystemCredentialResolver::lock()
606 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".CredentialResolver."FILESYSTEM_CREDENTIAL_RESOLVER);
610 // Check each managed resource while holding a read lock for staleness.
611 // If it comes back false, the lock is left as is, and the resource was stable.
612 // If it comes back true, the lock was elevated to a write lock, and the resource
613 // needs to be reloaded, and the credential replaced.
614 // Once a stale check comes back true, further checks leave the lock alone.
616 bool writelock = false, updated = false;
618 if (m_key.stale(log, m_lock)) {
621 m_key.load(log, m_keypass.c_str());
624 catch (exception& ex) {
625 log.crit("maintaining existing key: %s", ex.what());
629 for (vector<ManagedCert>::iterator i = m_certs.begin(); i != m_certs.end(); ++i) {
630 if (i->stale(log, writelock ? NULL : m_lock)) {
633 i->load(log, (i==m_certs.begin()) ? m_certpass.c_str() : NULL);
636 catch (exception& ex) {
637 log.crit("maintaining existing certificate(s): %s", ex.what());
642 for (vector<ManagedCRL>::iterator j = m_crls.begin(); j != m_crls.end(); ++j) {
643 if (j->stale(log, writelock ? NULL : m_lock)) {
649 catch (exception& ex) {
650 log.crit("maintaining existing CRL(s): %s", ex.what());
657 auto_ptr<Credential> credential(getCredential());
659 m_credential = credential.release();
661 catch (exception& ex) {
662 log.crit("maintaining existing credentials, error reloading: %s", ex.what());
673 const Credential* FilesystemCredentialResolver::resolve(const CredentialCriteria* criteria) const
675 return (criteria ? (criteria->matches(*m_credential) ? m_credential : NULL) : m_credential);
678 vector<const Credential*>::size_type FilesystemCredentialResolver::resolve(
679 vector<const Credential*>& results, const CredentialCriteria* criteria
682 if (!criteria || criteria->matches(*m_credential)) {
683 results.push_back(m_credential);
689 // OpenSSL password callback...
690 static int passwd_callback(char* buf, int len, int verify, void* passwd)
694 if(passwd && len > strlen(reinterpret_cast<char*>(passwd)))
696 strcpy(buf,reinterpret_cast<char*>(passwd));
703 void FilesystemCredential::attach(SSL_CTX* ctx) const
710 const char* path = m_resolver->m_key.local ? m_resolver->m_key.source.c_str() : m_resolver->m_key.backing.c_str();
712 throw XMLSecurityException("No key available, unable to attach private key to SSL context.");
714 if (!m_resolver->m_keypass.empty()) {
715 SSL_CTX_set_default_passwd_cb(ctx, passwd_callback);
716 SSL_CTX_set_default_passwd_cb_userdata(ctx, const_cast<char*>(m_resolver->m_keypass.c_str()));
719 if (m_resolver->m_key.format == "PEM") {
720 ret=SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM);
722 else if (m_resolver->m_key.format == "DER") {
723 ret=SSL_CTX_use_RSAPrivateKey_file(ctx, path, SSL_FILETYPE_ASN1);
725 else if (m_resolver->m_key.format == "PKCS12") {
726 BIO* in=BIO_new(BIO_s_file_internal());
727 if (in && BIO_read_filename(in,path)>0) {
728 PKCS12* p12 = d2i_PKCS12_bio(in, NULL);
732 PKCS12_parse(p12, const_cast<char*>(m_resolver->m_keypass.c_str()), &pkey, &x, NULL);
737 ret=SSL_CTX_use_PrivateKey(ctx, pkey);
748 throw XMLSecurityException("Unable to attach private key to SSL context.");
752 for (vector<XSECCryptoX509*>::const_iterator i=m_xseccerts.begin(); i!=m_xseccerts.end(); i++) {
753 if (i==m_xseccerts.begin()) {
754 if (SSL_CTX_use_certificate(ctx, static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509()) != 1) {
756 throw XMLSecurityException("Unable to attach client certificate to SSL context.");
760 // When we add certs, they don't get ref counted, so we need to duplicate them.
761 X509* dup = X509_dup(static_cast<OpenSSLCryptoX509*>(*i)->getOpenSSLX509());
762 if (SSL_CTX_add_extra_chain_cert(ctx, dup) != 1) {
765 throw XMLSecurityException("Unable to attach CA certificate to SSL context.");