20 #include <openssl/ssl.h>
27 char TSSLSocket::fgSSLCAFile[FILENAME_MAX] =
"";
28 char TSSLSocket::fgSSLCAPath[FILENAME_MAX] =
"";
29 char TSSLSocket::fgSSLUCert[FILENAME_MAX] =
"";
30 char TSSLSocket::fgSSLUKey[FILENAME_MAX] =
"";
35 void TSSLSocket::WrapWithSSL(
void)
40 if (!(fSSLCtx = SSL_CTX_new(SSLv23_method()))) {
41 Error(
"WrapWithSSL",
"the context could not be created");
45 if ((fgSSLCAFile[0] || fgSSLCAPath[0]) && SSL_CTX_load_verify_locations(fSSLCtx, fgSSLCAFile, fgSSLCAPath) == 0) {
46 Error(
"WrapWithSSL",
"could not set the CA file and/or the CA path");
50 if (fgSSLUCert[0] && SSL_CTX_use_certificate_chain_file(fSSLCtx, fgSSLUCert) == 0) {
51 Error(
"WrapWithSSL",
"could not set the client certificate");
55 if (fgSSLUKey[0] && SSL_CTX_use_PrivateKey_file(fSSLCtx, fgSSLUKey, SSL_FILETYPE_PEM) == 0) {
56 Error(
"WrapWithSSL",
"could not set the client private key");
61 if (!(fSSL = SSL_new(fSSLCtx))) {
62 Error(
"WrapWithSSL",
"cannot create the ssl struct");
67 if (SSL_set_fd(fSSL, fSocket) != 1) {
68 Error(
"WrapWithSSL",
"cannot bind to the socket %d", fSocket);
73 if (SSL_connect(fSSL) != 1) {
74 Error(
"WrapWithSSL",
"cannot connect");
91 TSSLSocket::TSSLSocket(TInetAddress addr,
const char *service, Int_t tcpwindowsize)
92 : TSocket(addr, service, tcpwindowsize)
99 TSSLSocket::TSSLSocket(TInetAddress addr, Int_t port, Int_t tcpwindowsize)
100 : TSocket(addr, port, tcpwindowsize)
107 TSSLSocket::TSSLSocket(
const char *host,
const char *service, Int_t tcpwindowsize)
108 : TSocket(host, service, tcpwindowsize)
115 TSSLSocket::TSSLSocket(
const char *url, Int_t port, Int_t tcpwindowsize)
116 : TSocket(url, port, tcpwindowsize)
123 TSSLSocket::TSSLSocket(
const char *sockpath) : TSocket(sockpath)
130 TSSLSocket::TSSLSocket(Int_t desc) : TSocket(desc)
137 TSSLSocket::TSSLSocket(Int_t desc,
const char *sockpath) : TSocket(desc, sockpath)
144 TSSLSocket::TSSLSocket(
const TSSLSocket &s) : TSocket(s)
152 TSSLSocket::~TSSLSocket()
158 SSL_CTX_free(fSSLCtx);
164 void TSSLSocket::Close(Option_t *option)
168 TSocket::Close(option);
174 void TSSLSocket::SetUpSSL(
const char *cafile,
const char *capath,
175 const char *ucert,
const char *ukey)
178 strlcpy(fgSSLCAFile, cafile, FILENAME_MAX);
180 strlcpy(fgSSLCAPath, capath, FILENAME_MAX);
182 strlcpy(fgSSLUCert, ucert, FILENAME_MAX);
184 strlcpy(fgSSLUKey, ukey, FILENAME_MAX);
189 Int_t TSSLSocket::Recv(TMessage *& )
191 Error(
"Recv",
"not implemented");
198 Int_t TSSLSocket::RecvRaw(
void *buffer, Int_t length, ESendRecvOptions opt)
200 TSystem::ResetErrno();
202 if (fSocket == -1)
return -1;
203 if (length == 0)
return 0;
205 ResetBit(TSocket::kBrokenConn);
209 Int_t remain = length;
214 n = SSL_peek(fSSL, (
char*)buffer + offset, (
int)remain);
216 n = SSL_read(fSSL, (
char*)buffer + offset, (
int)remain);
220 Error(
"RecvRaw",
"failed to read from the socket");
222 if (SSL_get_error(fSSL, n) == SSL_ERROR_ZERO_RETURN || SSL_get_error(fSSL, n) == SSL_ERROR_SYSCALL) {
224 SetBit(TSocket::kBrokenConn);
225 SSL_set_quiet_shutdown(fSSL, 1);
238 if (opt == kPeek)
return n;
244 fBytesRecv += length;
245 fgBytesRecv += length;
254 Int_t TSSLSocket::Send(
const TMessage & )
256 Error(
"Send",
"not implemented");
263 Int_t TSSLSocket::SendRaw(
const void *buffer, Int_t length, ESendRecvOptions )
265 TSystem::ResetErrno();
267 if (fSocket == -1)
return -1;
269 ResetBit(TSocket::kBrokenConn);
272 if ((nsent = SSL_write(fSSL, buffer, (
int)length)) <= 0) {
273 if (SSL_get_error(fSSL, nsent) == SSL_ERROR_ZERO_RETURN) {
275 SetBit(TSocket::kBrokenConn);
282 fgBytesSent += nsent;