Separate iochannel implementation from HCConn interface.
[cascardo/rnetproxy.git] / ssl_server.c
1 /*
2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
4 **  
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **  
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ** GNU General Public License for more details.
14 **  
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 **  
19 */
20
21 #include <gnutls/gnutls.h>
22 #include <glib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include "ssl.h"
26
27 static struct ssl_data *
28 ssl_data_new (void)
29 {
30   struct ssl_data *ssl;
31   int kx_prio[] = {GNUTLS_KX_RSA, 0};
32   gnutls_certificate_credentials cred;
33   gnutls_certificate_allocate_credentials (&cred);
34   ssl = g_slice_new (struct ssl_data);
35   gnutls_init (&ssl->session, GNUTLS_CLIENT);
36   gnutls_set_default_priority (ssl->session);
37   gnutls_set_default_priority (ssl->session);
38   gnutls_kx_set_priority (ssl->session, kx_prio);
39   gnutls_credentials_set (ssl->session, GNUTLS_CRD_CERTIFICATE, cred);
40   ssl->buffer = g_string_sized_new (4096);
41   ssl->handshaking = FALSE;
42   return ssl;
43 }
44
45 static void
46 ssl_data_destroy (struct ssl_data *ssl)
47 {
48   gnutls_deinit (ssl->session);
49   g_string_free (ssl->buffer, TRUE);
50   g_slice_free (struct ssl_data, ssl);
51 }
52
53 static ssize_t
54 ssl_push (gnutls_transport_ptr_t ptr, const void *buffer, size_t len)
55 {
56   net_hook_t *hook = ptr;
57   struct ssl_data *ssl = hook->data;
58   int r;
59   if (ssl->handshaking == TRUE)
60     {
61       hc_conn_write (hook->conn, (void *) buffer, len);
62       return len;
63     }
64   hc_conn_write (hook->conn, (void *) buffer, len);
65   return len;
66 }
67
68 static ssize_t
69 ssl_pull (gnutls_transport_ptr_t ptr, void *buffer, size_t len)
70 {
71   net_hook_t *hook = ptr;
72   struct ssl_data *ssl = hook->data;
73   int r;
74   if (ssl->handshaking == TRUE)
75     {
76       r = hc_conn_read (hook->conn, buffer, len);
77       return r;
78     }
79   if (len > ssl->buffer->len)
80     {
81       r = ssl->buffer->len;
82       memcpy (buffer, ssl->buffer->str, r);
83       g_string_truncate (ssl->buffer, 0);
84     }
85   else
86     {
87       r = len;
88       memcpy (buffer, ssl->buffer->str, r);
89       g_string_erase (ssl->buffer, 0, r);
90     }
91   if (r == 0)
92     {
93       gnutls_transport_set_errno (ssl->session, EAGAIN);
94       return -1;
95     }
96   return r;
97 }
98
99 static void
100 ssl_server_connect (net_hook_t *hook)
101 {
102   struct ssl_data *ssl = hook->data;
103   int error;
104   gnutls_transport_set_ptr (ssl->session, (gnutls_transport_ptr_t) hook);
105   gnutls_transport_set_push_function (ssl->session, ssl_push);
106   gnutls_transport_set_pull_function (ssl->session, ssl_pull);
107   ssl->handshaking = TRUE;
108   if ((error = gnutls_handshake (ssl->session)) < 0)
109     {
110       if (gnutls_error_is_fatal (error))
111         g_critical ("Fatal error while doing TLS handshaking: %s\n",
112                     gnutls_strerror (error));
113     }
114   if (error != GNUTLS_E_AGAIN && error != GNUTLS_E_INTERRUPTED)
115     {
116       ssl->handshaking = FALSE;
117     }
118 }
119
120 static void
121 ssl_server_close (net_hook_t *hook)
122 {
123   struct ssl_data *ssl = hook->data;
124   if (hook->peer)
125     {
126       hook->peer->peer = NULL;
127       hc_conn_close (hook->peer->conn);
128     }
129   hc_conn_close (hook->conn);
130   if (ssl != NULL)
131     {
132       gnutls_bye (ssl->session, GNUTLS_SHUT_RDWR);
133       ssl_data_destroy (ssl);
134     }
135   g_slice_free (net_hook_t, hook);
136 }
137
138 static void
139 ssl_server_read (net_hook_t *hook, gchar *buffer, size_t len)
140 {
141   struct ssl_data *ssl = hook->data;
142   int r;
143   g_string_append_len (ssl->buffer, buffer, len);
144   do
145     {
146       r = gnutls_record_recv (ssl->session, buffer, len);
147       if (r > 0)
148         hc_conn_write (hook->peer->conn, buffer, r);
149     } while (r > 0);
150 }
151
152 static void
153 ssl_server_error (net_hook_t *hook)
154 {
155   g_message ("Error in POP3 client connection.");
156 }
157
158 net_hook_t *
159 ssl_server_hook_new (net_hook_t *client_hook, char *server, char *port)
160 {
161   net_hook_t *hook;
162   int fd;
163   hook = g_slice_new (net_hook_t);
164   hook->peer = client_hook;
165   hook->server = TRUE;
166   hook->connect = ssl_server_connect;
167   hook->close = ssl_server_close;
168   hook->read = ssl_server_read;
169   hook->data = ssl_data_new ();
170   hook->conn = hc_conn_new (nethook_event, hook);
171   fd = hc_tcp_connect (server, port);
172   hc_conn_set_driver_channel (hook->conn, fd);
173   return hook;
174 }