-rw-r--r-- | lib/basic_op.cc | 35 | ||||
-rw-r--r-- | lib/util.cc | 30 |
2 files changed, 33 insertions, 32 deletions
diff --git a/lib/basic_op.cc b/lib/basic_op.cc index f7573aa..11ffb48 100644 --- a/lib/basic_op.cc +++ b/lib/basic_op.cc | |||
@@ -1,62 +1,59 @@ | |||
1 | #include <time.h> | 1 | #include <time.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <algorithm> | ||
4 | #include <openssl/sha.h> | 3 | #include <openssl/sha.h> |
5 | #include <openssl/hmac.h> | 4 | #include <openssl/hmac.h> |
6 | #include <opkele/data.h> | 5 | #include <opkele/data.h> |
7 | #include <opkele/basic_op.h> | 6 | #include <opkele/basic_op.h> |
8 | #include <opkele/exception.h> | 7 | #include <opkele/exception.h> |
9 | #include <opkele/util.h> | 8 | #include <opkele/util.h> |
10 | #include <opkele/uris.h> | 9 | #include <opkele/uris.h> |
11 | 10 | ||
12 | namespace opkele { | 11 | namespace opkele { |
13 | using std::pair; | ||
14 | using std::mismatch; | ||
15 | 12 | ||
16 | void basic_op::reset_vars() { | 13 | void basic_op::reset_vars() { |
17 | assoc.reset(); | 14 | assoc.reset(); |
18 | return_to.clear(); realm.clear(); | 15 | return_to.clear(); realm.clear(); |
19 | claimed_id.clear(); identity.clear(); | 16 | claimed_id.clear(); identity.clear(); |
20 | invalidate_handle.clear(); | 17 | invalidate_handle.clear(); |
21 | } | 18 | } |
22 | 19 | ||
23 | bool basic_op::has_return_to() const { | 20 | bool basic_op::has_return_to() const { |
24 | return !return_to.empty(); | 21 | return !return_to.empty(); |
25 | } | 22 | } |
26 | const string& basic_op::get_return_to() const { | 23 | const string& basic_op::get_return_to() const { |
27 | if(return_to.empty()) | 24 | if(return_to.empty()) |
28 | throw no_return_to(OPKELE_CP_ "No return_to URL provided with request"); | 25 | throw no_return_to(OPKELE_CP_ "No return_to URL provided with request"); |
29 | return return_to; | 26 | return return_to; |
30 | } | 27 | } |
31 | 28 | ||
32 | const string& basic_op::get_realm() const { | 29 | const string& basic_op::get_realm() const { |
33 | assert(!realm.empty()); | 30 | assert(!realm.empty()); |
34 | return realm; | 31 | return realm; |
35 | } | 32 | } |
36 | 33 | ||
37 | bool basic_op::has_identity() const { | 34 | bool basic_op::has_identity() const { |
38 | return !identity.empty(); | 35 | return !identity.empty(); |
39 | } | 36 | } |
40 | const string& basic_op::get_claimed_id() const { | 37 | const string& basic_op::get_claimed_id() const { |
41 | if(claimed_id.empty()) | 38 | if(claimed_id.empty()) |
42 | throw non_identity(OPKELE_CP_ "attempting to retrieve claimed_id of non-identity related request"); | 39 | throw non_identity(OPKELE_CP_ "attempting to retrieve claimed_id of non-identity related request"); |
43 | assert(!identity.empty()); | 40 | assert(!identity.empty()); |
44 | return claimed_id; | 41 | return claimed_id; |
45 | } | 42 | } |
46 | const string& basic_op::get_identity() const { | 43 | const string& basic_op::get_identity() const { |
47 | if(identity.empty()) | 44 | if(identity.empty()) |
48 | throw non_identity(OPKELE_CP_ "attempting to retrieve identity of non-identity related request"); | 45 | throw non_identity(OPKELE_CP_ "attempting to retrieve identity of non-identity related request"); |
49 | assert(!claimed_id.empty()); | 46 | assert(!claimed_id.empty()); |
50 | return identity; | 47 | return identity; |
51 | } | 48 | } |
52 | 49 | ||
53 | bool basic_op::is_id_select() const { | 50 | bool basic_op::is_id_select() const { |
54 | return identity==IDURI_SELECT20; | 51 | return identity==IDURI_SELECT20; |
55 | } | 52 | } |
56 | 53 | ||
57 | void basic_op::select_identity(const string& c,const string& i) { | 54 | void basic_op::select_identity(const string& c,const string& i) { |
58 | claimed_id = c; identity = i; | 55 | claimed_id = c; identity = i; |
59 | } | 56 | } |
60 | void basic_op::set_claimed_id(const string& c) { | 57 | void basic_op::set_claimed_id(const string& c) { |
61 | claimed_id = c; | 58 | claimed_id = c; |
62 | } | 59 | } |
@@ -276,81 +273,55 @@ namespace opkele { | |||
276 | try { | 273 | try { |
277 | o2 = (inm.get_field("ns")==OIURI_OPENID20); | 274 | o2 = (inm.get_field("ns")==OIURI_OPENID20); |
278 | }catch(failed_lookup&) { o2 = false; } | 275 | }catch(failed_lookup&) { o2 = false; } |
279 | string nonce; | 276 | string nonce; |
280 | if(o2) { | 277 | if(o2) { |
281 | try { | 278 | try { |
282 | if(!check_nonce(nonce = inm.get_field("response_nonce"))) | 279 | if(!check_nonce(nonce = inm.get_field("response_nonce"))) |
283 | throw failed_check_authentication(OPKELE_CP_ "Invalid nonce"); | 280 | throw failed_check_authentication(OPKELE_CP_ "Invalid nonce"); |
284 | }catch(failed_lookup&) { | 281 | }catch(failed_lookup&) { |
285 | throw failed_check_authentication(OPKELE_CP_ "No nonce provided with check_authentication request"); | 282 | throw failed_check_authentication(OPKELE_CP_ "No nonce provided with check_authentication request"); |
286 | } | 283 | } |
287 | } | 284 | } |
288 | try { | 285 | try { |
289 | assoc = retrieve_assoc(inm.get_field("assoc_handle")); | 286 | assoc = retrieve_assoc(inm.get_field("assoc_handle")); |
290 | if(!assoc->stateless()) | 287 | if(!assoc->stateless()) |
291 | throw failed_check_authentication(OPKELE_CP_ "Will not do check_authentication on a stateful handle"); | 288 | throw failed_check_authentication(OPKELE_CP_ "Will not do check_authentication on a stateful handle"); |
292 | }catch(failed_lookup&) { | 289 | }catch(failed_lookup&) { |
293 | throw failed_check_authentication(OPKELE_CP_ "No assoc_handle or invalid assoc_handle specified with check_authentication request"); | 290 | throw failed_check_authentication(OPKELE_CP_ "No assoc_handle or invalid assoc_handle specified with check_authentication request"); |
294 | } | 291 | } |
295 | static const string idresmode = "id_res"; | 292 | static const string idresmode = "id_res"; |
296 | try { | 293 | try { |
297 | if(util::base64_signature(assoc,util::change_mode_message_proxy(inm,idresmode))!=inm.get_field("sig")) | 294 | if(util::base64_signature(assoc,util::change_mode_message_proxy(inm,idresmode))!=inm.get_field("sig")) |
298 | throw failed_check_authentication(OPKELE_CP_ "Signature mismatch"); | 295 | throw failed_check_authentication(OPKELE_CP_ "Signature mismatch"); |
299 | }catch(failed_lookup&) { | 296 | }catch(failed_lookup&) { |
300 | throw failed_check_authentication(OPKELE_CP_ "failed to calculate signature"); | 297 | throw failed_check_authentication(OPKELE_CP_ "failed to calculate signature"); |
301 | } | 298 | } |
302 | oum.set_field("is_valid","true"); | 299 | oum.set_field("is_valid","true"); |
303 | try { | 300 | try { |
304 | string h = inm.get_field("invalidate_handle"); | 301 | string h = inm.get_field("invalidate_handle"); |
305 | try { | 302 | try { |
306 | assoc_t ih = retrieve_assoc(h); | 303 | assoc_t ih = retrieve_assoc(h); |
307 | }catch(invalid_handle& ih) { | 304 | }catch(invalid_handle& ih) { |
308 | oum.set_field("invalidate_handle",h); | 305 | oum.set_field("invalidate_handle",h); |
309 | }catch(failed_lookup& ih) { | 306 | }catch(failed_lookup& ih) { |
310 | oum.set_field("invalidate_handle",h); | 307 | oum.set_field("invalidate_handle",h); |
311 | } | 308 | } |
312 | }catch(failed_lookup&) { } | 309 | }catch(failed_lookup&) { } |
313 | if(o2) { | 310 | if(o2) { |
314 | assert(!nonce.empty()); | 311 | assert(!nonce.empty()); |
315 | invalidate_nonce(nonce); | 312 | invalidate_nonce(nonce); |
316 | } | 313 | } |
317 | return oum; | 314 | return oum; |
318 | }catch(failed_check_authentication& ) { | 315 | }catch(failed_check_authentication& ) { |
319 | oum.set_field("is_valid","false"); | 316 | oum.set_field("is_valid","false"); |
320 | return oum; | 317 | return oum; |
321 | } | 318 | } |
322 | 319 | ||
323 | void basic_op::verify_return_to() { | 320 | void basic_op::verify_return_to() { |
324 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 321 | if(realm.find('#')!=string::npos) |
325 | if(nrealm.find('#')!=string::npos) | ||
326 | throw opkele::bad_realm(OPKELE_CP_ "authentication realm contains URI fragment"); | 322 | throw opkele::bad_realm(OPKELE_CP_ "authentication realm contains URI fragment"); |
327 | string nrt = opkele::util::rfc_3986_normalize_uri(return_to); | 323 | if(!util::uri_matches_realm(return_to,realm)) |
328 | string::size_type pr = nrealm.find("://"); | 324 | throw bad_return_to(OPKELE_CP_ "return_to URL doesn't match realm"); |
329 | string::size_type prt = nrt.find("://"); | ||
330 | assert(!(pr==string::npos || prt==string::npos)); | ||
331 | pr += sizeof("://")-1; | ||
332 | prt += sizeof("://")-1; | ||
333 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | ||
334 | pr = nrealm.find('.',pr); | ||
335 | prt = nrt.find('.',prt); | ||
336 | assert(pr!=string::npos); | ||
337 | if(prt==string::npos) | ||
338 | throw bad_return_to( | ||
339 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
340 | // TODO: check for overgeneralized realm | ||
341 | } | ||
342 | string::size_type lr = nrealm.length(); | ||
343 | string::size_type lrt = nrt.length(); | ||
344 | if( (lrt-prt) < (lr-pr) ) | ||
345 | throw bad_return_to( | ||
346 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
347 | pair<const char*,const char*> mp = mismatch( | ||
348 | nrealm.c_str()+pr,nrealm.c_str()+lr, | ||
349 | nrt.c_str()+prt); | ||
350 | if( (*(mp.first-1))!='/' | ||
351 | && !strchr("/?#",*mp.second) ) | ||
352 | throw bad_return_to( | ||
353 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
354 | } | 325 | } |
355 | 326 | ||
356 | } | 327 | } |
diff --git a/lib/util.cc b/lib/util.cc index b7bc437..b85a377 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,55 +1,56 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <algorithm> | ||
8 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
9 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
10 | #include <openssl/hmac.h> | 11 | #include <openssl/hmac.h> |
11 | #include <curl/curl.h> | 12 | #include <curl/curl.h> |
12 | #include "opkele/util.h" | 13 | #include "opkele/util.h" |
13 | #include "opkele/exception.h" | 14 | #include "opkele/exception.h" |
14 | 15 | ||
15 | #include <config.h> | 16 | #include <config.h> |
16 | #ifdef HAVE_DEMANGLE | 17 | #ifdef HAVE_DEMANGLE |
17 | # include <cxxabi.h> | 18 | # include <cxxabi.h> |
18 | #endif | 19 | #endif |
19 | 20 | ||
20 | namespace opkele { | 21 | namespace opkele { |
21 | using namespace std; | 22 | using namespace std; |
22 | 23 | ||
23 | namespace util { | 24 | namespace util { |
24 | 25 | ||
25 | /* | 26 | /* |
26 | * base64 | 27 | * base64 |
27 | */ | 28 | */ |
28 | string encode_base64(const void *data,size_t length) { | 29 | string encode_base64(const void *data,size_t length) { |
29 | BIO *b64 = 0, *bmem = 0; | 30 | BIO *b64 = 0, *bmem = 0; |
30 | try { | 31 | try { |
31 | b64 = BIO_new(BIO_f_base64()); | 32 | b64 = BIO_new(BIO_f_base64()); |
32 | if(!b64) | 33 | if(!b64) |
33 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 34 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
34 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 35 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
35 | bmem = BIO_new(BIO_s_mem()); | 36 | bmem = BIO_new(BIO_s_mem()); |
36 | BIO_set_flags(b64,BIO_CLOSE); | 37 | BIO_set_flags(b64,BIO_CLOSE); |
37 | if(!bmem) | 38 | if(!bmem) |
38 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 39 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
39 | BIO_push(b64,bmem); | 40 | BIO_push(b64,bmem); |
40 | if(((size_t)BIO_write(b64,data,length))!=length) | 41 | if(((size_t)BIO_write(b64,data,length))!=length) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
42 | if(BIO_flush(b64)!=1) | 43 | if(BIO_flush(b64)!=1) |
43 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
44 | char *rvd; | 45 | char *rvd; |
45 | long rvl = BIO_get_mem_data(bmem,&rvd); | 46 | long rvl = BIO_get_mem_data(bmem,&rvd); |
46 | string rv(rvd,rvl); | 47 | string rv(rvd,rvl); |
47 | BIO_free_all(b64); | 48 | BIO_free_all(b64); |
48 | return rv; | 49 | return rv; |
49 | }catch(...) { | 50 | }catch(...) { |
50 | if(b64) BIO_free_all(b64); | 51 | if(b64) BIO_free_all(b64); |
51 | throw; | 52 | throw; |
52 | } | 53 | } |
53 | } | 54 | } |
54 | 55 | ||
55 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 56 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
@@ -306,96 +307,125 @@ namespace opkele { | |||
306 | } | 307 | } |
307 | }else{ | 308 | }else{ |
308 | psegs.push(rv.length()); | 309 | psegs.push(rv.length()); |
309 | if(c!='/') { | 310 | if(c!='/') { |
310 | pseg += c; | 311 | pseg += c; |
311 | qf = true; | 312 | qf = true; |
312 | } | 313 | } |
313 | rv += '/'; rv += pseg; | 314 | rv += '/'; rv += pseg; |
314 | } | 315 | } |
315 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 316 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
316 | rv += '/'; | 317 | rv += '/'; |
317 | if(n<ul) | 318 | if(n<ul) |
318 | qf = true; | 319 | qf = true; |
319 | }else if(strchr("?#",c)) { | 320 | }else if(strchr("?#",c)) { |
320 | if(psegs.size()==1 && psegs.top()==rv.length()) | 321 | if(psegs.size()==1 && psegs.top()==rv.length()) |
321 | rv += '/'; | 322 | rv += '/'; |
322 | if(pseg.empty()) | 323 | if(pseg.empty()) |
323 | rv += c; | 324 | rv += c; |
324 | qf = true; | 325 | qf = true; |
325 | } | 326 | } |
326 | pseg.clear(); | 327 | pseg.clear(); |
327 | }else{ | 328 | }else{ |
328 | pseg += c; | 329 | pseg += c; |
329 | } | 330 | } |
330 | } | 331 | } |
331 | if(!pseg.empty()) { | 332 | if(!pseg.empty()) { |
332 | if(!qf) rv += '/'; | 333 | if(!qf) rv += '/'; |
333 | rv += pseg; | 334 | rv += pseg; |
334 | } | 335 | } |
335 | return rv; | 336 | return rv; |
336 | } | 337 | } |
337 | 338 | ||
338 | string& strip_uri_fragment_part(string& u) { | 339 | string& strip_uri_fragment_part(string& u) { |
339 | string::size_type q = u.find('?'), f = u.find('#'); | 340 | string::size_type q = u.find('?'), f = u.find('#'); |
340 | if(q==string::npos) { | 341 | if(q==string::npos) { |
341 | if(f!=string::npos) | 342 | if(f!=string::npos) |
342 | u.erase(f); | 343 | u.erase(f); |
343 | }else{ | 344 | }else{ |
344 | if(f!=string::npos) { | 345 | if(f!=string::npos) { |
345 | if(f<q) | 346 | if(f<q) |
346 | u.erase(f,q-f); | 347 | u.erase(f,q-f); |
347 | else | 348 | else |
348 | u.erase(f); | 349 | u.erase(f); |
349 | } | 350 | } |
350 | } | 351 | } |
351 | return u; | 352 | return u; |
352 | } | 353 | } |
353 | 354 | ||
355 | bool uri_matches_realm(const string& uri,const string& realm) { | ||
356 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | ||
357 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | ||
358 | string::size_type pr = nrealm.find("://"); | ||
359 | string::size_type pu = nu.find("://"); | ||
360 | assert(!(pr==string::npos || pu==string::npos)); | ||
361 | pr += sizeof("://")-1; | ||
362 | pu += sizeof("://")-1; | ||
363 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | ||
364 | pr = nrealm.find('.',pr); | ||
365 | pu = nu.find('.',pu); | ||
366 | assert(pr!=string::npos); | ||
367 | if(pu==string::npos) | ||
368 | return false; | ||
369 | // TODO: check for overgeneralized realm | ||
370 | } | ||
371 | string::size_type lr = nrealm.length(); | ||
372 | string::size_type lu = nu.length(); | ||
373 | if( (lu-pu) < (lr-pr) ) | ||
374 | return false; | ||
375 | pair<const char*,const char*> mp = mismatch( | ||
376 | nrealm.c_str()+pr,nrealm.c_str()+lr, | ||
377 | nu.c_str()+pu); | ||
378 | if( (*(mp.first-1))!='/' | ||
379 | && !strchr("/?#",*mp.second) ) | ||
380 | return false; | ||
381 | return true; | ||
382 | } | ||
383 | |||
354 | string abi_demangle(const char *mn) { | 384 | string abi_demangle(const char *mn) { |
355 | #ifndef HAVE_DEMANGLE | 385 | #ifndef HAVE_DEMANGLE |
356 | return mn; | 386 | return mn; |
357 | #else /* !HAVE_DEMANGLE */ | 387 | #else /* !HAVE_DEMANGLE */ |
358 | int dstat; | 388 | int dstat; |
359 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 389 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
360 | if(dstat) | 390 | if(dstat) |
361 | return mn; | 391 | return mn; |
362 | string rv = demangled; | 392 | string rv = demangled; |
363 | free(demangled); | 393 | free(demangled); |
364 | return rv; | 394 | return rv; |
365 | #endif /* !HAVE_DEMANGLE */ | 395 | #endif /* !HAVE_DEMANGLE */ |
366 | } | 396 | } |
367 | 397 | ||
368 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 398 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
369 | const string& slist = om.get_field("signed"); | 399 | const string& slist = om.get_field("signed"); |
370 | string kv; | 400 | string kv; |
371 | string::size_type p=0; | 401 | string::size_type p=0; |
372 | while(true) { | 402 | while(true) { |
373 | string::size_type co = slist.find(',',p); | 403 | string::size_type co = slist.find(',',p); |
374 | string f = (co==string::npos) | 404 | string f = (co==string::npos) |
375 | ?slist.substr(p):slist.substr(p,co-p); | 405 | ?slist.substr(p):slist.substr(p,co-p); |
376 | kv += f; | 406 | kv += f; |
377 | kv += ':'; | 407 | kv += ':'; |
378 | kv += om.get_field(f); | 408 | kv += om.get_field(f); |
379 | kv += '\n'; | 409 | kv += '\n'; |
380 | if(co==string::npos) break; | 410 | if(co==string::npos) break; |
381 | p = co+1; | 411 | p = co+1; |
382 | } | 412 | } |
383 | const secret_t& secret = assoc->secret(); | 413 | const secret_t& secret = assoc->secret(); |
384 | const EVP_MD *evpmd; | 414 | const EVP_MD *evpmd; |
385 | const string& at = assoc->assoc_type(); | 415 | const string& at = assoc->assoc_type(); |
386 | if(at=="HMAC-SHA256") | 416 | if(at=="HMAC-SHA256") |
387 | evpmd = EVP_sha256(); | 417 | evpmd = EVP_sha256(); |
388 | else if(at=="HMAC-SHA1") | 418 | else if(at=="HMAC-SHA1") |
389 | evpmd = EVP_sha1(); | 419 | evpmd = EVP_sha1(); |
390 | else | 420 | else |
391 | throw unsupported(OPKELE_CP_ "unknown association type"); | 421 | throw unsupported(OPKELE_CP_ "unknown association type"); |
392 | unsigned int md_len = 0; | 422 | unsigned int md_len = 0; |
393 | unsigned char *md = HMAC(evpmd, | 423 | unsigned char *md = HMAC(evpmd, |
394 | &(secret.front()),secret.size(), | 424 | &(secret.front()),secret.size(), |
395 | (const unsigned char*)kv.data(),kv.length(), | 425 | (const unsigned char*)kv.data(),kv.length(), |
396 | 0,&md_len); | 426 | 0,&md_len); |
397 | return encode_base64(md,md_len); | 427 | return encode_base64(md,md_len); |
398 | } | 428 | } |
399 | 429 | ||
400 | } | 430 | } |
401 | 431 | ||