summaryrefslogtreecommitdiff
path: root/vio/VioSSL.cc
blob: a8efacf912f0665994663a7b98410e8f44ece30c (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* 
**  Virtual I/O library for SSL wrapper
**  Written by Andrei Errapart <andreie@no.spam.ee>
*/

/*
 * This file has some huge DBUG_ statements. Boy, this is silly...
 */

#include	"vio-global.h"
#ifdef	VIO_HAVE_OPENSSL
#include	<assert.h>
#include	<netinet/in.h>
#include	<openssl/x509.h>
#include	<openssl/ssl.h>
#include	<openssl/err.h>
#include	<openssl/pem.h>

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

VIO_NS_BEGIN

#define this_ssl_con	my_static_cast(SSL*)(this->ssl_con_)
#define this_bio	my_static_cast(BIO*)(this->bio_)
typedef char*		dataptr_t;

static void
report_errors()
{
  unsigned long	l;
  const char*	file;
  const char*	data;
  int		line,flags;
  DBUG_ENTER("VioSSLConnectorFd::report_errors");

  while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
  {
    char buf[200];
    DBUG_PRINT("error", ("OpenSSL: %s:%s:%d:%s\n", ERR_error_string(l,buf),
			 file,line,(flags&ERR_TXT_STRING)?data:"")) ;
  }
  DBUG_VOID_RETURN;
}

//FIXME: duplicate code!
VioSSL::VioSSL(int fd,
	       vio_ptr	ssl_context,
	       int state)
  : bio_(0), ssl_con_(0), open_(false), sd_(new VioSocket(fd))
{
  DBUG_ENTER("VioSSL::VioSSL");
  DBUG_PRINT("enter", ("this=%p, fd=%d, ssl_context=%p, state=%d",
		       this, fd, ssl_context, state));
  assert(fd!=0);
  assert(ssl_context!=0);
  assert(state==state_connect || state==state_accept);

  if (!init_bio_(fd, ssl_context, state, BIO_NOCLOSE))
    open_ = true;
  DBUG_VOID_RETURN;
}


VioSSL::VioSSL(VioSocket* sd,
	       vio_ptr	 ssl_context,
	       int state)
  :bio_(0), ssl_con_(0), open_(false), sd_(sd)
{
  DBUG_ENTER("VioSSL::VioSSL");
  DBUG_PRINT("enter",
	     ("this=%p, sd=%s, ssl_context=%p, state=%d",
	      this, sd ? sd->description() : "0", ssl_context, state));
  assert(sd != 0);
  assert(ssl_context != 0);
  assert(state == state_connect || state==state_accept);

  if (!init_bio_(sd->sd_, ssl_context, state, BIO_NOCLOSE))
    open_ = true;
  DBUG_VOID_RETURN;
}

VioSSL::~VioSSL()
{
  DBUG_ENTER("VioSSL::~VioSSL");
  DBUG_PRINT("enter", ("this=%p", this));
  if (ssl_con_!=0)
  {
    SSL_shutdown(this_ssl_con);
    SSL_free(this_ssl_con);
  }
  if (sd_!=0)
    delete sd_;
  /* FIXME: no need to close bio? */
  /*
    if (bio_!=0)
    BIO_free(this_bio);
  */
  DBUG_VOID_RETURN;
}

bool
VioSSL::is_open() const
{
  return open_;
}

int
VioSSL::read(vio_ptr buf, int size)
{
  int r;
  DBUG_ENTER("VioSSL::read");
  DBUG_PRINT("enter", ("this=%p, buf=%p, size=%d", this, buf, size));
  assert(this_ssl_con != 0);
  r = SSL_read(this_ssl_con, my_static_cast(dataptr_t)(buf), size);
  if ( r< 0)
    report_errors();
  DBUG_PRINT("exit", ("r=%d", r));
  DBUG_RETURN(r);
}

int
VioSSL::write(const vio_ptr buf, int size)
{
  int r;
  DBUG_ENTER("VioSSL::write");
  DBUG_PRINT("enter", ("this=%p, buf=%p, size=%d", this, buf, size));
  assert(this_ssl_con!=0);
  r = SSL_write(this_ssl_con, my_static_cast(dataptr_t)(buf), size);
  if (r<0)
    report_errors();
  DBUG_PRINT("exit", ("r=%d", r));
  DBUG_RETURN(r);
}

int
VioSSL::blocking(bool onoff)
{
  int r;
  DBUG_ENTER("VioSSL::blocking");
  DBUG_PRINT("enter", ("this=%p, onoff=%s", this, onoff?"true":"false"));
  r = sd_->blocking(onoff);
  DBUG_PRINT("exit", ("r=%d", (int)r ));
  DBUG_RETURN(r);
}

bool 
VioSSL::blocking() const
{
  bool r;
  DBUG_ENTER("VioSSL::blocking");
  DBUG_PRINT("enter", ("this=%p", this));
  r = sd_->blocking();
  DBUG_PRINT("exit", ("r=%d", (int)r ));
  DBUG_RETURN(r);
}

int
VioSSL::fastsend(bool onoff)
{
  int r;
  DBUG_ENTER("VioSSL::fastsend");
  DBUG_PRINT("enter", ("this=%p, onoff=%d", this, (int) onoff));
  r = sd_->fastsend(onoff);
  DBUG_PRINT("exit", ("r=%d", (int)r ));
  DBUG_RETURN(r);
}

int VioSSL::keepalive(bool onoff)
{
  int r;
  DBUG_ENTER("VioSSL::keepalive");
  DBUG_PRINT("enter", ("this=%p, onoff=%d", this, (int) onoff));
  r = sd_->keepalive(onoff);
  DBUG_PRINT("exit", ("r=%d", int(r) ));
  DBUG_RETURN(r);
}

bool
VioSSL::fcntl() const
{
  bool	r;
  DBUG_ENTER("VioSSL::fcntl");
  DBUG_PRINT("enter", ("this=%p", this));
  r = sd_->fcntl();
  DBUG_PRINT("exit", ("r=%d", (int)r ));
  DBUG_RETURN(r);
}

bool
VioSSL::should_retry() const
{
  bool r;
  DBUG_ENTER("VioSSL::should_retry");
  DBUG_PRINT("enter", ("this=%p", this));
  r = sd_->should_retry();
  DBUG_PRINT("exit", ("r=%d", (int)r ));
  DBUG_RETURN(r);
}

int
VioSSL::close()
{
  int r= -2;
  DBUG_ENTER("VioSSL::close");
  DBUG_PRINT("enter", ("this=%p", this));
  if (ssl_con)
  {
    r = SSL_shutdown(this_ssl_con);
    SSL_free(this_ssl_con);
    ssl_con_ = 0;
    BIO_free(this_bio);
    bio_ = 0;
  }
  DBUG_PRINT("exit", ("r=%d", r));
  DBUG_RETURN(r);
}

const char*
VioSSL::description() const
{
  return desc_;
}

const char*
VioSSL::peer_addr() const
{
  if (sd_!=0)
    return sd != 0 ? sd_->peer_addr() : "";
}

const char*
VioSSL::peer_name() const
{
  return sd != 0 ? sd_->peer_name() : "";
}

const char*
VioSSL::cipher_description() const
{
  return SSL_get_cipher_name(this_ssl_con);
}


int
VioSSL::init_bio_(int fd,
		  vio_ptr ssl_context,
		  int state,
		  int bio_flags)
{
  DBUG_ENTER("VioSSL::init_bio_");
  DBUG_PRINT("enter",
	     ("this=%p, fd=%p, ssl_context=%p, state=%d, bio_flags=%d",
	      this, fd, ssl_context, state, bio_flags));

  
  if (!(ssl_con_ = SSL_new(my_static_cast(SSL_CTX*)(ssl_context))))
 {
    DBUG_PRINT("error", ("SSL_new failure"));
    report_errors();
    DBUG_RETURN(-1);
  }
  if (!(bio_ = BIO_new_socket(fd, bio_flags)))
  {
    DBUG_PRINT("error", ("BIO_new_socket failure"));
    report_errors();
    SSL_free(ssl_con_);
    ssl_con_ =0;
    DBUG_RETURN(-1);
  }
  SSL_set_bio(this_ssl_con, this_bio, this_bio);
  switch(state) {
  case state_connect:
    SSL_set_connect_state(this_ssl_con);
    break;
  case state_accept:
    SSL_set_accept_state(this_ssl_con);
    break;
  default:
    assert(0);
  }
  sprintf(desc_, "VioSSL(%d)", fd);
  ssl_cip_ = new SSL_CIPHER ;
  DBUG_RETURN(0);
}


VIO_NS_END

#endif /* VIO_HAVE_OPENSSL */