6738
|
1 /**
|
|
2 * @file ssl-gnutls.c SSL Operations for GNUTLS
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU General Public License as published by
|
|
11 * the Free Software Foundation; either version 2 of the License, or
|
|
12 * (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23 #include "debug.h"
|
|
24 #include "sslconn.h"
|
|
25
|
|
26 #include <gnutls/gnutls.h>
|
|
27
|
|
28 typedef struct
|
|
29 {
|
|
30 gnutls_session session;
|
|
31 gnutls_certificate_client_credentials xcred;
|
|
32
|
|
33 } GaimSslGnutlsData;
|
|
34
|
|
35 #define GAIM_SSL_GNUTLS_DATA(gsc) ((GaimSslGnutlsData *)gsc->private_data)
|
|
36
|
|
37 static gnutls_certificate_client_credentials xcred;
|
|
38
|
|
39 static gboolean
|
|
40 ssl_gnutls_init(void)
|
|
41 {
|
|
42 gnutls_global_init();
|
|
43
|
|
44 gnutls_certificate_allocate_credentials(&xcred);
|
|
45 gnutls_certificate_set_x509_trust_file(xcred, "ca.pem", GNUTLS_X509_FMT_PEM);
|
|
46
|
|
47 return TRUE;
|
|
48 }
|
|
49
|
|
50 static void
|
|
51 ssl_gnutls_uninit(void)
|
|
52 {
|
|
53 gnutls_global_deinit();
|
|
54
|
|
55 gnutls_certificate_free_credentials(xcred);
|
|
56 }
|
|
57
|
|
58 static void
|
|
59 ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond)
|
|
60 {
|
|
61 GaimSslConnection *gsc = (GaimSslConnection *)data;
|
|
62 GaimSslGnutlsData *gnutls_data;
|
|
63 int ret;
|
|
64 const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 };
|
|
65
|
|
66 gsc->fd = source;
|
|
67
|
|
68 gnutls_data = g_new0(GaimSslGnutlsData, 1);
|
|
69 gsc->private_data = gnutls_data;
|
|
70
|
|
71 gnutls_init(&gnutls_data->session, GNUTLS_CLIENT);
|
|
72 gnutls_set_default_priority(gnutls_data->session);
|
|
73
|
|
74 gnutls_certificate_type_set_priority(gnutls_data->session,
|
|
75 cert_type_priority);
|
|
76
|
|
77 gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE,
|
|
78 xcred);
|
|
79
|
|
80 gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(source));
|
|
81
|
|
82 gaim_debug_info("gnutls", "Handshaking\n");
|
|
83 ret = gnutls_handshake(gnutls_data->session);
|
|
84
|
|
85 if (ret < 0)
|
|
86 {
|
|
87 }
|
|
88 else
|
|
89 {
|
|
90 gaim_debug_info("gnutls", "Calling input function\n");
|
|
91 gsc->input_func(gsc->user_data, (GaimSslConnection *)gsc, cond);
|
|
92 }
|
|
93 }
|
|
94
|
|
95 static void
|
|
96 ssl_gnutls_close(GaimSslConnection *gsc)
|
|
97 {
|
|
98 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc);
|
|
99
|
|
100 gnutls_bye(gnutls_data->session, GNUTLS_SHUT_RDWR);
|
|
101
|
|
102 gnutls_deinit(gnutls_data->session);
|
|
103 // gnutls_certificate_free_credentials(gnutls_data->xcred);
|
|
104
|
|
105 g_free(gnutls_data);
|
|
106 }
|
|
107
|
|
108 static size_t
|
|
109 ssl_gnutls_read(GaimSslConnection *gsc, void *data, size_t len)
|
|
110 {
|
|
111 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc);
|
|
112 int s;
|
|
113
|
|
114 s = gnutls_record_recv(gnutls_data->session, data, len);
|
|
115
|
|
116 if (s < 0)
|
|
117 s = 0;
|
|
118
|
|
119 gaim_debug_misc("gnutls", "s = %d\n", s);
|
|
120
|
|
121 return s;
|
|
122 }
|
|
123
|
|
124 static size_t
|
|
125 ssl_gnutls_write(GaimSslConnection *gsc, const void *data, size_t len)
|
|
126 {
|
|
127 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc);
|
|
128 size_t s;
|
|
129
|
|
130 gaim_debug_misc("gnutls", "Writing: {%s}\n", data);
|
|
131
|
|
132 s = gnutls_record_send(gnutls_data->session, data, len);
|
|
133 }
|
|
134
|
|
135 static GaimSslOps ssl_ops =
|
|
136 {
|
|
137 ssl_gnutls_init,
|
|
138 ssl_gnutls_uninit,
|
|
139 ssl_gnutls_connect_cb,
|
|
140 ssl_gnutls_close,
|
|
141 ssl_gnutls_read,
|
|
142 ssl_gnutls_write
|
|
143 };
|
|
144
|
|
145 GaimSslOps *
|
|
146 gaim_ssl_gnutls_get_ops()
|
|
147 {
|
|
148 return &ssl_ops;
|
|
149 }
|