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() && initializeTls(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::initializeTls(Security::SessionPointer
&serverSession
)
103 Security::ContextPtr
sslContext(getSslContext());
106 if (!Ssl::CreateClient(sslContext
, 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 SSL 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()
155 const int fd
= serverConnection()->fd
;
156 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
158 // retrieve TLS server negotiated information if any
159 serverConnection()->tlsNegotiations()->retrieveNegotiatedInfo(ssl
);
160 // retrieve TLS parsed extra info
161 BIO
*b
= SSL_get_rbio(ssl
);
162 Ssl::ServerBio
*bio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
163 if (const Security::TlsDetails::Pointer
&details
= bio
->receivedHelloDetails())
164 serverConnection()->tlsNegotiations()->retrieveParsedInfo(details
);
169 Security::PeerConnector::negotiateSsl()
171 if (!Comm::IsConnOpen(serverConnection()) || fd_table
[serverConnection()->fd
].closing())
175 const int fd
= serverConnection()->fd
;
176 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
177 const int result
= SSL_connect(ssl
);
179 const int result
= -1;
182 handleNegotiateError(result
);
183 return; // we might be gone by now
186 recordNegotiationDetails();
195 Security::PeerConnector::sslFinalized()
198 if (Ssl::TheConfig
.ssl_crt_validator
&& useCertValidator_
) {
199 const int fd
= serverConnection()->fd
;
200 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
202 Ssl::CertValidationRequest validationRequest
;
203 // WARNING: Currently we do not use any locking for any of the
204 // members of the Ssl::CertValidationRequest class. In this code the
205 // Ssl::CertValidationRequest object used only to pass data to
206 // Ssl::CertValidationHelper::submit method.
207 validationRequest
.ssl
= ssl
;
208 validationRequest
.domainName
= request
->url
.host();
209 if (Ssl::CertErrors
*errs
= static_cast<Ssl::CertErrors
*>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_errors
)))
210 // validationRequest disappears on return so no need to cbdataReference
211 validationRequest
.errors
= errs
;
213 validationRequest
.errors
= NULL
;
215 debugs(83, 5, "Sending SSL certificate for validation to ssl_crtvd.");
216 AsyncCall::Pointer call
= asyncCall(83,5, "Security::PeerConnector::sslCrtvdHandleReply", Ssl::CertValidationHelper::CbDialer(this, &Security::PeerConnector::sslCrtvdHandleReply
, nullptr));
217 Ssl::CertValidationHelper::GetInstance()->sslSubmit(validationRequest
, call
);
219 } catch (const std::exception
&e
) {
220 debugs(83, DBG_IMPORTANT
, "ERROR: Failed to compose ssl_crtvd " <<
221 "request for " << validationRequest
.domainName
<<
222 " certificate: " << e
.what() << "; will now block to " <<
223 "validate that certificate.");
224 // fall through to do blocking in-process generation.
225 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
227 noteNegotiationDone(anErr
);
235 noteNegotiationDone(NULL
);
241 Security::PeerConnector::sslCrtvdHandleReply(Ssl::CertValidationResponse::Pointer validationResponse
)
243 Must(validationResponse
!= NULL
);
245 Ssl::ErrorDetail
*errDetails
= NULL
;
246 bool validatorFailed
= false;
247 if (!Comm::IsConnOpen(serverConnection())) {
251 debugs(83,5, request
->url
.host() << " cert validation result: " << validationResponse
->resultCode
);
253 if (validationResponse
->resultCode
== ::Helper::Error
) {
254 if (Ssl::CertErrors
*errs
= sslCrtvdCheckForErrors(*validationResponse
, errDetails
)) {
255 Security::SessionPtr ssl
= fd_table
[serverConnection()->fd
].ssl
.get();
256 Ssl::CertErrors
*oldErrs
= static_cast<Ssl::CertErrors
*>(SSL_get_ex_data(ssl
, ssl_ex_index_ssl_errors
));
257 SSL_set_ex_data(ssl
, ssl_ex_index_ssl_errors
, (void *)errs
);
260 } else if (validationResponse
->resultCode
!= ::Helper::Okay
)
261 validatorFailed
= true;
263 if (!errDetails
&& !validatorFailed
) {
264 noteNegotiationDone(NULL
);
269 ErrorState
*anErr
= NULL
;
270 if (validatorFailed
) {
271 anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
273 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, request
.getRaw());
274 anErr
->detail
= errDetails
;
275 /*anErr->xerrno= Should preserved*/
278 noteNegotiationDone(anErr
);
286 /// Checks errors in the cert. validator response against sslproxy_cert_error.
287 /// The first honored error, if any, is returned via errDetails parameter.
288 /// The method returns all seen errors except SSL_ERROR_NONE as Ssl::CertErrors.
290 Security::PeerConnector::sslCrtvdCheckForErrors(Ssl::CertValidationResponse
const &resp
, Ssl::ErrorDetail
*& errDetails
)
292 Ssl::CertErrors
*errs
= NULL
;
294 ACLFilledChecklist
*check
= NULL
;
295 if (acl_access
*acl
= ::Config
.ssl_client
.cert_error
) {
296 check
= new ACLFilledChecklist(acl
, request
.getRaw(), dash_str
);
300 Security::SessionPtr ssl
= fd_table
[serverConnection()->fd
].ssl
.get();
301 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI
;
302 for (SVCRECI i
= resp
.errors
.begin(); i
!= resp
.errors
.end(); ++i
) {
303 debugs(83, 7, "Error item: " << i
->error_no
<< " " << i
->error_reason
);
305 assert(i
->error_no
!= SSL_ERROR_NONE
);
308 bool allowed
= false;
310 check
->sslErrors
= new Ssl::CertErrors(Ssl::CertError(i
->error_no
, i
->cert
.get(), i
->error_depth
));
311 if (check
->fastCheck() == ACCESS_ALLOWED
)
314 // else the Config.ssl_client.cert_error access list is not defined
315 // and the first error will cause the error page
318 debugs(83, 3, "bypassing SSL error " << i
->error_no
<< " in " << "buffer");
320 debugs(83, 5, "confirming SSL error " << i
->error_no
);
321 X509
*brokenCert
= i
->cert
.get();
322 Security::CertPointer
peerCert(SSL_get_peer_certificate(ssl
));
323 const char *aReason
= i
->error_reason
.empty() ? NULL
: i
->error_reason
.c_str();
324 errDetails
= new Ssl::ErrorDetail(i
->error_no
, peerCert
.get(), brokenCert
, aReason
);
327 delete check
->sslErrors
;
328 check
->sslErrors
= NULL
;
333 errs
= new Ssl::CertErrors(Ssl::CertError(i
->error_no
, i
->cert
.get(), i
->error_depth
));
335 errs
->push_back_unique(Ssl::CertError(i
->error_no
, i
->cert
.get(), i
->error_depth
));
344 /// A wrapper for Comm::SetSelect() notifications.
346 Security::PeerConnector::NegotiateSsl(int, void *data
)
348 PeerConnector
*pc
= static_cast<PeerConnector
*>(data
);
349 // Use job calls to add done() checks and other job logic/protections.
350 CallJobHere(83, 7, pc
, Security::PeerConnector
, negotiateSsl
);
354 Security::PeerConnector::handleNegotiateError(const int ret
)
357 const int fd
= serverConnection()->fd
;
358 unsigned long ssl_lib_error
= SSL_ERROR_NONE
;
359 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
360 const int ssl_error
= SSL_get_error(ssl
, ret
);
363 case SSL_ERROR_WANT_READ
:
367 case SSL_ERROR_WANT_WRITE
:
372 case SSL_ERROR_SYSCALL
:
373 ssl_lib_error
= ERR_get_error();
374 // proceed to the general error handling code
377 // no special error handling for all other errors
381 // Log connection details, if any
382 recordNegotiationDetails();
383 noteSslNegotiationError(ret
, ssl_error
, ssl_lib_error
);
388 Security::PeerConnector::noteWantRead()
390 const int fd
= serverConnection()->fd
;
392 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
393 BIO
*b
= SSL_get_rbio(ssl
);
394 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
395 if (srvBio
->holdRead()) {
396 if (srvBio
->gotHello()) {
397 if (checkForMissingCertificates())
398 return; // Wait to download certificates before proceed.
400 srvBio
->holdRead(false);
401 // schedule a negotiateSSl to allow openSSL parse received data
402 Security::PeerConnector::NegotiateSsl(fd
, this);
404 } else if (srvBio
->gotHelloFailed()) {
405 srvBio
->holdRead(false);
406 debugs(83, DBG_IMPORTANT
, "Error parsing SSL Server Hello Message on FD " << fd
);
407 // schedule a negotiateSSl to allow openSSL parse received data
408 Security::PeerConnector::NegotiateSsl(fd
, this);
414 Comm::SetSelect(fd
, COMM_SELECT_READ
, &NegotiateSsl
, this, 0);
418 Security::PeerConnector::noteWantWrite()
420 const int fd
= serverConnection()->fd
;
421 Comm::SetSelect(fd
, COMM_SELECT_WRITE
, &NegotiateSsl
, this, 0);
426 Security::PeerConnector::noteSslNegotiationError(const int ret
, const int ssl_error
, const int ssl_lib_error
)
428 #if USE_OPENSSL // not used unless OpenSSL enabled.
430 int sysErrNo
= EPROTO
;
432 int sysErrNo
= EACCES
;
435 // store/report errno when ssl_error is SSL_ERROR_SYSCALL, ssl_lib_error is 0, and ret is -1
436 if (ssl_error
== SSL_ERROR_SYSCALL
&& ret
== -1 && ssl_lib_error
== 0)
439 const int fd
= serverConnection()->fd
;
440 debugs(83, DBG_IMPORTANT
, "Error negotiating SSL on FD " << fd
<<
441 ": " << ERR_error_string(ssl_lib_error
, NULL
) << " (" <<
442 ssl_error
<< "/" << ret
<< "/" << errno
<< ")");
444 ErrorState
*anErr
= NULL
;
446 anErr
= ErrorState::NewForwarding(ERR_SECURE_CONNECT_FAIL
, request
.getRaw());
448 anErr
= new ErrorState(ERR_SECURE_CONNECT_FAIL
, Http::scServiceUnavailable
, NULL
);
449 anErr
->xerrno
= sysErrNo
;
451 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
452 Ssl::ErrorDetail
*errFromFailure
= (Ssl::ErrorDetail
*)SSL_get_ex_data(ssl
, ssl_ex_index_ssl_error_detail
);
453 if (errFromFailure
!= NULL
) {
454 // The errFromFailure is attached to the ssl object
455 // and will be released when ssl object destroyed.
456 // Copy errFromFailure to a new Ssl::ErrorDetail object
457 anErr
->detail
= new Ssl::ErrorDetail(*errFromFailure
);
459 // server_cert can be NULL here
460 X509
*server_cert
= SSL_get_peer_certificate(ssl
);
461 anErr
->detail
= new Ssl::ErrorDetail(SQUID_ERR_SSL_HANDSHAKE
, server_cert
, NULL
);
462 X509_free(server_cert
);
465 if (ssl_lib_error
!= SSL_ERROR_NONE
)
466 anErr
->detail
->setLibError(ssl_lib_error
);
468 noteNegotiationDone(anErr
);
474 Security::PeerConnector::bail(ErrorState
*error
)
476 Must(error
); // or the recepient will not know there was a problem
477 Must(callback
!= NULL
);
478 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(callback
->getDialer());
480 dialer
->answer().error
= error
;
483 // Our job is done. The callabck recepient will probably close the failed
484 // peer connection and try another peer or go direct (if possible). We
485 // can close the connection ourselves (our error notification would reach
486 // the recepient before the fd-closure notification), but we would rather
487 // minimize the number of fd-closure notifications and let the recepient
488 // manage the TCP state of the connection.
492 Security::PeerConnector::callBack()
494 AsyncCall::Pointer cb
= callback
;
495 // Do this now so that if we throw below, swanSong() assert that we _tried_
496 // to call back holds.
497 callback
= NULL
; // this should make done() true
499 // remove close handler
500 comm_remove_close_handler(serverConnection()->fd
, closeHandler
);
502 CbDialer
*dialer
= dynamic_cast<CbDialer
*>(cb
->getDialer());
504 dialer
->answer().conn
= serverConnection();
505 ScheduleCallHere(cb
);
509 Security::PeerConnector::swanSong()
511 // XXX: unregister fd-closure monitoring and CommSetSelect interest, if any
512 AsyncJob::swanSong();
513 if (callback
!= NULL
) { // paranoid: we have left the caller waiting
514 debugs(83, DBG_IMPORTANT
, "BUG: Unexpected state while connecting to a cache_peer or origin server");
515 ErrorState
*anErr
= new ErrorState(ERR_GATEWAY_FAILURE
, Http::scInternalServerError
, request
.getRaw());
523 Security::PeerConnector::status() const
528 // TODO: redesign AsyncJob::status() API to avoid this
529 // id and stop reason reporting duplication.
531 if (stopReason
!= NULL
) {
532 buf
.append("Stopped, reason:", 16);
533 buf
.appendf("%s",stopReason
);
535 if (serverConn
!= NULL
)
536 buf
.appendf(" FD %d", serverConn
->fd
);
537 buf
.appendf(" %s%u]", id
.Prefix
, id
.value
);
540 return buf
.content();
544 /// CallDialer to allow use Downloader objects within PeerConnector class.
545 class PeerConnectorCertDownloaderDialer
: public Downloader::CbDialer
548 typedef void (Security::PeerConnector::*Method
)(SBuf
&object
, int status
);
550 PeerConnectorCertDownloaderDialer(Method method
, Security::PeerConnector
*pc
):
552 peerConnector_(pc
) {}
555 virtual bool canDial(AsyncCall
&call
) { return peerConnector_
.valid(); }
556 virtual void dial(AsyncCall
&call
) { ((&(*peerConnector_
))->*method_
)(object
, status
); }
557 Method method_
; ///< The Security::PeerConnector method to dial
558 CbcPointer
<Security::PeerConnector
> peerConnector_
; ///< The Security::PeerConnector object
562 Security::PeerConnector::startCertDownloading(SBuf
&url
)
564 AsyncCall::Pointer certCallback
= asyncCall(81, 4,
565 "Security::PeerConnector::certDownloadingDone",
566 PeerConnectorCertDownloaderDialer(&Security::PeerConnector::certDownloadingDone
, this));
568 const Downloader
*csd
= dynamic_cast<const Downloader
*>(request
->downloader
.valid());
569 Downloader
*dl
= new Downloader(url
, certCallback
, csd
? csd
->nestedLevel() + 1 : 1);
574 Security::PeerConnector::certDownloadingDone(SBuf
&obj
, int downloadStatus
)
577 debugs(81, 5, "Certificate downloading status: " << downloadStatus
<< " certificate size: " << obj
.length());
579 // get ServerBio from SSL object
580 const int fd
= serverConnection()->fd
;
581 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
582 BIO
*b
= SSL_get_rbio(ssl
);
583 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
585 // Parse Certificate. Assume that it is in DER format.
586 // According to RFC 4325:
587 // The server must provide a DER encoded certificate or a collection
588 // collection of certificates in a "certs-only" CMS message.
589 // The applications MUST accept DER encoded certificates and SHOULD
590 // be able to accept collection of certificates.
591 // TODO: support collection of certificates
592 const unsigned char *raw
= (const unsigned char*)obj
.rawContent();
593 if (X509
*cert
= d2i_X509(NULL
, &raw
, obj
.length())) {
595 debugs(81, 5, "Retrieved certificate: " << X509_NAME_oneline(X509_get_subject_name(cert
), buffer
, 1024));
596 const Security::CertList
&certsList
= srvBio
->serverCertificatesIfAny();
597 if (const char *issuerUri
= Ssl::uriOfIssuerIfMissing(cert
, certsList
)) {
598 urlsOfMissingCerts
.push(SBuf(issuerUri
));
600 Ssl::SSL_add_untrusted_cert(ssl
, cert
);
603 // Check if there are URIs to download from and if yes start downloading
604 // the first in queue.
605 if (urlsOfMissingCerts
.size() && certsDownloads
<= MaxCertsDownloads
) {
606 startCertDownloading(urlsOfMissingCerts
.front());
607 urlsOfMissingCerts
.pop();
611 srvBio
->holdRead(false);
612 Security::PeerConnector::NegotiateSsl(serverConnection()->fd
, this);
616 Security::PeerConnector::checkForMissingCertificates()
618 // Check for nested SSL certificates downloads. For example when the
619 // certificate located in an SSL site which requires to download a
620 // a missing certificate (... from an SSL site which requires to ...).
622 const Downloader
*csd
= request
->downloader
.get();
623 if (csd
&& csd
->nestedLevel() >= MaxNestedDownloads
)
626 const int fd
= serverConnection()->fd
;
627 Security::SessionPtr ssl
= fd_table
[fd
].ssl
.get();
628 BIO
*b
= SSL_get_rbio(ssl
);
629 Ssl::ServerBio
*srvBio
= static_cast<Ssl::ServerBio
*>(b
->ptr
);
630 const Security::CertList
&certs
= srvBio
->serverCertificatesIfAny();
633 debugs(83, 5, "SSL server sent " << certs
.size() << " certificates");
634 Ssl::missingChainCertificatesUrls(urlsOfMissingCerts
, certs
);
635 if (urlsOfMissingCerts
.size()) {
636 startCertDownloading(urlsOfMissingCerts
.front());
637 urlsOfMissingCerts
.pop();