1
0
mirror of https://github.com/moparisthebest/curl synced 2025-03-03 18:59:46 -05:00

vauth: factor base64 conversions out of authentication procedures

Input challenges and returned messages are now in binary.
Conversions from/to base64 are performed by callers (currently curl_sasl.c
and http_ntlm.c).

Closes #6654
This commit is contained in:
Patrick Monnerat 2021-03-17 20:09:55 +01:00 committed by Daniel Stenberg
parent 34cf40321c
commit 19ea52da4d
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
13 changed files with 438 additions and 712 deletions

View File

@ -242,6 +242,49 @@ static void state(struct SASL *sasl, struct Curl_easy *data,
sasl->state = newstate; sasl->state = newstate;
} }
/* Get the SASL server message and convert it to binary. */
static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
struct bufref *out)
{
unsigned char *msg;
size_t msglen;
char *serverdata = NULL;
CURLcode result = CURLE_OK;
sasl->params->getmessage(data->state.buffer, &serverdata);
if(!serverdata)
result = CURLE_BAD_CONTENT_ENCODING;
else if(!*serverdata || *serverdata == '=')
Curl_bufref_set(out, NULL, 0, NULL);
else {
result = Curl_base64_decode(serverdata, &msg, &msglen);
if(!result)
Curl_bufref_set(out, msg, msglen, curl_free);
}
return result;
}
/* Encode the outgoing SASL message. */
static CURLcode build_message(struct Curl_easy *data, struct bufref *msg)
{
CURLcode result = CURLE_OK;
char *base64;
size_t base64len;
if(!Curl_bufref_ptr(msg)) /* Empty mesage. */
Curl_bufref_set(msg, "", 0, NULL);
else if(!Curl_bufref_len(msg)) /* Explicit empty response. */
Curl_bufref_set(msg, "=", 1, NULL);
else {
result = Curl_base64_encode(data, (const char *) Curl_bufref_ptr(msg),
Curl_bufref_len(msg), &base64, &base64len);
if(!result)
Curl_bufref_set(msg, base64, base64len, curl_free);
}
return result;
}
/* /*
* Curl_sasl_can_authenticate() * Curl_sasl_can_authenticate()
* *
@ -272,8 +315,7 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
unsigned int enabledmechs; unsigned int enabledmechs;
const char *mech = NULL; const char *mech = NULL;
char *resp = NULL; struct bufref resp;
size_t len = 0;
saslstate state1 = SASL_STOP; saslstate state1 = SASL_STOP;
saslstate state2 = SASL_FINAL; saslstate state2 = SASL_FINAL;
#ifndef CURL_DISABLE_PROXY #ifndef CURL_DISABLE_PROXY
@ -290,7 +332,10 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->params->service; sasl->params->service;
#endif #endif
const char *oauth_bearer = data->set.str[STRING_BEARER]; const char *oauth_bearer = data->set.str[STRING_BEARER];
struct bufref nullmsg;
Curl_bufref_init(&nullmsg);
Curl_bufref_init(&resp);
sasl->force_ir = force_ir; /* Latch for future use */ sasl->force_ir = force_ir; /* Latch for future use */
sasl->authused = 0; /* No mechanism used yet */ sasl->authused = 0; /* No mechanism used yet */
enabledmechs = sasl->authmechs & sasl->prefmech; enabledmechs = sasl->authmechs & sasl->prefmech;
@ -304,8 +349,7 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->authused = SASL_MECH_EXTERNAL; sasl->authused = SASL_MECH_EXTERNAL;
if(force_ir || data->set.sasl_ir) if(force_ir || data->set.sasl_ir)
result = Curl_auth_create_external_message(data, conn->user, &resp, result = Curl_auth_create_external_message(conn->user, &resp);
&len);
} }
else if(conn->bits.user_passwd) { else if(conn->bits.user_passwd) {
#if defined(USE_KERBEROS5) #if defined(USE_KERBEROS5)
@ -321,10 +365,10 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
result = Curl_auth_create_gssapi_user_message(data, conn->user, result = Curl_auth_create_gssapi_user_message(data, conn->user,
conn->passwd, conn->passwd,
service, service,
data->conn->host.name, conn->host.name,
sasl->mutual_auth, sasl->mutual_auth,
NULL, &conn->krb5, NULL, &conn->krb5,
&resp, &len); &resp);
} }
else else
#endif #endif
@ -340,8 +384,7 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
result = Curl_auth_gsasl_start(data, conn->user, result = Curl_auth_gsasl_start(data, conn->user,
conn->passwd, &conn->gsasl); conn->passwd, &conn->gsasl);
if(result == CURLE_OK && (force_ir || data->set.sasl_ir)) if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
result = Curl_auth_gsasl_token(data, NULL, &conn->gsasl, result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
&resp, &len);
} }
else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) && else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1, Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
@ -354,8 +397,7 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
result = Curl_auth_gsasl_start(data, conn->user, result = Curl_auth_gsasl_start(data, conn->user,
conn->passwd, &conn->gsasl); conn->passwd, &conn->gsasl);
if(result == CURLE_OK && (force_ir || data->set.sasl_ir)) if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
result = Curl_auth_gsasl_token(data, NULL, &conn->gsasl, result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
&resp, &len);
} }
else else
#endif #endif
@ -385,8 +427,7 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
conn->user, conn->passwd, conn->user, conn->passwd,
service, service,
hostname, hostname,
&conn->ntlm, &resp, &conn->ntlm, &resp);
&len);
} }
else else
#endif #endif
@ -397,11 +438,11 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->authused = SASL_MECH_OAUTHBEARER; sasl->authused = SASL_MECH_OAUTHBEARER;
if(force_ir || data->set.sasl_ir) if(force_ir || data->set.sasl_ir)
result = Curl_auth_create_oauth_bearer_message(data, conn->user, result = Curl_auth_create_oauth_bearer_message(conn->user,
hostname, hostname,
port, port,
oauth_bearer, oauth_bearer,
&resp, &len); &resp);
} }
else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) { else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) {
mech = SASL_MECH_STRING_XOAUTH2; mech = SASL_MECH_STRING_XOAUTH2;
@ -409,9 +450,9 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->authused = SASL_MECH_XOAUTH2; sasl->authused = SASL_MECH_XOAUTH2;
if(force_ir || data->set.sasl_ir) if(force_ir || data->set.sasl_ir)
result = Curl_auth_create_xoauth_bearer_message(data, conn->user, result = Curl_auth_create_xoauth_bearer_message(conn->user,
oauth_bearer, oauth_bearer,
&resp, &len); &resp);
} }
else if(enabledmechs & SASL_MECH_PLAIN) { else if(enabledmechs & SASL_MECH_PLAIN) {
mech = SASL_MECH_STRING_PLAIN; mech = SASL_MECH_STRING_PLAIN;
@ -419,9 +460,9 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->authused = SASL_MECH_PLAIN; sasl->authused = SASL_MECH_PLAIN;
if(force_ir || data->set.sasl_ir) if(force_ir || data->set.sasl_ir)
result = Curl_auth_create_plain_message(data, conn->sasl_authzid, result = Curl_auth_create_plain_message(conn->sasl_authzid,
conn->user, conn->passwd, conn->user, conn->passwd,
&resp, &len); &resp);
} }
else if(enabledmechs & SASL_MECH_LOGIN) { else if(enabledmechs & SASL_MECH_LOGIN) {
mech = SASL_MECH_STRING_LOGIN; mech = SASL_MECH_STRING_LOGIN;
@ -430,26 +471,29 @@ CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
sasl->authused = SASL_MECH_LOGIN; sasl->authused = SASL_MECH_LOGIN;
if(force_ir || data->set.sasl_ir) if(force_ir || data->set.sasl_ir)
result = Curl_auth_create_login_message(data, conn->user, &resp, &len); result = Curl_auth_create_login_message(conn->user, &resp);
} }
} }
if(!result && mech) { if(!result && mech) {
if(resp && sasl->params->maxirlen && if(Curl_bufref_ptr(&resp))
strlen(mech) + len > sasl->params->maxirlen) { result = build_message(data, &resp);
free(resp);
resp = NULL; if(sasl->params->maxirlen &&
} strlen(mech) + Curl_bufref_len(&resp) > sasl->params->maxirlen)
Curl_bufref_free(&resp);
if(!result)
result = sasl->params->sendauth(data, conn, mech,
(const char *) Curl_bufref_ptr(&resp));
result = sasl->params->sendauth(data, conn, mech, resp);
if(!result) { if(!result) {
*progress = SASL_INPROGRESS; *progress = SASL_INPROGRESS;
state(sasl, data, resp ? state2 : state1); state(sasl, data, Curl_bufref_ptr(&resp) ? state2 : state1);
} }
} }
free(resp); Curl_bufref_free(&resp);
return result; return result;
} }
@ -464,29 +508,25 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
saslstate newstate = SASL_FINAL; saslstate newstate = SASL_FINAL;
char *resp = NULL; struct bufref resp;
#ifndef CURL_DISABLE_PROXY #ifndef CURL_DISABLE_PROXY
const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name : const char * const hostname = SSL_HOST_NAME();
conn->host.name;
const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port; const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
#else #else
const char * const hostname = conn->host.name; const char * const hostname = conn->host.name;
const long int port = conn->remote_port; const long int port = conn->remote_port;
#endif #endif
#if !defined(CURL_DISABLE_CRYPTO_AUTH)
char *chlg = NULL;
size_t chlglen = 0;
#endif
#if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5) || \ #if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5) || \
defined(USE_NTLM) defined(USE_NTLM)
const char *service = data->set.str[STRING_SERVICE_NAME] ? const char *service = data->set.str[STRING_SERVICE_NAME] ?
data->set.str[STRING_SERVICE_NAME] : data->set.str[STRING_SERVICE_NAME] :
sasl->params->service; sasl->params->service;
char *serverdata;
#endif #endif
size_t len = 0;
const char *oauth_bearer = data->set.str[STRING_BEARER]; const char *oauth_bearer = data->set.str[STRING_BEARER];
struct bufref serverdata;
Curl_bufref_init(&serverdata);
Curl_bufref_init(&resp);
*progress = SASL_INPROGRESS; *progress = SASL_INPROGRESS;
if(sasl->state == SASL_FINAL) { if(sasl->state == SASL_FINAL) {
@ -509,50 +549,45 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
*progress = SASL_DONE; *progress = SASL_DONE;
return result; return result;
case SASL_PLAIN: case SASL_PLAIN:
result = Curl_auth_create_plain_message(data, conn->sasl_authzid, result = Curl_auth_create_plain_message(conn->sasl_authzid,
conn->user, conn->passwd, conn->user, conn->passwd, &resp);
&resp, &len);
break; break;
case SASL_LOGIN: case SASL_LOGIN:
result = Curl_auth_create_login_message(data, conn->user, &resp, &len); result = Curl_auth_create_login_message(conn->user, &resp);
newstate = SASL_LOGIN_PASSWD; newstate = SASL_LOGIN_PASSWD;
break; break;
case SASL_LOGIN_PASSWD: case SASL_LOGIN_PASSWD:
result = Curl_auth_create_login_message(data, conn->passwd, &resp, &len); result = Curl_auth_create_login_message(conn->passwd, &resp);
break; break;
case SASL_EXTERNAL: case SASL_EXTERNAL:
result = Curl_auth_create_external_message(data, conn->user, &resp, &len); result = Curl_auth_create_external_message(conn->user, &resp);
break; break;
#ifndef CURL_DISABLE_CRYPTO_AUTH #ifndef CURL_DISABLE_CRYPTO_AUTH
#ifdef USE_GSASL #ifdef USE_GSASL
case SASL_GSASL: case SASL_GSASL:
sasl->params->getmessage(data->state.buffer, &serverdata); result = get_server_message(sasl, data, &serverdata);
result = Curl_auth_gsasl_token(data, serverdata, &conn->gsasl, if(!result)
&resp, &len); result = Curl_auth_gsasl_token(data, &serverdata, &conn->gsasl, &resp);
if(len > 0) if(!result && Curl_bufref_len(&resp) > 0)
newstate = SASL_GSASL; newstate = SASL_GSASL;
break; break;
#endif #endif
case SASL_CRAMMD5: case SASL_CRAMMD5:
sasl->params->getmessage(data->state.buffer, &serverdata); result = get_server_message(sasl, data, &serverdata);
result = Curl_auth_decode_cram_md5_message(serverdata, &chlg, &chlglen);
if(!result) if(!result)
result = Curl_auth_create_cram_md5_message(data, chlg, conn->user, result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
conn->passwd, &resp, &len); conn->passwd, &resp);
free(chlg);
break; break;
case SASL_DIGESTMD5: case SASL_DIGESTMD5:
sasl->params->getmessage(data->state.buffer, &serverdata); result = get_server_message(sasl, data, &serverdata);
result = Curl_auth_create_digest_md5_message(data, serverdata, if(!result)
result = Curl_auth_create_digest_md5_message(data, &serverdata,
conn->user, conn->passwd, conn->user, conn->passwd,
service, service, &resp);
&resp, &len);
newstate = SASL_DIGESTMD5_RESP; newstate = SASL_DIGESTMD5_RESP;
break; break;
case SASL_DIGESTMD5_RESP: case SASL_DIGESTMD5_RESP:
resp = strdup(""); /* Keep response NULL to output an empty line. */
if(!resp)
result = CURLE_OUT_OF_MEMORY;
break; break;
#endif #endif
@ -562,18 +597,19 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
result = Curl_auth_create_ntlm_type1_message(data, result = Curl_auth_create_ntlm_type1_message(data,
conn->user, conn->passwd, conn->user, conn->passwd,
service, hostname, service, hostname,
&conn->ntlm, &resp, &len); &conn->ntlm, &resp);
newstate = SASL_NTLM_TYPE2MSG; newstate = SASL_NTLM_TYPE2MSG;
break; break;
case SASL_NTLM_TYPE2MSG: case SASL_NTLM_TYPE2MSG:
/* Decode the type-2 message */ /* Decode the type-2 message */
sasl->params->getmessage(data->state.buffer, &serverdata); result = get_server_message(sasl, data, &serverdata);
result = Curl_auth_decode_ntlm_type2_message(data, serverdata, if(!result)
result = Curl_auth_decode_ntlm_type2_message(data, &serverdata,
&conn->ntlm); &conn->ntlm);
if(!result) if(!result)
result = Curl_auth_create_ntlm_type3_message(data, conn->user, result = Curl_auth_create_ntlm_type3_message(data, conn->user,
conn->passwd, &conn->ntlm, conn->passwd, &conn->ntlm,
&resp, &len); &resp);
break; break;
#endif #endif
@ -582,55 +618,59 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
result = Curl_auth_create_gssapi_user_message(data, conn->user, result = Curl_auth_create_gssapi_user_message(data, conn->user,
conn->passwd, conn->passwd,
service, service,
data->conn->host.name, conn->host.name,
sasl->mutual_auth, NULL, sasl->mutual_auth, NULL,
&conn->krb5, &conn->krb5,
&resp, &len); &resp);
newstate = SASL_GSSAPI_TOKEN; newstate = SASL_GSSAPI_TOKEN;
break; break;
case SASL_GSSAPI_TOKEN: case SASL_GSSAPI_TOKEN:
sasl->params->getmessage(data->state.buffer, &serverdata); result = get_server_message(sasl, data, &serverdata);
if(!result) {
if(sasl->mutual_auth) { if(sasl->mutual_auth) {
/* Decode the user token challenge and create the optional response /* Decode the user token challenge and create the optional response
message */ message */
result = Curl_auth_create_gssapi_user_message(data, NULL, NULL, result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
NULL, NULL, NULL, NULL,
sasl->mutual_auth, sasl->mutual_auth,
serverdata, &conn->krb5, &serverdata,
&resp, &len); &conn->krb5,
&resp);
newstate = SASL_GSSAPI_NO_DATA; newstate = SASL_GSSAPI_NO_DATA;
} }
else else
/* Decode the security challenge and create the response message */ /* Decode the security challenge and create the response message */
result = Curl_auth_create_gssapi_security_message(data, serverdata, result = Curl_auth_create_gssapi_security_message(data, &serverdata,
&conn->krb5, &conn->krb5,
&resp, &len); &resp);
}
break; break;
case SASL_GSSAPI_NO_DATA: case SASL_GSSAPI_NO_DATA:
sasl->params->getmessage(data->state.buffer, &serverdata);
/* Decode the security challenge and create the response message */ /* Decode the security challenge and create the response message */
result = Curl_auth_create_gssapi_security_message(data, serverdata, result = get_server_message(sasl, data, &serverdata);
if(!result)
result = Curl_auth_create_gssapi_security_message(data, &serverdata,
&conn->krb5, &conn->krb5,
&resp, &len); &resp);
break; break;
#endif #endif
case SASL_OAUTH2: case SASL_OAUTH2:
/* Create the authorisation message */ /* Create the authorisation message */
if(sasl->authused == SASL_MECH_OAUTHBEARER) { if(sasl->authused == SASL_MECH_OAUTHBEARER) {
result = Curl_auth_create_oauth_bearer_message(data, conn->user, result = Curl_auth_create_oauth_bearer_message(conn->user,
hostname, hostname,
port, port,
oauth_bearer, oauth_bearer,
&resp, &len); &resp);
/* Failures maybe sent by the server as continuations for OAUTHBEARER */ /* Failures maybe sent by the server as continuations for OAUTHBEARER */
newstate = SASL_OAUTH2_RESP; newstate = SASL_OAUTH2_RESP;
} }
else else
result = Curl_auth_create_xoauth_bearer_message(data, conn->user, result = Curl_auth_create_xoauth_bearer_message(conn->user,
oauth_bearer, oauth_bearer,
&resp, &len); &resp);
break; break;
case SASL_OAUTH2_RESP: case SASL_OAUTH2_RESP:
@ -642,11 +682,8 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
return result; return result;
} }
else if(code == sasl->params->contcode) { else if(code == sasl->params->contcode) {
/* Acknowledge the continuation by sending a 0x01 response base64 /* Acknowledge the continuation by sending a 0x01 response. */
encoded */ Curl_bufref_set(&resp, "\x01", 1, NULL);
resp = strdup("AQ==");
if(!resp)
result = CURLE_OUT_OF_MEMORY;
break; break;
} }
else { else {
@ -660,15 +697,15 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
sasl->authmechs ^= sasl->authused; sasl->authmechs ^= sasl->authused;
/* Start an alternative SASL authentication */ /* Start an alternative SASL authentication */
result = Curl_sasl_start(sasl, data, conn, sasl->force_ir, progress); return Curl_sasl_start(sasl, data, conn, sasl->force_ir, progress);
newstate = sasl->state; /* Use state from Curl_sasl_start() */
break;
default: default:
failf(data, "Unsupported SASL authentication mechanism"); failf(data, "Unsupported SASL authentication mechanism");
result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */ result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
break; break;
} }
Curl_bufref_free(&serverdata);
switch(result) { switch(result) {
case CURLE_BAD_CONTENT_ENCODING: case CURLE_BAD_CONTENT_ENCODING:
/* Cancel dialog */ /* Cancel dialog */
@ -676,8 +713,10 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
newstate = SASL_CANCEL; newstate = SASL_CANCEL;
break; break;
case CURLE_OK: case CURLE_OK:
if(resp) result = build_message(data, &resp);
result = sasl->params->sendcont(data, conn, resp); if(!result)
result = sasl->params->sendcont(data, conn,
(const char *) Curl_bufref_ptr(&resp));
break; break;
default: default:
newstate = SASL_STOP; /* Stop on error */ newstate = SASL_STOP; /* Stop on error */
@ -685,7 +724,7 @@ CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
break; break;
} }
free(resp); Curl_bufref_free(&resp);
state(sasl, data, newstate); state(sasl, data, newstate);

View File

@ -39,6 +39,7 @@
#include "http_ntlm.h" #include "http_ntlm.h"
#include "curl_ntlm_core.h" #include "curl_ntlm_core.h"
#include "curl_ntlm_wb.h" #include "curl_ntlm_wb.h"
#include "curl_base64.h"
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "url.h" #include "url.h"
@ -80,7 +81,18 @@ CURLcode Curl_input_ntlm(struct Curl_easy *data,
header++; header++;
if(*header) { if(*header) {
result = Curl_auth_decode_ntlm_type2_message(data, header, ntlm); unsigned char *hdr;
size_t hdrlen;
result = Curl_base64_decode(header, &hdr, &hdrlen);
if(!result) {
struct bufref hdrbuf;
Curl_bufref_init(&hdrbuf);
Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
Curl_bufref_free(&hdrbuf);
}
if(result) if(result)
return result; return result;
@ -116,7 +128,8 @@ CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
{ {
char *base64 = NULL; char *base64 = NULL;
size_t len = 0; size_t len = 0;
CURLcode result; CURLcode result = CURLE_OK;
struct bufref ntlmmsg;
/* point to the address of the pointer that holds the string to send to the /* point to the address of the pointer that holds the string to send to the
server, which is for a plain host or for a HTTP proxy */ server, which is for a plain host or for a HTTP proxy */
@ -184,51 +197,53 @@ CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
#endif #endif
#endif #endif
Curl_bufref_init(&ntlmmsg);
switch(*state) { switch(*state) {
case NTLMSTATE_TYPE1: case NTLMSTATE_TYPE1:
default: /* for the weird cases we (re)start here */ default: /* for the weird cases we (re)start here */
/* Create a type-1 message */ /* Create a type-1 message */
result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp, result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp,
service, hostname, service, hostname,
ntlm, &base64, ntlm, &ntlmmsg);
&len); if(!result) {
if(result) DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
return result; result = Curl_base64_encode(data,
(const char *) Curl_bufref_ptr(&ntlmmsg),
if(base64) { Curl_bufref_len(&ntlmmsg), &base64, &len);
if(!result) {
free(*allocuserpwd); free(*allocuserpwd);
*allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n", *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
proxy ? "Proxy-" : "", proxy ? "Proxy-" : "",
base64); base64);
free(base64); free(base64);
if(!*allocuserpwd) if(!*allocuserpwd)
return CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
}
DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
} }
break; break;
case NTLMSTATE_TYPE2: case NTLMSTATE_TYPE2:
/* We already received the type-2 message, create a type-3 message */ /* We already received the type-2 message, create a type-3 message */
result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp, result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
ntlm, &base64, &len); ntlm, &ntlmmsg);
if(result) if(!result && Curl_bufref_len(&ntlmmsg)) {
return result; result = Curl_base64_encode(data,
(const char *) Curl_bufref_ptr(&ntlmmsg),
if(base64) { Curl_bufref_len(&ntlmmsg), &base64, &len);
if(!result) {
free(*allocuserpwd); free(*allocuserpwd);
*allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n", *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
proxy ? "Proxy-" : "", proxy ? "Proxy-" : "",
base64); base64);
free(base64); free(base64);
if(!*allocuserpwd) if(!*allocuserpwd)
return CURLE_OUT_OF_MEMORY; result = CURLE_OUT_OF_MEMORY;
else {
DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
*state = NTLMSTATE_TYPE3; /* we send a type-3 */ *state = NTLMSTATE_TYPE3; /* we send a type-3 */
authp->done = TRUE; authp->done = TRUE;
} }
}
}
break; break;
case NTLMSTATE_TYPE3: case NTLMSTATE_TYPE3:
@ -241,8 +256,9 @@ CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
authp->done = TRUE; authp->done = TRUE;
break; break;
} }
Curl_bufref_free(&ntlmmsg);
return CURLE_OK; return result;
} }
void Curl_http_auth_cleanup_ntlm(struct connectdata *conn) void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -32,7 +32,6 @@
#include "urldata.h" #include "urldata.h"
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "curl_base64.h"
#include "curl_md5.h" #include "curl_md5.h"
#include "warnless.h" #include "warnless.h"
#include "strtok.h" #include "strtok.h"
@ -51,31 +50,24 @@
* *
* Parameters: * Parameters:
* *
* data [in] - The session handle.
* authzid [in] - The authorization identity. * authzid [in] - The authorization identity.
* authcid [in] - The authentication identity. * authcid [in] - The authentication identity.
* passwd [in] - The password. * passwd [in] - The password.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_plain_message(struct Curl_easy *data, CURLcode Curl_auth_create_plain_message(const char *authzid,
const char *authzid,
const char *authcid, const char *authcid,
const char *passwd, const char *passwd,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result;
char *plainauth; char *plainauth;
size_t plainlen;
size_t zlen; size_t zlen;
size_t clen; size_t clen;
size_t plen; size_t plen;
size_t plainlen;
*outlen = 0;
*outptr = NULL;
zlen = (authzid == NULL ? 0 : strlen(authzid)); zlen = (authzid == NULL ? 0 : strlen(authzid));
clen = strlen(authcid); clen = strlen(authcid);
plen = strlen(passwd); plen = strlen(passwd);
@ -86,7 +78,7 @@ CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
plainlen = zlen + clen + plen + 2; plainlen = zlen + clen + plen + 2;
plainauth = malloc(plainlen); plainauth = malloc(plainlen + 1);
if(!plainauth) if(!plainauth)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
@ -97,12 +89,9 @@ CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
memcpy(plainauth + zlen + 1, authcid, clen); memcpy(plainauth + zlen + 1, authcid, clen);
plainauth[zlen + clen + 1] = '\0'; plainauth[zlen + clen + 1] = '\0';
memcpy(plainauth + zlen + clen + 2, passwd, plen); memcpy(plainauth + zlen + clen + 2, passwd, plen);
plainauth[plainlen] = '\0';
/* Base64 encode the reply */ Curl_bufref_set(out, plainauth, plainlen, curl_free);
result = Curl_base64_encode(data, plainauth, plainlen, outptr, outlen); return CURLE_OK;
free(plainauth);
return result;
} }
/* /*
@ -113,36 +102,17 @@ CURLcode Curl_auth_create_plain_message(struct Curl_easy *data,
* *
* Parameters: * Parameters:
* *
* data [in] - The session handle.
* valuep [in] - The user name or user's password. * valuep [in] - The user name or user's password.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_login_message(struct Curl_easy *data, CURLcode Curl_auth_create_login_message(const char *valuep, struct bufref *out)
const char *valuep, char **outptr,
size_t *outlen)
{ {
size_t vlen = strlen(valuep); Curl_bufref_set(out, valuep, strlen(valuep), NULL);
if(!vlen) {
/* Calculate an empty reply */
*outptr = strdup("=");
if(*outptr) {
*outlen = (size_t) 1;
return CURLE_OK; return CURLE_OK;
} }
*outlen = 0;
return CURLE_OUT_OF_MEMORY;
}
/* Base64 encode the value */
return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
}
/* /*
* Curl_auth_create_external_message() * Curl_auth_create_external_message()
* *
@ -151,20 +121,16 @@ CURLcode Curl_auth_create_login_message(struct Curl_easy *data,
* *
* Parameters: * Parameters:
* *
* data [in] - The session handle.
* user [in] - The user name. * user [in] - The user name.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_external_message(struct Curl_easy *data, CURLcode Curl_auth_create_external_message(const char *user,
const char *user, char **outptr, struct bufref *out)
size_t *outlen)
{ {
/* This is the same formatting as the login message */ /* This is the same formatting as the login message */
return Curl_auth_create_login_message(data, user, outptr, outlen); return Curl_auth_create_login_message(user, out);
} }
#endif /* if no users */ #endif /* if no users */

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -30,7 +30,6 @@
#include "urldata.h" #include "urldata.h"
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "curl_base64.h"
#include "curl_hmac.h" #include "curl_hmac.h"
#include "curl_md5.h" #include "curl_md5.h"
#include "warnless.h" #include "warnless.h"
@ -40,69 +39,31 @@
#include "curl_memory.h" #include "curl_memory.h"
#include "memdebug.h" #include "memdebug.h"
/*
* Curl_auth_decode_cram_md5_message()
*
* This is used to decode an already encoded CRAM-MD5 challenge message.
*
* Parameters:
*
* chlg64 [in] - The base64 encoded challenge message.
* 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.
*/
CURLcode Curl_auth_decode_cram_md5_message(const char *chlg64, char **outptr,
size_t *outlen)
{
CURLcode result = CURLE_OK;
size_t chlg64len = strlen(chlg64);
*outptr = NULL;
*outlen = 0;
/* Decode the challenge if necessary */
if(chlg64len && *chlg64 != '=')
result = Curl_base64_decode(chlg64, (unsigned char **) outptr, outlen);
return result;
}
/* /*
* Curl_auth_create_cram_md5_message() * Curl_auth_create_cram_md5_message()
* *
* This is used to generate an already encoded CRAM-MD5 response message ready * This is used to generate a CRAM-MD5 response message ready for sending to
* for sending to the recipient. * the recipient.
* *
* Parameters: * Parameters:
* *
* data [in] - The session handle.
* chlg [in] - The challenge. * chlg [in] - The challenge.
* userp [in] - The user name. * userp [in] - The user name.
* passwdp [in] - The user's password. * passwdp [in] - The user's password.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_cram_md5_message(struct Curl_easy *data, CURLcode Curl_auth_create_cram_md5_message(const struct bufref *chlg,
const char *chlg,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK;
size_t chlglen = 0;
struct HMAC_context *ctxt; struct HMAC_context *ctxt;
unsigned char digest[MD5_DIGEST_LEN]; unsigned char digest[MD5_DIGEST_LEN];
char *response; char *response;
if(chlg)
chlglen = strlen(chlg);
/* Compute the digest using the password as the key */ /* Compute the digest using the password as the key */
ctxt = Curl_HMAC_init(Curl_HMAC_MD5, ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
(const unsigned char *) passwdp, (const unsigned char *) passwdp,
@ -111,9 +72,9 @@ CURLcode Curl_auth_create_cram_md5_message(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
/* Update the digest with the given challenge */ /* Update the digest with the given challenge */
if(chlglen > 0) if(Curl_bufref_len(chlg))
Curl_HMAC_update(ctxt, (const unsigned char *) chlg, Curl_HMAC_update(ctxt, Curl_bufref_ptr(chlg),
curlx_uztoui(chlglen)); curlx_uztoui(Curl_bufref_len(chlg)));
/* Finalise the digest */ /* Finalise the digest */
Curl_HMAC_final(ctxt, digest); Curl_HMAC_final(ctxt, digest);
@ -127,12 +88,8 @@ CURLcode Curl_auth_create_cram_md5_message(struct Curl_easy *data,
if(!response) if(!response)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
/* Base64 encode the response */ Curl_bufref_set(out, response, strlen(response), curl_free);
result = Curl_base64_encode(data, response, 0, outptr, outlen); return CURLE_OK;
free(response);
return result;
} }
#endif /* !CURL_DISABLE_CRYPTO_AUTH */ #endif /* !CURL_DISABLE_CRYPTO_AUTH */

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -254,7 +254,7 @@ static CURLcode auth_digest_get_qop_values(const char *options, int *value)
* *
* Parameters: * Parameters:
* *
* chlg64 [in] - The base64 encoded challenge message. * chlgref [in] - The challenge message.
* nonce [in/out] - The buffer where the nonce will be stored. * nonce [in/out] - The buffer where the nonce will be stored.
* nlen [in] - The length of the nonce buffer. * nlen [in] - The length of the nonce buffer.
* realm [in/out] - The buffer where the realm will be stored. * realm [in/out] - The buffer where the realm will be stored.
@ -266,55 +266,35 @@ static CURLcode auth_digest_get_qop_values(const char *options, int *value)
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
static CURLcode auth_decode_digest_md5_message(const char *chlg64, static CURLcode auth_decode_digest_md5_message(const struct bufref *chlgref,
char *nonce, size_t nlen, char *nonce, size_t nlen,
char *realm, size_t rlen, char *realm, size_t rlen,
char *alg, size_t alen, char *alg, size_t alen,
char *qop, size_t qlen) char *qop, size_t qlen)
{ {
CURLcode result = CURLE_OK; const char *chlg = (const char *) Curl_bufref_ptr(chlgref);
unsigned char *chlg = NULL;
size_t chlglen = 0;
size_t chlg64len = strlen(chlg64);
/* Decode the base-64 encoded challenge message */
if(chlg64len && *chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */ /* Ensure we have a valid challenge message */
if(!chlg) if(!Curl_bufref_len(chlgref))
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
/* Retrieve nonce string from the challenge */ /* Retrieve nonce string from the challenge */
if(!auth_digest_get_key_value((char *) chlg, "nonce=\"", nonce, nlen, if(!auth_digest_get_key_value(chlg, "nonce=\"", nonce, nlen, '\"'))
'\"')) {
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
}
/* Retrieve realm string from the challenge */ /* Retrieve realm string from the challenge */
if(!auth_digest_get_key_value((char *) chlg, "realm=\"", realm, rlen, if(!auth_digest_get_key_value(chlg, "realm=\"", realm, rlen, '\"')) {
'\"')) {
/* Challenge does not have a realm, set empty string [RFC2831] page 6 */ /* Challenge does not have a realm, set empty string [RFC2831] page 6 */
strcpy(realm, ""); strcpy(realm, "");
} }
/* Retrieve algorithm string from the challenge */ /* Retrieve algorithm string from the challenge */
if(!auth_digest_get_key_value((char *) chlg, "algorithm=", alg, alen, ',')) { if(!auth_digest_get_key_value(chlg, "algorithm=", alg, alen, ','))
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
}
/* Retrieve qop-options string from the challenge */ /* Retrieve qop-options string from the challenge */
if(!auth_digest_get_key_value((char *) chlg, "qop=\"", qop, qlen, '\"')) { if(!auth_digest_get_key_value(chlg, "qop=\"", qop, qlen, '\"'))
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
}
free(chlg);
return CURLE_OK; return CURLE_OK;
} }
@ -342,22 +322,20 @@ bool Curl_auth_is_digest_supported(void)
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* chlg64 [in] - The base64 encoded challenge message. * chlg [in] - The challenge message.
* userp [in] - The user name. * userp [in] - The user name.
* passwdp [in] - The user's password. * passwdp [in] - The user's password.
* service [in] - The service type such as http, smtp, pop or imap. * service [in] - The service type such as http, smtp, pop or imap.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data, CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
const char *service, const char *service,
char **outptr, size_t *outlen) struct bufref *out)
{ {
size_t i; size_t i;
struct MD5_context *ctxt; struct MD5_context *ctxt;
@ -378,9 +356,10 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
char *spn = NULL; char *spn = NULL;
/* Decode the challenge message */ /* Decode the challenge message */
CURLcode result = auth_decode_digest_md5_message(chlg64, nonce, CURLcode result = auth_decode_digest_md5_message(chlg,
sizeof(nonce), realm, nonce, sizeof(nonce),
sizeof(realm), algorithm, realm, sizeof(realm),
algorithm,
sizeof(algorithm), sizeof(algorithm),
qop_options, qop_options,
sizeof(qop_options)); sizeof(qop_options));
@ -500,11 +479,8 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
if(!response) if(!response)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
/* Base64 encode the response */ /* Return the response. */
result = Curl_base64_encode(data, response, 0, outptr, outlen); Curl_bufref_set(out, response, strlen(response), curl_free);
free(response);
return result; return result;
} }

View File

@ -6,7 +6,7 @@
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 2014 - 2016, Steve Holme, <steve_holme@hotmail.com>. * Copyright (C) 2014 - 2016, Steve Holme, <steve_holme@hotmail.com>.
* Copyright (C) 2015 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 2015 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -32,7 +32,6 @@
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "vauth/digest.h" #include "vauth/digest.h"
#include "urldata.h" #include "urldata.h"
#include "curl_base64.h"
#include "warnless.h" #include "warnless.h"
#include "curl_multibyte.h" #include "curl_multibyte.h"
#include "sendf.h" #include "sendf.h"
@ -79,28 +78,24 @@ bool Curl_auth_is_digest_supported(void)
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* chlg64 [in] - The base64 encoded challenge message. * chlg [in] - The challenge message.
* userp [in] - The user name in the format User or Domain\User. * userp [in] - The user name in the format User or Domain\User.
* passwdp [in] - The user's password. * passwdp [in] - The user's password.
* service [in] - The service type such as http, smtp, pop or imap. * service [in] - The service type such as http, smtp, pop or imap.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data, CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
const char *service, const char *service,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
TCHAR *spn = NULL; TCHAR *spn = NULL;
size_t chlglen = 0;
size_t token_max = 0; size_t token_max = 0;
unsigned char *input_token = NULL;
unsigned char *output_token = NULL; unsigned char *output_token = NULL;
CredHandle credentials; CredHandle credentials;
CtxtHandle context; CtxtHandle context;
@ -115,17 +110,9 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
unsigned long attrs; unsigned long attrs;
TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */ TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
/* Decode the base-64 encoded challenge message */
if(strlen(chlg64) && *chlg64 != '=') {
result = Curl_base64_decode(chlg64, &input_token, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */ /* Ensure we have a valid challenge message */
if(!input_token) { if(!Curl_bufref_len(chlg)) {
infof(data, "DIGEST-MD5 handshake failure (empty challenge message)\n"); infof(data, "DIGEST-MD5 handshake failure (empty challenge message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -133,8 +120,6 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST), status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT(SP_NAME_DIGEST),
&SecurityPackage); &SecurityPackage);
if(status != SEC_E_OK) { if(status != SEC_E_OK) {
free(input_token);
failf(data, "SSPI: couldn't get auth info"); failf(data, "SSPI: couldn't get auth info");
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
@ -146,18 +131,13 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
/* Allocate our response buffer */ /* Allocate our response buffer */
output_token = malloc(token_max); output_token = malloc(token_max);
if(!output_token) { if(!output_token)
free(input_token);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
}
/* Generate our SPN */ /* Generate our SPN */
spn = Curl_auth_build_spn(service, data->conn->host.name, NULL); spn = Curl_auth_build_spn(service, data->conn->host.name, NULL);
if(!spn) { if(!spn) {
free(output_token); free(output_token);
free(input_token);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }
@ -167,8 +147,6 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
if(result) { if(result) {
free(spn); free(spn);
free(output_token); free(output_token);
free(input_token);
return result; return result;
} }
@ -190,8 +168,6 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
Curl_sspi_free_identity(p_identity); Curl_sspi_free_identity(p_identity);
free(spn); free(spn);
free(output_token); free(output_token);
free(input_token);
return CURLE_LOGIN_DENIED; return CURLE_LOGIN_DENIED;
} }
@ -200,8 +176,8 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
chlg_desc.cBuffers = 1; chlg_desc.cBuffers = 1;
chlg_desc.pBuffers = &chlg_buf; chlg_desc.pBuffers = &chlg_buf;
chlg_buf.BufferType = SECBUFFER_TOKEN; chlg_buf.BufferType = SECBUFFER_TOKEN;
chlg_buf.pvBuffer = input_token; chlg_buf.pvBuffer = (void *) Curl_bufref_ptr(chlg);
chlg_buf.cbBuffer = curlx_uztoul(chlglen); chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
/* Setup the response "output" security buffer */ /* Setup the response "output" security buffer */
resp_desc.ulVersion = SECBUFFER_VERSION; resp_desc.ulVersion = SECBUFFER_VERSION;
@ -227,7 +203,6 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
Curl_sspi_free_identity(p_identity); Curl_sspi_free_identity(p_identity);
free(spn); free(spn);
free(output_token); free(output_token);
free(input_token);
if(status == SEC_E_INSUFFICIENT_MEMORY) if(status == SEC_E_INSUFFICIENT_MEMORY)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
@ -238,9 +213,8 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
/* Base64 encode the response */ /* Return the response. */
result = Curl_base64_encode(data, (char *) output_token, resp_buf.cbBuffer, Curl_bufref_set(out, output_token, resp_buf.cbBuffer, curl_free);
outptr, outlen);
/* Free our handles */ /* Free our handles */
s_pSecFn->DeleteSecurityContext(&context); s_pSecFn->DeleteSecurityContext(&context);
@ -252,12 +226,6 @@ CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
/* Free the SPN */ /* Free the SPN */
free(spn); free(spn);
/* Free the response buffer */
free(output_token);
/* Free the decoded challenge message */
free(input_token);
return result; return result;
} }

View File

@ -28,7 +28,6 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "curl_base64.h"
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "urldata.h" #include "urldata.h"
#include "sendf.h" #include "sendf.h"
@ -94,42 +93,24 @@ CURLcode Curl_auth_gsasl_start(struct Curl_easy *data,
} }
CURLcode Curl_auth_gsasl_token(struct Curl_easy *data, CURLcode Curl_auth_gsasl_token(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
struct gsasldata *gsasl, struct gsasldata *gsasl,
char **outptr, size_t *outlen) struct bufref *out)
{ {
unsigned char *chlg = NULL;
size_t chlglen = 0;
CURLcode result = CURLE_OK;
int res; int res;
char *response; char *response;
size_t outlen;
if(chlg64) {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
res = gsasl_step(gsasl->client, res = gsasl_step(gsasl->client,
(const char *)chlg, chlglen, &response, outlen); (const char *) Curl_bufref_ptr(chlg), Curl_bufref_len(chlg),
&response, &outlen);
if(res != GSASL_OK && res != GSASL_NEEDS_MORE) { if(res != GSASL_OK && res != GSASL_NEEDS_MORE) {
if(chlg64)
free(chlg);
failf(data, "GSASL step: %s\n", gsasl_strerror(res)); failf(data, "GSASL step: %s\n", gsasl_strerror(res));
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
if(*outlen > 0) { Curl_bufref_set(out, response, outlen, gsasl_free);
result = Curl_base64_encode(data, response, 0, outptr, outlen); return CURLE_OK;
gsasl_free(response);
}
else {
*outptr = strdup("");
if(!*outptr)
result = CURLE_OUT_OF_MEMORY;
}
return result;
} }
void Curl_auth_gsasl_cleanup(struct gsasldata *gsasl) void Curl_auth_gsasl_cleanup(struct gsasldata *gsasl)

View File

@ -6,7 +6,7 @@
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>. * Copyright (C) 2014 - 2019, Steve Holme, <steve_holme@hotmail.com>.
* Copyright (C) 2015 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 2015 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -32,7 +32,6 @@
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "curl_sasl.h" #include "curl_sasl.h"
#include "urldata.h" #include "urldata.h"
#include "curl_base64.h"
#include "curl_gssapi.h" #include "curl_gssapi.h"
#include "sendf.h" #include "sendf.h"
#include "curl_printf.h" #include "curl_printf.h"
@ -70,12 +69,9 @@ bool Curl_auth_is_gssapi_supported(void)
* host [in[ - The host name. * host [in[ - The host name.
* mutual_auth [in] - Flag specifying whether or not mutual authentication * mutual_auth [in] - Flag specifying whether or not mutual authentication
* is enabled. * is enabled.
* chlg64 [in] - Pointer to the optional base64 encoded challenge * chlg [in] - Optional challenge message.
* message.
* krb5 [in/out] - The Kerberos 5 data struct being used and modified. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -85,13 +81,11 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
const char *service, const char *service,
const char *host, const char *host,
const bool mutual_auth, const bool mutual_auth,
const char *chlg64, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
size_t chlglen = 0;
unsigned char *chlg = NULL;
OM_uint32 major_status; OM_uint32 major_status;
OM_uint32 minor_status; OM_uint32 minor_status;
OM_uint32 unused_status; OM_uint32 unused_status;
@ -127,24 +121,13 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
free(spn); free(spn);
} }
if(chlg64 && *chlg64) { if(chlg) {
/* Decode the base-64 encoded challenge message */ if(!Curl_bufref_len(chlg)) {
if(*chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */
if(!chlg) {
infof(data, "GSSAPI handshake failure (empty challenge message)\n"); infof(data, "GSSAPI handshake failure (empty challenge message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
input_token.value = (void *) Curl_bufref_ptr(chlg);
/* Setup the challenge "input" security buffer */ input_token.length = Curl_bufref_len(chlg);
input_token.value = chlg;
input_token.length = chlglen;
} }
major_status = Curl_gss_init_sec_context(data, major_status = Curl_gss_init_sec_context(data,
@ -158,9 +141,6 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
mutual_auth, mutual_auth,
NULL); NULL);
/* Free the decoded challenge as it is not required anymore */
free(input_token.value);
if(GSS_ERROR(major_status)) { if(GSS_ERROR(major_status)) {
if(output_token.value) if(output_token.value)
gss_release_buffer(&unused_status, &output_token); gss_release_buffer(&unused_status, &output_token);
@ -172,17 +152,11 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
} }
if(output_token.value && output_token.length) { if(output_token.value && output_token.length) {
/* Base64 encode the response */ result = Curl_bufref_memdup(out, output_token.value, output_token.length);
result = Curl_base64_encode(data, (char *) output_token.value,
output_token.length, outptr, outlen);
gss_release_buffer(&unused_status, &output_token); gss_release_buffer(&unused_status, &output_token);
} }
else if(mutual_auth) { else
*outptr = strdup(""); Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
if(!*outptr)
result = CURLE_OUT_OF_MEMORY;
}
return result; return result;
} }
@ -196,24 +170,19 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* chlg64 [in] - Pointer to the optional base64 encoded challenge message. * chlg [in] - Optional challenge message.
* krb5 [in/out] - The Kerberos 5 data struct being used and modified. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data, CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, struct bufref *out)
size_t *outlen)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
size_t chlglen = 0;
size_t messagelen = 0; size_t messagelen = 0;
unsigned char *chlg = NULL;
unsigned char *message = NULL; unsigned char *message = NULL;
OM_uint32 major_status; OM_uint32 major_status;
OM_uint32 minor_status; OM_uint32 minor_status;
@ -228,17 +197,9 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
gss_name_t username = GSS_C_NO_NAME; gss_name_t username = GSS_C_NO_NAME;
gss_buffer_desc username_token; gss_buffer_desc username_token;
/* Decode the base-64 encoded input message */
if(strlen(chlg64) && *chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */ /* Ensure we have a valid challenge message */
if(!chlg) { if(!Curl_bufref_len(chlg)) {
infof(data, "GSSAPI handshake failure (empty security message)\n"); infof(data, "GSSAPI handshake failure (empty security message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -249,9 +210,6 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
if(GSS_ERROR(major_status)) { if(GSS_ERROR(major_status)) {
Curl_gss_log_error(data, "gss_inquire_context() failed: ", Curl_gss_log_error(data, "gss_inquire_context() failed: ",
major_status, minor_status); major_status, minor_status);
free(chlg);
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
@ -261,15 +219,12 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
if(GSS_ERROR(major_status)) { if(GSS_ERROR(major_status)) {
Curl_gss_log_error(data, "gss_display_name() failed: ", Curl_gss_log_error(data, "gss_display_name() failed: ",
major_status, minor_status); major_status, minor_status);
free(chlg);
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
/* Setup the challenge "input" security buffer */ /* Setup the challenge "input" security buffer */
input_token.value = chlg; input_token.value = (void *) Curl_bufref_ptr(chlg);
input_token.length = chlglen; input_token.length = Curl_bufref_len(chlg);
/* Decrypt the inbound challenge and obtain the qop */ /* Decrypt the inbound challenge and obtain the qop */
major_status = gss_unwrap(&minor_status, krb5->context, &input_token, major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
@ -277,27 +232,20 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
if(GSS_ERROR(major_status)) { if(GSS_ERROR(major_status)) {
Curl_gss_log_error(data, "gss_unwrap() failed: ", Curl_gss_log_error(data, "gss_unwrap() failed: ",
major_status, minor_status); major_status, minor_status);
gss_release_buffer(&unused_status, &username_token); gss_release_buffer(&unused_status, &username_token);
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
/* Not 4 octets long so fail as per RFC4752 Section 3.1 */ /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
if(output_token.length != 4) { if(output_token.length != 4) {
infof(data, "GSSAPI handshake failure (invalid security data)\n"); infof(data, "GSSAPI handshake failure (invalid security data)\n");
gss_release_buffer(&unused_status, &username_token); gss_release_buffer(&unused_status, &username_token);
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
/* Copy the data out and free the challenge as it is not required anymore */ /* Copy the data out and free the challenge as it is not required anymore */
memcpy(&indata, output_token.value, 4); memcpy(&indata, output_token.value, 4);
gss_release_buffer(&unused_status, &output_token); gss_release_buffer(&unused_status, &output_token);
free(chlg);
/* Extract the security layer */ /* Extract the security layer */
sec_layer = indata & 0x000000FF; sec_layer = indata & 0x000000FF;
@ -305,7 +253,6 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
infof(data, "GSSAPI handshake failure (invalid security layer)\n"); infof(data, "GSSAPI handshake failure (invalid security layer)\n");
gss_release_buffer(&unused_status, &username_token); gss_release_buffer(&unused_status, &username_token);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -323,7 +270,6 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
message = malloc(messagelen); message = malloc(messagelen);
if(!message) { if(!message) {
gss_release_buffer(&unused_status, &username_token); gss_release_buffer(&unused_status, &username_token);
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }
@ -352,16 +298,12 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
if(GSS_ERROR(major_status)) { if(GSS_ERROR(major_status)) {
Curl_gss_log_error(data, "gss_wrap() failed: ", Curl_gss_log_error(data, "gss_wrap() failed: ",
major_status, minor_status); major_status, minor_status);
free(message); free(message);
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
/* Base64 encode the response */ /* Return the response. */
result = Curl_base64_encode(data, (char *) output_token.value, result = Curl_bufref_memdup(out, output_token.value, output_token.length);
output_token.length, outptr, outlen);
/* Free the output buffer */ /* Free the output buffer */
gss_release_buffer(&unused_status, &output_token); gss_release_buffer(&unused_status, &output_token);

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 2014 - 2020, Steve Holme, <steve_holme@hotmail.com>. * Copyright (C) 2014 - 2021, Steve Holme, <steve_holme@hotmail.com>.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -30,7 +30,6 @@
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "urldata.h" #include "urldata.h"
#include "curl_base64.h"
#include "warnless.h" #include "warnless.h"
#include "curl_multibyte.h" #include "curl_multibyte.h"
#include "sendf.h" #include "sendf.h"
@ -81,11 +80,9 @@ bool Curl_auth_is_gssapi_supported(void)
* host [in] - The host name. * host [in] - The host name.
* mutual_auth [in] - Flag specifying whether or not mutual authentication * mutual_auth [in] - Flag specifying whether or not mutual authentication
* is enabled. * is enabled.
* chlg64 [in] - The optional base64 encoded challenge message. * chlg [in] - Optional challenge message.
* krb5 [in/out] - The Kerberos 5 data struct being used and modified. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -95,13 +92,11 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
const char *service, const char *service,
const char *host, const char *host,
const bool mutual_auth, const bool mutual_auth,
const char *chlg64, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
size_t chlglen = 0;
unsigned char *chlg = NULL;
CtxtHandle context; CtxtHandle context;
PSecPkgInfo SecurityPackage; PSecPkgInfo SecurityPackage;
SecBuffer chlg_buf; SecBuffer chlg_buf;
@ -176,18 +171,9 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
} }
if(chlg64 && *chlg64) { if(chlg) {
/* Decode the base-64 encoded challenge message */ if(!Curl_bufref_len(chlg)) {
if(*chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */
if(!chlg) {
infof(data, "GSSAPI handshake failure (empty challenge message)\n"); infof(data, "GSSAPI handshake failure (empty challenge message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -196,8 +182,8 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
chlg_desc.cBuffers = 1; chlg_desc.cBuffers = 1;
chlg_desc.pBuffers = &chlg_buf; chlg_desc.pBuffers = &chlg_buf;
chlg_buf.BufferType = SECBUFFER_TOKEN; chlg_buf.BufferType = SECBUFFER_TOKEN;
chlg_buf.pvBuffer = chlg; chlg_buf.pvBuffer = (void *) Curl_bufref_ptr(chlg);
chlg_buf.cbBuffer = curlx_uztoul(chlglen); chlg_buf.cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
} }
/* Setup the response "output" security buffer */ /* Setup the response "output" security buffer */
@ -220,16 +206,11 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
&resp_desc, &attrs, &resp_desc, &attrs,
&expiry); &expiry);
/* Free the decoded challenge as it is not required anymore */ if(status == SEC_E_INSUFFICIENT_MEMORY)
free(chlg);
if(status == SEC_E_INSUFFICIENT_MEMORY) {
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
}
if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) { if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
}
if(memcmp(&context, krb5->context, sizeof(context))) { if(memcmp(&context, krb5->context, sizeof(context))) {
s_pSecFn->DeleteSecurityContext(krb5->context); s_pSecFn->DeleteSecurityContext(krb5->context);
@ -238,15 +219,12 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
} }
if(resp_buf.cbBuffer) { if(resp_buf.cbBuffer) {
/* Base64 encode the response */ result = Curl_bufref_memdup(out, resp_buf.pvBuffer, resp_buf.cbBuffer);
result = Curl_base64_encode(data, (char *) resp_buf.pvBuffer,
resp_buf.cbBuffer, outptr, outlen);
}
else if(mutual_auth) {
*outptr = strdup("");
if(!*outptr)
result = CURLE_OUT_OF_MEMORY;
} }
else if(mutual_auth)
Curl_bufref_set(out, "", 0, NULL);
else
Curl_bufref_set(out, NULL, 0, NULL);
return result; return result;
} }
@ -260,26 +238,20 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* chlg64 [in] - The optional base64 encoded challenge message. * chlg [in] - The optional challenge message.
* krb5 [in/out] - The Kerberos 5 data struct being used and modified. * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data, CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, struct bufref *out)
size_t *outlen)
{ {
CURLcode result = CURLE_OK;
size_t offset = 0; size_t offset = 0;
size_t chlglen = 0;
size_t messagelen = 0; size_t messagelen = 0;
size_t appdatalen = 0; size_t appdatalen = 0;
unsigned char *chlg = NULL;
unsigned char *trailer = NULL; unsigned char *trailer = NULL;
unsigned char *message = NULL; unsigned char *message = NULL;
unsigned char *padding = NULL; unsigned char *padding = NULL;
@ -298,17 +270,9 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
SECURITY_STATUS status; SECURITY_STATUS status;
char *user_name; char *user_name;
/* Decode the base-64 encoded input message */
if(strlen(chlg64) && *chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Ensure we have a valid challenge message */ /* Ensure we have a valid challenge message */
if(!chlg) { if(!Curl_bufref_len(chlg)) {
infof(data, "GSSAPI handshake failure (empty security message)\n"); infof(data, "GSSAPI handshake failure (empty security message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -316,35 +280,31 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
status = s_pSecFn->QueryContextAttributes(krb5->context, status = s_pSecFn->QueryContextAttributes(krb5->context,
SECPKG_ATTR_SIZES, SECPKG_ATTR_SIZES,
&sizes); &sizes);
if(status != SEC_E_OK) {
free(chlg);
if(status == SEC_E_INSUFFICIENT_MEMORY) if(status == SEC_E_INSUFFICIENT_MEMORY)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
if(status != SEC_E_OK)
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
}
/* Get the fully qualified username back from the context */ /* Get the fully qualified username back from the context */
status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials, status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
SECPKG_CRED_ATTR_NAMES, SECPKG_CRED_ATTR_NAMES,
&names); &names);
if(status != SEC_E_OK) {
free(chlg);
if(status == SEC_E_INSUFFICIENT_MEMORY) if(status == SEC_E_INSUFFICIENT_MEMORY)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
if(status != SEC_E_OK)
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
}
/* Setup the "input" security buffer */ /* Setup the "input" security buffer */
input_desc.ulVersion = SECBUFFER_VERSION; input_desc.ulVersion = SECBUFFER_VERSION;
input_desc.cBuffers = 2; input_desc.cBuffers = 2;
input_desc.pBuffers = input_buf; input_desc.pBuffers = input_buf;
input_buf[0].BufferType = SECBUFFER_STREAM; input_buf[0].BufferType = SECBUFFER_STREAM;
input_buf[0].pvBuffer = chlg; input_buf[0].pvBuffer = (void *) Curl_bufref_ptr(chlg);
input_buf[0].cbBuffer = curlx_uztoul(chlglen); input_buf[0].cbBuffer = curlx_uztoul(Curl_bufref_len(chlg));
input_buf[1].BufferType = SECBUFFER_DATA; input_buf[1].BufferType = SECBUFFER_DATA;
input_buf[1].pvBuffer = NULL; input_buf[1].pvBuffer = NULL;
input_buf[1].cbBuffer = 0; input_buf[1].cbBuffer = 0;
@ -353,31 +313,23 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop); status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
if(status != SEC_E_OK) { if(status != SEC_E_OK) {
infof(data, "GSSAPI handshake failure (empty security message)\n"); infof(data, "GSSAPI handshake failure (empty security message)\n");
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
/* Not 4 octets long so fail as per RFC4752 Section 3.1 */ /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
if(input_buf[1].cbBuffer != 4) { if(input_buf[1].cbBuffer != 4) {
infof(data, "GSSAPI handshake failure (invalid security data)\n"); infof(data, "GSSAPI handshake failure (invalid security data)\n");
free(chlg);
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
/* Copy the data out and free the challenge as it is not required anymore */ /* Copy the data out and free the challenge as it is not required anymore */
memcpy(&indata, input_buf[1].pvBuffer, 4); memcpy(&indata, input_buf[1].pvBuffer, 4);
s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer); s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
free(chlg);
/* Extract the security layer */ /* Extract the security layer */
sec_layer = indata & 0x000000FF; sec_layer = indata & 0x000000FF;
if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) { if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
infof(data, "GSSAPI handshake failure (invalid security layer)\n"); infof(data, "GSSAPI handshake failure (invalid security layer)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -479,17 +431,14 @@ CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
offset += wrap_buf[1].cbBuffer; offset += wrap_buf[1].cbBuffer;
memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer); memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
/* Base64 encode the response */
result = Curl_base64_encode(data, (char *) appdata, appdatalen, outptr,
outlen);
/* Free all of our local buffers */ /* Free all of our local buffers */
free(appdata);
free(padding); free(padding);
free(message); free(message);
free(trailer); free(trailer);
return result; /* Return the response. */
Curl_bufref_set(out, appdata, appdatalen, curl_free);
return CURLE_OK;
} }
/* /*

View File

@ -36,7 +36,6 @@
#include "urldata.h" #include "urldata.h"
#include "non-ascii.h" #include "non-ascii.h"
#include "sendf.h" #include "sendf.h"
#include "curl_base64.h"
#include "curl_ntlm_core.h" #include "curl_ntlm_core.h"
#include "curl_gethostname.h" #include "curl_gethostname.h"
#include "curl_multibyte.h" #include "curl_multibyte.h"
@ -157,31 +156,30 @@ static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* buffer [in] - The decoded type-2 message. * type2ref [in] - The type-2 message.
* size [in] - The input buffer size, at least 32 bytes.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
static CURLcode ntlm_decode_type2_target(struct Curl_easy *data, static CURLcode ntlm_decode_type2_target(struct Curl_easy *data,
unsigned char *buffer, const struct bufref *type2ref,
size_t size,
struct ntlmdata *ntlm) struct ntlmdata *ntlm)
{ {
unsigned short target_info_len = 0; unsigned short target_info_len = 0;
unsigned int target_info_offset = 0; unsigned int target_info_offset = 0;
const unsigned char *type2 = Curl_bufref_ptr(type2ref);
size_t type2len = Curl_bufref_len(type2ref);
#if defined(CURL_DISABLE_VERBOSE_STRINGS) #if defined(CURL_DISABLE_VERBOSE_STRINGS)
(void) data; (void) data;
#endif #endif
if(size >= 48) { if(type2len >= 48) {
target_info_len = Curl_read16_le(&buffer[40]); target_info_len = Curl_read16_le(&type2[40]);
target_info_offset = Curl_read32_le(&buffer[44]); target_info_offset = Curl_read32_le(&type2[44]);
if(target_info_len > 0) { if(target_info_len > 0) {
if((target_info_offset >= size) || if((target_info_offset + target_info_len) > type2len ||
((target_info_offset + target_info_len) > size) || target_info_offset < 48) {
(target_info_offset < 48)) {
infof(data, "NTLM handshake failure (bad type-2 message). " infof(data, "NTLM handshake failure (bad type-2 message). "
"Target Info Offset Len is set incorrect by the peer\n"); "Target Info Offset Len is set incorrect by the peer\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
@ -192,7 +190,7 @@ static CURLcode ntlm_decode_type2_target(struct Curl_easy *data,
if(!ntlm->target_info) if(!ntlm->target_info)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
memcpy(ntlm->target_info, &buffer[target_info_offset], target_info_len); memcpy(ntlm->target_info, &type2[target_info_offset], target_info_len);
} }
} }
@ -234,21 +232,20 @@ bool Curl_auth_is_ntlm_supported(void)
/* /*
* Curl_auth_decode_ntlm_type2_message() * Curl_auth_decode_ntlm_type2_message()
* *
* This is used to decode an already encoded NTLM type-2 message. The message * This is used to decode an NTLM type-2 message. The raw NTLM message is
* is first decoded from a base64 string into a raw NTLM message and checked * checked * for validity before the appropriate data for creating a type-3
* for validity before the appropriate data for creating a type-3 message is * message is * written to the given NTLM data structure.
* written to the given NTLM data structure.
* *
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* type2msg [in] - The base64 encoded type-2 message. * type2ref [in] - The type-2 message.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data, CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
const char *type2msg, const struct bufref *type2ref,
struct ntlmdata *ntlm) struct ntlmdata *ntlm)
{ {
static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 }; static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
@ -270,8 +267,8 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
*/ */
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
unsigned char *type2 = NULL; const unsigned char *type2 = Curl_bufref_ptr(type2ref);
size_t type2_len = 0; size_t type2len = Curl_bufref_len(type2ref);
#if defined(NTLM_NEEDS_NSS_INIT) #if defined(NTLM_NEEDS_NSS_INIT)
/* Make sure the crypto backend is initialized */ /* Make sure the crypto backend is initialized */
@ -282,26 +279,12 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
(void)data; (void)data;
#endif #endif
/* Decode the base-64 encoded type-2 message */
if(strlen(type2msg) && *type2msg != '=') {
result = Curl_base64_decode(type2msg, &type2, &type2_len);
if(result)
return result;
}
/* Ensure we have a valid type-2 message */
if(!type2) {
infof(data, "NTLM handshake failure (empty type-2 message)\n");
return CURLE_BAD_CONTENT_ENCODING;
}
ntlm->flags = 0; ntlm->flags = 0;
if((type2_len < 32) || if((type2len < 32) ||
(memcmp(type2, NTLMSSP_SIGNATURE, 8) != 0) || (memcmp(type2, NTLMSSP_SIGNATURE, 8) != 0) ||
(memcmp(type2 + 8, type2_marker, sizeof(type2_marker)) != 0)) { (memcmp(type2 + 8, type2_marker, sizeof(type2_marker)) != 0)) {
/* This was not a good enough type-2 message */ /* This was not a good enough type-2 message */
free(type2);
infof(data, "NTLM handshake failure (bad type-2 message)\n"); infof(data, "NTLM handshake failure (bad type-2 message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
@ -310,9 +293,8 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
memcpy(ntlm->nonce, &type2[24], 8); memcpy(ntlm->nonce, &type2[24], 8);
if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) { if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) {
result = ntlm_decode_type2_target(data, type2, type2_len, ntlm); result = ntlm_decode_type2_target(data, type2ref, ntlm);
if(result) { if(result) {
free(type2);
infof(data, "NTLM handshake failure (bad type-2 message)\n"); infof(data, "NTLM handshake failure (bad type-2 message)\n");
return result; return result;
} }
@ -327,8 +309,6 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
fprintf(stderr, "**** Header %s\n ", header); fprintf(stderr, "**** Header %s\n ", header);
}); });
free(type2);
return result; return result;
} }
@ -346,8 +326,8 @@ static void unicodecpy(unsigned char *dest, const char *src, size_t length)
/* /*
* Curl_auth_create_ntlm_type1_message() * Curl_auth_create_ntlm_type1_message()
* *
* This is used to generate an already encoded NTLM type-1 message ready for * This is used to generate an NTLM type-1 message ready for sending to the
* sending to the recipient using the appropriate compile time crypto API. * recipient using the appropriate compile time crypto API.
* *
* Parameters: * Parameters:
* *
@ -357,9 +337,7 @@ static void unicodecpy(unsigned char *dest, const char *src, size_t length)
* service [in] - The service type such as http, smtp, pop or imap. * service [in] - The service type such as http, smtp, pop or imap.
* host [in] - The host name. * host [in] - The host name.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -369,7 +347,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
const char *service, const char *service,
const char *hostname, const char *hostname,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, size_t *outlen) struct bufref *out)
{ {
/* NTLM type-1 message structure: /* NTLM type-1 message structure:
@ -387,7 +365,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
size_t size; size_t size;
unsigned char ntlmbuf[NTLM_BUFSIZE]; char *ntlmbuf;
const char *host = ""; /* empty */ const char *host = ""; /* empty */
const char *domain = ""; /* empty */ const char *domain = ""; /* empty */
size_t hostlen = 0; size_t hostlen = 0;
@ -395,6 +373,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
size_t hostoff = 0; size_t hostoff = 0;
size_t domoff = hostoff + hostlen; /* This is 0: remember that host and size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
domain are empty */ domain are empty */
(void)data;
(void)userp; (void)userp;
(void)passwdp; (void)passwdp;
(void)service, (void)service,
@ -409,8 +388,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
#else #else
#define NTLM2FLAG 0 #define NTLM2FLAG 0
#endif #endif
msnprintf((char *)ntlmbuf, NTLM_BUFSIZE, ntlmbuf = aprintf(NTLMSSP_SIGNATURE "%c"
NTLMSSP_SIGNATURE "%c"
"\x01%c%c%c" /* 32-bit type = 1 */ "\x01%c%c%c" /* 32-bit type = 1 */
"%c%c%c%c" /* 32-bit NTLM flag field */ "%c%c%c%c" /* 32-bit NTLM flag field */
"%c%c" /* domain length */ "%c%c" /* domain length */
@ -442,6 +420,9 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
host, /* this is empty */ host, /* this is empty */
domain /* this is empty */); domain /* this is empty */);
if(!ntlmbuf)
return CURLE_OUT_OF_MEMORY;
/* Initial packet length */ /* Initial packet length */
size = 32 + hostlen + domlen; size = 32 + hostlen + domlen;
@ -467,8 +448,8 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
fprintf(stderr, "\n****\n"); fprintf(stderr, "\n****\n");
}); });
/* Return with binary blob encoded into base64 */ Curl_bufref_set(out, ntlmbuf, size, curl_free);
return Curl_base64_encode(data, (char *)ntlmbuf, size, outptr, outlen); return CURLE_OK;
} }
/* /*
@ -483,9 +464,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
* userp [in] - The user name in the format User or Domain\User. * userp [in] - The user name in the format User or Domain\User.
* passwdp [in] - The user's password. * passwdp [in] - The user's password.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -493,7 +472,7 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, size_t *outlen) struct bufref *out)
{ {
/* NTLM type-3 message structure: /* NTLM type-3 message structure:
@ -847,8 +826,8 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
if(result) if(result)
return CURLE_CONV_FAILED; return CURLE_CONV_FAILED;
/* Return with binary blob encoded into base64 */ /* Return the binary blob. */
result = Curl_base64_encode(data, (char *)ntlmbuf, size, outptr, outlen); result = Curl_bufref_memdup(out, ntlmbuf, size);
Curl_auth_cleanup_ntlm(ntlm); Curl_auth_cleanup_ntlm(ntlm);

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -28,7 +28,6 @@
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "urldata.h" #include "urldata.h"
#include "curl_base64.h"
#include "curl_ntlm_core.h" #include "curl_ntlm_core.h"
#include "warnless.h" #include "warnless.h"
#include "curl_multibyte.h" #include "curl_multibyte.h"
@ -78,9 +77,7 @@ bool Curl_auth_is_ntlm_supported(void)
* service [in] - The service type such as http, smtp, pop or imap. * service [in] - The service type such as http, smtp, pop or imap.
* host [in] - The host name. * host [in] - The host name.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -90,7 +87,7 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
const char *service, const char *service,
const char *host, const char *host,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, size_t *outlen) struct bufref *out)
{ {
PSecPkgInfo SecurityPackage; PSecPkgInfo SecurityPackage;
SecBuffer type_1_buf; SecBuffer type_1_buf;
@ -181,9 +178,9 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) else if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED)
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
/* Base64 encode the response */ /* Return the response. */
return Curl_base64_encode(data, (char *) ntlm->output_token, Curl_bufref_set(out, ntlm->output_token, type_1_buf.cbBuffer, NULL);
type_1_buf.cbBuffer, outptr, outlen); return CURLE_OK;
} }
/* /*
@ -194,42 +191,34 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
* Parameters: * Parameters:
* *
* data [in] - The session handle. * data [in] - The session handle.
* type2msg [in] - The base64 encoded type-2 message. * type2 [in] - The type-2 message.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data, CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
const char *type2msg, const struct bufref *type2,
struct ntlmdata *ntlm) struct ntlmdata *ntlm)
{ {
CURLcode result = CURLE_OK;
unsigned char *type2 = NULL;
size_t type2_len = 0;
#if defined(CURL_DISABLE_VERBOSE_STRINGS) #if defined(CURL_DISABLE_VERBOSE_STRINGS)
(void) data; (void) data;
#endif #endif
/* Decode the base-64 encoded type-2 message */
if(strlen(type2msg) && *type2msg != '=') {
result = Curl_base64_decode(type2msg, &type2, &type2_len);
if(result)
return result;
}
/* Ensure we have a valid type-2 message */ /* Ensure we have a valid type-2 message */
if(!type2) { if(!Curl_bufref_len(type2)) {
infof(data, "NTLM handshake failure (empty type-2 message)\n"); infof(data, "NTLM handshake failure (empty type-2 message)\n");
return CURLE_BAD_CONTENT_ENCODING; return CURLE_BAD_CONTENT_ENCODING;
} }
/* Simply store the challenge for use later */ /* Store the challenge for later use */
ntlm->input_token = type2; ntlm->input_token = malloc(Curl_bufref_len(type2) + 1);
ntlm->input_token_len = type2_len; if(!ntlm->input_token)
return CURLE_OUT_OF_MEMORY;
memcpy(ntlm->input_token, Curl_bufref_ptr(type2), Curl_bufref_len(type2));
ntlm->input_token[Curl_bufref_len(type2)] = '\0';
ntlm->input_token_len = Curl_bufref_len(type2);
return result; return CURLE_OK;
} }
/* /*
@ -245,9 +234,7 @@ CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
* userp [in] - The user name in the format User or Domain\User. * userp [in] - The user name in the format User or Domain\User.
* passwdp [in] - The user's password. * passwdp [in] - The user's password.
* ntlm [in/out] - The NTLM data struct being used and modified. * ntlm [in/out] - The NTLM data struct being used and modified.
* outptr [in/out] - The address where a pointer to newly allocated memory * out [out] - The result storage.
* holding the result will be stored upon completion.
* outlen [out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
@ -255,7 +242,7 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
SecBuffer type_2_bufs[2]; SecBuffer type_2_bufs[2];
@ -331,12 +318,9 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
return CURLE_AUTH_ERROR; return CURLE_AUTH_ERROR;
} }
/* Base64 encode the response */ /* Return the response. */
result = Curl_base64_encode(data, (char *) ntlm->output_token, result = Curl_bufref_memdup(out, ntlm->output_token, type_3_buf.cbBuffer);
type_3_buf.cbBuffer, outptr, outlen);
Curl_auth_cleanup_ntlm(ntlm); Curl_auth_cleanup_ntlm(ntlm);
return result; return result;
} }

View File

@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___ * | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____| * \___|\___/|_| \_\_____|
* *
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
* *
* This software is licensed as described in the file COPYING, which * This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms * you should have received as part of this distribution. The terms
@ -31,7 +31,6 @@
#include "urldata.h" #include "urldata.h"
#include "vauth/vauth.h" #include "vauth/vauth.h"
#include "curl_base64.h"
#include "warnless.h" #include "warnless.h"
#include "curl_printf.h" #include "curl_printf.h"
@ -42,31 +41,26 @@
/* /*
* Curl_auth_create_oauth_bearer_message() * Curl_auth_create_oauth_bearer_message()
* *
* This is used to generate an already encoded OAuth 2.0 message ready for * This is used to generate an OAuth 2.0 message ready for sending to the
* sending to the recipient. * recipient.
* *
* Parameters: * Parameters:
* *
* data[in] - The session handle.
* user[in] - The user name. * user[in] - The user name.
* host[in] - The host name. * host[in] - The host name.
* port[in] - The port(when not Port 80). * port[in] - The port(when not Port 80).
* bearer[in] - The bearer token. * bearer[in] - The bearer token.
* outptr[in / out] - The address where a pointer to newly allocated memory * out[out] - The result storage.
* holding the result will be stored upon completion.
* outlen[out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data, CURLcode Curl_auth_create_oauth_bearer_message(const char *user,
const char *user,
const char *host, const char *host,
const long port, const long port,
const char *bearer, const char *bearer,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK; char *oauth;
char *oauth = NULL;
/* Generate the message */ /* Generate the message */
if(port == 0 || port == 80) if(port == 0 || port == 80)
@ -78,49 +72,34 @@ CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data,
if(!oauth) if(!oauth)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
/* Base64 encode the reply */ Curl_bufref_set(out, oauth, strlen(oauth), curl_free);
result = Curl_base64_encode(data, oauth, strlen(oauth), outptr, outlen); return CURLE_OK;
free(oauth);
return result;
} }
/* /*
* Curl_auth_create_xoauth_bearer_message() * Curl_auth_create_xoauth_bearer_message()
* *
* This is used to generate an already encoded XOAuth 2.0 message ready for * This is used to generate a XOAuth 2.0 message ready for * sending to the
* sending to the recipient. * recipient.
* *
* Parameters: * Parameters:
* *
* data[in] - The session handle.
* user[in] - The user name. * user[in] - The user name.
* bearer[in] - The bearer token. * bearer[in] - The bearer token.
* outptr[in / out] - The address where a pointer to newly allocated memory * out[out] - The result storage.
* holding the result will be stored upon completion.
* outlen[out] - The length of the output message.
* *
* Returns CURLE_OK on success. * Returns CURLE_OK on success.
*/ */
CURLcode Curl_auth_create_xoauth_bearer_message(struct Curl_easy *data, CURLcode Curl_auth_create_xoauth_bearer_message(const char *user,
const char *user,
const char *bearer, const char *bearer,
char **outptr, size_t *outlen) struct bufref *out)
{ {
CURLcode result = CURLE_OK;
/* Generate the message */ /* Generate the message */
char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer); char *xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
if(!xoauth) if(!xoauth)
return CURLE_OUT_OF_MEMORY; return CURLE_OUT_OF_MEMORY;
/* Base64 encode the reply */ Curl_bufref_set(out, xoauth, strlen(xoauth), curl_free);
result = Curl_base64_encode(data, xoauth, strlen(xoauth), outptr, outlen); return CURLE_OK;
free(xoauth);
return result;
} }
#endif /* disabled, no users */ #endif /* disabled, no users */

View File

@ -24,6 +24,8 @@
#include <curl/curl.h> #include <curl/curl.h>
#include "bufref.h"
struct Curl_easy; struct Curl_easy;
#if !defined(CURL_DISABLE_CRYPTO_AUTH) #if !defined(CURL_DISABLE_CRYPTO_AUTH)
@ -62,45 +64,37 @@ TCHAR *Curl_auth_build_spn(const char *service, const char *host,
/* This is used to test if the user contains a Windows domain name */ /* This is used to test if the user contains a Windows domain name */
bool Curl_auth_user_contains_domain(const char *user); bool Curl_auth_user_contains_domain(const char *user);
/* This is used to generate a base64 encoded PLAIN cleartext message */ /* This is used to generate a PLAIN cleartext message */
CURLcode Curl_auth_create_plain_message(struct Curl_easy *data, CURLcode Curl_auth_create_plain_message(const char *authzid,
const char *authzid,
const char *authcid, const char *authcid,
const char *passwd, const char *passwd,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to generate a base64 encoded LOGIN cleartext message */ /* This is used to generate a LOGIN cleartext message */
CURLcode Curl_auth_create_login_message(struct Curl_easy *data, CURLcode Curl_auth_create_login_message(const char *value,
const char *valuep, char **outptr, struct bufref *out);
size_t *outlen);
/* This is used to generate a base64 encoded EXTERNAL cleartext message */ /* This is used to generate an EXTERNAL cleartext message */
CURLcode Curl_auth_create_external_message(struct Curl_easy *data, CURLcode Curl_auth_create_external_message(const char *user,
const char *user, char **outptr, struct bufref *out);
size_t *outlen);
#if !defined(CURL_DISABLE_CRYPTO_AUTH) #if !defined(CURL_DISABLE_CRYPTO_AUTH)
/* This is used to decode a CRAM-MD5 challenge message */
CURLcode Curl_auth_decode_cram_md5_message(const char *chlg64, char **outptr,
size_t *outlen);
/* This is used to generate a CRAM-MD5 response message */ /* This is used to generate a CRAM-MD5 response message */
CURLcode Curl_auth_create_cram_md5_message(struct Curl_easy *data, CURLcode Curl_auth_create_cram_md5_message(const struct bufref *chlg,
const char *chlg,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to evaluate if DIGEST is supported */ /* This is used to evaluate if DIGEST is supported */
bool Curl_auth_is_digest_supported(void); bool Curl_auth_is_digest_supported(void);
/* This is used to generate a base64 encoded DIGEST-MD5 response message */ /* This is used to generate a base64 encoded DIGEST-MD5 response message */
CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data, CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
const char *service, const char *service,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to decode a HTTP DIGEST challenge message */ /* This is used to decode a HTTP DIGEST challenge message */
CURLcode Curl_auth_decode_digest_http_message(const char *chlg, CURLcode Curl_auth_decode_digest_http_message(const char *chlg,
@ -132,9 +126,9 @@ CURLcode Curl_auth_gsasl_start(struct Curl_easy *data,
/* This is used to process and generate a new SASL token */ /* This is used to process and generate a new SASL token */
CURLcode Curl_auth_gsasl_token(struct Curl_easy *data, CURLcode Curl_auth_gsasl_token(struct Curl_easy *data,
const char *chlg64, const struct bufref *chlg,
struct gsasldata *gsasl, struct gsasldata *gsasl,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to clean up the gsasl specific data */ /* This is used to clean up the gsasl specific data */
void Curl_auth_gsasl_cleanup(struct gsasldata *digest); void Curl_auth_gsasl_cleanup(struct gsasldata *digest);
@ -151,12 +145,11 @@ CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
const char *service, const char *service,
const char *host, const char *host,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, struct bufref *out);
size_t *outlen);
/* This is used to decode a base64 encoded NTLM type-2 message */ /* This is used to decode a base64 encoded NTLM type-2 message */
CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data, CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
const char *type2msg, const struct bufref *type2,
struct ntlmdata *ntlm); struct ntlmdata *ntlm);
/* This is used to generate a base64 encoded NTLM type-3 message */ /* This is used to generate a base64 encoded NTLM type-3 message */
@ -164,25 +157,23 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
const char *userp, const char *userp,
const char *passwdp, const char *passwdp,
struct ntlmdata *ntlm, struct ntlmdata *ntlm,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to clean up the NTLM specific data */ /* This is used to clean up the NTLM specific data */
void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm); void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm);
#endif /* USE_NTLM */ #endif /* USE_NTLM */
/* This is used to generate a base64 encoded OAuth 2.0 message */ /* This is used to generate a base64 encoded OAuth 2.0 message */
CURLcode Curl_auth_create_oauth_bearer_message(struct Curl_easy *data, CURLcode Curl_auth_create_oauth_bearer_message(const char *user,
const char *user,
const char *host, const char *host,
const long port, const long port,
const char *bearer, const char *bearer,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to generate a base64 encoded XOAuth 2.0 message */ /* This is used to generate a base64 encoded XOAuth 2.0 message */
CURLcode Curl_auth_create_xoauth_bearer_message(struct Curl_easy *data, CURLcode Curl_auth_create_xoauth_bearer_message(const char *user,
const char *user,
const char *bearer, const char *bearer,
char **outptr, size_t *outlen); struct bufref *out);
#if defined(USE_KERBEROS5) #if defined(USE_KERBEROS5)
/* This is used to evaluate if GSSAPI (Kerberos V5) is supported */ /* This is used to evaluate if GSSAPI (Kerberos V5) is supported */
@ -196,17 +187,16 @@ CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
const char *service, const char *service,
const char *host, const char *host,
const bool mutual, const bool mutual,
const char *chlg64, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, size_t *outlen); struct bufref *out);
/* This is used to generate a base64 encoded GSSAPI (Kerberos V5) security /* This is used to generate a base64 encoded GSSAPI (Kerberos V5) security
token message */ token message */
CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data, CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
const char *input, const struct bufref *chlg,
struct kerberos5data *krb5, struct kerberos5data *krb5,
char **outptr, struct bufref *out);
size_t *outlen);
/* This is used to clean up the GSSAPI specific data */ /* This is used to clean up the GSSAPI specific data */
void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5); void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5);