1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/http/http_auth_sspi_win.h"
#include <string_view>
#include <vector>
#include "base/base64.h"
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "net/base/net_errors.h"
#include "net/http/http_auth.h"
#include "net/http/http_auth_challenge_tokenizer.h"
#include "net/http/mock_sspi_library_win.h"
#include "net/log/net_log_entry.h"
#include "net/log/net_log_event_type.h"
#include "net/log/net_log_with_source.h"
#include "net/log/test_net_log.h"
#include "net/test/gtest_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using net::test::IsError;
using net::test::IsOk;
namespace net {
namespace {
void MatchDomainUserAfterSplit(const std::u16string& combined,
const std::u16string& expected_domain,
const std::u16string& expected_user) {
std::u16string actual_domain;
std::u16string actual_user;
SplitDomainAndUser(combined, &actual_domain, &actual_user);
EXPECT_EQ(expected_domain, actual_domain);
EXPECT_EQ(expected_user, actual_user);
}
const ULONG kMaxTokenLength = 100;
void UnexpectedCallback(int result) {
// At present getting tokens from gssapi is fully synchronous, so the callback
// should never be called.
ADD_FAILURE();
}
} // namespace
TEST(HttpAuthSSPITest, SplitUserAndDomain) {
MatchDomainUserAfterSplit(u"foobar", u"", u"foobar");
MatchDomainUserAfterSplit(u"FOO\\bar", u"FOO", u"bar");
}
TEST(HttpAuthSSPITest, DetermineMaxTokenLength_Normal) {
SecPkgInfoW package_info;
UNSAFE_TODO(memset(&package_info, 0x0, sizeof(package_info)));
package_info.cbMaxToken = 1337;
MockSSPILibrary mock_library{L"NTLM"};
mock_library.ExpectQuerySecurityPackageInfo(SEC_E_OK, &package_info);
ULONG max_token_length = kMaxTokenLength;
int rv = mock_library.DetermineMaxTokenLength(&max_token_length);
EXPECT_THAT(rv, IsOk());
EXPECT_EQ(1337u, max_token_length);
}
TEST(HttpAuthSSPITest, DetermineMaxTokenLength_InvalidPackage) {
MockSSPILibrary mock_library{L"Foo"};
mock_library.ExpectQuerySecurityPackageInfo(SEC_E_SECPKG_NOT_FOUND, nullptr);
ULONG max_token_length = kMaxTokenLength;
int rv = mock_library.DetermineMaxTokenLength(&max_token_length);
EXPECT_THAT(rv, IsError(ERR_UNSUPPORTED_AUTH_SCHEME));
// |DetermineMaxTokenLength()| interface states that |max_token_length| should
// not change on failure.
EXPECT_EQ(100u, max_token_length);
}
TEST(HttpAuthSSPITest, ParseChallenge_FirstRound) {
// The first round should just consist of an unadorned "Negotiate" header.
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
HttpAuthChallengeTokenizer challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&challenge));
}
TEST(HttpAuthSSPITest, ParseChallenge_TwoRounds) {
// The first round should just have "Negotiate", and the second round should
// have a valid base64 token associated with it.
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
HttpAuthChallengeTokenizer first_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&first_challenge));
// Generate an auth token and create another thing.
std::string auth_token;
EXPECT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
NetLogWithSource(), base::BindOnce(&UnexpectedCallback)));
HttpAuthChallengeTokenizer second_challenge("Negotiate Zm9vYmFy");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&second_challenge));
}
TEST(HttpAuthSSPITest, ParseChallenge_UnexpectedTokenFirstRound) {
// If the first round challenge has an additional authentication token, it
// should be treated as an invalid challenge from the server.
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
HttpAuthChallengeTokenizer challenge("Negotiate Zm9vYmFy");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
auth_sspi.ParseChallenge(&challenge));
}
TEST(HttpAuthSSPITest, ParseChallenge_MissingTokenSecondRound) {
// If a later-round challenge is simply "Negotiate", it should be treated as
// an authentication challenge rejection from the server or proxy.
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
HttpAuthChallengeTokenizer first_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&first_challenge));
std::string auth_token;
EXPECT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
NetLogWithSource(), base::BindOnce(&UnexpectedCallback)));
HttpAuthChallengeTokenizer second_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_REJECT,
auth_sspi.ParseChallenge(&second_challenge));
}
TEST(HttpAuthSSPITest, ParseChallenge_NonBase64EncodedToken) {
// If a later-round challenge has an invalid base64 encoded token, it should
// be treated as an invalid challenge.
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
std::string first_challenge_text = "Negotiate";
HttpAuthChallengeTokenizer first_challenge("Negotiate");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&first_challenge));
std::string auth_token;
EXPECT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
NetLogWithSource(), base::BindOnce(&UnexpectedCallback)));
HttpAuthChallengeTokenizer second_challenge("Negotiate =happyjoy=");
EXPECT_EQ(HttpAuth::AUTHORIZATION_RESULT_INVALID,
auth_sspi.ParseChallenge(&second_challenge));
}
// Runs through a full handshake against the MockSSPILibrary.
TEST(HttpAuthSSPITest, GenerateAuthToken_FullHandshake_AmbientCreds) {
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
std::string first_challenge_text = "Negotiate";
HttpAuthChallengeTokenizer first_challenge("Negotiate");
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&first_challenge));
std::string auth_token;
ASSERT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
NetLogWithSource(), base::BindOnce(&UnexpectedCallback)));
EXPECT_EQ("Negotiate ", auth_token.substr(0, 10));
std::string decoded_token;
ASSERT_TRUE(base::Base64Decode(auth_token.substr(10), &decoded_token));
// This token string indicates that HttpAuthSSPI correctly established the
// security context using the default credentials.
EXPECT_EQ("<Default>'s token #1 for HTTP/intranet.google.com", decoded_token);
// The server token is arbitrary.
HttpAuthChallengeTokenizer second_challenge("Negotiate UmVzcG9uc2U=");
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&second_challenge));
ASSERT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
NetLogWithSource(), base::BindOnce(&UnexpectedCallback)));
ASSERT_EQ("Negotiate ", auth_token.substr(0, 10));
ASSERT_TRUE(base::Base64Decode(auth_token.substr(10), &decoded_token));
EXPECT_EQ("<Default>'s token #2 for HTTP/intranet.google.com", decoded_token);
}
// Test NetLogs produced while going through a full Negotiate handshake.
TEST(HttpAuthSSPITest, GenerateAuthToken_FullHandshake_AmbientCreds_Logging) {
RecordingNetLogObserver net_log_observer;
NetLogWithSource net_log_with_source =
NetLogWithSource::Make(NetLogSourceType::NONE);
MockSSPILibrary mock_library{NEGOSSP_NAME};
HttpAuthSSPI auth_sspi(&mock_library, HttpAuth::AUTH_SCHEME_NEGOTIATE);
HttpAuthChallengeTokenizer first_challenge("Negotiate");
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&first_challenge));
std::string auth_token;
ASSERT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
net_log_with_source, base::BindOnce(&UnexpectedCallback)));
// The token is the ASCII string "Response" in base64.
HttpAuthChallengeTokenizer second_challenge("Negotiate UmVzcG9uc2U=");
ASSERT_EQ(HttpAuth::AUTHORIZATION_RESULT_ACCEPT,
auth_sspi.ParseChallenge(&second_challenge));
ASSERT_EQ(OK,
auth_sspi.GenerateAuthToken(
nullptr, "HTTP/intranet.google.com", std::string(), &auth_token,
net_log_with_source, base::BindOnce(&UnexpectedCallback)));
auto entries = net_log_observer.GetEntriesWithType(
NetLogEventType::AUTH_LIBRARY_ACQUIRE_CREDS);
ASSERT_EQ(2u, entries.size()); // BEGIN and END.
auto expected = base::JSONReader::Read(R"(
{
"status": {
"net_error": 0,
"security_status": 0
}
}
)");
EXPECT_EQ(expected, entries[1].params);
entries = net_log_observer.GetEntriesWithType(
NetLogEventType::AUTH_LIBRARY_INIT_SEC_CTX);
ASSERT_EQ(4u, entries.size());
expected = base::JSONReader::Read(R"(
{
"flags": {
"delegated": false,
"mutual": false,
"value": "0x00000000"
},
"spn": "HTTP/intranet.google.com"
}
)");
EXPECT_EQ(expected, entries[0].params);
expected = base::JSONReader::Read(R"(
{
"context": {
"authority": "Dodgy Server",
"flags": {
"delegated": false,
"mutual": false,
"value": "0x00000000"
},
"mechanism": "Itsa me Kerberos!!",
"open": true,
"source": "\u003CDefault>",
"target": "HTTP/intranet.google.com"
},
"status": {
"net_error": 0,
"security_status": 0
}
}
)");
EXPECT_EQ(expected, entries[1].params);
expected = base::JSONReader::Read(R"(
{
"context": {
"authority": "Dodgy Server",
"flags": {
"delegated": false,
"mutual": false,
"value": "0x00000000"
},
"mechanism": "Itsa me Kerberos!!",
"open": false,
"source": "\u003CDefault>",
"target": "HTTP/intranet.google.com"
},
"status": {
"net_error": 0,
"security_status": 0
}
}
)");
EXPECT_EQ(expected, entries[3].params);
}
} // namespace net
|