2 * Copyright 2001-2006 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * CURLSOAPTransport.cpp
20 * libcurl-based SOAPTransport implementation
24 #include "exceptions.h"
25 #include "security/OpenSSLTrustEngine.h"
26 #include "signature/OpenSSLCredentialResolver.h"
27 #include "soap/HTTPSOAPTransport.h"
28 #include "soap/OpenSSLSOAPTransport.h"
30 #include "util/Threads.h"
33 #include <curl/curl.h>
34 #include <log4cpp/Category.hh>
35 #include <openssl/x509_vfy.h>
37 using namespace xmlsignature;
38 using namespace xmltooling;
39 using namespace log4cpp;
42 namespace xmltooling {
44 // Manages cache of socket connections via CURL handles.
45 class XMLTOOL_DLLLOCAL CURLPool
48 CURLPool() : m_size(256), m_lock(Mutex::create()),
49 m_log(Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport.CURLPool")) {}
52 CURL* get(const string& to, const char* endpoint);
53 void put(const string& to, const char* endpoint, CURL* handle);
56 typedef map<string,vector<CURL*> > poolmap_t;
57 poolmap_t m_bindingMap;
58 list< vector<CURL*>* > m_pools;
64 static XMLTOOL_DLLLOCAL CURLPool* g_CURLPool = NULL;
66 class XMLTOOL_DLLLOCAL CURLSOAPTransport : public HTTPSOAPTransport, public OpenSSLSOAPTransport
69 CURLSOAPTransport(const KeyInfoSource& peer, const char* endpoint)
70 : m_peer(peer), m_endpoint(endpoint), m_handle(NULL), m_headers(NULL),
71 m_credResolver(NULL), m_trustEngine(NULL), m_keyResolver(NULL),
72 m_ssl_callback(NULL), m_ssl_userptr(NULL) {
73 m_handle = g_CURLPool->get(peer.getName(), endpoint);
74 curl_easy_setopt(m_handle,CURLOPT_URL,endpoint);
75 curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,15);
76 curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,30);
77 curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0);
78 curl_easy_setopt(m_handle,CURLOPT_USERPWD,NULL);
79 curl_easy_setopt(m_handle,CURLOPT_HEADERDATA,this);
80 m_headers=curl_slist_append(m_headers,"Content-Type: text/xml");
83 virtual ~CURLSOAPTransport() {
84 curl_slist_free_all(m_headers);
85 curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,NULL);
86 g_CURLPool->put(m_peer.getName(), m_endpoint.c_str(), m_handle);
89 bool setConnectTimeout(long timeout) const {
90 return (curl_easy_setopt(m_handle,CURLOPT_CONNECTTIMEOUT,timeout)==CURLE_OK);
93 bool setTimeout(long timeout) const {
94 return (curl_easy_setopt(m_handle,CURLOPT_TIMEOUT,timeout)==CURLE_OK);
97 bool setAuth(transport_auth_t authType, const char* username=NULL, const char* password=NULL) const;
99 bool setCredentialResolver(const CredentialResolver* credResolver) const {
100 const OpenSSLCredentialResolver* down = dynamic_cast<const OpenSSLCredentialResolver*>(credResolver);
102 m_credResolver = NULL;
103 return (credResolver==NULL);
105 m_credResolver = down;
109 bool setTrustEngine(const X509TrustEngine* trustEngine, const KeyResolver* keyResolver=NULL) const {
110 const OpenSSLTrustEngine* down = dynamic_cast<const OpenSSLTrustEngine*>(trustEngine);
112 m_trustEngine = NULL;
113 m_keyResolver = NULL;
114 return (trustEngine==NULL);
116 m_trustEngine = down;
117 m_keyResolver = keyResolver;
121 void send(istream& in);
127 string getContentType() const;
129 bool setRequestHeader(const char* name, const char* val) const {
131 temp=temp + ": " + val;
132 m_headers=curl_slist_append(m_headers,temp.c_str());
136 const vector<string>& getResponseHeader(const char* val) const;
138 bool setSSLCallback(ssl_ctx_callback_fn fn, void* userptr=NULL) const {
140 m_ssl_userptr=userptr;
146 const KeyInfoSource& m_peer;
149 stringstream m_stream;
150 mutable struct curl_slist* m_headers;
151 map<string,vector<string> > m_response_headers;
152 mutable const OpenSSLCredentialResolver* m_credResolver;
153 mutable const OpenSSLTrustEngine* m_trustEngine;
154 mutable const KeyResolver* m_keyResolver;
155 mutable ssl_ctx_callback_fn m_ssl_callback;
156 mutable void* m_ssl_userptr;
158 friend size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
159 friend CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
160 friend int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
163 // libcurl callback functions
164 size_t XMLTOOL_DLLLOCAL curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream);
165 size_t XMLTOOL_DLLLOCAL curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream);
166 size_t XMLTOOL_DLLLOCAL curl_read_hook( void *ptr, size_t size, size_t nmemb, void *stream);
167 int XMLTOOL_DLLLOCAL curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr);
168 CURLcode XMLTOOL_DLLLOCAL xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr);
169 int XMLTOOL_DLLLOCAL verify_callback(X509_STORE_CTX* x509_ctx, void* arg);
171 SOAPTransport* CURLSOAPTransportFactory(const pair<const KeyInfoSource*,const char*>& dest)
173 return new CURLSOAPTransport(*dest.first, dest.second);
177 void xmltooling::registerSOAPTransports()
179 XMLToolingConfig& conf=XMLToolingConfig::getConfig();
180 conf.SOAPTransportManager.registerFactory("http", CURLSOAPTransportFactory);
181 conf.SOAPTransportManager.registerFactory("https", CURLSOAPTransportFactory);
184 void xmltooling::initSOAPTransports()
186 g_CURLPool=new CURLPool();
189 void xmltooling::termSOAPTransports()
195 CURLPool::~CURLPool()
197 for (poolmap_t::iterator i=m_bindingMap.begin(); i!=m_bindingMap.end(); i++) {
198 for (vector<CURL*>::iterator j=i->second.begin(); j!=i->second.end(); j++)
199 curl_easy_cleanup(*j);
204 CURL* CURLPool::get(const string& to, const char* endpoint)
207 xmltooling::NDC("get");
209 m_log.debug("getting connection handle to %s", endpoint);
211 poolmap_t::iterator i=m_bindingMap.find(to + "|" + endpoint);
213 if (i!=m_bindingMap.end()) {
214 // Move this pool to the front of the list.
215 m_pools.remove(&(i->second));
216 m_pools.push_front(&(i->second));
218 // If a free connection exists, return it.
219 if (!(i->second.empty())) {
220 CURL* handle=i->second.back();
221 i->second.pop_back();
224 m_log.debug("returning existing connection handle from pool");
230 m_log.debug("nothing free in pool, returning new connection handle");
232 // Create a new connection and set non-varying options.
233 CURL* handle=curl_easy_init();
236 curl_easy_setopt(handle,CURLOPT_NOPROGRESS,1);
237 curl_easy_setopt(handle,CURLOPT_NOSIGNAL,1);
238 curl_easy_setopt(handle,CURLOPT_FAILONERROR,1);
239 curl_easy_setopt(handle,CURLOPT_SSLVERSION,3);
240 curl_easy_setopt(handle,CURLOPT_SSL_VERIFYHOST,2);
241 curl_easy_setopt(handle,CURLOPT_HEADERFUNCTION,&curl_header_hook);
242 curl_easy_setopt(handle,CURLOPT_READFUNCTION,&curl_read_hook);
243 curl_easy_setopt(handle,CURLOPT_WRITEFUNCTION,&curl_write_hook);
244 curl_easy_setopt(handle,CURLOPT_DEBUGFUNCTION,&curl_debug_hook);
249 void CURLPool::put(const string& to, const char* endpoint, CURL* handle)
251 string key = to + "|" + endpoint;
253 poolmap_t::iterator i=m_bindingMap.find(key);
254 if (i==m_bindingMap.end())
255 m_pools.push_front(&(m_bindingMap.insert(poolmap_t::value_type(key,vector<CURL*>(1,handle))).first->second));
257 i->second.push_back(handle);
260 if (++m_size > 256) {
261 // Kick a handle out from the back of the bus.
263 vector<CURL*>* corpse=m_pools.back();
264 if (!corpse->empty()) {
265 killit=corpse->back();
271 // Move an empty pool up to the front so we don't keep hitting it.
273 m_pools.push_front(corpse);
278 curl_easy_cleanup(killit);
280 xmltooling::NDC("put");
282 m_log.info("conn_pool_max limit reached, dropping an old connection");
286 bool CURLSOAPTransport::setAuth(transport_auth_t authType, const char* username, const char* password) const
288 if (authType==transport_auth_none) {
289 if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,0)!=CURLE_OK)
291 return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,NULL)==CURLE_OK);
295 case transport_auth_basic: flag = CURLAUTH_BASIC; break;
296 case transport_auth_digest: flag = CURLAUTH_DIGEST; break;
297 case transport_auth_ntlm: flag = CURLAUTH_NTLM; break;
298 case transport_auth_gss: flag = CURLAUTH_GSSNEGOTIATE; break;
299 default: return false;
301 if (curl_easy_setopt(m_handle,CURLOPT_HTTPAUTH,flag)!=CURLE_OK)
303 string creds = string(username ? username : "") + ':' + (password ? password : "");
304 return (curl_easy_setopt(m_handle,CURLOPT_USERPWD,creds.c_str())==CURLE_OK);
307 const vector<string>& CURLSOAPTransport::getResponseHeader(const char* name) const
309 static vector<string> emptyVector;
311 map<string,vector<string> >::const_iterator i=m_response_headers.find(name);
312 if (i!=m_response_headers.end())
315 for (map<string,vector<string> >::const_iterator j=m_response_headers.begin(); j!=m_response_headers.end(); j++) {
316 #ifdef HAVE_STRCASECMP
317 if (!strcasecmp(j->first.c_str(), name))
319 if (!stricmp(j->first.c_str(), name))
327 string CURLSOAPTransport::getContentType() const
329 char* content_type=NULL;
330 curl_easy_getinfo(m_handle,CURLINFO_CONTENT_TYPE,&content_type);
331 return content_type ? content_type : "";
334 void CURLSOAPTransport::send(istream& in)
337 xmltooling::NDC ndc("send");
339 Category& log=Category::getInstance(XMLTOOLING_LOGCAT".SOAPTransport");
340 Category& log_curl=Category::getInstance(XMLTOOLING_LOGCAT".libcurl");
342 // By this time, the handle has been prepared with the URL to use and the
343 // caller should have executed any set functions to manipulate it.
345 // Setup standard per-call curl properties.
346 curl_easy_setopt(m_handle,CURLOPT_POST,1);
347 curl_easy_setopt(m_handle,CURLOPT_READDATA,&in);
348 curl_easy_setopt(m_handle,CURLOPT_FILE,&m_stream);
349 curl_easy_setopt(m_handle,CURLOPT_DEBUGDATA,&log_curl);
351 char curl_errorbuf[CURL_ERROR_SIZE];
353 curl_easy_setopt(m_handle,CURLOPT_ERRORBUFFER,curl_errorbuf);
354 if (log_curl.isDebugEnabled())
355 curl_easy_setopt(m_handle,CURLOPT_VERBOSE,1);
357 // Set request headers (possibly appended by hooks).
358 curl_easy_setopt(m_handle,CURLOPT_HTTPHEADER,m_headers);
360 if (m_ssl_callback || m_credResolver || m_trustEngine) {
361 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,xml_ssl_ctx_callback);
362 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,this);
365 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_FUNCTION,NULL);
366 curl_easy_setopt(m_handle,CURLOPT_SSL_CTX_DATA,NULL);
369 // Verification of the peer is via TrustEngine only.
370 curl_easy_setopt(m_handle,CURLOPT_SSL_VERIFYPEER,0);
373 log.info("sending SOAP message to %s", m_endpoint.c_str());
374 if (curl_easy_perform(m_handle) != CURLE_OK) {
375 log.error("failed communicating with SOAP endpoint: %s",
376 (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
378 string("CURLSOAPTransport::send() failed while contacting SOAP responder: ") +
379 (curl_errorbuf[0] ? curl_errorbuf : "no further information available"));
383 // callback to buffer headers from server
384 size_t xmltooling::curl_header_hook(void* ptr, size_t size, size_t nmemb, void* stream)
386 // only handle single-byte data
389 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(stream);
390 char* buf = (char*)malloc(nmemb + 1);
392 memset(buf,0,nmemb + 1);
393 memcpy(buf,ptr,nmemb);
394 char* sep=(char*)strchr(buf,':');
399 char* white=buf+nmemb-1;
400 while (isspace(*white))
402 ctx->m_response_headers[buf].push_back(sep);
410 // callback to send data to server
411 size_t xmltooling::curl_read_hook(void* ptr, size_t size, size_t nmemb, void* stream)
413 // *stream is actually an istream object
414 istream& buf=*(reinterpret_cast<istream*>(stream));
415 buf.read(reinterpret_cast<char*>(ptr),size*nmemb);
419 // callback to buffer data from server
420 size_t xmltooling::curl_write_hook(void* ptr, size_t size, size_t nmemb, void* stream)
422 size_t len = size*nmemb;
423 reinterpret_cast<stringstream*>(stream)->write(reinterpret_cast<const char*>(ptr),len);
427 // callback for curl debug data
428 int xmltooling::curl_debug_hook(CURL* handle, curl_infotype type, char* data, size_t len, void* ptr)
430 // *ptr is actually a logging object
432 CategoryStream log=reinterpret_cast<Category*>(ptr)->debugStream();
433 for (char* ch=data; len && (isprint(*ch) || isspace(*ch)); len--)
435 log << CategoryStream::ENDLINE;
439 int xmltooling::verify_callback(X509_STORE_CTX* x509_ctx, void* arg)
441 Category::getInstance("OpenSSL").debug("invoking X509 verify callback");
442 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
443 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(arg);
445 // Yes, this sucks. I'd use TLS, but there's no really obvious spot to put the thread key
446 // and global variables suck too. We can't access the X509_STORE_CTX depth directly because
447 // OpenSSL only copies it into the context if it's >=0, and the unsigned pointer may be
448 // negative in the SSL structure's int member.
449 CURLSOAPTransport* ctx = reinterpret_cast<CURLSOAPTransport*>(
450 SSL_get_verify_depth(
451 reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(x509_ctx,SSL_get_ex_data_X509_STORE_CTX_idx()))
456 // Bypass name check (handled for us by curl).
457 if (!ctx->m_trustEngine->validate(x509_ctx->cert,x509_ctx->untrusted,ctx->m_peer,false,ctx->m_keyResolver)) {
458 x509_ctx->error=X509_V_ERR_APPLICATION_VERIFICATION; // generic error, check log for plugin specifics
462 // Signal success. Hopefully it doesn't matter what's actually in the structure now.
466 // callback to invoke a caller-defined SSL callback
467 CURLcode xmltooling::xml_ssl_ctx_callback(CURL* curl, SSL_CTX* ssl_ctx, void* userptr)
469 CURLSOAPTransport* conf = reinterpret_cast<CURLSOAPTransport*>(userptr);
470 if (conf->m_credResolver)
471 conf->m_credResolver->attach(ssl_ctx);
473 if (conf->m_trustEngine) {
474 SSL_CTX_set_verify(ssl_ctx,SSL_VERIFY_PEER,NULL);
475 #if (OPENSSL_VERSION_NUMBER >= 0x00907000L)
476 // With 0.9.7, we can pass a callback argument directly.
477 SSL_CTX_set_cert_verify_callback(ssl_ctx,verify_callback,userptr);
479 // With 0.9.6, there's no argument, so we're going to use a really embarrassing hack and
480 // stuff the argument in the depth property where it will get copied to the context object
481 // that's handed to the callback.
482 SSL_CTX_set_cert_verify_callback(ssl_ctx,reinterpret_cast<int (*)()>(verify_callback),NULL);
483 SSL_CTX_set_verify_depth(ssl_ctx,reinterpret_cast<int>(userptr));
487 if (!conf->m_ssl_callback(ssl_ctx,conf->m_ssl_userptr))
488 return CURLE_SSL_CERTPROBLEM;