PeekingPeerConnector.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 83 SSL-Bump Server/Peer negotiation */
10 
11 #include "squid.h"
12 #include "acl/FilledChecklist.h"
13 #include "client_side.h"
14 #include "errorpage.h"
15 #include "fde.h"
16 #include "http/Stream.h"
17 #include "HttpRequest.h"
19 #include "SquidConfig.h"
20 #include "ssl/bio.h"
22 #include "ssl/ServerBump.h"
23 
24 CBDATA_NAMESPACED_CLASS_INIT(Ssl, PeekingPeerConnector);
25 
27 
28 void
30 {
32  // Use job calls to add done() checks and other job logic/protections.
34 }
35 
36 void
38 {
39  const Ssl::BumpMode finalAction = answer.allowed() ?
40  static_cast<Ssl::BumpMode>(answer.kind):
41  checkForPeekAndSpliceGuess();
42  checkForPeekAndSpliceMatched(finalAction);
43 }
44 
45 void
47 {
48  // Mark Step3 of bumping
49  if (request->clientConnectionManager.valid()) {
50  if (Ssl::ServerBump *serverBump = request->clientConnectionManager->serverBump()) {
51  serverBump->step = Ssl::bumpStep3;
52  }
53  }
54 
55  handleServerCertificate();
56 
57  ACLFilledChecklist *acl_checklist = new ACLFilledChecklist(
58  ::Config.accessList.ssl_bump,
59  request.getRaw(), NULL);
60  acl_checklist->al = al;
66  Security::SessionPointer session(fd_table[serverConn->fd].ssl);
67  BIO *b = SSL_get_rbio(session.get());
68  Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(BIO_get_data(b));
69  if (!srvBio->canSplice())
71  if (!srvBio->canBump())
74 }
75 
76 void
78 {
79  Security::SessionPointer session(fd_table[serverConn->fd].ssl);
80  BIO *b = SSL_get_rbio(session.get());
81  Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(BIO_get_data(b));
82  debugs(83,5, "Will check for peek and splice on FD " << serverConn->fd);
83 
84  Ssl::BumpMode finalAction = action;
85  Must(finalAction == Ssl::bumpSplice || finalAction == Ssl::bumpBump || finalAction == Ssl::bumpTerminate);
86  // Record final decision
87  if (request->clientConnectionManager.valid()) {
88  request->clientConnectionManager->sslBumpMode = finalAction;
89  request->clientConnectionManager->serverBump()->act.step3 = finalAction;
90  }
91  al->ssl.bumpMode = finalAction;
92 
93  if (finalAction == Ssl::bumpTerminate) {
94  serverConn->close();
95  clientConn->close();
96  } else if (finalAction != Ssl::bumpSplice) {
97  //Allow write, proceed with the connection
98  srvBio->holdWrite(false);
99  srvBio->recordInput(false);
100  debugs(83,5, "Retry the fwdNegotiateSSL on FD " << serverConn->fd);
102  } else {
103  splice = true;
104  // Ssl Negotiation stops here. Last SSL checks for valid certificates
105  // and if done, switch to tunnel mode
106  if (sslFinalized()) {
107  debugs(83,5, "Abort NegotiateSSL on FD " << serverConn->fd << " and splice the connection");
108  callBack();
109  }
110  }
111 }
112 
115 {
116  if (const ConnStateData *csd = request->clientConnectionManager.valid()) {
117  const Ssl::BumpMode currentMode = csd->sslBumpMode;
118  if (currentMode == Ssl::bumpStare) {
119  debugs(83,5, "default to bumping after staring");
120  return Ssl::bumpBump;
121  }
122  debugs(83,5, "default to splicing after " << currentMode);
123  } else {
124  debugs(83,3, "default to splicing due to missing info");
125  }
126 
127  return Ssl::bumpSplice;
128 }
129 
132 {
134 }
135 
136 bool
138 {
139  if (!Security::PeerConnector::initialize(serverSession))
140  return false;
141 
142  if (ConnStateData *csd = request->clientConnectionManager.valid()) {
143 
144  // client connection is required in the case we need to splice
145  // or terminate client and server connections
146  assert(clientConn != NULL);
147  SBuf *hostName = NULL;
148 
149  //Enable Status_request TLS extension, required to bump some clients
150  SSL_set_tlsext_status_type(serverSession.get(), TLSEXT_STATUSTYPE_ocsp);
151 
152  const Security::TlsDetails::Pointer details = csd->tlsParser.details;
153  if (details && !details->serverName.isEmpty())
154  hostName = new SBuf(details->serverName);
155 
156  if (!hostName) {
157  // While we are peeking at the certificate, we may not know the server
158  // name that the client will request (after interception or CONNECT)
159  // unless it was the CONNECT request with a user-typed address.
160  const bool isConnectRequest = !csd->port->flags.isIntercepted();
161  if (!request->flags.sslPeek || isConnectRequest)
162  hostName = new SBuf(request->url.host());
163  }
164 
165  if (hostName)
166  SSL_set_ex_data(serverSession.get(), ssl_ex_index_server, (void*)hostName);
167 
168  Must(!csd->serverBump() || csd->serverBump()->step <= Ssl::bumpStep2);
169  if (csd->sslBumpMode == Ssl::bumpPeek || csd->sslBumpMode == Ssl::bumpStare) {
170  auto clientSession = fd_table[clientConn->fd].ssl.get();
171  Must(clientSession);
172  BIO *bc = SSL_get_rbio(clientSession);
173  Ssl::ClientBio *cltBio = static_cast<Ssl::ClientBio *>(BIO_get_data(bc));
174  Must(cltBio);
175  if (details && details->tlsVersion.protocol != AnyP::PROTO_NONE)
176  applyTlsDetailsToSSL(serverSession.get(), details, csd->sslBumpMode);
177 
178  BIO *b = SSL_get_rbio(serverSession.get());
179  Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(BIO_get_data(b));
180  Must(srvBio);
181  // inherit client features such as TLS version and SNI
182  srvBio->setClientFeatures(details, cltBio->rBufData());
183  srvBio->recordInput(true);
184  srvBio->mode(csd->sslBumpMode);
185  } else {
186  // Set client SSL options
187  SSL_set_options(serverSession.get(), ::Security::ProxyOutgoingConfig.parsedOptions);
188 
189  const bool redirected = request->flags.redirected && ::Config.onoff.redir_rewrites_host;
190  const char *sniServer = (!hostName || redirected) ?
191  request->url.host() :
192  hostName->c_str();
193  if (sniServer)
194  setClientSNI(serverSession.get(), sniServer);
195  }
196 
197  if (Ssl::ServerBump *serverBump = csd->serverBump()) {
198  serverBump->attachServerSession(serverSession);
199  // store peeked cert to check SQUID_X509_V_ERR_CERT_CHANGE
200  if (X509 *peeked_cert = serverBump->serverCert.get()) {
201  X509_up_ref(peeked_cert);
202  SSL_set_ex_data(serverSession.get(), ssl_ex_index_ssl_peeked_cert, peeked_cert);
203  }
204  }
205  }
206 
207  return true;
208 }
209 
210 void
212 {
213  // Check the list error with
214  if (!request->clientConnectionManager.valid() || !fd_table[serverConnection()->fd].ssl)
215  return;
216 
217  // remember the server certificate from the ErrorDetail object
218  if (Ssl::ServerBump *serverBump = request->clientConnectionManager->serverBump()) {
219  if (!serverBump->serverCert.get()) {
220  // remember the server certificate from the ErrorDetail object
221  if (error && error->detail && error->detail->peerCert())
222  serverBump->serverCert.resetAndLock(error->detail->peerCert());
223  else {
224  handleServerCertificate();
225  }
226  }
227 
228  if (error) {
229  // For intercepted connections, set the host name to the server
230  // certificate CN. Otherwise, we just hope that CONNECT is using
231  // a user-entered address (a host name or a user-entered IP).
232  const bool isConnectRequest = !request->clientConnectionManager->port->flags.isIntercepted();
233  if (request->flags.sslPeek && !isConnectRequest) {
234  if (X509 *srvX509 = serverBump->serverCert.get()) {
235  if (const char *name = Ssl::CommonHostName(srvX509)) {
236  request->url.host(name);
237  debugs(83, 3, "reset request host: " << name);
238  }
239  }
240  }
241  }
242  }
243 
244  if (!error) {
245  serverCertificateVerified();
246  if (splice) {
247  switchToTunnel(request.getRaw(), clientConn, serverConn);
248  tunnelInsteadOfNegotiating();
249  }
250  }
251 }
252 
253 void
255 {
256  const int fd = serverConnection()->fd;
257  Security::SessionPointer session(fd_table[fd].ssl);
258  BIO *b = SSL_get_rbio(session.get());
259  Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(BIO_get_data(b));
260 
261  if ((srvBio->bumpMode() == Ssl::bumpPeek || srvBio->bumpMode() == Ssl::bumpStare) && srvBio->holdWrite()) {
262  debugs(81, 3, "hold write on SSL connection on FD " << fd);
263  checkForPeekAndSplice();
264  return;
265  }
266 
268 }
269 
270 void
271 Ssl::PeekingPeerConnector::noteNegotiationError(const int result, const int ssl_error, const int ssl_lib_error)
272 {
273  const int fd = serverConnection()->fd;
274  Security::SessionPointer session(fd_table[fd].ssl);
275  BIO *b = SSL_get_rbio(session.get());
276  Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(BIO_get_data(b));
277 
278  // In Peek mode, the ClientHello message sent to the server. If the
279  // server resuming a previous (spliced) SSL session with the client,
280  // then probably we are here because local SSL object does not know
281  // anything about the session being resumed.
282  //
283  if (srvBio->bumpMode() == Ssl::bumpPeek && (resumingSession = srvBio->resumingSession())) {
284  // we currently splice all resumed sessions unconditionally
285  if (const bool spliceResumed = true) {
286  bypassCertValidator();
287  checkForPeekAndSpliceMatched(Ssl::bumpSplice);
288  return;
289  } // else fall through to find a matching ssl_bump action (with limited info)
290  }
291 
292  // If we are in peek-and-splice mode and still we did not write to
293  // server yet, try to see if we should splice.
294  // In this case the connection can be saved.
295  // If the checklist decision is do not splice a new error will
296  // occur in the next SSL_connect call, and we will fail again.
297  // Abort on certificate validation errors to avoid splicing and
298  // thus hiding them.
299  // Abort if no certificate found probably because of malformed or
300  // unsupported server Hello message (TODO: make configurable).
301  if (!SSL_get_ex_data(session.get(), ssl_ex_index_ssl_error_detail) &&
302  (srvBio->bumpMode() == Ssl::bumpPeek || srvBio->bumpMode() == Ssl::bumpStare) && srvBio->holdWrite()) {
303  Security::CertPointer serverCert(SSL_get_peer_certificate(session.get()));
304  if (serverCert) {
305  debugs(81, 3, "Error (" << Security::ErrorString(ssl_lib_error) << ") but, hold write on SSL connection on FD " << fd);
306  checkForPeekAndSplice();
307  return;
308  }
309  }
310 
311  // else call parent noteNegotiationError to produce an error page
312  Security::PeerConnector::noteNegotiationError(result, ssl_error, ssl_lib_error);
313 }
314 
315 void
317 {
318  if (serverCertificateHandled)
319  return;
320 
321  if (ConnStateData *csd = request->clientConnectionManager.valid()) {
322  const int fd = serverConnection()->fd;
323  Security::SessionPointer session(fd_table[fd].ssl);
324  Security::CertPointer serverCert(SSL_get_peer_certificate(session.get()));
325  if (!serverCert)
326  return;
327 
328  serverCertificateHandled = true;
329 
330  // remember the server certificate for later use
331  if (Ssl::ServerBump *serverBump = csd->serverBump()) {
332  serverBump->serverCert = std::move(serverCert);
333  }
334  }
335 }
336 
337 void
339 {
340  if (ConnStateData *csd = request->clientConnectionManager.valid()) {
341  Security::CertPointer serverCert;
342  if(Ssl::ServerBump *serverBump = csd->serverBump())
343  serverCert.resetAndLock(serverBump->serverCert.get());
344  else {
345  const int fd = serverConnection()->fd;
346  Security::SessionPointer session(fd_table[fd].ssl);
347  serverCert.resetWithoutLocking(SSL_get_peer_certificate(session.get()));
348  }
349  if (serverCert) {
350  csd->resetSslCommonName(Ssl::CommonHostName(serverCert.get()));
351  debugs(83, 5, "HTTPS server CN: " << csd->sslCommonName() <<
352  " bumped: " << *serverConnection());
353  }
354  }
355 }
356 
357 void
359 {
360  Must(callback != NULL);
361  CbDialer *dialer = dynamic_cast<CbDialer*>(callback->getDialer());
362  Must(dialer);
363  dialer->answer().tunneled = true;
364  debugs(83, 5, "The SSL negotiation with server aborted");
365 }
366 
int ssl_ex_index_ssl_peeked_cert
void nonBlockingCheck(ACLCB *callback, void *callback_data)
Definition: Checklist.cc:238
void tunnelInsteadOfNegotiating()
Inform caller class that the SSL negotiation aborted.
#define fd_table
Definition: fde.h:157
#define assert(EX)
Definition: assert.h:17
void const char HLPCB * callback
Definition: stub_helper.cc:16
static void cbCheckForPeekAndSpliceDone(allow_t answer, void *data)
A wrapper function for checkForPeekAndSpliceDone for use with acl.
int ssl_ex_index_server
const SBuf & rBufData()
The buffered input data.
Definition: bio.h:60
virtual void noteNegotiationDone(ErrorState *error)
Definition: Acl.h:113
Definition: SBuf.h:87
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
void error(char *format,...)
void banAction(const allow_t &action)
add action to the list of banned actions
Definition: Checklist.cc:402
Security::ParsedOptions parsedOptions
parsed value of sslOptions
Definition: PeerOptions.h:82
AccessLogEntry::Pointer al
info for the future access.log, and external ACL
Security::ContextPointer sslContext
Definition: SquidConfig.h:509
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
void switchToTunnel(HttpRequest *request, Comm::ConnectionPointer &clientConn, Comm::ConnectionPointer &srvConn)
Definition: tunnel.cc:1333
X509 * peerCert()
the peer certificate
Definition: ErrorDetail.h:57
A PeerConnector for HTTP origin servers. Capable of SslBumping.
struct SquidConfig::@123 ssl_client
virtual void noteWantWrite()
virtual Security::ContextPointer getTlsContext()
void const char HLPCB void * data
Definition: stub_helper.cc:16
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:121
int ssl_ex_index_ssl_error_detail
const char * CommonHostName(X509 *x509)
Definition: gadgets.cc:913
virtual bool initialize(Security::SessionPointer &)
PeerOptions ProxyOutgoingConfig
configuration options for DIRECT server access
Definition: PeerOptions.cc:22
int kind
which custom access list verb matched
Definition: Acl.h:153
Ssl::BumpMode checkForPeekAndSpliceGuess() const
Guesses the final bumping decision when no ssl_bump rules match.
std::shared_ptr< SSL_CTX > ContextPointer
Definition: Context.h:28
bool allowed() const
Definition: Acl.h:141
void checkForPeekAndSpliceDone(allow_t answer)
Callback function for ssl_bump acl check in step3 SSL bump step.
virtual Security::EncryptorAnswer & answer()=0
gives PeerConnector access to the in-dialer answer
#define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1)
Definition: AsyncJobCalls.h:62
bool tunneled
whether we spliced the connections instead of negotiating encryption
bool action(int fd, size_t metasize, const char *fn, const char *url, const SquidMetaList &meta)
Definition: purge.cc:311
CBDATA_NAMESPACED_CLASS_INIT(Ssl, PeekingPeerConnector)
void applyTlsDetailsToSSL(SSL *ssl, Security::TlsDetails::Pointer const &details, Ssl::BumpMode bumpMode)
Definition: bio.cc:686
char * url
Definition: tcp-banger2.c:114
void Comm::ConnectionPointer & clientConn
Definition: stub_tunnel.cc:19
void setClientSNI(SSL *ssl, const char *fqdn)
Definition: support.cc:914
virtual void noteNegotiationError(const int result, const int ssl_error, const int ssl_lib_error)
Callback dialer API to allow PeerConnector to set the answer.
Definition: PeerConnector.h:69
virtual bool initialize(Security::SessionPointer &)
void * BIO_get_data(BIO *table)
Definition: bio.h:209
BumpMode
Definition: support.h:130
void checkForPeekAndSpliceMatched(const Ssl::BumpMode finalMode)
Handles the final bumping decision.
virtual void noteNegotiationError(const int result, const int ssl_error, const int ssl_lib_error)
Ssl::ErrorDetail * detail
Definition: errorpage.h:170
const char * ErrorString(const ErrorCode code)
Definition: forward.h:120
class SquidConfig Config
Definition: SquidConfig.cc:12
#define NULL
Definition: types.h:166
std::shared_ptr< SSL > SessionPointer
Definition: Session.h:41

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors