-
Notifications
You must be signed in to change notification settings - Fork 149
/
XrdClTls.cc
202 lines (176 loc) · 7.97 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
//------------------------------------------------------------------------------
// 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 "XrdTls/XrdTlsContext.hh"
#include <openssl/ssl.h>
namespace XrdCl
{
Tls::Tls( Socket *socket, AsyncSocketHandler *socketHandler ) : pSocket( socket ), pTlsHSRevert( None ), pSocketHandler( socketHandler )
{
static XrdTlsContext tlsContext; // Need only one thread-safe instance
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 )
{
int rc = pTls->Connect( thehost.c_str(), netInfo );
if( rc ) return Status( stError, errTlsError, rc );
return Status();
}
Status Tls::Read( char *buffer, size_t size, int &bytesRead )
{
//--------------------------------------------------------------------------
// If necessary, SSL_read() will negotiate a TLS/SSL session, so we don't
// have to explicitly call SSL_connect or SSL_do_handshake.
//--------------------------------------------------------------------------
int 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 == SSL_ERROR_WANT_WRITE )
{
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;
return status;
}
Status Tls::Send( const char *buffer, size_t size, int &bytesWritten )
{
//--------------------------------------------------------------------------
// If necessary, SSL_write() will negotiate a TLS/SSL session, so we don't
// have to explicitly call SSL_connect or SSL_do_handshake.
//--------------------------------------------------------------------------
int 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 == SSL_ERROR_WANT_READ )
{
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;
return status;
}
Status Tls::ToStatus( int error )
{
switch( error )
{
case SSL_ERROR_NONE: return Status();
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_READ: return Status( stOK, suRetry, error );
case SSL_ERROR_ZERO_RETURN:
case SSL_ERROR_SYSCALL:
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;
}
}