summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2007-01-15 00:09:35 (UTC)
committer Michael Krelin <hacker@klever.net>2007-01-15 00:09:35 (UTC)
commit6c7a4fbf0c0e68a500a6b5834a1f3877b160bf77 (patch) (unidiff)
treeba6fa2874ab75f16cd588cc1a0729f6f78b18b93
parent4cfc41760dea44228b590fa9682b19f8a2e38ec2 (diff)
downloadlibopkele-6c7a4fbf0c0e68a500a6b5834a1f3877b160bf77.zip
libopkele-6c7a4fbf0c0e68a500a6b5834a1f3877b160bf77.tar.gz
libopkele-6c7a4fbf0c0e68a500a6b5834a1f3877b160bf77.tar.bz2
--disable-ssl-verify-{host,peer} options added
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--configure.ac14
-rw-r--r--lib/consumer.cc6
2 files changed, 20 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index fd50721..8397914 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,71 +1,85 @@
1AC_INIT([libopkele], [0.1.1], [libopkele-bugs@klever.net]) 1AC_INIT([libopkele], [0.1.1], [libopkele-bugs@klever.net])
2AC_CONFIG_SRCDIR([include/opkele/opkele-config.h]) 2AC_CONFIG_SRCDIR([include/opkele/opkele-config.h])
3AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h]) 3AC_CONFIG_HEADERS([config.h include/opkele/acconfig.h])
4AM_INIT_AUTOMAKE([dist-bzip2]) 4AM_INIT_AUTOMAKE([dist-bzip2])
5 5
6AC_PROG_INSTALL 6AC_PROG_INSTALL
7AC_PROG_CXX 7AC_PROG_CXX
8AC_PROG_CC 8AC_PROG_CC
9AC_PROG_LIBTOOL 9AC_PROG_LIBTOOL
10 10
11AC_HEADER_STDC 11AC_HEADER_STDC
12 12
13AC_PATH_PROG([XSLTPROC],[xsltproc],[true]) 13AC_PATH_PROG([XSLTPROC],[xsltproc],[true])
14AC_WITH_PKGCONFIG 14AC_WITH_PKGCONFIG
15 15
16PKG_CHECK_MODULES([OPENSSL],[openssl],,[ 16PKG_CHECK_MODULES([OPENSSL],[openssl],,[
17 AC_MSG_ERROR([no openssl library found. get one from http://www.openssl.org/]) 17 AC_MSG_ERROR([no openssl library found. get one from http://www.openssl.org/])
18]) 18])
19 19
20WANT_KONFORKA="yes" 20WANT_KONFORKA="yes"
21AC_ARG_ENABLE([konforka], 21AC_ARG_ENABLE([konforka],
22 AC_HELP_STRING([--disable-konforka],[do not use konforka library (default: use if found)]), 22 AC_HELP_STRING([--disable-konforka],[do not use konforka library (default: use if found)]),
23 [ 23 [
24 test "${enableval}" = "no" && WANT_KONFORKA="no" 24 test "${enableval}" = "no" && WANT_KONFORKA="no"
25 ] 25 ]
26) 26)
27if test "${WANT_KONFORKA}" = "yes" ; then 27if test "${WANT_KONFORKA}" = "yes" ; then
28 PKG_CHECK_MODULES([KONFORKA],[konforka],[ 28 PKG_CHECK_MODULES([KONFORKA],[konforka],[
29 AC_SUBST([KONFORKA_CFLAGS]) 29 AC_SUBST([KONFORKA_CFLAGS])
30 AC_SUBST([KONFORKA_LIBS]) 30 AC_SUBST([KONFORKA_LIBS])
31 AC_DEFINE([HAVE_KONFORKA],,[defined in presence of konforka library]) 31 AC_DEFINE([HAVE_KONFORKA],,[defined in presence of konforka library])
32 AC_DEFINE([OPKELE_HAVE_KONFORKA],,[defined in presence of konforka library]) 32 AC_DEFINE([OPKELE_HAVE_KONFORKA],,[defined in presence of konforka library])
33 AC_SUBST([KONFORKA_KONFORKA],[konforka]) 33 AC_SUBST([KONFORKA_KONFORKA],[konforka])
34 ],[true]) 34 ],[true])
35fi 35fi
36 36
37WANT_DOXYGEN="yes" 37WANT_DOXYGEN="yes"
38AC_ARG_ENABLE([doxygen], 38AC_ARG_ENABLE([doxygen],
39 AC_HELP_STRING([--disable-doxygen],[do not generate documentation]), 39 AC_HELP_STRING([--disable-doxygen],[do not generate documentation]),
40 [ 40 [
41 test "${enableval}" = "no" && WANT_DOXYGEN="no" 41 test "${enableval}" = "no" && WANT_DOXYGEN="no"
42 ] 42 ]
43) 43)
44if test "${WANT_DOXYGEN}" = "yes" ; then 44if test "${WANT_DOXYGEN}" = "yes" ; then
45 AC_WITH_DOXYGEN 45 AC_WITH_DOXYGEN
46 AC_WITH_DOT 46 AC_WITH_DOT
47else 47else
48 AM_CONDITIONAL([HAVE_DOXYGEN],[false]) 48 AM_CONDITIONAL([HAVE_DOXYGEN],[false])
49 AM_CONDITIONAL([HAVE_DOT],[false]) 49 AM_CONDITIONAL([HAVE_DOT],[false])
50fi 50fi
51 51
52LIBCURL_CHECK_CONFIG(,,,[ 52LIBCURL_CHECK_CONFIG(,,,[
53 AC_MSG_ERROR([no required libcurl library. get one from http://curl.haxx.se/]) 53 AC_MSG_ERROR([no required libcurl library. get one from http://curl.haxx.se/])
54]) 54])
55AC_WITH_PCRE([ 55AC_WITH_PCRE([
56 AC_WITH_PCREPP(,[ 56 AC_WITH_PCREPP(,[
57 AC_MSG_ERROR([no pcre++ library found. get one at http://www.daemon.de/PCRE]) 57 AC_MSG_ERROR([no pcre++ library found. get one at http://www.daemon.de/PCRE])
58 ]) 58 ])
59 ],[ 59 ],[
60 AC_MSG_ERROR([no pcre library found. get one at http://www.pcre.org/]) 60 AC_MSG_ERROR([no pcre library found. get one at http://www.pcre.org/])
61 ] 61 ]
62) 62)
63 63
64curl_ssl_verify_host="true"
65AC_ARG_ENABLE([ssl-verify-host],
66 AC_HELP_STRING([--disable-ssl-verify-host],[disable cURL cert/host relationships verification]),
67 [ test "${enableval}" = "no" && curl_ssl_verify_host="false" ]
68)
69${curl_ssl_verify_host} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYHOST],,[defined if cURL is not to verify cert/host])
70
71curl_ssl_verify_peer="true"
72AC_ARG_ENABLE([ssl-verify-peer],
73 AC_HELP_STRING([--disable-ssl-verify-peer],[disable cURL cert validity verification]),
74 [ test "${enableval}" = "no" && curl_ssl_verify_peer="false" ]
75)
76${curl_ssl_verify_peer} || AC_DEFINE([DISABLE_CURL_SSL_VERIFYPEER],,[defined if cURL is not to verify cert validity])
77
64AC_CONFIG_FILES([ 78AC_CONFIG_FILES([
65 Makefile 79 Makefile
66 libopkele.pc 80 libopkele.pc
67 Doxyfile 81 Doxyfile
68 include/Makefile 82 include/Makefile
69 lib/Makefile 83 lib/Makefile
70]) 84])
71AC_OUTPUT 85AC_OUTPUT
diff --git a/lib/consumer.cc b/lib/consumer.cc
index 331b1e9..dc49405 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,340 +1,346 @@
1#include <algorithm> 1#include <algorithm>
2#include <cassert> 2#include <cassert>
3#include <opkele/util.h> 3#include <opkele/util.h>
4#include <opkele/exception.h> 4#include <opkele/exception.h>
5#include <opkele/data.h> 5#include <opkele/data.h>
6#include <opkele/consumer.h> 6#include <opkele/consumer.h>
7#include <openssl/sha.h> 7#include <openssl/sha.h>
8#include <openssl/hmac.h> 8#include <openssl/hmac.h>
9#include <curl/curl.h> 9#include <curl/curl.h>
10#include <pcre++.h> 10#include <pcre++.h>
11 11
12#include <iostream> 12#include <iostream>
13 13
14#include "config.h" 14#include "config.h"
15 15
16namespace opkele { 16namespace opkele {
17 using namespace std; 17 using namespace std;
18 18
19 class curl_t { 19 class curl_t {
20 public: 20 public:
21 CURL *_c; 21 CURL *_c;
22 22
23 curl_t() : _c(0) { } 23 curl_t() : _c(0) { }
24 curl_t(CURL *c) : _c(c) { } 24 curl_t(CURL *c) : _c(c) { }
25 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } 25 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); }
26 26
27 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } 27 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; }
28 28
29 operator const CURL*(void) const { return _c; } 29 operator const CURL*(void) const { return _c; }
30 operator CURL*(void) { return _c; } 30 operator CURL*(void) { return _c; }
31 }; 31 };
32 32
33 static CURLcode curl_misc_sets(CURL* c) { 33 static CURLcode curl_misc_sets(CURL* c) {
34 CURLcode r; 34 CURLcode r;
35 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) 35 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1))
36 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) 36 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5))
37 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) 37 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120))
38 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) 38 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1))
39 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) 39 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION))
40 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) 40 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20))
41 #ifdefDISABLE_CURL_SSL_VERIFYHOST
42 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYHOST,0))
43#endif
44 #ifdefDISABLE_CURL_SSL_VERYPEER
45 || (r=curl_easy_setopt(c,CURLOPT_SSL_VERIFYPEER,0))
46#endif
41 ; 47 ;
42 return r; 48 return r;
43 } 49 }
44 50
45 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { 51 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
46 string *str = (string*)stream; 52 string *str = (string*)stream;
47 size_t bytes = size*nmemb; 53 size_t bytes = size*nmemb;
48 size_t get = min(16384-str->length(),bytes); 54 size_t get = min(16384-str->length(),bytes);
49 str->append((const char*)ptr,get); 55 str->append((const char*)ptr,get);
50 return get; 56 return get;
51 } 57 }
52 58
53 assoc_t consumer_t::associate(const string& server) { 59 assoc_t consumer_t::associate(const string& server) {
54 util::dh_t dh = DH_new(); 60 util::dh_t dh = DH_new();
55 if(!dh) 61 if(!dh)
56 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 62 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
57 dh->p = util::dec_to_bignum(data::_default_p); 63 dh->p = util::dec_to_bignum(data::_default_p);
58 dh->g = util::dec_to_bignum(data::_default_g); 64 dh->g = util::dec_to_bignum(data::_default_g);
59 if(!DH_generate_key(dh)) 65 if(!DH_generate_key(dh))
60 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 66 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
61 string request = 67 string request =
62 "openid.mode=associate" 68 "openid.mode=associate"
63 "&openid.assoc_type=HMAC-SHA1" 69 "&openid.assoc_type=HMAC-SHA1"
64 "&openid.session_type=DH-SHA1" 70 "&openid.session_type=DH-SHA1"
65 "&openid.dh_consumer_public="; 71 "&openid.dh_consumer_public=";
66 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 72 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
67 curl_t curl = curl_easy_init(); 73 curl_t curl = curl_easy_init();
68 if(!curl) 74 if(!curl)
69 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 75 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
70 string response; 76 string response;
71 CURLcode r; 77 CURLcode r;
72 (r=curl_misc_sets(curl)) 78 (r=curl_misc_sets(curl))
73 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 79 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
74 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 80 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
75 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 81 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
76 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 82 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
77 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 83 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
78 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 84 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
79 ; 85 ;
80 if(r) 86 if(r)
81 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 87 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
82 if(r=curl_easy_perform(curl)) 88 if(r=curl_easy_perform(curl))
83 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 89 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
84 params_t p; p.parse_keyvalues(response); 90 params_t p; p.parse_keyvalues(response);
85 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 91 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
86 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 92 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
87 string st; 93 string st;
88 if(p.has_param("session_type")) st = p.get_param("session_type"); 94 if(p.has_param("session_type")) st = p.get_param("session_type");
89 if((!st.empty()) && st!="DH-SHA1") 95 if((!st.empty()) && st!="DH-SHA1")
90 throw bad_input(OPKELE_CP_ "unsupported session_type"); 96 throw bad_input(OPKELE_CP_ "unsupported session_type");
91 secret_t secret; 97 secret_t secret;
92 if(st.empty()) { 98 if(st.empty()) {
93 secret.from_base64(p.get_param("mac_key")); 99 secret.from_base64(p.get_param("mac_key"));
94 }else{ 100 }else{
95 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 101 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
96 vector<unsigned char> ck(DH_size(dh)); 102 vector<unsigned char> ck(DH_size(dh));
97 int cklen = DH_compute_key(&(ck.front()),s_pub,dh); 103 int cklen = DH_compute_key(&(ck.front()),s_pub,dh);
98 if(cklen<0) 104 if(cklen<0)
99 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 105 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
100 ck.resize(cklen); 106 ck.resize(cklen);
101 // OpenID algorithm requires extra zero in case of set bit here 107 // OpenID algorithm requires extra zero in case of set bit here
102 if(ck[0]&0x80) ck.insert(ck.begin(),1,0); 108 if(ck[0]&0x80) ck.insert(ck.begin(),1,0);
103 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 109 unsigned char key_sha1[SHA_DIGEST_LENGTH];
104 SHA1(&(ck.front()),ck.size(),key_sha1); 110 SHA1(&(ck.front()),ck.size(),key_sha1);
105 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 111 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
106 } 112 }
107 int expires_in = 0; 113 int expires_in = 0;
108 if(p.has_param("expires_in")) { 114 if(p.has_param("expires_in")) {
109 expires_in = util::string_to_long(p.get_param("expires_in")); 115 expires_in = util::string_to_long(p.get_param("expires_in"));
110 }else if(p.has_param("issued") && p.has_param("expiry")) { 116 }else if(p.has_param("issued") && p.has_param("expiry")) {
111 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 117 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
112 }else 118 }else
113 throw bad_input(OPKELE_CP_ "no expiration information"); 119 throw bad_input(OPKELE_CP_ "no expiration information");
114 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 120 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
115 } 121 }
116 122
117 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 123 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
118 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); 124 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
119 } 125 }
120 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 126 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
121 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); 127 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
122 } 128 }
123 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 129 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
124 params_t p; 130 params_t p;
125 if(mode==mode_checkid_immediate) 131 if(mode==mode_checkid_immediate)
126 p["mode"]="checkid_immediate"; 132 p["mode"]="checkid_immediate";
127 else if(mode==mode_checkid_setup) 133 else if(mode==mode_checkid_setup)
128 p["mode"]="checkid_setup"; 134 p["mode"]="checkid_setup";
129 else 135 else
130 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 136 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
131 string iurl = canonicalize(identity); 137 string iurl = canonicalize(identity);
132 string server, delegate; 138 string server, delegate;
133 retrieve_links(iurl,server,delegate); 139 retrieve_links(iurl,server,delegate);
134 p["identity"] = delegate.empty()?iurl:delegate; 140 p["identity"] = delegate.empty()?iurl:delegate;
135 if(!trust_root.empty()) 141 if(!trust_root.empty())
136 p["trust_root"] = trust_root; 142 p["trust_root"] = trust_root;
137 p["return_to"] = return_to; 143 p["return_to"] = return_to;
138 try { 144 try {
139 try { 145 try {
140 string ah = find_assoc(server)->handle(); 146 string ah = find_assoc(server)->handle();
141 p["assoc_handle"] = ah; 147 p["assoc_handle"] = ah;
142 }catch(failed_lookup& fl) { 148 }catch(failed_lookup& fl) {
143 string ah = associate(server)->handle(); 149 string ah = associate(server)->handle();
144 p["assoc_handle"] = ah; 150 p["assoc_handle"] = ah;
145 } 151 }
146 }catch(exception& e) { } 152 }catch(exception& e) { }
147 if(ext) ext->checkid_hook(p,identity); 153 if(ext) ext->checkid_hook(p,identity);
148 return p.append_query(server); 154 return p.append_query(server);
149 } 155 }
150 156
151 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { 157 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
152 if(pin.has_param("openid.user_setup_url")) 158 if(pin.has_param("openid.user_setup_url"))
153 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 159 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
154 string server,delegate; 160 string server,delegate;
155 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 161 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
156 params_t ps; 162 params_t ps;
157 try { 163 try {
158 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 164 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
159 const string& sigenc = pin.get_param("openid.sig"); 165 const string& sigenc = pin.get_param("openid.sig");
160 vector<unsigned char> sig; 166 vector<unsigned char> sig;
161 util::decode_base64(sigenc,sig); 167 util::decode_base64(sigenc,sig);
162 const string& slist = pin.get_param("openid.signed"); 168 const string& slist = pin.get_param("openid.signed");
163 string kv; 169 string kv;
164 string::size_type p = 0; 170 string::size_type p = 0;
165 while(true) { 171 while(true) {
166 string::size_type co = slist.find(',',p); 172 string::size_type co = slist.find(',',p);
167 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 173 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
168 kv += f; 174 kv += f;
169 kv += ':'; 175 kv += ':';
170 f.insert(0,"openid."); 176 f.insert(0,"openid.");
171 kv += pin.get_param(f); 177 kv += pin.get_param(f);
172 kv += '\n'; 178 kv += '\n';
173 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); 179 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f);
174 if(co==string::npos) 180 if(co==string::npos)
175 break; 181 break;
176 p = co+1; 182 p = co+1;
177 } 183 }
178 secret_t secret = assoc->secret(); 184 secret_t secret = assoc->secret();
179 unsigned int md_len = 0; 185 unsigned int md_len = 0;
180 unsigned char *md = HMAC( 186 unsigned char *md = HMAC(
181 EVP_sha1(), 187 EVP_sha1(),
182 &(secret.front()),secret.size(), 188 &(secret.front()),secret.size(),
183 (const unsigned char *)kv.data(),kv.length(), 189 (const unsigned char *)kv.data(),kv.length(),
184 0,&md_len); 190 0,&md_len);
185 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 191 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
186 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 192 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
187 }catch(failed_lookup& e) { /* XXX: more specific? */ 193 }catch(failed_lookup& e) { /* XXX: more specific? */
188 const string& slist = pin.get_param("openid.signed"); 194 const string& slist = pin.get_param("openid.signed");
189 string::size_type pp = 0; 195 string::size_type pp = 0;
190 params_t p; 196 params_t p;
191 while(true) { 197 while(true) {
192 string::size_type co = slist.find(',',pp); 198 string::size_type co = slist.find(',',pp);
193 string f = "openid."; 199 string f = "openid.";
194 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 200 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
195 p[f] = pin.get_param(f); 201 p[f] = pin.get_param(f);
196 if(co==string::npos) 202 if(co==string::npos)
197 break; 203 break;
198 pp = co+1; 204 pp = co+1;
199 } 205 }
200 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 206 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
201 p["openid.sig"] = pin.get_param("openid.sig"); 207 p["openid.sig"] = pin.get_param("openid.sig");
202 p["openid.signed"] = pin.get_param("openid.signed"); 208 p["openid.signed"] = pin.get_param("openid.signed");
203 try { 209 try {
204 string ih = pin.get_param("openid.invalidate_handle"); 210 string ih = pin.get_param("openid.invalidate_handle");
205 p["openid.invalidate_handle"] = ih; 211 p["openid.invalidate_handle"] = ih;
206 }catch(failed_lookup& fl) { } 212 }catch(failed_lookup& fl) { }
207 try { 213 try {
208 check_authentication(server,p); 214 check_authentication(server,p);
209 }catch(failed_check_authentication& fca) { 215 }catch(failed_check_authentication& fca) {
210 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 216 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
211 } 217 }
212 } 218 }
213 if(ext) ext->id_res_hook(pin,ps,identity); 219 if(ext) ext->id_res_hook(pin,ps,identity);
214 } 220 }
215 221
216 void consumer_t::check_authentication(const string& server,const params_t& p) { 222 void consumer_t::check_authentication(const string& server,const params_t& p) {
217 string request = "openid.mode=check_authentication"; 223 string request = "openid.mode=check_authentication";
218 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 224 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
219 if(i->first!="openid.mode") { 225 if(i->first!="openid.mode") {
220 request += '&'; 226 request += '&';
221 request += i->first; 227 request += i->first;
222 request += '='; 228 request += '=';
223 request += util::url_encode(i->second); 229 request += util::url_encode(i->second);
224 } 230 }
225 } 231 }
226 curl_t curl = curl_easy_init(); 232 curl_t curl = curl_easy_init();
227 if(!curl) 233 if(!curl)
228 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 234 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
229 string response; 235 string response;
230 CURLcode r; 236 CURLcode r;
231 (r=curl_misc_sets(curl)) 237 (r=curl_misc_sets(curl))
232 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 238 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
233 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 239 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
234 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 240 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
235 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 241 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
236 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 242 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
237 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 243 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
238 ; 244 ;
239 if(r) 245 if(r)
240 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 246 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
241 if(r=curl_easy_perform(curl)) 247 if(r=curl_easy_perform(curl))
242 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 248 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
243 params_t pp; pp.parse_keyvalues(response); 249 params_t pp; pp.parse_keyvalues(response);
244 if(pp.has_param("invalidate_handle")) 250 if(pp.has_param("invalidate_handle"))
245 invalidate_assoc(server,pp.get_param("invalidate_handle")); 251 invalidate_assoc(server,pp.get_param("invalidate_handle"));
246 if(pp.has_param("is_valid")) { 252 if(pp.has_param("is_valid")) {
247 if(pp.get_param("is_valid")=="true") 253 if(pp.get_param("is_valid")=="true")
248 return; 254 return;
249 }else if(pp.has_param("lifetime")) { 255 }else if(pp.has_param("lifetime")) {
250 if(util::string_to_long(pp.get_param("lifetime"))) 256 if(util::string_to_long(pp.get_param("lifetime")))
251 return; 257 return;
252 } 258 }
253 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 259 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
254 } 260 }
255 261
256 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 262 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
257 server.erase(); 263 server.erase();
258 delegate.erase(); 264 delegate.erase();
259 curl_t curl = curl_easy_init(); 265 curl_t curl = curl_easy_init();
260 if(!curl) 266 if(!curl)
261 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 267 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
262 string html; 268 string html;
263 CURLcode r; 269 CURLcode r;
264 (r=curl_misc_sets(curl)) 270 (r=curl_misc_sets(curl))
265 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) 271 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str()))
266 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 272 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
267 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) 273 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html))
268 ; 274 ;
269 if(r) 275 if(r)
270 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 276 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
271 r = curl_easy_perform(curl); 277 r = curl_easy_perform(curl);
272 if(r && r!=CURLE_WRITE_ERROR) 278 if(r && r!=CURLE_WRITE_ERROR)
273 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 279 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
274 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); 280 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS);
275 // strip out everything past body 281 // strip out everything past body
276 if(bre.search(html)) 282 if(bre.search(html))
277 html.erase(bre.get_match_start()); 283 html.erase(bre.get_match_start());
278 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS); 284 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS);
279 if(!hdre.search(html)) 285 if(!hdre.search(html))
280 throw bad_input(OPKELE_CP_ "failed to find head"); 286 throw bad_input(OPKELE_CP_ "failed to find head");
281 html.erase(0,hdre.get_match_end()+1); 287 html.erase(0,hdre.get_match_end()+1);
282 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS), 288 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS),
283 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS), 289 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS),
284 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS); 290 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS);
285 while(lre.search(html)) { 291 while(lre.search(html)) {
286 string attrs = lre[0]; 292 string attrs = lre[0];
287 html.erase(0,lre.get_match_end()+1); 293 html.erase(0,lre.get_match_end()+1);
288 if(!(rre.search(attrs)&&hre.search(attrs))) 294 if(!(rre.search(attrs)&&hre.search(attrs)))
289 continue; 295 continue;
290 if(rre[0]=="openid.server") { 296 if(rre[0]=="openid.server") {
291 server = hre[0]; 297 server = hre[0];
292 if(!delegate.empty()) 298 if(!delegate.empty())
293 break; 299 break;
294 }else if(rre[0]=="openid.delegate") { 300 }else if(rre[0]=="openid.delegate") {
295 delegate = hre[0]; 301 delegate = hre[0];
296 if(!server.empty()) 302 if(!server.empty())
297 break; 303 break;
298 } 304 }
299 } 305 }
300 if(server.empty()) 306 if(server.empty())
301 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); 307 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
302 } 308 }
303 309
304 assoc_t consumer_t::find_assoc(const string& server) { 310 assoc_t consumer_t::find_assoc(const string& server) {
305 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); 311 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
306 } 312 }
307 313
308 string consumer_t::canonicalize(const string& url) { 314 string consumer_t::canonicalize(const string& url) {
309 string rv = url; 315 string rv = url;
310 // strip leading and trailing spaces 316 // strip leading and trailing spaces
311 string::size_type i = rv.find_first_not_of(" \t\r\n"); 317 string::size_type i = rv.find_first_not_of(" \t\r\n");
312 if(i==string::npos) 318 if(i==string::npos)
313 throw bad_input(OPKELE_CP_ "empty URL"); 319 throw bad_input(OPKELE_CP_ "empty URL");
314 if(i) 320 if(i)
315 rv.erase(0,i); 321 rv.erase(0,i);
316 i = rv.find_last_not_of(" \t\r\n"); 322 i = rv.find_last_not_of(" \t\r\n");
317 assert(i!=string::npos); 323 assert(i!=string::npos);
318 if(i<(rv.length()-1)) 324 if(i<(rv.length()-1))
319 rv.erase(i+1); 325 rv.erase(i+1);
320 // add missing http:// 326 // add missing http://
321 i = rv.find("://"); 327 i = rv.find("://");
322 if(i==string::npos) { // primitive. but do we need more? 328 if(i==string::npos) { // primitive. but do we need more?
323 rv.insert(0,"http://"); 329 rv.insert(0,"http://");
324 i = sizeof("http://")-1; 330 i = sizeof("http://")-1;
325 }else{ 331 }else{
326 i += sizeof("://")-1; 332 i += sizeof("://")-1;
327 } 333 }
328 string::size_type qm = rv.find('?',i); 334 string::size_type qm = rv.find('?',i);
329 string::size_type sl = rv.find('/',i); 335 string::size_type sl = rv.find('/',i);
330 if(qm!=string::npos) { 336 if(qm!=string::npos) {
331 if(sl==string::npos || sl>qm) 337 if(sl==string::npos || sl>qm)
332 rv.insert(qm,1,'/'); 338 rv.insert(qm,1,'/');
333 }else{ 339 }else{
334 if(sl==string::npos) 340 if(sl==string::npos)
335 rv += '/'; 341 rv += '/';
336 } 342 }
337 return rv; 343 return rv;
338 } 344 }
339 345
340} 346}