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