1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/* tnt/gnutls.h
* Copyright (C) 2006 Tommi Maekitalo
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
* NON-INFRINGEMENT. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef TNT_GNUTLS_H
#define TNT_GNUTLS_H
#include <cxxtools/tcpstream.h>
#include <gnutls/gnutls.h>
namespace tnt
{
class GnuTlsException : public std::runtime_error
{
unsigned long code;
static std::string formatMessage(const std::string& function, int code_);
public:
GnuTlsException(const std::string& function, int code_)
: std::runtime_error(formatMessage(function, code_)),
code(code_)
{ }
unsigned long getCode() const
{ return code; }
};
class GnuTlsInit
{
static unsigned initCount;
static gnutls_dh_params dhParams;
public:
GnuTlsInit();
~GnuTlsInit();
gnutls_dh_params getDhParams() const { return dhParams; }
};
class GnuTlsX509Cred
{
gnutls_certificate_credentials x509_cred;
GnuTlsInit init;
public:
GnuTlsX509Cred(const char* certificateFile, const char* privateKeyFile);
~GnuTlsX509Cred();
operator gnutls_certificate_credentials() const { return x509_cred; }
};
class GnuTlsServer : public cxxtools::net::Server
{
GnuTlsX509Cred cred;
public:
GnuTlsServer(const char* certificateFile, const char* privateKeyFile);
gnutls_certificate_credentials getCred() const { return cred; }
};
class GnuTlsStream : public cxxtools::net::Stream
{
gnutls_session session;
public:
GnuTlsStream()
: session(0)
{ }
explicit GnuTlsStream(int fd)
: cxxtools::net::Stream(fd)
{ }
explicit GnuTlsStream(const GnuTlsServer& server)
{ accept(server); }
~GnuTlsStream();
void accept(const GnuTlsServer& server);
int sslRead(char* buffer, int bufsize) const;
int sslWrite(const char* buffer, int bufsize) const;
void shutdown() const;
};
class GnuTls_streambuf : public std::streambuf
{
GnuTlsStream& m_stream;
char_type* m_buffer;
unsigned m_bufsize;
public:
explicit GnuTls_streambuf(GnuTlsStream& stream, unsigned bufsize = 256, int timeout = -1);
~GnuTls_streambuf()
{ delete[] m_buffer; }
void setTimeout(int t) { m_stream.setTimeout(t); }
int getTimeout() const { return m_stream.getTimeout(); }
/// overload std::streambuf
int_type overflow(int_type c);
/// overload std::streambuf
int_type underflow();
/// overload std::streambuf
int sync();
};
class GnuTls_iostream : public GnuTlsStream, public std::iostream
{
GnuTls_streambuf m_buffer;
public:
explicit GnuTls_iostream(unsigned bufsize = 256, int timeout = -1)
: GnuTlsStream(-1),
std::iostream(&m_buffer),
m_buffer(*this, bufsize, timeout)
{ }
explicit GnuTls_iostream(const GnuTlsServer& server, unsigned bufsize = 256, int timeout = -1)
: GnuTlsStream(server),
std::iostream(&m_buffer),
m_buffer(*this, bufsize, timeout)
{ }
void setTimeout(int timeout) { m_buffer.setTimeout(timeout); }
int getTimeout() const { return m_buffer.getTimeout(); }
};
}
#endif // TNT_GNUTLS_H
|