2012-05-30 23:52:52 +04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2016-03-12 14:56:10 +03:00
|
|
|
* Copyright (C) 2012 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2012-05-30 23: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-03 02:19:02 +03:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2012-05-30 23: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 20:21:49 +04:00
|
|
|
* RFC2195 CRAM-MD5 authentication
|
2014-11-05 17:33:26 +03:00
|
|
|
* RFC2617 Basic and Digest Access Authentication
|
2012-06-04 13:53:18 +04:00
|
|
|
* RFC2831 DIGEST-MD5 authentication
|
2013-01-23 02:02:30 +04:00
|
|
|
* RFC4422 Simple Authentication and Security Layer (SASL)
|
2012-05-30 23:52:52 +04:00
|
|
|
* RFC4616 PLAIN authentication
|
2013-08-25 21:17:01 +04:00
|
|
|
* RFC6749 OAuth 2.0 Authorization Framework
|
2015-09-04 09:11:09 +03:00
|
|
|
* RFC7628 A Set of SASL Mechanisms for OAuth
|
2013-10-26 17:35:27 +04:00
|
|
|
* Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
|
2012-05-30 23:52:52 +04:00
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
#include "curl_setup.h"
|
2012-05-30 23:52:52 +04:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "urldata.h"
|
2012-05-30 23:52:52 +04:00
|
|
|
|
|
|
|
#include "curl_base64.h"
|
2012-06-03 20:21:49 +04:00
|
|
|
#include "curl_md5.h"
|
2015-09-12 13:48:24 +03:00
|
|
|
#include "vauth/vauth.h"
|
2013-12-18 02:32:47 +04:00
|
|
|
#include "vtls/vtls.h"
|
2012-06-03 20:21:49 +04:00
|
|
|
#include "curl_hmac.h"
|
2012-05-30 23:52:52 +04:00
|
|
|
#include "curl_sasl.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "warnless.h"
|
2014-04-09 00:08:02 +04:00
|
|
|
#include "strtok.h"
|
2015-01-20 17:27:25 +03:00
|
|
|
#include "strequal.h"
|
2014-04-09 00:08:02 +04:00
|
|
|
#include "rawstr.h"
|
2015-01-20 19:33:05 +03:00
|
|
|
#include "sendf.h"
|
2014-11-05 18:01:51 +03:00
|
|
|
#include "non-ascii.h" /* included for Curl_convert_... prototypes */
|
2015-03-03 14:36:18 +03:00
|
|
|
#include "curl_printf.h"
|
2012-05-30 23:52:52 +04:00
|
|
|
|
2015-03-25 01:12:03 +03:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "memdebug.h"
|
2012-05-30 23:52:52 +04:00
|
|
|
|
2015-01-20 22:21:56 +03:00
|
|
|
/* Supported mechanisms */
|
|
|
|
const struct {
|
2015-01-22 23:47:38 +03:00
|
|
|
const char *name; /* Name */
|
2015-01-20 22:21:56 +03:00
|
|
|
size_t len; /* Name length */
|
|
|
|
unsigned int bit; /* Flag bit */
|
|
|
|
} mechtable[] = {
|
2015-09-05 20:09:40 +03: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 22:21:56 +03:00
|
|
|
};
|
|
|
|
|
2012-06-02 14: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 19:31:39 +03:00
|
|
|
* conn [in] - The connection data.
|
2012-06-02 14:07:58 +04:00
|
|
|
* authused [in] - The authentication mechanism used.
|
|
|
|
*/
|
|
|
|
void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
|
|
|
|
{
|
2014-11-16 12:50:06 +03:00
|
|
|
#if defined(USE_KERBEROS5)
|
2014-08-14 03:03:45 +04:00
|
|
|
/* Cleanup the gssapi structure */
|
|
|
|
if(authused == SASL_MECH_GSSAPI) {
|
|
|
|
Curl_sasl_gssapi_cleanup(&conn->krb5);
|
|
|
|
}
|
2014-11-09 14:25:10 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_NTLM)
|
2016-03-12 14:56:10 +03:00
|
|
|
/* Cleanup the NTLM structure */
|
2014-11-09 14:25:10 +03:00
|
|
|
if(authused == SASL_MECH_NTLM) {
|
2014-10-09 14:03:14 +04:00
|
|
|
Curl_sasl_ntlm_cleanup(&conn->ntlm);
|
2012-06-02 14:07:58 +04:00
|
|
|
}
|
2014-08-14 03:03:45 +04:00
|
|
|
#endif
|
2014-11-09 14:25:10 +03:00
|
|
|
|
2014-11-16 12:50:06 +03:00
|
|
|
#if !defined(USE_KERBEROS5) && !defined(USE_NTLM)
|
2012-06-02 14:07:58 +04:00
|
|
|
/* Reserved for future use */
|
|
|
|
(void)conn;
|
|
|
|
(void)authused;
|
|
|
|
#endif
|
2012-06-03 03:00:34 +04:00
|
|
|
}
|
2015-01-20 17:27:25 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_decode_mech()
|
|
|
|
*
|
2015-01-28 22:11:19 +03:00
|
|
|
* Convert a SASL mechanism name into a token.
|
2015-01-20 17:27:25 +03:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ptr [in] - The mechanism string.
|
|
|
|
* maxlen [in] - Maximum mechanism string length.
|
|
|
|
* len [out] - If not NULL, effective name length.
|
|
|
|
*
|
2015-01-28 22:11:19 +03:00
|
|
|
* Returns the SASL mechanism token or 0 if no match.
|
2015-01-20 17:27:25 +03:00
|
|
|
*/
|
2015-01-22 23:47:38 +03:00
|
|
|
unsigned int Curl_sasl_decode_mech(const char *ptr, size_t maxlen, size_t *len)
|
2015-01-20 17:27:25 +03: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 23:47:38 +03:00
|
|
|
|
2015-01-20 17:27:25 +03:00
|
|
|
if(maxlen == mechtable[i].len)
|
|
|
|
return mechtable[i].bit;
|
2015-01-22 23:47:38 +03:00
|
|
|
|
2015-01-20 17:27:25 +03: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 23:47:38 +03:00
|
|
|
size_t mechlen;
|
2015-01-20 17:27:25 +03:00
|
|
|
|
|
|
|
if(!len)
|
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
|
2016-03-14 11:55:38 +03:00
|
|
|
if(sasl->resetprefs) {
|
|
|
|
sasl->resetprefs = FALSE;
|
|
|
|
sasl->prefmech = SASL_AUTH_NONE;
|
|
|
|
}
|
2015-01-20 17:27:25 +03:00
|
|
|
|
2016-03-14 11:55:38 +03:00
|
|
|
if(strnequal(value, "*", len))
|
|
|
|
sasl->prefmech = SASL_AUTH_DEFAULT;
|
2016-03-19 20:15:53 +03: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 17:27:25 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_init()
|
|
|
|
*
|
2015-01-28 22:11:19 +03:00
|
|
|
* Initializes the SASL structure.
|
2015-01-20 17:27:25 +03:00
|
|
|
*/
|
2015-01-20 19:33:05 +03:00
|
|
|
void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params)
|
2015-01-20 17:27:25 +03:00
|
|
|
{
|
2015-01-20 19:33:05 +03:00
|
|
|
sasl->params = params; /* Set protocol dependent parameters */
|
|
|
|
sasl->state = SASL_STOP; /* Not yet running */
|
2015-01-20 17:27:25 +03:00
|
|
|
sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
|
2015-01-27 19:24:55 +03:00
|
|
|
sasl->prefmech = SASL_AUTH_DEFAULT; /* Prefer all mechanisms */
|
2015-01-20 17:27:25 +03: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 19:33:05 +03:00
|
|
|
sasl->force_ir = FALSE; /* Respect external option */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* state()
|
|
|
|
*
|
|
|
|
* This is the ONLY way to change SASL state!
|
|
|
|
*/
|
2015-02-02 19:44:00 +03:00
|
|
|
static void state(struct SASL *sasl, struct connectdata *conn,
|
|
|
|
saslstate newstate)
|
2015-01-20 19:33:05 +03: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 19:24:55 +03:00
|
|
|
"EXTERNAL",
|
2015-01-20 19:33:05 +03:00
|
|
|
"CRAMMD5",
|
|
|
|
"DIGESTMD5",
|
|
|
|
"DIGESTMD5_RESP",
|
|
|
|
"NTLM",
|
|
|
|
"NTLM_TYPE2MSG",
|
|
|
|
"GSSAPI",
|
|
|
|
"GSSAPI_TOKEN",
|
|
|
|
"GSSAPI_NO_DATA",
|
2015-09-05 19:32:37 +03:00
|
|
|
"OAUTH2",
|
2015-09-05 20:35:47 +03:00
|
|
|
"OAUTH2_RESP",
|
2015-01-20 19:33:05 +03: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-29 01:48:01 +03:00
|
|
|
#else
|
|
|
|
(void) conn;
|
2015-01-20 19:33:05 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
sasl->state = newstate;
|
|
|
|
}
|
|
|
|
|
2015-01-27 19:24:55 +03: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 19:44:00 +03:00
|
|
|
/* Have credentials been provided? */
|
2015-01-27 19:24:55 +03:00
|
|
|
if(conn->bits.user_passwd)
|
2015-02-02 19:44:00 +03:00
|
|
|
return TRUE;
|
2015-01-27 19:24:55 +03:00
|
|
|
|
2015-02-02 19:44:00 +03:00
|
|
|
/* EXTERNAL can authenticate without a user name and/or password */
|
2015-01-27 19:24:55 +03:00
|
|
|
if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
|
2015-02-02 19:44:00 +03:00
|
|
|
return TRUE;
|
2015-01-27 19:24:55 +03:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:33:05 +03: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;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
unsigned int enabledmechs;
|
|
|
|
const char *mech = NULL;
|
|
|
|
char *resp = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
saslstate state1 = SASL_STOP;
|
|
|
|
saslstate state2 = SASL_FINAL;
|
|
|
|
|
|
|
|
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 22:11:19 +03:00
|
|
|
security, as well as the initial response where appropriate */
|
2015-01-27 19:24:55 +03: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 19:33:05 +03:00
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2015-01-27 19:24:55 +03:00
|
|
|
result = sasl_create_external_message(data, conn->user, &resp, &len);
|
2015-01-20 19:33:05 +03:00
|
|
|
}
|
2015-01-27 19:24:55 +03: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)
|
|
|
|
result = Curl_sasl_create_gssapi_user_message(data, conn->user,
|
|
|
|
conn->passwd,
|
|
|
|
sasl->params->service,
|
|
|
|
sasl->mutual_auth,
|
|
|
|
NULL, &conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
}
|
|
|
|
else
|
2015-01-20 19:33:05 +03:00
|
|
|
#endif
|
|
|
|
#ifndef CURL_DISABLE_CRYPTO_AUTH
|
2015-01-27 19:24:55 +03: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 19:33:05 +03:00
|
|
|
#endif
|
|
|
|
#ifdef USE_NTLM
|
2015-01-27 19:24:55 +03: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)
|
|
|
|
result = Curl_sasl_create_ntlm_type1_message(conn->user, conn->passwd,
|
|
|
|
&conn->ntlm, &resp, &len);
|
|
|
|
}
|
|
|
|
else
|
2015-01-20 19:33:05 +03:00
|
|
|
#endif
|
2015-09-05 20:09:40 +03:00
|
|
|
if((enabledmechs & SASL_MECH_OAUTHBEARER) && conn->oauth_bearer) {
|
|
|
|
mech = SASL_MECH_STRING_OAUTHBEARER;
|
|
|
|
state1 = SASL_OAUTH2;
|
2015-09-05 20:35:47 +03:00
|
|
|
state2 = SASL_OAUTH2_RESP;
|
2015-09-05 20:09:40 +03:00
|
|
|
sasl->authused = SASL_MECH_OAUTHBEARER;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
|
|
|
result = sasl_create_oauth_bearer_message(data, conn->user,
|
|
|
|
conn->host.name,
|
|
|
|
conn->port,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
|
|
|
}
|
|
|
|
else if((enabledmechs & SASL_MECH_XOAUTH2) && conn->oauth_bearer) {
|
2015-01-27 19:24:55 +03:00
|
|
|
mech = SASL_MECH_STRING_XOAUTH2;
|
2015-09-05 19:32:37 +03:00
|
|
|
state1 = SASL_OAUTH2;
|
2015-01-27 19:24:55 +03:00
|
|
|
sasl->authused = SASL_MECH_XOAUTH2;
|
|
|
|
|
|
|
|
if(force_ir || data->set.sasl_ir)
|
2015-11-04 10:20:21 +03:00
|
|
|
result = sasl_create_oauth_bearer_message(data, conn->user,
|
2015-09-05 19:08:54 +03:00
|
|
|
NULL, 0,
|
2015-11-04 10:20:21 +03:00
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-01-27 19:24:55 +03: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)
|
|
|
|
result = sasl_create_login_message(data, conn->user, &resp, &len);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
result = sasl_create_plain_message(data, conn->user, conn->passwd,
|
|
|
|
&resp, &len);
|
|
|
|
}
|
2015-01-20 19:33:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!result) {
|
|
|
|
if(resp && sasl->params->maxirlen &&
|
|
|
|
strlen(mech) + len > sasl->params->maxirlen) {
|
2015-03-16 17:01:15 +03:00
|
|
|
free(resp);
|
2015-01-20 19:33:05 +03:00
|
|
|
resp = NULL;
|
|
|
|
}
|
2015-02-02 19:44:00 +03:00
|
|
|
|
2015-01-20 19:33:05 +03:00
|
|
|
if(mech) {
|
|
|
|
result = sasl->params->sendauth(conn, mech, resp);
|
|
|
|
if(!result) {
|
|
|
|
*progress = SASL_INPROGRESS;
|
|
|
|
state(sasl, conn, resp? state2: state1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 17:01:15 +03:00
|
|
|
free(resp);
|
2015-01-22 23:47:38 +03:00
|
|
|
|
2015-01-20 19:33:05 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_sasl_continue()
|
|
|
|
*
|
2015-01-28 22:11:19 +03:00
|
|
|
* Continue the authentication.
|
2015-01-20 19:33:05 +03:00
|
|
|
*/
|
|
|
|
CURLcode Curl_sasl_continue(struct SASL *sasl, struct connectdata *conn,
|
2015-01-22 23:47:38 +03:00
|
|
|
int code, saslprogress *progress)
|
2015-01-20 19:33:05 +03:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
saslstate newstate = SASL_FINAL;
|
|
|
|
char *resp = NULL;
|
2015-01-20 22:25:43 +03:00
|
|
|
#if !defined(CURL_DISABLE_CRYPTO_AUTH)
|
2015-01-20 19:33:05 +03:00
|
|
|
char *serverdata;
|
2015-01-29 14:48:11 +03:00
|
|
|
char *chlg = NULL;
|
2015-01-20 22:28:54 +03:00
|
|
|
size_t chlglen = 0;
|
2015-01-20 22:25:43 +03:00
|
|
|
#endif
|
2015-01-20 19:33:05 +03:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
*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 20:35:47 +03:00
|
|
|
if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
|
|
|
|
code != sasl->params->contcode) {
|
2015-01-20 19:33:05 +03: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:
|
2015-01-22 23:58:15 +03:00
|
|
|
result = sasl_create_plain_message(data, conn->user, conn->passwd, &resp,
|
|
|
|
&len);
|
2015-01-20 19:33:05 +03:00
|
|
|
break;
|
|
|
|
case SASL_LOGIN:
|
2015-01-22 23:58:15 +03:00
|
|
|
result = sasl_create_login_message(data, conn->user, &resp, &len);
|
2015-01-20 19:33:05 +03:00
|
|
|
newstate = SASL_LOGIN_PASSWD;
|
|
|
|
break;
|
|
|
|
case SASL_LOGIN_PASSWD:
|
2015-01-22 23:58:15 +03:00
|
|
|
result = sasl_create_login_message(data, conn->passwd, &resp, &len);
|
2015-01-20 19:33:05 +03:00
|
|
|
break;
|
2015-01-27 19:24:55 +03:00
|
|
|
case SASL_EXTERNAL:
|
|
|
|
result = sasl_create_external_message(data, conn->user, &resp, &len);
|
|
|
|
break;
|
2015-01-20 19:33:05 +03:00
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_CRYPTO_AUTH
|
|
|
|
case SASL_CRAMMD5:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
2015-01-22 23:58:15 +03:00
|
|
|
result = sasl_decode_cram_md5_message(serverdata, &chlg, &chlglen);
|
2015-01-20 19:33:05 +03:00
|
|
|
if(!result)
|
2015-01-22 23:58:15 +03:00
|
|
|
result = sasl_create_cram_md5_message(data, chlg, conn->user,
|
|
|
|
conn->passwd, &resp, &len);
|
2015-03-16 17:01:15 +03:00
|
|
|
free(chlg);
|
2015-01-20 19:33:05 +03:00
|
|
|
break;
|
|
|
|
case SASL_DIGESTMD5:
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
|
|
|
result = Curl_sasl_create_digest_md5_message(data, serverdata,
|
|
|
|
conn->user, conn->passwd,
|
|
|
|
sasl->params->service,
|
|
|
|
&resp, &len);
|
|
|
|
newstate = SASL_DIGESTMD5_RESP;
|
|
|
|
break;
|
|
|
|
case SASL_DIGESTMD5_RESP:
|
2016-03-19 20:15:53 +03:00
|
|
|
resp = strdup("");
|
|
|
|
if(!resp)
|
2015-01-20 19:33:05 +03:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NTLM
|
|
|
|
case SASL_NTLM:
|
|
|
|
/* Create the type-1 message */
|
|
|
|
result = Curl_sasl_create_ntlm_type1_message(conn->user, conn->passwd,
|
2015-02-02 19:44:00 +03:00
|
|
|
&conn->ntlm, &resp, &len);
|
2015-01-20 19:33:05 +03:00
|
|
|
newstate = SASL_NTLM_TYPE2MSG;
|
|
|
|
break;
|
|
|
|
case SASL_NTLM_TYPE2MSG:
|
|
|
|
/* Decode the type-2 message */
|
|
|
|
sasl->params->getmessage(data->state.buffer, &serverdata);
|
2015-02-02 19:44:00 +03:00
|
|
|
result = Curl_sasl_decode_ntlm_type2_message(data, serverdata,
|
|
|
|
&conn->ntlm);
|
2015-01-20 19:33:05 +03:00
|
|
|
if(!result)
|
|
|
|
result = Curl_sasl_create_ntlm_type3_message(data, conn->user,
|
|
|
|
conn->passwd, &conn->ntlm,
|
|
|
|
&resp, &len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_KERBEROS5)
|
|
|
|
case SASL_GSSAPI:
|
|
|
|
result = Curl_sasl_create_gssapi_user_message(data, conn->user,
|
|
|
|
conn->passwd,
|
|
|
|
sasl->params->service,
|
|
|
|
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 */
|
|
|
|
result = Curl_sasl_create_gssapi_user_message(data, NULL, NULL, NULL,
|
|
|
|
sasl->mutual_auth,
|
|
|
|
serverdata, &conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
newstate = SASL_GSSAPI_NO_DATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* Decode the security challenge and create the response message */
|
|
|
|
result = Curl_sasl_create_gssapi_security_message(data, serverdata,
|
|
|
|
&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 */
|
|
|
|
result = Curl_sasl_create_gssapi_security_message(data, serverdata,
|
|
|
|
&conn->krb5,
|
|
|
|
&resp, &len);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2015-09-05 19:32:37 +03:00
|
|
|
case SASL_OAUTH2:
|
2015-01-20 19:33:05 +03:00
|
|
|
/* Create the authorisation message */
|
2015-09-05 20:35:47 +03:00
|
|
|
if(sasl->authused == SASL_MECH_OAUTHBEARER) {
|
2015-09-05 20:09:40 +03:00
|
|
|
result = sasl_create_oauth_bearer_message(data, conn->user,
|
|
|
|
conn->host.name,
|
|
|
|
conn->port,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-09-05 20:35:47 +03:00
|
|
|
|
|
|
|
/* Failures maybe sent by the server as continuations for OAUTHBEARER */
|
|
|
|
newstate = SASL_OAUTH2_RESP;
|
|
|
|
}
|
2015-09-05 20:09:40 +03:00
|
|
|
else
|
|
|
|
result = sasl_create_oauth_bearer_message(data, conn->user,
|
|
|
|
NULL, 0,
|
|
|
|
conn->oauth_bearer,
|
|
|
|
&resp, &len);
|
2015-01-20 19:33:05 +03:00
|
|
|
break;
|
2015-09-05 20:35:47 +03:00
|
|
|
|
|
|
|
case SASL_OAUTH2_RESP:
|
|
|
|
/* The continuation is optional so check the response code */
|
2015-11-16 01:15:00 +03:00
|
|
|
if(code == sasl->params->finalcode) {
|
2015-09-05 20:35:47 +03:00
|
|
|
/* Final response was received so we are done */
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return result;
|
|
|
|
}
|
2015-11-16 01:15:00 +03: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 20:35:47 +03:00
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*progress = SASL_DONE;
|
|
|
|
state(sasl, conn, SASL_STOP);
|
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
|
|
|
|
2015-01-20 19:33:05 +03: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 17:01:15 +03:00
|
|
|
free(resp);
|
2015-01-22 23:47:38 +03:00
|
|
|
|
2015-01-20 19:33:05 +03:00
|
|
|
state(sasl, conn, newstate);
|
2015-01-22 23:47:38 +03:00
|
|
|
|
2015-01-20 19:33:05 +03:00
|
|
|
return result;
|
2015-01-20 17:27:25 +03:00
|
|
|
}
|