2018-11-30 18:39:55 +03:00
|
|
|
/* vim:set ts=4 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-08-18 13:38:39 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// GSSAPI Authentication Support Module
|
|
|
|
//
|
|
|
|
// Described by IETF Internet draft: draft-brezak-kerberos-http-00.txt
|
|
|
|
// (formerly draft-brezak-spnego-http-04.txt)
|
|
|
|
//
|
|
|
|
// Also described here:
|
|
|
|
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/http-sso-1.asp
|
|
|
|
//
|
2005-08-18 13:38:44 +04:00
|
|
|
//
|
|
|
|
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
2011-10-11 09:50:08 +04:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIPrefService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2017-12-07 06:36:57 +03:00
|
|
|
#include "nsMemory.h"
|
2005-08-18 19:22:33 +04:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2018-02-24 19:32:37 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/SharedLibrary.h"
|
2013-07-25 20:54:11 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2005-08-18 13:38:39 +04:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
#include "nsAuthGSSAPI.h"
|
2005-08-18 13:38:39 +04:00
|
|
|
|
2005-08-18 13:38:42 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
# include <Kerberos/Kerberos.h>
|
|
|
|
#endif
|
|
|
|
|
2005-08-18 13:38:45 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
typedef KLStatus (*KLCacheHasValidTickets_type)(KLPrincipal, KLKerberosVersion,
|
2019-05-01 11:47:10 +03:00
|
|
|
KLBoolean*, KLPrincipal*,
|
|
|
|
char**);
|
2005-08-18 13:38:45 +04:00
|
|
|
#endif
|
|
|
|
|
2009-10-30 12:13:34 +03:00
|
|
|
#if defined(HAVE_RES_NINIT)
|
2013-11-19 18:42:55 +04:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <netinet/in.h>
|
|
|
|
# include <arpa/nameser.h>
|
2009-10-30 12:13:34 +03:00
|
|
|
# include <resolv.h>
|
|
|
|
#endif
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
// We define GSS_C_NT_HOSTBASED_SERVICE explicitly since it may be referenced
|
|
|
|
// by by a different name depending on the implementation of gss but always
|
|
|
|
// has the same value
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
static gss_OID_desc gss_c_nt_hostbased_service = {
|
2019-05-01 11:47:10 +03:00
|
|
|
10, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x01\x04"};
|
2005-08-18 13:38:44 +04:00
|
|
|
|
|
|
|
static const char kNegotiateAuthGssLib[] = "network.negotiate-auth.gsslib";
|
2017-07-06 15:00:35 +03:00
|
|
|
static const char kNegotiateAuthNativeImp[] =
|
2005-08-18 13:38:44 +04:00
|
|
|
"network.negotiate-auth.using-native-gsslib";
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
static struct GSSFunction {
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* str;
|
2011-10-11 09:50:08 +04:00
|
|
|
PRFuncPtr func;
|
2013-04-03 05:06:20 +04:00
|
|
|
} gssFuncs[] = {{"gss_display_status", nullptr},
|
|
|
|
{"gss_init_sec_context", nullptr},
|
|
|
|
{"gss_indicate_mechs", nullptr},
|
|
|
|
{"gss_release_oid_set", nullptr},
|
|
|
|
{"gss_delete_sec_context", nullptr},
|
|
|
|
{"gss_import_name", nullptr},
|
|
|
|
{"gss_release_buffer", nullptr},
|
|
|
|
{"gss_release_name", nullptr},
|
|
|
|
{"gss_wrap", nullptr},
|
|
|
|
{"gss_unwrap", nullptr}};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gssNativeImp = true;
|
2019-05-01 11:47:10 +03:00
|
|
|
static PRLibrary* gssLibrary = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
#define gss_display_status_ptr ((gss_display_status_type)*gssFuncs[0].func)
|
|
|
|
#define gss_init_sec_context_ptr ((gss_init_sec_context_type)*gssFuncs[1].func)
|
|
|
|
#define gss_indicate_mechs_ptr ((gss_indicate_mechs_type)*gssFuncs[2].func)
|
|
|
|
#define gss_release_oid_set_ptr ((gss_release_oid_set_type)*gssFuncs[3].func)
|
|
|
|
#define gss_delete_sec_context_ptr \
|
|
|
|
((gss_delete_sec_context_type)*gssFuncs[4].func)
|
|
|
|
#define gss_import_name_ptr ((gss_import_name_type)*gssFuncs[5].func)
|
|
|
|
#define gss_release_buffer_ptr ((gss_release_buffer_type)*gssFuncs[6].func)
|
|
|
|
#define gss_release_name_ptr ((gss_release_name_type)*gssFuncs[7].func)
|
|
|
|
#define gss_wrap_ptr ((gss_wrap_type)*gssFuncs[8].func)
|
|
|
|
#define gss_unwrap_ptr ((gss_unwrap_type)*gssFuncs[9].func)
|
2005-08-18 13:38:44 +04:00
|
|
|
|
2005-08-18 13:38:45 +04:00
|
|
|
#ifdef XP_MACOSX
|
2005-08-18 13:38:46 +04:00
|
|
|
static PRFuncPtr KLCacheHasValidTicketsPtr;
|
2005-08-18 13:38:45 +04:00
|
|
|
# define KLCacheHasValidTickets_ptr \
|
|
|
|
((KLCacheHasValidTickets_type)*KLCacheHasValidTicketsPtr)
|
|
|
|
#endif
|
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
static nsresult gssInit() {
|
2018-02-24 19:32:37 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
nsAutoString libPathU;
|
|
|
|
Preferences::GetString(kNegotiateAuthGssLib, libPathU);
|
|
|
|
NS_ConvertUTF16toUTF8 libPath(libPathU);
|
|
|
|
#else
|
2017-10-27 02:30:33 +03:00
|
|
|
nsAutoCString libPath;
|
2018-02-24 19:32:37 +03:00
|
|
|
Preferences::GetCString(kNegotiateAuthGssLib, libPath);
|
|
|
|
#endif
|
|
|
|
gssNativeImp = Preferences::GetBool(kNegotiateAuthNativeImp);
|
2005-08-18 13:38:44 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
PRLibrary* lib = nullptr;
|
2005-08-18 13:38:44 +04:00
|
|
|
|
|
|
|
if (!libPath.IsEmpty()) {
|
|
|
|
LOG(("Attempting to load user specified library [%s]\n", libPath.get()));
|
2011-10-17 18:59:28 +04:00
|
|
|
gssNativeImp = false;
|
2018-02-24 19:32:37 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
lib = LoadLibraryWithFlags(libPathU.get());
|
|
|
|
#else
|
|
|
|
lib = LoadLibraryWithFlags(libPath.get());
|
|
|
|
#endif
|
2005-08-18 13:38:44 +04:00
|
|
|
} else {
|
2005-08-18 19:22:33 +04:00
|
|
|
#ifdef XP_WIN
|
2017-10-18 22:38:01 +03:00
|
|
|
# ifdef _WIN64
|
2018-02-24 19:32:37 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(kLibName, "gssapi64.dll");
|
2017-10-18 22:38:01 +03:00
|
|
|
# else
|
2018-02-24 19:32:37 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(kLibName, "gssapi32.dll");
|
2017-10-18 22:38:01 +03:00
|
|
|
# endif
|
|
|
|
|
2018-02-24 19:32:37 +03:00
|
|
|
lib = LoadLibraryWithFlags(kLibName.get());
|
2014-03-06 23:11:24 +04:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
/* OpenBSD doesn't register inter-library dependencies in basesystem
|
|
|
|
* libs therefor we need to load all the libraries gssapi depends on,
|
|
|
|
* in the correct order and with LD_GLOBAL for GSSAPI auth to work
|
|
|
|
* fine.
|
|
|
|
*/
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* const verLibNames[] = {
|
2014-03-06 23:11:24 +04:00
|
|
|
"libasn1.so", "libcrypto.so", "libroken.so", "libheimbase.so",
|
|
|
|
"libcom_err.so", "libkrb5.so", "libgssapi.so"};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-03-06 23:11:24 +04:00
|
|
|
PRLibSpec libSpec;
|
|
|
|
for (size_t i = 0; i < ArrayLength(verLibNames); ++i) {
|
|
|
|
libSpec.type = PR_LibSpec_Pathname;
|
|
|
|
libSpec.value.pathname = verLibNames[i];
|
|
|
|
lib = PR_LoadLibraryWithFlags(libSpec, PR_LD_GLOBAL);
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2014-03-06 23:11:24 +04:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
#else
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* const libNames[] = {"gss", "gssapi_krb5", "gssapi"};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* const verLibNames[] = {
|
2006-02-21 19:15:16 +03:00
|
|
|
"libgssapi_krb5.so.2", /* MIT - FC, Suse10, Debian */
|
|
|
|
"libgssapi.so.4", /* Heimdal - Suse10, MDK */
|
2014-03-06 23:11:24 +04:00
|
|
|
"libgssapi.so.1" /* Heimdal - Suse9, CITI - FC, MDK, Suse10*/
|
2005-08-18 13:38:44 +04:00
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(verLibNames) && !lib; ++i) {
|
2006-02-21 19:15:16 +03:00
|
|
|
lib = PR_LoadLibrary(verLibNames[i]);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2006-02-21 19:15:16 +03:00
|
|
|
/* The CITI libgssapi library calls exit() during
|
|
|
|
* initialization if it's not correctly configured. Try to
|
|
|
|
* ensure that we never use this library for our GSSAPI
|
|
|
|
* support, as its just a wrapper library, anyway.
|
|
|
|
* See Bugzilla #325433
|
|
|
|
*/
|
|
|
|
if (lib && PR_FindFunctionSymbol(lib, "internal_krb5_gss_initialize") &&
|
|
|
|
PR_FindFunctionSymbol(lib, "gssd_pname_to_uid")) {
|
|
|
|
LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
|
|
|
|
PR_UnloadLibrary(lib);
|
2013-04-03 05:06:20 +04:00
|
|
|
lib = nullptr;
|
2006-02-21 19:15:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(libNames) && !lib; ++i) {
|
2019-05-01 11:47:10 +03:00
|
|
|
char* libName = PR_GetLibraryName(nullptr, libNames[i]);
|
2005-08-18 13:38:44 +04:00
|
|
|
if (libName) {
|
|
|
|
lib = PR_LoadLibrary(libName);
|
|
|
|
PR_FreeLibraryName(libName);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2006-02-21 19:15:16 +03:00
|
|
|
if (lib && PR_FindFunctionSymbol(lib, "internal_krb5_gss_initialize") &&
|
|
|
|
PR_FindFunctionSymbol(lib, "gssd_pname_to_uid")) {
|
|
|
|
LOG(("CITI libgssapi found, which calls exit(). Skipping\n"));
|
|
|
|
PR_UnloadLibrary(lib);
|
2013-04-03 05:06:20 +04:00
|
|
|
lib = nullptr;
|
2005-08-18 13:38:44 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-08-18 13:38:44 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
#endif
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
if (!lib) {
|
|
|
|
LOG(("Fail to load gssapi library\n"));
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("Attempting to load gss functions\n"));
|
|
|
|
|
2011-10-11 09:50:08 +04:00
|
|
|
for (size_t i = 0; i < ArrayLength(gssFuncs); ++i) {
|
|
|
|
gssFuncs[i].func = PR_FindFunctionSymbol(lib, gssFuncs[i].str);
|
|
|
|
if (!gssFuncs[i].func) {
|
|
|
|
LOG(("Fail to load %s function from gssapi library\n", gssFuncs[i].str));
|
2005-08-18 13:38:44 +04:00
|
|
|
PR_UnloadLibrary(lib);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-08-18 13:38:45 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
if (gssNativeImp && !(KLCacheHasValidTicketsPtr = PR_FindFunctionSymbol(
|
|
|
|
lib, "KLCacheHasValidTickets"))) {
|
|
|
|
LOG(("Fail to load KLCacheHasValidTickets function from gssapi library\n"));
|
|
|
|
PR_UnloadLibrary(lib);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-18 13:38:44 +04:00
|
|
|
|
2007-04-16 02:22:58 +04:00
|
|
|
gssLibrary = lib;
|
2005-08-18 13:38:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
// Generate proper GSSAPI error messages from the major and
|
|
|
|
// minor status codes.
|
2019-05-01 11:47:10 +03:00
|
|
|
void LogGssError(OM_uint32 maj_stat, OM_uint32 min_stat, const char* prefix) {
|
2015-06-04 01:25:57 +03:00
|
|
|
if (!MOZ_LOG_TEST(gNegotiateLog, LogLevel::Debug)) {
|
2015-05-14 20:13:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
OM_uint32 new_stat;
|
|
|
|
OM_uint32 msg_ctx = 0;
|
|
|
|
gss_buffer_desc status1_string;
|
|
|
|
gss_buffer_desc status2_string;
|
|
|
|
OM_uint32 ret;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString errorStr;
|
2005-08-18 19:22:33 +04:00
|
|
|
errorStr.Assign(prefix);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-04-16 02:22:58 +04:00
|
|
|
if (!gssLibrary) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
errorStr += ": ";
|
2005-08-18 13:38:39 +04:00
|
|
|
do {
|
2005-08-18 13:38:44 +04:00
|
|
|
ret = gss_display_status_ptr(&new_stat, maj_stat, GSS_C_GSS_CODE,
|
|
|
|
GSS_C_NULL_OID, &msg_ctx, &status1_string);
|
2019-05-01 11:47:10 +03:00
|
|
|
errorStr.Append((const char*)status1_string.value, status1_string.length);
|
2005-08-18 19:22:33 +04:00
|
|
|
gss_release_buffer_ptr(&new_stat, &status1_string);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
errorStr += '\n';
|
2005-08-18 13:38:44 +04:00
|
|
|
ret = gss_display_status_ptr(&new_stat, min_stat, GSS_C_MECH_CODE,
|
|
|
|
GSS_C_NULL_OID, &msg_ctx, &status2_string);
|
2019-05-01 11:47:10 +03:00
|
|
|
errorStr.Append((const char*)status2_string.value, status2_string.length);
|
2005-08-18 19:22:33 +04:00
|
|
|
errorStr += '\n';
|
2005-08-18 13:38:39 +04:00
|
|
|
} while (!GSS_ERROR(ret) && msg_ctx != 0);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
LOG(("%s\n", errorStr.get()));
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
nsAuthGSSAPI::nsAuthGSSAPI(pType package) : mServiceFlags(REQ_DEFAULT) {
|
|
|
|
OM_uint32 minstat;
|
|
|
|
OM_uint32 majstat;
|
2005-08-18 13:38:39 +04:00
|
|
|
gss_OID_set mech_set;
|
|
|
|
gss_OID item;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
unsigned int i;
|
|
|
|
static gss_OID_desc gss_krb5_mech_oid_desc = {
|
2019-05-01 11:47:10 +03:00
|
|
|
9, (void*)"\x2a\x86\x48\x86\xf7\x12\x01\x02\x02"};
|
2005-08-18 13:38:39 +04:00
|
|
|
static gss_OID_desc gss_spnego_mech_oid_desc = {
|
2019-05-01 11:47:10 +03:00
|
|
|
6, (void*)"\x2b\x06\x01\x05\x05\x02"};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
LOG(("entering nsAuthGSSAPI::nsAuthGSSAPI()\n"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mComplete = false;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2007-04-16 02:22:58 +04:00
|
|
|
if (!gssLibrary && NS_FAILED(gssInit())) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
mCtx = GSS_C_NO_CONTEXT;
|
|
|
|
mMechOID = &gss_krb5_mech_oid_desc;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
// if the type is kerberos we accept it as default
|
2017-07-06 15:00:35 +03:00
|
|
|
// and exit
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
if (package == PACKAGE_TYPE_KERBEROS) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
// Now, look at the list of supported mechanisms,
|
|
|
|
// if SPNEGO is found, then use it.
|
|
|
|
// Otherwise, set the desired mechanism to
|
|
|
|
// GSS_C_NO_OID and let the system try to use
|
|
|
|
// the default mechanism.
|
|
|
|
//
|
|
|
|
// Using Kerberos directly (instead of negotiating
|
|
|
|
// with SPNEGO) may work in some cases depending
|
|
|
|
// on how smart the server side is.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
majstat = gss_indicate_mechs_ptr(&minstat, &mech_set);
|
2005-08-18 13:38:39 +04:00
|
|
|
if (GSS_ERROR(majstat)) return;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
if (mech_set) {
|
|
|
|
for (i = 0; i < mech_set->count; i++) {
|
2017-07-06 15:00:35 +03:00
|
|
|
item = &mech_set->elements[i];
|
2005-08-18 19:22:33 +04:00
|
|
|
if (item->length == gss_spnego_mech_oid_desc.length &&
|
|
|
|
!memcmp(item->elements, gss_spnego_mech_oid_desc.elements,
|
|
|
|
item->length)) {
|
|
|
|
// ok, we found it
|
|
|
|
mMechOID = &gss_spnego_mech_oid_desc;
|
|
|
|
break;
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
}
|
2005-08-18 19:22:33 +04:00
|
|
|
gss_release_oid_set_ptr(&minstat, &mech_set);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
void nsAuthGSSAPI::Reset() {
|
2007-04-16 02:22:58 +04:00
|
|
|
if (gssLibrary && mCtx != GSS_C_NO_CONTEXT) {
|
2005-08-18 13:38:39 +04:00
|
|
|
OM_uint32 minor_status;
|
2005-08-18 13:38:44 +04:00
|
|
|
gss_delete_sec_context_ptr(&minor_status, &mCtx, GSS_C_NO_BUFFER);
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
mCtx = GSS_C_NO_CONTEXT;
|
2011-10-17 18:59:28 +04:00
|
|
|
mComplete = false;
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:47 +03:00
|
|
|
/* static */
|
|
|
|
void nsAuthGSSAPI::Shutdown() {
|
2007-04-16 02:22:58 +04:00
|
|
|
if (gssLibrary) {
|
|
|
|
PR_UnloadLibrary(gssLibrary);
|
2012-07-30 18:20:58 +04:00
|
|
|
gssLibrary = nullptr;
|
2007-04-16 02:22:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-13 22:31:34 +04:00
|
|
|
/* Limitations apply to this class's thread safety. See the header file */
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsAuthGSSAPI, nsIAuthModule)
|
2005-08-18 13:38:39 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAuthGSSAPI::Init(const char* serviceName, uint32_t serviceFlags,
|
|
|
|
const char16_t* domain, const char16_t* username,
|
|
|
|
const char16_t* password) {
|
2005-08-18 13:38:39 +04:00
|
|
|
// we don't expect to be passed any user credentials
|
|
|
|
NS_ASSERTION(!domain && !username && !password, "unexpected credentials");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
// it's critial that the caller supply a service name to be used
|
|
|
|
NS_ENSURE_TRUE(serviceName && *serviceName, NS_ERROR_INVALID_ARG);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
LOG(("entering nsAuthGSSAPI::Init()\n"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
if (!gssLibrary) return NS_ERROR_NOT_INITIALIZED;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
mServiceName = serviceName;
|
|
|
|
mServiceFlags = serviceFlags;
|
2013-07-25 20:54:11 +04:00
|
|
|
|
|
|
|
static bool sTelemetrySent = false;
|
|
|
|
if (!sTelemetrySent) {
|
2013-10-10 21:10:45 +04:00
|
|
|
mozilla::Telemetry::Accumulate(mozilla::Telemetry::NTLM_MODULE_USED_2,
|
|
|
|
serviceFlags & nsIAuthModule::REQ_PROXY_AUTH
|
2013-07-25 20:54:11 +04:00
|
|
|
? NTLM_MODULE_KERBEROS_PROXY
|
|
|
|
: NTLM_MODULE_KERBEROS_DIRECT);
|
|
|
|
sTelemetrySent = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAuthGSSAPI::GetNextToken(const void* inToken, uint32_t inTokenLen,
|
|
|
|
void** outToken, uint32_t* outTokenLen) {
|
2005-08-18 13:38:39 +04:00
|
|
|
OM_uint32 major_status, minor_status;
|
|
|
|
OM_uint32 req_flags = 0;
|
|
|
|
gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_t in_token_ptr = GSS_C_NO_BUFFER;
|
|
|
|
gss_name_t server;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString userbuf;
|
2005-08-18 19:22:33 +04:00
|
|
|
nsresult rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
LOG(("entering nsAuthGSSAPI::GetNextToken()\n"));
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
if (!gssLibrary) return NS_ERROR_NOT_INITIALIZED;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
// If they've called us again after we're complete, reset to start afresh.
|
|
|
|
if (mComplete) Reset();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
if (mServiceFlags & REQ_DELEGATE) req_flags |= GSS_C_DELEG_FLAG;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
if (mServiceFlags & REQ_MUTUAL_AUTH) req_flags |= GSS_C_MUTUAL_FLAG;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
input_token.value = (void*)mServiceName.get();
|
2005-08-18 13:38:39 +04:00
|
|
|
input_token.length = mServiceName.Length() + 1;
|
|
|
|
|
2009-10-30 12:13:34 +03:00
|
|
|
#if defined(HAVE_RES_NINIT)
|
|
|
|
res_ninit(&_res);
|
|
|
|
#endif
|
2005-08-18 13:38:44 +04:00
|
|
|
major_status = gss_import_name_ptr(&minor_status, &input_token,
|
|
|
|
&gss_c_nt_hostbased_service, &server);
|
2013-04-03 05:06:20 +04:00
|
|
|
input_token.value = nullptr;
|
2005-08-18 13:38:39 +04:00
|
|
|
input_token.length = 0;
|
|
|
|
if (GSS_ERROR(major_status)) {
|
|
|
|
LogGssError(major_status, minor_status, "gss_import_name() failed");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
if (inToken) {
|
|
|
|
input_token.length = inTokenLen;
|
2019-05-01 11:47:10 +03:00
|
|
|
input_token.value = (void*)inToken;
|
2005-08-18 13:38:39 +04:00
|
|
|
in_token_ptr = &input_token;
|
|
|
|
} else if (mCtx != GSS_C_NO_CONTEXT) {
|
2005-08-18 13:38:43 +04:00
|
|
|
// If there is no input token, then we are starting a new
|
|
|
|
// authentication sequence. If we have already initialized our
|
|
|
|
// security context, then we're in trouble because it means that the
|
|
|
|
// first sequence failed. We need to bail or else we might end up in
|
|
|
|
// an infinite loop.
|
|
|
|
LOG(("Cannot restart authentication sequence!"));
|
2017-07-06 15:00:35 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
|
2005-08-18 13:38:42 +04:00
|
|
|
#if defined(XP_MACOSX)
|
|
|
|
// Suppress Kerberos prompts to get credentials. See bug 240643.
|
2017-07-06 15:00:35 +03:00
|
|
|
// We can only use Mac OS X specific kerb functions if we are using
|
2005-08-18 13:38:44 +04:00
|
|
|
// the native lib
|
2017-07-06 15:00:35 +03:00
|
|
|
KLBoolean found;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doingMailTask = mServiceName.Find("imap@") ||
|
2008-04-13 22:31:34 +04:00
|
|
|
mServiceName.Find("pop@") ||
|
|
|
|
mServiceName.Find("smtp@") || mServiceName.Find("ldap@");
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-10-25 00:02:00 +04:00
|
|
|
if (!doingMailTask &&
|
|
|
|
(gssNativeImp &&
|
2013-04-03 05:06:20 +04:00
|
|
|
(KLCacheHasValidTickets_ptr(nullptr, kerberosVersion_V5, &found, nullptr,
|
|
|
|
nullptr) != klNoErr ||
|
|
|
|
!found))) {
|
2005-08-18 13:38:42 +04:00
|
|
|
major_status = GSS_S_FAILURE;
|
|
|
|
minor_status = 0;
|
|
|
|
} else
|
|
|
|
#endif /* XP_MACOSX */
|
2005-08-18 13:38:44 +04:00
|
|
|
major_status = gss_init_sec_context_ptr(
|
|
|
|
&minor_status, GSS_C_NO_CREDENTIAL, &mCtx, server, mMechOID, req_flags,
|
|
|
|
GSS_C_INDEFINITE, GSS_C_NO_CHANNEL_BINDINGS, in_token_ptr, nullptr,
|
|
|
|
&output_token, nullptr, nullptr);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 13:38:39 +04:00
|
|
|
if (GSS_ERROR(major_status)) {
|
|
|
|
LogGssError(major_status, minor_status, "gss_init_sec_context() failed");
|
|
|
|
Reset();
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (major_status == GSS_S_COMPLETE) {
|
2005-08-18 19:22:33 +04:00
|
|
|
// Mark ourselves as being complete, so that if we're called again
|
|
|
|
// we know to start afresh.
|
2011-10-17 18:59:28 +04:00
|
|
|
mComplete = true;
|
2005-08-18 13:38:39 +04:00
|
|
|
} else if (major_status == GSS_S_CONTINUE_NEEDED) {
|
|
|
|
//
|
|
|
|
// The important thing is that we do NOT reset the
|
|
|
|
// context here because it will be needed on the
|
|
|
|
// next call.
|
|
|
|
//
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
*outTokenLen = output_token.length;
|
|
|
|
if (output_token.length != 0)
|
2018-08-28 08:59:19 +03:00
|
|
|
*outToken = moz_xmemdup(output_token.value, output_token.length);
|
2005-08-18 19:22:33 +04:00
|
|
|
else
|
2013-04-03 05:06:20 +04:00
|
|
|
*outToken = nullptr;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
gss_release_buffer_ptr(&minor_status, &output_token);
|
2005-08-18 13:38:39 +04:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
if (major_status == GSS_S_COMPLETE)
|
|
|
|
rv = NS_SUCCESS_AUTH_FINISHED;
|
|
|
|
else
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
end:
|
|
|
|
gss_release_name_ptr(&minor_status, &server);
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((" leaving nsAuthGSSAPI::GetNextToken [rv=%" PRIx32 "]",
|
|
|
|
static_cast<uint32_t>(rv)));
|
2005-08-18 19:22:33 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAuthGSSAPI::Unwrap(const void* inToken, uint32_t inTokenLen, void** outToken,
|
|
|
|
uint32_t* outTokenLen) {
|
2005-08-18 19:22:33 +04:00
|
|
|
OM_uint32 major_status, minor_status;
|
|
|
|
|
|
|
|
gss_buffer_desc input_token;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
input_token.value = (void*)inToken;
|
2005-08-18 19:22:33 +04:00
|
|
|
input_token.length = inTokenLen;
|
|
|
|
|
|
|
|
major_status = gss_unwrap_ptr(&minor_status, mCtx, &input_token,
|
|
|
|
&output_token, nullptr, nullptr);
|
|
|
|
if (GSS_ERROR(major_status)) {
|
|
|
|
LogGssError(major_status, minor_status, "gss_unwrap() failed");
|
|
|
|
Reset();
|
|
|
|
gss_release_buffer_ptr(&minor_status, &output_token);
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-08-18 13:38:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*outTokenLen = output_token.length;
|
2005-08-18 19:22:33 +04:00
|
|
|
|
|
|
|
if (output_token.length)
|
2018-08-28 08:59:19 +03:00
|
|
|
*outToken = moz_xmemdup(output_token.value, output_token.length);
|
2005-08-18 19:22:33 +04:00
|
|
|
else
|
2013-04-03 05:06:20 +04:00
|
|
|
*outToken = nullptr;
|
2005-08-18 13:38:39 +04:00
|
|
|
|
2005-08-18 13:38:44 +04:00
|
|
|
gss_release_buffer_ptr(&minor_status, &output_token);
|
2005-08-18 13:38:39 +04:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAuthGSSAPI::Wrap(const void* inToken, uint32_t inTokenLen, bool confidential,
|
|
|
|
void** outToken, uint32_t* outTokenLen) {
|
2005-08-18 19:22:33 +04:00
|
|
|
OM_uint32 major_status, minor_status;
|
|
|
|
|
|
|
|
gss_buffer_desc input_token;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
input_token.value = (void*)inToken;
|
2005-08-18 19:22:33 +04:00
|
|
|
input_token.length = inTokenLen;
|
|
|
|
|
|
|
|
major_status =
|
|
|
|
gss_wrap_ptr(&minor_status, mCtx, confidential, GSS_C_QOP_DEFAULT,
|
2018-08-28 08:59:19 +03:00
|
|
|
&input_token, nullptr, &output_token);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
if (GSS_ERROR(major_status)) {
|
2018-08-28 08:59:19 +03:00
|
|
|
LogGssError(major_status, minor_status, "gss_wrap() failed");
|
2018-11-30 13:46:48 +03:00
|
|
|
Reset();
|
2005-08-18 19:22:33 +04:00
|
|
|
gss_release_buffer_ptr(&minor_status, &output_token);
|
|
|
|
return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-08-18 19:22:33 +04:00
|
|
|
|
|
|
|
*outTokenLen = output_token.length;
|
|
|
|
|
|
|
|
/* it is not possible for output_token.length to be zero */
|
2018-08-28 08:59:19 +03:00
|
|
|
*outToken = moz_xmemdup(output_token.value, output_token.length);
|
2005-08-18 19:22:33 +04:00
|
|
|
gss_release_buffer_ptr(&minor_status, &output_token);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-08-18 19:22:33 +04:00
|
|
|
return NS_OK;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|