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 *
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;
46 ssl_data_destroy (struct ssl_data *ssl)
48 gnutls_deinit (ssl->session);
49 g_string_free (ssl->buffer, TRUE);
50 g_slice_free (struct ssl_data, ssl);
54 ssl_push (gnutls_transport_ptr_t ptr, const void *buffer, size_t len)
56 net_hook_t *hook = ptr;
57 struct ssl_data *ssl = hook->data;
59 if (ssl->handshaking == TRUE)
61 hc_conn_write (hook->conn, (void *) buffer, len);
64 hc_conn_write (hook->conn, (void *) buffer, len);
69 ssl_pull (gnutls_transport_ptr_t ptr, void *buffer, size_t len)
71 net_hook_t *hook = ptr;
72 struct ssl_data *ssl = hook->data;
74 if (ssl->handshaking == TRUE)
76 r = hc_conn_read (hook->conn, buffer, len);
79 if (len > ssl->buffer->len)
82 memcpy (buffer, ssl->buffer->str, r);
83 g_string_truncate (ssl->buffer, 0);
88 memcpy (buffer, ssl->buffer->str, r);
89 g_string_erase (ssl->buffer, 0, r);
93 gnutls_transport_set_errno (ssl->session, EAGAIN);
100 ssl_server_connect (net_hook_t *hook)
102 struct ssl_data *ssl = hook->data;
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)
110 if (gnutls_error_is_fatal (error))
111 g_critical ("Fatal error while doing TLS handshaking: %s\n",
112 gnutls_strerror (error));
114 if (error != GNUTLS_E_AGAIN && error != GNUTLS_E_INTERRUPTED)
116 ssl->handshaking = FALSE;
121 ssl_server_close (net_hook_t *hook)
123 struct ssl_data *ssl = hook->data;
126 hook->peer->peer = NULL;
127 hc_conn_close (hook->peer->conn);
129 hc_conn_close (hook->conn);
132 gnutls_bye (ssl->session, GNUTLS_SHUT_RDWR);
133 ssl_data_destroy (ssl);
135 g_slice_free (net_hook_t, hook);
139 ssl_server_read (net_hook_t *hook, gchar *buffer, size_t len)
141 struct ssl_data *ssl = hook->data;
143 g_string_append_len (ssl->buffer, buffer, len);
146 r = gnutls_record_recv (ssl->session, buffer, len);
148 hc_conn_write (hook->peer->conn, buffer, r);
153 ssl_server_error (net_hook_t *hook)
155 g_message ("Error in POP3 client connection.");
159 ssl_server_hook_new (net_hook_t *client_hook, char *server, char *port)
162 hook = g_slice_new (net_hook_t);
163 hook->peer = client_hook;
165 hook->connect = ssl_server_connect;
166 hook->close = ssl_server_close;
167 hook->read = ssl_server_read;
168 hook->data = ssl_data_new ();
169 hook->conn = hc_conn_new (hc_tcp_connect (server, port), nethook_event, hook);