summaryrefslogtreecommitdiff
path: root/httpd/tnt/openssl.h
blob: 863782f2dbe1842f877f7e28ce366ee87976ceb6 (plain)
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
/* tnt/openssl.h
 * Copyright (C) 2003 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_OPENSSL_H
#define TNT_OPENSSL_H

#include <cxxtools/tcpstream.h>
#include <openssl/ssl.h>

namespace tnt
{
  class OpensslException : public std::runtime_error
  {
      unsigned long code;

    public:
      OpensslException(const std::string& what, unsigned long code_)
        : std::runtime_error(what),
          code(code_)
      { }

      unsigned long getCode() const
      { return code; }
  };

  class OpensslServer : public cxxtools::net::Server
  {
      SSL_CTX* ctx;
      void installCertificates(const char* certificateFile, const char* privateKeyFile);

    public:
      OpensslServer(const char* certificateFile);
      OpensslServer(const char* certificateFile, const char* privateKeyFile);
      ~OpensslServer();

      SSL_CTX* getSslContext() const  { return ctx; }
  };

  class OpensslStream : public cxxtools::net::Stream
  {
      SSL* ssl;

    public:
      OpensslStream();

      explicit OpensslStream(int fd)
        : cxxtools::net::Stream(fd)
        { }

      explicit OpensslStream(const OpensslServer& server);
      ~OpensslStream();

      void accept(const OpensslServer& server);

      int sslRead(char* buffer, int bufsize) const;
      int sslWrite(const char* buffer, int bufsize) const;
      void shutdown() const;
  };

  class openssl_streambuf : public std::streambuf
  {
      OpensslStream& m_stream;
      char_type* m_buffer;
      unsigned m_bufsize;

    public:
      explicit openssl_streambuf(OpensslStream& stream, unsigned bufsize = 256, int timeout = -1);
      ~openssl_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 openssl_iostream : public OpensslStream, public std::iostream
  {
      openssl_streambuf m_buffer;

    public:
      explicit openssl_iostream(unsigned bufsize = 256, int timeout = -1)
        : OpensslStream(-1),
          std::iostream(&m_buffer),
          m_buffer(*this, bufsize, timeout)
        { }

      explicit openssl_iostream(const OpensslServer& server, unsigned bufsize = 256, int timeout = -1)
        : OpensslStream(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_OPENSSL_H