openssl.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2003 Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 
30 #ifndef TNT_OPENSSL_H
31 #define TNT_OPENSSL_H
32 
33 #include <cxxtools/net/tcpstream.h>
34 #include <cxxtools/smartptr.h>
35 #include <openssl/ssl.h>
36 
38 namespace tnt
39 {
40  class OpensslException : public std::runtime_error
41  {
42  unsigned long _code;
43 
44  public:
45  OpensslException(const std::string& what, unsigned long code)
46  : std::runtime_error(what),
47  _code(code)
48  { }
49 
50  unsigned long getCode() const { return _code; }
51  };
52 
53  // destroy policy for smart pointer
54  template <typename ctx>
55  class SslCtxReleaser;
56 
57  template <>
58  class SslCtxReleaser<SSL_CTX>
59  {
60  protected:
61  void destroy(SSL_CTX* ctx);
62  };
63 
65 
66  class OpensslServer : public cxxtools::net::TcpServer
67  {
68  SslCtxPtr _ctx;
69  void setOptions();
70  void installCertificates(const char* certificateFile, const char* privateKeyFile);
71 
72  public:
73  explicit OpensslServer(const char* certificateFile);
74  OpensslServer(const char* certificateFile, const char* privateKeyFile);
75 
76  SslCtxPtr getSslContext() const { return _ctx; }
77  };
78 
79  class OpensslStream : public cxxtools::net::TcpSocket
80  {
81  SslCtxPtr _ctx;
82  SSL* _ssl;
83 
84  public:
85  OpensslStream();
86 
87  explicit OpensslStream(const OpensslServer& server, bool inherit = false);
88  ~OpensslStream();
89 
90  void accept(const OpensslServer& server, bool inherit = false);
91  void handshake(const OpensslServer& server);
92 
93  int sslRead(char* buffer, int bufsize) const;
94  int sslWrite(const char* buffer, int bufsize) const;
95  void shutdown() const;
96  };
97 
98  class openssl_streambuf : public std::streambuf
99  {
100  OpensslStream& _stream;
101  char_type* _buffer;
102  unsigned _bufsize;
103 
104  public:
105  explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 8192, int timeout = -1);
106  ~openssl_streambuf() { delete[] _buffer; }
107 
108  void setTimeout(int t) { _stream.setTimeout(t); }
109  int getTimeout() const { return _stream.getTimeout(); }
110 
112  int_type overflow(int_type c);
114  int_type underflow();
116  int sync();
117  };
118 
119  class openssl_iostream : public OpensslStream, public std::iostream
120  {
121  openssl_streambuf _buffer;
122 
123  public:
124  explicit openssl_iostream(unsigned bufsize = 8192, int timeout = -1)
125  : std::iostream(0),
126  _buffer(*this, bufsize, timeout)
127  { init(&_buffer); }
128 
129  explicit openssl_iostream(const OpensslServer& server, unsigned bufsize = 8192, int timeout = -1)
130  : OpensslStream(server),
131  std::iostream(0),
132  _buffer(*this, bufsize, timeout)
133  { init(&_buffer); }
134 
135  void setTimeout(int timeout) { _buffer.setTimeout(timeout); }
136  int getTimeout() const { return _buffer.getTimeout(); }
137  };
138 }
140 
141 #endif // TNT_OPENSSL_H
142