Mercurial > pidgin.yaz
annotate src/ssl-gnutls.c @ 7008:ecce6b23ea25
[gaim-migrate @ 7565]
plug a big memleak, and simplify some code in the process
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Mon, 29 Sep 2003 04:30:35 +0000 |
parents | 6d0d4e9149b9 |
children |
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 | |
63 ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
64 { | |
65 GaimSslConnection *gsc = (GaimSslConnection *)data; | |
66 GaimSslGnutlsData *gnutls_data; | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
67 static const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 }; |
6738 | 68 int ret; |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
69 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
70 if (source < 0) |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
71 return; |
6738 | 72 |
73 gsc->fd = source; | |
74 | |
75 gnutls_data = g_new0(GaimSslGnutlsData, 1); | |
76 gsc->private_data = gnutls_data; | |
77 | |
78 gnutls_init(&gnutls_data->session, GNUTLS_CLIENT); | |
79 gnutls_set_default_priority(gnutls_data->session); | |
80 | |
81 gnutls_certificate_type_set_priority(gnutls_data->session, | |
82 cert_type_priority); | |
83 | |
84 gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE, | |
85 xcred); | |
86 | |
87 gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(source)); | |
88 | |
89 gaim_debug_info("gnutls", "Handshaking\n"); | |
90 ret = gnutls_handshake(gnutls_data->session); | |
91 | |
92 if (ret < 0) | |
93 { | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
94 gaim_debug_error("gnutls", "Handshake failed\n"); |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
95 |
6764 | 96 /* XXX: notify the guy expecting the callback somehow? */ |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
97 gaim_ssl_close(gsc); |
6738 | 98 } |
99 else | |
100 { | |
6764 | 101 gsc->connect_cb(gsc->connect_cb_data, gsc, cond); |
6738 | 102 } |
103 } | |
104 | |
105 static void | |
106 ssl_gnutls_close(GaimSslConnection *gsc) | |
107 { | |
108 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
109 | |
110 gnutls_bye(gnutls_data->session, GNUTLS_SHUT_RDWR); | |
111 | |
112 gnutls_deinit(gnutls_data->session); | |
113 | |
114 g_free(gnutls_data); | |
115 } | |
116 | |
117 static size_t | |
118 ssl_gnutls_read(GaimSslConnection *gsc, void *data, size_t len) | |
119 { | |
120 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
121 int s; | |
122 | |
123 s = gnutls_record_recv(gnutls_data->session, data, len); | |
124 | |
125 if (s < 0) | |
126 s = 0; | |
127 | |
128 return s; | |
129 } | |
130 | |
131 static size_t | |
132 ssl_gnutls_write(GaimSslConnection *gsc, const void *data, size_t len) | |
133 { | |
134 GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
135 size_t s; | |
136 | |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
137 s = gnutls_record_send(gnutls_data->session, data, len); |
6738 | 138 |
6758
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
139 if (s < 0) |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
140 s = 0; |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
141 |
424647996866
[gaim-migrate @ 7290]
Christian Hammond <chipx86@chipx86.com>
parents:
6747
diff
changeset
|
142 return s; |
6738 | 143 } |
144 | |
145 static GaimSslOps ssl_ops = | |
146 { | |
147 ssl_gnutls_init, | |
148 ssl_gnutls_uninit, | |
149 ssl_gnutls_connect_cb, | |
150 ssl_gnutls_close, | |
151 ssl_gnutls_read, | |
152 ssl_gnutls_write | |
153 }; | |
154 | |
155 GaimSslOps * | |
156 gaim_ssl_gnutls_get_ops() | |
157 { | |
158 return &ssl_ops; | |
159 } | |
6747
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
160 |
82348b5ab87e
[gaim-migrate @ 7279]
Christian Hammond <chipx86@chipx86.com>
parents:
6738
diff
changeset
|
161 #endif /* HAVE_GNUTLS */ |