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 
37 namespace tnt
38 {
39  class GnuTlsException : public std::runtime_error
40  {
41  unsigned long _code;
42  static std::string formatMessage(const char* function, int code);
43 
44  public:
45  explicit GnuTlsException(const std::string& message)
46  : std::runtime_error(message),
47  _code(0)
48  { }
49 
50  GnuTlsException(const char* function, int code)
51  : std::runtime_error(formatMessage(function, code)),
52  _code(code)
53  { }
54 
55  unsigned long getCode() const
56  { return _code; }
57  };
58 
59  class GnuTlsInit
60  {
61  static unsigned _initCount;
62  static gnutls_dh_params _dhParams;
63 
64  public:
65  GnuTlsInit();
66  ~GnuTlsInit();
67 
68  gnutls_dh_params getDhParams() const { return _dhParams; }
69  };
70 
71  class GnuTlsX509Cred
72  {
73  gnutls_certificate_credentials _x509_cred;
74  GnuTlsInit _init;
75 
76  public:
77  GnuTlsX509Cred(const char* certificateFile, const char* privateKeyFile);
78  ~GnuTlsX509Cred();
79 
80  operator gnutls_certificate_credentials() const { return _x509_cred; }
81  };
82 
83  class GnuTlsServer : public cxxtools::net::TcpServer
84  {
85  GnuTlsX509Cred _cred;
86 
87  public:
88  GnuTlsServer(const char* certificateFile, const char* privateKeyFile);
89 
90  gnutls_certificate_credentials getCred() const { return _cred; }
91  };
92 
93  class GnuTlsStream : public cxxtools::net::TcpSocket
94  {
95  public:
96  struct FdInfo
97  {
98  int fd;
99  int timeout;
100  };
101 
102  private:
103  gnutls_session _session;
104  bool _connected;
105  mutable FdInfo _fdInfo;
106 
107  public:
108  GnuTlsStream()
109  : _session(0),
110  _connected(false)
111  { }
112 
113  explicit GnuTlsStream(const GnuTlsServer& server, bool inherit = false)
114  : _session(0),
115  _connected(false)
116  { accept(server, inherit); }
117 
118  ~GnuTlsStream();
119 
120  void accept(const GnuTlsServer& server, bool inherit = false);
121  void handshake(const GnuTlsServer& server);
122  int sslRead(char* buffer, int bufsize) const;
123  int sslWrite(const char* buffer, int bufsize) const;
124  void shutdown();
125  };
126 
127  class GnuTls_streambuf : public std::streambuf
128  {
129  GnuTlsStream& _stream;
130  char_type* _buffer;
131  unsigned _bufsize;
132 
133  public:
134  explicit GnuTls_streambuf(GnuTlsStream& stream, unsigned bufsize = 256, int timeout = -1);
135  ~GnuTls_streambuf()
136  { delete[] _buffer; }
137 
138  void setTimeout(int t) { _stream.setTimeout(t); }
139  int getTimeout() const { return _stream.getTimeout(); }
140 
142  int_type overflow(int_type c);
144  int_type underflow();
146  int sync();
147  };
148 
149  class GnuTls_iostream : public GnuTlsStream, public std::iostream
150  {
151  GnuTls_streambuf _buffer;
152 
153  public:
154  explicit GnuTls_iostream(unsigned bufsize = 8192, int timeout = -1)
155  : std::iostream(0),
156  _buffer(*this, bufsize, timeout)
157  { init(&_buffer); }
158 
159  explicit GnuTls_iostream(const GnuTlsServer& server, unsigned bufsize = 8192, int timeout = -1)
160  : GnuTlsStream(server),
161  std::iostream(0),
162  _buffer(*this, bufsize, timeout)
163  { init(&_buffer); }
164 
165  void setTimeout(int timeout) { _buffer.setTimeout(timeout); }
166  int getTimeout() const { return _buffer.getTimeout(); }
167  };
168 }
170 
171 #endif // TNT_GNUTLS_H
172