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 
37 namespace tnt
38 {
39  class OpensslException : public std::runtime_error
40  {
41  unsigned long code;
42 
43  public:
44  OpensslException(const std::string& what, unsigned long code_)
45  : std::runtime_error(what),
46  code(code_)
47  { }
48 
49  unsigned long getCode() const
50  { 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 
67  {
68  public:
69 
70  private:
71  SslCtxPtr ctx;
72  void installCertificates(const char* certificateFile, const char* privateKeyFile);
73 
74  public:
75  explicit OpensslServer(const char* certificateFile);
76  OpensslServer(const char* certificateFile, const char* privateKeyFile);
77 
78  SslCtxPtr getSslContext() const { return ctx; }
79  };
80 
82  {
83  SslCtxPtr ctx;
84  SSL* ssl;
85 
86  public:
87  OpensslStream();
88 
89  explicit OpensslStream(const OpensslServer& server, bool inherit = false);
91 
92  void accept(const OpensslServer& server, bool inherit = false);
93  void handshake(const OpensslServer& server);
94 
95  int sslRead(char* buffer, int bufsize) const;
96  int sslWrite(const char* buffer, int bufsize) const;
97  void shutdown() const;
98  };
99 
100  class openssl_streambuf : public std::streambuf
101  {
102  OpensslStream& m_stream;
103  char_type* m_buffer;
104  unsigned m_bufsize;
105 
106  public:
107  explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 8192, int timeout = -1);
109  { delete[] m_buffer; }
110 
111  void setTimeout(int t) { m_stream.setTimeout(t); }
112  int getTimeout() const { return m_stream.getTimeout(); }
113 
115  int_type overflow(int_type c);
117  int_type underflow();
119  int sync();
120  };
121 
123  {
124  openssl_streambuf m_buffer;
125 
126  public:
127  explicit openssl_iostream(unsigned bufsize = 8192, int timeout = -1)
128  : std::iostream(0),
129  m_buffer(*this, bufsize, timeout)
130  { init(&m_buffer); }
131 
132  explicit openssl_iostream(const OpensslServer& server, unsigned bufsize = 8192, int timeout = -1)
133  : OpensslStream(server),
134  std::iostream(0),
135  m_buffer(*this, bufsize, timeout)
136  { init(&m_buffer); }
137 
138  void setTimeout(int timeout) { m_buffer.setTimeout(timeout); }
139  int getTimeout() const { return m_buffer.getTimeout(); }
140  };
141 }
142 
143 #endif // TNT_OPENSSL_H
144