Mercurial > pidgin.yaz
annotate src/ssl-gnutls.c @ 6758:424647996866
[gaim-migrate @ 7290]
SSL in GNUTLS no longer blocks everything. Now it uses the input watchers,
as it should.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 06 Sep 2003 00:47:24 +0000 |
parents | 82348b5ab87e |
children | 6d0d4e9149b9 |
rev | line source |
---|---|
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 */ | |
6747
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
23 #include "internal.h" |
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
24 |
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
25 #ifdef HAVE_GNUTLS |
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
26 |
6738 | 27 #include "debug.h" |
28 #include "sslconn.h" | |
29 | |
30 #include <gnutls/gnutls.h> | |
31 | |
32 typedef struct | |
33 { | |
34 gnutls_session session; | |
35 | |
36 } GaimSslGnutlsData; | |
37 | |
38 #define GAIM_SSL_GNUTLS_DATA(gsc) ((GaimSslGnutlsData *)gsc->private_data) | |
39 | |
40 static gnutls_certificate_client_credentials xcred; | |
41 | |
42 static gboolean | |
43 ssl_gnutls_init(void) | |
44 { | |
45 gnutls_global_init(); | |
46 | |
47 gnutls_certificate_allocate_credentials(&xcred); | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
48 gnutls_certificate_set_x509_trust_file(xcred, "ca.pem", |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
49 GNUTLS_X509_FMT_PEM); |
6738 | 50 |
51 return TRUE; | |
52 } | |
53 | |
54 static void | |
55 ssl_gnutls_uninit(void) | |
56 { | |
57 gnutls_global_deinit(); | |
58 | |
59 gnutls_certificate_free_credentials(xcred); | |
60 } | |
61 | |
62 static void | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
63 input_func(gpointer data, gint source, GaimInputCondition cond) |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
64 { |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
65 GaimSslConnection *gsc = (GaimSslConnection *)data; |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
66 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
67 gaim_debug_misc("gnutls", "In input_func\n"); |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
68 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
69 gsc->input_func(gsc->user_data, gsc, cond); |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
70 } |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
71 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
72 static void |
6738 | 73 ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond) |
74 { | |
75 GaimSslConnection *gsc = (GaimSslConnection *)data; | |
76 GaimSslGnutlsData *gnutls_data; | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
77 static const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 }; |
6738 | 78 int ret; |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
79 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
80 if (source < 0) |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
81 return; |
6738 | 82 |
83 gsc->fd = source; | |
84 | |
85 gnutls_data = g_new0(GaimSslGnutlsData, 1); | |
86 gsc->private_data = gnutls_data; | |
87 | |
88 gnutls_init(&gnutls_data->session, GNUTLS_CLIENT); | |
89 gnutls_set_default_priority(gnutls_data->session); | |
90 | |
91 gnutls_certificate_type_set_priority(gnutls_data->session, | |
92 cert_type_priority); | |
93 | |
94 gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE, | |
95 xcred); | |
96 | |
97 gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(source)); | |
98 | |
99 gaim_debug_info("gnutls", "Handshaking\n"); | |
100 ret = gnutls_handshake(gnutls_data->session); | |
101 | |
102 if (ret < 0) | |
103 { | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
104 gaim_debug_error("gnutls", "Handshake failed\n"); |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
105 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
106 gaim_ssl_close(gsc); |
6738 | 107 } |
108 else | |
109 { | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
110 gaim_debug_info("gnutls", "Adding input handler.\n"); |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
111 gsc->inpa = gaim_input_add(gsc->fd, |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
112 GAIM_INPUT_READ | GAIM_INPUT_WRITE, |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
113 input_func, gsc); |
6738 | 114 } |
115 } | |
116 | |
117 static void | |
118 ssl_gnutls_close(GaimSslConnection *gsc) | |
119 { | |
120 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
121 | |
122 gnutls_bye(gnutls_data->session, GNUTLS_SHUT_RDWR); | |
123 | |
124 gnutls_deinit(gnutls_data->session); | |
125 | |
126 g_free(gnutls_data); | |
127 } | |
128 | |
129 static size_t | |
130 ssl_gnutls_read(GaimSslConnection *gsc, void *data, size_t len) | |
131 { | |
132 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
133 int s; | |
134 | |
135 s = gnutls_record_recv(gnutls_data->session, data, len); | |
136 | |
137 if (s < 0) | |
138 s = 0; | |
139 | |
140 return s; | |
141 } | |
142 | |
143 static size_t | |
144 ssl_gnutls_write(GaimSslConnection *gsc, const void *data, size_t len) | |
145 { | |
146 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
147 size_t s; | |
148 | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
149 s = gnutls_record_send(gnutls_data->session, data, len); |
6738 | 150 |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
151 if (s < 0) |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
152 s = 0; |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
153 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
154 return s; |
6738 | 155 } |
156 | |
157 static GaimSslOps ssl_ops = | |
158 { | |
159 ssl_gnutls_init, | |
160 ssl_gnutls_uninit, | |
161 ssl_gnutls_connect_cb, | |
162 ssl_gnutls_close, | |
163 ssl_gnutls_read, | |
164 ssl_gnutls_write | |
165 }; | |
166 | |
167 GaimSslOps * | |
168 gaim_ssl_gnutls_get_ops() | |
169 { | |
170 return &ssl_ops; | |
171 } | |
6747
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
172 |
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
173 #endif /* HAVE_GNUTLS */ |