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