Curl_base64_decode() now returns an allocated buffer

This commit is contained in:
Daniel Stenberg 2005-02-22 12:10:30 +00:00
parent 19f66c7575
commit 527f70e540
6 changed files with 45 additions and 23 deletions

View File

@ -76,10 +76,10 @@ static void decodeQuantum(unsigned char *dest, const char *src)
/* /*
* Curl_base64_decode() * Curl_base64_decode()
* *
* Given a base64 string at src, decode it into the memory pointed to by * Given a base64 string at src, decode it and return an allocated memory in
* dest. Returns the length of the decoded data. * the *outptr. Returns the length of the decoded data.
*/ */
size_t Curl_base64_decode(const char *src, char *dest) size_t Curl_base64_decode(const char *src, unsigned char **outptr)
{ {
int length = 0; int length = 0;
int equalsTerm = 0; int equalsTerm = 0;
@ -87,6 +87,9 @@ size_t Curl_base64_decode(const char *src, char *dest)
int numQuantums; int numQuantums;
unsigned char lastQuantum[3]; unsigned char lastQuantum[3];
size_t rawlen=0; size_t rawlen=0;
unsigned char *newstr;
*outptr = NULL;
while((src[length] != '=') && src[length]) while((src[length] != '=') && src[length])
length++; length++;
@ -97,15 +100,22 @@ size_t Curl_base64_decode(const char *src, char *dest)
rawlen = (numQuantums * 3) - equalsTerm; rawlen = (numQuantums * 3) - equalsTerm;
newstr = malloc(rawlen+1);
if(!newstr)
return 0;
*outptr = newstr;
for(i = 0; i < numQuantums - 1; i++) { for(i = 0; i < numQuantums - 1; i++) {
decodeQuantum((unsigned char *)dest, src); decodeQuantum((unsigned char *)newstr, src);
dest += 3; src += 4; newstr += 3; src += 4;
} }
decodeQuantum(lastQuantum, src); decodeQuantum(lastQuantum, src);
for(i = 0; i < 3 - equalsTerm; i++) for(i = 0; i < 3 - equalsTerm; i++)
dest[i] = lastQuantum[i]; newstr[i] = lastQuantum[i];
newstr[i] = 0; /* zero terminate */
return rawlen; return rawlen;
} }

View File

@ -23,5 +23,5 @@
* $Id$ * $Id$
***************************************************************************/ ***************************************************************************/
size_t Curl_base64_encode(const char *input, size_t size, char **str); size_t Curl_base64_encode(const char *input, size_t size, char **str);
size_t Curl_base64_decode(const char *source, char *dest); size_t Curl_base64_decode(const char *source, unsigned char **outptr);
#endif #endif

View File

@ -166,12 +166,7 @@ int Curl_input_negotiate(struct connectdata *conn, char *header)
len = strlen(header); len = strlen(header);
if (len > 0) { if (len > 0) {
int rawlen; int rawlen = Curl_base64_decode(header, &input_token.value);
input_token.length = (len+3)/4 * 3;
input_token.value = malloc(input_token.length);
if (input_token.value == NULL)
return ENOMEM;
rawlen = Curl_base64_decode(header, input_token.value);
if (rawlen < 0) if (rawlen < 0)
return -1; return -1;
input_token.length = rawlen; input_token.length = rawlen;

View File

@ -123,17 +123,17 @@ CURLntlm Curl_input_ntlm(struct connectdata *conn,
32 (48) start of data block 32 (48) start of data block
*/ */
size_t size; size_t size;
unsigned char *buffer = (unsigned char *)malloc(strlen(header)); unsigned char *buffer;
if (buffer == NULL) size = Curl_base64_decode(header, &buffer);
if(!buffer)
return CURLNTLM_BAD; return CURLNTLM_BAD;
size = Curl_base64_decode(header, (char *)buffer);
ntlm->state = NTLMSTATE_TYPE2; /* we got a type-2 */ ntlm->state = NTLMSTATE_TYPE2; /* we got a type-2 */
if(size >= 48) if(size >= 48)
/* the nonce of interest is index [24 .. 31], 8 bytes */ /* the nonce of interest is index [24 .. 31], 8 bytes */
memcpy(ntlm->nonce, &buffer[24], 8); memcpy(ntlm->nonce, &buffer[24], 8);
/* FIX: add an else here! */
/* at index decimal 20, there's a 32bit NTLM flag field */ /* at index decimal 20, there's a 32bit NTLM flag field */

View File

@ -199,6 +199,7 @@ krb4_auth(void *app_data, struct connectdata *conn)
{ {
int ret; int ret;
char *p; char *p;
unsigned char *ptr;
int len; int len;
KTEXT_ST adat; KTEXT_ST adat;
MSG_DAT msg_data; MSG_DAT msg_data;
@ -275,11 +276,17 @@ krb4_auth(void *app_data, struct connectdata *conn)
return AUTH_ERROR; return AUTH_ERROR;
} }
p += 5; p += 5;
len = Curl_base64_decode(p, (char *)adat.dat); len = Curl_base64_decode(p, &ptr);
if(len < 0) { if(len > sizeof(adat.dat)-1) {
free(ptr);
len=0;
}
if(!len || !ptr) {
Curl_failf(data, "Failed to decode base64 from server"); Curl_failf(data, "Failed to decode base64 from server");
return AUTH_ERROR; return AUTH_ERROR;
} }
memcpy((char *)adat.dat, ptr, len);
free(ptr);
adat.length = len; adat.length = len;
ret = krb_rd_safe(adat.dat, adat.length, &d->key, ret = krb_rd_safe(adat.dat, adat.length, &d->key,
(struct sockaddr_in *)hisctladdr, (struct sockaddr_in *)hisctladdr,
@ -321,6 +328,7 @@ CURLcode Curl_krb_kauth(struct connectdata *conn)
ssize_t nread; ssize_t nread;
int save; int save;
CURLcode result; CURLcode result;
unsigned char *ptr;
save = Curl_set_command_prot(conn, prot_private); save = Curl_set_command_prot(conn, prot_private);
@ -346,12 +354,18 @@ CURLcode Curl_krb_kauth(struct connectdata *conn)
} }
p += 2; p += 2;
tmp = Curl_base64_decode(p, (char *)tkt.dat); tmp = Curl_base64_decode(p, &ptr);
if(tmp < 0) { if(len > sizeof(tkt.dat)-1) {
free(ptr);
len=0;
}
if(!len || !ptr) {
Curl_failf(conn->data, "Failed to decode base64 in reply.\n"); Curl_failf(conn->data, "Failed to decode base64 in reply.\n");
Curl_set_command_prot(conn, save); Curl_set_command_prot(conn, save);
return CURLE_FTP_WEIRD_SERVER_REPLY; return CURLE_FTP_WEIRD_SERVER_REPLY;
} }
memcpy((char *)tkt.dat, ptr, tmp);
free(ptr);
tkt.length = tmp; tkt.length = tmp;
tktcopy.length = tkt.length; tktcopy.length = tkt.length;

View File

@ -61,11 +61,11 @@ char *appendstring(char *string, /* original string */
{ {
size_t len = strlen(buffer); size_t len = strlen(buffer);
size_t needed_len = len + *stringlen + 1; size_t needed_len = len + *stringlen + 1;
char buf64[256]; /* big enough? */ unsigned char *buf64=NULL;
if(base64) { if(base64) {
/* decode the given buffer first */ /* decode the given buffer first */
len = Curl_base64_decode(buffer, buf64); /* updated len */ len = Curl_base64_decode(buffer, &buf64); /* updated len */
buffer = buf64; buffer = buf64;
needed_len = len + *stringlen + 1; /* recalculate */ needed_len = len + *stringlen + 1; /* recalculate */
} }
@ -87,6 +87,9 @@ char *appendstring(char *string, /* original string */
*stringlen += len; *stringlen += len;
string[*stringlen]=0; string[*stringlen]=0;
if(buf64)
free(buf64);
return string; return string;
} }