~ubuntu-branches/ubuntu/trusty/aria2/trusty-proposed

« back to all changes in this revision

Viewing changes to src/WinTLSSession.h

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-12-16 18:41:03 UTC
  • mfrom: (2.5.21 sid)
  • Revision ID: package-import@ubuntu.com-20131216184103-xzah3019zwut429g
Tags: 1.18.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <!-- copyright */
 
2
/*
 
3
 * aria2 - The high speed download utility
 
4
 *
 
5
 * Copyright (C) 2013 Nils Maier
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
20
 *
 
21
 * In addition, as a special exception, the copyright holders give
 
22
 * permission to link the code of portions of this program with the
 
23
 * OpenSSL library under certain conditions as described in each
 
24
 * individual source file, and distribute linked combinations
 
25
 * including the two.
 
26
 * You must obey the GNU General Public License in all respects
 
27
 * for all of the code used other than OpenSSL.  If you modify
 
28
 * file(s) with this exception, you may extend this exception to your
 
29
 * version of the file(s), but you are not obligated to do so.  If you
 
30
 * do not wish to do so, delete this exception statement from your
 
31
 * version.  If you delete this exception statement from all source
 
32
 * files in the program, then also delete it here.
 
33
 */
 
34
/* copyright --> */
 
35
 
 
36
#ifndef WIN_TLS_SESSION_H
 
37
#define WIN_TLS_SESSION_H
 
38
 
 
39
#include <vector>
 
40
 
 
41
#include "common.h"
 
42
#include "TLSSession.h"
 
43
#include "WinTLSContext.h"
 
44
 
 
45
namespace aria2 {
 
46
 
 
47
namespace wintls {
 
48
  struct Buffer {
 
49
  private:
 
50
    size_t off_, free_, cap_;
 
51
    std::vector<char> buf_;
 
52
 
 
53
  public:
 
54
    inline Buffer() : off_(0), free_(0), cap_(0) {}
 
55
 
 
56
    inline size_t size() const {
 
57
      return off_;
 
58
    }
 
59
    inline size_t free() const {
 
60
      return free_;
 
61
    }
 
62
    inline void resize(size_t len) {
 
63
      if (cap_ >= len) {
 
64
        return;
 
65
      }
 
66
      buf_.resize(len);
 
67
      cap_ = buf_.size();
 
68
      free_ = cap_ - off_;
 
69
    }
 
70
    inline char* data() {
 
71
      return buf_.data();
 
72
    }
 
73
    inline char* end() {
 
74
      return buf_.data() + off_;
 
75
    }
 
76
    inline void eat(size_t len) {
 
77
      off_ -= len;
 
78
      if (off_) {
 
79
        memmove(buf_.data(), buf_.data() + len, off_);
 
80
      }
 
81
      free_ = cap_ - off_;
 
82
    }
 
83
    inline void clear() {
 
84
      eat(off_);
 
85
    }
 
86
    inline void advance(size_t len) {
 
87
      off_ += len;
 
88
      free_ = cap_ - off_;
 
89
    }
 
90
    inline void write(const void* data, size_t len) {
 
91
      if (!len) {
 
92
        return;
 
93
      }
 
94
      resize(off_ + len);
 
95
      memcpy(end(), data, len);
 
96
      advance(len);
 
97
    }
 
98
  };
 
99
} // namespace wintls
 
100
 
 
101
class WinTLSSession : public TLSSession {
 
102
  enum state_t {
 
103
    st_constructed,
 
104
    st_initialized,
 
105
    st_handshake_write,
 
106
    st_handshake_write_last,
 
107
    st_handshake_read,
 
108
    st_handshake_done,
 
109
    st_connected,
 
110
    st_closing,
 
111
    st_closed,
 
112
    st_error
 
113
  };
 
114
 
 
115
public:
 
116
  WinTLSSession(WinTLSContext* ctx);
 
117
 
 
118
  // MUST deallocate all resources
 
119
  virtual ~WinTLSSession();
 
120
 
 
121
  // Initializes SSL/TLS session. The |sockfd| is the underlying
 
122
  // tranport socket. This function returns TLS_ERR_OK if it
 
123
  // succeeds, or TLS_ERR_ERROR.
 
124
  virtual int init(sock_t sockfd) CXX11_OVERRIDE;
 
125
 
 
126
  // Sets |hostname| for TLS SNI extension. This is only meaningful for
 
127
  // client side session. This function returns TLS_ERR_OK if it
 
128
  // succeeds, or TLS_ERR_ERROR.
 
129
  virtual int setSNIHostname(const std::string& hostname) CXX11_OVERRIDE;
 
130
 
 
131
  // Closes the SSL/TLS session. Don't close underlying transport
 
132
  // socket. This function returns TLS_ERR_OK if it succeeds, or
 
133
  // TLS_ERR_ERROR.
 
134
  virtual int closeConnection() CXX11_OVERRIDE;
 
135
 
 
136
  // Returns TLS_WANT_READ if SSL/TLS session needs more data from
 
137
  // remote endpoint to proceed, or TLS_WANT_WRITE if SSL/TLS session
 
138
  // needs to write more data to proceed. If SSL/TLS session needs
 
139
  // neither read nor write data at the moment, return value is
 
140
  // undefined.
 
141
  virtual int checkDirection() CXX11_OVERRIDE;
 
142
 
 
143
  // Sends |data| with length |len|. This function returns the number
 
144
  // of bytes sent if it succeeds, or TLS_ERR_WOULDBLOCK if the
 
145
  // underlying tranport blocks, or TLS_ERR_ERROR.
 
146
  virtual ssize_t writeData(const void* data, size_t len) CXX11_OVERRIDE;
 
147
 
 
148
  // Receives data into |data| with length |len|. This function returns
 
149
  // the number of bytes received if it succeeds, or TLS_ERR_WOULDBLOCK
 
150
  // if the underlying tranport blocks, or TLS_ERR_ERROR.
 
151
  virtual ssize_t readData(void* data, size_t len) CXX11_OVERRIDE;
 
152
 
 
153
  // Performs client side handshake. The |hostname| is the hostname of
 
154
  // the remote endpoint and is used to verify its certificate. This
 
155
  // function returns TLS_ERR_OK if it succeeds, or TLS_ERR_WOULDBLOCK
 
156
  // if the underlying transport blocks, or TLS_ERR_ERROR.
 
157
  // When returning TLS_ERR_ERROR, provide certificate validation error
 
158
  // in |handshakeErr|.
 
159
  virtual int tlsConnect(const std::string& hostname, std::string& handshakeErr) CXX11_OVERRIDE;
 
160
 
 
161
  // Performs server side handshake. This function returns TLS_ERR_OK
 
162
  // if it succeeds, or TLS_ERR_WOULDBLOCK if the underlying transport
 
163
  // blocks, or TLS_ERR_ERROR.
 
164
  virtual int tlsAccept() CXX11_OVERRIDE;
 
165
 
 
166
  // Returns last error string
 
167
  virtual std::string getLastErrorString() CXX11_OVERRIDE;
 
168
 
 
169
private:
 
170
  std::string hostname_;
 
171
  sock_t sockfd_;
 
172
  TLSSessionSide side_;
 
173
  CredHandle* cred_;
 
174
  CtxtHandle handle_;
 
175
 
 
176
  // Buffer for already encrypted writes
 
177
  wintls::Buffer writeBuf_;
 
178
  // While the writeBuf_ holds encrypted messages, writeBuffered_ has the
 
179
  // corresponding size of unencrpted data used to procude the messages.
 
180
  size_t writeBuffered_;
 
181
  // Buffer for still encrypted reads
 
182
  wintls::Buffer readBuf_;
 
183
  // Buffer for already decrypted reads
 
184
  wintls::Buffer decBuf_;
 
185
 
 
186
  state_t state_;
 
187
 
 
188
  SECURITY_STATUS status_;
 
189
  std::unique_ptr<SecPkgContext_StreamSizes> streamSizes_;
 
190
};
 
191
 
 
192
} // namespace aria2
 
193
 
 
194
#endif // TLS_SESSION_H