gnutls.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 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_GNUTLS_H
31 #define TNT_GNUTLS_H
32 
33 #include <cxxtools/net/tcpstream.h>
34 #include <gnutls/gnutls.h>
35 
36 namespace tnt
37 {
38  class GnuTlsException : public std::runtime_error
39  {
40  unsigned long code;
41  static std::string formatMessage(const char* function, int code_);
42 
43  public:
44  explicit GnuTlsException(const std::string& message)
45  : std::runtime_error(message),
46  code(0)
47  { }
48 
49  GnuTlsException(const char* function, int code_)
50  : std::runtime_error(formatMessage(function, code_)),
51  code(code_)
52  { }
53 
54  unsigned long getCode() const
55  { return code; }
56  };
57 
58  class GnuTlsInit
59  {
60  static unsigned initCount;
61  static gnutls_dh_params dhParams;
62 
63  public:
64  GnuTlsInit();
65  ~GnuTlsInit();
66 
67  gnutls_dh_params getDhParams() const { return dhParams; }
68  };
69 
71  {
72  gnutls_certificate_credentials x509_cred;
73  GnuTlsInit init;
74 
75  public:
76  GnuTlsX509Cred(const char* certificateFile, const char* privateKeyFile);
78 
79  operator gnutls_certificate_credentials() const { return x509_cred; }
80  };
81 
83  {
84  GnuTlsX509Cred cred;
85 
86  public:
87  GnuTlsServer(const char* certificateFile, const char* privateKeyFile);
88 
89  gnutls_certificate_credentials getCred() const { return cred; }
90  };
91 
93  {
94  public:
95  struct FdInfo
96  {
97  int fd;
98  int timeout;
99  };
100 
101  private:
102  gnutls_session session;
103  bool connected;
104  mutable FdInfo fdInfo;
105 
106  public:
108  : session(0),
109  connected(false)
110  { }
111 
112  explicit GnuTlsStream(const GnuTlsServer& server, bool inherit = false)
113  : session(0),
114  connected(false)
115  { accept(server, inherit); }
116 
117  ~GnuTlsStream();
118 
119  void accept(const GnuTlsServer& server, bool inherit = false);
120  void handshake(const GnuTlsServer& server);
121  int sslRead(char* buffer, int bufsize) const;
122  int sslWrite(const char* buffer, int bufsize) const;
123  void shutdown();
124  };
125 
126  class GnuTls_streambuf : public std::streambuf
127  {
128  GnuTlsStream& m_stream;
129  char_type* m_buffer;
130  unsigned m_bufsize;
131 
132  public:
133  explicit GnuTls_streambuf(GnuTlsStream& stream, unsigned bufsize = 256, int timeout = -1);
135  { delete[] m_buffer; }
136 
137  void setTimeout(int t) { m_stream.setTimeout(t); }
138  int getTimeout() const { return m_stream.getTimeout(); }
139 
141  int_type overflow(int_type c);
143  int_type underflow();
145  int sync();
146  };
147 
149  {
150  GnuTls_streambuf m_buffer;
151 
152  public:
153  explicit GnuTls_iostream(unsigned bufsize = 8192, int timeout = -1)
154  : std::iostream(0),
155  m_buffer(*this, bufsize, timeout)
156  { init(&m_buffer); }
157 
158  explicit GnuTls_iostream(const GnuTlsServer& server, unsigned bufsize = 8192, int timeout = -1)
159  : GnuTlsStream(server),
160  std::iostream(0),
161  m_buffer(*this, bufsize, timeout)
162  { init(&m_buffer); }
163 
164  void setTimeout(int timeout) { m_buffer.setTimeout(timeout); }
165  int getTimeout() const { return m_buffer.getTimeout(); }
166  };
167 }
168 
169 #endif // TNT_GNUTLS_H
170