[Home]

Summary:ASTERISK-08947: Wrong behaviour in Asterisk Base64 conversion routines
Reporter:Lorenzo Miniero (lminiero)Labels:
Date Opened:2007-03-06 10:30:55.000-0600Date Closed:2007-03-06 17:58:37.000-0600
Priority:MinorRegression?No
Status:Closed/CompleteComponents:Core/Configuration
Versions:Frequency of
Occurrence
Related
Issues:
Environment:Attachments:
Description:While playing with the Base64 conversion routines Asterisk provides in main/utils.c (SVN rev. 51493 of the file), I noticed a wrong behaviour in a specific case, i.e. encoding/decoding 16 bytes of data.

****** ADDITIONAL INFORMATION ******

While playing with the Base64 conversion routines Asterisk provides in main/utils.c (SVN rev. 51486 of the file), I noticed a wrong behaviour in a specific case. When a buffer of 16 bytes of data is encoded, a string of 20 bytes is given as a result; when decoding this string back to the original data, the function only returns 15 bytes, hence corrupting the data.
To reproduce this bug, check the following code:

char buffer[16], buffer_decoded[16];
sprintf(buffer, "0123456789012345");
ast_log(LOG_NOTICE, "Buffer: %s\n", buffer);
char *base64 = calloc(24, sizeof(char));
int err = ast_base64encode(base64, (const unsigned char *)buffer, 16, 24);
ast_log(LOG_NOTICE, "\tEncoded in Base64: %s (%d bytes)\n", base64, err);
err = ast_base64decode((unsigned char *)buffer_decoded, base64, 16);
ast_log(LOG_NOTICE, "\tDecoded back from Base64: %s (%d bytes)\n", buffer_decoded, err);

I used a string as data buffer to encode only as an example: the problem is the same with the data I manipulate in my custom application module.
I tested the previous lines of code by adding them to app_skel.c, and this was the output (headers cut):

Buffer: 0123456789012345
      Encoded in Base64: MDEyMzQ1Njc4OTAxMjM0 (20 bytes)
      Decoded back from Base64: 012345678901234 (15 bytes)

which clearly reproduces the problem: only 15 bytes are recovered after the decoding. I think the problem is in the encoding phase, since the Base64 encoding is supposed to increment the buffer size of 33%, which in the case of 16 bytes should be 21/22 bytes and not 20 as happens instead.
Comments:By: Serge Vecher (serge-v) 2007-03-06 10:34:28.000-0600

switching tags, as 1.4 branch is affected too and fix needs to be done there first.

By: Lorenzo Miniero (lminiero) 2007-03-06 10:45:21.000-0600

I found out the problem...
The routines are fine, the problem was in the "max" parameter: 24 is exactly the size the encoded buffer will have, but this value is decremented in the routine, and so the last 4 bytes are cut from the encoded buffer.
Passing a greater size, e.g.:

   char *base64 = calloc(25, sizeof(char));
   int err = ast_base64encode(base64, (const unsigned char *)buffer, 16, 25);

works fine.

By: Russell Bryant (russell) 2007-03-06 17:58:37.000-0600

I'm glad you got it figured out.  I went ahead and added some basic documentation on the encode/decode functions.  I'm sorry it wasn't there before ...