Changeset b38d399


Ignore:
Timestamp:
2014-11-24T05:16:09Z (10 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
9f8bb17
Parents:
e2472dd
Message:

Use glib functions for base64 decoding/encoding

This fixes several coverity warnings about 'tainted data index sink' and
a fixme about thread safety in the old base64_decode implementation.

Had to adapt the code that used base64_encode_real:

  • oauth.c: different character set order, but it's for the nonce so it doesn't matter
  • libyahoo2.c: used as part of the auth, changes "+/=" into "._-". Fixed by encoding first the usual way through glib, then replacing.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • lib/base64.c

    re2472dd rb38d399  
    2727#include "base64.h"
    2828
    29 static const char real_b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    30 
    3129char *tobase64(const char *text)
    3230{
     
    3634char *base64_encode(const unsigned char *in, int len)
    3735{
    38         char *out;
    39        
    40         out = g_malloc((len + 2)    /* the == padding */
    41                             / 3     /* every 3-byte block */
    42                             * 4     /* becomes a 4-byte one */
    43                             + 1);   /* and of course, ASCIIZ! */
    44        
    45         base64_encode_real((unsigned char*) in, len, (unsigned char*) out, real_b64);
    46        
    47         return out;
    48 }
    49 
    50 int base64_encode_real(const unsigned char *in, int inlen, unsigned char *out, const char *b64digits)
    51 {
    52         int outlen = 0;
    53        
    54         for (; inlen >= 3; inlen -= 3)
    55         {
    56                 out[outlen++] = b64digits[in[0] >> 2];
    57                 out[outlen++] = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)];
    58                 out[outlen++] = b64digits[((in[1]<<2) & 0x3c) | (in[2]>>6)];
    59                 out[outlen++] = b64digits[in[2] & 0x3f];
    60                 in += 3;
    61         }
    62         if (inlen > 0)
    63         {
    64                 out[outlen++] = b64digits[in[0] >> 2];
    65                 if (inlen > 1)
    66                 {
    67                         out[outlen++] = b64digits[((in[0]<<4) & 0x30) | (in[1]>>4)];
    68                         out[outlen++] = b64digits[((in[1]<<2) & 0x3c)];
    69                 }
    70                 else
    71                 {
    72                         out[outlen++] = b64digits[((in[0]<<4) & 0x30)];
    73                         out[outlen++] = b64digits[64];
    74                 }
    75                 out[outlen++] = b64digits[64];
    76         }
    77         out[outlen] = 0;
    78        
    79         return outlen;
     36        return g_base64_encode(in, len);
    8037}
    8138
     
    8542{
    8643        unsigned char *out;
    87        
    8844        base64_decode(in, &out);
    89        
    9045        return (char*) out;
    9146}
    9247
    93 /* FIXME: Lookup table stuff is not threadsafe! (But for now BitlBee is not threaded.) */
    9448int base64_decode(const char *in, unsigned char **out)
    9549{
    96         static char b64rev[256] = { 0 };
    97         int len, i;
    98        
    99         /* Create a reverse-lookup for the Base64 sequence. */
    100         if( b64rev[0] == 0 )
    101         {
    102                 memset( b64rev, 0xff, 256 );
    103                 for( i = 0; i <= 64; i ++ )
    104                         b64rev[(int)real_b64[i]] = i;
    105         }
    106        
    107         len = strlen( in );
    108         *out = g_malloc( ( len + 6 ) / 4 * 3 );
    109         len = base64_decode_real( (unsigned char*) in, *out, b64rev );
    110         *out = g_realloc( *out, len + 1 );
    111         out[0][len] = 0;        /* Zero termination can't hurt. */
    112        
     50        gsize len;
     51        *out = g_base64_decode(in, &len);
     52
     53        /* Some silly functions expect it to be zero terminated */
     54        *out = g_realloc(*out, len + 1);
     55        out[0][len] = 0;
     56
    11357        return len;
    11458}
    115 
    116 int base64_decode_real(const unsigned char *in, unsigned char *out, char *b64rev)
    117 {
    118         int i, outlen = 0;
    119        
    120         for( i = 0; in[i] && in[i+1] && in[i+2] && in[i+3]; i += 4 )
    121         {
    122                 int sx;
    123                
    124                 sx = b64rev[(int)in[i+0]];
    125                 if( sx >= 64 )
    126                         break;
    127                 out[outlen] = ( sx << 2 ) & 0xfc;
    128                
    129                 sx = b64rev[(int)in[i+1]];
    130                 if( sx >= 64 )
    131                         break;
    132                 out[outlen] |= ( sx >> 4 ) & 0x03;
    133                 outlen ++;
    134                 out[outlen] = ( sx << 4 ) & 0xf0;
    135                
    136                 sx = b64rev[(int)in[i+2]];
    137                 if( sx >= 64 )
    138                         break;
    139                 out[outlen] |= ( sx >> 2 ) & 0x0f;
    140                 outlen ++;
    141                 out[outlen] = ( sx << 6 ) & 0xc0;
    142                
    143                 sx = b64rev[(int)in[i+3]];
    144                 if( sx >= 64 )
    145                         break;
    146                 out[outlen] |= sx;
    147                 outlen ++;
    148         }
    149        
    150         /* If sx > 64 the base64 string was damaged. Should we ignore this? */
    151        
    152         return outlen;
    153 }
  • lib/base64.h

    re2472dd rb38d399  
    2828G_MODULE_EXPORT char *tobase64( const char *text );
    2929G_MODULE_EXPORT char *base64_encode( const unsigned char *in, int len );
    30 G_MODULE_EXPORT int base64_encode_real( const unsigned char *in, int inlen, unsigned char *out, const char *b64digits );
    3130G_MODULE_EXPORT char *frombase64( const char *in );
    3231G_MODULE_EXPORT int base64_decode( const char *in, unsigned char **out );
    33 G_MODULE_EXPORT int base64_decode_real( const unsigned char *in, unsigned char *out, char *b64reverse );
  • lib/oauth.c

    re2472dd rb38d399  
    7676{
    7777        unsigned char bytes[21];
    78         char *ret = g_new0( char, sizeof( bytes) / 3 * 4 + 1 );
    79        
    8078        random_bytes( bytes, sizeof( bytes ) );
    81         base64_encode_real( bytes, sizeof( bytes), (unsigned char*) ret, "0123456789"
    82                             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0A" );
    83        
    84         return ret;
     79        return base64_encode( bytes, sizeof( bytes ) );
    8580}
    8681
  • protocols/yahoo/libyahoo2.c

    re2472dd rb38d399  
    681681}
    682682
    683 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
     683/* yahoo's variant of base64 */
    684684static void to_y64(unsigned char *out, const unsigned char *in, int inlen)
    685685{
    686         base64_encode_real(in, inlen, out, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-");
     686        char *encoded = base64_encode(in, inlen);
     687        int i = 0;
     688
     689        do {
     690                if (encoded[i] == '+') {
     691                        out[i] = '.';
     692                } else if (encoded[i] == '/') {
     693                        out[i] = '_';
     694                } else if (encoded[i] == '=') {
     695                        out[i] = '-';
     696                } else {
     697                        out[i] = encoded[i];
     698                }
     699        } while (encoded[i++]);
     700
     701        g_free(encoded);
    687702}
    688703
Note: See TracChangeset for help on using the changeset viewer.