-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClTls.cc
316 lines (282 loc) · 12.7 KB
/
XrdClTls.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//------------------------------------------------------------------------------
// Copyright (c) 2011-2012 by European Organization for Nuclear Research (CERN)
// Author: Michal Simon <simonm@cern.ch>
//------------------------------------------------------------------------------
// XRootD is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// XRootD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with XRootD. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------
#include "XrdCl/XrdClTls.hh"
#include "XrdCl/XrdClPoller.hh"
#include "XrdCl/XrdClSocket.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include "XrdCl/XrdClLog.hh"
#include "XrdCl/XrdClConstants.hh"
#include "XrdTls/XrdTls.hh"
#include "XrdTls/XrdTlsContext.hh"
namespace
{
//------------------------------------------------------------------------
// Helper class for setting the message callback for the TLS layer for
// logging purposes
//------------------------------------------------------------------------
struct SetTlsMsgCB
{
//----------------------------------------------------------------------
// The message callback
//----------------------------------------------------------------------
static void MsgCallBack(const char *tid, const char *msg, bool sslmsg)
{
XrdCl::Log *log = XrdCl::DefaultEnv::GetLog();
if( sslmsg )
log->Debug( XrdCl::TlsMsg, "[%s] %s", tid, msg );
else
log->Error( XrdCl::TlsMsg, "[%s] %s", tid, msg );
}
inline static void Once()
{
static SetTlsMsgCB instance;
}
private:
//--------------------------------------------------------------------
// Constructor. Sets the callback, there should be only one static
// instance
//--------------------------------------------------------------------
inline SetTlsMsgCB()
{
XrdTls::SetMsgCB( MsgCallBack );
}
};
//------------------------------------------------------------------------
// Helper function for setting the CA directory in TLS context
//------------------------------------------------------------------------
static const char* GetCaDir()
{
static const char *envval = getenv("X509_CERT_DIR");
static const std::string cadir = envval ? envval :
"/etc/grid-security/certificates";
return cadir.c_str();
}
}
namespace XrdCl
{
//------------------------------------------------------------------------
// Constructor
//------------------------------------------------------------------------
Tls::Tls( Socket *socket, AsyncSocketHandler *socketHandler ) : pSocket( socket ), pTlsHSRevert( None ), pSocketHandler( socketHandler )
{
//----------------------------------------------------------------------
// Set the message callback for TLS layer
//----------------------------------------------------------------------
SetTlsMsgCB::Once();
//----------------------------------------------------------------------
// we only need one instance of TLS
//----------------------------------------------------------------------
static XrdTlsContext tlsContext( 0, 0, GetCaDir(), 0, 0 );
//----------------------------------------------------------------------
// If the context is not valid throw an exception! We throw generic
// exception as this will be translated to TlsError anyway.
//----------------------------------------------------------------------
if( !tlsContext.Context() ) throw std::exception();
pTls.reset(
new XrdTlsSocket( tlsContext, pSocket->GetFD(), XrdTlsSocket::TLS_RNB_WNB,
XrdTlsSocket::TLS_HS_NOBLK, true ) );
}
//------------------------------------------------------------------------
// Establish a TLS/SSL session and perform host verification.
//------------------------------------------------------------------------
Status Tls::Connect( const std::string &thehost, XrdNetAddrInfo *netInfo )
{
XrdTls::RC error = pTls->Connect( thehost.c_str(), netInfo );
Status status = ToStatus( error );
//--------------------------------------------------------------------------
// There's no follow up if the read simply failed
//--------------------------------------------------------------------------
if( !status.IsOK() ) return status;
if( pTls->NeedHandShake() )
{
//------------------------------------------------------------------------
// Make sure the socket is uncorked so the TLS hand-shake can go through
//------------------------------------------------------------------------
if( pSocket->IsCorked() )
{
Status st = pSocket->Uncork();
if( !st.IsOK() ) return st;
}
//----------------------------------------------------------------------
// Check if TLS hand-shake wants to write something
//----------------------------------------------------------------------
if( error == XrdTls::TLS_WantWrite )
{
Status st = pSocketHandler->EnableUplink();
if( !st.IsOK() ) return st;
}
//----------------------------------------------------------------------
// Otherwise disable uplink
//----------------------------------------------------------------------
else if( error == XrdTls::TLS_WantRead )
{
Status st = pSocketHandler->DisableUplink();
if( !st.IsOK() ) return st;
}
}
return status;
}
Status Tls::Read( char *buffer, size_t size, int &bytesRead )
{
//--------------------------------------------------------------------------
// If necessary, TLS_read() will negotiate a TLS/SSL session, so we don't
// have to explicitly call connect or do_handshake.
//--------------------------------------------------------------------------
XrdTls::RC error = pTls->Read( buffer, size, bytesRead );
Status status = ToStatus( error );
//--------------------------------------------------------------------------
// There's no follow up if the read simply failed
//--------------------------------------------------------------------------
if( !status.IsOK() ) return status;
if( pTls->NeedHandShake() )
{
//------------------------------------------------------------------------
// Make sure the socket is uncorked so the TLS hand-shake can go through
//------------------------------------------------------------------------
if( pSocket->IsCorked() )
{
Status st = pSocket->Uncork();
if( !st.IsOK() ) return st;
}
//----------------------------------------------------------------------
// Check if we need to switch on a revert state
//----------------------------------------------------------------------
if( error == XrdTls::TLS_WantWrite )
{
pTlsHSRevert = ReadOnWrite;
Status st = pSocketHandler->EnableUplink();
if( !st.IsOK() ) status = st;
//--------------------------------------------------------------------
// Return early so the revert state wont get cleared
//--------------------------------------------------------------------
return status;
}
}
//------------------------------------------------------------------------
// If we got up until here we need to clear the revert state
//------------------------------------------------------------------------
if( pTlsHSRevert == ReadOnWrite )
{
Status st = pSocketHandler->DisableUplink();
if( !st.IsOK() ) status = st;
}
pTlsHSRevert = None;
//------------------------------------------------------------------------
// If we didn't manage to read any data wait for another read event
//------------------------------------------------------------------------
if( bytesRead == 0 )
return Status( stOK, suRetry );
return status;
}
Status Tls::Send( const char *buffer, size_t size, int &bytesWritten )
{
//--------------------------------------------------------------------------
// If necessary, TLS_write() will negotiate a TLS/SSL session, so we don't
// have to explicitly call connect or do_handshake.
//--------------------------------------------------------------------------
XrdTls::RC error = pTls->Write( buffer, size, bytesWritten );
Status status = ToStatus( error );
//--------------------------------------------------------------------------
// There's no follow up if the write simply failed
//--------------------------------------------------------------------------
if( !status.IsOK() ) return status;
//--------------------------------------------------------------------------
// We are in the middle of a TLS hand-shake
//--------------------------------------------------------------------------
if( pTls->NeedHandShake() )
{
//------------------------------------------------------------------------
// Make sure the socket is uncorked so the TLS hand-shake can go through
//------------------------------------------------------------------------
if( pSocket->IsCorked() )
{
Status st = pSocket->Uncork();
if( !st.IsOK() ) return st;
}
//------------------------------------------------------------------------
// Check if we need to switch on a revert state
//------------------------------------------------------------------------
if( error == XrdTls::TLS_WantRead )
{
pTlsHSRevert = WriteOnRead;
Status st = pSocketHandler->DisableUplink();
if( !st.IsOK() ) status = st;
//----------------------------------------------------------------------
// Return early so the revert state wont get cleared
//----------------------------------------------------------------------
return status;
}
}
//--------------------------------------------------------------------------
// If we got up until here we need to clear the revert state
//--------------------------------------------------------------------------
if( pTlsHSRevert == WriteOnRead )
{
Status st = pSocketHandler->EnableUplink();
if( !st.IsOK() ) status = st;
}
pTlsHSRevert = None;
//------------------------------------------------------------------------
// If we didn't manage to read any data wait for another write event
//
// Adding this by symmetry, never actually experienced this (for reads
// it has been experienced)
//------------------------------------------------------------------------
if( bytesWritten == 0 )
return Status( stOK, suRetry );
return status;
}
Status Tls::ToStatus( int error )
{
switch( error )
{
case XrdTls::TLS_AOK: return Status();
case XrdTls::TLS_WantConnect:
case XrdTls::TLS_WantWrite:
case XrdTls::TLS_WantRead: return Status( stOK, suRetry, error );
case XrdTls::TLS_SYS_Error: return Status( stError, errTlsError, errno );
default:
return Status( stError, errTlsError, error );
}
}
//------------------------------------------------------------------------
// Map:
// * in case the TLS layer requested reads on writes map
// ReadyToWrite to ReadyToRead
// * in case the TLS layer requested writes on reads map
// ReadyToRead to ReadyToWrite
//------------------------------------------------------------------------
uint8_t Tls::MapEvent( uint8_t event )
{
if( pTlsHSRevert == ReadOnWrite )
{
//------------------------------------------------------------------------
// In this case we would like to call the OnRead routine on the Write event
//------------------------------------------------------------------------
if( event & SocketHandler::ReadyToWrite ) return SocketHandler::ReadyToRead;
}
else if( pTlsHSRevert == WriteOnRead )
{
//------------------------------------------------------------------------
// In this case we would like to call the OnWrite routine on the Read event
//------------------------------------------------------------------------
if( event & SocketHandler::ReadyToRead ) return SocketHandler::ReadyToWrite;
}
return event;
}
}