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