2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * CURLSOAPTransport.cpp
24 * libcurl-based SOAPTransport implementation.
28 #include "exceptions.h"
30 #include "security/CredentialCriteria.h"
31 #include "security/OpenSSLTrustEngine.h"
32 #include "security/OpenSSLCredential.h"
33 #include "soap/HTTPSOAPTransport.h"
34 #include "soap/OpenSSLSOAPTransport.h"
36 #include "util/Threads.h"
39 #include <curl/curl.h>
40 #include <openssl/x509_vfy.h>
42 using namespace xmltooling::logging;
43 using namespace xmltooling;
46 namespace xmltooling {
48 // Manages cache of socket connections via CURL handles.
49 class XMLTOOL_DLLLOCAL CURLPool
52 CURLPool() : m_size(0), m_lock(Mutex::create()),
53 m_log(Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL")) {}
56 CURL* get(const SOAPTransport::Address& addr);
57 void put(const char* from, const char* to, const char* endpoint, CURL* handle);
60 typedef map<string,vector<CURL*> > poolmap_t;
61 poolmap_t m_bindingMap;
62 list< vector<CURL*>* > m_pools;
68 static XMLTOOL_DLLLOCAL CURLPool* g_CURLPool = nullptr;
70 class XMLTOOL_DLLLOCAL CURLSOAPTransport : public HTTPSOAPTransport, public OpenSSLSOAPTransport
73 CURLSOAPTransport(const Address& addr)
74 : m_sender(addr.m_from ? addr.m_from : ""), m_peerName(addr.m_to ? addr.m_to : ""), m_endpoint(addr.m_endpoint),
75 m_handle(nullptr), m_headers(nullptr),
76 #ifndef XMLTOOLING_NO_XMLSEC
77 m_cred(nullptr), m_trustEngine(nullptr), m_peerResolver(nullptr), m_mandatory(false),
79 m_openssl_ops(SSL_OP_ALL|SSL_OP_NO_SSLv2), m_ssl_callback(nullptr), m_ssl_userptr(nullptr),
80 m_chunked(true), m_authenticated(false), m_cacheTag(nullptr) {
81 m_handle = g_CURLPool->get(addr);
82 curl_easy_setopt(m_handle,CURLOPT_URL,addr.m_endpoint);
83 curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,15);
84 curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,30);
85 curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0);
86 curl_easy_setopt(m_handle,CURLOPT_USERPWD,nullptr);
87 curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYHOST,2);
88 curl_easy_setopt(m_handle,CURLOPT_HEADERDATA,this);
89 m_headers=curl_slist_append(m_headers,"Content-Type: text/xml");
92 virtual ~CURLSOAPTransport() {
93 curl_slist_free_all(m_headers);
94 curl_easy_setopt(m_handle, CURLOPT_USERAGENT, nullptr);
95 curl_easy_setopt(m_handle, CURLOPT_ERRORBUFFER, nullptr);
96 curl_easy_setopt(m_handle, CURLOPT_PRIVATE, m_authenticated ? "secure" : nullptr); // Save off security "state".
97 g_CURLPool->put(m_sender.c_str(), m_peerName.c_str(), m_endpoint.c_str(), m_handle);
100 bool isConfidential() const {
101 return m_endpoint.find("https")==0;
104 bool setConnectTimeout(long timeout) {
105 return (curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,timeout)==CURLE_OK);
108 bool setTimeout(long timeout) {
109 return (curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,timeout)==CURLE_OK);
112 bool setAuth(transport_auth_t authType, const char* username=nullptr, const char* password=nullptr);
114 bool setVerifyHost(bool verify) {
115 return (curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYHOST,verify ? 2 : 0)==CURLE_OK);
118 #ifndef XMLTOOLING_NO_XMLSEC
119 bool setCredential(const Credential* cred=nullptr) {
120 const OpenSSLCredential* down = dynamic_cast<const OpenSSLCredential*>(cred);
123 return (cred==nullptr);
130 const X509TrustEngine* trustEngine=nullptr,
131 const CredentialResolver* peerResolver=nullptr,
132 CredentialCriteria* criteria=nullptr,
135 const OpenSSLTrustEngine* down = dynamic_cast<const OpenSSLTrustEngine*>(trustEngine);
137 m_trustEngine = nullptr;
138 m_peerResolver = nullptr;
139 m_criteria = nullptr;
140 return (trustEngine==nullptr);
142 m_trustEngine = down;
143 m_peerResolver = peerResolver;
144 m_criteria = criteria;
145 m_mandatory = mandatory;
151 bool useChunkedEncoding(bool chunked=true) {
156 bool followRedirects(bool follow, unsigned int maxRedirs) {
158 curl_easy_setopt(m_handle, CURLOPT_FOLLOWLOCATION, (follow ? 1 : 0)) == CURLE_OK &&
159 curl_easy_setopt(m_handle, CURLOPT_MAXREDIRS, (follow ? maxRedirs : 0)) == CURLE_OK
163 bool setCacheTag(string* cacheTag) {
164 m_cacheTag = cacheTag;
168 bool setProviderOption(const char* provider, const char* option, const char* value);
170 void send(istream& in) {
174 void send(istream* in=nullptr);
180 bool isAuthenticated() const {
181 return m_authenticated;
184 void setAuthenticated(bool auth) {
185 m_authenticated = auth;
188 string getContentType() const;
189 long getStatusCode() const;
191 bool setRequestHeader(const char* name, const char* val) {
193 temp=temp + ": " + val;
194 m_headers=curl_slist_append(m_headers,temp.c_str());
198 const vector<string>& getResponseHeader(const char* val) const;
200 bool setSSLCallback(ssl_ctx_callback_fn fn, void* userptr=nullptr) {
202 m_ssl_userptr=userptr;
208 string m_sender,m_peerName,m_endpoint,m_simplecreds;
210 stringstream m_stream;
211 struct curl_slist* m_headers;
213 map<string,vector<string> > m_response_headers;
214 vector<string> m_saved_options;
215 #ifndef XMLTOOLING_NO_XMLSEC
216 const OpenSSLCredential* m_cred;
217 const OpenSSLTrustEngine* m_trustEngine;
218 const CredentialResolver* m_peerResolver;
219 CredentialCriteria* m_criteria;
223 ssl_ctx_callback_fn m_ssl_callback;
226 bool m_authenticated;
229 friend size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
230 friend CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
231 friend int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
234 // libcurl callback functions
235 size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
236 size_t XMLTOOL_DLLLOCAL curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream);
237 size_t XMLTOOL_DLLLOCAL curl_read_hook( void *ptr, size_t size, size_t nmemb, void *stream);
238 int XMLTOOL_DLLLOCAL curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr);
239 CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
240 #ifndef XMLTOOLING_NO_XMLSEC
241 int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
244 SOAPTransport* CURLSOAPTransportFactory(const SOAPTransport::Address& addr)
246 return new CURLSOAPTransport(addr);
250 void xmltooling::registerSOAPTransports()
252 XMLToolingConfig& conf=XMLToolingConfig::getConfig();
253 conf.SOAPTransportManager.registerFactory("http", CURLSOAPTransportFactory);
254 conf.SOAPTransportManager.registerFactory("https", CURLSOAPTransportFactory);
257 void xmltooling::initSOAPTransports()
259 g_CURLPool=new CURLPool();
262 void xmltooling::termSOAPTransports()
265 g_CURLPool = nullptr;
268 OpenSSLSOAPTransport::OpenSSLSOAPTransport()
272 OpenSSLSOAPTransport::~OpenSSLSOAPTransport()
276 CURLPool::~CURLPool()
278 for (poolmap_t::iterator i=m_bindingMap.begin(); i!=m_bindingMap.end(); i++) {
279 for (vector<CURL*>::iterator j=i->second.begin(); j!=i->second.end(); j++)
280 curl_easy_cleanup(*j);
285 CURL* CURLPool::get(const SOAPTransport::Address& addr)
288 xmltooling::NDC("get");
290 m_log.debug("getting connection handle to %s", addr.m_endpoint);
291 string key(addr.m_endpoint);
293 key = key + '|' + addr.m_from;
295 key = key + '|' + addr.m_to;
297 poolmap_t::iterator i=m_bindingMap.find(key);
299 if (i!=m_bindingMap.end()) {
300 // Move this pool to the front of the list.
301 m_pools.remove(&(i->second));
302 m_pools.push_front(&(i->second));
304 // If a free connection exists, return it.
305 if (!(i->second.empty())) {
306 CURL* handle=i->second.back();
307 i->second.pop_back();
310 m_log.debug("returning existing connection handle from pool");
316 m_log.debug("nothing free in pool, returning new connection handle");
318 // Create a new connection and set non-varying options.
319 CURL* handle=curl_easy_init();
322 curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1);
323 curl_easy_setopt(handle,CURLOPT_NOSIGNAL,1);
324 curl_easy_setopt(handle,CURLOPT_FAILONERROR,1);
325 curl_easy_setopt(handle,CURLOPT_SSL_CIPHER_LIST,"ALL:!aNULL:!LOW:!EXPORT:!SSLv2");
326 // Verification of the peer is via TrustEngine only.
327 curl_easy_setopt(handle,CURLOPT_SSL_VERIFYPEER,0);
328 curl_easy_setopt(handle,CURLOPT_CAINFO,nullptr);
329 curl_easy_setopt(handle,CURLOPT_HEADERFUNCTION,&curl_header_hook);
330 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&curl_write_hook);
331 curl_easy_setopt(handle,CURLOPT_DEBUGFUNCTION,&curl_debug_hook);
335 void CURLPool::put(const char* from, const char* to, const char* endpoint, CURL* handle)
337 string key(endpoint);
339 key = key + '|' + from;
341 key = key + '|' + to;
343 poolmap_t::iterator i=m_bindingMap.find(key);
344 if (i==m_bindingMap.end())
345 m_pools.push_front(&(m_bindingMap.insert(poolmap_t::value_type(key,vector<CURL*>(1,handle))).first->second));
347 i->second.push_back(handle);
349 CURL* killit=nullptr;
350 if (++m_size > 256) {
351 // Kick a handle out from the back of the bus.
353 vector<CURL*>* corpse=m_pools.back();
354 if (!corpse->empty()) {
355 killit=corpse->back();
361 // Move an empty pool up to the front so we don't keep hitting it.
363 m_pools.push_front(corpse);
368 curl_easy_cleanup(killit);
370 xmltooling::NDC("put");
372 m_log.info("conn_pool_max limit reached, dropping an old connection");
376 bool CURLSOAPTransport::setAuth(transport_auth_t authType, const char* username, const char* password)
378 if (authType==transport_auth_none) {
379 if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0)!=CURLE_OK)
381 return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,nullptr)==CURLE_OK);
385 case transport_auth_basic: flag = CURLAUTH_BASIC; break;
386 case transport_auth_digest: flag = CURLAUTH_DIGEST; break;
387 case transport_auth_ntlm: flag = CURLAUTH_NTLM; break;
388 case transport_auth_gss: flag = CURLAUTH_GSSNEGOTIATE; break;
389 default: return false;
391 if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,flag)!=CURLE_OK)
393 m_simplecreds = string(username ? username : "") + ':' + (password ? password : "");
394 return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,m_simplecreds.c_str())==CURLE_OK);
397 bool CURLSOAPTransport::setProviderOption(const char* provider, const char* option, const char* value)
399 if (!provider || !option || !value) {
402 else if (!strcmp(provider, "OpenSSL")) {
403 if (!strcmp(option, "SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION") && (*value=='1' || *value=='t')) {
404 // If the new option to enable buggy rengotiation is available, set it.
405 // Otherwise, signal false if this is newer than 0.9.8k, because that
406 // means it's 0.9.8l, which blocks renegotiation, and therefore will
407 // not honor this request. Older versions are buggy, so behave as though
408 // the flag was set anyway, so we signal true.
409 #if defined(SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
410 m_openssl_ops |= SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
412 #elif (OPENSSL_VERSION_NUMBER > 0x009080bfL)
420 else if (strcmp(provider, "CURL")) {
424 // For libcurl, the option is an enum and the value type depends on the option.
425 CURLoption opt = static_cast<CURLoption>(strtol(option, nullptr, 10));
426 if (opt < CURLOPTTYPE_OBJECTPOINT)
427 return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
428 #ifdef CURLOPTTYPE_OFF_T
429 else if (opt < CURLOPTTYPE_OFF_T) {
431 m_saved_options.push_back(value);
432 return (curl_easy_setopt(m_handle, opt, value ? m_saved_options.back().c_str() : nullptr) == CURLE_OK);
434 # ifdef HAVE_CURL_OFF_T
435 else if (sizeof(curl_off_t) == sizeof(long))
436 return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
438 else if (sizeof(off_t) == sizeof(long))
439 return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
445 m_saved_options.push_back(value);
446 return (curl_easy_setopt(m_handle, opt, value ? m_saved_options.back().c_str() : nullptr) == CURLE_OK);
451 const vector<string>& CURLSOAPTransport::getResponseHeader(const char* name) const
453 static vector<string> emptyVector;
455 map<string,vector<string> >::const_iterator i=m_response_headers.find(name);
456 if (i!=m_response_headers.end())
459 for (map<string,vector<string> >::const_iterator j=m_response_headers.begin(); j!=m_response_headers.end(); j++) {
460 #ifdef HAVE_STRCASECMP
461 if (!strcasecmp(j->first.c_str(), name))
463 if (!stricmp(j->first.c_str(), name))
471 string CURLSOAPTransport::getContentType() const
473 char* content_type=nullptr;
474 curl_easy_getinfo(m_handle,CURLINFO_CONTENT_TYPE,&content_type);
475 return content_type ? content_type : "";
478 long CURLSOAPTransport::getStatusCode() const
481 if (curl_easy_getinfo(m_handle, CURLINFO_RESPONSE_CODE, &code) != CURLE_OK)
486 void CURLSOAPTransport::send(istream* in)
489 xmltooling::NDC ndc("send");
491 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL");
492 Category& log_curl=Category::getInstance(XMLTOOLING_LOGCAT".libcurl");
494 // For this implementation, it's sufficient to check for https as a sign of transport security.
495 if (m_mandatory && !isConfidential())
496 throw IOException("Blocking unprotected HTTP request, transport authentication by server required.");
500 // By this time, the handle has been prepared with the URL to use and the
501 // caller should have executed any set functions to manipulate it.
503 // Setup standard per-call curl properties.
504 curl_easy_setopt(m_handle,CURLOPT_DEBUGDATA,&log_curl);
505 curl_easy_setopt(m_handle,CURLOPT_FILE,&m_stream);
506 if (m_chunked && in) {
507 curl_easy_setopt(m_handle,CURLOPT_POST,1);
508 m_headers=curl_slist_append(m_headers,"Transfer-Encoding: chunked");
509 curl_easy_setopt(m_handle,CURLOPT_READFUNCTION,&curl_read_hook);
510 curl_easy_setopt(m_handle,CURLOPT_READDATA,in);
516 msg.append(buf,in->gcount());
518 curl_easy_setopt(m_handle,CURLOPT_POST,1);
519 curl_easy_setopt(m_handle,CURLOPT_READFUNCTION,nullptr);
520 curl_easy_setopt(m_handle,CURLOPT_POSTFIELDS,msg.c_str());
521 curl_easy_setopt(m_handle,CURLOPT_POSTFIELDSIZE,msg.length());
524 curl_easy_setopt(m_handle,CURLOPT_HTTPGET,1);
525 curl_easy_setopt(m_handle,CURLOPT_FOLLOWLOCATION,1);
526 curl_easy_setopt(m_handle,CURLOPT_MAXREDIRS,6);
529 char curl_errorbuf[CURL_ERROR_SIZE];
531 curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,curl_errorbuf);
532 if (log_curl.isDebugEnabled())
533 curl_easy_setopt(m_handle,CURLOPT_VERBOSE,1);
535 // Check for cache tag.
536 if (m_cacheTag && !m_cacheTag->empty()) {
537 string hdr("If-None-Match: ");
539 m_headers = curl_slist_append(m_headers, hdr.c_str());
542 m_useragent = XMLToolingConfig::getConfig().user_agent;
543 if (!m_useragent.empty()) {
544 curl_version_info_data* curlver = curl_version_info(CURLVERSION_NOW);
545 m_useragent += " libcurl/";
547 m_useragent = m_useragent + curlver->version + ' ' + curlver->ssl_version;
549 m_useragent = m_useragent + LIBCURL_VERSION + ' ' + OPENSSL_VERSION_TEXT;
550 curl_easy_setopt(m_handle, CURLOPT_USERAGENT, m_useragent.c_str());
553 // Set request headers.
554 curl_easy_setopt(m_handle,CURLOPT_HTTPHEADER,m_headers);
556 #ifndef XMLTOOLING_NO_XMLSEC
557 if (m_ssl_callback || m_cred || m_trustEngine) {
559 if (m_ssl_callback) {
561 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,xml_ssl_ctx_callback);
562 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,this);
564 // Restore security "state". Necessary because the callback only runs
565 // when handshakes occur. Even new TCP connections won't execute it.
567 curl_easy_getinfo(m_handle,CURLINFO_PRIVATE,&priv);
569 m_authenticated=true;
572 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,nullptr);
573 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,nullptr);
577 log.debug("sending SOAP message to %s", m_endpoint.c_str());
578 if (curl_easy_perform(m_handle) != CURLE_OK) {
580 string("CURLSOAPTransport failed while contacting SOAP endpoint (") + m_endpoint + "): " +
581 (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
584 // Check for outgoing cache tag.
586 const vector<string>& tags = getResponseHeader("ETag");
588 *m_cacheTag = tags.front();
592 // callback to buffer headers from server
593 size_t xmltooling::curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream)
595 // only handle single-byte data
598 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(stream);
599 char* buf = (char*)malloc(nmemb + 1);
601 memset(buf,0,nmemb + 1);
602 memcpy(buf,ptr,nmemb);
603 char* sep=(char*)strchr(buf,':');
608 char* white=buf+nmemb-1;
609 while (isspace(*white))
611 ctx->m_response_headers[buf].push_back(sep);
619 // callback to send data to server
620 size_t xmltooling::curl_read_hook(void* ptr, size_t size, size_t nmemb, void* stream)
622 // stream is actually an istream pointer
623 istream* buf=reinterpret_cast<istream*>(stream);
624 buf->read(reinterpret_cast<char*>(ptr),size*nmemb);
625 return buf->gcount();
628 // callback to buffer data from server
629 size_t xmltooling::curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream)
631 size_t len = size*nmemb;
632 reinterpret_cast<stringstream*>(stream)->write(reinterpret_cast<const char*>(ptr),len);
636 // callback for curl debug data
637 int xmltooling::curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr)
639 // *ptr is actually a logging object
641 CategoryStream log=reinterpret_cast<Category*>(ptr)->debugStream();
642 for (unsigned char* ch=(unsigned char*)data; len && (isprint(*ch) || isspace(*ch)); len--)
647 #ifndef XMLTOOLING_NO_XMLSEC
648 int xmltooling::verify_callback(X509_STORE_CTX* x509_ctx, void* arg)
650 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL");
651 log.debug("invoking custom X.509 verify callback");
652 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
653 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(arg);
655 // Yes, this sucks. I'd use TLS, but there's no really obvious spot to put the thread key
656 // and global variables suck too. We can't access the X509_STORE_CTX depth directly because
657 // OpenSSL only copies it into the context if it's >=0, and the unsigned pointer may be
658 // negative in the SSL structure's int member.
659 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(
660 SSL_get_verify_depth(
661 reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(x509_ctx,SSL_get_ex_data_X509_STORE_CTX_idx()))
667 if (ctx->m_criteria) {
668 ctx->m_criteria->setUsage(Credential::TLS_CREDENTIAL);
669 // Bypass name check (handled for us by curl).
670 ctx->m_criteria->setPeerName(nullptr);
671 success = ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,*(ctx->m_peerResolver),ctx->m_criteria);
674 // Bypass name check (handled for us by curl).
675 CredentialCriteria cc;
676 cc.setUsage(Credential::TLS_CREDENTIAL);
677 success = ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,*(ctx->m_peerResolver),&cc);
681 log.error("supplied TrustEngine failed to validate SSL/TLS server certificate");
682 x509_ctx->error=X509_V_ERR_APPLICATION_VERIFICATION; // generic error, check log for plugin specifics
683 ctx->setAuthenticated(false);
684 return ctx->m_mandatory ? 0 : 1;
687 // Signal success. Hopefully it doesn't matter what's actually in the structure now.
688 ctx->setAuthenticated(true);
693 // callback to invoke a caller-defined SSL callback
694 CURLcode xmltooling::xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr)
696 CURLSOAPTransport* conf = reinterpret_cast<CURLSOAPTransport*>(userptr);
698 // Default flags manually disable SSLv2 so we're not dependent on libcurl to do it.
699 // Also disable the ticket option where implemented, since this breaks a variety
700 // of servers. Newer libcurl also does this for us.
701 #ifdef SSL_OP_NO_TICKET
702 SSL_CTX_set_options(ssl_ctx, conf->m_openssl_ops|SSL_OP_NO_TICKET);
704 SSL_CTX_set_options(ssl_ctx, conf->m_openssl_ops);
707 #ifndef XMLTOOLING_NO_XMLSEC
709 conf->m_cred->attach(ssl_ctx);
711 if (conf->m_trustEngine) {
712 SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_PEER,nullptr);
713 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
714 // With 0.9.7, we can pass a callback argument directly.
715 SSL_CTX_set_cert_verify_callback(ssl_ctx,verify_callback,userptr);
717 // With 0.9.6, there's no argument, so we're going to use a really embarrassing hack and
718 // stuff the argument in the depth property where it will get copied to the context object
719 // that's handed to the callback.
720 SSL_CTX_set_cert_verify_callback(ssl_ctx,reinterpret_cast<int (*)()>(verify_callback),nullptr);
721 SSL_CTX_set_verify_depth(ssl_ctx,reinterpret_cast<int>(userptr));
726 if (conf->m_ssl_callback && !conf->m_ssl_callback(conf, ssl_ctx, conf->m_ssl_userptr))
727 return CURLE_SSL_CERTPROBLEM;