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 "compat/openssl.h"
15 #include "FadingCounter.h"
16 #include "fd.h"
17 #include "MemBuf.h"
18 #include "security/Handshake.h"
19 #include "ssl/support.h"
20 
21 #include <iosfwd>
22 #include <list>
23 #if HAVE_OPENSSL_BIO_H
24 #include <openssl/bio.h>
25 #endif
26 #include <string>
27 #include <type_traits>
28 
29 namespace Ssl
30 {
31 
33 class Bio
34 {
35 public:
36  explicit Bio(const int anFd);
37  virtual ~Bio();
38 
40  virtual int write(const char *buf, int size, BIO *table);
41 
43  virtual int read(char *buf, int size, BIO *table);
44 
47  virtual void flush(BIO *table) {}
48 
49  int fd() const { return fd_; }
50 
53  virtual void stateChanged(const SSL *ssl, int where, int ret);
54 
57  static BIO *Create(const int fd, Security::Io::Type type);
59  static void Link(SSL *ssl, BIO *bio);
60 
61  const SBuf &rBufData() {return rbuf;}
62 protected:
63  const int fd_;
65 };
66 
70 class ClientBio: public Bio
71 {
72 public:
73  explicit ClientBio(const int anFd);
74 
78  virtual void stateChanged(const SSL *ssl, int where, int ret);
80  virtual int write(const char *buf, int size, BIO *table);
84  virtual int read(char *buf, int size, BIO *table);
86  void hold(bool h) {holdRead_ = holdWrite_ = h;}
87 
92 private:
94  static const time_t RenegotiationsWindow = 10;
95 
97  static const int RenegotiationsLimit = 5;
98 
99  bool holdRead_;
100  bool holdWrite_;
101  int helloSize;
103 
105  const char *abortReason;
106 };
107 
123 class ServerBio: public Bio
124 {
125 public:
126  explicit ServerBio(const int anFd);
127 
129  virtual void stateChanged(const SSL *ssl, int where, int ret);
134  virtual int write(const char *buf, int size, BIO *table);
137  virtual int read(char *buf, int size, BIO *table);
140  virtual void flush(BIO *table);
142  void setClientFeatures(Security::TlsDetails::Pointer const &details, SBuf const &hello);
143 
144  bool resumingSession();
145 
147  bool holdWrite() const {return holdWrite_;}
149  void holdWrite(bool h) {holdWrite_ = h;}
151  bool holdRead() const {return holdRead_;}
153  void holdRead(bool h) {holdRead_ = h;}
155  void recordInput(bool r) {record_ = r;}
157  bool canSplice() {return allowSplice;}
159  bool canBump() {return allowBump;}
161  void mode(Ssl::BumpMode m) {bumpMode_ = m;}
163 
165  bool gotHello() const { return (parsedHandshake && !parseError); }
166 
168  bool gotHelloFailed() const { return (parsedHandshake && parseError); }
169 
172 
175 
176 private:
177  int readAndGive(char *buf, const int size, BIO *table);
178  int readAndParse(char *buf, const int size, BIO *table);
179  int readAndBuffer(BIO *table);
180  int giveBuffered(char *buf, const int size);
181 
188  bool helloBuild;
189  bool allowSplice;
190  bool allowBump;
191  bool holdWrite_;
192  bool holdRead_;
193  bool record_;
195  bool parseError;
197 
201 };
202 
203 } // namespace Ssl
204 
205 void
207 
208 #endif /* USE_OPENSSL */
209 #endif /* SQUID_SSL_BIO_H */
210 
static BIO * Create(const int fd, Security::Io::Type type)
Definition: bio.cc:62
Incremental TLS/SSL Handshake parser.
Definition: Handshake.h:56
Ssl::BumpMode bumpMode_
Definition: bio.h:196
const SBuf & rBufData()
The buffered input data.
Definition: bio.h:61
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:183
static void Link(SSL *ssl, BIO *bio)
Tells ssl connection to use BIO and monitor state via stateChanged()
Definition: bio.cc:88
virtual void flush(BIO *table)
Definition: bio.h:47
virtual int write(const char *buf, int size, BIO *table)
The ClientBio version of the Ssl::Bio::write method.
Definition: bio.cc:199
bool canSplice()
Whether we can splice or not the SSL stream.
Definition: bio.h:157
std::list< Security::CertPointer > CertList
Definition: forward.h:80
virtual int write(const char *buf, int size, BIO *table)
Writes the given data to socket.
Definition: bio.cc:104
const char * bumpMode(int bm)
Definition: support.h:145
const int fd_
the SSL socket we are reading and writing
Definition: bio.h:63
void recordInput(bool r)
Enables or disables the input data recording, for internal analysis.
Definition: bio.h:155
const Security::TlsDetails::Pointer & receivedHelloDetails() const
Definition: bio.h:174
bool holdWrite_
The write hold state of the bio.
Definition: bio.h:191
bool holdRead_
The read hold state of the bio.
Definition: bio.h:192
bool holdWrite_
The write hold state of the bio.
Definition: bio.h:100
Ssl::BumpMode bumpMode()
return the bumping mode
Definition: bio.h:162
void mode(Ssl::BumpMode m)
The bumping mode.
Definition: bio.h:161
void setClientFeatures(Security::TlsDetails::Pointer const &details, SBuf const &hello)
Sets the random number to use in client SSL HELLO message.
Definition: bio.cc:264
size_t rbufConsumePos
The size of data stored in rbuf which passed to the openSSL.
Definition: bio.h:199
SBuf helloMsg
Used to buffer output data.
Definition: bio.h:186
bool helloBuild
True if the client hello message sent to the server.
Definition: bio.h:188
virtual ~Bio()
Definition: bio.cc:99
bool holdRead() const
The read hold state.
Definition: bio.h:151
virtual int write(const char *buf, int size, BIO *table)
Definition: bio.cc:467
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:94
bool holdRead_
The read hold state of the bio.
Definition: bio.h:99
int helloSize
The SSL hello message sent by client size.
Definition: bio.h:101
Security::CertList serverCertificates
parsed certificates chain
Definition: Handshake.h:72
Security::HandshakeParser parser_
The TLS/SSL messages parser.
Definition: bio.h:200
virtual int read(char *buf, int size, BIO *table)
Definition: bio.cc:271
int giveBuffered(char *buf, const int size)
Definition: bio.cc:347
Counts events, forgetting old ones. Usefull for &quot;3 errors/minute&quot; limits.
Definition: FadingCounter.h:15
void holdWrite(bool h)
Enables or disables the write hold state.
Definition: bio.h:149
bool gotHello() const
Definition: bio.h:165
bool parseError
error while parsing server hello message
Definition: bio.h:195
BIO source and sink node, handling socket I/O and monitoring SSL state.
Definition: bio.h:33
static const int RenegotiationsLimit
the maximum tolerated number of client-initiated renegotiations in RenegotiationsWindow ...
Definition: bio.h:97
void setReadBufData(SBuf &data)
Definition: bio.h:91
virtual int read(char *buf, int size, BIO *table)
Reads data from socket.
Definition: bio.cc:127
bool parsedHandshake
whether we are done parsing TLS Hello
Definition: bio.h:194
ClientBio(const int anFd)
Definition: bio.cc:167
bool holdWrite() const
The write hold state.
Definition: bio.h:147
void const char * buf
Definition: stub_helper.cc:16
bool allowSplice
True if the SSL stream can be spliced.
Definition: bio.h:189
void applyTlsDetailsToSSL(SSL *ssl, Security::TlsDetails::Pointer const &details, Ssl::BumpMode bumpMode)
Definition: bio.cc:682
bool resumingSession()
Definition: bio.cc:552
void holdRead(bool h)
Enables or disables the read hold state.
Definition: bio.h:153
bool canBump()
Whether we can bump or not the SSL stream.
Definition: bio.h:159
SBuf rbuf
Used to buffer input data.
Definition: bio.h:64
TlsDetails::Pointer details
TLS handshake meta info or nil.
Definition: Handshake.h:70
FadingCounter renegotiations
client requested renegotiations limit control
Definition: bio.h:102
virtual void stateChanged(const SSL *ssl, int where, int ret)
Definition: bio.cc:152
const Security::CertList & serverCertificatesIfAny()
Definition: bio.h:171
virtual void stateChanged(const SSL *ssl, int where, int ret)
Definition: bio.cc:178
SBuf clientSentHello
TLS client hello message, used to adapt our tls Hello message to the server.
Definition: bio.h:185
bool record_
If true the input data recorded to rbuf for internal use.
Definition: bio.h:193
mb_size_t helloMsgSize
Definition: bio.h:187
virtual void flush(BIO *table)
Definition: bio.cc:543
void hold(bool h)
Prevents or allow writting on socket.
Definition: bio.h:86
int fd() const
The SSL socket descriptor.
Definition: bio.h:49
virtual int read(char *buf, int size, BIO *table)
Definition: bio.cc:216
Bio(const int anFd)
Definition: bio.cc:94
ssize_t mb_size_t
Definition: MemBuf.h:17
bool gotHelloFailed() const
Return true if the Server Hello parsing failed.
Definition: bio.h:168
ServerBio(const int anFd)
Definition: bio.cc:241
BumpMode
Definition: support.h:131
int readAndBuffer(BIO *table)
Definition: bio.cc:333
int readAndGive(char *buf, const int size, BIO *table)
Read and give everything to OpenSSL.
Definition: bio.cc:281
int readAndParse(char *buf, const int size, BIO *table)
Definition: bio.cc:301
const char * abortReason
why we should terminate the connection during next TLS operation (or nil)
Definition: bio.h:105
virtual void stateChanged(const SSL *ssl, int where, int ret)
The ServerBio version of the Ssl::Bio::stateChanged method.
Definition: bio.cc:258
bool allowBump
True if the SSL stream can be bumped.
Definition: bio.h:190
int size
Definition: ModDevPoll.cc:77

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors