Mercurial > pidgin
annotate plugins/ssl/ssl-gnutls.c @ 8369:27c0cb0d8d07
[gaim-migrate @ 9096]
that was dumb
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sun, 29 Feb 2004 15:50:00 +0000 |
parents | 1976914caa51 |
children | 1556970088d4 |
rev | line source |
---|---|
7016 | 1 /** |
2 * @file ssl-gnutls.c GNUTLS SSL plugin. | |
3 * | |
4 * gaim | |
5 * | |
6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 #include "internal.h" | |
7051
e885d4963a68
[gaim-migrate @ 7614]
Christian Hammond <chipx86@chipx86.com>
parents:
7050
diff
changeset
|
23 #include "debug.h" |
7016 | 24 #include "plugin.h" |
7051
e885d4963a68
[gaim-migrate @ 7614]
Christian Hammond <chipx86@chipx86.com>
parents:
7050
diff
changeset
|
25 #include "sslconn.h" |
7016 | 26 |
27 #define SSL_GNUTLS_PLUGIN_ID "ssl-gnutls" | |
28 | |
29 #ifdef HAVE_GNUTLS | |
30 | |
31 #include <gnutls/gnutls.h> | |
32 | |
33 typedef struct | |
34 { | |
35 gnutls_session session; | |
36 | |
37 } GaimSslGnutlsData; | |
38 | |
39 #define GAIM_SSL_GNUTLS_DATA(gsc) ((GaimSslGnutlsData *)gsc->private_data) | |
40 | |
41 static gnutls_certificate_client_credentials xcred; | |
42 | |
7862 | 43 static void |
44 ssl_gnutls_init_gnutls(void) | |
7016 | 45 { |
46 gnutls_global_init(); | |
47 | |
48 gnutls_certificate_allocate_credentials(&xcred); | |
49 gnutls_certificate_set_x509_trust_file(xcred, "ca.pem", | |
50 GNUTLS_X509_FMT_PEM); | |
7862 | 51 } |
7016 | 52 |
7862 | 53 static gboolean |
54 ssl_gnutls_init(void) | |
55 { | |
56 return TRUE; | |
7016 | 57 } |
58 | |
59 static void | |
60 ssl_gnutls_uninit(void) | |
61 { | |
62 gnutls_global_deinit(); | |
63 | |
64 gnutls_certificate_free_credentials(xcred); | |
65 } | |
66 | |
67 static void | |
68 ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
69 { | |
70 GaimSslConnection *gsc = (GaimSslConnection *)data; | |
71 GaimSslGnutlsData *gnutls_data; | |
72 static const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 }; | |
73 int ret; | |
74 | |
8362 | 75 if (source < 0) { |
76 if(gsc->error_cb != NULL) | |
77 gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data); | |
78 | |
79 gaim_ssl_close(gsc); | |
7016 | 80 return; |
8362 | 81 } |
7016 | 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"); | |
7834
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
100 |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
101 do |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
102 { |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
103 ret = gnutls_handshake(gnutls_data->session); |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
104 } |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
105 while ((ret == GNUTLS_E_AGAIN) || (ret == GNUTLS_E_INTERRUPTED)); |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
106 |
7016 | 107 if (ret < 0) |
108 { | |
7325
35e652831230
[gaim-migrate @ 7911]
Christian Hammond <chipx86@chipx86.com>
parents:
7274
diff
changeset
|
109 gaim_debug_error("gnutls", "Handshake failed. Error %d\n", ret); |
7016 | 110 |
7274
448e39ace278
[gaim-migrate @ 7851]
Christian Hammond <chipx86@chipx86.com>
parents:
7051
diff
changeset
|
111 if (gsc->error_cb != NULL) |
7481
5f0f9d7fba6a
[gaim-migrate @ 8094]
Christian Hammond <chipx86@chipx86.com>
parents:
7467
diff
changeset
|
112 gsc->error_cb(gsc, GAIM_SSL_HANDSHAKE_FAILED, |
5f0f9d7fba6a
[gaim-migrate @ 8094]
Christian Hammond <chipx86@chipx86.com>
parents:
7467
diff
changeset
|
113 gsc->connect_cb_data); |
7274
448e39ace278
[gaim-migrate @ 7851]
Christian Hammond <chipx86@chipx86.com>
parents:
7051
diff
changeset
|
114 |
7016 | 115 gaim_ssl_close(gsc); |
116 } | |
117 else | |
118 { | |
8369 | 119 gaim_debug_info("gnutls", "Handshake complete\n"); |
120 | |
7016 | 121 gsc->connect_cb(gsc->connect_cb_data, gsc, cond); |
122 } | |
123 } | |
124 | |
125 static void | |
126 ssl_gnutls_close(GaimSslConnection *gsc) | |
127 { | |
128 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
129 | |
7467 | 130 if(!gnutls_data) |
131 return; | |
132 | |
7016 | 133 gnutls_bye(gnutls_data->session, GNUTLS_SHUT_RDWR); |
134 | |
135 gnutls_deinit(gnutls_data->session); | |
136 | |
137 g_free(gnutls_data); | |
138 } | |
139 | |
140 static size_t | |
141 ssl_gnutls_read(GaimSslConnection *gsc, void *data, size_t len) | |
142 { | |
143 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
144 int s; | |
145 | |
7834
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
146 do |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
147 { |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
148 s = gnutls_record_recv(gnutls_data->session, data, len); |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
149 } |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
150 while ((s == GNUTLS_E_AGAIN) || (s == GNUTLS_E_INTERRUPTED)); |
7016 | 151 |
152 if (s < 0) | |
7834
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
153 { |
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
154 gaim_debug_error("gnutls", "receive failed: %d\n", s); |
7016 | 155 s = 0; |
7834
99ffabc6ce73
[gaim-migrate @ 8487]
Christian Hammond <chipx86@chipx86.com>
parents:
7631
diff
changeset
|
156 } |
7016 | 157 |
158 return s; | |
159 } | |
160 | |
161 static size_t | |
162 ssl_gnutls_write(GaimSslConnection *gsc, const void *data, size_t len) | |
163 { | |
164 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
7467 | 165 size_t s = 0; |
7016 | 166 |
7467 | 167 if(gnutls_data) |
168 s = gnutls_record_send(gnutls_data->session, data, len); | |
7016 | 169 |
170 return s; | |
171 } | |
172 | |
173 static GaimSslOps ssl_ops = | |
174 { | |
175 ssl_gnutls_init, | |
176 ssl_gnutls_uninit, | |
177 ssl_gnutls_connect_cb, | |
178 ssl_gnutls_close, | |
179 ssl_gnutls_read, | |
180 ssl_gnutls_write | |
181 }; | |
182 | |
183 #endif /* HAVE_GNUTLS */ | |
184 | |
185 static gboolean | |
186 plugin_load(GaimPlugin *plugin) | |
187 { | |
188 #ifdef HAVE_GNUTLS | |
7862 | 189 if (!gaim_ssl_get_ops()) { |
190 gaim_ssl_set_ops(&ssl_ops); | |
191 } | |
7016 | 192 |
7862 | 193 /* Init GNUTLS now so others can use it even if sslconn never does */ |
194 ssl_gnutls_init_gnutls(); | |
7016 | 195 return TRUE; |
196 #else | |
197 return FALSE; | |
198 #endif | |
199 } | |
200 | |
201 static gboolean | |
202 plugin_unload(GaimPlugin *plugin) | |
203 { | |
7050
e8cd8827fb25
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
204 #ifdef HAVE_GNUTLS |
7862 | 205 if (gaim_ssl_get_ops() == &ssl_ops) { |
206 gaim_ssl_set_ops(NULL); | |
207 } | |
7050
e8cd8827fb25
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
208 #endif |
e8cd8827fb25
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
209 |
7016 | 210 return TRUE; |
211 } | |
212 | |
213 static GaimPluginInfo info = | |
214 { | |
215 2, /**< api_version */ | |
216 GAIM_PLUGIN_STANDARD, /**< type */ | |
217 NULL, /**< ui_requirement */ | |
218 GAIM_PLUGIN_FLAG_INVISIBLE, /**< flags */ | |
219 NULL, /**< dependencies */ | |
220 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
221 | |
222 SSL_GNUTLS_PLUGIN_ID, /**< id */ | |
223 N_("GNUTLS"), /**< name */ | |
224 VERSION, /**< version */ | |
225 /** summary */ | |
226 N_("Provides SSL support through GNUTLS."), | |
227 /** description */ | |
228 N_("Provides SSL support through GNUTLS."), | |
229 "Christian Hammond <chipx86@gnupdate.org>", | |
230 GAIM_WEBSITE, /**< homepage */ | |
231 | |
232 plugin_load, /**< load */ | |
233 plugin_unload, /**< unload */ | |
234 NULL, /**< destroy */ | |
235 | |
236 NULL, /**< ui_info */ | |
237 NULL /**< extra_info */ | |
238 }; | |
239 | |
240 static void | |
241 init_plugin(GaimPlugin *plugin) | |
242 { | |
243 } | |
244 | |
245 GAIM_INIT_PLUGIN(ssl_gnutls, init_plugin, info) |