Mercurial > pidgin
annotate plugins/ssl/ssl-nss.c @ 10261:d4e9ff2edc4e
[gaim-migrate @ 11405]
This should fix segfault bug 1072604. Oops.
committer: Tailor Script <tailor@pidgin.im>
| author | Tim Ringenbach <marv@pidgin.im> |
|---|---|
| date | Thu, 25 Nov 2004 18:35:26 +0000 |
| parents | c143a3fac58d |
| children | 1668fcab5968 |
| rev | line source |
|---|---|
| 7016 | 1 /** |
| 2 * @file ssl-nss.c Mozilla NSS 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" |
| 9943 | 26 #include "version.h" |
| 7016 | 27 |
|
7029
b80c7a4dfae7
[gaim-migrate @ 7592]
Christian Hammond <chipx86@chipx86.com>
parents:
7028
diff
changeset
|
28 #define SSL_NSS_PLUGIN_ID "ssl-nss" |
| 7016 | 29 |
| 30 #ifdef HAVE_NSS | |
| 31 | |
|
9582
fec83fb0b6a9
[gaim-migrate @ 10425]
Christian Hammond <chipx86@chipx86.com>
parents:
8749
diff
changeset
|
32 #undef HAVE_LONG_LONG /* Make Mozilla less angry. If angry, Mozilla SMASH! */ |
|
fec83fb0b6a9
[gaim-migrate @ 10425]
Christian Hammond <chipx86@chipx86.com>
parents:
8749
diff
changeset
|
33 |
| 7016 | 34 #include <nspr.h> |
| 35 #include <private/pprio.h> | |
| 36 #include <nss.h> | |
| 37 #include <pk11func.h> | |
| 38 #include <prio.h> | |
| 39 #include <secerr.h> | |
| 40 #include <secmod.h> | |
| 41 #include <ssl.h> | |
| 42 #include <sslerr.h> | |
| 43 #include <sslproto.h> | |
| 44 | |
| 45 typedef struct | |
| 46 { | |
| 47 PRFileDesc *fd; | |
| 48 PRFileDesc *in; | |
| 49 | |
| 50 } GaimSslNssData; | |
| 51 | |
| 52 #define GAIM_SSL_NSS_DATA(gsc) ((GaimSslNssData *)gsc->private_data) | |
| 53 | |
| 54 static const PRIOMethods *_nss_methods = NULL; | |
| 55 static PRDescIdentity _identity; | |
| 56 | |
|
7993
495d3dbb4e84
[gaim-migrate @ 8670]
Christian Hammond <chipx86@chipx86.com>
parents:
7862
diff
changeset
|
57 static void |
| 7862 | 58 ssl_nss_init_nss(void) |
| 59 { | |
| 60 PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); | |
| 61 NSS_NoDB_Init(NULL); | |
| 62 | |
| 63 /* TODO: Fix this so autoconf does the work trying to find this lib. */ | |
| 64 SECMOD_AddNewModule("Builtins", | |
| 65 #ifndef _WIN32 | |
| 10245 | 66 BR_LIBDIR("/libnssckbi.so"), |
| 7862 | 67 #else |
| 68 "nssckbi.dll", | |
| 69 #endif | |
| 70 0, 0); | |
| 71 NSS_SetDomesticPolicy(); | |
| 72 | |
| 73 _identity = PR_GetUniqueIdentity("Gaim"); | |
| 74 _nss_methods = PR_GetDefaultIOMethods(); | |
| 75 } | |
| 76 | |
| 7016 | 77 static SECStatus |
| 78 ssl_auth_cert(void *arg, PRFileDesc *socket, PRBool checksig, | |
| 79 PRBool is_server) | |
| 80 { | |
| 81 return SECSuccess; | |
| 82 | |
| 83 #if 0 | |
| 84 CERTCertificate *cert; | |
| 85 void *pinArg; | |
| 86 SECStatus status; | |
| 87 | |
| 88 cert = SSL_PeerCertificate(socket); | |
| 89 pinArg = SSL_RevealPinArg(socket); | |
| 90 | |
| 91 status = CERT_VerifyCertNow((CERTCertDBHandle *)arg, cert, checksig, | |
| 92 certUsageSSLClient, pinArg); | |
| 93 | |
| 94 if (status != SECSuccess) { | |
| 95 gaim_debug_error("nss", "CERT_VerifyCertNow failed\n"); | |
| 96 CERT_DestroyCertificate(cert); | |
| 97 return status; | |
| 98 } | |
| 99 | |
| 100 CERT_DestroyCertificate(cert); | |
| 101 return SECSuccess; | |
| 102 #endif | |
| 103 } | |
| 104 | |
| 105 static SECStatus | |
| 106 ssl_bad_cert(void *arg, PRFileDesc *socket) | |
| 107 { | |
| 108 SECStatus status = SECFailure; | |
| 109 PRErrorCode err; | |
| 110 | |
| 111 if (arg == NULL) | |
| 112 return status; | |
| 113 | |
| 114 *(PRErrorCode *)arg = err = PORT_GetError(); | |
| 115 | |
| 116 switch (err) | |
| 117 { | |
| 118 case SEC_ERROR_INVALID_AVA: | |
| 119 case SEC_ERROR_INVALID_TIME: | |
| 120 case SEC_ERROR_BAD_SIGNATURE: | |
| 121 case SEC_ERROR_EXPIRED_CERTIFICATE: | |
| 122 case SEC_ERROR_UNKNOWN_ISSUER: | |
| 123 case SEC_ERROR_UNTRUSTED_CERT: | |
| 124 case SEC_ERROR_CERT_VALID: | |
| 125 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE: | |
| 126 case SEC_ERROR_CRL_EXPIRED: | |
| 127 case SEC_ERROR_CRL_BAD_SIGNATURE: | |
| 128 case SEC_ERROR_EXTENSION_VALUE_INVALID: | |
| 129 case SEC_ERROR_CA_CERT_INVALID: | |
| 130 case SEC_ERROR_CERT_USAGES_INVALID: | |
| 131 case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION: | |
| 132 status = SECSuccess; | |
| 133 break; | |
| 134 | |
| 135 default: | |
| 136 status = SECFailure; | |
| 137 break; | |
| 138 } | |
| 139 | |
| 140 gaim_debug_error("nss", "Bad certificate: %d\n"); | |
| 141 | |
| 142 return status; | |
| 143 } | |
| 144 | |
| 145 static gboolean | |
| 146 ssl_nss_init(void) | |
| 147 { | |
| 7862 | 148 return TRUE; |
| 7016 | 149 } |
| 150 | |
| 151 static void | |
| 152 ssl_nss_uninit(void) | |
| 153 { | |
| 154 PR_Cleanup(); | |
| 155 | |
| 156 _nss_methods = NULL; | |
| 157 } | |
| 158 | |
| 159 static void | |
| 160 ssl_nss_connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 161 { | |
| 162 GaimSslConnection *gsc = (GaimSslConnection *)data; | |
| 163 GaimSslNssData *nss_data = g_new0(GaimSslNssData, 1); | |
| 164 PRSocketOptionData socket_opt; | |
| 165 | |
| 166 gsc->private_data = nss_data; | |
| 167 | |
| 168 gsc->fd = source; | |
| 169 | |
| 170 nss_data->fd = PR_ImportTCPSocket(gsc->fd); | |
| 171 | |
| 172 if (nss_data->fd == NULL) | |
| 173 { | |
| 174 gaim_debug_error("nss", "nss_data->fd == NULL!\n"); | |
| 175 | |
| 8362 | 176 if (gsc->error_cb != NULL) |
| 177 gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data); | |
| 178 | |
| 7016 | 179 gaim_ssl_close((GaimSslConnection *)gsc); |
| 180 | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 socket_opt.option = PR_SockOpt_Nonblocking; | |
| 185 socket_opt.value.non_blocking = PR_FALSE; | |
| 186 | |
| 187 PR_SetSocketOption(nss_data->fd, &socket_opt); | |
| 188 | |
| 189 nss_data->in = SSL_ImportFD(NULL, nss_data->fd); | |
| 190 | |
| 191 if (nss_data->in == NULL) | |
| 192 { | |
| 193 gaim_debug_error("nss", "nss_data->in == NUL!\n"); | |
| 194 | |
| 8362 | 195 if (gsc->error_cb != NULL) |
| 196 gsc->error_cb(gsc, GAIM_SSL_CONNECT_FAILED, gsc->connect_cb_data); | |
| 197 | |
| 7016 | 198 gaim_ssl_close((GaimSslConnection *)gsc); |
| 199 | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 SSL_OptionSet(nss_data->in, SSL_SECURITY, PR_TRUE); | |
| 204 SSL_OptionSet(nss_data->in, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE); | |
| 205 | |
| 206 SSL_AuthCertificateHook(nss_data->in, | |
| 207 (SSLAuthCertificate)ssl_auth_cert, | |
| 208 (void *)CERT_GetDefaultCertDB()); | |
| 209 SSL_BadCertHook(nss_data->in, (SSLBadCertHandler)ssl_bad_cert, NULL); | |
| 210 | |
| 7157 | 211 if(gsc->host) |
| 212 SSL_SetURL(nss_data->in, gsc->host); | |
| 7016 | 213 |
| 214 SSL_ResetHandshake(nss_data->in, PR_FALSE); | |
| 215 | |
| 216 if (SSL_ForceHandshake(nss_data->in)) | |
| 217 { | |
| 218 gaim_debug_error("nss", "Handshake failed\n"); | |
| 219 | |
|
7274
448e39ace278
[gaim-migrate @ 7851]
Christian Hammond <chipx86@chipx86.com>
parents:
7157
diff
changeset
|
220 if (gsc->error_cb != NULL) |
| 8360 | 221 gsc->error_cb(gsc, GAIM_SSL_HANDSHAKE_FAILED, gsc->connect_cb_data); |
|
7274
448e39ace278
[gaim-migrate @ 7851]
Christian Hammond <chipx86@chipx86.com>
parents:
7157
diff
changeset
|
222 |
| 7016 | 223 gaim_ssl_close(gsc); |
| 224 | |
| 225 return; | |
| 226 } | |
| 227 | |
| 228 gsc->connect_cb(gsc->connect_cb_data, gsc, cond); | |
| 229 } | |
| 230 | |
| 231 static void | |
| 232 ssl_nss_close(GaimSslConnection *gsc) | |
| 233 { | |
| 234 GaimSslNssData *nss_data = GAIM_SSL_NSS_DATA(gsc); | |
| 235 | |
| 7467 | 236 if(!nss_data) |
| 237 return; | |
| 238 | |
| 7016 | 239 if (nss_data->in) PR_Close(nss_data->in); |
| 240 /* if (nss_data->fd) PR_Close(nss_data->fd); */ | |
| 241 | |
| 242 g_free(nss_data); | |
| 243 } | |
| 244 | |
| 245 static size_t | |
| 246 ssl_nss_read(GaimSslConnection *gsc, void *data, size_t len) | |
| 247 { | |
| 248 GaimSslNssData *nss_data = GAIM_SSL_NSS_DATA(gsc); | |
| 249 | |
| 250 return PR_Read(nss_data->in, data, len); | |
| 251 } | |
| 252 | |
| 253 static size_t | |
| 254 ssl_nss_write(GaimSslConnection *gsc, const void *data, size_t len) | |
| 255 { | |
| 256 GaimSslNssData *nss_data = GAIM_SSL_NSS_DATA(gsc); | |
| 257 | |
| 7467 | 258 if(!nss_data) |
| 259 return 0; | |
| 260 | |
| 7016 | 261 return PR_Write(nss_data->in, data, len); |
| 262 } | |
| 263 | |
| 264 static GaimSslOps ssl_ops = | |
| 265 { | |
| 266 ssl_nss_init, | |
| 267 ssl_nss_uninit, | |
| 268 ssl_nss_connect_cb, | |
| 269 ssl_nss_close, | |
| 270 ssl_nss_read, | |
| 271 ssl_nss_write | |
| 272 }; | |
| 273 | |
| 274 #endif /* HAVE_NSS */ | |
| 275 | |
| 276 | |
| 277 static gboolean | |
| 278 plugin_load(GaimPlugin *plugin) | |
| 279 { | |
| 280 #ifdef HAVE_NSS | |
| 7862 | 281 if (!gaim_ssl_get_ops()) { |
| 282 gaim_ssl_set_ops(&ssl_ops); | |
| 283 } | |
| 7016 | 284 |
| 7862 | 285 /* Init NSS now, so others can use it even if sslconn never does */ |
| 286 ssl_nss_init_nss(); | |
| 7016 | 287 return TRUE; |
| 288 #else | |
| 289 return FALSE; | |
| 290 #endif | |
| 291 } | |
| 292 | |
| 293 static gboolean | |
| 294 plugin_unload(GaimPlugin *plugin) | |
| 295 { | |
| 7053 | 296 #ifdef HAVE_NSS |
| 7862 | 297 if (gaim_ssl_get_ops() == &ssl_ops) { |
| 298 gaim_ssl_set_ops(NULL); | |
| 299 } | |
|
7050
e8cd8827fb25
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7029
diff
changeset
|
300 #endif |
|
e8cd8827fb25
[gaim-migrate @ 7613]
Christian Hammond <chipx86@chipx86.com>
parents:
7029
diff
changeset
|
301 |
| 7016 | 302 return TRUE; |
| 303 } | |
| 304 | |
| 305 static GaimPluginInfo info = | |
| 306 { | |
| 9943 | 307 GAIM_PLUGIN_MAGIC, |
| 308 GAIM_MAJOR_VERSION, | |
| 309 GAIM_MINOR_VERSION, | |
| 7016 | 310 GAIM_PLUGIN_STANDARD, /**< type */ |
| 311 NULL, /**< ui_requirement */ | |
| 312 GAIM_PLUGIN_FLAG_INVISIBLE, /**< flags */ | |
| 313 NULL, /**< dependencies */ | |
| 314 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
| 315 | |
|
7029
b80c7a4dfae7
[gaim-migrate @ 7592]
Christian Hammond <chipx86@chipx86.com>
parents:
7028
diff
changeset
|
316 SSL_NSS_PLUGIN_ID, /**< id */ |
| 7016 | 317 N_("NSS"), /**< name */ |
| 318 VERSION, /**< version */ | |
| 319 /** summary */ | |
| 320 N_("Provides SSL support through Mozilla NSS."), | |
| 321 /** description */ | |
| 322 N_("Provides SSL support through Mozilla NSS."), | |
| 323 "Christian Hammond <chipx86@gnupdate.org>", | |
| 324 GAIM_WEBSITE, /**< homepage */ | |
| 325 | |
| 326 plugin_load, /**< load */ | |
| 327 plugin_unload, /**< unload */ | |
| 328 NULL, /**< destroy */ | |
| 329 | |
| 330 NULL, /**< ui_info */ | |
| 331 NULL /**< extra_info */ | |
| 332 }; | |
| 333 | |
| 334 static void | |
| 335 init_plugin(GaimPlugin *plugin) | |
| 336 { | |
| 337 } | |
| 338 | |
| 339 GAIM_INIT_PLUGIN(ssl_nss, init_plugin, info) |
