Tue, 30 Sep 2003 02:42:18 +0000
[gaim-migrate @ 7614]
Warnings are bad, m'kay?
| 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
8ddb8f560399
[gaim-migrate @ 7614]
Christian Hammond <chipx86@chipx86.com>
parents:
7050
diff
changeset
|
23 | #include "debug.h" |
| 7016 | 24 | #include "plugin.h" |
|
7051
8ddb8f560399
[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 | ||
| 43 | static gboolean | |
| 44 | ssl_gnutls_init(void) | |
| 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); | |
| 51 | ||
| 52 | return TRUE; | |
| 53 | } | |
| 54 | ||
| 55 | static void | |
| 56 | ssl_gnutls_uninit(void) | |
| 57 | { | |
| 58 | gnutls_global_deinit(); | |
| 59 | ||
| 60 | gnutls_certificate_free_credentials(xcred); | |
| 61 | } | |
| 62 | ||
| 63 | static void | |
| 64 | ssl_gnutls_connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 65 | { | |
| 66 | GaimSslConnection *gsc = (GaimSslConnection *)data; | |
| 67 | GaimSslGnutlsData *gnutls_data; | |
| 68 | static const int cert_type_priority[2] = { GNUTLS_CRT_X509, 0 }; | |
| 69 | int ret; | |
| 70 | ||
| 71 | if (source < 0) | |
| 72 | return; | |
| 73 | ||
| 74 | gsc->fd = source; | |
| 75 | ||
| 76 | gnutls_data = g_new0(GaimSslGnutlsData, 1); | |
| 77 | gsc->private_data = gnutls_data; | |
| 78 | ||
| 79 | gnutls_init(&gnutls_data->session, GNUTLS_CLIENT); | |
| 80 | gnutls_set_default_priority(gnutls_data->session); | |
| 81 | ||
| 82 | gnutls_certificate_type_set_priority(gnutls_data->session, | |
| 83 | cert_type_priority); | |
| 84 | ||
| 85 | gnutls_credentials_set(gnutls_data->session, GNUTLS_CRD_CERTIFICATE, | |
| 86 | xcred); | |
| 87 | ||
| 88 | gnutls_transport_set_ptr(gnutls_data->session, GINT_TO_POINTER(source)); | |
| 89 | ||
| 90 | gaim_debug_info("gnutls", "Handshaking\n"); | |
| 91 | ret = gnutls_handshake(gnutls_data->session); | |
| 92 | ||
| 93 | if (ret < 0) | |
| 94 | { | |
| 95 | gaim_debug_error("gnutls", "Handshake failed\n"); | |
| 96 | ||
| 97 | /* XXX: notify the guy expecting the callback somehow? */ | |
| 98 | gaim_ssl_close(gsc); | |
| 99 | } | |
| 100 | else | |
| 101 | { | |
| 102 | gsc->connect_cb(gsc->connect_cb_data, gsc, cond); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | static void | |
| 107 | ssl_gnutls_close(GaimSslConnection *gsc) | |
| 108 | { | |
| 109 | GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
| 110 | ||
| 111 | gnutls_bye(gnutls_data->session, GNUTLS_SHUT_RDWR); | |
| 112 | ||
| 113 | gnutls_deinit(gnutls_data->session); | |
| 114 | ||
| 115 | g_free(gnutls_data); | |
| 116 | } | |
| 117 | ||
| 118 | static size_t | |
| 119 | ssl_gnutls_read(GaimSslConnection *gsc, void *data, size_t len) | |
| 120 | { | |
| 121 | GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
| 122 | int s; | |
| 123 | ||
| 124 | s = gnutls_record_recv(gnutls_data->session, data, len); | |
| 125 | ||
| 126 | if (s < 0) | |
| 127 | s = 0; | |
| 128 | ||
| 129 | return s; | |
| 130 | } | |
| 131 | ||
| 132 | static size_t | |
| 133 | ssl_gnutls_write(GaimSslConnection *gsc, const void *data, size_t len) | |
| 134 | { | |
| 135 | GaimSslGnutlsData *gnutls_data = GAIM_SSL_GNUTLS_DATA(gsc); | |
| 136 | size_t s; | |
| 137 | ||
| 138 | s = gnutls_record_send(gnutls_data->session, data, len); | |
| 139 | ||
| 140 | if (s < 0) | |
| 141 | s = 0; | |
| 142 | ||
| 143 | return s; | |
| 144 | } | |
| 145 | ||
| 146 | static GaimSslOps ssl_ops = | |
| 147 | { | |
| 148 | ssl_gnutls_init, | |
| 149 | ssl_gnutls_uninit, | |
| 150 | ssl_gnutls_connect_cb, | |
| 151 | ssl_gnutls_close, | |
| 152 | ssl_gnutls_read, | |
| 153 | ssl_gnutls_write | |
| 154 | }; | |
| 155 | ||
| 156 | #endif /* HAVE_GNUTLS */ | |
| 157 | ||
| 158 | static gboolean | |
| 159 | plugin_load(GaimPlugin *plugin) | |
| 160 | { | |
| 161 | #ifdef HAVE_GNUTLS | |
| 162 | gaim_ssl_set_ops(&ssl_ops); | |
| 163 | ||
| 164 | return TRUE; | |
| 165 | #else | |
| 166 | return FALSE; | |
| 167 | #endif | |
| 168 | } | |
| 169 | ||
| 170 | static gboolean | |
| 171 | plugin_unload(GaimPlugin *plugin) | |
| 172 | { | |
|
7050
12730863b0f9
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
173 | #ifdef HAVE_GNUTLS |
|
12730863b0f9
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
174 | gaim_ssl_set_ops(NULL); |
|
12730863b0f9
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
175 | #endif |
|
12730863b0f9
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7016
diff
changeset
|
176 | |
| 7016 | 177 | return TRUE; |
| 178 | } | |
| 179 | ||
| 180 | static GaimPluginInfo info = | |
| 181 | { | |
| 182 | 2, /**< api_version */ | |
| 183 | GAIM_PLUGIN_STANDARD, /**< type */ | |
| 184 | NULL, /**< ui_requirement */ | |
| 185 | GAIM_PLUGIN_FLAG_INVISIBLE, /**< flags */ | |
| 186 | NULL, /**< dependencies */ | |
| 187 | GAIM_PRIORITY_DEFAULT, /**< priority */ | |
| 188 | ||
| 189 | SSL_GNUTLS_PLUGIN_ID, /**< id */ | |
| 190 | N_("GNUTLS"), /**< name */ | |
| 191 | VERSION, /**< version */ | |
| 192 | /** summary */ | |
| 193 | N_("Provides SSL support through GNUTLS."), | |
| 194 | /** description */ | |
| 195 | N_("Provides SSL support through GNUTLS."), | |
| 196 | "Christian Hammond <chipx86@gnupdate.org>", | |
| 197 | GAIM_WEBSITE, /**< homepage */ | |
| 198 | ||
| 199 | plugin_load, /**< load */ | |
| 200 | plugin_unload, /**< unload */ | |
| 201 | NULL, /**< destroy */ | |
| 202 | ||
| 203 | NULL, /**< ui_info */ | |
| 204 | NULL /**< extra_info */ | |
| 205 | }; | |
| 206 | ||
| 207 | static void | |
| 208 | init_plugin(GaimPlugin *plugin) | |
| 209 | { | |
| 210 | } | |
| 211 | ||
| 212 | GAIM_INIT_PLUGIN(ssl_gnutls, init_plugin, info) |