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 (!Security::CreateClientSession(ctx
, serverConnection(), "server https start")) {
107 ErrorState
*anErr
= new ErrorState(ERR_SOCKET_FAILURE
, Http::scInternalServerError
, request
.getRaw());
108 anErr
->xerrno
= errno
;
109 debugs(83, DBG_IMPORTANT
, "Error allocating TLS handle: " << ERR_error_string(ERR_get_error(), NULL
));
110 noteNegotiationDone(anErr
);
115 // A TLS/SSL session has now been created for the connection and stored in fd_table
116 serverSession
= fd_table
[serverConnection()->fd
].ssl
;
118 // If CertValidation Helper used do not lookup checklist for errors,
119 // but keep a list of errors to send it to CertValidator
120 if (!Ssl::TheConfig
.ssl_crt_validator
) {
121 // Create the ACL check list now, while we have access to more info.
122 // The list is used in ssl_verify_cb() and is freed in ssl_free().
123 if (acl_access
*acl
= ::Config
.ssl_client
.cert_error
) {
124 ACLFilledChecklist
*check
= new ACLFilledChecklist(acl
, request
.getRaw(), dash_str
);
126 // check->fd(fd); XXX: need client FD here
127 SSL_set_ex_data(serverSession
.get(), ssl_ex_index_cert_error_check
, check
);
138 Security::PeerConnector::setReadTimeout()
141 if (negotiationTimeout
) {
142 const int timeUsed
= squid_curtime
- startTime
;
143 const int timeLeft
= max(0, static_cast<int>(negotiationTimeout
- timeUsed
));
144 timeToRead
= min(static_cast<int>(::Config
.Timeout
.read
), timeLeft
);
146 timeToRead
= ::Config
.Timeout
.read
;
147 AsyncCall::Pointer nil
;
148 commSetConnTimeout(serverConnection(), timeToRead
, nil
);
152 Security::PeerConnector::recordNegotiationDetails()
154 const int fd
= serverConnection()->fd
;
155 Security::SessionPointer
session(fd_table
[fd
].ssl
);
157 // retrieve TLS server negotiated information if any
158 serverConnection()->tlsNegotiations()->retrieveNegotiatedInfo(session
);
161 // retrieve TLS parsed extra info
162 BIO
*b
= SSL_get_rbio(session
.get());
163 Ssl::ServerBio
*bio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
164 if (const Security::TlsDetails::Pointer
&details
= bio
->receivedHelloDetails())
165 serverConnection()->tlsNegotiations()->retrieveParsedInfo(details
);
170 Security::PeerConnector::negotiate()
172 if (!Comm::IsConnOpen(serverConnection()))
175 const int fd
= serverConnection()->fd
;
176 if (fd_table
[fd
].closing())
180 const int result
= SSL_connect(fd_table
[fd
].ssl
.get());
182 const int result
= -1;
185 handleNegotiateError(result
);
186 return; // we might be gone by now
189 recordNegotiationDetails();
198 Security::PeerConnector::sslFinalized()
201 if (Ssl::TheConfig
.ssl_crt_validator
&& useCertValidator_
) {
202 const int fd
= serverConnection()->fd
;
203 Security::SessionPointer
session(fd_table
[fd
].ssl
);
205 Ssl::CertValidationRequest validationRequest
;
206 // WARNING: Currently we do not use any locking for any of the
207 // members of the Ssl::CertValidationRequest class. In this code the
208 // Ssl::CertValidationRequest object used only to pass data to
209 // Ssl::CertValidationHelper::submit method.
210 validationRequest
.ssl
= session
.get();
211 SBuf
*dName
= (SBuf
*)SSL_get_ex_data(session
.get(), ssl_ex_index_server
);
212 validationRequest
.domainName
= dName
->c_str();
213 if (Security::CertErrors
*errs
= static_cast<Security::CertErrors
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_errors
)))
214 // validationRequest disappears on return so no need to cbdataReference
215 validationRequest
.errors
= errs
;
217 debugs(83, 5, "Sending SSL certificate for validation to ssl_crtvd.");
218 AsyncCall::Pointer call
= asyncCall(83,5, "Security::PeerConnector::sslCrtvdHandleReply", Ssl::CertValidationHelper::CbDialer(this, &Security::PeerConnector::sslCrtvdHandleReply
, nullptr));
219 Ssl::CertValidationHelper::GetInstance()->sslSubmit(validationRequest
, call
);
221 } catch (const std::exception
&e
) {
222 debugs(83, DBG_IMPORTANT
, "ERROR: Failed to compose ssl_crtvd " <<
223 "request for " << validationRequest
.domainName
<<
224 " certificate: " << e
.what() << "; will now block to " <<
225 "validate that certificate.");
226 // fall through to do blocking in-process generation.
227 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
229 noteNegotiationDone(anErr
);
237 noteNegotiationDone(NULL
);
243 Security::PeerConnector::sslCrtvdHandleReply(Ssl::CertValidationResponse::Pointer validationResponse
)
245 Must(validationResponse
!= NULL
);
247 Ssl::ErrorDetail
*errDetails
= NULL
;
248 bool validatorFailed
= false;
249 if (!Comm::IsConnOpen(serverConnection())) {
253 if (Debug::Enabled(83, 5)) {
254 Security::SessionPointer
ssl(fd_table
[serverConnection()->fd
].ssl
);
255 SBuf
*server
= static_cast<SBuf
*>(SSL_get_ex_data(ssl
.get(), ssl_ex_index_server
));
256 debugs(83,5, *server
<< " cert validation result: " << validationResponse
->resultCode
);
259 if (validationResponse
->resultCode
== ::Helper::Error
) {
260 if (Security::CertErrors
*errs
= sslCrtvdCheckForErrors(*validationResponse
, errDetails
)) {
261 Security::SessionPointer
session(fd_table
[serverConnection()->fd
].ssl
);
262 Security::CertErrors
*oldErrs
= static_cast<Security::CertErrors
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_errors
));
263 SSL_set_ex_data(session
.get(), ssl_ex_index_ssl_errors
, (void *)errs
);
266 } else if (validationResponse
->resultCode
!= ::Helper::Okay
)
267 validatorFailed
= true;
269 if (!errDetails
&& !validatorFailed
) {
270 noteNegotiationDone(NULL
);
275 ErrorState
*anErr
= NULL
;
276 if (validatorFailed
) {
277 anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
279 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, request
.getRaw());
280 anErr
->detail
= errDetails
;
281 /*anErr->xerrno= Should preserved*/
284 noteNegotiationDone(anErr
);
292 /// Checks errors in the cert. validator response against sslproxy_cert_error.
293 /// The first honored error, if any, is returned via errDetails parameter.
294 /// The method returns all seen errors except SSL_ERROR_NONE as Security::CertErrors.
295 Security::CertErrors
*
296 Security::PeerConnector::sslCrtvdCheckForErrors(Ssl::CertValidationResponse
const &resp
, Ssl::ErrorDetail
*& errDetails
)
298 ACLFilledChecklist
*check
= NULL
;
299 if (acl_access
*acl
= ::Config
.ssl_client
.cert_error
) {
300 check
= new ACLFilledChecklist(acl
, request
.getRaw(), dash_str
);
304 Security::CertErrors
*errs
= nullptr;
305 Security::SessionPointer
session(fd_table
[serverConnection()->fd
].ssl
);
306 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI
;
307 for (SVCRECI i
= resp
.errors
.begin(); i
!= resp
.errors
.end(); ++i
) {
308 debugs(83, 7, "Error item: " << i
->error_no
<< " " << i
->error_reason
);
310 assert(i
->error_no
!= SSL_ERROR_NONE
);
313 bool allowed
= false;
315 check
->sslErrors
= new Security::CertErrors(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
316 if (check
->fastCheck() == ACCESS_ALLOWED
)
319 // else the Config.ssl_client.cert_error access list is not defined
320 // and the first error will cause the error page
323 debugs(83, 3, "bypassing SSL error " << i
->error_no
<< " in " << "buffer");
325 debugs(83, 5, "confirming SSL error " << i
->error_no
);
326 X509
*brokenCert
= i
->cert
.get();
327 Security::CertPointer
peerCert(SSL_get_peer_certificate(session
.get()));
328 const char *aReason
= i
->error_reason
.empty() ? NULL
: i
->error_reason
.c_str();
329 errDetails
= new Ssl::ErrorDetail(i
->error_no
, peerCert
.get(), brokenCert
, aReason
);
332 delete check
->sslErrors
;
333 check
->sslErrors
= NULL
;
338 errs
= new Security::CertErrors(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
340 errs
->push_back_unique(Security::CertError(i
->error_no
, i
->cert
, i
->error_depth
));
349 /// A wrapper for Comm::SetSelect() notifications.
351 Security::PeerConnector::NegotiateSsl(int, void *data
)
353 PeerConnector
*pc
= static_cast<Security::PeerConnector
*>(data
);
354 // Use job calls to add done() checks and other job logic/protections.
355 CallJobHere(83, 7, pc
, Security::PeerConnector
, negotiate
);
359 Security::PeerConnector::handleNegotiateError(const int ret
)
362 const int fd
= serverConnection()->fd
;
363 unsigned long ssl_lib_error
= SSL_ERROR_NONE
;
364 Security::SessionPointer
session(fd_table
[fd
].ssl
);
365 const int ssl_error
= SSL_get_error(session
.get(), ret
);
368 case SSL_ERROR_WANT_READ
:
372 case SSL_ERROR_WANT_WRITE
:
377 case SSL_ERROR_SYSCALL
:
378 ssl_lib_error
= ERR_get_error();
379 // proceed to the general error handling code
382 // no special error handling for all other errors
386 // Log connection details, if any
387 recordNegotiationDetails();
388 noteNegotiationError(ret
, ssl_error
, ssl_lib_error
);
393 Security::PeerConnector::noteWantRead()
395 const int fd
= serverConnection()->fd
;
397 Security::SessionPointer
session(fd_table
[fd
].ssl
);
398 BIO
*b
= SSL_get_rbio(session
.get());
399 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
400 if (srvBio
->holdRead()) {
401 if (srvBio
->gotHello()) {
402 if (checkForMissingCertificates())
403 return; // Wait to download certificates before proceed.
405 srvBio
->holdRead(false);
406 // schedule a negotiateSSl to allow openSSL parse received data
407 Security::PeerConnector::NegotiateSsl(fd
, this);
409 } else if (srvBio
->gotHelloFailed()) {
410 srvBio
->holdRead(false);
411 debugs(83, DBG_IMPORTANT
, "Error parsing SSL Server Hello Message on FD " << fd
);
412 // schedule a negotiateSSl to allow openSSL parse received data
413 Security::PeerConnector::NegotiateSsl(fd
, this);
419 Comm::SetSelect(fd
, COMM_SELECT_READ
, &NegotiateSsl
, this, 0);
423 Security::PeerConnector::noteWantWrite()
425 const int fd
= serverConnection()->fd
;
426 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, &NegotiateSsl
, this, 0);
431 Security::PeerConnector::noteNegotiationError(const int ret
, const int ssl_error
, const int ssl_lib_error
)
433 #if USE_OPENSSL // not used unless OpenSSL enabled.
435 int sysErrNo
= EPROTO
;
437 int sysErrNo
= EACCES
;
440 // store/report errno when ssl_error is SSL_ERROR_SYSCALL, ssl_lib_error is 0, and ret is -1
441 if (ssl_error
== SSL_ERROR_SYSCALL
&& ret
== -1 && ssl_lib_error
== 0)
444 const int fd
= serverConnection()->fd
;
445 debugs(83, DBG_IMPORTANT
, "Error negotiating SSL on FD " << fd
<<
446 ": " << ERR_error_string(ssl_lib_error
, NULL
) << " (" <<
447 ssl_error
<< "/" << ret
<< "/" << errno
<< ")");
449 ErrorState
*anErr
= NULL
;
451 anErr
= ErrorState::NewForwarding(ERR_SECURE_CONNECT_FAIL
, request
.getRaw());
453 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, NULL
);
454 anErr
->xerrno
= sysErrNo
;
456 Security::SessionPointer
session(fd_table
[fd
].ssl
);
457 Ssl::ErrorDetail
*errFromFailure
= static_cast<Ssl::ErrorDetail
*>(SSL_get_ex_data(session
.get(), ssl_ex_index_ssl_error_detail
));
458 if (errFromFailure
!= NULL
) {
459 // The errFromFailure is attached to the ssl object
460 // and will be released when ssl object destroyed.
461 // Copy errFromFailure to a new Ssl::ErrorDetail object
462 anErr
->detail
= new Ssl::ErrorDetail(*errFromFailure
);
464 // server_cert can be NULL here
465 X509
*server_cert
= SSL_get_peer_certificate(session
.get());
466 anErr
->detail
= new Ssl::ErrorDetail(SQUID_ERR_SSL_HANDSHAKE
, server_cert
, NULL
);
467 X509_free(server_cert
);
470 if (ssl_lib_error
!= SSL_ERROR_NONE
)
471 anErr
->detail
->setLibError(ssl_lib_error
);
473 noteNegotiationDone(anErr
);
479 Security::PeerConnector::bail(ErrorState
*error
)
481 Must(error
); // or the recepient will not know there was a problem
482 Must(callback
!= NULL
);
483 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(callback
->getDialer());
485 dialer
->answer().error
= error
;
488 // Our job is done. The callabck recepient will probably close the failed
489 // peer connection and try another peer or go direct (if possible). We
490 // can close the connection ourselves (our error notification would reach
491 // the recepient before the fd-closure notification), but we would rather
492 // minimize the number of fd-closure notifications and let the recepient
493 // manage the TCP state of the connection.
497 Security::PeerConnector::callBack()
499 AsyncCall::Pointer cb
= callback
;
500 // Do this now so that if we throw below, swanSong() assert that we _tried_
501 // to call back holds.
502 callback
= NULL
; // this should make done() true
504 // remove close handler
505 comm_remove_close_handler(serverConnection()->fd
, closeHandler
);
507 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(cb
->getDialer());
509 dialer
->answer().conn
= serverConnection();
510 ScheduleCallHere(cb
);
514 Security::PeerConnector::swanSong()
516 // XXX: unregister fd-closure monitoring and CommSetSelect interest, if any
517 AsyncJob::swanSong();
518 if (callback
!= NULL
) { // paranoid: we have left the caller waiting
519 debugs(83, DBG_IMPORTANT
, "BUG: Unexpected state while connecting to a cache_peer or origin server");
520 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
528 Security::PeerConnector::status() const
533 // TODO: redesign AsyncJob::status() API to avoid this
534 // id and stop reason reporting duplication.
536 if (stopReason
!= NULL
) {
537 buf
.append("Stopped, reason:", 16);
538 buf
.appendf("%s",stopReason
);
540 if (serverConn
!= NULL
)
541 buf
.appendf(" FD %d", serverConn
->fd
);
542 buf
.appendf(" %s%u]", id
.prefix(), id
.value
);
545 return buf
.content();
549 /// CallDialer to allow use Downloader objects within PeerConnector class.
550 class PeerConnectorCertDownloaderDialer
: public Downloader::CbDialer
553 typedef void (Security::PeerConnector::*Method
)(SBuf
&object
, int status
);
555 PeerConnectorCertDownloaderDialer(Method method
, Security::PeerConnector
*pc
):
557 peerConnector_(pc
) {}
560 virtual bool canDial(AsyncCall
&call
) { return peerConnector_
.valid(); }
561 virtual void dial(AsyncCall
&call
) { ((&(*peerConnector_
))->*method_
)(object
, status
); }
562 Method method_
; ///< The Security::PeerConnector method to dial
563 CbcPointer
<Security::PeerConnector
> peerConnector_
; ///< The Security::PeerConnector object
567 Security::PeerConnector::startCertDownloading(SBuf
&url
)
569 AsyncCall::Pointer certCallback
= asyncCall(81, 4,
570 "Security::PeerConnector::certDownloadingDone",
571 PeerConnectorCertDownloaderDialer(&Security::PeerConnector::certDownloadingDone
, this));
573 const Downloader
*csd
= (request
? dynamic_cast<const Downloader
*>(request
->downloader
.valid()) : nullptr);
574 Downloader
*dl
= new Downloader(url
, certCallback
, csd
? csd
->nestedLevel() + 1 : 1);
579 Security::PeerConnector::certDownloadingDone(SBuf
&obj
, int downloadStatus
)
582 debugs(81, 5, "Certificate downloading status: " << downloadStatus
<< " certificate size: " << obj
.length());
584 // get ServerBio from SSL object
585 const int fd
= serverConnection()->fd
;
586 Security::SessionPointer
session(fd_table
[fd
].ssl
);
587 BIO
*b
= SSL_get_rbio(session
.get());
588 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
590 // Parse Certificate. Assume that it is in DER format.
591 // According to RFC 4325:
592 // The server must provide a DER encoded certificate or a collection
593 // collection of certificates in a "certs-only" CMS message.
594 // The applications MUST accept DER encoded certificates and SHOULD
595 // be able to accept collection of certificates.
596 // TODO: support collection of certificates
597 const unsigned char *raw
= (const unsigned char*)obj
.rawContent();
598 if (X509
*cert
= d2i_X509(NULL
, &raw
, obj
.length())) {
600 debugs(81, 5, "Retrieved certificate: " << X509_NAME_oneline(X509_get_subject_name(cert
), buffer
, 1024));
601 const Security::CertList
&certsList
= srvBio
->serverCertificatesIfAny();
602 if (const char *issuerUri
= Ssl::uriOfIssuerIfMissing(cert
, certsList
)) {
603 urlsOfMissingCerts
.push(SBuf(issuerUri
));
605 Ssl::SSL_add_untrusted_cert(session
.get(), cert
);
608 // Check if there are URIs to download from and if yes start downloading
609 // the first in queue.
610 if (urlsOfMissingCerts
.size() && certsDownloads
<= MaxCertsDownloads
) {
611 startCertDownloading(urlsOfMissingCerts
.front());
612 urlsOfMissingCerts
.pop();
616 srvBio
->holdRead(false);
617 Security::PeerConnector::NegotiateSsl(serverConnection()->fd
, this);
621 Security::PeerConnector::checkForMissingCertificates()
623 // Check for nested SSL certificates downloads. For example when the
624 // certificate located in an SSL site which requires to download a
625 // a missing certificate (... from an SSL site which requires to ...).
627 const Downloader
*csd
= (request
? request
->downloader
.get() : nullptr);
628 if (csd
&& csd
->nestedLevel() >= MaxNestedDownloads
)
631 const int fd
= serverConnection()->fd
;
632 Security::SessionPointer
session(fd_table
[fd
].ssl
);
633 BIO
*b
= SSL_get_rbio(session
.get());
634 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
635 const Security::CertList
&certs
= srvBio
->serverCertificatesIfAny();
638 debugs(83, 5, "SSL server sent " << certs
.size() << " certificates");
639 Ssl::missingChainCertificatesUrls(urlsOfMissingCerts
, certs
);
640 if (urlsOfMissingCerts
.size()) {
641 startCertDownloading(urlsOfMissingCerts
.front());
642 urlsOfMissingCerts
.pop();