sasl: Moved cram-md5 authentication message creation from smtp.c

Moved the cram-md5 message creation from smtp.c into the sasl module
to allow for use by other modules such as pop3.
This commit is contained in:
Steve Holme 2012-06-03 17:21:49 +01:00
parent b5bb61ee69
commit c12a414b21
3 changed files with 92 additions and 41 deletions

View File

@ -18,6 +18,7 @@
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied. * KIND, either express or implied.
* *
* RFC2195 CRAM-MD5 authentication
* RFC4616 PLAIN authentication * RFC4616 PLAIN authentication
* *
***************************************************************************/ ***************************************************************************/
@ -28,8 +29,14 @@
#include "urldata.h" #include "urldata.h"
#include "curl_base64.h" #include "curl_base64.h"
#include "curl_md5.h"
#include "curl_hmac.h"
#include "curl_ntlm_msgs.h" #include "curl_ntlm_msgs.h"
#include "curl_sasl.h" #include "curl_sasl.h"
#include "warnless.h"
#define _MPRINTF_REPLACE /* use our functions only */
#include <curl/mprintf.h>
/* The last #include file should be: */ /* The last #include file should be: */
#include "memdebug.h" #include "memdebug.h"
@ -115,6 +122,79 @@ CURLcode Curl_sasl_create_login_message(struct SessionHandle *data,
return Curl_base64_encode(data, valuep, vlen, outptr, outlen); return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
} }
#ifndef CURL_DISABLE_CRYPTO_AUTH
/*
* Curl_sasl_create_cram_md5_message()
*
* This is used to generate an already encoded CRAM-MD5 message ready for
* sending to the recipient.
*
* Parameters:
*
* data [in] - The session handle.
* chlg64 [in] - Pointer to the input buffer.
* userp [in] - The user name in the format User or Domain\User.
* passdwp [in] - The user's password.
* 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_sasl_create_cram_md5_message(struct SessionHandle *data,
const char* chlg64,
const char* user,
const char* passwdp,
char **outptr, size_t *outlen)
{
CURLcode result = CURLE_OK;
size_t chlg64len = strlen(chlg64);
size_t len = 0;
unsigned char *chlg = (unsigned char *) NULL;
size_t chlglen = 0;
HMAC_context *ctxt;
unsigned char digest[MD5_DIGEST_LEN];
char reply[MAX_CURL_USER_LENGTH + 2 * MD5_DIGEST_LEN + 1];
/* Decode the challenge if necessary */
if(chlg64len && *chlg64 != '=') {
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
}
/* Compute the digest using the password as the key */
ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
(const unsigned char *) passwdp,
curlx_uztoui(strlen(passwdp)));
if(!ctxt) {
Curl_safefree(chlg);
return CURLE_OUT_OF_MEMORY;
}
/* Update the digest with the given challenge */
if(chlglen > 0)
Curl_HMAC_update(ctxt, chlg, curlx_uztoui(chlglen));
Curl_safefree(chlg);
/* Finalise the digest */
Curl_HMAC_final(ctxt, digest);
/* Prepare the reply */
snprintf(reply, sizeof(reply),
"%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
user, digest[0], digest[1], digest[2], digest[3], digest[4],
digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
digest[11], digest[12], digest[13], digest[14], digest[15]);
/* Base64 encode the reply */
return Curl_base64_encode(data, reply, 0, outptr, outlen);
}
#endif
#ifdef USE_NTLM #ifdef USE_NTLM
/* /*
* Curl_sasl_create_ntlm_type1_message() * Curl_sasl_create_ntlm_type1_message()

View File

@ -45,6 +45,15 @@ CURLcode Curl_sasl_create_login_message(struct SessionHandle *data,
const char* valuep, char **outptr, const char* valuep, char **outptr,
size_t *outlen); size_t *outlen);
#ifndef CURL_DISABLE_CRYPTO_AUTH
/* This is used to generate a base64 encoded CRAM-MD5 message */
CURLcode Curl_sasl_create_cram_md5_message(struct SessionHandle *data,
const char* chlg64,
const char* user,
const char* passwdp,
char **outptr, size_t *outlen);
#endif
#ifdef USE_NTLM #ifdef USE_NTLM
/* This is used to generate a base64 encoded NTLM type-1 message */ /* This is used to generate a base64 encoded NTLM type-1 message */
CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp, CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,

View File

@ -85,7 +85,6 @@
#include "curl_base64.h" #include "curl_base64.h"
#include "curl_rand.h" #include "curl_rand.h"
#include "curl_md5.h" #include "curl_md5.h"
#include "curl_hmac.h"
#include "curl_gethostname.h" #include "curl_gethostname.h"
#include "curl_sasl.h" #include "curl_sasl.h"
#include "warnless.h" #include "warnless.h"
@ -710,7 +709,6 @@ static CURLcode smtp_state_authpasswd_resp(struct connectdata *conn,
} }
#ifndef CURL_DISABLE_CRYPTO_AUTH #ifndef CURL_DISABLE_CRYPTO_AUTH
/* for AUTH CRAM-MD5 responses */ /* for AUTH CRAM-MD5 responses */
static CURLcode smtp_state_authcram_resp(struct connectdata *conn, static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
int smtpcode, int smtpcode,
@ -719,13 +717,8 @@ static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
CURLcode result = CURLE_OK; CURLcode result = CURLE_OK;
struct SessionHandle *data = conn->data; struct SessionHandle *data = conn->data;
char *chlg64 = data->state.buffer; char *chlg64 = data->state.buffer;
unsigned char *chlg;
size_t chlglen;
size_t len = 0; size_t len = 0;
char *rplyb64 = NULL; char *rplyb64 = NULL;
HMAC_context *ctxt;
unsigned char digest[MD5_DIGEST_LEN];
char reply[MAX_CURL_USER_LENGTH + 2 * MD5_DIGEST_LEN + 1];
(void)instate; /* no use for this yet */ (void)instate; /* no use for this yet */
@ -738,9 +731,7 @@ static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
for(chlg64 += 4; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++) for(chlg64 += 4; *chlg64 == ' ' || *chlg64 == '\t'; chlg64++)
; ;
chlg = (unsigned char *) NULL; /* Terminate the challenge */
chlglen = 0;
if(*chlg64 != '=') { if(*chlg64 != '=') {
for(len = strlen(chlg64); len--;) for(len = strlen(chlg64); len--;)
if(chlg64[len] != '\r' && chlg64[len] != '\n' && chlg64[len] != ' ' && if(chlg64[len] != '\r' && chlg64[len] != '\n' && chlg64[len] != ' ' &&
@ -749,40 +740,11 @@ static CURLcode smtp_state_authcram_resp(struct connectdata *conn,
if(++len) { if(++len) {
chlg64[len] = '\0'; chlg64[len] = '\0';
result = Curl_base64_decode(chlg64, &chlg, &chlglen);
if(result)
return result;
} }
} }
/* Compute digest */ result = Curl_sasl_create_cram_md5_message(data, chlg64, conn->user,
ctxt = Curl_HMAC_init(Curl_HMAC_MD5, conn->passwd, &rplyb64, &len);
(const unsigned char *) conn->passwd,
curlx_uztoui(strlen(conn->passwd)));
if(!ctxt) {
Curl_safefree(chlg);
return CURLE_OUT_OF_MEMORY;
}
if(chlglen > 0)
Curl_HMAC_update(ctxt, chlg, curlx_uztoui(chlglen));
Curl_safefree(chlg);
Curl_HMAC_final(ctxt, digest);
/* Prepare the reply */
snprintf(reply, sizeof(reply),
"%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
conn->user, digest[0], digest[1], digest[2], digest[3], digest[4],
digest[5],
digest[6], digest[7], digest[8], digest[9], digest[10], digest[11],
digest[12], digest[13], digest[14], digest[15]);
/* Encode it to base64 and send it */
result = Curl_base64_encode(data, reply, 0, &rplyb64, &len);
if(!result) { if(!result) {
if(rplyb64) { if(rplyb64) {