summaryrefslogtreecommitdiffabout
path: root/lib
Unidiff
Diffstat (limited to 'lib') (more/less context) (ignore whitespace changes)
-rw-r--r--lib/Makefile.am3
-rw-r--r--lib/consumer.cc16
-rw-r--r--lib/extension.cc15
-rw-r--r--lib/server.cc14
4 files changed, 35 insertions, 13 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 6f3f9f3..69c749e 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,24 +1,25 @@
1lib_LTLIBRARIES = libopkele.la 1lib_LTLIBRARIES = libopkele.la
2 2
3INCLUDES = \ 3INCLUDES = \
4 -I${top_srcdir}/include/ \ 4 -I${top_srcdir}/include/ \
5 ${KONFORKA_CFLAGS} \ 5 ${KONFORKA_CFLAGS} \
6 ${OPENSSL_CFLAGS} \ 6 ${OPENSSL_CFLAGS} \
7 ${MIMETIC_CFLAGS} \ 7 ${MIMETIC_CFLAGS} \
8 ${LIBCURL_CPPFLAGS} \ 8 ${LIBCURL_CPPFLAGS} \
9 ${PCREPP_CFLAGS} 9 ${PCREPP_CFLAGS}
10LDADD = \ 10LDADD = \
11 ${LIBCURL} \ 11 ${LIBCURL} \
12 ${PCREPP_LIBS} \ 12 ${PCREPP_LIBS} \
13 ${MIMETIC_LIBS} \ 13 ${MIMETIC_LIBS} \
14 ${OPENSSL_LIBS} \ 14 ${OPENSSL_LIBS} \
15 ${KONFORKA_LIBS} 15 ${KONFORKA_LIBS}
16 16
17libopkele_la_SOURCES = \ 17libopkele_la_SOURCES = \
18 params.cc \ 18 params.cc \
19 util.cc \ 19 util.cc \
20 server.cc \ 20 server.cc \
21 secret.cc \ 21 secret.cc \
22 data.cc \ 22 data.cc \
23 consumer.cc \ 23 consumer.cc \
24 exception.cc 24 exception.cc \
25 extension.cc
diff --git a/lib/consumer.cc b/lib/consumer.cc
index bb6358c..10c2fa0 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -1,348 +1,352 @@
1#include <algorithm> 1#include <algorithm>
2#include <opkele/util.h> 2#include <opkele/util.h>
3#include <opkele/exception.h> 3#include <opkele/exception.h>
4#include <opkele/data.h> 4#include <opkele/data.h>
5#include <opkele/consumer.h> 5#include <opkele/consumer.h>
6#include <openssl/sha.h> 6#include <openssl/sha.h>
7#include <openssl/hmac.h> 7#include <openssl/hmac.h>
8#include <mimetic/mimetic.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 */ 14/* silly mimetic */
15#undef PACKAGE 15#undef PACKAGE
16#undef PACKAGE_BUGREPORT 16#undef PACKAGE_BUGREPORT
17#undef PACKAGE_NAME 17#undef PACKAGE_NAME
18#undef PACKAGE_STRING 18#undef PACKAGE_STRING
19#undef PACKAGE_TARNAME 19#undef PACKAGE_TARNAME
20#undef PACKAGE_VERSION 20#undef PACKAGE_VERSION
21#undef VERSION 21#undef VERSION
22 22
23#include "config.h" 23#include "config.h"
24 24
25namespace opkele { 25namespace opkele {
26 using namespace std; 26 using namespace std;
27 27
28 class curl_t { 28 class curl_t {
29 public: 29 public:
30 CURL *_c; 30 CURL *_c;
31 31
32 curl_t() : _c(0) { } 32 curl_t() : _c(0) { }
33 curl_t(CURL *c) : _c(c) { } 33 curl_t(CURL *c) : _c(c) { }
34 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); } 34 ~curl_t() throw() { if(_c) curl_easy_cleanup(_c); }
35 35
36 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; } 36 curl_t& operator=(CURL *c) { if(_c) curl_easy_cleanup(_c); _c=c; return *this; }
37 37
38 operator const CURL*(void) const { return _c; } 38 operator const CURL*(void) const { return _c; }
39 operator CURL*(void) { return _c; } 39 operator CURL*(void) { return _c; }
40 }; 40 };
41 41
42 static CURLcode curl_misc_sets(CURL* c) { 42 static CURLcode curl_misc_sets(CURL* c) {
43 CURLcode r; 43 CURLcode r;
44 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1)) 44 (r=curl_easy_setopt(c,CURLOPT_FOLLOWLOCATION,1))
45 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5)) 45 || (r=curl_easy_setopt(c,CURLOPT_MAXREDIRS,5))
46 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120)) 46 || (r=curl_easy_setopt(c,CURLOPT_DNS_CACHE_TIMEOUT,120))
47 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1)) 47 || (r=curl_easy_setopt(c,CURLOPT_DNS_USE_GLOBAL_CACHE,1))
48 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION)) 48 || (r=curl_easy_setopt(c,CURLOPT_USERAGENT,PACKAGE_NAME"/"PACKAGE_VERSION))
49 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20)) 49 || (r=curl_easy_setopt(c,CURLOPT_TIMEOUT,20))
50 ; 50 ;
51 return r; 51 return r;
52 } 52 }
53 53
54 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) { 54 static size_t _curl_tostring(void *ptr,size_t size,size_t nmemb,void *stream) {
55 string *str = (string*)stream; 55 string *str = (string*)stream;
56 size_t bytes = size*nmemb; 56 size_t bytes = size*nmemb;
57 size_t get = min(16384-str->length(),bytes); 57 size_t get = min(16384-str->length(),bytes);
58 str->append((const char*)ptr,get); 58 str->append((const char*)ptr,get);
59 return get; 59 return get;
60 } 60 }
61 61
62 assoc_t consumer_t::associate(const string& server) { 62 assoc_t consumer_t::associate(const string& server) {
63 util::dh_t dh = DH_new(); 63 util::dh_t dh = DH_new();
64 if(!dh) 64 if(!dh)
65 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 65 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
66 dh->p = util::dec_to_bignum(data::_default_p); 66 dh->p = util::dec_to_bignum(data::_default_p);
67 dh->g = util::dec_to_bignum(data::_default_g); 67 dh->g = util::dec_to_bignum(data::_default_g);
68 if(!DH_generate_key(dh)) 68 if(!DH_generate_key(dh))
69 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 69 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
70 string request = 70 string request =
71 "openid.mode=associate" 71 "openid.mode=associate"
72 "&openid.assoc_type=HMAC-SHA1" 72 "&openid.assoc_type=HMAC-SHA1"
73 "&openid.session_type=DH-SHA1" 73 "&openid.session_type=DH-SHA1"
74 "&openid.dh_consumer_public="; 74 "&openid.dh_consumer_public=";
75 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 75 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
76 curl_t curl = curl_easy_init(); 76 curl_t curl = curl_easy_init();
77 if(!curl) 77 if(!curl)
78 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 78 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
79 string response; 79 string response;
80 CURLcode r; 80 CURLcode r;
81 (r=curl_misc_sets(curl)) 81 (r=curl_misc_sets(curl))
82 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 82 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
83 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 83 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
84 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 84 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
85 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 85 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
86 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 86 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
87 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 87 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
88 ; 88 ;
89 if(r) 89 if(r)
90 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 90 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
91 if(r=curl_easy_perform(curl)) 91 if(r=curl_easy_perform(curl))
92 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 92 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
93 params_t p; p.parse_keyvalues(response); 93 params_t p; p.parse_keyvalues(response);
94 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 94 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
95 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 95 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
96 string st; 96 string st;
97 if(p.has_param("session_type")) st = p.get_param("session_type"); 97 if(p.has_param("session_type")) st = p.get_param("session_type");
98 if((!st.empty()) && st!="DH-SHA1") 98 if((!st.empty()) && st!="DH-SHA1")
99 throw bad_input(OPKELE_CP_ "unsupported session_type"); 99 throw bad_input(OPKELE_CP_ "unsupported session_type");
100 secret_t secret; 100 secret_t secret;
101 if(st.empty()) { 101 if(st.empty()) {
102 secret.from_base64(p.get_param("mac_key")); 102 secret.from_base64(p.get_param("mac_key"));
103 }else{ 103 }else{
104 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 104 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
105 vector<unsigned char> ck(DH_size(dh)); 105 vector<unsigned char> ck(DH_size(dh));
106 int cklen = DH_compute_key(&(ck.front()),s_pub,dh); 106 int cklen = DH_compute_key(&(ck.front()),s_pub,dh);
107 if(cklen<0) 107 if(cklen<0)
108 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 108 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
109 ck.resize(cklen); 109 ck.resize(cklen);
110 // OpenID algorithm requires extra zero in case of set bit here 110 // OpenID algorithm requires extra zero in case of set bit here
111 if(ck[0]&0x80) ck.insert(ck.begin(),1,0); 111 if(ck[0]&0x80) ck.insert(ck.begin(),1,0);
112 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 112 unsigned char key_sha1[SHA_DIGEST_LENGTH];
113 SHA1(&(ck.front()),ck.size(),key_sha1); 113 SHA1(&(ck.front()),ck.size(),key_sha1);
114 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 114 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
115 } 115 }
116 int expires_in = 0; 116 int expires_in = 0;
117 if(p.has_param("expires_in")) { 117 if(p.has_param("expires_in")) {
118 expires_in = util::string_to_long(p.get_param("expires_in")); 118 expires_in = util::string_to_long(p.get_param("expires_in"));
119 }else if(p.has_param("issued") && p.has_param("expiry")) { 119 }else if(p.has_param("issued") && p.has_param("expiry")) {
120 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 120 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
121 }else 121 }else
122 throw bad_input(OPKELE_CP_ "no expiration information"); 122 throw bad_input(OPKELE_CP_ "no expiration information");
123 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 123 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
124 } 124 }
125 125
126 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root) { 126 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); 127 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
128 } 128 }
129 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root) { 129 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); 130 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
131 } 131 }
132 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root) { 132 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; 133 params_t p;
134 if(mode==mode_checkid_immediate) 134 if(mode==mode_checkid_immediate)
135 p["mode"]="checkid_immediate"; 135 p["mode"]="checkid_immediate";
136 else if(mode==mode_checkid_setup) 136 else if(mode==mode_checkid_setup)
137 p["mode"]="checkid_setup"; 137 p["mode"]="checkid_setup";
138 else 138 else
139 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 139 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
140 string iurl = canonicalize(identity); 140 string iurl = canonicalize(identity);
141 string server, delegate; 141 string server, delegate;
142 retrieve_links(iurl,server,delegate); 142 retrieve_links(iurl,server,delegate);
143 p["identity"] = delegate.empty()?iurl:delegate; 143 p["identity"] = delegate.empty()?iurl:delegate;
144 if(!trust_root.empty()) 144 if(!trust_root.empty())
145 p["trust_root"] = trust_root; 145 p["trust_root"] = trust_root;
146 p["return_to"] = return_to; 146 p["return_to"] = return_to;
147 try { 147 try {
148 try { 148 try {
149 string ah = find_assoc(server)->handle(); 149 string ah = find_assoc(server)->handle();
150 p["assoc_handle"] = ah; 150 p["assoc_handle"] = ah;
151 }catch(failed_lookup& fl) { 151 }catch(failed_lookup& fl) {
152 string ah = associate(server)->handle(); 152 string ah = associate(server)->handle();
153 p["assoc_handle"] = ah; 153 p["assoc_handle"] = ah;
154 } 154 }
155 }catch(exception& e) { } 155 }catch(exception& e) { }
156 if(ext) ext->checkid_hook(p,identity);
156 return p.append_query(server); 157 return p.append_query(server);
157 } 158 }
158 159
159 void consumer_t::id_res(const params_t& pin,const string& identity) { 160 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
160 if(pin.has_param("openid.user_setup_url")) 161 if(pin.has_param("openid.user_setup_url"))
161 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 162 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
162 string server,delegate; 163 string server,delegate;
163 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 164 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
165 params_t ps;
164 try { 166 try {
165 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 167 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
166 const string& sigenc = pin.get_param("openid.sig"); 168 const string& sigenc = pin.get_param("openid.sig");
167 mimetic::Base64::Decoder b; 169 mimetic::Base64::Decoder b;
168 vector<unsigned char> sig; 170 vector<unsigned char> sig;
169 mimetic::decode( 171 mimetic::decode(
170 sigenc.begin(),sigenc.end(), b, 172 sigenc.begin(),sigenc.end(), b,
171 back_insert_iterator<vector<unsigned char> >(sig) ); 173 back_insert_iterator<vector<unsigned char> >(sig) );
172 const string& slist = pin.get_param("openid.signed"); 174 const string& slist = pin.get_param("openid.signed");
173 string kv; 175 string kv;
174 string::size_type p = 0; 176 string::size_type p = 0;
175 while(true) { 177 while(true) {
176 string::size_type co = slist.find(',',p); 178 string::size_type co = slist.find(',',p);
177 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 179 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
178 kv += f; 180 kv += f;
179 kv += ':'; 181 kv += ':';
180 f.insert(0,"openid."); 182 f.insert(0,"openid.");
181 kv += pin.get_param(f); 183 kv += pin.get_param(f);
182 kv += '\n'; 184 kv += '\n';
185 if(ext) ps[f.substr(sizeof("openid."))] = pin.get_param(f);
183 if(co==string::npos) 186 if(co==string::npos)
184 break; 187 break;
185 p = co+1; 188 p = co+1;
186 } 189 }
187 secret_t secret = assoc->secret(); 190 secret_t secret = assoc->secret();
188 unsigned int md_len = 0; 191 unsigned int md_len = 0;
189 unsigned char *md = HMAC( 192 unsigned char *md = HMAC(
190 EVP_sha1(), 193 EVP_sha1(),
191 &(secret.front()),secret.size(), 194 &(secret.front()),secret.size(),
192 (const unsigned char *)kv.data(),kv.length(), 195 (const unsigned char *)kv.data(),kv.length(),
193 0,&md_len); 196 0,&md_len);
194 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 197 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
195 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 198 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
196 }catch(failed_lookup& e) { /* XXX: more specific? */ 199 }catch(failed_lookup& e) { /* XXX: more specific? */
197 const string& slist = pin.get_param("openid.signed"); 200 const string& slist = pin.get_param("openid.signed");
198 string::size_type pp = 0; 201 string::size_type pp = 0;
199 params_t p; 202 params_t p;
200 while(true) { 203 while(true) {
201 string::size_type co = slist.find(',',pp); 204 string::size_type co = slist.find(',',pp);
202 string f = "openid."; 205 string f = "openid.";
203 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 206 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
204 p[f] = pin.get_param(f); 207 p[f] = pin.get_param(f);
205 if(co==string::npos) 208 if(co==string::npos)
206 break; 209 break;
207 pp = co+1; 210 pp = co+1;
208 } 211 }
209 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 212 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
210 p["openid.sig"] = pin.get_param("openid.sig"); 213 p["openid.sig"] = pin.get_param("openid.sig");
211 p["openid.signed"] = pin.get_param("openid.signed"); 214 p["openid.signed"] = pin.get_param("openid.signed");
212 try { 215 try {
213 string ih = pin.get_param("openid.invalidate_handle"); 216 string ih = pin.get_param("openid.invalidate_handle");
214 p["openid.invalidate_handle"] = ih; 217 p["openid.invalidate_handle"] = ih;
215 }catch(failed_lookup& fl) { } 218 }catch(failed_lookup& fl) { }
216 try { 219 try {
217 check_authentication(server,p); 220 check_authentication(server,p);
218 }catch(failed_check_authentication& fca) { 221 }catch(failed_check_authentication& fca) {
219 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 222 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
220 } 223 }
221 } 224 }
225 if(ext) ext->id_res_hook(pin,ps,identity);
222 } 226 }
223 227
224 void consumer_t::check_authentication(const string& server,const params_t& p) { 228 void consumer_t::check_authentication(const string& server,const params_t& p) {
225 string request = "openid.mode=check_authentication"; 229 string request = "openid.mode=check_authentication";
226 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 230 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
227 if(i->first!="openid.mode") { 231 if(i->first!="openid.mode") {
228 request += '&'; 232 request += '&';
229 request += i->first; 233 request += i->first;
230 request += '='; 234 request += '=';
231 request += util::url_encode(i->second); 235 request += util::url_encode(i->second);
232 } 236 }
233 } 237 }
234 curl_t curl = curl_easy_init(); 238 curl_t curl = curl_easy_init();
235 if(!curl) 239 if(!curl)
236 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 240 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
237 string response; 241 string response;
238 CURLcode r; 242 CURLcode r;
239 (r=curl_misc_sets(curl)) 243 (r=curl_misc_sets(curl))
240 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) 244 || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str()))
241 || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) 245 || (r=curl_easy_setopt(curl,CURLOPT_POST,1))
242 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) 246 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data()))
243 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) 247 || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length()))
244 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 248 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
245 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) 249 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response))
246 ; 250 ;
247 if(r) 251 if(r)
248 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 252 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
249 if(r=curl_easy_perform(curl)) 253 if(r=curl_easy_perform(curl))
250 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 254 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
251 params_t pp; pp.parse_keyvalues(response); 255 params_t pp; pp.parse_keyvalues(response);
252 if(pp.has_param("invalidate_handle")) 256 if(pp.has_param("invalidate_handle"))
253 invalidate_assoc(server,pp.get_param("invalidate_handle")); 257 invalidate_assoc(server,pp.get_param("invalidate_handle"));
254 if(pp.has_param("is_valid")) { 258 if(pp.has_param("is_valid")) {
255 if(pp.get_param("is_valid")=="true") 259 if(pp.get_param("is_valid")=="true")
256 return; 260 return;
257 }else if(pp.has_param("lifetime")) { 261 }else if(pp.has_param("lifetime")) {
258 if(util::string_to_long(pp.get_param("lifetime"))) 262 if(util::string_to_long(pp.get_param("lifetime")))
259 return; 263 return;
260 } 264 }
261 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 265 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
262 } 266 }
263 267
264 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 268 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
265 server.erase(); 269 server.erase();
266 delegate.erase(); 270 delegate.erase();
267 curl_t curl = curl_easy_init(); 271 curl_t curl = curl_easy_init();
268 if(!curl) 272 if(!curl)
269 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); 273 throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()");
270 string html; 274 string html;
271 CURLcode r; 275 CURLcode r;
272 (r=curl_misc_sets(curl)) 276 (r=curl_misc_sets(curl))
273 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) 277 || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str()))
274 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) 278 || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring))
275 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) 279 || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html))
276 ; 280 ;
277 if(r) 281 if(r)
278 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); 282 throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r);
279 r = curl_easy_perform(curl); 283 r = curl_easy_perform(curl);
280 if(r && r!=CURLE_WRITE_ERROR) 284 if(r && r!=CURLE_WRITE_ERROR)
281 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); 285 throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r);
282 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); 286 pcrepp::Pcre bre("<body\\b",PCRE_CASELESS);
283 // strip out everything past body 287 // strip out everything past body
284 if(bre.search(html)) 288 if(bre.search(html))
285 html.erase(bre.get_match_start()); 289 html.erase(bre.get_match_start());
286 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS); 290 pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS);
287 if(!hdre.search(html)) 291 if(!hdre.search(html))
288 throw bad_input(OPKELE_CP_ "failed to find head"); 292 throw bad_input(OPKELE_CP_ "failed to find head");
289 html.erase(0,hdre.get_match_end()+1); 293 html.erase(0,hdre.get_match_end()+1);
290 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS), 294 pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS),
291 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS), 295 rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS),
292 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS); 296 hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS);
293 while(lre.search(html)) { 297 while(lre.search(html)) {
294 string attrs = lre[0]; 298 string attrs = lre[0];
295 html.erase(0,lre.get_match_end()+1); 299 html.erase(0,lre.get_match_end()+1);
296 if(!(rre.search(attrs)&&hre.search(attrs))) 300 if(!(rre.search(attrs)&&hre.search(attrs)))
297 continue; 301 continue;
298 if(rre[0]=="openid.server") { 302 if(rre[0]=="openid.server") {
299 server = hre[0]; 303 server = hre[0];
300 if(!delegate.empty()) 304 if(!delegate.empty())
301 break; 305 break;
302 }else if(rre[0]=="openid.delegate") { 306 }else if(rre[0]=="openid.delegate") {
303 delegate = hre[0]; 307 delegate = hre[0];
304 if(!server.empty()) 308 if(!server.empty())
305 break; 309 break;
306 } 310 }
307 } 311 }
308 if(server.empty()) 312 if(server.empty())
309 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); 313 throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration");
310 } 314 }
311 315
312 assoc_t consumer_t::find_assoc(const string& server) { 316 assoc_t consumer_t::find_assoc(const string& server) {
313 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); 317 throw failed_lookup(OPKELE_CP_ "no find_assoc() provided");
314 } 318 }
315 319
316 string consumer_t::canonicalize(const string& url) { 320 string consumer_t::canonicalize(const string& url) {
317 string rv = url; 321 string rv = url;
318 // strip leading and trailing spaces 322 // strip leading and trailing spaces
319 string::size_type i = rv.find_first_not_of(" \t\r\n"); 323 string::size_type i = rv.find_first_not_of(" \t\r\n");
320 if(i==string::npos) 324 if(i==string::npos)
321 throw bad_input(OPKELE_CP_ "empty URL"); 325 throw bad_input(OPKELE_CP_ "empty URL");
322 if(i) 326 if(i)
323 rv.erase(0,i); 327 rv.erase(0,i);
324 i = rv.find_last_not_of(" \t\r\n"); 328 i = rv.find_last_not_of(" \t\r\n");
325 assert(i!=string::npos); 329 assert(i!=string::npos);
326 if(i<(rv.length()-1)) 330 if(i<(rv.length()-1))
327 rv.erase(i+1); 331 rv.erase(i+1);
328 // add missing http:// 332 // add missing http://
329 i = rv.find("://"); 333 i = rv.find("://");
330 if(i==string::npos) { // primitive. but do we need more? 334 if(i==string::npos) { // primitive. but do we need more?
331 rv.insert(0,"http://"); 335 rv.insert(0,"http://");
332 i = sizeof("http://")-1; 336 i = sizeof("http://")-1;
333 }else{ 337 }else{
334 i += sizeof("://")-1; 338 i += sizeof("://")-1;
335 } 339 }
336 string::size_type qm = rv.find('?',i); 340 string::size_type qm = rv.find('?',i);
337 string::size_type sl = rv.find('/',i); 341 string::size_type sl = rv.find('/',i);
338 if(qm!=string::npos) { 342 if(qm!=string::npos) {
339 if(sl==string::npos || sl>qm) 343 if(sl==string::npos || sl>qm)
340 rv.insert(qm,1,'/'); 344 rv.insert(qm,1,'/');
341 }else{ 345 }else{
342 if(sl==string::npos) 346 if(sl==string::npos)
343 rv += '/'; 347 rv += '/';
344 } 348 }
345 return rv; 349 return rv;
346 } 350 }
347 351
348} 352}
diff --git a/lib/extension.cc b/lib/extension.cc
new file mode 100644
index 0000000..bd2195d
--- a/dev/null
+++ b/lib/extension.cc
@@ -0,0 +1,15 @@
1#include <opkele/exception.h>
2#include <opkele/extension.h>
3
4namespace opkele {
5
6 void extension_t::checkid_hook(params_t& p,const string& identity) {
7 throw not_implemented(OPKELE_CP_ "Consumer checkid_hook not implemented");
8 }
9 void id_res_hook(const params_t& p,const params_t& sp,const string& identity) {
10 throw not_implemented(OPKELE_CP_ "Consumer id_res_hook not implemented");
11 }
12 void checkid_hook(const params_t& pin,params_t& pout) {
13 throw not_implemented(OPKELE_CP_ "Server checkid_hook not implemented");
14 }
15}
diff --git a/lib/server.cc b/lib/server.cc
index 5eee1f3..8c29abb 100644
--- a/lib/server.cc
+++ b/lib/server.cc
@@ -1,171 +1,173 @@
1#include <vector> 1#include <vector>
2#include <openssl/sha.h> 2#include <openssl/sha.h>
3#include <openssl/hmac.h> 3#include <openssl/hmac.h>
4#include <mimetic/mimetic.h> 4#include <mimetic/mimetic.h>
5#include <opkele/util.h> 5#include <opkele/util.h>
6#include <opkele/exception.h> 6#include <opkele/exception.h>
7#include <opkele/server.h> 7#include <opkele/server.h>
8#include <opkele/data.h> 8#include <opkele/data.h>
9 9
10namespace opkele { 10namespace opkele {
11 using namespace std; 11 using namespace std;
12 12
13 void server_t::associate(const params_t& pin,params_t& pout) { 13 void server_t::associate(const params_t& pin,params_t& pout) {
14 util::dh_t dh; 14 util::dh_t dh;
15 util::bignum_t c_pub; 15 util::bignum_t c_pub;
16 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 16 unsigned char key_sha1[SHA_DIGEST_LENGTH];
17 enum { 17 enum {
18 sess_cleartext, 18 sess_cleartext,
19 sess_dh_sha1 19 sess_dh_sha1
20 } st = sess_cleartext; 20 } st = sess_cleartext;
21 if( 21 if(
22 pin.has_param("openid.session_type") 22 pin.has_param("openid.session_type")
23 && pin.get_param("openid.session_type")=="DH-SHA1" ) { 23 && pin.get_param("openid.session_type")=="DH-SHA1" ) {
24 /* TODO: fallback to cleartext in case of exceptions here? */ 24 /* TODO: fallback to cleartext in case of exceptions here? */
25 if(!(dh = DH_new())) 25 if(!(dh = DH_new()))
26 throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); 26 throw exception_openssl(OPKELE_CP_ "failed to DH_new()");
27 c_pub = util::base64_to_bignum(pin.get_param("openid.dh_consumer_public")); 27 c_pub = util::base64_to_bignum(pin.get_param("openid.dh_consumer_public"));
28 if(pin.has_param("openid.dh_modulus")) 28 if(pin.has_param("openid.dh_modulus"))
29 dh->p = util::base64_to_bignum(pin.get_param("openid.dh_modulus")); 29 dh->p = util::base64_to_bignum(pin.get_param("openid.dh_modulus"));
30 else 30 else
31 dh->p = util::dec_to_bignum(data::_default_p); 31 dh->p = util::dec_to_bignum(data::_default_p);
32 if(pin.has_param("openid.dh_gen")) 32 if(pin.has_param("openid.dh_gen"))
33 dh->g = util::base64_to_bignum(pin.get_param("openid.dh_gen")); 33 dh->g = util::base64_to_bignum(pin.get_param("openid.dh_gen"));
34 else 34 else
35 dh->g = util::dec_to_bignum(data::_default_g); 35 dh->g = util::dec_to_bignum(data::_default_g);
36 if(!DH_generate_key(dh)) 36 if(!DH_generate_key(dh))
37 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 37 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
38 vector<unsigned char> ck(DH_size(dh)); 38 vector<unsigned char> ck(DH_size(dh));
39 int cklen = DH_compute_key(&(ck.front()),c_pub,dh); 39 int cklen = DH_compute_key(&(ck.front()),c_pub,dh);
40 if(cklen<0) 40 if(cklen<0)
41 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 41 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
42 ck.resize(cklen); 42 ck.resize(cklen);
43 // OpenID algorithm requires extra zero in case of set bit here 43 // OpenID algorithm requires extra zero in case of set bit here
44 if(ck[0]&0x80) ck.insert(ck.begin(),1,0); 44 if(ck[0]&0x80) ck.insert(ck.begin(),1,0);
45 SHA1(&(ck.front()),ck.size(),key_sha1); 45 SHA1(&(ck.front()),ck.size(),key_sha1);
46 st = sess_dh_sha1; 46 st = sess_dh_sha1;
47 } 47 }
48 assoc_t assoc = alloc_assoc(mode_associate); 48 assoc_t assoc = alloc_assoc(mode_associate);
49 time_t now = time(0); 49 time_t now = time(0);
50 pout.clear(); 50 pout.clear();
51 pout["assoc_type"] = assoc->assoc_type(); 51 pout["assoc_type"] = assoc->assoc_type();
52 pout["assoc_handle"] = assoc->handle(); 52 pout["assoc_handle"] = assoc->handle();
53 /* TODO: eventually remove deprecated stuff */ 53 /* TODO: eventually remove deprecated stuff */
54 pout["issued"] = util::time_to_w3c(now); 54 pout["issued"] = util::time_to_w3c(now);
55 pout["expiry"] = util::time_to_w3c(now+assoc->expires_in()); 55 pout["expiry"] = util::time_to_w3c(now+assoc->expires_in());
56 pout["expires_in"] = util::long_to_string(assoc->expires_in()); 56 pout["expires_in"] = util::long_to_string(assoc->expires_in());
57 secret_t secret = assoc->secret(); 57 secret_t secret = assoc->secret();
58 switch(st) { 58 switch(st) {
59 case sess_dh_sha1: 59 case sess_dh_sha1:
60 pout["session_type"] = "DH-SHA1"; 60 pout["session_type"] = "DH-SHA1";
61 pout["dh_server_public"] = util::bignum_to_base64(dh->pub_key); 61 pout["dh_server_public"] = util::bignum_to_base64(dh->pub_key);
62 secret.enxor_to_base64(key_sha1,pout["enc_mac_key"]); 62 secret.enxor_to_base64(key_sha1,pout["enc_mac_key"]);
63 break; 63 break;
64 default: 64 default:
65 secret.to_base64(pout["mac_key"]); 65 secret.to_base64(pout["mac_key"]);
66 break; 66 break;
67 } 67 }
68 } 68 }
69 69
70 void server_t::checkid_immediate(const params_t& pin,string& return_to,params_t& pout) { 70 void server_t::checkid_immediate(const params_t& pin,string& return_to,params_t& pout,extension_t *ext) {
71 checkid_(mode_checkid_immediate,pin,return_to,pout); 71 checkid_(mode_checkid_immediate,pin,return_to,pout,ext);
72 } 72 }
73 73
74 void server_t::checkid_setup(const params_t& pin,string& return_to,params_t& pout) { 74 void server_t::checkid_setup(const params_t& pin,string& return_to,params_t& pout,extension_t *ext) {
75 checkid_(mode_checkid_setup,pin,return_to,pout); 75 checkid_(mode_checkid_setup,pin,return_to,pout,ext);
76 } 76 }
77 77
78 void server_t::checkid_(mode_t mode,const params_t& pin,string& return_to,params_t& pout) { 78 void server_t::checkid_(mode_t mode,const params_t& pin,string& return_to,params_t& pout,extension_t *ext) {
79 if(mode!=mode_checkid_immediate && mode!=mode_checkid_setup) 79 if(mode!=mode_checkid_immediate && mode!=mode_checkid_setup)
80 throw bad_input(OPKELE_CP_ "invalid checkid_* mode"); 80 throw bad_input(OPKELE_CP_ "invalid checkid_* mode");
81 pout.clear(); 81 pout.clear();
82 assoc_t assoc; 82 assoc_t assoc;
83 try { 83 try {
84 assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); 84 assoc = retrieve_assoc(pin.get_param("openid.assoc_handle"));
85 }catch(failed_lookup& fl) { 85 }catch(failed_lookup& fl) {
86 // no handle specified or no valid handle found, going dumb 86 // no handle specified or no valid handle found, going dumb
87 assoc = alloc_assoc(mode_checkid_setup); 87 assoc = alloc_assoc(mode_checkid_setup);
88 if(pin.has_param("openid.assoc_handle")) 88 if(pin.has_param("openid.assoc_handle"))
89 pout["invalidate_handle"]=pin.get_param("openid.assoc_handle"); 89 pout["invalidate_handle"]=pin.get_param("openid.assoc_handle");
90 } 90 }
91 string trust_root; 91 string trust_root;
92 try { 92 try {
93 trust_root = pin.get_param("openid.trust_root"); 93 trust_root = pin.get_param("openid.trust_root");
94 }catch(failed_lookup& fl) { } 94 }catch(failed_lookup& fl) { }
95 string identity = pin.get_param("openid.identity"); 95 string identity = pin.get_param("openid.identity");
96 return_to = pin.get_param("openid.return_to"); 96 return_to = pin.get_param("openid.return_to");
97 validate(*assoc,pin,identity,trust_root); 97 validate(*assoc,pin,identity,trust_root);
98 pout["mode"] = "id_res"; 98 pout["mode"] = "id_res";
99 pout["assoc_handle"] = assoc->handle(); 99 pout["assoc_handle"] = assoc->handle();
100 if(pin.has_param("openid.assoc_handle") && assoc->stateless()) 100 if(pin.has_param("openid.assoc_handle") && assoc->stateless())
101 pout["invalidate_handle"] = pin.get_param("openid.assoc_handle"); 101 pout["invalidate_handle"] = pin.get_param("openid.assoc_handle");
102 pout["identity"] = identity; 102 pout["identity"] = identity;
103 pout["return_to"] = return_to; 103 pout["return_to"] = return_to;
104 /* TODO: eventually remove deprecated stuff */ 104 /* TODO: eventually remove deprecated stuff */
105 time_t now = time(0); 105 time_t now = time(0);
106 pout["issued"] = util::time_to_w3c(now); 106 pout["issued"] = util::time_to_w3c(now);
107 pout["valid_to"] = util::time_to_w3c(now+120); 107 pout["valid_to"] = util::time_to_w3c(now+120);
108 pout["exipres_in"] = "120"; 108 pout["exipres_in"] = "120";
109 pout.sign(assoc->secret(),pout["sig"],pout["signed"]="mode,identity,return_to"); 109 pout["signed"]="mode,identity,return_to";
110 if(ext) ext->checkid_hook(pin,pout);
111 pout.sign(assoc->secret(),pout["sig"],pout["signed"]);
110 } 112 }
111 113
112 void server_t::check_authentication(const params_t& pin,params_t& pout) { 114 void server_t::check_authentication(const params_t& pin,params_t& pout) {
113 vector<unsigned char> sig; 115 vector<unsigned char> sig;
114 mimetic::Base64::Decoder b; 116 mimetic::Base64::Decoder b;
115 const string& sigenc = pin.get_param("openid.sig"); 117 const string& sigenc = pin.get_param("openid.sig");
116 mimetic::decode( 118 mimetic::decode(
117 sigenc.begin(),sigenc.end(), b, 119 sigenc.begin(),sigenc.end(), b,
118 back_insert_iterator<vector<unsigned char> >(sig)); 120 back_insert_iterator<vector<unsigned char> >(sig));
119 assoc_t assoc; 121 assoc_t assoc;
120 try { 122 try {
121 assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); 123 assoc = retrieve_assoc(pin.get_param("openid.assoc_handle"));
122 }catch(failed_lookup& fl) { 124 }catch(failed_lookup& fl) {
123 throw failed_assertion(OPKELE_CP_ "invalid handle or handle not specified"); 125 throw failed_assertion(OPKELE_CP_ "invalid handle or handle not specified");
124 } 126 }
125 if(!assoc->stateless()) 127 if(!assoc->stateless())
126 throw stateful_handle(OPKELE_CP_ "will not do check_authentication on a stateful handle"); 128 throw stateful_handle(OPKELE_CP_ "will not do check_authentication on a stateful handle");
127 const string& slist = pin.get_param("openid.signed"); 129 const string& slist = pin.get_param("openid.signed");
128 string kv; 130 string kv;
129 string::size_type p =0; 131 string::size_type p =0;
130 while(true) { 132 while(true) {
131 string::size_type co = slist.find(',',p); 133 string::size_type co = slist.find(',',p);
132 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 134 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
133 kv += f; 135 kv += f;
134 kv += ':'; 136 kv += ':';
135 if(f=="mode") 137 if(f=="mode")
136 kv += "id_res"; 138 kv += "id_res";
137 else { 139 else {
138 f.insert(0,"openid."); 140 f.insert(0,"openid.");
139 kv += pin.get_param(f); 141 kv += pin.get_param(f);
140 } 142 }
141 kv += '\n'; 143 kv += '\n';
142 if(co==string::npos) 144 if(co==string::npos)
143 break; 145 break;
144 p = co+1; 146 p = co+1;
145 } 147 }
146 secret_t secret = assoc->secret(); 148 secret_t secret = assoc->secret();
147 unsigned int md_len = 0; 149 unsigned int md_len = 0;
148 unsigned char *md = HMAC( 150 unsigned char *md = HMAC(
149 EVP_sha1(), 151 EVP_sha1(),
150 &(secret.front()),secret.size(), 152 &(secret.front()),secret.size(),
151 (const unsigned char *)kv.data(),kv.length(), 153 (const unsigned char *)kv.data(),kv.length(),
152 0,&md_len); 154 0,&md_len);
153 pout.clear(); 155 pout.clear();
154 if(sig.size()==md_len && !memcmp(&(sig.front()),md,md_len)) { 156 if(sig.size()==md_len && !memcmp(&(sig.front()),md,md_len)) {
155 pout["is_valid"]="true"; 157 pout["is_valid"]="true";
156 pout["lifetime"]="60"; /* TODO: eventually remove deprecated stuff */ 158 pout["lifetime"]="60"; /* TODO: eventually remove deprecated stuff */
157 }else{ 159 }else{
158 pout["is_valid"]="false"; 160 pout["is_valid"]="false";
159 pout["lifetime"]="0"; /* TODO: eventually remove deprecated stuff */ 161 pout["lifetime"]="0"; /* TODO: eventually remove deprecated stuff */
160 } 162 }
161 if(pin.has_param("openid.invalidate_handle")) { 163 if(pin.has_param("openid.invalidate_handle")) {
162 string h = pin.get_param("openid.invalidate_handle"); 164 string h = pin.get_param("openid.invalidate_handle");
163 try { 165 try {
164 assoc_t assoc = retrieve_assoc(h); 166 assoc_t assoc = retrieve_assoc(h);
165 }catch(invalid_handle& ih) { 167 }catch(invalid_handle& ih) {
166 pout["invalidate_handle"] = h; 168 pout["invalidate_handle"] = h;
167 }catch(failed_lookup& fl) { } 169 }catch(failed_lookup& fl) { }
168 } 170 }
169 } 171 }
170 172
171} 173}