2012-05-30 15:52:52 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-03-12 06:56:10 -05:00
|
|
|
* Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-05-30 15:52:52 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2012-05-30 15:52:52 -04: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.
|
|
|
|
*
|
2012-06-03 12:21:49 -04:00
|
|
|
* RFC2195 CRAM-MD5 authentication
|
2014-11-05 09:33:26 -05:00
|
|
|
* RFC2617 Basic and Digest Access Authentication
|
2012-06-04 05:53:18 -04:00
|
|
|
* RFC2831 DIGEST-MD5 authentication
|
2013-01-22 17:02:30 -05:00
|
|
|
* RFC4422 Simple Authentication and Security Layer (SASL)
|
2012-05-30 15:52:52 -04:00
|
|
|
* RFC4616 PLAIN authentication
|
2013-08-25 13:17:01 -04:00
|
|
|
* RFC6749 OAuth 2.0 Authorization Framework
|
2015-09-04 02:11:09 -04:00
|
|
|
* RFC7628 A Set of SASL Mechanisms for OAuth
|
2013-10-26 09:35:27 -04:00
|
|
|
* Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
|
2012-05-30 15:52:52 -04:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2012-05-30 15:52:52 -04:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
2012-05-30 15:52:52 -04:00
|
|
|
|
|
|
|
#include "curl_base64.h"
|
2012-06-03 12:21:49 -04:00
|
|
|
#include "curl_md5.h"
|
2015-09-12 06:48:24 -04:00
|
|
|
#include "vauth/vauth.h"
|
2013-12-17 17:32:47 -05:00
|
|
|
#include "vtls/vtls.h"
|
2012-06-03 12:21:49 -04:00
|
|
|
#include "curl_hmac.h"
|
2012-05-30 15:52:52 -04:00
|
|
|
#include "curl_sasl.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "warnless.h"
|
2014-04-08 16:08:02 -04:00
|
|
|
#include "strtok.h"
|
2015-01-20 09:27:25 -05:00
|
|
|
#include "strequal.h"
|
2014-04-08 16:08:02 -04:00
|
|
|
#include "rawstr.h"
|
2015-01-20 11:33:05 -05:00
|
|
|
#include "sendf.h"
|
2014-11-05 10:01:51 -05:00
|
|
|
#include "non-ascii.h" /* included for Curl_convert_... prototypes */
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2015-03-24 18:12:03 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2012-05-30 15:52:52 -04:00
|
|
|
|
2015-01-20 14:21:56 -05:00
|
|
|
/* Supported mechanisms */
|
|
|
|
const struct {
|
2015-01-22 15:47:38 -05:00
|
|
|
const char *name; /* Name */
|
2015-01-20 14:21:56 -05:00
|
|
|
size_t len; /* Name length */
|
|
|
|
unsigned int bit; /* Flag bit */
|
|
|
|
} mechtable[] = {
|
2015-09-05 13:09:40 -04:00
|
|
|
{ "LOGIN", 5, SASL_MECH_LOGIN },
|
|
|
|
{ "PLAIN", 5, SASL_MECH_PLAIN },
|
|
|
|
{ "CRAM-MD5", 8, SASL_MECH_CRAM_MD5 },
|
|
|
|
{ "DIGEST-MD5", 10, SASL_MECH_DIGEST_MD5 },
|
|
|
|
{ "GSSAPI", 6, SASL_MECH_GSSAPI },
|
|
|
|
{ "EXTERNAL", 8, SASL_MECH_EXTERNAL },
|
|
|
|
{ "NTLM", 4, SASL_MECH_NTLM },
|
|
|
|
{ "XOAUTH2", 7, SASL_MECH_XOAUTH2 },
|
|
|
|
{ "OAUTHBEARER", 11, SASL_MECH_OAUTHBEARER },
|
|
|
|
{ ZERO_NULL, 0, 0 }
|
2015-01-20 14:21:56 -05:00
|
|
|
};
|
|
|
|
|
2012-06-02 06:07:58 -04:00
|
|
|
/*
|
|
|
|
* Curl_sasl_cleanup()
|
|
|
|
*
|
|
|
|
* This is used to cleanup any libraries or curl modules used by the sasl
|
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
2014-11-23 11:31:39 -05:00
|
|
|
* conn [in] - The connection data.
|
2012-06-02 06:07:58 -04:00
|
|
|
* authused [in] - The authentication mechanism used.
|
|
|
|
*/
|
|
|
|
void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
|
|
|
|
{
|
2014-11-16 04:50:06 -05:00
|
|
|
#if defined(USE_KERBEROS5)
|
2014-08-13 19:03:45 -04:00
|
|
|
/* Cleanup the gssapi structure */
|
|
|
|
if(authused == SASL_MECH_GSSAPI) {
|
2016-03-13 07:28:42 -04:00
|
|
|
Curl_auth_gssapi_cleanup(&conn->krb5);
|
2014-08-13 19:03:45 -04:00
|
|
|
}
|
2014-11-09 06:25:10 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_NTLM)
|
2016-03-12 06:56:10 -05:00
|
|
|
/* Cleanup the NTLM structure */
|
2014-11-09 06:25:10 -05:00
|
|
|
if(authused == SASL_MECH_NTLM) {
|
2016-03-13 07:28:42 -04:00
|
|
|
Curl_auth_ntlm_cleanup(&conn->ntlm);
|
2012-06-02 06:07:58 -04:00
|
|
|
}
|
2014-08-13 19:03:45 -04:00
|
|
|
#endif
|
2014-11-09 06:25:10 -05:00
|
|
|
|
2014-11-16 04:50:06 -05:00
|
|
|
#if !defined(USE_KERBEROS5) && !defined(USE_NTLM)
|
2012-06-02 06:07:58 -04:00
|
|
|
/* Reserved for future use */
|
|
|
|
(void)conn;
|
|
|
|
(void)authused;
|
|
|
|
#endif
|
2012-06-02 19:00:34 -04:00
|
|
|
}
|
2015-01-20 09:27:25 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_decode_mech()
|
|
|
|
*
|
2015-01-28 14:11:19 -05:00
|
|
|
* Convert a SASL mechanism name into a token.
|
2015-01-20 09:27:25 -05:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ptr [in] - The mechanism string.
|
|
|
|
* maxlen [in] - Maximum mechanism string length.
|
|
|
|
* len [out] - If not NULL, effective name length.
|
|
|
|
*
|
2015-01-28 14:11:19 -05:00
|
|
|
* Returns the SASL mechanism token or 0 if no match.
|
2015-01-20 09:27:25 -05:00
|
|
|
*/
|
2015-01-22 15:47:38 -05:00
|
|
|
unsigned int Curl_sasl_decode_mech(const char *ptr, size_t maxlen, size_t *len)
|
2015-01-20 09:27:25 -05:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
for(i = 0; mechtable[i].name; i++) {
|
|
|
|
if(maxlen >= mechtable[i].len &&
|
|
|
|
!memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
|
|
|
|
if(len)
|
|
|
|
*len = mechtable[i].len;
|
2015-01-22 15:47:38 -05:00
|
|
|
|
2015-01-20 09:27:25 -05:00
|
|
|
if(maxlen == mechtable[i].len)
|
|
|
|
return mechtable[i].bit;
|
2015-01-22 15:47:38 -05:00
|
|
|
|
2015-01-20 09:27:25 -05:00
|
|
|
c = ptr[mechtable[i].len];
|
|
|
|
if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
|
|
|
|
return mechtable[i].bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_parse_url_auth_option()
|
|
|
|
*
|
|
|
|
* Parse the URL login options.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
|
|
|
|
const char *value, size_t len)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
unsigned int mechbit;
|
2015-01-22 15:47:38 -05:00
|
|
|
size_t mechlen;
|
2015-01-20 09:27:25 -05:00
|
|
|
|
|
|
|
if(!len)
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
|
2016-03-14 04:55:38 -04:00
|
|
|
if(sasl->resetprefs) {
|
|
|
|
sasl->resetprefs = FALSE;
|
|
|
|
sasl->prefmech = SASL_AUTH_NONE;
|
|
|
|
}
|
2015-01-20 09:27:25 -05:00
|
|
|
|
2016-03-14 04:55:38 -04:00
|
|
|
if(strnequal(value, "*", len))
|
|
|
|
sasl->prefmech = SASL_AUTH_DEFAULT;
|
2016-03-19 13:15:53 -04:00
|
|
|
else {
|
|
|
|
mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
|
|
|
|
if(mechbit && mechlen == len)
|
|
|
|
sasl->prefmech |= mechbit;
|
|
|
|
else
|
|
|
|
result = CURLE_URL_MALFORMAT;
|
|
|
|
}
|
2015-01-20 09:27:25 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_init()
|
|
|
|
*
|
2015-01-28 14:11:19 -05:00
|
|
|
* Initializes the SASL structure.
|
2015-01-20 09:27:25 -05:00
|
|
|
*/
|
2015-01-20 11:33:05 -05:00
|
|
|
void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params)
|
2015-01-20 09:27:25 -05:00
|
|
|
{
|
2015-01-20 11:33:05 -05:00
|
|
|
sasl->params = params; /* Set protocol dependent parameters */
|
|
|
|
sasl->state = SASL_STOP; /* Not yet running */
|
2015-01-20 09:27:25 -05:00
|
|
|
sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
|
2015-01-27 11:24:55 -05:00
|
|
|
sasl->prefmech = SASL_AUTH_DEFAULT; /* Prefer all mechanisms */
|
2015-01-20 09:27:25 -05:00
|
|
|
sasl->authused = SASL_AUTH_NONE; /* No the authentication mechanism used */
|
|
|
|
sasl->resetprefs = TRUE; /* Reset prefmech upon AUTH parsing. */
|
|
|
|
sasl->mutual_auth = FALSE; /* No mutual authentication (GSSAPI only) */
|
2015-01-20 11:33:05 -05:00
|
|
|
sasl->force_ir = FALSE; /* Respect external option */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* state()
|
|
|
|
*
|
|
|
|
* This is the ONLY way to change SASL state!
|
|
|
|
*/
|
2015-02-02 11:44:00 -05:00
|
|
|
static void state(struct SASL *sasl, struct connectdata *conn,
|
|
|
|
saslstate newstate)
|
2015-01-20 11:33:05 -05:00
|
|
|
{
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* for debug purposes */
|
|
|
|
static const char * const names[]={
|
|
|
|
"STOP",
|
|
|
|
"PLAIN",
|
|
|
|
"LOGIN",
|
|
|
|
"LOGIN_PASSWD",
|
2015-01-27 11:24:55 -05:00
|
|
|
"EXTERNAL",
|
2015-01-20 11:33:05 -05:00
|
|
|
"CRAMMD5",
|
|
|
|
"DIGESTMD5",
|
|
|
|
"DIGESTMD5_RESP",
|
|
|
|
"NTLM",
|
|
|
|
"NTLM_TYPE2MSG",
|
|
|
|
"GSSAPI",
|
|
|
|
"GSSAPI_TOKEN",
|
|
|
|
"GSSAPI_NO_DATA",
|
2015-09-05 12:32:37 -04:00
|
|
|
"OAUTH2",
|
2015-09-05 13:35:47 -04:00
|
|
|
"OAUTH2_RESP",
|
2015-01-20 11:33:05 -05:00
|
|
|
"CANCEL",
|
|
|
|
"FINAL",
|
|
|
|
/* LAST */
|
|
|
|
};
|
|
|
|
|
|
|
|
if(sasl->state != newstate)
|
|
|
|
infof(conn->data, "SASL %p state change from %s to %s\n",
|
|
|
|
(void *)sasl, names[sasl->state], names[newstate]);
|
2015-01-28 17:48:01 -05:00
|
|
|
#else
|
|
|
|
(void) conn;
|
2015-01-20 11:33:05 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
sasl->state = newstate;
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:24:55 -05:00
|
|
|
/*
|
|
|
|
* Curl_sasl_can_authenticate()
|
|
|
|
*
|
|
|
|
* Check if we have enough auth data and capabilities to authenticate.
|
|
|
|
*/
|
|
|
|
bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn)
|
|
|
|
{
|
2015-02-02 11:44:00 -05:00
|
|
|
/* Have credentials been provided? */
|
2015-01-27 11:24:55 -05:00
|
|
|
if(conn->bits.user_passwd)
|
2015-02-02 11:44:00 -05:00
|
|
|
return TRUE;
|
2015-01-27 11:24:55 -05:00
|
|
|
|
2015-02-02 11:44:00 -05:00
|
|
|
/* EXTERNAL can authenticate without a user name and/or password */
|
2015-01-27 11:24:55 -05:00
|
|
|
if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
|
2015-02-02 11:44:00 -05:00
|
|
|
return TRUE;
|
2015-01-27 11:24:55 -05:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-20 11:33:05 -05:00
|
|
|
/*
|
|
|
|
* Curl_sasl_start()
|
|
|
|
*
|
|
|
|
* Calculate the required login details for SASL authentication.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_start(struct SASL *sasl, struct connectdata *conn,
|
|
|
|
bool force_ir, saslprogress *progress)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-01-20 11:33:05 -05:00
|
|
|
unsigned int enabledmechs;
|
|
|
|
const char *mech = NULL;
|
|
|
|
char *resp = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
saslstate state1 = SASL_STOP;
|
|
|
|
saslstate state2 = SASL_FINAL;
|
2016-04-09 00:57:10 -04:00
|
|
|
#if defined(USE_KERBEROS5)
|
2016-04-08 13:59:33 -04:00
|
|
|
const char* service = data->set.str[STRING_SERVICE_NAME] ?
|
|
|
|
data->set.str[STRING_SERVICE_NAME] :
|
|
|
|
sasl->params->service;
|
2016-04-09 00:57:10 -04:00
|
|
|
#endif
|
2015-01-20 11:33:05 -05:00
|
|
|
|
|
|
|
sasl->force_ir = force_ir; /* Latch for future use */
|
|
|
|
sasl->authused = 0; /* No mechanism used yet */
|
|
|
|
enabledmechs = sasl->authmechs & sasl->prefmech;
|
|
|
|
*progress = SASL_IDLE;
|
|
|
|
|
|
|
|
/* Calculate the supported authentication mechanism, by decreasing order of
|
2015-01-28 14:11:19 -05:00
|
|
|
security, as well as the initial response where appropriate */
|
2015-01-27 11:24:55 -05:00
|
|
|
if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
|
|
|
|
mech = SASL_MECH_STRING_EXTERNAL;
|
|
|
|
state1 = SASL_EXTERNAL;
|
|
|
|
sasl->authused = SASL_MECH_EXTERNAL;
|
2015-01-20 11:33:05 -05:00
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_external_message(data, conn->user, &resp,
|
|
|
|
&len);
|
2015-01-20 11:33:05 -05:00
|
|
|
}
|
2015-01-27 11:24:55 -05:00
|
|
|
else if(conn->bits.user_passwd) {
|
|
|
|
#if defined(USE_KERBEROS5)
|
|
|
|
if(enabledmechs & SASL_MECH_GSSAPI) {
|
|
|
|
sasl->mutual_auth = FALSE; /* TODO: Calculate mutual authentication */
|
|
|
|
mech = SASL_MECH_STRING_GSSAPI;
|
|
|
|
state1 = SASL_GSSAPI;
|
|
|
|
state2 = SASL_GSSAPI_TOKEN;
|
|
|
|
sasl->authused = SASL_MECH_GSSAPI;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_gssapi_user_message(data, conn->user,
|
2015-01-27 11:24:55 -05:00
|
|
|
conn->passwd,
|
2016-04-08 13:59:33 -04:00
|
|
|
service,
|
2016-04-02 01:15:29 -04:00
|
|
|
data->easy_conn->
|
|
|
|
host.name,
|
2015-01-27 11:24:55 -05:00
|
|
|
sasl->mutual_auth,
|
|
|
|
NULL, &conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
}
|
|
|
|
else
|
2015-01-20 11:33:05 -05:00
|
|
|
#endif
|
|
|
|
#ifndef CURL_DISABLE_CRYPTO_AUTH
|
2015-01-27 11:24:55 -05:00
|
|
|
if(enabledmechs & SASL_MECH_DIGEST_MD5) {
|
|
|
|
mech = SASL_MECH_STRING_DIGEST_MD5;
|
|
|
|
state1 = SASL_DIGESTMD5;
|
|
|
|
sasl->authused = SASL_MECH_DIGEST_MD5;
|
|
|
|
}
|
|
|
|
else if(enabledmechs & SASL_MECH_CRAM_MD5) {
|
|
|
|
mech = SASL_MECH_STRING_CRAM_MD5;
|
|
|
|
state1 = SASL_CRAMMD5;
|
|
|
|
sasl->authused = SASL_MECH_CRAM_MD5;
|
|
|
|
}
|
|
|
|
else
|
2015-01-20 11:33:05 -05:00
|
|
|
#endif
|
|
|
|
#ifdef USE_NTLM
|
2015-01-27 11:24:55 -05:00
|
|
|
if(enabledmechs & SASL_MECH_NTLM) {
|
|
|
|
mech = SASL_MECH_STRING_NTLM;
|
|
|
|
state1 = SASL_NTLM;
|
|
|
|
state2 = SASL_NTLM_TYPE2MSG;
|
|
|
|
sasl->authused = SASL_MECH_NTLM;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_ntlm_type1_message(conn->user, conn->passwd,
|
2015-01-27 11:24:55 -05:00
|
|
|
&conn->ntlm, &resp, &len);
|
|
|
|
}
|
|
|
|
else
|
2015-01-20 11:33:05 -05:00
|
|
|
#endif
|
2015-09-05 13:09:40 -04:00
|
|
|
if((enabledmechs & SASL_MECH_OAUTHBEARER) && conn->oauth_bearer) {
|
|
|
|
mech = SASL_MECH_STRING_OAUTHBEARER;
|
|
|
|
state1 = SASL_OAUTH2;
|
2015-09-05 13:35:47 -04:00
|
|
|
state2 = SASL_OAUTH2_RESP;
|
2015-09-05 13:09:40 -04:00
|
|
|
sasl->authused = SASL_MECH_OAUTHBEARER;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_oauth_bearer_message(data, conn->user,
|
|
|
|
conn->host.name,
|
|
|
|
conn->port,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-09-05 13:09:40 -04:00
|
|
|
}
|
|
|
|
else if((enabledmechs & SASL_MECH_XOAUTH2) && conn->oauth_bearer) {
|
2015-01-27 11:24:55 -05:00
|
|
|
mech = SASL_MECH_STRING_XOAUTH2;
|
2015-09-05 12:32:37 -04:00
|
|
|
state1 = SASL_OAUTH2;
|
2015-01-27 11:24:55 -05:00
|
|
|
sasl->authused = SASL_MECH_XOAUTH2;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_oauth_bearer_message(data, conn->user,
|
|
|
|
NULL, 0,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-01-27 11:24:55 -05:00
|
|
|
}
|
|
|
|
else if(enabledmechs & SASL_MECH_LOGIN) {
|
|
|
|
mech = SASL_MECH_STRING_LOGIN;
|
|
|
|
state1 = SASL_LOGIN;
|
|
|
|
state2 = SASL_LOGIN_PASSWD;
|
|
|
|
sasl->authused = SASL_MECH_LOGIN;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_login_message(data, conn->user, &resp, &len);
|
2015-01-27 11:24:55 -05:00
|
|
|
}
|
|
|
|
else if(enabledmechs & SASL_MECH_PLAIN) {
|
|
|
|
mech = SASL_MECH_STRING_PLAIN;
|
|
|
|
state1 = SASL_PLAIN;
|
|
|
|
sasl->authused = SASL_MECH_PLAIN;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_plain_message(data, conn->user, conn->passwd,
|
|
|
|
&resp, &len);
|
2015-01-27 11:24:55 -05:00
|
|
|
}
|
2015-01-20 11:33:05 -05:00
|
|
|
}
|
|
|
|
|
2016-04-03 12:55:17 -04:00
|
|
|
if(!result && mech) {
|
2015-01-20 11:33:05 -05:00
|
|
|
if(resp && sasl->params->maxirlen &&
|
|
|
|
strlen(mech) + len > sasl->params->maxirlen) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(resp);
|
2015-01-20 11:33:05 -05:00
|
|
|
resp = NULL;
|
|
|
|
}
|
2015-02-02 11:44:00 -05:00
|
|
|
|
2016-04-03 12:55:17 -04:00
|
|
|
result = sasl->params->sendauth(conn, mech, resp);
|
|
|
|
if(!result) {
|
|
|
|
*progress = SASL_INPROGRESS;
|
|
|
|
state(sasl, conn, resp ? state2 : state1);
|
2015-01-20 11:33:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(resp);
|
2015-01-22 15:47:38 -05:00
|
|
|
|
2015-01-20 11:33:05 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_continue()
|
|
|
|
*
|
2015-01-28 14:11:19 -05:00
|
|
|
* Continue the authentication.
|
2015-01-20 11:33:05 -05:00
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_continue(struct SASL *sasl, struct connectdata *conn,
|
2015-01-22 15:47:38 -05:00
|
|
|
int code, saslprogress *progress)
|
2015-01-20 11:33:05 -05:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-01-20 11:33:05 -05:00
|
|
|
saslstate newstate = SASL_FINAL;
|
|
|
|
char *resp = NULL;
|
2015-01-20 14:25:43 -05:00
|
|
|
#if !defined(CURL_DISABLE_CRYPTO_AUTH)
|
2015-01-20 11:33:05 -05:00
|
|
|
char *serverdata;
|
2015-01-29 06:48:11 -05:00
|
|
|
char *chlg = NULL;
|
2015-01-20 14:28:54 -05:00
|
|
|
size_t chlglen = 0;
|
2015-01-20 14:25:43 -05:00
|
|
|
#endif
|
2016-04-09 00:57:10 -04:00
|
|
|
#if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5)
|
2016-04-08 13:59:33 -04:00
|
|
|
const char *service = data->set.str[STRING_SERVICE_NAME] ?
|
|
|
|
data->set.str[STRING_SERVICE_NAME] :
|
|
|
|
sasl->params->service;
|
2016-04-09 00:57:10 -04:00
|
|
|
#endif
|
|
|
|
size_t len = 0;
|
2015-01-20 11:33:05 -05:00
|
|
|
|
|
|
|
*progress = SASL_INPROGRESS;
|
|
|
|
|
|
|
|
if(sasl->state == SASL_FINAL) {
|
|
|
|
if(code != sasl->params->finalcode)
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-09-05 13:35:47 -04:00
|
|
|
if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
|
|
|
|
code != sasl->params->contcode) {
|
2015-01-20 11:33:05 -05:00
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(sasl->state) {
|
|
|
|
case SASL_STOP:
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
return result;
|
|
|
|
case SASL_PLAIN:
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_plain_message(data, conn->user, conn->passwd,
|
|
|
|
&resp,
|
|
|
|
&len);
|
2015-01-20 11:33:05 -05:00
|
|
|
break;
|
|
|
|
case SASL_LOGIN:
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_login_message(data, conn->user, &resp, &len);
|
2015-01-20 11:33:05 -05:00
|
|
|
newstate = SASL_LOGIN_PASSWD;
|
|
|
|
break;
|
|
|
|
case SASL_LOGIN_PASSWD:
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_login_message(data, conn->passwd, &resp, &len);
|
2015-01-20 11:33:05 -05:00
|
|
|
break;
|
2015-01-27 11:24:55 -05:00
|
|
|
case SASL_EXTERNAL:
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_external_message(data, conn->user, &resp, &len);
|
2015-01-27 11:24:55 -05:00
|
|
|
break;
|
2015-01-20 11:33:05 -05:00
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_CRYPTO_AUTH
|
|
|
|
case SASL_CRAMMD5:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_decode_cram_md5_message(serverdata, &chlg, &chlglen);
|
2015-01-20 11:33:05 -05:00
|
|
|
if(!result)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_cram_md5_message(data, chlg, conn->user,
|
|
|
|
conn->passwd, &resp, &len);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(chlg);
|
2015-01-20 11:33:05 -05:00
|
|
|
break;
|
|
|
|
case SASL_DIGESTMD5:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_digest_md5_message(data, serverdata,
|
2015-01-20 11:33:05 -05:00
|
|
|
conn->user, conn->passwd,
|
2016-04-08 13:59:33 -04:00
|
|
|
service,
|
2015-01-20 11:33:05 -05:00
|
|
|
&resp, &len);
|
|
|
|
newstate = SASL_DIGESTMD5_RESP;
|
|
|
|
break;
|
|
|
|
case SASL_DIGESTMD5_RESP:
|
2016-03-19 13:15:53 -04:00
|
|
|
resp = strdup("");
|
|
|
|
if(!resp)
|
2015-01-20 11:33:05 -05:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NTLM
|
|
|
|
case SASL_NTLM:
|
|
|
|
/* Create the type-1 message */
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_ntlm_type1_message(conn->user, conn->passwd,
|
2015-02-02 11:44:00 -05:00
|
|
|
&conn->ntlm, &resp, &len);
|
2015-01-20 11:33:05 -05:00
|
|
|
newstate = SASL_NTLM_TYPE2MSG;
|
|
|
|
break;
|
|
|
|
case SASL_NTLM_TYPE2MSG:
|
|
|
|
/* Decode the type-2 message */
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_decode_ntlm_type2_message(data, serverdata,
|
2015-02-02 11:44:00 -05:00
|
|
|
&conn->ntlm);
|
2015-01-20 11:33:05 -05:00
|
|
|
if(!result)
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_ntlm_type3_message(data, conn->user,
|
2015-01-20 11:33:05 -05:00
|
|
|
conn->passwd, &conn->ntlm,
|
|
|
|
&resp, &len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_KERBEROS5)
|
|
|
|
case SASL_GSSAPI:
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_gssapi_user_message(data, conn->user,
|
2015-01-20 11:33:05 -05:00
|
|
|
conn->passwd,
|
2016-04-08 13:59:33 -04:00
|
|
|
service,
|
2016-04-02 01:15:29 -04:00
|
|
|
data->easy_conn->host.name,
|
2015-01-20 11:33:05 -05:00
|
|
|
sasl->mutual_auth, NULL,
|
|
|
|
&conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
newstate = SASL_GSSAPI_TOKEN;
|
|
|
|
break;
|
|
|
|
case SASL_GSSAPI_TOKEN:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
|
|
|
if(sasl->mutual_auth) {
|
|
|
|
/* Decode the user token challenge and create the optional response
|
|
|
|
message */
|
2016-04-02 01:15:29 -04:00
|
|
|
result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
|
|
|
|
NULL, NULL,
|
2015-01-20 11:33:05 -05:00
|
|
|
sasl->mutual_auth,
|
|
|
|
serverdata, &conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
newstate = SASL_GSSAPI_NO_DATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Decode the security challenge and create the response message */
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_gssapi_security_message(data, serverdata,
|
2015-01-20 11:33:05 -05:00
|
|
|
&conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
break;
|
|
|
|
case SASL_GSSAPI_NO_DATA:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
|
|
|
/* Decode the security challenge and create the response message */
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_gssapi_security_message(data, serverdata,
|
2015-01-20 11:33:05 -05:00
|
|
|
&conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-09-05 12:32:37 -04:00
|
|
|
case SASL_OAUTH2:
|
2015-01-20 11:33:05 -05:00
|
|
|
/* Create the authorisation message */
|
2015-09-05 13:35:47 -04:00
|
|
|
if(sasl->authused == SASL_MECH_OAUTHBEARER) {
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_oauth_bearer_message(data, conn->user,
|
|
|
|
conn->host.name,
|
|
|
|
conn->port,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-09-05 13:35:47 -04:00
|
|
|
|
|
|
|
/* Failures maybe sent by the server as continuations for OAUTHBEARER */
|
|
|
|
newstate = SASL_OAUTH2_RESP;
|
|
|
|
}
|
2015-09-05 13:09:40 -04:00
|
|
|
else
|
2016-03-13 07:28:42 -04:00
|
|
|
result = Curl_auth_create_oauth_bearer_message(data, conn->user,
|
|
|
|
NULL, 0,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-01-20 11:33:05 -05:00
|
|
|
break;
|
2015-09-05 13:35:47 -04:00
|
|
|
|
|
|
|
case SASL_OAUTH2_RESP:
|
|
|
|
/* The continuation is optional so check the response code */
|
2015-11-15 17:15:00 -05:00
|
|
|
if(code == sasl->params->finalcode) {
|
2015-09-05 13:35:47 -04:00
|
|
|
/* Final response was received so we are done */
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return result;
|
|
|
|
}
|
2015-11-15 17:15:00 -05:00
|
|
|
else if(code == sasl->params->contcode) {
|
|
|
|
/* Acknowledge the continuation by sending a 0x01 response base64
|
|
|
|
encoded */
|
|
|
|
resp = strdup("AQ==");
|
|
|
|
if(!resp)
|
2015-09-05 13:35:47 -04:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
2015-01-20 11:33:05 -05:00
|
|
|
case SASL_CANCEL:
|
|
|
|
/* Remove the offending mechanism from the supported list */
|
|
|
|
sasl->authmechs ^= sasl->authused;
|
|
|
|
|
|
|
|
/* Start an alternative SASL authentication */
|
|
|
|
result = Curl_sasl_start(sasl, conn, sasl->force_ir, progress);
|
|
|
|
newstate = sasl->state; /* Use state from Curl_sasl_start() */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
failf(data, "Unsupported SASL authentication mechanism");
|
|
|
|
result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(result) {
|
|
|
|
case CURLE_BAD_CONTENT_ENCODING:
|
|
|
|
/* Cancel dialog */
|
|
|
|
result = sasl->params->sendcont(conn, "*");
|
|
|
|
newstate = SASL_CANCEL;
|
|
|
|
break;
|
|
|
|
case CURLE_OK:
|
|
|
|
if(resp)
|
|
|
|
result = sasl->params->sendcont(conn, resp);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
newstate = SASL_STOP; /* Stop on error */
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(resp);
|
2015-01-22 15:47:38 -05:00
|
|
|
|
2015-01-20 11:33:05 -05:00
|
|
|
state(sasl, conn, newstate);
|
2015-01-22 15:47:38 -05:00
|
|
|
|
2015-01-20 11:33:05 -05:00
|
|
|
return result;
|
2015-01-20 09:27:25 -05:00
|
|
|
}
|