bio.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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 #ifndef SQUID_SSL_BIO_H
10 #define SQUID_SSL_BIO_H
11 
12 #if USE_OPENSSL
13 
14 #include "FadingCounter.h"
15 #include "fd.h"
16 #include "MemBuf.h"
17 #include "security/Handshake.h"
18 #include "ssl/support.h"
19 
20 #include <iosfwd>
21 #include <list>
22 #if HAVE_OPENSSL_BIO_H
23 #include <openssl/bio.h>
24 #endif
25 #include <string>
26 #include <type_traits>
27 
28 namespace Ssl
29 {
30 
32 class Bio
33 {
34 public:
35  explicit Bio(const int anFd);
36  virtual ~Bio();
37 
39  virtual int write(const char *buf, int size, BIO *table);
40 
42  virtual int read(char *buf, int size, BIO *table);
43 
46  virtual void flush(BIO *table) {}
47 
48  int fd() const { return fd_; }
49 
52  virtual void stateChanged(const SSL *ssl, int where, int ret);
53 
56  static BIO *Create(const int fd, Security::Io::Type type);
58  static void Link(SSL *ssl, BIO *bio);
59 
60  const SBuf &rBufData() {return rbuf;}
61 protected:
62  const int fd_;
64 };
65 
69 class ClientBio: public Bio
70 {
71 public:
72  explicit ClientBio(const int anFd);
73 
77  virtual void stateChanged(const SSL *ssl, int where, int ret);
79  virtual int write(const char *buf, int size, BIO *table);
83  virtual int read(char *buf, int size, BIO *table);
85  void hold(bool h) {holdRead_ = holdWrite_ = h;}
86 
91 private:
93  static const time_t RenegotiationsWindow = 10;
94 
96  static const int RenegotiationsLimit = 5;
97 
98  bool holdRead_;
99  bool holdWrite_;
100  int helloSize;
102 
104  const char *abortReason;
105 };
106 
122 class ServerBio: public Bio
123 {
124 public:
125  explicit ServerBio(const int anFd);
126 
128  virtual void stateChanged(const SSL *ssl, int where, int ret);
133  virtual int write(const char *buf, int size, BIO *table);
136  virtual int read(char *buf, int size, BIO *table);
139  virtual void flush(BIO *table);
141  void setClientFeatures(Security::TlsDetails::Pointer const &details, SBuf const &hello);
142 
143  bool resumingSession();
144 
146  bool holdWrite() const {return holdWrite_;}
148  void holdWrite(bool h) {holdWrite_ = h;}
150  bool holdRead() const {return holdRead_;}
152  void holdRead(bool h) {holdRead_ = h;}
154  void recordInput(bool r) {record_ = r;}
156  bool canSplice() {return allowSplice;}
158  bool canBump() {return allowBump;}
160  void mode(Ssl::BumpMode m) {bumpMode_ = m;}
162 
164  bool gotHello() const { return (parsedHandshake && !parseError); }
165 
167  bool gotHelloFailed() const { return (parsedHandshake && parseError); }
168 
171 
174 
175 private:
176  int readAndGive(char *buf, const int size, BIO *table);
177  int readAndParse(char *buf, const int size, BIO *table);
178  int readAndBuffer(BIO *table);
179  int giveBuffered(char *buf, const int size);
180 
187  bool helloBuild;
188  bool allowSplice;
189  bool allowBump;
190  bool holdWrite_;
191  bool holdRead_;
192  bool record_;
194  bool parseError;
196 
200 };
201 
202 } // namespace Ssl
203 
204 void
206 
207 #if !HAVE_LIBCRYPTO_BIO_GET_INIT
208 // OpenSSL v1.0 bio compatibility functions
209 inline void *BIO_get_data(BIO *table) { return table->ptr; }
210 inline void BIO_set_data(BIO *table, void *data) { table->ptr = data; }
211 inline int BIO_get_init(BIO *table) { return table->init; }
212 inline void BIO_set_init(BIO *table, int init) { table->init = init; }
213 #endif
214 
215 #endif /* USE_OPENSSL */
216 #endif /* SQUID_SSL_BIO_H */
217 
static BIO * Create(const int fd, Security::Io::Type type)
Definition: bio.cc:66
Incremental TLS/SSL Handshake parser.
Definition: Handshake.h:56
Ssl::BumpMode bumpMode_
Definition: bio.h:195
const SBuf & rBufData()
The buffered input data.
Definition: bio.h:60
int type
Definition: errorpage.cc:78
Definition: SBuf.h:86
Security::TlsDetails::Pointer clientTlsDetails
SSL client features extracted from ClientHello message or SSL object.
Definition: bio.h:182
static void Link(SSL *ssl, BIO *bio)
Tells ssl connection to use BIO and monitor state via stateChanged()
Definition: bio.cc:92
virtual void flush(BIO *table)
Definition: bio.h:46
virtual int write(const char *buf, int size, BIO *table)
The ClientBio version of the Ssl::Bio::write method.
Definition: bio.cc:203
bool canSplice()
Whether we can splice or not the SSL stream.
Definition: bio.h:156
virtual int write(const char *buf, int size, BIO *table)
Writes the given data to socket.
Definition: bio.cc:108
const char * bumpMode(int bm)
Definition: support.h:144
const int fd_
the SSL socket we are reading and writing
Definition: bio.h:62
void recordInput(bool r)
Enables or disables the input data recording, for internal analysis.
Definition: bio.h:154
const Security::TlsDetails::Pointer & receivedHelloDetails() const
Definition: bio.h:173
bool holdWrite_
The write hold state of the bio.
Definition: bio.h:190
bool holdRead_
The read hold state of the bio.
Definition: bio.h:191
bool holdWrite_
The write hold state of the bio.
Definition: bio.h:99
Ssl::BumpMode bumpMode()
return the bumping mode
Definition: bio.h:161
void mode(Ssl::BumpMode m)
The bumping mode.
Definition: bio.h:160
void setClientFeatures(Security::TlsDetails::Pointer const &details, SBuf const &hello)
Sets the random number to use in client SSL HELLO message.
Definition: bio.cc:268
size_t rbufConsumePos
The size of data stored in rbuf which passed to the openSSL.
Definition: bio.h:198
void BIO_set_init(BIO *table, int init)
Definition: bio.h:212
SBuf helloMsg
Used to buffer output data.
Definition: bio.h:185
bool helloBuild
True if the client hello message sent to the server.
Definition: bio.h:187
virtual ~Bio()
Definition: bio.cc:103
CtoCpp1(X509_free, X509 *) typedef Security CtoCpp1(X509_CRL_free, X509_CRL *) typedef Security typedef std::list< Security::CertPointer CertList)
Definition: forward.h:95
bool holdRead() const
The read hold state.
Definition: bio.h:150
virtual int write(const char *buf, int size, BIO *table)
Definition: bio.cc:471
void const char HLPCB void * data
Definition: stub_helper.cc:16
static const time_t RenegotiationsWindow
approximate size of a time window for computing client-initiated renegotiation rate (in seconds) ...
Definition: bio.h:93
bool holdRead_
The read hold state of the bio.
Definition: bio.h:98
int helloSize
The SSL hello message sent by client size.
Definition: bio.h:100
Security::CertList serverCertificates
parsed certificates chain
Definition: Handshake.h:72
Security::HandshakeParser parser_
The TLS/SSL messages parser.
Definition: bio.h:199
virtual int read(char *buf, int size, BIO *table)
Definition: bio.cc:275
int giveBuffered(char *buf, const int size)
Definition: bio.cc:351
Counts events, forgetting old ones. Usefull for "3 errors/minute" limits.
Definition: FadingCounter.h:15
int BIO_get_init(BIO *table)
Definition: bio.h:211
void holdWrite(bool h)
Enables or disables the write hold state.
Definition: bio.h:148
bool gotHello() const
Definition: bio.h:164
bool parseError
error while parsing server hello message
Definition: bio.h:194
BIO source and sink node, handling socket I/O and monitoring SSL state.
Definition: bio.h:32
static const int RenegotiationsLimit
the maximum tolerated number of client-initiated renegotiations in RenegotiationsWindow ...
Definition: bio.h:96
void setReadBufData(SBuf &data)
Definition: bio.h:90
virtual int read(char *buf, int size, BIO *table)
Reads data from socket.
Definition: bio.cc:131
bool parsedHandshake
whether we are done parsing TLS Hello
Definition: bio.h:193
ClientBio(const int anFd)
Definition: bio.cc:171
bool holdWrite() const
The write hold state.
Definition: bio.h:146
void const char * buf
Definition: stub_helper.cc:16
bool allowSplice
True if the SSL stream can be spliced.
Definition: bio.h:188
bool resumingSession()
Definition: bio.cc:556
void BIO_set_data(BIO *table, void *data)
Definition: bio.h:210
void holdRead(bool h)
Enables or disables the read hold state.
Definition: bio.h:152
bool canBump()
Whether we can bump or not the SSL stream.
Definition: bio.h:158
SBuf rbuf
Used to buffer input data.
Definition: bio.h:63
TlsDetails::Pointer details
TLS handshake meta info or nil.
Definition: Handshake.h:70
FadingCounter renegotiations
client requested renegotiations limit control
Definition: bio.h:101
virtual void stateChanged(const SSL *ssl, int where, int ret)
Definition: bio.cc:156
const Security::CertList & serverCertificatesIfAny()
Definition: bio.h:170
virtual void stateChanged(const SSL *ssl, int where, int ret)
Definition: bio.cc:182
SBuf clientSentHello
TLS client hello message, used to adapt our tls Hello message to the server.
Definition: bio.h:184
bool record_
If true the input data recorded to rbuf for internal use.
Definition: bio.h:192
mb_size_t helloMsgSize
Definition: bio.h:186
virtual void flush(BIO *table)
Definition: bio.cc:547
void applyTlsDetailsToSSL(SSL *ssl, Security::TlsDetails::Pointer const &details, Ssl::BumpMode bumpMode)
Definition: bio.cc:686
void hold(bool h)
Prevents or allow writting on socket.
Definition: bio.h:85
int fd() const
The SSL socket descriptor.
Definition: bio.h:48
virtual int read(char *buf, int size, BIO *table)
Definition: bio.cc:220
Bio(const int anFd)
Definition: bio.cc:98
ssize_t mb_size_t
Definition: MemBuf.h:17
bool gotHelloFailed() const
Return true if the Server Hello parsing failed.
Definition: bio.h:167
ServerBio(const int anFd)
Definition: bio.cc:245
void * BIO_get_data(BIO *table)
Definition: bio.h:209
BumpMode
Definition: support.h:130
int readAndBuffer(BIO *table)
Definition: bio.cc:337
int readAndGive(char *buf, const int size, BIO *table)
Read and give everything to OpenSSL.
Definition: bio.cc:285
int readAndParse(char *buf, const int size, BIO *table)
Definition: bio.cc:305
const char * abortReason
why we should terminate the connection during next TLS operation (or nil)
Definition: bio.h:104
virtual void stateChanged(const SSL *ssl, int where, int ret)
The ServerBio version of the Ssl::Bio::stateChanged method.
Definition: bio.cc:262
bool allowBump
True if the SSL stream can be bumped.
Definition: bio.h:189
int size
Definition: ModDevPoll.cc:77

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors