blob: 2aa11b8bc705f4a5dcf1cc60fa2e82dbf1f581f4 [file] [log] [blame]
Emeric Brun46591952012-05-18 15:47:34 +02001/*
2 * SSL data transfer functions between buffers and SOCK_STREAM sockets
3 *
4 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#define _GNU_SOURCE
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <sys/socket.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <netinet/tcp.h>
24
25#include <openssl/ssl.h>
26
27#include <common/buffer.h>
28#include <common/compat.h>
29#include <common/config.h>
30#include <common/debug.h>
31#include <common/standard.h>
32#include <common/ticks.h>
33#include <common/time.h>
34
35#include <proto/connection.h>
36#include <proto/fd.h>
37#include <proto/freq_ctr.h>
38#include <proto/frontend.h>
39#include <proto/log.h>
40#include <proto/protocols.h>
41#include <proto/ssl_sock.h>
42#include <proto/task.h>
43
44#include <types/global.h>
45
46
47/*
48 * This function is called if SSL * context is not yet allocated. The function
49 * is designed to be called before any other data-layer operation and sets the
50 * handshake flag on the connection. It is safe to call it multiple times.
51 * It returns 0 on success and -1 in error case.
52 */
53static int ssl_sock_init(struct connection *conn)
54{
55 /* already initialized */
56 if (conn->data_ctx)
57 return 0;
58
59 /* If it is in client mode initiate SSL session
60 in connect state otherwise accept state */
61 if (target_srv(&conn->target)) {
62
63 /* Alloc a new SSL session ctx */
64 conn->data_ctx = SSL_new(target_srv(&conn->target)->ssl_ctx.ctx);
65 if (!conn->data_ctx)
66 return -1;
67
68 SSL_set_connect_state(conn->data_ctx);
69 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
70 SSL_set_session(conn->data_ctx, target_srv(&conn->target)->ssl_ctx.reused_sess);
71
72 /* set fd on SSL session context */
73 SSL_set_fd(conn->data_ctx, conn->t.sock.fd);
74
75 /* leave init state and start handshake */
76 conn->flags |= CO_FL_SSL_WAIT_HS;
77 return 0;
78 }
79 else if (target_client(&conn->target)) {
80
81 /* Alloc a new SSL session ctx */
82 conn->data_ctx = SSL_new(target_client(&conn->target)->ssl_ctx.ctx);
83 if (!conn->data_ctx)
84 return -1;
85
86 SSL_set_accept_state(conn->data_ctx);
87
88 /* set fd on SSL session context */
89 SSL_set_fd(conn->data_ctx, conn->t.sock.fd);
90
91 /* leave init state and start handshake */
92 conn->flags |= CO_FL_SSL_WAIT_HS;
93 return 0;
94 }
95 /* don't know how to handle such a target */
96 return -1;
97}
98
99
100/* This is the callback which is used when an SSL handshake is pending. It
101 * updates the FD status if it wants some polling before being called again.
102 * It returns 0 if it fails in a fatal way or needs to poll to go further,
103 * otherwise it returns non-zero and removes itself from the connection's
104 * flags (the bit is provided in <flag> by the caller).
105 */
106int ssl_sock_handshake(struct connection *conn, unsigned int flag)
107{
108 int ret;
109
110 if (!conn->data_ctx)
111 goto out_error;
112
113 ret = SSL_do_handshake(conn->data_ctx);
114 if (ret != 1) {
115 /* handshake did not complete, let's find why */
116 ret = SSL_get_error(conn->data_ctx, ret);
117
118 if (ret == SSL_ERROR_WANT_WRITE) {
119 /* SSL handshake needs to write, L4 connection may not be ready */
120 __conn_sock_stop_recv(conn);
121 __conn_sock_poll_send(conn);
122 return 0;
123 }
124 else if (ret == SSL_ERROR_WANT_READ) {
125 /* SSL handshake needs to read, L4 connection is ready */
126 if (conn->flags & CO_FL_WAIT_L4_CONN)
127 conn->flags &= ~CO_FL_WAIT_L4_CONN;
128 __conn_sock_stop_send(conn);
129 __conn_sock_poll_recv(conn);
130 return 0;
131 }
132 else {
133 /* Fail on all other handshake errors */
134 goto out_error;
135 }
136 }
137
138 /* Handshake succeeded */
139 if (target_srv(&conn->target)) {
140 if (!SSL_session_reused(conn->data_ctx)) {
141 /* check if session was reused, if not store current session on server for reuse */
142 if (target_srv(&conn->target)->ssl_ctx.reused_sess)
143 SSL_SESSION_free(target_srv(&conn->target)->ssl_ctx.reused_sess);
144
145 target_srv(&conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->data_ctx);
146 }
147 }
148
149 /* The connection is now established at both layers, it's time to leave */
150 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
151 return 1;
152
153 out_error:
154 /* Fail on all other handshake errors */
155 conn->flags |= CO_FL_ERROR;
156 conn->flags &= ~flag;
157 return 0;
158}
159
160/* Receive up to <count> bytes from connection <conn>'s socket and store them
161 * into buffer <buf>. The caller must ensure that <count> is always smaller
162 * than the buffer's size. Only one call to recv() is performed, unless the
163 * buffer wraps, in which case a second call may be performed. The connection's
164 * flags are updated with whatever special event is detected (error, read0,
165 * empty). The caller is responsible for taking care of those events and
166 * avoiding the call if inappropriate. The function does not call the
167 * connection's polling update function, so the caller is responsible for this.
168 */
169static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
170{
171 int ret, done = 0;
172 int try = count;
173
174 if (!conn->data_ctx)
175 goto out_error;
176
177 if (conn->flags & CO_FL_HANDSHAKE)
178 /* a handshake was requested */
179 return 0;
180
181 /* compute the maximum block size we can read at once. */
182 if (buffer_empty(buf)) {
183 /* let's realign the buffer to optimize I/O */
184 buf->p = buf->data;
185 }
186 else if (buf->data + buf->o < buf->p &&
187 buf->p + buf->i < buf->data + buf->size) {
188 /* remaining space wraps at the end, with a moving limit */
189 if (try > buf->data + buf->size - (buf->p + buf->i))
190 try = buf->data + buf->size - (buf->p + buf->i);
191 }
192
193 /* read the largest possible block. For this, we perform only one call
194 * to recv() unless the buffer wraps and we exactly fill the first hunk,
195 * in which case we accept to do it once again. A new attempt is made on
196 * EINTR too.
197 */
198 while (try) {
199 ret = SSL_read(conn->data_ctx, bi_end(buf), try);
200
201 if (ret > 0) {
202 buf->i += ret;
203 done += ret;
204 if (ret < try)
205 break;
206 count -= ret;
207 try = count;
208 }
209 else if (ret == 0) {
210 goto read0;
211 }
212 else {
213 ret = SSL_get_error(conn->data_ctx, ret);
214 if (ret == SSL_ERROR_WANT_WRITE) {
215 /* handshake is running, and it needs to poll for a write event */
216 conn->flags |= CO_FL_SSL_WAIT_HS;
217 __conn_sock_poll_send(conn);
218 break;
219 }
220 else if (ret == SSL_ERROR_WANT_READ) {
221 /* we need to poll for retry a read later */
222 __conn_data_poll_recv(conn);
223 break;
224 }
225 /* otherwise it's a real error */
226 goto out_error;
227 }
228 }
229 return done;
230
231 read0:
232 conn_sock_read0(conn);
233 return done;
234 out_error:
235 conn->flags |= CO_FL_ERROR;
236 return done;
237}
238
239
240/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
241 * <flags> may contain MSG_MORE to make the system hold on without sending
242 * data too fast, but this flag is ignored at the moment.
243 * Only one call to send() is performed, unless the buffer wraps, in which case
244 * a second call may be performed. The connection's flags are updated with
245 * whatever special event is detected (error, empty). The caller is responsible
246 * for taking care of those events and avoiding the call if inappropriate. The
247 * function does not call the connection's polling update function, so the caller
248 * is responsible for this.
249 */
250static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
251{
252 int ret, try, done;
253
254 done = 0;
255
256 if (!conn->data_ctx)
257 goto out_error;
258
259 if (conn->flags & CO_FL_HANDSHAKE)
260 /* a handshake was requested */
261 return 0;
262
263 /* send the largest possible block. For this we perform only one call
264 * to send() unless the buffer wraps and we exactly fill the first hunk,
265 * in which case we accept to do it once again.
266 */
267 while (buf->o) {
268 try = buf->o;
269 /* outgoing data may wrap at the end */
270 if (buf->data + try > buf->p)
271 try = buf->data + try - buf->p;
272
273 ret = SSL_write(conn->data_ctx, bo_ptr(buf), try);
274 if (ret > 0) {
275 buf->o -= ret;
276 done += ret;
277
278 if (likely(!buffer_len(buf)))
279 /* optimize data alignment in the buffer */
280 buf->p = buf->data;
281
282 /* if the system buffer is full, don't insist */
283 if (ret < try)
284 break;
285 }
286 else {
287 ret = SSL_get_error(conn->data_ctx, ret);
288 if (ret == SSL_ERROR_WANT_WRITE) {
289 /* we need to poll to retry a write later */
290 __conn_data_poll_send(conn);
291 break;
292 }
293 else if (ret == SSL_ERROR_WANT_READ) {
294 /* handshake is running, and
295 it needs to poll for a read event,
296 write polling must be disabled cause
297 we are sure we can't write anything more
298 before handshake re-performed */
299 conn->flags |= CO_FL_SSL_WAIT_HS;
300 __conn_sock_poll_recv(conn);
301 break;
302 }
303 goto out_error;
304 }
305 }
306 return done;
307
308 out_error:
309 conn->flags |= CO_FL_ERROR;
310 return done;
311}
312
313
314static void ssl_sock_close(struct connection *conn) {
315
316 if (conn->data_ctx) {
317 SSL_free(conn->data_ctx);
318 conn->data_ctx = NULL;
319 }
320
321}
322
323/* This function tries to perform a clean shutdown on an SSL connection, and in
324 * any case, flags the connection as reusable if no handshake was in progress.
325 */
326static void ssl_sock_shutw(struct connection *conn, int clean)
327{
328 if (conn->flags & CO_FL_HANDSHAKE)
329 return;
330 /* no handshake was in progress, try a clean ssl shutdown */
331 if (clean)
332 SSL_shutdown(conn->data_ctx);
333
334 /* force flag on ssl to keep session in cache regardless shutdown result */
335 SSL_set_shutdown(conn->data_ctx, SSL_SENT_SHUTDOWN);
336}
337
338
339/* data-layer operations for SSL sockets */
340struct data_ops ssl_sock = {
341 .snd_buf = ssl_sock_from_buf,
342 .rcv_buf = ssl_sock_to_buf,
343 .rcv_pipe = NULL,
344 .snd_pipe = NULL,
345 .shutr = NULL,
346 .shutw = ssl_sock_shutw,
347 .close = ssl_sock_close,
348 .init = ssl_sock_init,
349};
350
351__attribute__((constructor))
352static void __ssl_sock_init(void) {
353 STACK_OF(SSL_COMP)* cm;
354
355 SSL_library_init();
356 cm = SSL_COMP_get_compression_methods();
357 sk_SSL_COMP_zero(cm);
358}
359
360/*
361 * Local variables:
362 * c-indent-level: 8
363 * c-basic-offset: 8
364 * End:
365 */