]> git.ipfire.org Git - thirdparty/squid.git/blame - src/ssl/PeerConnector.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / ssl / PeerConnector.h
CommitLineData
a23223bf 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
a23223bf 3 *
bbc27441
AJ
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.
a23223bf 7 */
bbc27441 8
a23223bf
CT
9#ifndef SQUID_SSL_PEER_CONNECTOR_H
10#define SQUID_SSL_PEER_CONNECTOR_H
11
20b79af2 12#include "acl/Acl.h"
a23223bf 13#include "base/AsyncCbdataCalls.h"
e2849af8 14#include "base/AsyncJob.h"
a23223bf
CT
15#include "ssl/support.h"
16#include <iosfwd>
17
18class HttpRequest;
19class ErrorState;
20
e2849af8
A
21namespace Ssl
22{
a23223bf
CT
23
24class ErrorDetail;
25class CertValidationResponse;
26
27/// PeerConnector results (supplied via a callback).
28/// The connection to peer was secured if and only if the error member is nil.
e2849af8
A
29class PeerConnectorAnswer
30{
a23223bf
CT
31public:
32 ~PeerConnectorAnswer(); ///< deletes error if it is still set
33 Comm::ConnectionPointer conn; ///< peer connection (secured on success)
34
35 /// answer recepients must clear the error member in order to keep its info
36 /// XXX: We should refcount ErrorState instead of cbdata-protecting it.
37 CbcPointer<ErrorState> error; ///< problem details (nil on success)
38};
39
40/**
41 \par
42 * Connects Squid client-side to an SSL peer (cache_peer ... ssl).
43 * Handles peer certificate validation.
44 * Used by TunnelStateData, FwdState, and PeerPoolMgr to start talking to an
45 * SSL peer.
46 \par
47 * The caller receives a call back with PeerConnectorAnswer. If answer.error
48 * is not nil, then there was an error and the SSL connection to the SSL peer
49 * was not fully established. The error object is suitable for error response
50 * generation.
51 \par
52 * The caller must monitor the connection for closure because this
53 * job will not inform the caller about such events.
54 \par
8aec3e1b
CT
55 * PeerConnector class curently supports a form of SSL negotiation timeout,
56 * which accounted only when sets the read timeout from SSL peer.
57 * For a complete solution, the caller must monitor the overall connection
58 * establishment timeout and close the connection on timeouts. This is probably
59 * better than having dedicated (or none at all!) timeouts for peer selection,
60 * DNS lookup, TCP handshake, SSL handshake, etc. Some steps may have their
719dc243 61 * own timeout, but not all steps should be forced to have theirs.
8aec3e1b
CT
62 * XXX: tunnel.cc and probably other subsystems does not have an "overall
63 * connection establishment" timeout. We need to change their code so that they
64 * start monitoring earlier and close on timeouts. This change may need to be
65 * discussed on squid-dev.
a23223bf
CT
66 \par
67 * This job never closes the connection, even on errors. If a 3rd-party
68 * closes the connection, this job simply quits without informing the caller.
e2849af8 69*/
a23223bf
CT
70class PeerConnector: virtual public AsyncJob
71{
72public:
73 /// Callback dialier API to allow PeerConnector to set the answer.
e2849af8
A
74 class CbDialer
75 {
a23223bf
CT
76 public:
77 virtual ~CbDialer() {}
78 /// gives PeerConnector access to the in-dialer answer
79 virtual PeerConnectorAnswer &answer() = 0;
80 };
81
82 typedef RefCount<HttpRequest> HttpRequestPointer;
83
84public:
85 PeerConnector(HttpRequestPointer &aRequest,
86 const Comm::ConnectionPointer &aServerConn,
93ead3fd 87 const Comm::ConnectionPointer &aClientConn,
8aec3e1b 88 AsyncCall::Pointer &aCallback, const time_t timeout = 0);
a23223bf
CT
89 virtual ~PeerConnector();
90
91protected:
92 // AsyncJob API
93 virtual void start();
94 virtual bool doneAll() const;
95 virtual void swanSong();
96 virtual const char *status() const;
97
98 /// The comm_close callback handler.
99 void commCloseHandler(const CommCloseCbParams &params);
100
101 /// Inform us that the connection is closed. Does the required clean-up.
102 void connectionClosed(const char *reason);
103
104 /// Sets up TCP socket-related notification callbacks if things go wrong.
105 /// If socket already closed return false, else install the comm_close
106 /// handler to monitor the socket.
107 bool prepareSocket();
108
8aec3e1b
CT
109 /// Sets the read timeout to avoid getting stuck while reading from a
110 /// silent server
719dc243 111 void setReadTimeout();
8aec3e1b 112
a23223bf
CT
113 void initializeSsl(); ///< Initializes SSL state
114
115 /// Performs a single secure connection negotiation step.
116 /// It is called multiple times untill the negotiation finish or aborted.
117 void negotiateSsl();
118
7f4e9b73 119 bool checkForPeekAndSplice(bool, Ssl::BumpMode);
31855516 120
a23223bf
CT
121 /// Called when the SSL negotiation step aborted because data needs to
122 /// be transferred to/from SSL server or on error. In the first case
123 /// setups the appropriate Comm::SetSelect handler. In second case
124 /// fill an error and report to the PeerConnector caller.
125 void handleNegotiateError(const int result);
126
127private:
128 PeerConnector(const PeerConnector &); // not implemented
129 PeerConnector &operator =(const PeerConnector &); // not implemented
130
131 /// mimics FwdState to minimize changes to FwdState::initiate/negotiateSsl
132 Comm::ConnectionPointer const &serverConnection() const { return serverConn; }
133
e2849af8 134 void bail(ErrorState *error); ///< Return an error to the PeerConnector caller
a23223bf
CT
135
136 /// Callback the caller class, and pass the ready to communicate secure
137 /// connection or an error if PeerConnector failed.
138 void callBack();
139
140 /// Process response from cert validator helper
141 void sslCrtvdHandleReply(Ssl::CertValidationResponse const &);
142
143 /// Check SSL errors returned from cert validator against sslproxy_cert_error access list
144 Ssl::CertErrors *sslCrtvdCheckForErrors(Ssl::CertValidationResponse const &, Ssl::ErrorDetail *&);
145
146 /// Callback function called when squid receive message from cert validator helper
147 static void sslCrtvdHandleReplyWrapper(void *data, Ssl::CertValidationResponse const &);
148
149 /// A wrapper function for negotiateSsl for use with Comm::SetSelect
150 static void NegotiateSsl(int fd, void *data);
151
7f4e9b73
CT
152 /// A wrapper function for checkForPeekAndSplice for use with acl
153 static void cbCheckForPeekAndSplice(allow_t answer, void *data);
154
a23223bf
CT
155 HttpRequestPointer request; ///< peer connection trigger or cause
156 Comm::ConnectionPointer serverConn; ///< TCP connection to the peer
93ead3fd 157 Comm::ConnectionPointer clientConn; ///< TCP connection to the client
a23223bf
CT
158 AsyncCall::Pointer callback; ///< we call this with the results
159 AsyncCall::Pointer closeHandler; ///< we call this when the connection closed
8aec3e1b
CT
160 time_t negotiationTimeout; ///< the ssl connection timeout to use
161 time_t startTime; ///< when the peer connector negotiation started
a23223bf
CT
162
163 CBDATA_CLASS2(PeerConnector);
164};
165
a23223bf
CT
166std::ostream &operator <<(std::ostream &os, const Ssl::PeerConnectorAnswer &a);
167
84321458
CT
168} // namespace Ssl
169
a23223bf 170#endif /* SQUID_PEER_CONNECTOR_H */