https://bugs.internet2.edu/jira/browse/SSPCPP-297
[shibboleth/xmltooling.git] / xmltooling / soap / impl / CURLSOAPTransport.cpp
1 /*
2  *  Copyright 2001-2010 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  * CURLSOAPTransport.cpp
19  *
20  * libcurl-based SOAPTransport implementation.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "logging.h"
26 #include "security/CredentialCriteria.h"
27 #include "security/OpenSSLTrustEngine.h"
28 #include "security/OpenSSLCredential.h"
29 #include "soap/HTTPSOAPTransport.h"
30 #include "soap/OpenSSLSOAPTransport.h"
31 #include "util/NDC.h"
32 #include "util/Threads.h"
33
34 #include <list>
35 #include <curl/curl.h>
36 #include <openssl/x509_vfy.h>
37
38 using namespace xmltooling::logging;
39 using namespace xmltooling;
40 using namespace std;
41
42 namespace xmltooling {
43
44     // Manages cache of socket connections via CURL handles.
45     class XMLTOOL_DLLLOCAL CURLPool
46     {
47     public:
48         CURLPool() : m_size(0), m_lock(Mutex::create()),
49             m_log(Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL")) {}
50         ~CURLPool();
51
52         CURL* get(const SOAPTransport::Address& addr);
53         void put(const char* from, const char* to, const char* endpoint, CURL* handle);
54
55     private:
56         typedef map<string,vector<CURL*> > poolmap_t;
57         poolmap_t m_bindingMap;
58         list< vector<CURL*>* > m_pools;
59         long m_size;
60         Mutex* m_lock;
61         Category& m_log;
62     };
63
64     static XMLTOOL_DLLLOCAL CURLPool* g_CURLPool = nullptr;
65
66     class XMLTOOL_DLLLOCAL CURLSOAPTransport : public HTTPSOAPTransport, public OpenSSLSOAPTransport
67     {
68     public:
69         CURLSOAPTransport(const Address& addr)
70             : m_sender(addr.m_from ? addr.m_from : ""), m_peerName(addr.m_to ? addr.m_to : ""), m_endpoint(addr.m_endpoint),
71                 m_handle(nullptr), m_headers(nullptr),
72 #ifndef XMLTOOLING_NO_XMLSEC
73                     m_cred(nullptr), m_trustEngine(nullptr), m_peerResolver(nullptr), m_mandatory(false),
74 #endif
75                     m_openssl_ops(SSL_OP_ALL|SSL_OP_NO_SSLv2), m_ssl_callback(nullptr), m_ssl_userptr(nullptr),
76                     m_chunked(true), m_authenticated(false), m_cacheTag(nullptr) {
77             m_handle = g_CURLPool->get(addr);
78             curl_easy_setopt(m_handle,CURLOPT_URL,addr.m_endpoint);
79             curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,15);
80             curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,30);
81             curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0);
82             curl_easy_setopt(m_handle,CURLOPT_USERPWD,nullptr);
83             curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYHOST,2);
84             curl_easy_setopt(m_handle,CURLOPT_HEADERDATA,this);
85             m_headers=curl_slist_append(m_headers,"Content-Type: text/xml");
86         }
87
88         virtual ~CURLSOAPTransport() {
89             curl_slist_free_all(m_headers);
90             curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,nullptr);
91             curl_easy_setopt(m_handle,CURLOPT_PRIVATE,m_authenticated ? "secure" : nullptr); // Save off security "state".
92             g_CURLPool->put(m_sender.c_str(), m_peerName.c_str(), m_endpoint.c_str(), m_handle);
93         }
94
95         bool isConfidential() const {
96             return m_endpoint.find("https")==0;
97         }
98
99         bool setConnectTimeout(long timeout) {
100             return (curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,timeout)==CURLE_OK);
101         }
102
103         bool setTimeout(long timeout) {
104             return (curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,timeout)==CURLE_OK);
105         }
106
107         bool setAuth(transport_auth_t authType, const char* username=nullptr, const char* password=nullptr);
108
109         bool setVerifyHost(bool verify) {
110             return (curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYHOST,verify ? 2 : 0)==CURLE_OK);
111         }
112
113 #ifndef XMLTOOLING_NO_XMLSEC
114         bool setCredential(const Credential* cred=nullptr) {
115             const OpenSSLCredential* down = dynamic_cast<const OpenSSLCredential*>(cred);
116             if (!down) {
117                 m_cred = nullptr;
118                 return (cred==nullptr);
119             }
120             m_cred = down;
121             return true;
122         }
123
124         bool setTrustEngine(
125             const X509TrustEngine* trustEngine=nullptr,
126             const CredentialResolver* peerResolver=nullptr,
127             CredentialCriteria* criteria=nullptr,
128             bool mandatory=true
129             ) {
130             const OpenSSLTrustEngine* down = dynamic_cast<const OpenSSLTrustEngine*>(trustEngine);
131             if (!down) {
132                 m_trustEngine = nullptr;
133                 m_peerResolver = nullptr;
134                 m_criteria = nullptr;
135                 return (trustEngine==nullptr);
136             }
137             m_trustEngine = down;
138             m_peerResolver = peerResolver;
139             m_criteria = criteria;
140             m_mandatory = mandatory;
141             return true;
142         }
143
144 #endif
145
146         bool useChunkedEncoding(bool chunked=true) {
147             m_chunked = chunked;
148             return true;
149         }
150
151         bool followRedirects(bool follow, unsigned int maxRedirs) {
152             return (
153                 curl_easy_setopt(m_handle, CURLOPT_FOLLOWLOCATION, (follow ? 1 : 0)) == CURLE_OK &&
154                 curl_easy_setopt(m_handle, CURLOPT_MAXREDIRS, (follow ? maxRedirs : 0)) == CURLE_OK
155                 );
156         }
157
158         bool setCacheTag(string* cacheTag) {
159             m_cacheTag = cacheTag;
160             return true;
161         }
162
163         bool setProviderOption(const char* provider, const char* option, const char* value);
164
165         void send(istream& in) {
166             send(&in);
167         }
168
169         void send(istream* in=nullptr);
170
171         istream& receive() {
172             return m_stream;
173         }
174
175         bool isAuthenticated() const {
176             return m_authenticated;
177         }
178
179         void setAuthenticated(bool auth) {
180             m_authenticated = auth;
181         }
182
183         string getContentType() const;
184         long getStatusCode() const;
185
186         bool setRequestHeader(const char* name, const char* val) {
187             string temp(name);
188             temp=temp + ": " + val;
189             m_headers=curl_slist_append(m_headers,temp.c_str());
190             return true;
191         }
192
193         const vector<string>& getResponseHeader(const char* val) const;
194
195         bool setSSLCallback(ssl_ctx_callback_fn fn, void* userptr=nullptr) {
196             m_ssl_callback=fn;
197             m_ssl_userptr=userptr;
198             return true;
199         }
200
201     private:
202         // per-call state
203         string m_sender,m_peerName,m_endpoint,m_simplecreds;
204         CURL* m_handle;
205         stringstream m_stream;
206         struct curl_slist* m_headers;
207         map<string,vector<string> > m_response_headers;
208         vector<string> m_saved_options;
209 #ifndef XMLTOOLING_NO_XMLSEC
210         const OpenSSLCredential* m_cred;
211         const OpenSSLTrustEngine* m_trustEngine;
212         const CredentialResolver* m_peerResolver;
213         CredentialCriteria* m_criteria;
214         bool m_mandatory;
215 #endif
216         int m_openssl_ops;
217         ssl_ctx_callback_fn m_ssl_callback;
218         void* m_ssl_userptr;
219         bool m_chunked;
220         bool m_authenticated;
221         string* m_cacheTag;
222
223         friend size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
224         friend CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
225         friend int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
226     };
227
228     // libcurl callback functions
229     size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
230     size_t XMLTOOL_DLLLOCAL curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream);
231     size_t XMLTOOL_DLLLOCAL curl_read_hook( void *ptr, size_t size, size_t nmemb, void *stream);
232     int XMLTOOL_DLLLOCAL curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr);
233     CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
234 #ifndef XMLTOOLING_NO_XMLSEC
235     int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
236 #endif
237
238     SOAPTransport* CURLSOAPTransportFactory(const SOAPTransport::Address& addr)
239     {
240         return new CURLSOAPTransport(addr);
241     }
242 };
243
244 void xmltooling::registerSOAPTransports()
245 {
246     XMLToolingConfig& conf=XMLToolingConfig::getConfig();
247     conf.SOAPTransportManager.registerFactory("http", CURLSOAPTransportFactory);
248     conf.SOAPTransportManager.registerFactory("https", CURLSOAPTransportFactory);
249 }
250
251 void xmltooling::initSOAPTransports()
252 {
253     g_CURLPool=new CURLPool();
254 }
255
256 void xmltooling::termSOAPTransports()
257 {
258     delete g_CURLPool;
259     g_CURLPool = nullptr;
260 }
261
262 OpenSSLSOAPTransport::OpenSSLSOAPTransport()
263 {
264 }
265
266 OpenSSLSOAPTransport::~OpenSSLSOAPTransport()
267 {
268 }
269
270 CURLPool::~CURLPool()
271 {
272     for (poolmap_t::iterator i=m_bindingMap.begin(); i!=m_bindingMap.end(); i++) {
273         for (vector<CURL*>::iterator j=i->second.begin(); j!=i->second.end(); j++)
274             curl_easy_cleanup(*j);
275     }
276     delete m_lock;
277 }
278
279 CURL* CURLPool::get(const SOAPTransport::Address& addr)
280 {
281 #ifdef _DEBUG
282     xmltooling::NDC("get");
283 #endif
284     m_log.debug("getting connection handle to %s", addr.m_endpoint);
285     string key(addr.m_endpoint);
286     if (addr.m_from)
287         key = key + '|' + addr.m_from;
288     if (addr.m_to)
289         key = key + '|' + addr.m_to;
290     m_lock->lock();
291     poolmap_t::iterator i=m_bindingMap.find(key);
292
293     if (i!=m_bindingMap.end()) {
294         // Move this pool to the front of the list.
295         m_pools.remove(&(i->second));
296         m_pools.push_front(&(i->second));
297
298         // If a free connection exists, return it.
299         if (!(i->second.empty())) {
300             CURL* handle=i->second.back();
301             i->second.pop_back();
302             m_size--;
303             m_lock->unlock();
304             m_log.debug("returning existing connection handle from pool");
305             return handle;
306         }
307     }
308
309     m_lock->unlock();
310     m_log.debug("nothing free in pool, returning new connection handle");
311
312     // Create a new connection and set non-varying options.
313     CURL* handle=curl_easy_init();
314     if (!handle)
315         return nullptr;
316     curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1);
317     curl_easy_setopt(handle,CURLOPT_NOSIGNAL,1);
318     curl_easy_setopt(handle,CURLOPT_FAILONERROR,1);
319     curl_easy_setopt(handle,CURLOPT_SSL_CIPHER_LIST,"ALL:!aNULL:!LOW:!EXPORT:!SSLv2");
320     // Verification of the peer is via TrustEngine only.
321     curl_easy_setopt(handle,CURLOPT_SSL_VERIFYPEER,0);
322     curl_easy_setopt(handle,CURLOPT_CAINFO,nullptr);
323     curl_easy_setopt(handle,CURLOPT_HEADERFUNCTION,&curl_header_hook);
324     curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&curl_write_hook);
325     curl_easy_setopt(handle,CURLOPT_DEBUGFUNCTION,&curl_debug_hook);
326     string ua = XMLToolingConfig::getConfig().user_agent;
327     if (!ua.empty()) {
328         ua = ua + " libcurl/" + LIBCURL_VERSION + ' ' + OPENSSL_VERSION_TEXT;
329         curl_easy_setopt(handle, CURLOPT_USERAGENT, ua.c_str());
330     }
331     return handle;
332 }
333
334 void CURLPool::put(const char* from, const char* to, const char* endpoint, CURL* handle)
335 {
336     string key(endpoint);
337     if (from)
338         key = key + '|' + from;
339     if (to)
340         key = key + '|' + to;
341     m_lock->lock();
342     poolmap_t::iterator i=m_bindingMap.find(key);
343     if (i==m_bindingMap.end())
344         m_pools.push_front(&(m_bindingMap.insert(poolmap_t::value_type(key,vector<CURL*>(1,handle))).first->second));
345     else
346         i->second.push_back(handle);
347
348     CURL* killit=nullptr;
349     if (++m_size > 256) {
350         // Kick a handle out from the back of the bus.
351         while (true) {
352             vector<CURL*>* corpse=m_pools.back();
353             if (!corpse->empty()) {
354                 killit=corpse->back();
355                 corpse->pop_back();
356                 m_size--;
357                 break;
358             }
359
360             // Move an empty pool up to the front so we don't keep hitting it.
361             m_pools.pop_back();
362             m_pools.push_front(corpse);
363         }
364     }
365     m_lock->unlock();
366     if (killit) {
367         curl_easy_cleanup(killit);
368 #ifdef _DEBUG
369         xmltooling::NDC("put");
370 #endif
371         m_log.info("conn_pool_max limit reached, dropping an old connection");
372     }
373 }
374
375 bool CURLSOAPTransport::setAuth(transport_auth_t authType, const char* username, const char* password)
376 {
377     if (authType==transport_auth_none) {
378         if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0)!=CURLE_OK)
379             return false;
380         return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,nullptr)==CURLE_OK);
381     }
382     long flag=0;
383     switch (authType) {
384         case transport_auth_basic:    flag = CURLAUTH_BASIC; break;
385         case transport_auth_digest:   flag = CURLAUTH_DIGEST; break;
386         case transport_auth_ntlm:     flag = CURLAUTH_NTLM; break;
387         case transport_auth_gss:      flag = CURLAUTH_GSSNEGOTIATE; break;
388         default:            return false;
389     }
390     if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,flag)!=CURLE_OK)
391         return false;
392     m_simplecreds = string(username ? username : "") + ':' + (password ? password : "");
393     return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,m_simplecreds.c_str())==CURLE_OK);
394 }
395
396 bool CURLSOAPTransport::setProviderOption(const char* provider, const char* option, const char* value)
397 {
398     if (!provider || !option || !value) {
399         return false;
400     }
401     else if (!strcmp(provider, "OpenSSL")) {
402         if (!strcmp(option, "SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION") && (*value=='1' || *value=='t')) {
403             // If the new option to enable buggy rengotiation is available, set it.
404             // Otherwise, signal false if this is newer than 0.9.8k, because that
405             // means it's 0.9.8l, which blocks renegotiation, and therefore will
406             // not honor this request. Older versions are buggy, so behave as though
407             // the flag was set anyway, so we signal true.
408 #if defined(SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
409             m_openssl_ops |= SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION;
410             return true;
411 #elif (OPENSSL_VERSION_NUMBER > 0x009080bfL)
412             return false;
413 #else
414             return true;
415 #endif
416         }
417         return false;
418     }
419     else if (strcmp(provider, "CURL")) {
420         return false;
421     }
422
423     // For libcurl, the option is an enum and the value type depends on the option.
424     CURLoption opt = static_cast<CURLoption>(strtol(option, nullptr, 10));
425     if (opt < CURLOPTTYPE_OBJECTPOINT)
426         return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
427 #ifdef CURLOPTTYPE_OFF_T
428     else if (opt < CURLOPTTYPE_OFF_T) {
429         if (value)
430             m_saved_options.push_back(value);
431         return (curl_easy_setopt(m_handle, opt, value ? m_saved_options.back().c_str() : nullptr) == CURLE_OK);
432     }
433 # ifdef HAVE_CURL_OFF_T
434     else if (sizeof(curl_off_t) == sizeof(long))
435         return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
436 # else
437     else if (sizeof(off_t) == sizeof(long))
438         return (curl_easy_setopt(m_handle, opt, strtol(value, nullptr, 10)) == CURLE_OK);
439 # endif
440     return false;
441 #else
442     else {
443         if (value)
444             m_saved_options.push_back(value);
445         return (curl_easy_setopt(m_handle, opt, value ? m_saved_options.back().c_str() : nullptr) == CURLE_OK);
446     }
447 #endif
448 }
449
450 const vector<string>& CURLSOAPTransport::getResponseHeader(const char* name) const
451 {
452     static vector<string> emptyVector;
453
454     map<string,vector<string> >::const_iterator i=m_response_headers.find(name);
455     if (i!=m_response_headers.end())
456         return i->second;
457
458     for (map<string,vector<string> >::const_iterator j=m_response_headers.begin(); j!=m_response_headers.end(); j++) {
459 #ifdef HAVE_STRCASECMP
460         if (!strcasecmp(j->first.c_str(), name))
461 #else
462         if (!stricmp(j->first.c_str(), name))
463 #endif
464             return j->second;
465     }
466
467     return emptyVector;
468 }
469
470 string CURLSOAPTransport::getContentType() const
471 {
472     char* content_type=nullptr;
473     curl_easy_getinfo(m_handle,CURLINFO_CONTENT_TYPE,&content_type);
474     return content_type ? content_type : "";
475 }
476
477 long CURLSOAPTransport::getStatusCode() const
478 {
479     long code=200;
480     if (curl_easy_getinfo(m_handle, CURLINFO_RESPONSE_CODE, &code) != CURLE_OK)
481         code = 200;
482     return code;
483 }
484
485 void CURLSOAPTransport::send(istream* in)
486 {
487 #ifdef _DEBUG
488     xmltooling::NDC ndc("send");
489 #endif
490     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL");
491     Category& log_curl=Category::getInstance(XMLTOOLING_LOGCAT".libcurl");
492
493     // For this implementation, it's sufficient to check for https as a sign of transport security.
494     if (m_mandatory && !isConfidential())
495         throw IOException("Blocking unprotected HTTP request, transport authentication by server required.");
496
497     string msg;
498
499     // By this time, the handle has been prepared with the URL to use and the
500     // caller should have executed any set functions to manipulate it.
501
502     // Setup standard per-call curl properties.
503     curl_easy_setopt(m_handle,CURLOPT_DEBUGDATA,&log_curl);
504     curl_easy_setopt(m_handle,CURLOPT_FILE,&m_stream);
505     if (m_chunked && in) {
506         curl_easy_setopt(m_handle,CURLOPT_POST,1);
507         m_headers=curl_slist_append(m_headers,"Transfer-Encoding: chunked");
508         curl_easy_setopt(m_handle,CURLOPT_READFUNCTION,&curl_read_hook);
509         curl_easy_setopt(m_handle,CURLOPT_READDATA,in);
510     }
511     else if (in) {
512         char buf[1024];
513         while (*in) {
514             in->read(buf,1024);
515             msg.append(buf,in->gcount());
516         }
517         curl_easy_setopt(m_handle,CURLOPT_POST,1);
518         curl_easy_setopt(m_handle,CURLOPT_READFUNCTION,nullptr);
519         curl_easy_setopt(m_handle,CURLOPT_POSTFIELDS,msg.c_str());
520         curl_easy_setopt(m_handle,CURLOPT_POSTFIELDSIZE,msg.length());
521     }
522     else {
523         curl_easy_setopt(m_handle,CURLOPT_HTTPGET,1);
524         curl_easy_setopt(m_handle,CURLOPT_FOLLOWLOCATION,1);
525         curl_easy_setopt(m_handle,CURLOPT_MAXREDIRS,6);
526     }
527
528     char curl_errorbuf[CURL_ERROR_SIZE];
529     curl_errorbuf[0]=0;
530     curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,curl_errorbuf);
531     if (log_curl.isDebugEnabled())
532         curl_easy_setopt(m_handle,CURLOPT_VERBOSE,1);
533
534     // Check for cache tag.
535     if (m_cacheTag && !m_cacheTag->empty()) {
536         string hdr("If-None-Match: ");
537         hdr += *m_cacheTag;
538         m_headers = curl_slist_append(m_headers, hdr.c_str());
539     }
540
541     // Set request headers.
542     curl_easy_setopt(m_handle,CURLOPT_HTTPHEADER,m_headers);
543
544 #ifndef XMLTOOLING_NO_XMLSEC
545     if (m_ssl_callback || m_cred || m_trustEngine) {
546 #else
547     if (m_ssl_callback) {
548 #endif
549         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,xml_ssl_ctx_callback);
550         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,this);
551
552         // Restore security "state". Necessary because the callback only runs
553         // when handshakes occur. Even new TCP connections won't execute it.
554         char* priv=nullptr;
555         curl_easy_getinfo(m_handle,CURLINFO_PRIVATE,&priv);
556         if (priv)
557             m_authenticated=true;
558     }
559     else {
560         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,nullptr);
561         curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,nullptr);
562     }
563
564     // Make the call.
565     log.debug("sending SOAP message to %s", m_endpoint.c_str());
566     if (curl_easy_perform(m_handle) != CURLE_OK) {
567         throw IOException(
568             string("CURLSOAPTransport failed while contacting SOAP endpoint (") + m_endpoint + "): " +
569                 (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
570     }
571
572     // Check for outgoing cache tag.
573     if (m_cacheTag) {
574         const vector<string>& tags = getResponseHeader("ETag");
575         if (!tags.empty())
576             *m_cacheTag = tags.front();
577     }
578 }
579
580 // callback to buffer headers from server
581 size_t xmltooling::curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream)
582 {
583     // only handle single-byte data
584     if (size!=1)
585         return 0;
586     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(stream);
587     char* buf = (char*)malloc(nmemb + 1);
588     if (buf) {
589         memset(buf,0,nmemb + 1);
590         memcpy(buf,ptr,nmemb);
591         char* sep=(char*)strchr(buf,':');
592         if (sep) {
593             *(sep++)=0;
594             while (*sep==' ')
595                 *(sep++)=0;
596             char* white=buf+nmemb-1;
597             while (isspace(*white))
598                 *(white--)=0;
599             ctx->m_response_headers[buf].push_back(sep);
600         }
601         free(buf);
602         return nmemb;
603     }
604     return 0;
605 }
606
607 // callback to send data to server
608 size_t xmltooling::curl_read_hook(void* ptr, size_t size, size_t nmemb, void* stream)
609 {
610     // stream is actually an istream pointer
611     istream* buf=reinterpret_cast<istream*>(stream);
612     buf->read(reinterpret_cast<char*>(ptr),size*nmemb);
613     return buf->gcount();
614 }
615
616 // callback to buffer data from server
617 size_t xmltooling::curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream)
618 {
619     size_t len = size*nmemb;
620     reinterpret_cast<stringstream*>(stream)->write(reinterpret_cast<const char*>(ptr),len);
621     return len;
622 }
623
624 // callback for curl debug data
625 int xmltooling::curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr)
626 {
627     // *ptr is actually a logging object
628     if (!ptr) return 0;
629     CategoryStream log=reinterpret_cast<Category*>(ptr)->debugStream();
630     for (unsigned char* ch=(unsigned char*)data; len && (isprint(*ch) || isspace(*ch)); len--)
631         log << *ch++;
632     return 0;
633 }
634
635 #ifndef XMLTOOLING_NO_XMLSEC
636 int xmltooling::verify_callback(X509_STORE_CTX* x509_ctx, void* arg)
637 {
638     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURL");
639     log.debug("invoking custom X.509 verify callback");
640 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
641     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(arg);
642 #else
643     // Yes, this sucks. I'd use TLS, but there's no really obvious spot to put the thread key
644     // and global variables suck too. We can't access the X509_STORE_CTX depth directly because
645     // OpenSSL only copies it into the context if it's >=0, and the unsigned pointer may be
646     // negative in the SSL structure's int member.
647     CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(
648         SSL_get_verify_depth(
649             reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(x509_ctx,SSL_get_ex_data_X509_STORE_CTX_idx()))
650             )
651         );
652 #endif
653
654     bool success=false;
655     if (ctx->m_criteria) {
656         ctx->m_criteria->setUsage(Credential::TLS_CREDENTIAL);
657         // Bypass name check (handled for us by curl).
658         ctx->m_criteria->setPeerName(nullptr);
659         success = ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,*(ctx->m_peerResolver),ctx->m_criteria);
660     }
661     else {
662         // Bypass name check (handled for us by curl).
663         CredentialCriteria cc;
664         cc.setUsage(Credential::TLS_CREDENTIAL);
665         success = ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,*(ctx->m_peerResolver),&cc);
666     }
667
668     if (!success) {
669         log.error("supplied TrustEngine failed to validate SSL/TLS server certificate");
670         x509_ctx->error=X509_V_ERR_APPLICATION_VERIFICATION;     // generic error, check log for plugin specifics
671         ctx->setAuthenticated(false);
672         return ctx->m_mandatory ? 0 : 1;
673     }
674
675     // Signal success. Hopefully it doesn't matter what's actually in the structure now.
676     ctx->setAuthenticated(true);
677     return 1;
678 }
679 #endif
680
681 // callback to invoke a caller-defined SSL callback
682 CURLcode xmltooling::xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr)
683 {
684     CURLSOAPTransport* conf = reinterpret_cast<CURLSOAPTransport*>(userptr);
685
686     // Default flags manually disable SSLv2 so we're not dependent on libcurl to do it.
687     // Also disable the ticket option where implemented, since this breaks a variety
688     // of servers. Newer libcurl also does this for us.
689 #ifdef SSL_OP_NO_TICKET
690     SSL_CTX_set_options(ssl_ctx, conf->m_openssl_ops|SSL_OP_NO_TICKET);
691 #else
692     SSL_CTX_set_options(ssl_ctx, conf->m_openssl_ops);
693 #endif
694
695 #ifndef XMLTOOLING_NO_XMLSEC
696     if (conf->m_cred)
697         conf->m_cred->attach(ssl_ctx);
698
699     if (conf->m_trustEngine) {
700         SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_PEER,nullptr);
701 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
702         // With 0.9.7, we can pass a callback argument directly.
703         SSL_CTX_set_cert_verify_callback(ssl_ctx,verify_callback,userptr);
704 #else
705         // With 0.9.6, there's no argument, so we're going to use a really embarrassing hack and
706         // stuff the argument in the depth property where it will get copied to the context object
707         // that's handed to the callback.
708         SSL_CTX_set_cert_verify_callback(ssl_ctx,reinterpret_cast<int (*)()>(verify_callback),nullptr);
709         SSL_CTX_set_verify_depth(ssl_ctx,reinterpret_cast<int>(userptr));
710 #endif
711     }
712 #endif
713
714     if (conf->m_ssl_callback && !conf->m_ssl_callback(conf, ssl_ctx, conf->m_ssl_userptr))
715         return CURLE_SSL_CERTPROBLEM;
716
717     return CURLE_OK;
718 }