Rename functions so we can distinguish client and server SSL support.
[cascardo/rnetproxy.git] / hcconn_ssl.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 <fcntl.h>
26 #include "hcconn_internal.h"
27
28 struct ssl_data
29 {
30   gnutls_session_t session;
31   GString *buffer;
32   gboolean handshaking;
33   gpointer lowconn;
34 };
35
36 static void
37 ssl_client_session_new (gnutls_session_t *session)
38 {
39   int kx_prio[] = {GNUTLS_KX_RSA, 0};
40   gnutls_certificate_credentials cred;
41   gnutls_certificate_allocate_credentials (&cred);
42   gnutls_init (session, GNUTLS_CLIENT);
43   gnutls_set_default_priority (*session);
44   gnutls_kx_set_priority (*session, kx_prio);
45   gnutls_credentials_set (*session, GNUTLS_CRD_CERTIFICATE, cred);
46 }
47
48 static struct ssl_data *
49 ssl_data_new_client (void)
50 {
51   struct ssl_data *ssl;
52   ssl = g_slice_new (struct ssl_data);
53   ssl_client_session_new (&ssl->session);
54   ssl->buffer = g_string_sized_new (4096);
55   ssl->handshaking = FALSE;
56   return ssl;
57 }
58
59 static void
60 ssl_data_destroy (struct ssl_data *ssl)
61 {
62   gnutls_deinit (ssl->session);
63   g_string_free (ssl->buffer, TRUE);
64   g_slice_free (struct ssl_data, ssl);
65 }
66
67 static ssize_t
68 ssl_push (gnutls_transport_ptr_t ptr, const void *buffer, size_t len)
69 {
70   HCConn *conn = ptr;
71   struct ssl_data *ssl = conn->layer;
72   hc_conn_write (ssl->lowconn, (void *) buffer, len);
73   return len;
74 }
75
76 static ssize_t
77 ssl_pull (gnutls_transport_ptr_t ptr, void *buffer, size_t len)
78 {
79   HCConn *conn = ptr;
80   struct ssl_data *ssl = conn->layer;
81   int r;
82   if (ssl->handshaking == TRUE)
83     {
84       r = hc_conn_read (ssl->lowconn, buffer, len);
85       return r;
86     }
87   if (len > ssl->buffer->len)
88     {
89       r = ssl->buffer->len;
90       memcpy (buffer, ssl->buffer->str, r);
91       g_string_truncate (ssl->buffer, 0);
92     }
93   else
94     {
95       r = len;
96       memcpy (buffer, ssl->buffer->str, r);
97       g_string_erase (ssl->buffer, 0, r);
98     }
99   if (r == 0)
100     {
101       gnutls_transport_set_errno (ssl->session, EAGAIN);
102       return -1;
103     }
104   return r;
105 }
106
107 static void
108 ssl_server_handshake (struct ssl_data *ssl)
109 {
110   int error;
111   if ((error = gnutls_handshake (ssl->session)) < 0)
112     {
113       if (gnutls_error_is_fatal (error))
114         g_critical ("Fatal error while doing TLS handshaking: %s\n",
115                     gnutls_strerror (error));
116     }
117   else
118     {
119       ssl->handshaking = FALSE;
120     }
121 }
122
123 static void
124 ssl_server_connect (HCConn *conn)
125 {
126   struct ssl_data *ssl = conn->layer;
127   gnutls_transport_set_ptr (ssl->session, (gnutls_transport_ptr_t) conn);
128   gnutls_transport_set_push_function (ssl->session, ssl_push);
129   gnutls_transport_set_pull_function (ssl->session, ssl_pull);
130   ssl->handshaking = TRUE;
131   ssl_server_handshake (ssl);
132 }
133
134 static void
135 hc_conn_ssl_close (gpointer data)
136 {
137   struct ssl_data *ssl = data;
138   if (ssl != NULL)
139     {
140       gnutls_bye (ssl->session, GNUTLS_SHUT_RDWR);
141       hc_conn_close (ssl->lowconn);
142       ssl_data_destroy (ssl);
143     }
144 }
145
146 static ssize_t
147 hc_conn_ssl_read (gpointer data, gchar *buffer, size_t len)
148 {
149   struct ssl_data *ssl = data;
150   return gnutls_record_recv (ssl->session, buffer, len);
151 }
152
153 static ssize_t
154 hc_conn_ssl_write (gpointer data, gchar *buffer, size_t len)
155 {
156   struct ssl_data *ssl = data;
157   return gnutls_record_send (ssl->session, buffer, len);
158 }
159
160 void
161 hc_conn_ssl_watch (HCConn *conn, HCEvent event, gpointer data)
162 {
163   char buffer[4096];
164   HCConn *ssl_conn = data;
165   struct ssl_data *ssl = ssl_conn->layer;
166   int r;
167   switch (event)
168     {
169     case HC_EVENT_READ:
170       if (ssl->handshaking)
171         {
172           ssl_server_handshake (ssl);
173           return;
174         }
175       while ((r = hc_conn_read (ssl->lowconn, buffer, sizeof (buffer))) > 0)
176         g_string_append_len (ssl->buffer, buffer, r);
177       if (ssl_conn->func && !ssl->handshaking)
178         ssl_conn->func (ssl_conn, event, ssl_conn->data);
179       break;
180     case HC_EVENT_CLOSE:
181       if (ssl_conn->func)
182         ssl_conn->func (ssl_conn, event, ssl_conn->data);
183     }
184 }
185
186 void
187 hc_conn_set_driver_ssl_client (HCConn *conn, HCConn *lowconn)
188 {
189   struct ssl_data *ssl = ssl_data_new_client ();
190   ssl->lowconn = lowconn;
191   conn->layer = ssl;
192   conn->read = hc_conn_ssl_read;
193   conn->write = hc_conn_ssl_write;
194   conn->close = hc_conn_ssl_close;
195   hc_conn_set_callback (lowconn, hc_conn_ssl_watch, conn);
196   ssl_server_connect (conn);
197 }