2016-03-14 02:51:46 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2016-03-14 02:51:46 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2016-03-14 02:51:46 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
2022-05-17 17:16:50 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
|
|
|
*
|
2016-03-14 02:51:46 +08:00
|
|
|
* RFC4178 Simple and Protected GSS-API Negotiation Mechanism
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_GSSAPI) && defined(USE_SPNEGO)
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include "vauth/vauth.h"
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "curl_base64.h"
|
|
|
|
#include "curl_gssapi.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "curl_multibyte.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
|
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
2016-03-20 19:56:07 +08:00
|
|
|
/*
|
|
|
|
* Curl_auth_is_spnego_supported()
|
|
|
|
*
|
|
|
|
* This is used to evaluate if SPNEGO (Negotiate) is supported.
|
|
|
|
*
|
|
|
|
* Parameters: None
|
|
|
|
*
|
|
|
|
* Returns TRUE if Negotiate supported by the GSS-API library.
|
|
|
|
*/
|
|
|
|
bool Curl_auth_is_spnego_supported(void)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-03-14 02:51:46 +08:00
|
|
|
/*
|
|
|
|
* Curl_auth_decode_spnego_message()
|
|
|
|
*
|
|
|
|
* This is used to decode an already encoded SPNEGO (Negotiate) challenge
|
|
|
|
* message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
|
|
|
* userp [in] - The user name in the format User or Domain\User.
|
2018-10-03 16:27:27 +08:00
|
|
|
* passwdp [in] - The user's password.
|
2016-04-06 07:21:07 +08:00
|
|
|
* service [in] - The service type such as http, smtp, pop or imap.
|
2016-04-02 02:07:49 +08:00
|
|
|
* host [in] - The host name.
|
2016-03-14 02:51:46 +08:00
|
|
|
* chlg64 [in] - The optional base64 encoded challenge message.
|
|
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
2016-06-21 21:47:12 +08:00
|
|
|
CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
|
2016-03-14 02:51:46 +08:00
|
|
|
const char *user,
|
|
|
|
const char *password,
|
|
|
|
const char *service,
|
|
|
|
const char *host,
|
|
|
|
const char *chlg64,
|
|
|
|
struct negotiatedata *nego)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
size_t chlglen = 0;
|
|
|
|
unsigned char *chlg = NULL;
|
|
|
|
OM_uint32 major_status;
|
|
|
|
OM_uint32 minor_status;
|
|
|
|
OM_uint32 unused_status;
|
|
|
|
gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
|
|
|
|
(void) user;
|
|
|
|
(void) password;
|
|
|
|
|
|
|
|
if(nego->context && nego->status == GSS_S_COMPLETE) {
|
|
|
|
/* We finished successfully our part of authentication, but server
|
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
2019-05-11 19:57:42 +08:00
|
|
|
Curl_auth_cleanup_spnego(nego);
|
2016-03-14 02:51:46 +08:00
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
2016-04-02 13:41:29 +08:00
|
|
|
if(!nego->spn) {
|
2016-04-04 00:32:10 +08:00
|
|
|
/* Generate our SPN */
|
2016-04-04 03:26:03 +08:00
|
|
|
char *spn = Curl_auth_build_spn(service, NULL, host);
|
2016-03-14 02:51:46 +08:00
|
|
|
if(!spn)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Populate the SPN structure */
|
|
|
|
spn_token.value = spn;
|
|
|
|
spn_token.length = strlen(spn);
|
|
|
|
|
|
|
|
/* Import the SPN */
|
|
|
|
major_status = gss_import_name(&minor_status, &spn_token,
|
|
|
|
GSS_C_NT_HOSTBASED_SERVICE,
|
2016-04-02 13:41:29 +08:00
|
|
|
&nego->spn);
|
2016-03-14 02:51:46 +08:00
|
|
|
if(GSS_ERROR(major_status)) {
|
2016-03-29 22:13:46 +08:00
|
|
|
Curl_gss_log_error(data, "gss_import_name() failed: ",
|
|
|
|
major_status, minor_status);
|
2016-03-14 02:51:46 +08:00
|
|
|
|
|
|
|
free(spn);
|
|
|
|
|
2019-05-11 14:23:09 +08:00
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-14 02:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free(spn);
|
|
|
|
}
|
|
|
|
|
2016-04-04 00:32:10 +08:00
|
|
|
if(chlg64 && *chlg64) {
|
2016-03-14 02:51:46 +08:00
|
|
|
/* Decode the base-64 encoded challenge message */
|
|
|
|
if(*chlg64 != '=') {
|
|
|
|
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure we have a valid challenge message */
|
|
|
|
if(!chlg) {
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "SPNEGO handshake failure (empty challenge message)");
|
2016-03-14 02:51:46 +08:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup the challenge "input" security buffer */
|
|
|
|
input_token.value = chlg;
|
|
|
|
input_token.length = chlglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generate our challenge-response message */
|
|
|
|
major_status = Curl_gss_init_sec_context(data,
|
|
|
|
&minor_status,
|
|
|
|
&nego->context,
|
2016-04-02 13:41:29 +08:00
|
|
|
nego->spn,
|
2016-03-14 02:51:46 +08:00
|
|
|
&Curl_spnego_mech_oid,
|
|
|
|
GSS_C_NO_CHANNEL_BINDINGS,
|
|
|
|
&input_token,
|
|
|
|
&output_token,
|
|
|
|
TRUE,
|
|
|
|
NULL);
|
2016-04-04 00:32:10 +08:00
|
|
|
|
|
|
|
/* Free the decoded challenge as it is not required anymore */
|
2016-03-14 02:51:46 +08:00
|
|
|
Curl_safefree(input_token.value);
|
|
|
|
|
|
|
|
nego->status = major_status;
|
|
|
|
if(GSS_ERROR(major_status)) {
|
|
|
|
if(output_token.value)
|
|
|
|
gss_release_buffer(&unused_status, &output_token);
|
|
|
|
|
2016-03-29 22:13:46 +08:00
|
|
|
Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
|
|
|
|
major_status, minor_status);
|
2016-03-14 02:51:46 +08:00
|
|
|
|
2019-08-14 15:47:17 +08:00
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-14 02:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!output_token.value || !output_token.length) {
|
|
|
|
if(output_token.value)
|
|
|
|
gss_release_buffer(&unused_status, &output_token);
|
|
|
|
|
2019-05-11 14:23:09 +08:00
|
|
|
return CURLE_AUTH_ERROR;
|
2016-03-14 02:51:46 +08:00
|
|
|
}
|
|
|
|
|
2017-07-22 07:00:46 +08:00
|
|
|
/* Free previous token */
|
|
|
|
if(nego->output_token.length && nego->output_token.value)
|
|
|
|
gss_release_buffer(&unused_status, &nego->output_token);
|
|
|
|
|
2016-03-14 02:51:46 +08:00
|
|
|
nego->output_token = output_token;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_auth_create_spnego_message()
|
|
|
|
*
|
|
|
|
* This is used to generate an already encoded SPNEGO (Negotiate) response
|
|
|
|
* message ready for sending to the recipient.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* data [in] - The session handle.
|
|
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
|
|
* outptr [in/out] - The address where a pointer to newly allocated memory
|
|
|
|
* holding the result will be stored upon completion.
|
|
|
|
* outlen [out] - The length of the output message.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
2022-02-03 20:04:30 +08:00
|
|
|
CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
|
2016-03-14 02:51:46 +08:00
|
|
|
char **outptr, size_t *outlen)
|
|
|
|
{
|
|
|
|
CURLcode result;
|
|
|
|
OM_uint32 minor_status;
|
|
|
|
|
|
|
|
/* Base64 encode the already generated response */
|
2022-02-03 20:04:30 +08:00
|
|
|
result = Curl_base64_encode(nego->output_token.value,
|
2016-03-14 02:51:46 +08:00
|
|
|
nego->output_token.length,
|
|
|
|
outptr, outlen);
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
gss_release_buffer(&minor_status, &nego->output_token);
|
|
|
|
nego->output_token.value = NULL;
|
|
|
|
nego->output_token.length = 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!*outptr || !*outlen) {
|
|
|
|
gss_release_buffer(&minor_status, &nego->output_token);
|
|
|
|
nego->output_token.value = NULL;
|
|
|
|
nego->output_token.length = 0;
|
|
|
|
|
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-05-11 19:57:42 +08:00
|
|
|
* Curl_auth_cleanup_spnego()
|
2016-03-14 02:51:46 +08:00
|
|
|
*
|
|
|
|
* This is used to clean up the SPNEGO (Negotiate) specific data.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* nego [in/out] - The Negotiate data struct being cleaned up.
|
|
|
|
*
|
|
|
|
*/
|
2019-05-11 19:57:42 +08:00
|
|
|
void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
|
2016-03-14 02:51:46 +08:00
|
|
|
{
|
|
|
|
OM_uint32 minor_status;
|
|
|
|
|
|
|
|
/* Free our security context */
|
|
|
|
if(nego->context != GSS_C_NO_CONTEXT) {
|
|
|
|
gss_delete_sec_context(&minor_status, &nego->context, GSS_C_NO_BUFFER);
|
|
|
|
nego->context = GSS_C_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the output token */
|
|
|
|
if(nego->output_token.value) {
|
|
|
|
gss_release_buffer(&minor_status, &nego->output_token);
|
|
|
|
nego->output_token.value = NULL;
|
|
|
|
nego->output_token.length = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the SPN */
|
2016-04-02 13:41:29 +08:00
|
|
|
if(nego->spn != GSS_C_NO_NAME) {
|
|
|
|
gss_release_name(&minor_status, &nego->spn);
|
|
|
|
nego->spn = GSS_C_NO_NAME;
|
2016-03-14 02:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset any variables */
|
|
|
|
nego->status = 0;
|
2018-09-10 15:18:01 +08:00
|
|
|
nego->noauthpersist = FALSE;
|
|
|
|
nego->havenoauthpersist = FALSE;
|
|
|
|
nego->havenegdata = FALSE;
|
|
|
|
nego->havemultiplerequests = FALSE;
|
2016-03-14 02:51:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_GSSAPI && USE_SPNEGO */
|