2 ** Copyright (C) 2006 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3 ** Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
21 #include <gnutls/gnutls.h>
27 static struct ssl_data *
28 ssl_data_new (char *server)
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 ssl->server = g_strdup (server);
36 gnutls_init (&ssl->session, GNUTLS_CLIENT);
37 gnutls_set_default_priority (ssl->session);
38 gnutls_set_default_priority (ssl->session);
39 gnutls_kx_set_priority (ssl->session, kx_prio);
40 gnutls_credentials_set (ssl->session, GNUTLS_CRD_CERTIFICATE, cred);
41 ssl->buffer = g_string_sized_new (4096);
42 ssl->handshaking = FALSE;
47 ssl_data_destroy (struct ssl_data *ssl)
49 gnutls_deinit (ssl->session);
51 g_string_free (ssl->buffer, TRUE);
52 g_slice_free (struct ssl_data, ssl);
56 ssl_push (gnutls_transport_ptr_t ptr, const void *buffer, size_t len)
58 net_hook_t *hook = ptr;
59 struct ssl_data *ssl = hook->data;
61 if (ssl->handshaking == TRUE)
63 hc_conn_write (hook->conn, (void *) buffer, len);
66 hc_conn_write (hook->conn, (void *) buffer, len);
71 ssl_pull (gnutls_transport_ptr_t ptr, void *buffer, size_t len)
73 net_hook_t *hook = ptr;
74 struct ssl_data *ssl = hook->data;
76 if (ssl->handshaking == TRUE)
78 r = hc_conn_read (hook->conn, buffer, len);
81 if (len > ssl->buffer->len)
84 memcpy (buffer, ssl->buffer->str, r);
85 g_string_truncate (ssl->buffer, 0);
90 memcpy (buffer, ssl->buffer->str, r);
91 g_string_erase (ssl->buffer, 0, r);
95 gnutls_transport_set_errno (ssl->session, EAGAIN);
102 ssl_server_connect (net_hook_t *hook)
104 struct ssl_data *ssl = hook->data;
106 gnutls_transport_set_ptr (ssl->session, (gnutls_transport_ptr_t) hook);
107 gnutls_transport_set_push_function (ssl->session, ssl_push);
108 gnutls_transport_set_pull_function (ssl->session, ssl_pull);
109 ssl->handshaking = TRUE;
110 if ((error = gnutls_handshake (ssl->session)) < 0)
112 if (gnutls_error_is_fatal (error))
113 g_critical ("Fatal error while doing TLS handshaking: %s\n",
114 gnutls_strerror (error));
116 if (error != GNUTLS_E_AGAIN && error != GNUTLS_E_INTERRUPTED)
118 ssl->handshaking = FALSE;
123 ssl_server_close (net_hook_t *hook)
125 struct ssl_data *ssl = hook->data;
128 hook->peer->peer = NULL;
129 hc_conn_close (hook->peer->conn);
131 hc_conn_close (hook->conn);
134 gnutls_bye (ssl->session, GNUTLS_SHUT_RDWR);
135 ssl_data_destroy (ssl);
137 g_slice_free (net_hook_t, hook);
141 ssl_server_read (net_hook_t *hook, gchar *buffer, size_t len)
143 struct ssl_data *ssl = hook->data;
145 g_string_append_len (ssl->buffer, buffer, len);
148 r = gnutls_record_recv (ssl->session, buffer, len);
150 hc_conn_write (hook->peer->conn, buffer, r);
155 ssl_server_error (net_hook_t *hook)
157 g_message ("Error in POP3 client connection.");
161 ssl_server_hook_new (net_hook_t *client_hook, char *server, char *port)
164 hook = g_slice_new (net_hook_t);
165 hook->peer = client_hook;
167 hook->connect = ssl_server_connect;
168 hook->close = ssl_server_close;
169 hook->read = ssl_server_read;
170 hook->data = ssl_data_new (server);
171 hook->conn = hc_conn_new (hc_tcp_connect (server, port), nethook_event, hook);