1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Simple OAuth client (consumer) implementation. * |
---|
5 | * * |
---|
6 | * Copyright 2010 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This library is free software; you can redistribute it and/or * |
---|
9 | * modify it under the terms of the GNU Lesser General Public * |
---|
10 | * License as published by the Free Software Foundation, version * |
---|
11 | * 2.1. * |
---|
12 | * * |
---|
13 | * This library is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
16 | * Lesser General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
21 | * * |
---|
22 | \***************************************************************************/ |
---|
23 | |
---|
24 | #include <glib.h> |
---|
25 | #include <gmodule.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <string.h> |
---|
28 | #include "http_client.h" |
---|
29 | #include "base64.h" |
---|
30 | #include "misc.h" |
---|
31 | #include "sha1.h" |
---|
32 | #include "url.h" |
---|
33 | #include "oauth.h" |
---|
34 | |
---|
35 | #define HMAC_BLOCK_SIZE 64 |
---|
36 | |
---|
37 | static char *oauth_sign( const char *method, const char *url, |
---|
38 | const char *params, struct oauth_info *oi ) |
---|
39 | { |
---|
40 | sha1_state_t sha1; |
---|
41 | uint8_t hash[sha1_hash_size]; |
---|
42 | uint8_t key[HMAC_BLOCK_SIZE+1]; |
---|
43 | 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¶ms, all http_encoded. */ |
---|
73 | sha1_append( &sha1, (const uint8_t*) method, strlen( method ) ); |
---|
74 | sha1_append( &sha1, (const uint8_t*) "&", 1 ); |
---|
75 | |
---|
76 | s = g_new0( char, strlen( url ) * 3 + 1 ); |
---|
77 | strcpy( s, url ); |
---|
78 | http_encode( s ); |
---|
79 | sha1_append( &sha1, (const uint8_t*) s, strlen( s ) ); |
---|
80 | sha1_append( &sha1, (const uint8_t*) "&", 1 ); |
---|
81 | g_free( s ); |
---|
82 | |
---|
83 | s = g_new0( char, strlen( params ) * 3 + 1 ); |
---|
84 | strcpy( s, params ); |
---|
85 | 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 ); |
---|
98 | |
---|
99 | /* base64_encode + HTTP escape it (both consumers |
---|
100 | need it that away) and we're done. */ |
---|
101 | s = base64_encode( hash, sha1_hash_size ); |
---|
102 | s = g_realloc( s, strlen( s ) * 3 + 1 ); |
---|
103 | http_encode( s ); |
---|
104 | |
---|
105 | return s; |
---|
106 | } |
---|
107 | |
---|
108 | static char *oauth_nonce() |
---|
109 | { |
---|
110 | unsigned char bytes[21]; |
---|
111 | char *ret = g_new0( char, sizeof( bytes) / 3 * 4 + 1 ); |
---|
112 | |
---|
113 | random_bytes( bytes, sizeof( bytes ) ); |
---|
114 | base64_encode_real( bytes, sizeof( bytes), (unsigned char*) ret, "0123456789" |
---|
115 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0A" ); |
---|
116 | |
---|
117 | return ret; |
---|
118 | } |
---|
119 | |
---|
120 | void oauth_params_add( GSList **params, const char *key, const char *value ) |
---|
121 | { |
---|
122 | char *item; |
---|
123 | |
---|
124 | if( !key || !value ) |
---|
125 | return; |
---|
126 | |
---|
127 | item = g_strdup_printf( "%s=%s", key, value ); |
---|
128 | *params = g_slist_insert_sorted( *params, item, (GCompareFunc) strcmp ); |
---|
129 | } |
---|
130 | |
---|
131 | void oauth_params_del( GSList **params, const char *key ) |
---|
132 | { |
---|
133 | int key_len = strlen( key ); |
---|
134 | GSList *l, *n; |
---|
135 | |
---|
136 | for( l = *params; l; l = n ) |
---|
137 | { |
---|
138 | n = l->next; |
---|
139 | |
---|
140 | if( strncmp( (char*) l->data, key, key_len ) == 0 && |
---|
141 | ((char*)l->data)[key_len] == '=' ) |
---|
142 | { |
---|
143 | g_free( l->data ); |
---|
144 | *params = g_slist_remove( *params, l->data ); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | void oauth_params_set( GSList **params, const char *key, const char *value ) |
---|
150 | { |
---|
151 | oauth_params_del( params, key ); |
---|
152 | oauth_params_add( params, key, value ); |
---|
153 | } |
---|
154 | |
---|
155 | const char *oauth_params_get( GSList **params, const char *key ) |
---|
156 | { |
---|
157 | int key_len = strlen( key ); |
---|
158 | GSList *l; |
---|
159 | |
---|
160 | for( l = *params; l; l = l->next ) |
---|
161 | { |
---|
162 | if( strncmp( (char*) l->data, key, key_len ) == 0 && |
---|
163 | ((char*)l->data)[key_len] == '=' ) |
---|
164 | return (const char*) l->data + key_len + 1; |
---|
165 | } |
---|
166 | |
---|
167 | return NULL; |
---|
168 | } |
---|
169 | |
---|
170 | void oauth_params_parse( GSList **params, char *in ) |
---|
171 | { |
---|
172 | char *amp, *eq, *s; |
---|
173 | |
---|
174 | while( in && *in ) |
---|
175 | { |
---|
176 | eq = strchr( in, '=' ); |
---|
177 | if( !eq ) |
---|
178 | break; |
---|
179 | |
---|
180 | *eq = '\0'; |
---|
181 | if( ( amp = strchr( eq + 1, '&' ) ) ) |
---|
182 | *amp = '\0'; |
---|
183 | |
---|
184 | s = g_strdup( eq + 1 ); |
---|
185 | http_decode( s ); |
---|
186 | oauth_params_add( params, in, s ); |
---|
187 | g_free( s ); |
---|
188 | |
---|
189 | *eq = '='; |
---|
190 | if( amp == NULL ) |
---|
191 | break; |
---|
192 | |
---|
193 | *amp = '&'; |
---|
194 | in = amp + 1; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | void oauth_params_free( GSList **params ) |
---|
199 | { |
---|
200 | while( params && *params ) |
---|
201 | { |
---|
202 | g_free( (*params)->data ); |
---|
203 | *params = g_slist_remove( *params, (*params)->data ); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | char *oauth_params_string( GSList *params ) |
---|
208 | { |
---|
209 | GSList *l; |
---|
210 | GString *str = g_string_new( "" ); |
---|
211 | |
---|
212 | for( l = params; l; l = l->next ) |
---|
213 | { |
---|
214 | char *s, *eq; |
---|
215 | |
---|
216 | s = g_malloc( strlen( l->data ) * 3 + 1 ); |
---|
217 | strcpy( s, l->data ); |
---|
218 | if( ( eq = strchr( s, '=' ) ) ) |
---|
219 | http_encode( eq + 1 ); |
---|
220 | g_string_append( str, s ); |
---|
221 | g_free( s ); |
---|
222 | |
---|
223 | if( l->next ) |
---|
224 | g_string_append_c( str, '&' ); |
---|
225 | } |
---|
226 | |
---|
227 | return g_string_free( str, FALSE ); |
---|
228 | } |
---|
229 | |
---|
230 | void oauth_info_free( struct oauth_info *info ) |
---|
231 | { |
---|
232 | if( info ) |
---|
233 | { |
---|
234 | g_free( info->auth_url ); |
---|
235 | g_free( info->request_token ); |
---|
236 | g_free( info->token ); |
---|
237 | g_free( info->token_secret ); |
---|
238 | oauth_params_free( &info->params ); |
---|
239 | g_free( info ); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | static void oauth_add_default_params( GSList **params, const struct oauth_service *sp ) |
---|
244 | { |
---|
245 | char *s; |
---|
246 | |
---|
247 | oauth_params_set( params, "oauth_consumer_key", sp->consumer_key ); |
---|
248 | oauth_params_set( params, "oauth_signature_method", "HMAC-SHA1" ); |
---|
249 | |
---|
250 | s = g_strdup_printf( "%d", (int) time( NULL ) ); |
---|
251 | oauth_params_set( params, "oauth_timestamp", s ); |
---|
252 | g_free( s ); |
---|
253 | |
---|
254 | s = oauth_nonce(); |
---|
255 | oauth_params_set( params, "oauth_nonce", s ); |
---|
256 | g_free( s ); |
---|
257 | |
---|
258 | oauth_params_set( params, "oauth_version", "1.0" ); |
---|
259 | } |
---|
260 | |
---|
261 | static void *oauth_post_request( const char *url, GSList **params_, http_input_function func, struct oauth_info *oi ) |
---|
262 | { |
---|
263 | GSList *params = NULL; |
---|
264 | char *s, *params_s, *post; |
---|
265 | void *req; |
---|
266 | url_t url_p; |
---|
267 | |
---|
268 | if( !url_set( &url_p, url ) ) |
---|
269 | { |
---|
270 | oauth_params_free( params_ ); |
---|
271 | return NULL; |
---|
272 | } |
---|
273 | |
---|
274 | if( params_ ) |
---|
275 | params = *params_; |
---|
276 | |
---|
277 | oauth_add_default_params( ¶ms, oi->sp ); |
---|
278 | |
---|
279 | params_s = oauth_params_string( params ); |
---|
280 | oauth_params_free( ¶ms ); |
---|
281 | |
---|
282 | s = oauth_sign( "POST", url, params_s, oi ); |
---|
283 | post = g_strdup_printf( "%s&oauth_signature=%s", params_s, s ); |
---|
284 | g_free( params_s ); |
---|
285 | g_free( s ); |
---|
286 | |
---|
287 | s = g_strdup_printf( "POST %s HTTP/1.0\r\n" |
---|
288 | "Host: %s\r\n" |
---|
289 | "Content-Type: application/x-www-form-urlencoded\r\n" |
---|
290 | "Content-Length: %zd\r\n" |
---|
291 | "Connection: close\r\n" |
---|
292 | "\r\n" |
---|
293 | "%s", url_p.file, url_p.host, strlen( post ), post ); |
---|
294 | g_free( post ); |
---|
295 | |
---|
296 | req = http_dorequest( url_p.host, url_p.port, url_p.proto == PROTO_HTTPS, |
---|
297 | s, func, oi ); |
---|
298 | g_free( s ); |
---|
299 | |
---|
300 | return req; |
---|
301 | } |
---|
302 | |
---|
303 | static void oauth_request_token_done( struct http_request *req ); |
---|
304 | |
---|
305 | struct oauth_info *oauth_request_token( const struct oauth_service *sp, oauth_cb func, void *data ) |
---|
306 | { |
---|
307 | struct oauth_info *st = g_new0( struct oauth_info, 1 ); |
---|
308 | GSList *params = NULL; |
---|
309 | |
---|
310 | st->func = func; |
---|
311 | st->data = data; |
---|
312 | st->sp = sp; |
---|
313 | |
---|
314 | oauth_params_add( ¶ms, "oauth_callback", "oob" ); |
---|
315 | |
---|
316 | if( !oauth_post_request( sp->url_request_token, ¶ms, oauth_request_token_done, st ) ) |
---|
317 | { |
---|
318 | oauth_info_free( st ); |
---|
319 | return NULL; |
---|
320 | } |
---|
321 | |
---|
322 | return st; |
---|
323 | } |
---|
324 | |
---|
325 | static void oauth_request_token_done( struct http_request *req ) |
---|
326 | { |
---|
327 | struct oauth_info *st = req->data; |
---|
328 | |
---|
329 | st->http = req; |
---|
330 | |
---|
331 | if( req->status_code == 200 ) |
---|
332 | { |
---|
333 | GSList *params = NULL; |
---|
334 | |
---|
335 | st->auth_url = g_strdup_printf( "%s?%s", st->sp->url_authorize, req->reply_body ); |
---|
336 | oauth_params_parse( ¶ms, req->reply_body ); |
---|
337 | st->request_token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); |
---|
338 | oauth_params_free( ¶ms ); |
---|
339 | } |
---|
340 | |
---|
341 | st->stage = OAUTH_REQUEST_TOKEN; |
---|
342 | st->func( st ); |
---|
343 | } |
---|
344 | |
---|
345 | static void oauth_access_token_done( struct http_request *req ); |
---|
346 | |
---|
347 | gboolean oauth_access_token( const char *pin, struct oauth_info *st ) |
---|
348 | { |
---|
349 | GSList *params = NULL; |
---|
350 | |
---|
351 | oauth_params_add( ¶ms, "oauth_token", st->request_token ); |
---|
352 | oauth_params_add( ¶ms, "oauth_verifier", pin ); |
---|
353 | |
---|
354 | return oauth_post_request( st->sp->url_access_token, ¶ms, oauth_access_token_done, st ) != NULL; |
---|
355 | } |
---|
356 | |
---|
357 | static void oauth_access_token_done( struct http_request *req ) |
---|
358 | { |
---|
359 | struct oauth_info *st = req->data; |
---|
360 | |
---|
361 | st->http = req; |
---|
362 | |
---|
363 | if( req->status_code == 200 ) |
---|
364 | { |
---|
365 | oauth_params_parse( &st->params, req->reply_body ); |
---|
366 | st->token = g_strdup( oauth_params_get( &st->params, "oauth_token" ) ); |
---|
367 | st->token_secret = g_strdup( oauth_params_get( &st->params, "oauth_token_secret" ) ); |
---|
368 | } |
---|
369 | |
---|
370 | st->stage = OAUTH_ACCESS_TOKEN; |
---|
371 | if( st->func( st ) ) |
---|
372 | { |
---|
373 | /* Don't need these anymore, but keep the rest. */ |
---|
374 | g_free( st->auth_url ); |
---|
375 | st->auth_url = NULL; |
---|
376 | g_free( st->request_token ); |
---|
377 | st->request_token = NULL; |
---|
378 | oauth_params_free( &st->params ); |
---|
379 | } |
---|
380 | } |
---|
381 | |
---|
382 | char *oauth_http_header( struct oauth_info *oi, const char *method, const char *url, char *args ) |
---|
383 | { |
---|
384 | GSList *params = NULL, *l; |
---|
385 | char *sig = NULL, *params_s, *s; |
---|
386 | GString *ret = NULL; |
---|
387 | |
---|
388 | oauth_params_add( ¶ms, "oauth_token", oi->token ); |
---|
389 | oauth_add_default_params( ¶ms, oi->sp ); |
---|
390 | |
---|
391 | /* Start building the OAuth header. 'key="value", '... */ |
---|
392 | ret = g_string_new( "OAuth " ); |
---|
393 | for( l = params; l; l = l->next ) |
---|
394 | { |
---|
395 | char *kv = l->data; |
---|
396 | char *eq = strchr( kv, '=' ); |
---|
397 | char esc[strlen(kv)*3+1]; |
---|
398 | |
---|
399 | if( eq == NULL ) |
---|
400 | break; /* WTF */ |
---|
401 | |
---|
402 | strcpy( esc, eq + 1 ); |
---|
403 | http_encode( esc ); |
---|
404 | |
---|
405 | g_string_append_len( ret, kv, eq - kv + 1 ); |
---|
406 | g_string_append_c( ret, '"' ); |
---|
407 | g_string_append( ret, esc ); |
---|
408 | g_string_append( ret, "\", " ); |
---|
409 | } |
---|
410 | |
---|
411 | /* Now, before generating the signature, add GET/POST arguments to params |
---|
412 | since they should be included in the base signature string (but not in |
---|
413 | the HTTP header). */ |
---|
414 | if( args ) |
---|
415 | oauth_params_parse( ¶ms, args ); |
---|
416 | if( ( s = strchr( url, '?' ) ) ) |
---|
417 | { |
---|
418 | s = g_strdup( s + 1 ); |
---|
419 | oauth_params_parse( ¶ms, s ); |
---|
420 | g_free( s ); |
---|
421 | } |
---|
422 | |
---|
423 | /* Append the signature and we're done! */ |
---|
424 | params_s = oauth_params_string( params ); |
---|
425 | sig = oauth_sign( method, url, params_s, oi ); |
---|
426 | g_string_append_printf( ret, "oauth_signature=\"%s\"", sig ); |
---|
427 | g_free( params_s ); |
---|
428 | |
---|
429 | oauth_params_free( ¶ms ); |
---|
430 | g_free( sig ); |
---|
431 | |
---|
432 | return ret ? g_string_free( ret, FALSE ) : NULL; |
---|
433 | } |
---|
434 | |
---|
435 | char *oauth_to_string( struct oauth_info *oi ) |
---|
436 | { |
---|
437 | GSList *params = NULL; |
---|
438 | char *ret; |
---|
439 | |
---|
440 | oauth_params_add( ¶ms, "oauth_token", oi->token ); |
---|
441 | oauth_params_add( ¶ms, "oauth_token_secret", oi->token_secret ); |
---|
442 | ret = oauth_params_string( params ); |
---|
443 | oauth_params_free( ¶ms ); |
---|
444 | |
---|
445 | return ret; |
---|
446 | } |
---|
447 | |
---|
448 | struct oauth_info *oauth_from_string( char *in, const struct oauth_service *sp ) |
---|
449 | { |
---|
450 | struct oauth_info *oi = g_new0( struct oauth_info, 1 ); |
---|
451 | GSList *params = NULL; |
---|
452 | |
---|
453 | oauth_params_parse( ¶ms, in ); |
---|
454 | oi->token = g_strdup( oauth_params_get( ¶ms, "oauth_token" ) ); |
---|
455 | oi->token_secret = g_strdup( oauth_params_get( ¶ms, "oauth_token_secret" ) ); |
---|
456 | oauth_params_free( ¶ms ); |
---|
457 | oi->sp = sp; |
---|
458 | |
---|
459 | return oi; |
---|
460 | } |
---|