extensions/auth/nsAuthSSPI.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim:set ts=4 sw=4 sts=4 et cindent: */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 //
michael@0 7 // Negotiate Authentication Support Module
michael@0 8 //
michael@0 9 // Described by IETF Internet draft: draft-brezak-kerberos-http-00.txt
michael@0 10 // (formerly draft-brezak-spnego-http-04.txt)
michael@0 11 //
michael@0 12 // Also described here:
michael@0 13 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsecure/html/http-sso-1.asp
michael@0 14 //
michael@0 15
michael@0 16 #include "nsAuthSSPI.h"
michael@0 17 #include "nsIServiceManager.h"
michael@0 18 #include "nsIDNSService.h"
michael@0 19 #include "nsIDNSRecord.h"
michael@0 20 #include "nsNetCID.h"
michael@0 21 #include "nsCOMPtr.h"
michael@0 22 #include "nsICryptoHash.h"
michael@0 23 #include "mozilla/Telemetry.h"
michael@0 24
michael@0 25 #include <windows.h>
michael@0 26
michael@0 27 #define SEC_SUCCESS(Status) ((Status) >= 0)
michael@0 28
michael@0 29 #ifndef KERB_WRAP_NO_ENCRYPT
michael@0 30 #define KERB_WRAP_NO_ENCRYPT 0x80000001
michael@0 31 #endif
michael@0 32
michael@0 33 #ifndef SECBUFFER_PADDING
michael@0 34 #define SECBUFFER_PADDING 9
michael@0 35 #endif
michael@0 36
michael@0 37 #ifndef SECBUFFER_STREAM
michael@0 38 #define SECBUFFER_STREAM 10
michael@0 39 #endif
michael@0 40
michael@0 41 //-----------------------------------------------------------------------------
michael@0 42
michael@0 43 static const wchar_t *const pTypeName [] = {
michael@0 44 L"Kerberos",
michael@0 45 L"Negotiate",
michael@0 46 L"NTLM"
michael@0 47 };
michael@0 48
michael@0 49 #ifdef DEBUG
michael@0 50 #define CASE_(_x) case _x: return # _x;
michael@0 51 static const char *MapErrorCode(int rc)
michael@0 52 {
michael@0 53 switch (rc) {
michael@0 54 CASE_(SEC_E_OK)
michael@0 55 CASE_(SEC_I_CONTINUE_NEEDED)
michael@0 56 CASE_(SEC_I_COMPLETE_NEEDED)
michael@0 57 CASE_(SEC_I_COMPLETE_AND_CONTINUE)
michael@0 58 CASE_(SEC_E_INCOMPLETE_MESSAGE)
michael@0 59 CASE_(SEC_I_INCOMPLETE_CREDENTIALS)
michael@0 60 CASE_(SEC_E_INVALID_HANDLE)
michael@0 61 CASE_(SEC_E_TARGET_UNKNOWN)
michael@0 62 CASE_(SEC_E_LOGON_DENIED)
michael@0 63 CASE_(SEC_E_INTERNAL_ERROR)
michael@0 64 CASE_(SEC_E_NO_CREDENTIALS)
michael@0 65 CASE_(SEC_E_NO_AUTHENTICATING_AUTHORITY)
michael@0 66 CASE_(SEC_E_INSUFFICIENT_MEMORY)
michael@0 67 CASE_(SEC_E_INVALID_TOKEN)
michael@0 68 }
michael@0 69 return "<unknown>";
michael@0 70 }
michael@0 71 #else
michael@0 72 #define MapErrorCode(_rc) ""
michael@0 73 #endif
michael@0 74
michael@0 75 //-----------------------------------------------------------------------------
michael@0 76
michael@0 77 static PSecurityFunctionTableW sspi;
michael@0 78
michael@0 79 static nsresult
michael@0 80 InitSSPI()
michael@0 81 {
michael@0 82 LOG((" InitSSPI\n"));
michael@0 83
michael@0 84 sspi = InitSecurityInterfaceW();
michael@0 85 if (!sspi) {
michael@0 86 LOG(("InitSecurityInterfaceW failed"));
michael@0 87 return NS_ERROR_UNEXPECTED;
michael@0 88 }
michael@0 89
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 //-----------------------------------------------------------------------------
michael@0 94
michael@0 95 static nsresult
michael@0 96 MakeSN(const char *principal, nsCString &result)
michael@0 97 {
michael@0 98 nsresult rv;
michael@0 99
michael@0 100 nsAutoCString buf(principal);
michael@0 101
michael@0 102 // The service name looks like "protocol@hostname", we need to map
michael@0 103 // this to a value that SSPI expects. To be consistent with IE, we
michael@0 104 // need to map '@' to '/' and canonicalize the hostname.
michael@0 105 int32_t index = buf.FindChar('@');
michael@0 106 if (index == kNotFound)
michael@0 107 return NS_ERROR_UNEXPECTED;
michael@0 108
michael@0 109 nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
michael@0 110 if (NS_FAILED(rv))
michael@0 111 return rv;
michael@0 112
michael@0 113 // This could be expensive if our DNS cache cannot satisfy the request.
michael@0 114 // However, we should have at least hit the OS resolver once prior to
michael@0 115 // reaching this code, so provided the OS resolver has this information
michael@0 116 // cached, we should not have to worry about blocking on this function call
michael@0 117 // for very long. NOTE: because we ask for the canonical hostname, we
michael@0 118 // might end up requiring extra network activity in cases where the OS
michael@0 119 // resolver might not have enough information to satisfy the request from
michael@0 120 // its cache. This is not an issue in versions of Windows up to WinXP.
michael@0 121 nsCOMPtr<nsIDNSRecord> record;
michael@0 122 rv = dns->Resolve(Substring(buf, index + 1),
michael@0 123 nsIDNSService::RESOLVE_CANONICAL_NAME,
michael@0 124 getter_AddRefs(record));
michael@0 125 if (NS_FAILED(rv))
michael@0 126 return rv;
michael@0 127
michael@0 128 nsAutoCString cname;
michael@0 129 rv = record->GetCanonicalName(cname);
michael@0 130 if (NS_SUCCEEDED(rv)) {
michael@0 131 result = StringHead(buf, index) + NS_LITERAL_CSTRING("/") + cname;
michael@0 132 LOG(("Using SPN of [%s]\n", result.get()));
michael@0 133 }
michael@0 134 return rv;
michael@0 135 }
michael@0 136
michael@0 137 //-----------------------------------------------------------------------------
michael@0 138
michael@0 139 nsAuthSSPI::nsAuthSSPI(pType package)
michael@0 140 : mServiceFlags(REQ_DEFAULT)
michael@0 141 , mMaxTokenLen(0)
michael@0 142 , mPackage(package)
michael@0 143 , mCertDERData(nullptr)
michael@0 144 , mCertDERLength(0)
michael@0 145 {
michael@0 146 memset(&mCred, 0, sizeof(mCred));
michael@0 147 memset(&mCtxt, 0, sizeof(mCtxt));
michael@0 148 }
michael@0 149
michael@0 150 nsAuthSSPI::~nsAuthSSPI()
michael@0 151 {
michael@0 152 Reset();
michael@0 153
michael@0 154 if (mCred.dwLower || mCred.dwUpper) {
michael@0 155 #ifdef __MINGW32__
michael@0 156 (sspi->FreeCredentialsHandle)(&mCred);
michael@0 157 #else
michael@0 158 (sspi->FreeCredentialHandle)(&mCred);
michael@0 159 #endif
michael@0 160 memset(&mCred, 0, sizeof(mCred));
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 void
michael@0 165 nsAuthSSPI::Reset()
michael@0 166 {
michael@0 167 mIsFirst = true;
michael@0 168
michael@0 169 if (mCertDERData){
michael@0 170 nsMemory::Free(mCertDERData);
michael@0 171 mCertDERData = nullptr;
michael@0 172 mCertDERLength = 0;
michael@0 173 }
michael@0 174
michael@0 175 if (mCtxt.dwLower || mCtxt.dwUpper) {
michael@0 176 (sspi->DeleteSecurityContext)(&mCtxt);
michael@0 177 memset(&mCtxt, 0, sizeof(mCtxt));
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 NS_IMPL_ISUPPORTS(nsAuthSSPI, nsIAuthModule)
michael@0 182
michael@0 183 NS_IMETHODIMP
michael@0 184 nsAuthSSPI::Init(const char *serviceName,
michael@0 185 uint32_t serviceFlags,
michael@0 186 const char16_t *domain,
michael@0 187 const char16_t *username,
michael@0 188 const char16_t *password)
michael@0 189 {
michael@0 190 LOG((" nsAuthSSPI::Init\n"));
michael@0 191
michael@0 192 mIsFirst = true;
michael@0 193 mCertDERLength = 0;
michael@0 194 mCertDERData = nullptr;
michael@0 195
michael@0 196 // The caller must supply a service name to be used. (For why we now require
michael@0 197 // a service name for NTLM, see bug 487872.)
michael@0 198 NS_ENSURE_TRUE(serviceName && *serviceName, NS_ERROR_INVALID_ARG);
michael@0 199
michael@0 200 nsresult rv;
michael@0 201
michael@0 202 // XXX lazy initialization like this assumes that we are single threaded
michael@0 203 if (!sspi) {
michael@0 204 rv = InitSSPI();
michael@0 205 if (NS_FAILED(rv))
michael@0 206 return rv;
michael@0 207 }
michael@0 208 SEC_WCHAR *package;
michael@0 209
michael@0 210 package = (SEC_WCHAR *) pTypeName[(int)mPackage];
michael@0 211
michael@0 212 if (mPackage == PACKAGE_TYPE_NTLM) {
michael@0 213 // (bug 535193) For NTLM, just use the uri host, do not do canonical host lookups.
michael@0 214 // The incoming serviceName is in the format: "protocol@hostname", SSPI expects
michael@0 215 // "<service class>/<hostname>", so swap the '@' for a '/'.
michael@0 216 mServiceName.Assign(serviceName);
michael@0 217 int32_t index = mServiceName.FindChar('@');
michael@0 218 if (index == kNotFound)
michael@0 219 return NS_ERROR_UNEXPECTED;
michael@0 220 mServiceName.Replace(index, 1, '/');
michael@0 221 }
michael@0 222 else {
michael@0 223 // Kerberos requires the canonical host, MakeSN takes care of this through a
michael@0 224 // DNS lookup.
michael@0 225 rv = MakeSN(serviceName, mServiceName);
michael@0 226 if (NS_FAILED(rv))
michael@0 227 return rv;
michael@0 228 }
michael@0 229
michael@0 230 mServiceFlags = serviceFlags;
michael@0 231
michael@0 232 SECURITY_STATUS rc;
michael@0 233
michael@0 234 PSecPkgInfoW pinfo;
michael@0 235 rc = (sspi->QuerySecurityPackageInfoW)(package, &pinfo);
michael@0 236 if (rc != SEC_E_OK) {
michael@0 237 LOG(("%s package not found\n", package));
michael@0 238 return NS_ERROR_UNEXPECTED;
michael@0 239 }
michael@0 240 mMaxTokenLen = pinfo->cbMaxToken;
michael@0 241 (sspi->FreeContextBuffer)(pinfo);
michael@0 242
michael@0 243 MS_TimeStamp useBefore;
michael@0 244
michael@0 245 SEC_WINNT_AUTH_IDENTITY_W ai;
michael@0 246 SEC_WINNT_AUTH_IDENTITY_W *pai = nullptr;
michael@0 247
michael@0 248 // domain, username, and password will be null if nsHttpNTLMAuth's ChallengeReceived
michael@0 249 // returns false for identityInvalid. Use default credentials in this case by passing
michael@0 250 // null for pai.
michael@0 251 if (username && password) {
michael@0 252 // Keep a copy of these strings for the duration
michael@0 253 mUsername.Assign(username);
michael@0 254 mPassword.Assign(password);
michael@0 255 mDomain.Assign(domain);
michael@0 256 ai.Domain = reinterpret_cast<unsigned short*>(mDomain.BeginWriting());
michael@0 257 ai.DomainLength = mDomain.Length();
michael@0 258 ai.User = reinterpret_cast<unsigned short*>(mUsername.BeginWriting());
michael@0 259 ai.UserLength = mUsername.Length();
michael@0 260 ai.Password = reinterpret_cast<unsigned short*>(mPassword.BeginWriting());
michael@0 261 ai.PasswordLength = mPassword.Length();
michael@0 262 ai.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
michael@0 263 pai = &ai;
michael@0 264 }
michael@0 265
michael@0 266 rc = (sspi->AcquireCredentialsHandleW)(nullptr,
michael@0 267 package,
michael@0 268 SECPKG_CRED_OUTBOUND,
michael@0 269 nullptr,
michael@0 270 pai,
michael@0 271 nullptr,
michael@0 272 nullptr,
michael@0 273 &mCred,
michael@0 274 &useBefore);
michael@0 275 if (rc != SEC_E_OK)
michael@0 276 return NS_ERROR_UNEXPECTED;
michael@0 277
michael@0 278 static bool sTelemetrySent = false;
michael@0 279 if (!sTelemetrySent) {
michael@0 280 mozilla::Telemetry::Accumulate(
michael@0 281 mozilla::Telemetry::NTLM_MODULE_USED_2,
michael@0 282 serviceFlags & nsIAuthModule::REQ_PROXY_AUTH
michael@0 283 ? NTLM_MODULE_WIN_API_PROXY
michael@0 284 : NTLM_MODULE_WIN_API_DIRECT);
michael@0 285 sTelemetrySent = true;
michael@0 286 }
michael@0 287
michael@0 288 LOG(("AcquireCredentialsHandle() succeeded.\n"));
michael@0 289 return NS_OK;
michael@0 290 }
michael@0 291
michael@0 292 // The arguments inToken and inTokenLen are used to pass in the server
michael@0 293 // certificate (when available) in the first call of the function. The
michael@0 294 // second time these arguments hold an input token.
michael@0 295 NS_IMETHODIMP
michael@0 296 nsAuthSSPI::GetNextToken(const void *inToken,
michael@0 297 uint32_t inTokenLen,
michael@0 298 void **outToken,
michael@0 299 uint32_t *outTokenLen)
michael@0 300 {
michael@0 301 // String for end-point bindings.
michael@0 302 const char end_point[] = "tls-server-end-point:";
michael@0 303 const int end_point_length = sizeof(end_point) - 1;
michael@0 304 const int hash_size = 32; // Size of a SHA256 hash.
michael@0 305 const int cbt_size = hash_size + end_point_length;
michael@0 306
michael@0 307 SECURITY_STATUS rc;
michael@0 308 MS_TimeStamp ignored;
michael@0 309
michael@0 310 DWORD ctxAttr, ctxReq = 0;
michael@0 311 CtxtHandle *ctxIn;
michael@0 312 SecBufferDesc ibd, obd;
michael@0 313 // Optional second input buffer for the CBT (Channel Binding Token)
michael@0 314 SecBuffer ib[2], ob;
michael@0 315 // Pointer to the block of memory that stores the CBT
michael@0 316 char* sspi_cbt = nullptr;
michael@0 317 SEC_CHANNEL_BINDINGS pendpoint_binding;
michael@0 318
michael@0 319 LOG(("entering nsAuthSSPI::GetNextToken()\n"));
michael@0 320
michael@0 321 if (!mCred.dwLower && !mCred.dwUpper) {
michael@0 322 LOG(("nsAuthSSPI::GetNextToken(), not initialized. exiting."));
michael@0 323 return NS_ERROR_NOT_INITIALIZED;
michael@0 324 }
michael@0 325
michael@0 326 if (mServiceFlags & REQ_DELEGATE)
michael@0 327 ctxReq |= ISC_REQ_DELEGATE;
michael@0 328 if (mServiceFlags & REQ_MUTUAL_AUTH)
michael@0 329 ctxReq |= ISC_REQ_MUTUAL_AUTH;
michael@0 330
michael@0 331 if (inToken) {
michael@0 332 if (mIsFirst) {
michael@0 333 // First time if it comes with a token,
michael@0 334 // the token represents the server certificate.
michael@0 335 mIsFirst = false;
michael@0 336 mCertDERLength = inTokenLen;
michael@0 337 mCertDERData = nsMemory::Alloc(inTokenLen);
michael@0 338 if (!mCertDERData)
michael@0 339 return NS_ERROR_OUT_OF_MEMORY;
michael@0 340 memcpy(mCertDERData, inToken, inTokenLen);
michael@0 341
michael@0 342 // We are starting a new authentication sequence.
michael@0 343 // If we have already initialized our
michael@0 344 // security context, then we're in trouble because it means that the
michael@0 345 // first sequence failed. We need to bail or else we might end up in
michael@0 346 // an infinite loop.
michael@0 347 if (mCtxt.dwLower || mCtxt.dwUpper) {
michael@0 348 LOG(("Cannot restart authentication sequence!"));
michael@0 349 return NS_ERROR_UNEXPECTED;
michael@0 350 }
michael@0 351 ctxIn = nullptr;
michael@0 352 // The certificate needs to be erased before being passed
michael@0 353 // to InitializeSecurityContextW().
michael@0 354 inToken = nullptr;
michael@0 355 inTokenLen = 0;
michael@0 356 } else {
michael@0 357 ibd.ulVersion = SECBUFFER_VERSION;
michael@0 358 ibd.cBuffers = 0;
michael@0 359 ibd.pBuffers = ib;
michael@0 360
michael@0 361 // If we have stored a certificate, the Channel Binding Token
michael@0 362 // needs to be generated and sent in the first input buffer.
michael@0 363 if (mCertDERLength > 0) {
michael@0 364 // First we create a proper Endpoint Binding structure.
michael@0 365 pendpoint_binding.dwInitiatorAddrType = 0;
michael@0 366 pendpoint_binding.cbInitiatorLength = 0;
michael@0 367 pendpoint_binding.dwInitiatorOffset = 0;
michael@0 368 pendpoint_binding.dwAcceptorAddrType = 0;
michael@0 369 pendpoint_binding.cbAcceptorLength = 0;
michael@0 370 pendpoint_binding.dwAcceptorOffset = 0;
michael@0 371 pendpoint_binding.cbApplicationDataLength = cbt_size;
michael@0 372 pendpoint_binding.dwApplicationDataOffset =
michael@0 373 sizeof(SEC_CHANNEL_BINDINGS);
michael@0 374
michael@0 375 // Then add it to the array of sec buffers accordingly.
michael@0 376 ib[ibd.cBuffers].BufferType = SECBUFFER_CHANNEL_BINDINGS;
michael@0 377 ib[ibd.cBuffers].cbBuffer =
michael@0 378 pendpoint_binding.cbApplicationDataLength
michael@0 379 + pendpoint_binding.dwApplicationDataOffset;
michael@0 380
michael@0 381 sspi_cbt = (char *) nsMemory::Alloc(ib[ibd.cBuffers].cbBuffer);
michael@0 382 if (!sspi_cbt){
michael@0 383 return NS_ERROR_OUT_OF_MEMORY;
michael@0 384 }
michael@0 385
michael@0 386 // Helper to write in the memory block that stores the CBT
michael@0 387 char* sspi_cbt_ptr = sspi_cbt;
michael@0 388
michael@0 389 ib[ibd.cBuffers].pvBuffer = sspi_cbt;
michael@0 390 ibd.cBuffers++;
michael@0 391
michael@0 392 memcpy(sspi_cbt_ptr, &pendpoint_binding,
michael@0 393 pendpoint_binding.dwApplicationDataOffset);
michael@0 394 sspi_cbt_ptr += pendpoint_binding.dwApplicationDataOffset;
michael@0 395
michael@0 396 memcpy(sspi_cbt_ptr, end_point, end_point_length);
michael@0 397 sspi_cbt_ptr += end_point_length;
michael@0 398
michael@0 399 // Start hashing. We are always doing SHA256, but depending
michael@0 400 // on the certificate, a different alogirthm might be needed.
michael@0 401 nsAutoCString hashString;
michael@0 402
michael@0 403 nsresult rv;
michael@0 404 nsCOMPtr<nsICryptoHash> crypto;
michael@0 405 crypto = do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
michael@0 406 if (NS_SUCCEEDED(rv))
michael@0 407 rv = crypto->Init(nsICryptoHash::SHA256);
michael@0 408 if (NS_SUCCEEDED(rv))
michael@0 409 rv = crypto->Update((unsigned char*)mCertDERData, mCertDERLength);
michael@0 410 if (NS_SUCCEEDED(rv))
michael@0 411 rv = crypto->Finish(false, hashString);
michael@0 412 if (NS_FAILED(rv)) {
michael@0 413 nsMemory::Free(mCertDERData);
michael@0 414 mCertDERData = nullptr;
michael@0 415 mCertDERLength = 0;
michael@0 416 nsMemory::Free(sspi_cbt);
michael@0 417 return rv;
michael@0 418 }
michael@0 419
michael@0 420 // Once the hash has been computed, we store it in memory right
michael@0 421 // after the Endpoint structure and the "tls-server-end-point:"
michael@0 422 // char array.
michael@0 423 memcpy(sspi_cbt_ptr, hashString.get(), hash_size);
michael@0 424
michael@0 425 // Free memory used to store the server certificate
michael@0 426 nsMemory::Free(mCertDERData);
michael@0 427 mCertDERData = nullptr;
michael@0 428 mCertDERLength = 0;
michael@0 429 } // End of CBT computation.
michael@0 430
michael@0 431 // We always need this SECBUFFER.
michael@0 432 ib[ibd.cBuffers].BufferType = SECBUFFER_TOKEN;
michael@0 433 ib[ibd.cBuffers].cbBuffer = inTokenLen;
michael@0 434 ib[ibd.cBuffers].pvBuffer = (void *) inToken;
michael@0 435 ibd.cBuffers++;
michael@0 436 ctxIn = &mCtxt;
michael@0 437 }
michael@0 438 } else { // First time and without a token (no server certificate)
michael@0 439 // We are starting a new authentication sequence. If we have already
michael@0 440 // initialized our security context, then we're in trouble because it
michael@0 441 // means that the first sequence failed. We need to bail or else we
michael@0 442 // might end up in an infinite loop.
michael@0 443 if (mCtxt.dwLower || mCtxt.dwUpper || mCertDERData || mCertDERLength) {
michael@0 444 LOG(("Cannot restart authentication sequence!"));
michael@0 445 return NS_ERROR_UNEXPECTED;
michael@0 446 }
michael@0 447 ctxIn = nullptr;
michael@0 448 mIsFirst = false;
michael@0 449 }
michael@0 450
michael@0 451 obd.ulVersion = SECBUFFER_VERSION;
michael@0 452 obd.cBuffers = 1;
michael@0 453 obd.pBuffers = &ob;
michael@0 454 ob.BufferType = SECBUFFER_TOKEN;
michael@0 455 ob.cbBuffer = mMaxTokenLen;
michael@0 456 ob.pvBuffer = nsMemory::Alloc(ob.cbBuffer);
michael@0 457 if (!ob.pvBuffer){
michael@0 458 if (sspi_cbt)
michael@0 459 nsMemory::Free(sspi_cbt);
michael@0 460 return NS_ERROR_OUT_OF_MEMORY;
michael@0 461 }
michael@0 462 memset(ob.pvBuffer, 0, ob.cbBuffer);
michael@0 463
michael@0 464 NS_ConvertUTF8toUTF16 wSN(mServiceName);
michael@0 465 SEC_WCHAR *sn = (SEC_WCHAR *) wSN.get();
michael@0 466
michael@0 467 rc = (sspi->InitializeSecurityContextW)(&mCred,
michael@0 468 ctxIn,
michael@0 469 sn,
michael@0 470 ctxReq,
michael@0 471 0,
michael@0 472 SECURITY_NATIVE_DREP,
michael@0 473 inToken ? &ibd : nullptr,
michael@0 474 0,
michael@0 475 &mCtxt,
michael@0 476 &obd,
michael@0 477 &ctxAttr,
michael@0 478 &ignored);
michael@0 479 if (rc == SEC_I_CONTINUE_NEEDED || rc == SEC_E_OK) {
michael@0 480
michael@0 481 #ifdef PR_LOGGING
michael@0 482 if (rc == SEC_E_OK)
michael@0 483 LOG(("InitializeSecurityContext: succeeded.\n"));
michael@0 484 else
michael@0 485 LOG(("InitializeSecurityContext: continue.\n"));
michael@0 486 #endif
michael@0 487 if (sspi_cbt)
michael@0 488 nsMemory::Free(sspi_cbt);
michael@0 489
michael@0 490 if (!ob.cbBuffer) {
michael@0 491 nsMemory::Free(ob.pvBuffer);
michael@0 492 ob.pvBuffer = nullptr;
michael@0 493 }
michael@0 494 *outToken = ob.pvBuffer;
michael@0 495 *outTokenLen = ob.cbBuffer;
michael@0 496
michael@0 497 if (rc == SEC_E_OK)
michael@0 498 return NS_SUCCESS_AUTH_FINISHED;
michael@0 499
michael@0 500 return NS_OK;
michael@0 501 }
michael@0 502
michael@0 503 LOG(("InitializeSecurityContext failed [rc=%d:%s]\n", rc, MapErrorCode(rc)));
michael@0 504 Reset();
michael@0 505 nsMemory::Free(ob.pvBuffer);
michael@0 506 return NS_ERROR_FAILURE;
michael@0 507 }
michael@0 508
michael@0 509 NS_IMETHODIMP
michael@0 510 nsAuthSSPI::Unwrap(const void *inToken,
michael@0 511 uint32_t inTokenLen,
michael@0 512 void **outToken,
michael@0 513 uint32_t *outTokenLen)
michael@0 514 {
michael@0 515 SECURITY_STATUS rc;
michael@0 516 SecBufferDesc ibd;
michael@0 517 SecBuffer ib[2];
michael@0 518
michael@0 519 ibd.cBuffers = 2;
michael@0 520 ibd.pBuffers = ib;
michael@0 521 ibd.ulVersion = SECBUFFER_VERSION;
michael@0 522
michael@0 523 // SSPI Buf
michael@0 524 ib[0].BufferType = SECBUFFER_STREAM;
michael@0 525 ib[0].cbBuffer = inTokenLen;
michael@0 526 ib[0].pvBuffer = nsMemory::Alloc(ib[0].cbBuffer);
michael@0 527 if (!ib[0].pvBuffer)
michael@0 528 return NS_ERROR_OUT_OF_MEMORY;
michael@0 529
michael@0 530 memcpy(ib[0].pvBuffer, inToken, inTokenLen);
michael@0 531
michael@0 532 // app data
michael@0 533 ib[1].BufferType = SECBUFFER_DATA;
michael@0 534 ib[1].cbBuffer = 0;
michael@0 535 ib[1].pvBuffer = nullptr;
michael@0 536
michael@0 537 rc = (sspi->DecryptMessage)(
michael@0 538 &mCtxt,
michael@0 539 &ibd,
michael@0 540 0, // no sequence numbers
michael@0 541 nullptr
michael@0 542 );
michael@0 543
michael@0 544 if (SEC_SUCCESS(rc)) {
michael@0 545 // check if ib[1].pvBuffer is really just ib[0].pvBuffer, in which
michael@0 546 // case we can let the caller free it. Otherwise, we need to
michael@0 547 // clone it, and free the original
michael@0 548 if (ib[0].pvBuffer == ib[1].pvBuffer) {
michael@0 549 *outToken = ib[1].pvBuffer;
michael@0 550 }
michael@0 551 else {
michael@0 552 *outToken = nsMemory::Clone(ib[1].pvBuffer, ib[1].cbBuffer);
michael@0 553 nsMemory::Free(ib[0].pvBuffer);
michael@0 554 if (!*outToken)
michael@0 555 return NS_ERROR_OUT_OF_MEMORY;
michael@0 556 }
michael@0 557 *outTokenLen = ib[1].cbBuffer;
michael@0 558 }
michael@0 559 else
michael@0 560 nsMemory::Free(ib[0].pvBuffer);
michael@0 561
michael@0 562 if (!SEC_SUCCESS(rc))
michael@0 563 return NS_ERROR_FAILURE;
michael@0 564
michael@0 565 return NS_OK;
michael@0 566 }
michael@0 567
michael@0 568 // utility class used to free memory on exit
michael@0 569 class secBuffers
michael@0 570 {
michael@0 571 public:
michael@0 572
michael@0 573 SecBuffer ib[3];
michael@0 574
michael@0 575 secBuffers() { memset(&ib, 0, sizeof(ib)); }
michael@0 576
michael@0 577 ~secBuffers()
michael@0 578 {
michael@0 579 if (ib[0].pvBuffer)
michael@0 580 nsMemory::Free(ib[0].pvBuffer);
michael@0 581
michael@0 582 if (ib[1].pvBuffer)
michael@0 583 nsMemory::Free(ib[1].pvBuffer);
michael@0 584
michael@0 585 if (ib[2].pvBuffer)
michael@0 586 nsMemory::Free(ib[2].pvBuffer);
michael@0 587 }
michael@0 588 };
michael@0 589
michael@0 590 NS_IMETHODIMP
michael@0 591 nsAuthSSPI::Wrap(const void *inToken,
michael@0 592 uint32_t inTokenLen,
michael@0 593 bool confidential,
michael@0 594 void **outToken,
michael@0 595 uint32_t *outTokenLen)
michael@0 596 {
michael@0 597 SECURITY_STATUS rc;
michael@0 598
michael@0 599 SecBufferDesc ibd;
michael@0 600 secBuffers bufs;
michael@0 601 SecPkgContext_Sizes sizes;
michael@0 602
michael@0 603 rc = (sspi->QueryContextAttributesW)(
michael@0 604 &mCtxt,
michael@0 605 SECPKG_ATTR_SIZES,
michael@0 606 &sizes);
michael@0 607
michael@0 608 if (!SEC_SUCCESS(rc))
michael@0 609 return NS_ERROR_FAILURE;
michael@0 610
michael@0 611 ibd.cBuffers = 3;
michael@0 612 ibd.pBuffers = bufs.ib;
michael@0 613 ibd.ulVersion = SECBUFFER_VERSION;
michael@0 614
michael@0 615 // SSPI
michael@0 616 bufs.ib[0].cbBuffer = sizes.cbSecurityTrailer;
michael@0 617 bufs.ib[0].BufferType = SECBUFFER_TOKEN;
michael@0 618 bufs.ib[0].pvBuffer = nsMemory::Alloc(sizes.cbSecurityTrailer);
michael@0 619
michael@0 620 if (!bufs.ib[0].pvBuffer)
michael@0 621 return NS_ERROR_OUT_OF_MEMORY;
michael@0 622
michael@0 623 // APP Data
michael@0 624 bufs.ib[1].BufferType = SECBUFFER_DATA;
michael@0 625 bufs.ib[1].pvBuffer = nsMemory::Alloc(inTokenLen);
michael@0 626 bufs.ib[1].cbBuffer = inTokenLen;
michael@0 627
michael@0 628 if (!bufs.ib[1].pvBuffer)
michael@0 629 return NS_ERROR_OUT_OF_MEMORY;
michael@0 630
michael@0 631 memcpy(bufs.ib[1].pvBuffer, inToken, inTokenLen);
michael@0 632
michael@0 633 // SSPI
michael@0 634 bufs.ib[2].BufferType = SECBUFFER_PADDING;
michael@0 635 bufs.ib[2].cbBuffer = sizes.cbBlockSize;
michael@0 636 bufs.ib[2].pvBuffer = nsMemory::Alloc(bufs.ib[2].cbBuffer);
michael@0 637
michael@0 638 if (!bufs.ib[2].pvBuffer)
michael@0 639 return NS_ERROR_OUT_OF_MEMORY;
michael@0 640
michael@0 641 rc = (sspi->EncryptMessage)(&mCtxt,
michael@0 642 confidential ? 0 : KERB_WRAP_NO_ENCRYPT,
michael@0 643 &ibd, 0);
michael@0 644
michael@0 645 if (SEC_SUCCESS(rc)) {
michael@0 646 int len = bufs.ib[0].cbBuffer + bufs.ib[1].cbBuffer + bufs.ib[2].cbBuffer;
michael@0 647 char *p = (char *) nsMemory::Alloc(len);
michael@0 648
michael@0 649 if (!p)
michael@0 650 return NS_ERROR_OUT_OF_MEMORY;
michael@0 651
michael@0 652 *outToken = (void *) p;
michael@0 653 *outTokenLen = len;
michael@0 654
michael@0 655 memcpy(p, bufs.ib[0].pvBuffer, bufs.ib[0].cbBuffer);
michael@0 656 p += bufs.ib[0].cbBuffer;
michael@0 657
michael@0 658 memcpy(p,bufs.ib[1].pvBuffer, bufs.ib[1].cbBuffer);
michael@0 659 p += bufs.ib[1].cbBuffer;
michael@0 660
michael@0 661 memcpy(p,bufs.ib[2].pvBuffer, bufs.ib[2].cbBuffer);
michael@0 662
michael@0 663 return NS_OK;
michael@0 664 }
michael@0 665
michael@0 666 return NS_ERROR_FAILURE;
michael@0 667 }

mercurial