]> git.ipfire.org Git - thirdparty/squid.git/blob - src/security/PeerConnector.cc
More debugs for PeerConnector
[thirdparty/squid.git] / src / security / PeerConnector.cc
1 /*
2 * Copyright (C) 1996-2016 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 TLS Server/Peer negotiation */
10
11 #include "squid.h"
12 #include "acl/FilledChecklist.h"
13 #include "comm/Loops.h"
14 #include "Downloader.h"
15 #include "errorpage.h"
16 #include "fde.h"
17 #include "http/Stream.h"
18 #include "HttpRequest.h"
19 #include "security/NegotiationHistory.h"
20 #include "security/PeerConnector.h"
21 #include "SquidConfig.h"
22 #if USE_OPENSSL
23 #include "ssl/bio.h"
24 #include "ssl/cert_validate_message.h"
25 #include "ssl/Config.h"
26 #include "ssl/helper.h"
27 #endif
28
29 CBDATA_NAMESPACED_CLASS_INIT(Security, PeerConnector);
30
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),
34 al(alp),
35 callback(aCallback),
36 negotiationTimeout(timeout),
37 startTime(squid_curtime),
38 useCertValidator_(true),
39 certsDownloads(0)
40 {
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()));
44 }
45
46 Security::PeerConnector::~PeerConnector()
47 {
48 debugs(83, 5, "Security::PeerConnector destructed, this=" << (void*)this);
49 }
50
51 bool Security::PeerConnector::doneAll() const
52 {
53 return (!callback || callback->canceled()) && AsyncJob::doneAll();
54 }
55
56 /// Preps connection and SSL state. Calls negotiate().
57 void
58 Security::PeerConnector::start()
59 {
60 AsyncJob::start();
61 debugs(83, 5, "this=" << (void*)this);
62
63 Security::SessionPointer tmp;
64 if (prepareSocket() && initialize(tmp))
65 negotiate();
66 else
67 mustStop("Security::PeerConnector TLS socket initialize failed");
68 }
69
70 void
71 Security::PeerConnector::commCloseHandler(const CommCloseCbParams &params)
72 {
73 debugs(83, 5, "FD " << params.fd << ", Security::PeerConnector=" << params.data);
74 connectionClosed("Security::PeerConnector::commCloseHandler");
75 }
76
77 void
78 Security::PeerConnector::connectionClosed(const char *reason)
79 {
80 debugs(83, 5, reason << " socket closed/closing. this=" << (void*)this);
81 mustStop(reason);
82 callback = NULL;
83 }
84
85 bool
86 Security::PeerConnector::prepareSocket()
87 {
88 debugs(83, 5, serverConnection() << ", this=" << (void*)this);
89 if (!Comm::IsConnOpen(serverConnection()) || fd_table[serverConnection()->fd].closing()) {
90 connectionClosed("Security::PeerConnector::prepareSocket");
91 return false;
92 }
93
94 debugs(83, 5, serverConnection());
95
96 // watch for external connection closures
97 typedef CommCbMemFunT<Security::PeerConnector, CommCloseCbParams> Dialer;
98 closeHandler = JobCallback(9, 5, Dialer, this, Security::PeerConnector::commCloseHandler);
99 comm_add_close_handler(serverConnection()->fd, closeHandler);
100 return true;
101 }
102
103 bool
104 Security::PeerConnector::initialize(Security::SessionPointer &serverSession)
105 {
106 Security::ContextPointer ctx(getTlsContext());
107 debugs(83, 5, serverConnection() << ", ctx=" << (void*)ctx.get());
108
109 if (!ctx || !Security::CreateClientSession(ctx, serverConnection(), "server https start")) {
110 const auto xerrno = errno;
111 if (!ctx) {
112 debugs(83, DBG_IMPORTANT, "Error initializing TLS connection: No security context.");
113 } // else CreateClientSession() did the appropriate debugs() already
114 ErrorState *anErr = new ErrorState(ERR_SOCKET_FAILURE, Http::scInternalServerError, request.getRaw());
115 anErr->xerrno = xerrno;
116 noteNegotiationDone(anErr);
117 bail(anErr);
118 return false;
119 }
120
121 // A TLS/SSL session has now been created for the connection and stored in fd_table
122 serverSession = fd_table[serverConnection()->fd].ssl;
123 debugs(83, 5, serverConnection() << ", session=" << (void*)serverSession.get());
124
125 #if USE_OPENSSL
126 // If CertValidation Helper used do not lookup checklist for errors,
127 // but keep a list of errors to send it to CertValidator
128 if (!Ssl::TheConfig.ssl_crt_validator) {
129 // Create the ACL check list now, while we have access to more info.
130 // The list is used in ssl_verify_cb() and is freed in ssl_free().
131 if (acl_access *acl = ::Config.ssl_client.cert_error) {
132 ACLFilledChecklist *check = new ACLFilledChecklist(acl, request.getRaw(), dash_str);
133 check->al = al;
134 // check->fd(fd); XXX: need client FD here
135 SSL_set_ex_data(serverSession.get(), ssl_ex_index_cert_error_check, check);
136 }
137 }
138 #endif
139
140 return true;
141 }
142
143 void
144 Security::PeerConnector::setReadTimeout()
145 {
146 int timeToRead;
147 if (negotiationTimeout) {
148 const int timeUsed = squid_curtime - startTime;
149 const int timeLeft = max(0, static_cast<int>(negotiationTimeout - timeUsed));
150 timeToRead = min(static_cast<int>(::Config.Timeout.read), timeLeft);
151 } else
152 timeToRead = ::Config.Timeout.read;
153 AsyncCall::Pointer nil;
154 commSetConnTimeout(serverConnection(), timeToRead, nil);
155 }
156
157 void
158 Security::PeerConnector::recordNegotiationDetails()
159 {
160 const int fd = serverConnection()->fd;
161 Security::SessionPointer session(fd_table[fd].ssl);
162
163 // retrieve TLS server negotiated information if any
164 serverConnection()->tlsNegotiations()->retrieveNegotiatedInfo(session);
165
166 #if USE_OPENSSL
167 // retrieve TLS parsed extra info
168 BIO *b = SSL_get_rbio(session.get());
169 Ssl::ServerBio *bio = static_cast<Ssl::ServerBio *>(b->ptr);
170 if (const Security::TlsDetails::Pointer &details = bio->receivedHelloDetails())
171 serverConnection()->tlsNegotiations()->retrieveParsedInfo(details);
172 #endif
173 }
174
175 void
176 Security::PeerConnector::negotiate()
177 {
178 if (!Comm::IsConnOpen(serverConnection()))
179 return;
180
181 const int fd = serverConnection()->fd;
182 if (fd_table[fd].closing())
183 return;
184
185 #if USE_OPENSSL
186 auto session = fd_table[fd].ssl.get();
187 debugs(83, 5, "SSL_connect session=" << (void*)session);
188 const int result = SSL_connect(session);
189 if (result <= 0) {
190 #elif USE_GNUTLS
191 auto session = fd_table[fd].ssl.get();
192 debugs(83, 5, "gnutls_handshake session=" << (void*)session);
193 const int result = gnutls_handshake(session);
194 if (result != GNUTLS_E_SUCCESS) {
195 debugs(83, 5, "gnutls_handshake session=" << (void*)session << ", result=" << result);
196 #else
197 if (const int result = -1) {
198 #endif
199 handleNegotiateError(result);
200 return; // we might be gone by now
201 }
202
203 recordNegotiationDetails();
204
205 if (!sslFinalized())
206 return;
207
208 callBack();
209 }
210
211 bool
212 Security::PeerConnector::sslFinalized()
213 {
214 #if USE_OPENSSL
215 if (Ssl::TheConfig.ssl_crt_validator && useCertValidator_) {
216 const int fd = serverConnection()->fd;
217 Security::SessionPointer session(fd_table[fd].ssl);
218
219 Ssl::CertValidationRequest validationRequest;
220 // WARNING: Currently we do not use any locking for 'errors' member
221 // of the Ssl::CertValidationRequest class. In this code the
222 // Ssl::CertValidationRequest object used only to pass data to
223 // Ssl::CertValidationHelper::submit method.
224 validationRequest.ssl = session;
225 if (SBuf *dName = (SBuf *)SSL_get_ex_data(session.get(), ssl_ex_index_server))
226 validationRequest.domainName = dName->c_str();
227 if (Security::CertErrors *errs = static_cast<Security::CertErrors *>(SSL_get_ex_data(session.get(), ssl_ex_index_ssl_errors)))
228 // validationRequest disappears on return so no need to cbdataReference
229 validationRequest.errors = errs;
230 try {
231 debugs(83, 5, "Sending SSL certificate for validation to ssl_crtvd.");
232 AsyncCall::Pointer call = asyncCall(83,5, "Security::PeerConnector::sslCrtvdHandleReply", Ssl::CertValidationHelper::CbDialer(this, &Security::PeerConnector::sslCrtvdHandleReply, nullptr));
233 Ssl::CertValidationHelper::GetInstance()->sslSubmit(validationRequest, call);
234 return false;
235 } catch (const std::exception &e) {
236 debugs(83, DBG_IMPORTANT, "ERROR: Failed to compose ssl_crtvd " <<
237 "request for " << validationRequest.domainName <<
238 " certificate: " << e.what() << "; will now block to " <<
239 "validate that certificate.");
240 // fall through to do blocking in-process generation.
241 ErrorState *anErr = new ErrorState(ERR_GATEWAY_FAILURE, Http::scInternalServerError, request.getRaw());
242
243 noteNegotiationDone(anErr);
244 bail(anErr);
245 serverConn->close();
246 return true;
247 }
248 }
249 #endif
250
251 noteNegotiationDone(NULL);
252 return true;
253 }
254
255 #if USE_OPENSSL
256 void
257 Security::PeerConnector::sslCrtvdHandleReply(Ssl::CertValidationResponse::Pointer validationResponse)
258 {
259 Must(validationResponse != NULL);
260
261 Ssl::ErrorDetail *errDetails = NULL;
262 bool validatorFailed = false;
263 if (!Comm::IsConnOpen(serverConnection())) {
264 return;
265 }
266
267 if (Debug::Enabled(83, 5)) {
268 Security::SessionPointer ssl(fd_table[serverConnection()->fd].ssl);
269 SBuf *server = static_cast<SBuf *>(SSL_get_ex_data(ssl.get(), ssl_ex_index_server));
270 debugs(83,5, RawPointer("host", server) << " cert validation result: " << validationResponse->resultCode);
271 }
272
273 if (validationResponse->resultCode == ::Helper::Error) {
274 if (Security::CertErrors *errs = sslCrtvdCheckForErrors(*validationResponse, errDetails)) {
275 Security::SessionPointer session(fd_table[serverConnection()->fd].ssl);
276 Security::CertErrors *oldErrs = static_cast<Security::CertErrors*>(SSL_get_ex_data(session.get(), ssl_ex_index_ssl_errors));
277 SSL_set_ex_data(session.get(), ssl_ex_index_ssl_errors, (void *)errs);
278 delete oldErrs;
279 }
280 } else if (validationResponse->resultCode != ::Helper::Okay)
281 validatorFailed = true;
282
283 if (!errDetails && !validatorFailed) {
284 noteNegotiationDone(NULL);
285 callBack();
286 return;
287 }
288
289 ErrorState *anErr = NULL;
290 if (validatorFailed) {
291 anErr = new ErrorState(ERR_GATEWAY_FAILURE, Http::scInternalServerError, request.getRaw());
292 } else {
293 anErr = new ErrorState(ERR_SECURE_CONNECT_FAIL, Http::scServiceUnavailable, request.getRaw());
294 anErr->detail = errDetails;
295 /*anErr->xerrno= Should preserved*/
296 }
297
298 noteNegotiationDone(anErr);
299 bail(anErr);
300 serverConn->close();
301 return;
302 }
303 #endif
304
305 #if USE_OPENSSL
306 /// Checks errors in the cert. validator response against sslproxy_cert_error.
307 /// The first honored error, if any, is returned via errDetails parameter.
308 /// The method returns all seen errors except SSL_ERROR_NONE as Security::CertErrors.
309 Security::CertErrors *
310 Security::PeerConnector::sslCrtvdCheckForErrors(Ssl::CertValidationResponse const &resp, Ssl::ErrorDetail *& errDetails)
311 {
312 ACLFilledChecklist *check = NULL;
313 if (acl_access *acl = ::Config.ssl_client.cert_error) {
314 check = new ACLFilledChecklist(acl, request.getRaw(), dash_str);
315 check->al = al;
316 }
317
318 Security::CertErrors *errs = nullptr;
319 Security::SessionPointer session(fd_table[serverConnection()->fd].ssl);
320 typedef Ssl::CertValidationResponse::RecvdErrors::const_iterator SVCRECI;
321 for (SVCRECI i = resp.errors.begin(); i != resp.errors.end(); ++i) {
322 debugs(83, 7, "Error item: " << i->error_no << " " << i->error_reason);
323
324 assert(i->error_no != SSL_ERROR_NONE);
325
326 if (!errDetails) {
327 bool allowed = false;
328 if (check) {
329 check->sslErrors = new Security::CertErrors(Security::CertError(i->error_no, i->cert, i->error_depth));
330 if (check->fastCheck() == ACCESS_ALLOWED)
331 allowed = true;
332 }
333 // else the Config.ssl_client.cert_error access list is not defined
334 // and the first error will cause the error page
335
336 if (allowed) {
337 debugs(83, 3, "bypassing SSL error " << i->error_no << " in " << "buffer");
338 } else {
339 debugs(83, 5, "confirming SSL error " << i->error_no);
340 X509 *brokenCert = i->cert.get();
341 Security::CertPointer peerCert(SSL_get_peer_certificate(session.get()));
342 const char *aReason = i->error_reason.empty() ? NULL : i->error_reason.c_str();
343 errDetails = new Ssl::ErrorDetail(i->error_no, peerCert.get(), brokenCert, aReason);
344 }
345 if (check) {
346 delete check->sslErrors;
347 check->sslErrors = NULL;
348 }
349 }
350
351 if (!errs)
352 errs = new Security::CertErrors(Security::CertError(i->error_no, i->cert, i->error_depth));
353 else
354 errs->push_back_unique(Security::CertError(i->error_no, i->cert, i->error_depth));
355 }
356 if (check)
357 delete check;
358
359 return errs;
360 }
361 #endif
362
363 /// A wrapper for Comm::SetSelect() notifications.
364 void
365 Security::PeerConnector::NegotiateSsl(int, void *data)
366 {
367 PeerConnector *pc = static_cast<Security::PeerConnector *>(data);
368 // Use job calls to add done() checks and other job logic/protections.
369 CallJobHere(83, 7, pc, Security::PeerConnector, negotiate);
370 }
371
372 void
373 Security::PeerConnector::handleNegotiateError(const int ret)
374 {
375 const int fd = serverConnection()->fd;
376 const Security::SessionPointer session(fd_table[fd].ssl);
377 unsigned long ssl_lib_error = ret;
378
379 #if USE_OPENSSL
380 const int ssl_error = SSL_get_error(session.get(), ret);
381
382 switch (ssl_error) {
383 case SSL_ERROR_WANT_READ:
384 noteWantRead();
385 return;
386
387 case SSL_ERROR_WANT_WRITE:
388 noteWantWrite();
389 return;
390
391 case SSL_ERROR_SSL:
392 case SSL_ERROR_SYSCALL:
393 ssl_lib_error = ERR_get_error();
394 // proceed to the general error handling code
395 break;
396 default:
397 // no special error handling for all other errors
398 ssl_lib_error = SSL_ERROR_NONE;
399 break;
400 }
401
402 #elif USE_GNUTLS
403 const int ssl_error = ret;
404
405 switch (ret) {
406 case GNUTLS_E_WARNING_ALERT_RECEIVED: {
407 auto alert = gnutls_alert_get(session.get());
408 debugs(83, DBG_IMPORTANT, "TLS ALERT: " << gnutls_alert_get_name(alert));
409 }
410 // drop through to next case
411
412 case GNUTLS_E_AGAIN:
413 case GNUTLS_E_INTERRUPTED:
414 if (gnutls_record_get_direction(session.get()) == 0)
415 noteWantWrite();
416 else
417 noteWantRead();
418 return;
419
420 default:
421 // no special error handling for all other errors
422 break;
423 }
424
425 #else
426 // this avoids unused variable compiler warnings.
427 Must(!session);
428 const int ssl_error = ret;
429 #endif
430
431 // Log connection details, if any
432 recordNegotiationDetails();
433 noteNegotiationError(ret, ssl_error, ssl_lib_error);
434 }
435
436 void
437 Security::PeerConnector::noteWantRead()
438 {
439 const int fd = serverConnection()->fd;
440 debugs(83, 5, "FD " << fd);
441 #if USE_OPENSSL
442 Security::SessionPointer session(fd_table[fd].ssl);
443 BIO *b = SSL_get_rbio(session.get());
444 Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(b->ptr);
445 if (srvBio->holdRead()) {
446 if (srvBio->gotHello()) {
447 if (checkForMissingCertificates())
448 return; // Wait to download certificates before proceed.
449
450 srvBio->holdRead(false);
451 // schedule a negotiateSSl to allow openSSL parse received data
452 Security::PeerConnector::NegotiateSsl(fd, this);
453 return;
454 } else if (srvBio->gotHelloFailed()) {
455 srvBio->holdRead(false);
456 debugs(83, DBG_IMPORTANT, "Error parsing SSL Server Hello Message on FD " << fd);
457 // schedule a negotiateSSl to allow openSSL parse received data
458 Security::PeerConnector::NegotiateSsl(fd, this);
459 return;
460 }
461 }
462 #endif
463 setReadTimeout();
464 Comm::SetSelect(fd, COMM_SELECT_READ, &NegotiateSsl, this, 0);
465 }
466
467 void
468 Security::PeerConnector::noteWantWrite()
469 {
470 const int fd = serverConnection()->fd;
471 debugs(83, 5, "FD " << fd);
472 Comm::SetSelect(fd, COMM_SELECT_WRITE, &NegotiateSsl, this, 0);
473 return;
474 }
475
476 void
477 Security::PeerConnector::noteNegotiationError(const int ret, const int ssl_error, const int ssl_lib_error)
478 {
479 #if defined(EPROTO)
480 int sysErrNo = EPROTO;
481 #else
482 int sysErrNo = EACCES;
483 #endif
484
485 #if USE_OPENSSL
486 // store/report errno when ssl_error is SSL_ERROR_SYSCALL, ssl_lib_error is 0, and ret is -1
487 if (ssl_error == SSL_ERROR_SYSCALL && ret == -1 && ssl_lib_error == 0)
488 sysErrNo = errno;
489 #endif
490 int xerr = errno;
491
492 const int fd = serverConnection()->fd;
493 debugs(83, DBG_IMPORTANT, "ERROR: negotiating TLS on FD " << fd <<
494 ": " << Security::ErrorString(ssl_lib_error) << " (" <<
495 ssl_error << "/" << ret << "/" << xerr << ")");
496
497 ErrorState *anErr = NULL;
498 if (request != NULL)
499 anErr = ErrorState::NewForwarding(ERR_SECURE_CONNECT_FAIL, request.getRaw());
500 else
501 anErr = new ErrorState(ERR_SECURE_CONNECT_FAIL, Http::scServiceUnavailable, NULL);
502 anErr->xerrno = sysErrNo;
503
504 #if USE_OPENSSL
505 Security::SessionPointer session(fd_table[fd].ssl);
506 Ssl::ErrorDetail *errFromFailure = static_cast<Ssl::ErrorDetail *>(SSL_get_ex_data(session.get(), ssl_ex_index_ssl_error_detail));
507 if (errFromFailure != NULL) {
508 // The errFromFailure is attached to the ssl object
509 // and will be released when ssl object destroyed.
510 // Copy errFromFailure to a new Ssl::ErrorDetail object
511 anErr->detail = new Ssl::ErrorDetail(*errFromFailure);
512 } else {
513 // server_cert can be NULL here
514 X509 *server_cert = SSL_get_peer_certificate(session.get());
515 anErr->detail = new Ssl::ErrorDetail(SQUID_ERR_SSL_HANDSHAKE, server_cert, NULL);
516 X509_free(server_cert);
517 }
518
519 if (ssl_lib_error != SSL_ERROR_NONE)
520 anErr->detail->setLibError(ssl_lib_error);
521 #endif
522
523 noteNegotiationDone(anErr);
524 bail(anErr);
525 }
526
527 void
528 Security::PeerConnector::bail(ErrorState *error)
529 {
530 Must(error); // or the recepient will not know there was a problem
531 Must(callback != NULL);
532 CbDialer *dialer = dynamic_cast<CbDialer*>(callback->getDialer());
533 Must(dialer);
534 dialer->answer().error = error;
535
536 callBack();
537 // Our job is done. The callabck recepient will probably close the failed
538 // peer connection and try another peer or go direct (if possible). We
539 // can close the connection ourselves (our error notification would reach
540 // the recepient before the fd-closure notification), but we would rather
541 // minimize the number of fd-closure notifications and let the recepient
542 // manage the TCP state of the connection.
543
544 #if USE_GNUTLS
545 // but we do need to release the bad TLS related details in fd_table
546 // ... or GnuTLS will SEGFAULT.
547 const int fd = serverConnection()->fd;
548 Security::SessionClose(fd_table[fd].ssl, fd);
549 #endif
550 }
551
552 void
553 Security::PeerConnector::callBack()
554 {
555 debugs(83, 5, "TLS setup ended for " << serverConnection());
556
557 AsyncCall::Pointer cb = callback;
558 // Do this now so that if we throw below, swanSong() assert that we _tried_
559 // to call back holds.
560 callback = NULL; // this should make done() true
561
562 // remove close handler
563 comm_remove_close_handler(serverConnection()->fd, closeHandler);
564
565 CbDialer *dialer = dynamic_cast<CbDialer*>(cb->getDialer());
566 Must(dialer);
567 dialer->answer().conn = serverConnection();
568 ScheduleCallHere(cb);
569 }
570
571 void
572 Security::PeerConnector::swanSong()
573 {
574 // XXX: unregister fd-closure monitoring and CommSetSelect interest, if any
575 AsyncJob::swanSong();
576 if (callback != NULL) { // paranoid: we have left the caller waiting
577 debugs(83, DBG_IMPORTANT, "BUG: Unexpected state while connecting to a cache_peer or origin server");
578 ErrorState *anErr = new ErrorState(ERR_GATEWAY_FAILURE, Http::scInternalServerError, request.getRaw());
579 bail(anErr);
580 assert(!callback);
581 return;
582 }
583 }
584
585 const char *
586 Security::PeerConnector::status() const
587 {
588 static MemBuf buf;
589 buf.reset();
590
591 // TODO: redesign AsyncJob::status() API to avoid this
592 // id and stop reason reporting duplication.
593 buf.append(" [", 2);
594 if (stopReason != NULL) {
595 buf.append("Stopped, reason:", 16);
596 buf.appendf("%s",stopReason);
597 }
598 if (serverConn != NULL)
599 buf.appendf(" FD %d", serverConn->fd);
600 buf.appendf(" %s%u]", id.prefix(), id.value);
601 buf.terminate();
602
603 return buf.content();
604 }
605
606 #if USE_OPENSSL
607 /// CallDialer to allow use Downloader objects within PeerConnector class.
608 class PeerConnectorCertDownloaderDialer: public Downloader::CbDialer
609 {
610 public:
611 typedef void (Security::PeerConnector::*Method)(SBuf &object, int status);
612
613 PeerConnectorCertDownloaderDialer(Method method, Security::PeerConnector *pc):
614 method_(method),
615 peerConnector_(pc) {}
616
617 /* CallDialer API */
618 virtual bool canDial(AsyncCall &call) { return peerConnector_.valid(); }
619 virtual void dial(AsyncCall &call) { ((&(*peerConnector_))->*method_)(object, status); }
620 Method method_; ///< The Security::PeerConnector method to dial
621 CbcPointer<Security::PeerConnector> peerConnector_; ///< The Security::PeerConnector object
622 };
623
624 void
625 Security::PeerConnector::startCertDownloading(SBuf &url)
626 {
627 AsyncCall::Pointer certCallback = asyncCall(81, 4,
628 "Security::PeerConnector::certDownloadingDone",
629 PeerConnectorCertDownloaderDialer(&Security::PeerConnector::certDownloadingDone, this));
630
631 const Downloader *csd = (request ? dynamic_cast<const Downloader*>(request->downloader.valid()) : nullptr);
632 Downloader *dl = new Downloader(url, certCallback, csd ? csd->nestedLevel() + 1 : 1);
633 AsyncJob::Start(dl);
634 }
635
636 void
637 Security::PeerConnector::certDownloadingDone(SBuf &obj, int downloadStatus)
638 {
639 ++certsDownloads;
640 debugs(81, 5, "Certificate downloading status: " << downloadStatus << " certificate size: " << obj.length());
641
642 // get ServerBio from SSL object
643 const int fd = serverConnection()->fd;
644 Security::SessionPointer session(fd_table[fd].ssl);
645 BIO *b = SSL_get_rbio(session.get());
646 Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(b->ptr);
647
648 // Parse Certificate. Assume that it is in DER format.
649 // According to RFC 4325:
650 // The server must provide a DER encoded certificate or a collection
651 // collection of certificates in a "certs-only" CMS message.
652 // The applications MUST accept DER encoded certificates and SHOULD
653 // be able to accept collection of certificates.
654 // TODO: support collection of certificates
655 const unsigned char *raw = (const unsigned char*)obj.rawContent();
656 if (X509 *cert = d2i_X509(NULL, &raw, obj.length())) {
657 char buffer[1024];
658 debugs(81, 5, "Retrieved certificate: " << X509_NAME_oneline(X509_get_subject_name(cert), buffer, 1024));
659 const Security::CertList &certsList = srvBio->serverCertificatesIfAny();
660 if (const char *issuerUri = Ssl::uriOfIssuerIfMissing(cert, certsList)) {
661 urlsOfMissingCerts.push(SBuf(issuerUri));
662 }
663 Ssl::SSL_add_untrusted_cert(session.get(), cert);
664 }
665
666 // Check if there are URIs to download from and if yes start downloading
667 // the first in queue.
668 if (urlsOfMissingCerts.size() && certsDownloads <= MaxCertsDownloads) {
669 startCertDownloading(urlsOfMissingCerts.front());
670 urlsOfMissingCerts.pop();
671 return;
672 }
673
674 srvBio->holdRead(false);
675 Security::PeerConnector::NegotiateSsl(serverConnection()->fd, this);
676 }
677
678 bool
679 Security::PeerConnector::checkForMissingCertificates()
680 {
681 // Check for nested SSL certificates downloads. For example when the
682 // certificate located in an SSL site which requires to download a
683 // a missing certificate (... from an SSL site which requires to ...).
684
685 const Downloader *csd = (request ? request->downloader.get() : nullptr);
686 if (csd && csd->nestedLevel() >= MaxNestedDownloads)
687 return false;
688
689 const int fd = serverConnection()->fd;
690 Security::SessionPointer session(fd_table[fd].ssl);
691 BIO *b = SSL_get_rbio(session.get());
692 Ssl::ServerBio *srvBio = static_cast<Ssl::ServerBio *>(b->ptr);
693 const Security::CertList &certs = srvBio->serverCertificatesIfAny();
694
695 if (certs.size()) {
696 debugs(83, 5, "SSL server sent " << certs.size() << " certificates");
697 Ssl::missingChainCertificatesUrls(urlsOfMissingCerts, certs);
698 if (urlsOfMissingCerts.size()) {
699 startCertDownloading(urlsOfMissingCerts.front());
700 urlsOfMissingCerts.pop();
701 return true;
702 }
703 }
704
705 return false;
706 }
707 #endif //USE_OPENSSL
708