2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 83 TLS Server/Peer negotiation */
12 #include "acl/FilledChecklist.h"
13 #include "comm/Loops.h"
14 #include "Downloader.h"
15 #include "errorpage.h"
17 #include "http/Stream.h"
18 #include "HttpRequest.h"
19 #include "security/NegotiationHistory.h"
20 #include "security/PeerConnector.h"
21 #include "SquidConfig.h"
24 #include "ssl/cert_validate_message.h"
25 #include "ssl/Config.h"
26 #include "ssl/helper.h"
29 CBDATA_NAMESPACED_CLASS_INIT(Security
, PeerConnector
);
31 Security::PeerConnector::PeerConnector(const Comm::ConnectionPointer
&aServerConn
, AsyncCall::Pointer
&aCallback
, const AccessLogEntryPointer
&alp
, const time_t timeout
) :
32 AsyncJob("Security::PeerConnector"),
33 serverConn(aServerConn
),
36 negotiationTimeout(timeout
),
37 startTime(squid_curtime
),
38 useCertValidator_(true),
41 debugs(83, 5, "Security::PeerConnector constructed, this=" << (void*)this);
42 // if this throws, the caller's cb dialer is not our CbDialer
43 Must(dynamic_cast<CbDialer
*>(callback
->getDialer()));
46 Security::PeerConnector::~PeerConnector()
48 debugs(83, 5, "Security::PeerConnector destructed, this=" << (void*)this);
51 bool Security::PeerConnector::doneAll() const
53 return (!callback
|| callback
->canceled()) && AsyncJob::doneAll();
56 /// Preps connection and SSL state. Calls negotiate().
58 Security::PeerConnector::start()
62 Security::SessionPointer tmp
;
63 if (prepareSocket() && initialize(tmp
))
66 mustStop("Security::PeerConnector TLS socket initialize failed");
70 Security::PeerConnector::commCloseHandler(const CommCloseCbParams
¶ms
)
72 debugs(83, 5, "FD " << params
.fd
<< ", Security::PeerConnector=" << params
.data
);
73 connectionClosed("Security::PeerConnector::commCloseHandler");
77 Security::PeerConnector::connectionClosed(const char *reason
)
84 Security::PeerConnector::prepareSocket()
86 const int fd
= serverConnection()->fd
;
87 if (!Comm::IsConnOpen(serverConn
) || fd_table
[serverConn
->fd
].closing()) {
88 connectionClosed("Security::PeerConnector::prepareSocket");
92 // watch for external connection closures
93 typedef CommCbMemFunT
<Security::PeerConnector
, CommCloseCbParams
> Dialer
;
94 closeHandler
= JobCallback(9, 5, Dialer
, this, Security::PeerConnector::commCloseHandler
);
95 comm_add_close_handler(fd
, closeHandler
);
100 Security::PeerConnector::initialize(Security::SessionPointer
&serverSession
)
103 Security::ContextPointer
ctx(getTlsContext());
106 if (!Ssl::CreateClient(ctx
, serverConnection(), "server https start")) {
107 const auto xerrno
= errno
;
108 const auto ssl_error
= ERR_get_error();
109 ErrorState
*anErr
= new ErrorState(ERR_SOCKET_FAILURE
, Http::scInternalServerError
, request
.getRaw());
110 anErr
->xerrno
= xerrno
;
111 debugs(83, DBG_IMPORTANT
, "Error allocating TLS handle: " << Security::ErrorString(ssl_error
));
112 noteNegotiationDone(anErr
);
117 // A TLS/SSL session has now been created for the connection and stored in fd_table
118 serverSession
= fd_table
[serverConnection()->fd
].ssl
;
120 // If CertValidation Helper used do not lookup checklist for errors,
121 // but keep a list of errors to send it to CertValidator
122 if (!Ssl::TheConfig
.ssl_crt_validator
) {
123 // Create the ACL check list now, while we have access to more info.
124 // The list is used in ssl_verify_cb() and is freed in ssl_free().
125 if (acl_access
*acl
= ::Config
.ssl_client
.cert_error
) {
126 ACLFilledChecklist
*check
= new ACLFilledChecklist(acl
, request
.getRaw(), dash_str
);
128 // check->fd(fd); XXX: need client FD here
129 SSL_set_ex_data(serverSession
.get(), ssl_ex_index_cert_error_check
, check
);
140 Security::PeerConnector::setReadTimeout()
143 if (negotiationTimeout
) {
144 const int timeUsed
= squid_curtime
- startTime
;
145 const int timeLeft
= max(0, static_cast<int>(negotiationTimeout
- timeUsed
));
146 timeToRead
= min(static_cast<int>(::Config
.Timeout
.read
), timeLeft
);
148 timeToRead
= ::Config
.Timeout
.read
;
149 AsyncCall::Pointer nil
;
150 commSetConnTimeout(serverConnection(), timeToRead
, nil
);
154 Security::PeerConnector::recordNegotiationDetails()
156 const int fd
= serverConnection()->fd
;
157 Security::SessionPointer
session(fd_table
[fd
].ssl
);
159 // retrieve TLS server negotiated information if any
160 serverConnection()->tlsNegotiations()->retrieveNegotiatedInfo(session
);
163 // retrieve TLS parsed extra info
164 BIO
*b
= SSL_get_rbio(session
.get());
165 Ssl::ServerBio
*bio
= static_cast<Ssl::ServerBio
*>(BIO_get_data(b
));
166 if (const Security::TlsDetails::Pointer
&details
= bio
->receivedHelloDetails())
167 serverConnection()->tlsNegotiations()->retrieveParsedInfo(details
);
172 Security::PeerConnector::negotiate()
174 if (!Comm::IsConnOpen(serverConnection()))
177 const int fd
= serverConnection()->fd
;
178 if (fd_table
[fd
].closing())
182 const int result
= SSL_connect(fd_table
[fd
].ssl
.get());
184 const int result
= -1;
187 handleNegotiateError(result
);
188 return; // we might be gone by now
191 recordNegotiationDetails();
200 Security::PeerConnector::sslFinalized()
203 if (Ssl::TheConfig
.ssl_crt_validator
&& useCertValidator_
) {
204 const int fd
= serverConnection()->fd
;
205 Security::SessionPointer
session(fd_table
[fd
].ssl
);
207 Ssl::CertValidationRequest validationRequest
;
208 // WARNING: Currently we do not use any locking for any of the
209 // members of the Ssl::CertValidationRequest class. In this code the
210 // Ssl::CertValidationRequest object used only to pass data to
211 // Ssl::CertValidationHelper::submit method.
212 validationRequest
.ssl
= session
.get();
213 if (SBuf
*dName
= (SBuf
*)SSL_get_ex_data(session
.get(), ssl_ex_index_server
))
214 validationRequest
.domainName
= dName
->c_str();
215 if (Security::CertErrors
*errs
= static_cast<Security::CertErrors
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_errors
)))
216 // validationRequest disappears on return so no need to cbdataReference
217 validationRequest
.errors
= errs
;
219 debugs(83, 5, "Sending SSL certificate for validation to ssl_crtvd.");
220 AsyncCall::Pointer call
= asyncCall(83,5, "Security::PeerConnector::sslCrtvdHandleReply", Ssl::CertValidationHelper::CbDialer(this, &Security::PeerConnector::sslCrtvdHandleReply
, nullptr));
221 Ssl::CertValidationHelper::GetInstance()->sslSubmit(validationRequest
, call
);
223 } catch (const std::exception
&e
) {
224 debugs(83, DBG_IMPORTANT
, "ERROR: Failed to compose ssl_crtvd " <<
225 "request for " << validationRequest
.domainName
<<
226 " certificate: " << e
.what() << "; will now block to " <<
227 "validate that certificate.");
228 // fall through to do blocking in-process generation.
229 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
231 noteNegotiationDone(anErr
);
239 noteNegotiationDone(NULL
);
245 Security::PeerConnector::sslCrtvdHandleReply(Ssl::CertValidationResponse::Pointer validationResponse
)
247 Must(validationResponse
!= NULL
);
249 Ssl::ErrorDetail
*errDetails
= NULL
;
250 bool validatorFailed
= false;
251 if (!Comm::IsConnOpen(serverConnection())) {
255 if (Debug::Enabled(83, 5)) {
256 Security::SessionPointer
ssl(fd_table
[serverConnection()->fd
].ssl
);
257 SBuf
*server
= static_cast<SBuf
*>(SSL_get_ex_data(ssl
.get(), ssl_ex_index_server
));
258 debugs(83,5, RawPointer("host", server
) << " cert validation result: " << validationResponse
->resultCode
);
261 if (validationResponse
->resultCode
== ::Helper::Error
) {
262 if (Security::CertErrors
*errs
= sslCrtvdCheckForErrors(*validationResponse
, errDetails
)) {
263 Security::SessionPointer
session(fd_table
[serverConnection()->fd
].ssl
);
264 Security::CertErrors
*oldErrs
= static_cast<Security::CertErrors
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_errors
));
265 SSL_set_ex_data(session
.get(), ssl_ex_index_ssl_errors
, (void *)errs
);
268 } else if (validationResponse
->resultCode
!= ::Helper::Okay
)
269 validatorFailed
= true;
271 if (!errDetails
&& !validatorFailed
) {
272 noteNegotiationDone(NULL
);
277 ErrorState
*anErr
= NULL
;
278 if (validatorFailed
) {
279 anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
281 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, request
.getRaw());
282 anErr
->detail
= errDetails
;
283 /*anErr->xerrno= Should preserved*/
286 noteNegotiationDone(anErr
);
294 /// Checks errors in the cert. validator response against sslproxy_cert_error.
295 /// The first honored error, if any, is returned via errDetails parameter.
296 /// The method returns all seen errors except SSL_ERROR_NONE as Security::CertErrors.
297 Security::CertErrors
*
298 Security::PeerConnector::sslCrtvdCheckForErrors(Ssl::CertValidationResponse
const &resp
, Ssl::ErrorDetail
*& errDetails
)
300 ACLFilledChecklist
*check
= NULL
;
301 if (acl_access
*acl
= ::Config
.ssl_client
.cert_error
) {
302 check
= new ACLFilledChecklist(acl
, request
.getRaw(), dash_str
);
306 Security::CertErrors
*errs
= nullptr;
307 Security::SessionPointer
session(fd_table
[serverConnection()->fd
].ssl
);
308 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI
;
309 for (SVCRECI i
= resp
.errors
.begin(); i
!= resp
.errors
.end(); ++i
) {
310 debugs(83, 7, "Error item: " << i
->error_no
<< " " << i
->error_reason
);
312 assert(i
->error_no
!= SSL_ERROR_NONE
);
315 bool allowed
= false;
317 check
->sslErrors
= new Security::CertErrors(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
318 if (check
->fastCheck() == ACCESS_ALLOWED
)
321 // else the Config.ssl_client.cert_error access list is not defined
322 // and the first error will cause the error page
325 debugs(83, 3, "bypassing SSL error " << i
->error_no
<< " in " << "buffer");
327 debugs(83, 5, "confirming SSL error " << i
->error_no
);
328 X509
*brokenCert
= i
->cert
.get();
329 Security::CertPointer
peerCert(SSL_get_peer_certificate(session
.get()));
330 const char *aReason
= i
->error_reason
.empty() ? NULL
: i
->error_reason
.c_str();
331 errDetails
= new Ssl::ErrorDetail(i
->error_no
, peerCert
.get(), brokenCert
, aReason
);
334 delete check
->sslErrors
;
335 check
->sslErrors
= NULL
;
340 errs
= new Security::CertErrors(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
342 errs
->push_back_unique(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
351 /// A wrapper for Comm::SetSelect() notifications.
353 Security::PeerConnector::NegotiateSsl(int, void *data
)
355 PeerConnector
*pc
= static_cast<Security::PeerConnector
*>(data
);
356 // Use job calls to add done() checks and other job logic/protections.
357 CallJobHere(83, 7, pc
, Security::PeerConnector
, negotiate
);
361 Security::PeerConnector::handleNegotiateError(const int ret
)
364 const int fd
= serverConnection()->fd
;
365 unsigned long ssl_lib_error
= SSL_ERROR_NONE
;
366 Security::SessionPointer
session(fd_table
[fd
].ssl
);
367 const int ssl_error
= SSL_get_error(session
.get(), ret
);
370 case SSL_ERROR_WANT_READ
:
374 case SSL_ERROR_WANT_WRITE
:
379 case SSL_ERROR_SYSCALL
:
380 ssl_lib_error
= ERR_get_error();
381 // proceed to the general error handling code
384 // no special error handling for all other errors
388 // Log connection details, if any
389 recordNegotiationDetails();
390 noteNegotiationError(ret
, ssl_error
, ssl_lib_error
);
395 Security::PeerConnector::noteWantRead()
397 const int fd
= serverConnection()->fd
;
399 Security::SessionPointer
session(fd_table
[fd
].ssl
);
400 BIO
*b
= SSL_get_rbio(session
.get());
401 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(BIO_get_data(b
));
402 if (srvBio
->holdRead()) {
403 if (srvBio
->gotHello()) {
404 if (checkForMissingCertificates())
405 return; // Wait to download certificates before proceed.
407 srvBio
->holdRead(false);
408 // schedule a negotiateSSl to allow openSSL parse received data
409 Security::PeerConnector::NegotiateSsl(fd
, this);
411 } else if (srvBio
->gotHelloFailed()) {
412 srvBio
->holdRead(false);
413 debugs(83, DBG_IMPORTANT
, "Error parsing SSL Server Hello Message on FD " << fd
);
414 // schedule a negotiateSSl to allow openSSL parse received data
415 Security::PeerConnector::NegotiateSsl(fd
, this);
421 Comm::SetSelect(fd
, COMM_SELECT_READ
, &NegotiateSsl
, this, 0);
425 Security::PeerConnector::noteWantWrite()
427 const int fd
= serverConnection()->fd
;
428 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, &NegotiateSsl
, this, 0);
433 Security::PeerConnector::noteNegotiationError(const int ret
, const int ssl_error
, const int ssl_lib_error
)
435 #if USE_OPENSSL // not used unless OpenSSL enabled.
437 int sysErrNo
= EPROTO
;
439 int sysErrNo
= EACCES
;
442 // store/report errno when ssl_error is SSL_ERROR_SYSCALL, ssl_lib_error is 0, and ret is -1
443 if (ssl_error
== SSL_ERROR_SYSCALL
&& ret
== -1 && ssl_lib_error
== 0)
446 const int fd
= serverConnection()->fd
;
447 debugs(83, DBG_IMPORTANT
, "Error negotiating SSL on FD " << fd
<<
448 ": " << Security::ErrorString(ssl_lib_error
) << " (" <<
449 ssl_error
<< "/" << ret
<< "/" << errno
<< ")");
451 ErrorState
*anErr
= NULL
;
453 anErr
= ErrorState::NewForwarding(ERR_SECURE_CONNECT_FAIL
, request
.getRaw());
455 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, NULL
);
456 anErr
->xerrno
= sysErrNo
;
458 Security::SessionPointer
session(fd_table
[fd
].ssl
);
459 Ssl::ErrorDetail
*errFromFailure
= static_cast<Ssl::ErrorDetail
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_error_detail
));
460 if (errFromFailure
!= NULL
) {
461 // The errFromFailure is attached to the ssl object
462 // and will be released when ssl object destroyed.
463 // Copy errFromFailure to a new Ssl::ErrorDetail object
464 anErr
->detail
= new Ssl::ErrorDetail(*errFromFailure
);
466 // server_cert can be NULL here
467 X509
*server_cert
= SSL_get_peer_certificate(session
.get());
468 anErr
->detail
= new Ssl::ErrorDetail(SQUID_ERR_SSL_HANDSHAKE
, server_cert
, NULL
);
469 X509_free(server_cert
);
472 if (ssl_lib_error
!= SSL_ERROR_NONE
)
473 anErr
->detail
->setLibError(ssl_lib_error
);
475 noteNegotiationDone(anErr
);
481 Security::PeerConnector::bail(ErrorState
*error
)
483 Must(error
); // or the recepient will not know there was a problem
484 Must(callback
!= NULL
);
485 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(callback
->getDialer());
487 dialer
->answer().error
= error
;
490 // Our job is done. The callabck recepient will probably close the failed
491 // peer connection and try another peer or go direct (if possible). We
492 // can close the connection ourselves (our error notification would reach
493 // the recepient before the fd-closure notification), but we would rather
494 // minimize the number of fd-closure notifications and let the recepient
495 // manage the TCP state of the connection.
499 Security::PeerConnector::callBack()
501 AsyncCall::Pointer cb
= callback
;
502 // Do this now so that if we throw below, swanSong() assert that we _tried_
503 // to call back holds.
504 callback
= NULL
; // this should make done() true
506 // remove close handler
507 comm_remove_close_handler(serverConnection()->fd
, closeHandler
);
509 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(cb
->getDialer());
511 dialer
->answer().conn
= serverConnection();
512 ScheduleCallHere(cb
);
516 Security::PeerConnector::swanSong()
518 // XXX: unregister fd-closure monitoring and CommSetSelect interest, if any
519 AsyncJob::swanSong();
520 if (callback
!= NULL
) { // paranoid: we have left the caller waiting
521 debugs(83, DBG_IMPORTANT
, "BUG: Unexpected state while connecting to a cache_peer or origin server");
522 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
530 Security::PeerConnector::status() const
535 // TODO: redesign AsyncJob::status() API to avoid this
536 // id and stop reason reporting duplication.
538 if (stopReason
!= NULL
) {
539 buf
.append("Stopped, reason:", 16);
540 buf
.appendf("%s",stopReason
);
542 if (serverConn
!= NULL
)
543 buf
.appendf(" FD %d", serverConn
->fd
);
544 buf
.appendf(" %s%u]", id
.prefix(), id
.value
);
547 return buf
.content();
551 /// CallDialer to allow use Downloader objects within PeerConnector class.
552 class PeerConnectorCertDownloaderDialer
: public Downloader::CbDialer
555 typedef void (Security::PeerConnector::*Method
)(SBuf
&object
, int status
);
557 PeerConnectorCertDownloaderDialer(Method method
, Security::PeerConnector
*pc
):
559 peerConnector_(pc
) {}
562 virtual bool canDial(AsyncCall
&call
) { return peerConnector_
.valid(); }
563 virtual void dial(AsyncCall
&call
) { ((&(*peerConnector_
))->*method_
)(object
, status
); }
564 Method method_
; ///< The Security::PeerConnector method to dial
565 CbcPointer
<Security::PeerConnector
> peerConnector_
; ///< The Security::PeerConnector object
569 Security::PeerConnector::startCertDownloading(SBuf
&url
)
571 AsyncCall::Pointer certCallback
= asyncCall(81, 4,
572 "Security::PeerConnector::certDownloadingDone",
573 PeerConnectorCertDownloaderDialer(&Security::PeerConnector::certDownloadingDone
, this));
575 const Downloader
*csd
= (request
? dynamic_cast<const Downloader
*>(request
->downloader
.valid()) : nullptr);
576 Downloader
*dl
= new Downloader(url
, certCallback
, csd
? csd
->nestedLevel() + 1 : 1);
581 Security::PeerConnector::certDownloadingDone(SBuf
&obj
, int downloadStatus
)
584 debugs(81, 5, "Certificate downloading status: " << downloadStatus
<< " certificate size: " << obj
.length());
586 // get ServerBio from SSL object
587 const int fd
= serverConnection()->fd
;
588 Security::SessionPointer
session(fd_table
[fd
].ssl
);
589 BIO
*b
= SSL_get_rbio(session
.get());
590 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(BIO_get_data(b
));
592 // Parse Certificate. Assume that it is in DER format.
593 // According to RFC 4325:
594 // The server must provide a DER encoded certificate or a collection
595 // collection of certificates in a "certs-only" CMS message.
596 // The applications MUST accept DER encoded certificates and SHOULD
597 // be able to accept collection of certificates.
598 // TODO: support collection of certificates
599 const unsigned char *raw
= (const unsigned char*)obj
.rawContent();
600 if (X509
*cert
= d2i_X509(NULL
, &raw
, obj
.length())) {
602 debugs(81, 5, "Retrieved certificate: " << X509_NAME_oneline(X509_get_subject_name(cert
), buffer
, 1024));
603 const Security::CertList
&certsList
= srvBio
->serverCertificatesIfAny();
604 if (const char *issuerUri
= Ssl::uriOfIssuerIfMissing(cert
, certsList
)) {
605 urlsOfMissingCerts
.push(SBuf(issuerUri
));
607 Ssl::SSL_add_untrusted_cert(session
.get(), cert
);
610 // Check if there are URIs to download from and if yes start downloading
611 // the first in queue.
612 if (urlsOfMissingCerts
.size() && certsDownloads
<= MaxCertsDownloads
) {
613 startCertDownloading(urlsOfMissingCerts
.front());
614 urlsOfMissingCerts
.pop();
618 srvBio
->holdRead(false);
619 Security::PeerConnector::NegotiateSsl(serverConnection()->fd
, this);
623 Security::PeerConnector::checkForMissingCertificates()
625 // Check for nested SSL certificates downloads. For example when the
626 // certificate located in an SSL site which requires to download a
627 // a missing certificate (... from an SSL site which requires to ...).
629 const Downloader
*csd
= (request
? request
->downloader
.get() : nullptr);
630 if (csd
&& csd
->nestedLevel() >= MaxNestedDownloads
)
633 const int fd
= serverConnection()->fd
;
634 Security::SessionPointer
session(fd_table
[fd
].ssl
);
635 BIO
*b
= SSL_get_rbio(session
.get());
636 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(BIO_get_data(b
));
637 const Security::CertList
&certs
= srvBio
->serverCertificatesIfAny();
640 debugs(83, 5, "SSL server sent " << certs
.size() << " certificates");
641 Ssl::missingChainCertificatesUrls(urlsOfMissingCerts
, certs
);
642 if (urlsOfMissingCerts
.size()) {
643 startCertDownloading(urlsOfMissingCerts
.front());
644 urlsOfMissingCerts
.pop();