Changeset 5f40da7 for lib


Ignore:
Timestamp:
2011-12-26T10:51:19Z (12 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
199fea6
Parents:
96f954d (diff), 644b808 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merging oauth-xmpp branch, which adds support for OAuth2 authentication
against some XMPP services (Google Talk, Facebook and Microsoft's MSN-XMPP
gateway).

Location:
lib
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • lib/Makefile

    r96f954d r5f40da7  
    1313
    1414# [SH] Program variables
    15 objects = arc.o base64.o $(DES) $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
     15objects = arc.o base64.o $(DES) $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
    1616
    1717LFLAGS += -r
  • lib/arc.c

    r96f954d r5f40da7  
    200200        {
    201201                *clear = g_strdup( "" );
    202                 return 0;
     202                return -1;
    203203        }
    204204       
  • lib/md5.c

    r96f954d r5f40da7  
    2424#include <sys/types.h>
    2525#include <string.h>             /* for memcpy() */
     26#include <stdio.h>
    2627#include "md5.h"
    2728
     
    160161        memcpy(digest, ctx->buf, 16);
    161162        memset(ctx, 0, sizeof(ctx));    /* In case it's sensitive */
     163}
     164
     165void md5_finish_ascii(struct MD5Context *context, char *ascii)
     166{
     167        md5_byte_t bin[16];
     168        int i;
     169       
     170        md5_finish(context, bin);
     171        for (i = 0; i < 16; i ++)
     172                sprintf(ascii + i * 2, "%02x", bin[i]);
    162173}
    163174
  • lib/md5.h

    r96f954d r5f40da7  
    4343G_MODULE_EXPORT void md5_append(struct MD5Context *context, const md5_byte_t *buf, unsigned int len);
    4444G_MODULE_EXPORT void md5_finish(struct MD5Context *context, md5_byte_t digest[16]);
     45G_MODULE_EXPORT void md5_finish_ascii(struct MD5Context *context, char *ascii);
    4546
    4647#endif
  • lib/misc.c

    r96f954d r5f40da7  
    729729        return cmd;
    730730}
     731
     732char *get_rfc822_header( char *text, char *header, int len )
     733{
     734        int hlen = strlen( header ), i;
     735        char *ret;
     736       
     737        if( text == NULL )
     738                return NULL;
     739       
     740        if( len == 0 )
     741                len = strlen( text );
     742       
     743        i = 0;
     744        while( ( i + hlen ) < len )
     745        {
     746                /* Maybe this is a bit over-commented, but I just hate this part... */
     747                if( g_strncasecmp( text + i, header, hlen ) == 0 )
     748                {
     749                        /* Skip to the (probable) end of the header */
     750                        i += hlen;
     751                       
     752                        /* Find the first non-[: \t] character */
     753                        while( i < len && ( text[i] == ':' || text[i] == ' ' || text[i] == '\t' ) ) i ++;
     754                       
     755                        /* Make sure we're still inside the string */
     756                        if( i >= len ) return( NULL );
     757                       
     758                        /* Save the position */
     759                        ret = text + i;
     760                       
     761                        /* Search for the end of this line */
     762                        while( i < len && text[i] != '\r' && text[i] != '\n' ) i ++;
     763                       
     764                        /* Make sure we're still inside the string */
     765                        if( i >= len ) return( NULL );
     766                       
     767                        /* Copy the found data */
     768                        return( g_strndup( ret, text + i - ret ) );
     769                }
     770               
     771                /* This wasn't the header we were looking for, skip to the next line. */
     772                while( i < len && ( text[i] != '\r' && text[i] != '\n' ) ) i ++;
     773                while( i < len && ( text[i] == '\r' || text[i] == '\n' ) ) i ++;
     774               
     775                /* End of headers? */
     776                if( ( i >= 4 && strncmp( text + i - 4, "\r\n\r\n", 4 ) == 0 ) ||
     777                    ( i >= 2 && ( strncmp( text + i - 2, "\n\n", 2 ) == 0 ||   
     778                                  strncmp( text + i - 2, "\r\r", 2 ) == 0 ) ) )
     779                {
     780                        break;
     781                }
     782        }
     783       
     784        return NULL;
     785}
  • lib/misc.h

    r96f954d r5f40da7  
    6565
    6666G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len );
    67 
    6867G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl );
    69 
    7068G_MODULE_EXPORT int md5_verify_password( char *password, char *hash );
    71 
    7269G_MODULE_EXPORT char **split_command_parts( char *command );
     70G_MODULE_EXPORT char *get_rfc822_header( char *text, char *header, int len );
    7371
    7472#endif
  • lib/oauth.c

    r96f954d r5f40da7  
    3838                         const char *params, struct oauth_info *oi )
    3939{
    40         sha1_state_t sha1;
    4140        uint8_t hash[sha1_hash_size];
    42         uint8_t key[HMAC_BLOCK_SIZE+1];
     41        GString *payload = g_string_new( "" );
     42        char *key;
    4343        char *s;
    44         int i;
    45        
    46         /* Create K. If our current key is >64 chars we have to hash it,
    47            otherwise just pad. */
    48         memset( key, 0, HMAC_BLOCK_SIZE );
    49         i = strlen( oi->sp->consumer_secret ) + 1 + ( oi->token_secret ? strlen( oi->token_secret ) : 0 );
    50         if( i > HMAC_BLOCK_SIZE )
    51         {
    52                 sha1_init( &sha1 );
    53                 sha1_append( &sha1, (uint8_t*) oi->sp->consumer_secret, strlen( oi->sp->consumer_secret ) );
    54                 sha1_append( &sha1, (uint8_t*) "&", 1 );
    55                 if( oi->token_secret )
    56                         sha1_append( &sha1, (uint8_t*) oi->token_secret, strlen( oi->token_secret ) );
    57                 sha1_finish( &sha1, key );
    58         }
    59         else
    60         {
    61                 g_snprintf( (gchar*) key, HMAC_BLOCK_SIZE + 1, "%s&%s",
    62                             oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "" );
    63         }
    64        
    65         /* Inner part: H(K XOR 0x36, text) */
    66         sha1_init( &sha1 );
    67        
    68         for( i = 0; i < HMAC_BLOCK_SIZE; i ++ )
    69                 key[i] ^= 0x36;
    70         sha1_append( &sha1, key, HMAC_BLOCK_SIZE );
    71        
    72         /* OAuth: text = method&url&params, all http_encoded. */
    73         sha1_append( &sha1, (const uint8_t*) method, strlen( method ) );
    74         sha1_append( &sha1, (const uint8_t*) "&", 1 );
     44       
     45        key = g_strdup_printf( "%s&%s", oi->sp->consumer_secret, oi->token_secret ? oi->token_secret : "" );
     46       
     47        g_string_append_printf( payload, "%s&", method );
    7548       
    7649        s = g_new0( char, strlen( url ) * 3 + 1 );
    7750        strcpy( s, url );
    7851        http_encode( s );
    79         sha1_append( &sha1, (const uint8_t*) s, strlen( s ) );
    80         sha1_append( &sha1, (const uint8_t*) "&", 1 );
     52        g_string_append_printf( payload, "%s&", s );
    8153        g_free( s );
    8254       
     
    8456        strcpy( s, params );
    8557        http_encode( s );
    86         sha1_append( &sha1, (const uint8_t*) s, strlen( s ) );
    87         g_free( s );
    88        
    89         sha1_finish( &sha1, hash );
    90        
    91         /* Final result: H(K XOR 0x5C, inner stuff) */
    92         sha1_init( &sha1 );
    93         for( i = 0; i < HMAC_BLOCK_SIZE; i ++ )
    94                 key[i] ^= 0x36 ^ 0x5c;
    95         sha1_append( &sha1, key, HMAC_BLOCK_SIZE );
    96         sha1_append( &sha1, hash, sha1_hash_size );
    97         sha1_finish( &sha1, hash );
     58        g_string_append( payload, s );
     59        g_free( s );
     60       
     61        sha1_hmac( key, 0, payload->str, 0, hash );
     62       
     63        g_free( key );
     64        g_string_free( payload, TRUE );
    9865       
    9966        /* base64_encode + HTTP escape it (both consumers
     
    12289        char *item;
    12390       
     91        if( !key || !value )
     92                return;
     93       
    12494        item = g_strdup_printf( "%s=%s", key, value );
    12595        *params = g_slist_insert_sorted( *params, item, (GCompareFunc) strcmp );
     
    130100        int key_len = strlen( key );
    131101        GSList *l, *n;
     102       
     103        if( params == NULL )
     104                return;
    132105       
    133106        for( l = *params; l; l = n )
     
    155128        GSList *l;
    156129       
     130        if( params == NULL )
     131                return NULL;
     132       
    157133        for( l = *params; l; l = l->next )
    158134        {
     
    165141}
    166142
    167 static void oauth_params_parse( GSList **params, char *in )
     143void oauth_params_parse( GSList **params, char *in )
    168144{
    169145        char *amp, *eq, *s;
     
    333309                oauth_params_parse( &params, req->reply_body );
    334310                st->request_token = g_strdup( oauth_params_get( &params, "oauth_token" ) );
     311                st->token_secret = g_strdup( oauth_params_get( &params, "oauth_token_secret" ) );
    335312                oauth_params_free( &params );
    336313        }
     
    362339                oauth_params_parse( &st->params, req->reply_body );
    363340                st->token = g_strdup( oauth_params_get( &st->params, "oauth_token" ) );
     341                g_free( st->token_secret );
    364342                st->token_secret = g_strdup( oauth_params_get( &st->params, "oauth_token_secret" ) );
    365343        }
  • lib/oauth.h

    r96f954d r5f40da7  
    9292
    9393/* For reading misc. data. */
     94void oauth_params_add( GSList **params, const char *key, const char *value );
     95void oauth_params_parse( GSList **params, char *in );
     96void oauth_params_free( GSList **params );
     97char *oauth_params_string( GSList *params );
     98void oauth_params_set( GSList **params, const char *key, const char *value );
    9499const char *oauth_params_get( GSList **params, const char *key );
Note: See TracChangeset for help on using the changeset viewer.