Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TSSLSocket.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id: TSSLSocket.cxx
2// Author: Alejandro Alvarez 16/09/2011
3
4/*************************************************************************
5 * Copyright (C) 1995-2011, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/**
13\file TSSLSocket.cxx
14\class TSSLSocket
15\brief A TSocket wrapped in by SSL.
16\note This class deals with sockets: the user is entirely responsible for the security of their usage, for example, but
17not limited to, the management of the connections to said sockets.
18**/
19
20
21#include <openssl/ssl.h>
22#include "TSSLSocket.h"
23#include "TSystem.h"
24#include <iostream>
25#include <cstdio>
26#include "strlcpy.h"
27
28// Static properties
33
34////////////////////////////////////////////////////////////////////////////////
35// SSL debugging
36
37void ssl_info_callback(const SSL* ssl, int where, int ret)
38{
39 if (ret == 0) {
40 std::cout << "-- ssl_info_callback: error occured.\n";
41 return;
42 }
43 if (where) {
44 std::cout << " - " << SSL_state_string_long(ssl);
45 std::cout << " - " << SSL_state_string(ssl);
46 std::cout << std::endl;
47 }
48}
49
50////////////////////////////////////////////////////////////////////////////////
51/// Wraps the socket with OpenSSL.
52
54{
56
57 // New context
58 if (!(fSSLCtx = SSL_CTX_new(SSLv23_method()))) {
59 Error("WrapWithSSL", "the context could not be created");
60 goto wrapFailed;
61 }
62
64 Error("WrapWithSSL", "could not set the CA file and/or the CA path");
65 goto wrapFailed;
66 }
67
69 Error("WrapWithSSL", "could not set the client certificate");
70 goto wrapFailed;
71 }
72
74 Error("WrapWithSSL", "could not set the client private key");
75 goto wrapFailed;
76 }
77
78 // New SSL structure
79 if (!(fSSL = SSL_new(fSSLCtx))) {
80 Error("WrapWithSSL", "cannot create the ssl struct");
81 goto wrapFailed;
82 }
83
84 if (gDebug > 0)
86
87 // Bind to the socket
88 if (SSL_set_fd(fSSL, fSocket) != 1) {
89 Error("WrapWithSSL", "cannot bind to the socket %d", fSocket);
90 goto wrapFailed;
91 }
92
93 // Open connection
94 if (SSL_connect(fSSL) != 1) {
95 Error("WrapWithSSL", "cannot connect");
96 goto wrapFailed;
97 }
98
99 return;
100
102 Close();
103 return;
104}
105
106////////////////////////////////////////////////////////////////////////////////
107
109
110////////////////////////////////////////////////////////////////////////////////
111
117
118////////////////////////////////////////////////////////////////////////////////
119
125
126////////////////////////////////////////////////////////////////////////////////
127
128TSSLSocket::TSSLSocket(const char *host, const char *service, Int_t tcpwindowsize)
130{
131 WrapWithSSL();
132}
133
134////////////////////////////////////////////////////////////////////////////////
135
137 : TSocket(url, port, tcpwindowsize)
138{
139 WrapWithSSL();
140}
141
142////////////////////////////////////////////////////////////////////////////////
143
148
149////////////////////////////////////////////////////////////////////////////////
150
152{
153 WrapWithSSL();
154}
155
156////////////////////////////////////////////////////////////////////////////////
157
159{
160 WrapWithSSL();
161}
162
163////////////////////////////////////////////////////////////////////////////////
164
166{
167 WrapWithSSL();
168}
169
170////////////////////////////////////////////////////////////////////////////////
171/// Close gracefully the connection, and free SSL structures.
172
174{
175 Close();
176 if (fSSL)
177 SSL_free(fSSL);
178 if (fSSLCtx)
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Close the SSL connection.
184
191
192////////////////////////////////////////////////////////////////////////////////
193/// Set up the static configuration variables.
194
195void TSSLSocket::SetUpSSL(const char *cafile, const char *capath,
196 const char *ucert, const char *ukey)
197{
198 if (cafile)
200 if (capath)
202 if (ucert)
204 if (ukey)
206}
207
208////////////////////////////////////////////////////////////////////////////////
209
211{
212 Error("Recv", "not implemented");
213 return -1;
214}
215
216////////////////////////////////////////////////////////////////////////////////
217/// Receive a raw buffer of specified length bytes.
218
220{
222
223 if (fSocket == -1) return -1;
224 if (length == 0) return 0;
225
227
228 Int_t n;
229 Int_t offset = 0;
231
232 // SSL_read/SSL_peek may not return the total length at once
233 while (remain > 0) {
234 if (opt == kPeek)
235 n = SSL_peek(fSSL, (char*)buffer + offset, (int)remain);
236 else
237 n = SSL_read(fSSL, (char*)buffer + offset, (int)remain);
238
239 if (n <= 0) {
240 if (gDebug > 0)
241 Error("RecvRaw", "failed to read from the socket");
242
244 // Connection closed, reset or broken
246 SSL_set_quiet_shutdown(fSSL, 1); // Socket is gone, sending "close notify" will fail
247 Close();
248 }
249 return n;
250 }
251
252 // When peeking, just return the available data, don't loop. Otherwise,
253 // we may copy the same chunk of data multiple times into the
254 // output buffer, for instance when there is no more recent data
255 // in the socket's internal reception buffers.
256 // Note that in this case we don't update the counters of data received
257 // through this socket. They will be updated when the data is actually
258 // read. This avoids double counting.
259 if (opt == kPeek) return n;
260
261 offset += n;
262 remain -= n;
263 }
264
267
268 Touch(); // update usage timestamp
269
270 return offset;
271}
272
273////////////////////////////////////////////////////////////////////////////////
274
275Int_t TSSLSocket::Send(const TMessage & /* mess */)
276{
277 Error("Send", "not implemented");
278 return -1;
279}
280
281////////////////////////////////////////////////////////////////////////////////
282/// Send a raw buffer of specified length.
283
285{
287
288 if (fSocket == -1) return -1;
289
291
292 Int_t nsent;
293 if ((nsent = SSL_write(fSSL, buffer, (int)length)) <= 0) {
295 // Connection reset or broken: close
297 Close();
298 }
299 return nsent;
300 }
301
302 fBytesSent += nsent;
304
305 Touch(); // update usage timestamp
306
307 return nsent;
308}
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Int_t gDebug
Definition TROOT.cxx:622
void ssl_info_callback(const SSL *ssl, int where, int ret)
ESendRecvOptions
Definition TSystem.h:242
@ kPeek
Definition TSystem.h:245
static void ssl_info_callback(const SSL *ssl, int what, int ret)
Definition civetweb.c:16263
This class represents an Internet Protocol (IP) address.
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:864
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
void ResetBit(UInt_t f)
Definition TObject.h:204
A TSocket wrapped in by SSL.
Definition TSSLSocket.h:28
Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault) override
Send a raw buffer of specified length.
Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault) override
Receive a raw buffer of specified length bytes.
SSL * fSSL
Definition TSSLSocket.h:41
SSL_CTX * fSSLCtx
Definition TSSLSocket.h:40
static char fgSSLUKey[]
Definition TSSLSocket.h:37
void Close(Option_t *option="") override
Close the SSL connection.
static char fgSSLCAPath[]
Definition TSSLSocket.h:35
Int_t Recv(TMessage *&mess) override
Receive a TMessage object.
void WrapWithSSL()
Wraps the socket with OpenSSL.
static char fgSSLCAFile[]
Definition TSSLSocket.h:34
static char fgSSLUCert[]
Definition TSSLSocket.h:36
virtual ~TSSLSocket()
Close gracefully the connection, and free SSL structures.
Int_t Send(const TMessage &mess) override
Send a TMessage object.
static void SetUpSSL(const char *cafile, const char *capath, const char *ucert, const char *ukey)
Set up the static configuration variables.
This class implements client sockets.
Definition TSocket.h:41
Int_t fSocket
Definition TSocket.h:69
static ULong64_t fgBytesRecv
Definition TSocket.h:78
@ kBrokenConn
Definition TSocket.h:49
virtual void Close(Option_t *opt="")
Close the socket.
Definition TSocket.cxx:391
void Touch()
Definition TSocket.h:157
static ULong64_t fgBytesSent
Definition TSocket.h:79
UInt_t fBytesSent
Definition TSocket.h:61
UInt_t fBytesRecv
Definition TSocket.h:60
static void ResetErrno()
Static function resetting system error number.
Definition TSystem.cxx:284
const Int_t n
Definition legend1.C:16
#define SSL_ERROR_SYSCALL
struct ssl_st SSL
#define SSL_ERROR_ZERO_RETURN