43 #include "blocxx/BLOCXX_config.h"
45 #ifdef BLOCXX_HAVE_OPENSSL
51 #include <openssl/err.h>
56 namespace BLOCXX_NAMESPACE
69 void sslWaitForIO(SocketBaseImpl& s,
int type)
71 if(type == SSL_ERROR_WANT_READ)
81 void shutdownSSL(SSL* ssl)
84 if (SSL_shutdown(ssl) == -1)
92 void connectWithSSL(SSL* ssl, SocketBaseImpl& s)
97 int cc = SSL_connect(ssl);
98 cc = SSL_get_error(ssl, cc);
99 while((cc == SSL_ERROR_WANT_READ
100 || cc == SSL_ERROR_WANT_WRITE)
101 && retries < BLOCXX_SSL_RETRY_LIMIT)
105 cc = SSL_connect(ssl);
106 cc = SSL_get_error(ssl, cc);
110 if (cc != SSL_ERROR_NONE)
112 BLOCXX_THROW(SSLException, Format(
"SSL connect error: %1", SSLCtxMgr::getOpenSSLErrorDescription()).c_str());
116 int acceptSSL(SSL* ssl, SocketBaseImpl& s, String& errorDescription)
120 int cc = SSL_ERROR_WANT_READ;
121 while((cc == SSL_ERROR_WANT_READ || cc == SSL_ERROR_WANT_WRITE)
122 && retries < BLOCXX_SSL_RETRY_LIMIT)
126 cc = SSL_accept(ssl);
127 cc = SSL_get_error(ssl, cc);
130 if (cc == SSL_ERROR_NONE)
136 errorDescription = SSLCtxMgr::getOpenSSLErrorDescription();
153 : SocketBaseImpl(fd, addrType)
157 m_ssl = SSL_new(sslCtx->getSSLCtx());
163 if (SSL_set_ex_data(m_ssl, SSLServerCtx::SSL_DATA_INDEX, &m_owctx) == 0)
168 BIO* bio = BIO_new_socket(fd, BIO_NOCLOSE);
175 SSL_set_bio(m_ssl, bio, bio);
176 String errorDescription;
177 if (acceptSSL(m_ssl, *
this, errorDescription) != 0)
182 BLOCXX_THROW(
SSLException, Format(
"SSLSocketImpl ctor: SSL accept error while connecting to %1: %2", m_peerAddress.toString(), errorDescription).c_str());
184 if (!SSLCtxMgr::checkClientCert(m_ssl, m_peerAddress.getName()))
197 : SocketBaseImpl(fd, addrType)
200 m_ssl = SSL_new(SSLCtxMgr::getSSLCtxServer());
206 m_sbio = BIO_new_socket(fd, BIO_NOCLOSE);
213 SSL_set_bio(m_ssl, m_sbio, m_sbio);
214 String errorDescription;
215 if (acceptSSL(m_ssl, *
this, errorDescription) != 0)
220 BLOCXX_THROW(
SSLException, Format(
"SSLSocketImpl ctor: SSL accept error while connecting to %1: %2", m_peerAddress.toString(), errorDescription).c_str());
222 if (!SSLCtxMgr::checkClientCert(m_ssl, m_peerAddress.getName()))
232 : SocketBaseImpl(addr)
258 #if defined(BLOCXX_WIN32)
263 st.networkevents = FD_READ | FD_WRITE;
293 BLOCXX_THROW(SSLException, Format(
"SSL_new failed: %1", SSLCtxMgr::getOpenSSLErrorDescription()).c_str());
299 BLOCXX_THROW(SSLException, Format(
"BIO_new_socket failed: %1", SSLCtxMgr::getOpenSSLErrorDescription()).c_str());
303 connectWithSSL(
m_ssl, *
this);
307 BLOCXX_THROW(SSLException,
"Failed to validate peer certificate");
315 #if defined(BLOCXX_WIN32)
332 return SSLCtxMgr::sslWrite(
m_ssl, static_cast<const char*>(dataOut),
339 return SSLCtxMgr::sslRead(
m_ssl, static_cast<char*>(dataIn),
353 return (
m_owctx.peerCertPassedVerify == OWSSLContext::VERIFY_PASS);
363 if (SSL_pending(
m_ssl))
374 #endif // #ifdef BLOCXX_HAVE_OPENSSL