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