-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClTls.cc
365 lines (322 loc) · 14.3 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//------------------------------------------------------------------------------
// 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"
#include <string>
#include <stdexcept>
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 );
XrdTls::SetDebug( TlsDbgLvl(), MsgCallBack );
}
//--------------------------------------------------------------------
// Get TLS debug level
//--------------------------------------------------------------------
static int TlsDbgLvl()
{
XrdCl::Env *env = XrdCl::DefaultEnv::GetEnv();
std::string tlsDbgLvl;
env->GetString( "TlsDbgLvl", tlsDbgLvl );
if( tlsDbgLvl == "OFF" ) return XrdTls::dbgOFF;
if( tlsDbgLvl == "CTX" ) return XrdTls::dbgCTX;
if( tlsDbgLvl == "SOK" ) return XrdTls::dbgSOK;
if( tlsDbgLvl == "SIO" ) return XrdTls::dbgSIO;
if( tlsDbgLvl == "ALL" ) return XrdTls::dbgALL;
if( tlsDbgLvl == "OUT" ) return XrdTls::dbgOUT;
return XrdTls::dbgOFF;
}
};
//------------------------------------------------------------------------
// 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
//----------------------------------------------------------------------
std::string emsg;
static XrdTlsContext tlsContext( 0, 0, GetCaDir(), 0, 0, &emsg );
//----------------------------------------------------------------------
// If the context is not valid throw an exception! We throw generic
// exception as this will be translated to TlsError anyway.
//----------------------------------------------------------------------
if( !tlsContext.isOK() ) throw std::runtime_error( emsg );
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.
//------------------------------------------------------------------------
XRootDStatus Tls::Connect( const std::string &thehost, XrdNetAddrInfo *netInfo )
{
std::string errmsg;
const char *verhost = 0;
if( thehost != "localhost" && thehost != "127.0.0.1" && thehost != "[::1]" )
verhost = thehost.c_str();
XrdTls::RC error = pTls->Connect( verhost, &errmsg );
XRootDStatus status = ToStatus( error );
if( !status.IsOK() )
status.SetErrorMessage( errmsg );
//--------------------------------------------------------------------------
// There's no follow up if the read simply failed
//--------------------------------------------------------------------------
if( !status.IsOK() )
{
XrdCl::Log *log = XrdCl::DefaultEnv::GetLog();
log->Error( XrdCl::TlsMsg, "Failed to do TLS connect: %s", errmsg.c_str() );
return status;
}
if( pTls->NeedHandShake() )
{
//------------------------------------------------------------------------
// Make sure the socket is uncorked so the TLS hand-shake can go through
//------------------------------------------------------------------------
if( pSocket->IsCorked() )
{
XRootDStatus st = pSocket->Uncork();
if( !st.IsOK() ) return st;
}
//----------------------------------------------------------------------
// Check if TLS hand-shake wants to write something
//----------------------------------------------------------------------
if( error == XrdTls::TLS_WantWrite )
{
XRootDStatus st = pSocketHandler->EnableUplink();
if( !st.IsOK() ) return st;
}
//----------------------------------------------------------------------
// Otherwise disable uplink
//----------------------------------------------------------------------
else if( error == XrdTls::TLS_WantRead )
{
XRootDStatus st = pSocketHandler->DisableUplink();
if( !st.IsOK() ) return st;
}
}
return status;
}
XRootDStatus 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 );
XRootDStatus 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() )
{
XRootDStatus 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;
XRootDStatus 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 )
{
XRootDStatus 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 XRootDStatus( stOK, suRetry );
return status;
}
XRootDStatus 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 );
XRootDStatus 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() )
{
XRootDStatus 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;
XRootDStatus 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 )
{
XRootDStatus 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 XRootDStatus( stOK, suRetry );
return status;
}
XRootDStatus Tls::ToStatus( XrdTls::RC rc )
{
std::string msg = XrdTls::RC2Text( rc, true );
switch( rc )
{
case XrdTls::TLS_AOK: return XRootDStatus();
case XrdTls::TLS_WantConnect:
case XrdTls::TLS_WantWrite:
case XrdTls::TLS_WantRead: return XRootDStatus( stOK, suRetry, 0, msg );
case XrdTls::TLS_UNK_Error:
case XrdTls::TLS_SYS_Error: return XRootDStatus( stError, errTlsError, 0, msg );
case XrdTls::TLS_VER_Error:
case XrdTls::TLS_SSL_Error:
case XrdTls::TLS_HNV_Error: return XRootDStatus( stFatal, errTlsError, 0, msg );
default:
return XRootDStatus( stError, errTlsError, 0, msg );
}
}
//------------------------------------------------------------------------
// 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;
}
void Tls::ClearErrorQueue()
{
XrdTls::ClearErrorQueue();
}
}