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// //
14// TSSLSocket //
15// //
16// A TSocket wrapped in by SSL. //
17// //
18//////////////////////////////////////////////////////////////////////////
19
20#include <openssl/ssl.h>
21#include "TSSLSocket.h"
22#include "TSystem.h"
23#include <iostream>
24#include <cstdio>
25#include "strlcpy.h"
26
27// Static properties
32
33////////////////////////////////////////////////////////////////////////////////
34// SSL debugging
35
36void ssl_info_callback(const SSL* ssl, int where, int ret)
37{
38 if (ret == 0) {
39 std::cout << "-- ssl_info_callback: error occured.\n";
40 return;
41 }
42 if (where) {
43 std::cout << " - " << SSL_state_string_long(ssl);
44 std::cout << " - " << SSL_state_string(ssl);
45 std::cout << std::endl;
46 }
47}
48
49////////////////////////////////////////////////////////////////////////////////
50/// Wraps the socket with OpenSSL.
51
53{
55
56 // New context
57 if (!(fSSLCtx = SSL_CTX_new(SSLv23_method()))) {
58 Error("WrapWithSSL", "the context could not be created");
59 goto wrapFailed;
60 }
61
63 Error("WrapWithSSL", "could not set the CA file and/or the CA path");
64 goto wrapFailed;
65 }
66
68 Error("WrapWithSSL", "could not set the client certificate");
69 goto wrapFailed;
70 }
71
73 Error("WrapWithSSL", "could not set the client private key");
74 goto wrapFailed;
75 }
76
77 // New SSL structure
78 if (!(fSSL = SSL_new(fSSLCtx))) {
79 Error("WrapWithSSL", "cannot create the ssl struct");
80 goto wrapFailed;
81 }
82
83 if (gDebug > 0)
85
86 // Bind to the socket
87 if (SSL_set_fd(fSSL, fSocket) != 1) {
88 Error("WrapWithSSL", "cannot bind to the socket %d", fSocket);
89 goto wrapFailed;
90 }
91
92 // Open connection
93 if (SSL_connect(fSSL) != 1) {
94 Error("WrapWithSSL", "cannot connect");
95 goto wrapFailed;
96 }
97
98 return;
99
101 Close();
102 return;
103}
104
105////////////////////////////////////////////////////////////////////////////////
106
108
109////////////////////////////////////////////////////////////////////////////////
110
116
117////////////////////////////////////////////////////////////////////////////////
118
124
125////////////////////////////////////////////////////////////////////////////////
126
127TSSLSocket::TSSLSocket(const char *host, const char *service, Int_t tcpwindowsize)
129{
130 WrapWithSSL();
131}
132
133////////////////////////////////////////////////////////////////////////////////
134
136 : TSocket(url, port, tcpwindowsize)
137{
138 WrapWithSSL();
139}
140
141////////////////////////////////////////////////////////////////////////////////
142
147
148////////////////////////////////////////////////////////////////////////////////
149
151{
152 WrapWithSSL();
153}
154
155////////////////////////////////////////////////////////////////////////////////
156
158{
159 WrapWithSSL();
160}
161
162////////////////////////////////////////////////////////////////////////////////
163
165{
166 WrapWithSSL();
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Close gracefully the connection, and free SSL structures.
171
173{
174 Close();
175 if (fSSL)
176 SSL_free(fSSL);
177 if (fSSLCtx)
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Close the SSL connection.
183
190
191////////////////////////////////////////////////////////////////////////////////
192/// Set up the static configuration variables.
193
194void TSSLSocket::SetUpSSL(const char *cafile, const char *capath,
195 const char *ucert, const char *ukey)
196{
197 if (cafile)
199 if (capath)
201 if (ucert)
203 if (ukey)
205}
206
207////////////////////////////////////////////////////////////////////////////////
208
210{
211 Error("Recv", "not implemented");
212 return -1;
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Receive a raw buffer of specified length bytes.
217
219{
221
222 if (fSocket == -1) return -1;
223 if (length == 0) return 0;
224
226
227 Int_t n;
228 Int_t offset = 0;
230
231 // SSL_read/SSL_peek may not return the total length at once
232 while (remain > 0) {
233 if (opt == kPeek)
234 n = SSL_peek(fSSL, (char*)buffer + offset, (int)remain);
235 else
236 n = SSL_read(fSSL, (char*)buffer + offset, (int)remain);
237
238 if (n <= 0) {
239 if (gDebug > 0)
240 Error("RecvRaw", "failed to read from the socket");
241
243 // Connection closed, reset or broken
245 SSL_set_quiet_shutdown(fSSL, 1); // Socket is gone, sending "close notify" will fail
246 Close();
247 }
248 return n;
249 }
250
251 // When peeking, just return the available data, don't loop. Otherwise,
252 // we may copy the same chunk of data multiple times into the
253 // output buffer, for instance when there is no more recent data
254 // in the socket's internal reception buffers.
255 // Note that in this case we don't update the counters of data received
256 // through this socket. They will be updated when the data is actually
257 // read. This avoids double counting.
258 if (opt == kPeek) return n;
259
260 offset += n;
261 remain -= n;
262 }
263
266
267 Touch(); // update usage timestamp
268
269 return offset;
270}
271
272////////////////////////////////////////////////////////////////////////////////
273
274Int_t TSSLSocket::Send(const TMessage & /* mess */)
275{
276 Error("Send", "not implemented");
277 return -1;
278}
279
280////////////////////////////////////////////////////////////////////////////////
281/// Send a raw buffer of specified length.
282
284{
286
287 if (fSocket == -1) return -1;
288
290
291 Int_t nsent;
292 if ((nsent = SSL_write(fSSL, buffer, (int)length)) <= 0) {
294 // Connection reset or broken: close
296 Close();
297 }
298 return nsent;
299 }
300
301 fBytesSent += nsent;
303
304 Touch(); // update usage timestamp
305
306 return nsent;
307}
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
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.
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:389
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