-rw-r--r-- | lib/consumer.cc | 16 |
1 files changed, 2 insertions, 14 deletions
diff --git a/lib/consumer.cc b/lib/consumer.cc index b215aa8..331b1e9 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -1,70 +1,61 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <cassert> | ||
2 | #include <opkele/util.h> | 3 | #include <opkele/util.h> |
3 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
4 | #include <opkele/data.h> | 5 | #include <opkele/data.h> |
5 | #include <opkele/consumer.h> | 6 | #include <opkele/consumer.h> |
6 | #include <openssl/sha.h> | 7 | #include <openssl/sha.h> |
7 | #include <openssl/hmac.h> | 8 | #include <openssl/hmac.h> |
8 | #include <mimetic/mimetic.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 | /* silly mimetic */ | ||
15 | #undef PACKAGE | ||
16 | #undef PACKAGE_BUGREPORT | ||
17 | #undef PACKAGE_NAME | ||
18 | #undef PACKAGE_STRING | ||
19 | #undef PACKAGE_TARNAME | ||
20 | #undef PACKAGE_VERSION | ||
21 | #undef VERSION | ||
22 | |||
23 | #include "config.h" | 14 | #include "config.h" |
24 | 15 | ||
25 | namespace opkele { | 16 | namespace opkele { |
26 | using namespace std; | 17 | using namespace std; |
27 | 18 | ||
28 | class curl_t { | 19 | class curl_t { |
29 | public: | 20 | public: |
30 | CURL *_c; | 21 | CURL *_c; |
31 | 22 | ||
32 | curl_t() : _c(0) { } | 23 | curl_t() : _c(0) { } |
33 | curl_t(CURL *c) : _c(c) { } | 24 | curl_t(CURL *c) : _c(c) { } |
34 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } | 25 | ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } |
35 | 26 | ||
36 | 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; } |
37 | 28 | ||
38 | operator const CURL*(void) const { return _c; } | 29 | operator const CURL*(void) const { return _c; } |
39 | operator CURL*(void) { return _c; } | 30 | operator CURL*(void) { return _c; } |
40 | }; | 31 | }; |
41 | 32 | ||
42 | static CURLcode curl_misc_sets(CURL* c) { | 33 | static CURLcode curl_misc_sets(CURL* c) { |
43 | CURLcode r; | 34 | CURLcode r; |
44 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) | 35 | (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) |
45 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) | 36 | || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) |
46 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) | 37 | || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) |
47 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) | 38 | || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) |
48 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) | 39 | || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) |
49 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) | 40 | || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) |
50 | ; | 41 | ; |
51 | return r; | 42 | return r; |
52 | } | 43 | } |
53 | 44 | ||
54 | static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { | 45 | static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { |
55 | string *str = (string*)stream; | 46 | string *str = (string*)stream; |
56 | size_t bytes = size*nmemb; | 47 | size_t bytes = size*nmemb; |
57 | size_t get = min(16384-str->length(),bytes); | 48 | size_t get = min(16384-str->length(),bytes); |
58 | str->append((const char*)ptr,get); | 49 | str->append((const char*)ptr,get); |
59 | return get; | 50 | return get; |
60 | } | 51 | } |
61 | 52 | ||
62 | assoc_t consumer_t::associate(const string& server) { | 53 | assoc_t consumer_t::associate(const string& server) { |
63 | util::dh_t dh = DH_new(); | 54 | util::dh_t dh = DH_new(); |
64 | if(!dh) | 55 | if(!dh) |
65 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); | 56 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); |
66 | dh->p = util::dec_to_bignum(data::_default_p); | 57 | dh->p = util::dec_to_bignum(data::_default_p); |
67 | dh->g = util::dec_to_bignum(data::_default_g); | 58 | dh->g = util::dec_to_bignum(data::_default_g); |
68 | if(!DH_generate_key(dh)) | 59 | if(!DH_generate_key(dh)) |
69 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 60 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
70 | string request = | 61 | string request = |
@@ -121,101 +112,98 @@ namespace opkele { | |||
121 | }else | 112 | }else |
122 | throw bad_input(OPKELE_CP_ "no expiration information"); | 113 | throw bad_input(OPKELE_CP_ "no expiration information"); |
123 | return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); | 114 | return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); |
124 | } | 115 | } |
125 | 116 | ||
126 | string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { | 117 | string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { |
127 | return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); | 118 | return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); |
128 | } | 119 | } |
129 | string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { | 120 | string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { |
130 | return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); | 121 | return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); |
131 | } | 122 | } |
132 | string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { | 123 | string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { |
133 | params_t p; | 124 | params_t p; |
134 | if(mode==mode_checkid_immediate) | 125 | if(mode==mode_checkid_immediate) |
135 | p["mode"]="checkid_immediate"; | 126 | p["mode"]="checkid_immediate"; |
136 | else if(mode==mode_checkid_setup) | 127 | else if(mode==mode_checkid_setup) |
137 | p["mode"]="checkid_setup"; | 128 | p["mode"]="checkid_setup"; |
138 | else | 129 | else |
139 | throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); | 130 | throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); |
140 | string iurl = canonicalize(identity); | 131 | string iurl = canonicalize(identity); |
141 | string server, delegate; | 132 | string server, delegate; |
142 | retrieve_links(iurl,server,delegate); | 133 | retrieve_links(iurl,server,delegate); |
143 | p["identity"] = delegate.empty()?iurl:delegate; | 134 | p["identity"] = delegate.empty()?iurl:delegate; |
144 | if(!trust_root.empty()) | 135 | if(!trust_root.empty()) |
145 | p["trust_root"] = trust_root; | 136 | p["trust_root"] = trust_root; |
146 | p["return_to"] = return_to; | 137 | p["return_to"] = return_to; |
147 | try { | 138 | try { |
148 | try { | 139 | try { |
149 | string ah = find_assoc(server)->handle(); | 140 | string ah = find_assoc(server)->handle(); |
150 | p["assoc_handle"] = ah; | 141 | p["assoc_handle"] = ah; |
151 | }catch(failed_lookup& fl) { | 142 | }catch(failed_lookup& fl) { |
152 | string ah = associate(server)->handle(); | 143 | string ah = associate(server)->handle(); |
153 | p["assoc_handle"] = ah; | 144 | p["assoc_handle"] = ah; |
154 | } | 145 | } |
155 | }catch(exception& e) { } | 146 | }catch(exception& e) { } |
156 | if(ext) ext->checkid_hook(p,identity); | 147 | if(ext) ext->checkid_hook(p,identity); |
157 | return p.append_query(server); | 148 | return p.append_query(server); |
158 | } | 149 | } |
159 | 150 | ||
160 | void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { | 151 | void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { |
161 | if(pin.has_param("openid.user_setup_url")) | 152 | if(pin.has_param("openid.user_setup_url")) |
162 | throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); | 153 | throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); |
163 | string server,delegate; | 154 | string server,delegate; |
164 | retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); | 155 | retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); |
165 | params_t ps; | 156 | params_t ps; |
166 | try { | 157 | try { |
167 | assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); | 158 | assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); |
168 | const string& sigenc = pin.get_param("openid.sig"); | 159 | const string& sigenc = pin.get_param("openid.sig"); |
169 | mimetic::Base64::Decoder b; | ||
170 | vector<unsigned char> sig; | 160 | vector<unsigned char> sig; |
171 | mimetic::decode( | 161 | util::decode_base64(sigenc,sig); |
172 | sigenc.begin(),sigenc.end(), b, | ||
173 | back_insert_iterator<vector<unsigned char> >(sig) ); | ||
174 | const string& slist = pin.get_param("openid.signed"); | 162 | const string& slist = pin.get_param("openid.signed"); |
175 | string kv; | 163 | string kv; |
176 | string::size_type p = 0; | 164 | string::size_type p = 0; |
177 | while(true) { | 165 | while(true) { |
178 | string::size_type co = slist.find(',',p); | 166 | string::size_type co = slist.find(',',p); |
179 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); | 167 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); |
180 | kv += f; | 168 | kv += f; |
181 | kv += ':'; | 169 | kv += ':'; |
182 | f.insert(0,"openid."); | 170 | f.insert(0,"openid."); |
183 | kv += pin.get_param(f); | 171 | kv += pin.get_param(f); |
184 | kv += '\n'; | 172 | kv += '\n'; |
185 | if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); | 173 | if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); |
186 | if(co==string::npos) | 174 | if(co==string::npos) |
187 | break; | 175 | break; |
188 | p = co+1; | 176 | p = co+1; |
189 | } | 177 | } |
190 | secret_t secret = assoc->secret(); | 178 | secret_t secret = assoc->secret(); |
191 | unsigned int md_len = 0; | 179 | unsigned int md_len = 0; |
192 | unsigned char *md = HMAC( | 180 | unsigned char *md = HMAC( |
193 | EVP_sha1(), | 181 | EVP_sha1(), |
194 | &(secret.front()),secret.size(), | 182 | &(secret.front()),secret.size(), |
195 | (const unsigned char *)kv.data(),kv.length(), | 183 | (const unsigned char *)kv.data(),kv.length(), |
196 | 0,&md_len); | 184 | 0,&md_len); |
197 | if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) | 185 | if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) |
198 | throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); | 186 | throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); |
199 | }catch(failed_lookup& e) { /* XXX: more specific? */ | 187 | }catch(failed_lookup& e) { /* XXX: more specific? */ |
200 | const string& slist = pin.get_param("openid.signed"); | 188 | const string& slist = pin.get_param("openid.signed"); |
201 | string::size_type pp = 0; | 189 | string::size_type pp = 0; |
202 | params_t p; | 190 | params_t p; |
203 | while(true) { | 191 | while(true) { |
204 | string::size_type co = slist.find(',',pp); | 192 | string::size_type co = slist.find(',',pp); |
205 | string f = "openid."; | 193 | string f = "openid."; |
206 | f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); | 194 | f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); |
207 | p[f] = pin.get_param(f); | 195 | p[f] = pin.get_param(f); |
208 | if(co==string::npos) | 196 | if(co==string::npos) |
209 | break; | 197 | break; |
210 | pp = co+1; | 198 | pp = co+1; |
211 | } | 199 | } |
212 | p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); | 200 | p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); |
213 | p["openid.sig"] = pin.get_param("openid.sig"); | 201 | p["openid.sig"] = pin.get_param("openid.sig"); |
214 | p["openid.signed"] = pin.get_param("openid.signed"); | 202 | p["openid.signed"] = pin.get_param("openid.signed"); |
215 | try { | 203 | try { |
216 | string ih = pin.get_param("openid.invalidate_handle"); | 204 | string ih = pin.get_param("openid.invalidate_handle"); |
217 | p["openid.invalidate_handle"] = ih; | 205 | p["openid.invalidate_handle"] = ih; |
218 | }catch(failed_lookup& fl) { } | 206 | }catch(failed_lookup& fl) { } |
219 | try { | 207 | try { |
220 | check_authentication(server,p); | 208 | check_authentication(server,p); |
221 | }catch(failed_check_authentication& fca) { | 209 | }catch(failed_check_authentication& fca) { |