-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/consumer.cc | 16 | ||||
-rw-r--r-- | lib/params.cc | 7 | ||||
-rw-r--r-- | lib/secret.cc | 22 | ||||
-rw-r--r-- | lib/server.cc | 6 | ||||
-rw-r--r-- | lib/util.cc | 67 |
6 files changed, 65 insertions, 55 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index bdadd44..ec63c25 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am | |||
@@ -1,29 +1,27 @@ | |||
1 | lib_LTLIBRARIES = libopkele.la | 1 | lib_LTLIBRARIES = libopkele.la |
2 | 2 | ||
3 | INCLUDES = \ | 3 | INCLUDES = \ |
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} \ | ||
8 | ${LIBCURL_CPPFLAGS} \ | 7 | ${LIBCURL_CPPFLAGS} \ |
9 | ${PCREPP_CFLAGS} | 8 | ${PCREPP_CFLAGS} |
10 | LDADD = \ | 9 | LDADD = \ |
11 | ${LIBCURL} \ | 10 | ${LIBCURL} \ |
12 | ${PCREPP_LIBS} \ | 11 | ${PCREPP_LIBS} \ |
13 | ${MIMETIC_LIBS} \ | ||
14 | ${OPENSSL_LIBS} \ | 12 | ${OPENSSL_LIBS} \ |
15 | ${KONFORKA_LIBS} | 13 | ${KONFORKA_LIBS} |
16 | 14 | ||
17 | libopkele_la_SOURCES = \ | 15 | libopkele_la_SOURCES = \ |
18 | params.cc \ | 16 | params.cc \ |
19 | util.cc \ | 17 | util.cc \ |
20 | server.cc \ | 18 | server.cc \ |
21 | secret.cc \ | 19 | secret.cc \ |
22 | data.cc \ | 20 | data.cc \ |
23 | consumer.cc \ | 21 | consumer.cc \ |
24 | exception.cc \ | 22 | exception.cc \ |
25 | extension.cc \ | 23 | extension.cc \ |
26 | sreg.cc \ | 24 | sreg.cc \ |
27 | extension_chain.cc | 25 | extension_chain.cc |
28 | libopkele_la_LDFLAGS = \ | 26 | libopkele_la_LDFLAGS = \ |
29 | -version-info 1:0:0 | 27 | -version-info 1:0:0 |
diff --git a/lib/consumer.cc b/lib/consumer.cc index b215aa8..331b1e9 100644 --- a/lib/consumer.cc +++ b/lib/consumer.cc | |||
@@ -1,352 +1,340 @@ | |||
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 = |
71 | "openid.mode=associate" | 62 | "openid.mode=associate" |
72 | "&openid.assoc_type=HMAC-SHA1" | 63 | "&openid.assoc_type=HMAC-SHA1" |
73 | "&openid.session_type=DH-SHA1" | 64 | "&openid.session_type=DH-SHA1" |
74 | "&openid.dh_consumer_public="; | 65 | "&openid.dh_consumer_public="; |
75 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); | 66 | request += util::url_encode(util::bignum_to_base64(dh->pub_key)); |
76 | curl_t curl = curl_easy_init(); | 67 | curl_t curl = curl_easy_init(); |
77 | if(!curl) | 68 | if(!curl) |
78 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 69 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
79 | string response; | 70 | string response; |
80 | CURLcode r; | 71 | CURLcode r; |
81 | (r=curl_misc_sets(curl)) | 72 | (r=curl_misc_sets(curl)) |
82 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 73 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) |
83 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 74 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) |
84 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 75 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) |
85 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 76 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) |
86 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 77 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
87 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 78 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) |
88 | ; | 79 | ; |
89 | if(r) | 80 | if(r) |
90 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 81 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
91 | if(r=curl_easy_perform(curl)) | 82 | if(r=curl_easy_perform(curl)) |
92 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 83 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
93 | params_t p; p.parse_keyvalues(response); | 84 | params_t p; p.parse_keyvalues(response); |
94 | if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") | 85 | if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") |
95 | throw bad_input(OPKELE_CP_ "unsupported assoc_type"); | 86 | throw bad_input(OPKELE_CP_ "unsupported assoc_type"); |
96 | string st; | 87 | string st; |
97 | if(p.has_param("session_type")) st = p.get_param("session_type"); | 88 | if(p.has_param("session_type")) st = p.get_param("session_type"); |
98 | if((!st.empty()) && st!="DH-SHA1") | 89 | if((!st.empty()) && st!="DH-SHA1") |
99 | throw bad_input(OPKELE_CP_ "unsupported session_type"); | 90 | throw bad_input(OPKELE_CP_ "unsupported session_type"); |
100 | secret_t secret; | 91 | secret_t secret; |
101 | if(st.empty()) { | 92 | if(st.empty()) { |
102 | secret.from_base64(p.get_param("mac_key")); | 93 | secret.from_base64(p.get_param("mac_key")); |
103 | }else{ | 94 | }else{ |
104 | util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); | 95 | util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); |
105 | vector<unsigned char> ck(DH_size(dh)); | 96 | vector<unsigned char> ck(DH_size(dh)); |
106 | int cklen = DH_compute_key(&(ck.front()),s_pub,dh); | 97 | int cklen = DH_compute_key(&(ck.front()),s_pub,dh); |
107 | if(cklen<0) | 98 | if(cklen<0) |
108 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); | 99 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); |
109 | ck.resize(cklen); | 100 | ck.resize(cklen); |
110 | // OpenID algorithm requires extra zero in case of set bit here | 101 | // OpenID algorithm requires extra zero in case of set bit here |
111 | if(ck[0]&0x80) ck.insert(ck.begin(),1,0); | 102 | if(ck[0]&0x80) ck.insert(ck.begin(),1,0); |
112 | unsigned char key_sha1[SHA_DIGEST_LENGTH]; | 103 | unsigned char key_sha1[SHA_DIGEST_LENGTH]; |
113 | SHA1(&(ck.front()),ck.size(),key_sha1); | 104 | SHA1(&(ck.front()),ck.size(),key_sha1); |
114 | secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); | 105 | secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); |
115 | } | 106 | } |
116 | int expires_in = 0; | 107 | int expires_in = 0; |
117 | if(p.has_param("expires_in")) { | 108 | if(p.has_param("expires_in")) { |
118 | expires_in = util::string_to_long(p.get_param("expires_in")); | 109 | expires_in = util::string_to_long(p.get_param("expires_in")); |
119 | }else if(p.has_param("issued") && p.has_param("expiry")) { | 110 | }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")); | 111 | expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); |
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) { |
222 | throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); | 210 | throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); |
223 | } | 211 | } |
224 | } | 212 | } |
225 | if(ext) ext->id_res_hook(pin,ps,identity); | 213 | if(ext) ext->id_res_hook(pin,ps,identity); |
226 | } | 214 | } |
227 | 215 | ||
228 | void consumer_t::check_authentication(const string& server,const params_t& p) { | 216 | void consumer_t::check_authentication(const string& server,const params_t& p) { |
229 | string request = "openid.mode=check_authentication"; | 217 | string request = "openid.mode=check_authentication"; |
230 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { | 218 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) { |
231 | if(i->first!="openid.mode") { | 219 | if(i->first!="openid.mode") { |
232 | request += '&'; | 220 | request += '&'; |
233 | request += i->first; | 221 | request += i->first; |
234 | request += '='; | 222 | request += '='; |
235 | request += util::url_encode(i->second); | 223 | request += util::url_encode(i->second); |
236 | } | 224 | } |
237 | } | 225 | } |
238 | curl_t curl = curl_easy_init(); | 226 | curl_t curl = curl_easy_init(); |
239 | if(!curl) | 227 | if(!curl) |
240 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 228 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
241 | string response; | 229 | string response; |
242 | CURLcode r; | 230 | CURLcode r; |
243 | (r=curl_misc_sets(curl)) | 231 | (r=curl_misc_sets(curl)) |
244 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) | 232 | || (r=curl_easy_setopt(curl,CURLOPT_URL,server.c_str())) |
245 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) | 233 | || (r=curl_easy_setopt(curl,CURLOPT_POST,1)) |
246 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) | 234 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDS,request.data())) |
247 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) | 235 | || (r=curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,request.length())) |
248 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 236 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
249 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) | 237 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&response)) |
250 | ; | 238 | ; |
251 | if(r) | 239 | if(r) |
252 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 240 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
253 | if(r=curl_easy_perform(curl)) | 241 | if(r=curl_easy_perform(curl)) |
254 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 242 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
255 | params_t pp; pp.parse_keyvalues(response); | 243 | params_t pp; pp.parse_keyvalues(response); |
256 | if(pp.has_param("invalidate_handle")) | 244 | if(pp.has_param("invalidate_handle")) |
257 | invalidate_assoc(server,pp.get_param("invalidate_handle")); | 245 | invalidate_assoc(server,pp.get_param("invalidate_handle")); |
258 | if(pp.has_param("is_valid")) { | 246 | if(pp.has_param("is_valid")) { |
259 | if(pp.get_param("is_valid")=="true") | 247 | if(pp.get_param("is_valid")=="true") |
260 | return; | 248 | return; |
261 | }else if(pp.has_param("lifetime")) { | 249 | }else if(pp.has_param("lifetime")) { |
262 | if(util::string_to_long(pp.get_param("lifetime"))) | 250 | if(util::string_to_long(pp.get_param("lifetime"))) |
263 | return; | 251 | return; |
264 | } | 252 | } |
265 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); | 253 | throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); |
266 | } | 254 | } |
267 | 255 | ||
268 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { | 256 | void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { |
269 | server.erase(); | 257 | server.erase(); |
270 | delegate.erase(); | 258 | delegate.erase(); |
271 | curl_t curl = curl_easy_init(); | 259 | curl_t curl = curl_easy_init(); |
272 | if(!curl) | 260 | if(!curl) |
273 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); | 261 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_init()"); |
274 | string html; | 262 | string html; |
275 | CURLcode r; | 263 | CURLcode r; |
276 | (r=curl_misc_sets(curl)) | 264 | (r=curl_misc_sets(curl)) |
277 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) | 265 | || (r=curl_easy_setopt(curl,CURLOPT_URL,url.c_str())) |
278 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) | 266 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curl_tostring)) |
279 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) | 267 | || (r=curl_easy_setopt(curl,CURLOPT_WRITEDATA,&html)) |
280 | ; | 268 | ; |
281 | if(r) | 269 | if(r) |
282 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); | 270 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_setopt()",r); |
283 | r = curl_easy_perform(curl); | 271 | r = curl_easy_perform(curl); |
284 | if(r && r!=CURLE_WRITE_ERROR) | 272 | if(r && r!=CURLE_WRITE_ERROR) |
285 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); | 273 | throw exception_curl(OPKELE_CP_ "failed to curl_easy_perform()",r); |
286 | pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); | 274 | pcrepp::Pcre bre("<body\\b",PCRE_CASELESS); |
287 | // strip out everything past body | 275 | // strip out everything past body |
288 | if(bre.search(html)) | 276 | if(bre.search(html)) |
289 | html.erase(bre.get_match_start()); | 277 | html.erase(bre.get_match_start()); |
290 | pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS); | 278 | pcrepp::Pcre hdre("<head[^>]*>",PCRE_CASELESS); |
291 | if(!hdre.search(html)) | 279 | if(!hdre.search(html)) |
292 | throw bad_input(OPKELE_CP_ "failed to find head"); | 280 | throw bad_input(OPKELE_CP_ "failed to find head"); |
293 | html.erase(0,hdre.get_match_end()+1); | 281 | html.erase(0,hdre.get_match_end()+1); |
294 | pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS), | 282 | pcrepp::Pcre lre("<link\\b([^>]+)>",PCRE_CASELESS), |
295 | rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS), | 283 | rre("\\brel=['\"]([^'\"]+)['\"]",PCRE_CASELESS), |
296 | hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS); | 284 | hre("\\bhref=['\"]([^'\"]+)['\"]",PCRE_CASELESS); |
297 | while(lre.search(html)) { | 285 | while(lre.search(html)) { |
298 | string attrs = lre[0]; | 286 | string attrs = lre[0]; |
299 | html.erase(0,lre.get_match_end()+1); | 287 | html.erase(0,lre.get_match_end()+1); |
300 | if(!(rre.search(attrs)&&hre.search(attrs))) | 288 | if(!(rre.search(attrs)&&hre.search(attrs))) |
301 | continue; | 289 | continue; |
302 | if(rre[0]=="openid.server") { | 290 | if(rre[0]=="openid.server") { |
303 | server = hre[0]; | 291 | server = hre[0]; |
304 | if(!delegate.empty()) | 292 | if(!delegate.empty()) |
305 | break; | 293 | break; |
306 | }else if(rre[0]=="openid.delegate") { | 294 | }else if(rre[0]=="openid.delegate") { |
307 | delegate = hre[0]; | 295 | delegate = hre[0]; |
308 | if(!server.empty()) | 296 | if(!server.empty()) |
309 | break; | 297 | break; |
310 | } | 298 | } |
311 | } | 299 | } |
312 | if(server.empty()) | 300 | if(server.empty()) |
313 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); | 301 | throw failed_assertion(OPKELE_CP_ "The location has no openid.server declaration"); |
314 | } | 302 | } |
315 | 303 | ||
316 | assoc_t consumer_t::find_assoc(const string& server) { | 304 | assoc_t consumer_t::find_assoc(const string& server) { |
317 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); | 305 | throw failed_lookup(OPKELE_CP_ "no find_assoc() provided"); |
318 | } | 306 | } |
319 | 307 | ||
320 | string consumer_t::canonicalize(const string& url) { | 308 | string consumer_t::canonicalize(const string& url) { |
321 | string rv = url; | 309 | string rv = url; |
322 | // strip leading and trailing spaces | 310 | // strip leading and trailing spaces |
323 | string::size_type i = rv.find_first_not_of(" \t\r\n"); | 311 | string::size_type i = rv.find_first_not_of(" \t\r\n"); |
324 | if(i==string::npos) | 312 | if(i==string::npos) |
325 | throw bad_input(OPKELE_CP_ "empty URL"); | 313 | throw bad_input(OPKELE_CP_ "empty URL"); |
326 | if(i) | 314 | if(i) |
327 | rv.erase(0,i); | 315 | rv.erase(0,i); |
328 | i = rv.find_last_not_of(" \t\r\n"); | 316 | i = rv.find_last_not_of(" \t\r\n"); |
329 | assert(i!=string::npos); | 317 | assert(i!=string::npos); |
330 | if(i<(rv.length()-1)) | 318 | if(i<(rv.length()-1)) |
331 | rv.erase(i+1); | 319 | rv.erase(i+1); |
332 | // add missing http:// | 320 | // add missing http:// |
333 | i = rv.find("://"); | 321 | i = rv.find("://"); |
334 | if(i==string::npos) { // primitive. but do we need more? | 322 | if(i==string::npos) { // primitive. but do we need more? |
335 | rv.insert(0,"http://"); | 323 | rv.insert(0,"http://"); |
336 | i = sizeof("http://")-1; | 324 | i = sizeof("http://")-1; |
337 | }else{ | 325 | }else{ |
338 | i += sizeof("://")-1; | 326 | i += sizeof("://")-1; |
339 | } | 327 | } |
340 | string::size_type qm = rv.find('?',i); | 328 | string::size_type qm = rv.find('?',i); |
341 | string::size_type sl = rv.find('/',i); | 329 | string::size_type sl = rv.find('/',i); |
342 | if(qm!=string::npos) { | 330 | if(qm!=string::npos) { |
343 | if(sl==string::npos || sl>qm) | 331 | if(sl==string::npos || sl>qm) |
344 | rv.insert(qm,1,'/'); | 332 | rv.insert(qm,1,'/'); |
345 | }else{ | 333 | }else{ |
346 | if(sl==string::npos) | 334 | if(sl==string::npos) |
347 | rv += '/'; | 335 | rv += '/'; |
348 | } | 336 | } |
349 | return rv; | 337 | return rv; |
350 | } | 338 | } |
351 | 339 | ||
352 | } | 340 | } |
diff --git a/lib/params.cc b/lib/params.cc index 14f1a53..03867d5 100644 --- a/lib/params.cc +++ b/lib/params.cc | |||
@@ -1,96 +1,91 @@ | |||
1 | #include <opkele/types.h> | 1 | #include <opkele/types.h> |
2 | #include <opkele/exception.h> | 2 | #include <opkele/exception.h> |
3 | #include <opkele/util.h> | 3 | #include <opkele/util.h> |
4 | #include <openssl/sha.h> | 4 | #include <openssl/sha.h> |
5 | #include <openssl/hmac.h> | 5 | #include <openssl/hmac.h> |
6 | #include <mimetic/mimetic.h> | ||
7 | 6 | ||
8 | namespace opkele { | 7 | namespace opkele { |
9 | using namespace std; | 8 | using namespace std; |
10 | 9 | ||
11 | bool params_t::has_param(const string& n) const { | 10 | bool params_t::has_param(const string& n) const { |
12 | return find(n)!=end(); | 11 | return find(n)!=end(); |
13 | } | 12 | } |
14 | const string& params_t::get_param(const string& n) const { | 13 | const string& params_t::get_param(const string& n) const { |
15 | const_iterator i = find(n); | 14 | const_iterator i = find(n); |
16 | if(i==end()) | 15 | if(i==end()) |
17 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 16 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
18 | return i->second; | 17 | return i->second; |
19 | } | 18 | } |
20 | string& params_t::get_param(const string& n) { | 19 | string& params_t::get_param(const string& n) { |
21 | iterator i = find(n); | 20 | iterator i = find(n); |
22 | if(i==end()) | 21 | if(i==end()) |
23 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); | 22 | throw failed_lookup(OPKELE_CP_ n+": no such parameter"); |
24 | return i->second; | 23 | return i->second; |
25 | } | 24 | } |
26 | 25 | ||
27 | void params_t::parse_keyvalues(const string& kv) { | 26 | void params_t::parse_keyvalues(const string& kv) { |
28 | clear(); | 27 | clear(); |
29 | string::size_type p = 0; | 28 | string::size_type p = 0; |
30 | while(true) { | 29 | while(true) { |
31 | string::size_type co = kv.find(':',p); | 30 | string::size_type co = kv.find(':',p); |
32 | if(co==string::npos) | 31 | if(co==string::npos) |
33 | break; | 32 | break; |
34 | string::size_type nl = kv.find('\n',co+1); | 33 | string::size_type nl = kv.find('\n',co+1); |
35 | if(nl==string::npos) | 34 | if(nl==string::npos) |
36 | throw bad_input(OPKELE_CP_ "malformed input"); | 35 | throw bad_input(OPKELE_CP_ "malformed input"); |
37 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); | 36 | insert(value_type(kv.substr(p,co-p),kv.substr(co+1,nl-co-1))); |
38 | p = nl+1; | 37 | p = nl+1; |
39 | } | 38 | } |
40 | } | 39 | } |
41 | 40 | ||
42 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { | 41 | void params_t::sign(secret_t secret,string& sig,const string& slist,const char *prefix) const { |
43 | string kv; | 42 | string kv; |
44 | string::size_type p = 0; | 43 | string::size_type p = 0; |
45 | while(true) { | 44 | while(true) { |
46 | string::size_type co = slist.find(',',p); | 45 | string::size_type co = slist.find(',',p); |
47 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); | 46 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); |
48 | kv += f; | 47 | kv += f; |
49 | kv += ':'; | 48 | kv += ':'; |
50 | if(prefix) f.insert(0,prefix); | 49 | if(prefix) f.insert(0,prefix); |
51 | kv += get_param(f); | 50 | kv += get_param(f); |
52 | kv += '\n'; | 51 | kv += '\n'; |
53 | if(co==string::npos) | 52 | if(co==string::npos) |
54 | break; | 53 | break; |
55 | p = co+1; | 54 | p = co+1; |
56 | } | 55 | } |
57 | unsigned int md_len = 0; | 56 | unsigned int md_len = 0; |
58 | unsigned char *md = HMAC( | 57 | unsigned char *md = HMAC( |
59 | EVP_sha1(), | 58 | EVP_sha1(), |
60 | &(secret.front()),secret.size(), | 59 | &(secret.front()),secret.size(), |
61 | (const unsigned char *)kv.data(),kv.length(), | 60 | (const unsigned char *)kv.data(),kv.length(), |
62 | 0,&md_len); | 61 | 0,&md_len); |
63 | mimetic::Base64::Encoder b(0); | 62 | sig = util::encode_base64(md,md_len); |
64 | sig.erase(); | ||
65 | mimetic::encode( | ||
66 | md,md+md_len, b, | ||
67 | back_insert_iterator<string>(sig) ); | ||
68 | } | 63 | } |
69 | 64 | ||
70 | string params_t::append_query(const string& url,const char *prefix) const { | 65 | string params_t::append_query(const string& url,const char *prefix) const { |
71 | string rv = url; | 66 | string rv = url; |
72 | bool p = true; | 67 | bool p = true; |
73 | if(rv.find('?')==string::npos) { | 68 | if(rv.find('?')==string::npos) { |
74 | rv += '?'; | 69 | rv += '?'; |
75 | p = false; | 70 | p = false; |
76 | } | 71 | } |
77 | for(const_iterator i=begin();i!=end();++i) { | 72 | for(const_iterator i=begin();i!=end();++i) { |
78 | if(p) | 73 | if(p) |
79 | rv += '&'; | 74 | rv += '&'; |
80 | else | 75 | else |
81 | p = true; | 76 | p = true; |
82 | rv += prefix; | 77 | rv += prefix; |
83 | rv += i->first; | 78 | rv += i->first; |
84 | rv += '='; | 79 | rv += '='; |
85 | rv += util::url_encode(i->second); | 80 | rv += util::url_encode(i->second); |
86 | } | 81 | } |
87 | return rv; | 82 | return rv; |
88 | } | 83 | } |
89 | 84 | ||
90 | ostream& operator << (ostream& o,const params_t& p) { | 85 | ostream& operator << (ostream& o,const params_t& p) { |
91 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) | 86 | for(params_t::const_iterator i=p.begin();i!=p.end();++i) |
92 | o << i->first << ':' << i->second << '\n'; | 87 | o << i->first << ':' << i->second << '\n'; |
93 | return o; | 88 | return o; |
94 | } | 89 | } |
95 | 90 | ||
96 | } | 91 | } |
diff --git a/lib/secret.cc b/lib/secret.cc index ae8a3c5..632a2ca 100644 --- a/lib/secret.cc +++ b/lib/secret.cc | |||
@@ -1,61 +1,49 @@ | |||
1 | #include <algorithm> | 1 | #include <algorithm> |
2 | #include <functional> | 2 | #include <functional> |
3 | #include <opkele/types.h> | 3 | #include <opkele/types.h> |
4 | #include <opkele/exception.h> | 4 | #include <opkele/exception.h> |
5 | #include <mimetic/mimetic.h> | 5 | #include <opkele/util.h> |
6 | 6 | ||
7 | namespace opkele { | 7 | namespace opkele { |
8 | using namespace std; | 8 | using namespace std; |
9 | 9 | ||
10 | template<class __a1,class __a2,class __r> | 10 | template<class __a1,class __a2,class __r> |
11 | struct bitwise_xor : public binary_function<__a1,__a2,__r> { | 11 | struct bitwise_xor : public binary_function<__a1,__a2,__r> { |
12 | __r operator() (const __a1& a1,const __a2& a2) const { | 12 | __r operator() (const __a1& a1,const __a2& a2) const { |
13 | return a1^a2; | 13 | return a1^a2; |
14 | } | 14 | } |
15 | }; | 15 | }; |
16 | 16 | ||
17 | void secret_t::enxor_to_base64(const unsigned char *key_sha1,string& rv) const { | 17 | void secret_t::enxor_to_base64(const unsigned char *key_sha1,string& rv) const { |
18 | if(size()!=20) | 18 | if(size()!=20) |
19 | throw bad_input(OPKELE_CP_ "wrong secret size"); | 19 | throw bad_input(OPKELE_CP_ "wrong secret size"); |
20 | vector<unsigned char> tmp; | 20 | vector<unsigned char> tmp; |
21 | transform( | 21 | transform( |
22 | begin(), end(), | 22 | begin(), end(), |
23 | key_sha1, | 23 | key_sha1, |
24 | back_insert_iterator<vector<unsigned char> >(tmp), | 24 | back_insert_iterator<vector<unsigned char> >(tmp), |
25 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); | 25 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); |
26 | mimetic::Base64::Encoder b(0); | 26 | rv = util::encode_base64(&(tmp.front()),tmp.size()); |
27 | mimetic::encode( | ||
28 | tmp.begin(),tmp.end(), b, | ||
29 | back_insert_iterator<string>(rv) ); | ||
30 | } | 27 | } |
31 | 28 | ||
32 | void secret_t::enxor_from_base64(const unsigned char *key_sha1,const string& b64) { | 29 | void secret_t::enxor_from_base64(const unsigned char *key_sha1,const string& b64) { |
33 | mimetic::Base64::Decoder b; | ||
34 | clear(); | 30 | clear(); |
35 | mimetic::decode( | 31 | util::decode_base64(b64,*this); |
36 | b64.begin(),b64.end(), b, | ||
37 | back_insert_iterator<secret_t>(*this) ); | ||
38 | transform( | 32 | transform( |
39 | begin(), end(), | 33 | begin(), end(), |
40 | key_sha1, | 34 | key_sha1, |
41 | begin(), | 35 | begin(), |
42 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); | 36 | bitwise_xor<unsigned char,unsigned char,unsigned char>() ); |
43 | } | 37 | } |
44 | 38 | ||
45 | void secret_t::to_base64(string& rv) const { | 39 | void secret_t::to_base64(string& rv) const { |
46 | if(size()!=20) | 40 | if(size()!=20) |
47 | throw bad_input(OPKELE_CP_ "wrong secret size"); | 41 | throw bad_input(OPKELE_CP_ "wrong secret size"); |
48 | mimetic::Base64::Encoder b(0); | 42 | rv = util::encode_base64(&(front()),size()); |
49 | mimetic::encode( | ||
50 | begin(),end(), b, | ||
51 | back_insert_iterator<string>(rv) ); | ||
52 | } | 43 | } |
53 | 44 | ||
54 | void secret_t::from_base64(const string& b64) { | 45 | void secret_t::from_base64(const string& b64) { |
55 | mimetic::Base64::Decoder b; | 46 | util::decode_base64(b64,*this); |
56 | mimetic::decode( | ||
57 | b64.begin(),b64.end(), b, | ||
58 | back_insert_iterator<secret_t>(*this) ); | ||
59 | } | 47 | } |
60 | 48 | ||
61 | } | 49 | } |
diff --git a/lib/server.cc b/lib/server.cc index 8c29abb..e81d4b6 100644 --- a/lib/server.cc +++ b/lib/server.cc | |||
@@ -1,173 +1,169 @@ | |||
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> | ||
5 | #include <opkele/util.h> | 4 | #include <opkele/util.h> |
6 | #include <opkele/exception.h> | 5 | #include <opkele/exception.h> |
7 | #include <opkele/server.h> | 6 | #include <opkele/server.h> |
8 | #include <opkele/data.h> | 7 | #include <opkele/data.h> |
9 | 8 | ||
10 | namespace opkele { | 9 | namespace opkele { |
11 | using namespace std; | 10 | using namespace std; |
12 | 11 | ||
13 | void server_t::associate(const params_t& pin,params_t& pout) { | 12 | void server_t::associate(const params_t& pin,params_t& pout) { |
14 | util::dh_t dh; | 13 | util::dh_t dh; |
15 | util::bignum_t c_pub; | 14 | util::bignum_t c_pub; |
16 | unsigned char key_sha1[SHA_DIGEST_LENGTH]; | 15 | unsigned char key_sha1[SHA_DIGEST_LENGTH]; |
17 | enum { | 16 | enum { |
18 | sess_cleartext, | 17 | sess_cleartext, |
19 | sess_dh_sha1 | 18 | sess_dh_sha1 |
20 | } st = sess_cleartext; | 19 | } st = sess_cleartext; |
21 | if( | 20 | if( |
22 | pin.has_param("openid.session_type") | 21 | pin.has_param("openid.session_type") |
23 | && pin.get_param("openid.session_type")=="DH-SHA1" ) { | 22 | && pin.get_param("openid.session_type")=="DH-SHA1" ) { |
24 | /* TODO: fallback to cleartext in case of exceptions here? */ | 23 | /* TODO: fallback to cleartext in case of exceptions here? */ |
25 | if(!(dh = DH_new())) | 24 | if(!(dh = DH_new())) |
26 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); | 25 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); |
27 | c_pub = util::base64_to_bignum(pin.get_param("openid.dh_consumer_public")); | 26 | c_pub = util::base64_to_bignum(pin.get_param("openid.dh_consumer_public")); |
28 | if(pin.has_param("openid.dh_modulus")) | 27 | if(pin.has_param("openid.dh_modulus")) |
29 | dh->p = util::base64_to_bignum(pin.get_param("openid.dh_modulus")); | 28 | dh->p = util::base64_to_bignum(pin.get_param("openid.dh_modulus")); |
30 | else | 29 | else |
31 | dh->p = util::dec_to_bignum(data::_default_p); | 30 | dh->p = util::dec_to_bignum(data::_default_p); |
32 | if(pin.has_param("openid.dh_gen")) | 31 | if(pin.has_param("openid.dh_gen")) |
33 | dh->g = util::base64_to_bignum(pin.get_param("openid.dh_gen")); | 32 | dh->g = util::base64_to_bignum(pin.get_param("openid.dh_gen")); |
34 | else | 33 | else |
35 | dh->g = util::dec_to_bignum(data::_default_g); | 34 | dh->g = util::dec_to_bignum(data::_default_g); |
36 | if(!DH_generate_key(dh)) | 35 | if(!DH_generate_key(dh)) |
37 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 36 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
38 | vector<unsigned char> ck(DH_size(dh)); | 37 | vector<unsigned char> ck(DH_size(dh)); |
39 | int cklen = DH_compute_key(&(ck.front()),c_pub,dh); | 38 | int cklen = DH_compute_key(&(ck.front()),c_pub,dh); |
40 | if(cklen<0) | 39 | if(cklen<0) |
41 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); | 40 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); |
42 | ck.resize(cklen); | 41 | ck.resize(cklen); |
43 | // OpenID algorithm requires extra zero in case of set bit here | 42 | // OpenID algorithm requires extra zero in case of set bit here |
44 | if(ck[0]&0x80) ck.insert(ck.begin(),1,0); | 43 | if(ck[0]&0x80) ck.insert(ck.begin(),1,0); |
45 | SHA1(&(ck.front()),ck.size(),key_sha1); | 44 | SHA1(&(ck.front()),ck.size(),key_sha1); |
46 | st = sess_dh_sha1; | 45 | st = sess_dh_sha1; |
47 | } | 46 | } |
48 | assoc_t assoc = alloc_assoc(mode_associate); | 47 | assoc_t assoc = alloc_assoc(mode_associate); |
49 | time_t now = time(0); | 48 | time_t now = time(0); |
50 | pout.clear(); | 49 | pout.clear(); |
51 | pout["assoc_type"] = assoc->assoc_type(); | 50 | pout["assoc_type"] = assoc->assoc_type(); |
52 | pout["assoc_handle"] = assoc->handle(); | 51 | pout["assoc_handle"] = assoc->handle(); |
53 | /* TODO: eventually remove deprecated stuff */ | 52 | /* TODO: eventually remove deprecated stuff */ |
54 | pout["issued"] = util::time_to_w3c(now); | 53 | pout["issued"] = util::time_to_w3c(now); |
55 | pout["expiry"] = util::time_to_w3c(now+assoc->expires_in()); | 54 | pout["expiry"] = util::time_to_w3c(now+assoc->expires_in()); |
56 | pout["expires_in"] = util::long_to_string(assoc->expires_in()); | 55 | pout["expires_in"] = util::long_to_string(assoc->expires_in()); |
57 | secret_t secret = assoc->secret(); | 56 | secret_t secret = assoc->secret(); |
58 | switch(st) { | 57 | switch(st) { |
59 | case sess_dh_sha1: | 58 | case sess_dh_sha1: |
60 | pout["session_type"] = "DH-SHA1"; | 59 | pout["session_type"] = "DH-SHA1"; |
61 | pout["dh_server_public"] = util::bignum_to_base64(dh->pub_key); | 60 | pout["dh_server_public"] = util::bignum_to_base64(dh->pub_key); |
62 | secret.enxor_to_base64(key_sha1,pout["enc_mac_key"]); | 61 | secret.enxor_to_base64(key_sha1,pout["enc_mac_key"]); |
63 | break; | 62 | break; |
64 | default: | 63 | default: |
65 | secret.to_base64(pout["mac_key"]); | 64 | secret.to_base64(pout["mac_key"]); |
66 | break; | 65 | break; |
67 | } | 66 | } |
68 | } | 67 | } |
69 | 68 | ||
70 | void server_t::checkid_immediate(const params_t& pin,string& return_to,params_t& pout,extension_t *ext) { | 69 | 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,ext); | 70 | checkid_(mode_checkid_immediate,pin,return_to,pout,ext); |
72 | } | 71 | } |
73 | 72 | ||
74 | void server_t::checkid_setup(const params_t& pin,string& return_to,params_t& pout,extension_t *ext) { | 73 | 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,ext); | 74 | checkid_(mode_checkid_setup,pin,return_to,pout,ext); |
76 | } | 75 | } |
77 | 76 | ||
78 | void server_t::checkid_(mode_t mode,const params_t& pin,string& return_to,params_t& pout,extension_t *ext) { | 77 | 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) | 78 | if(mode!=mode_checkid_immediate && mode!=mode_checkid_setup) |
80 | throw bad_input(OPKELE_CP_ "invalid checkid_* mode"); | 79 | throw bad_input(OPKELE_CP_ "invalid checkid_* mode"); |
81 | pout.clear(); | 80 | pout.clear(); |
82 | assoc_t assoc; | 81 | assoc_t assoc; |
83 | try { | 82 | try { |
84 | assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); | 83 | assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); |
85 | }catch(failed_lookup& fl) { | 84 | }catch(failed_lookup& fl) { |
86 | // no handle specified or no valid handle found, going dumb | 85 | // no handle specified or no valid handle found, going dumb |
87 | assoc = alloc_assoc(mode_checkid_setup); | 86 | assoc = alloc_assoc(mode_checkid_setup); |
88 | if(pin.has_param("openid.assoc_handle")) | 87 | if(pin.has_param("openid.assoc_handle")) |
89 | pout["invalidate_handle"]=pin.get_param("openid.assoc_handle"); | 88 | pout["invalidate_handle"]=pin.get_param("openid.assoc_handle"); |
90 | } | 89 | } |
91 | string trust_root; | 90 | string trust_root; |
92 | try { | 91 | try { |
93 | trust_root = pin.get_param("openid.trust_root"); | 92 | trust_root = pin.get_param("openid.trust_root"); |
94 | }catch(failed_lookup& fl) { } | 93 | }catch(failed_lookup& fl) { } |
95 | string identity = pin.get_param("openid.identity"); | 94 | string identity = pin.get_param("openid.identity"); |
96 | return_to = pin.get_param("openid.return_to"); | 95 | return_to = pin.get_param("openid.return_to"); |
97 | validate(*assoc,pin,identity,trust_root); | 96 | validate(*assoc,pin,identity,trust_root); |
98 | pout["mode"] = "id_res"; | 97 | pout["mode"] = "id_res"; |
99 | pout["assoc_handle"] = assoc->handle(); | 98 | pout["assoc_handle"] = assoc->handle(); |
100 | if(pin.has_param("openid.assoc_handle") && assoc->stateless()) | 99 | if(pin.has_param("openid.assoc_handle") && assoc->stateless()) |
101 | pout["invalidate_handle"] = pin.get_param("openid.assoc_handle"); | 100 | pout["invalidate_handle"] = pin.get_param("openid.assoc_handle"); |
102 | pout["identity"] = identity; | 101 | pout["identity"] = identity; |
103 | pout["return_to"] = return_to; | 102 | pout["return_to"] = return_to; |
104 | /* TODO: eventually remove deprecated stuff */ | 103 | /* TODO: eventually remove deprecated stuff */ |
105 | time_t now = time(0); | 104 | time_t now = time(0); |
106 | pout["issued"] = util::time_to_w3c(now); | 105 | pout["issued"] = util::time_to_w3c(now); |
107 | pout["valid_to"] = util::time_to_w3c(now+120); | 106 | pout["valid_to"] = util::time_to_w3c(now+120); |
108 | pout["exipres_in"] = "120"; | 107 | pout["exipres_in"] = "120"; |
109 | pout["signed"]="mode,identity,return_to"; | 108 | pout["signed"]="mode,identity,return_to"; |
110 | if(ext) ext->checkid_hook(pin,pout); | 109 | if(ext) ext->checkid_hook(pin,pout); |
111 | pout.sign(assoc->secret(),pout["sig"],pout["signed"]); | 110 | pout.sign(assoc->secret(),pout["sig"],pout["signed"]); |
112 | } | 111 | } |
113 | 112 | ||
114 | void server_t::check_authentication(const params_t& pin,params_t& pout) { | 113 | void server_t::check_authentication(const params_t& pin,params_t& pout) { |
115 | vector<unsigned char> sig; | 114 | vector<unsigned char> sig; |
116 | mimetic::Base64::Decoder b; | ||
117 | const string& sigenc = pin.get_param("openid.sig"); | 115 | const string& sigenc = pin.get_param("openid.sig"); |
118 | mimetic::decode( | 116 | util::decode_base64(sigenc,sig); |
119 | sigenc.begin(),sigenc.end(), b, | ||
120 | back_insert_iterator<vector<unsigned char> >(sig)); | ||
121 | assoc_t assoc; | 117 | assoc_t assoc; |
122 | try { | 118 | try { |
123 | assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); | 119 | assoc = retrieve_assoc(pin.get_param("openid.assoc_handle")); |
124 | }catch(failed_lookup& fl) { | 120 | }catch(failed_lookup& fl) { |
125 | throw failed_assertion(OPKELE_CP_ "invalid handle or handle not specified"); | 121 | throw failed_assertion(OPKELE_CP_ "invalid handle or handle not specified"); |
126 | } | 122 | } |
127 | if(!assoc->stateless()) | 123 | if(!assoc->stateless()) |
128 | throw stateful_handle(OPKELE_CP_ "will not do check_authentication on a stateful handle"); | 124 | throw stateful_handle(OPKELE_CP_ "will not do check_authentication on a stateful handle"); |
129 | const string& slist = pin.get_param("openid.signed"); | 125 | const string& slist = pin.get_param("openid.signed"); |
130 | string kv; | 126 | string kv; |
131 | string::size_type p =0; | 127 | string::size_type p =0; |
132 | while(true) { | 128 | while(true) { |
133 | string::size_type co = slist.find(',',p); | 129 | string::size_type co = slist.find(',',p); |
134 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); | 130 | string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); |
135 | kv += f; | 131 | kv += f; |
136 | kv += ':'; | 132 | kv += ':'; |
137 | if(f=="mode") | 133 | if(f=="mode") |
138 | kv += "id_res"; | 134 | kv += "id_res"; |
139 | else { | 135 | else { |
140 | f.insert(0,"openid."); | 136 | f.insert(0,"openid."); |
141 | kv += pin.get_param(f); | 137 | kv += pin.get_param(f); |
142 | } | 138 | } |
143 | kv += '\n'; | 139 | kv += '\n'; |
144 | if(co==string::npos) | 140 | if(co==string::npos) |
145 | break; | 141 | break; |
146 | p = co+1; | 142 | p = co+1; |
147 | } | 143 | } |
148 | secret_t secret = assoc->secret(); | 144 | secret_t secret = assoc->secret(); |
149 | unsigned int md_len = 0; | 145 | unsigned int md_len = 0; |
150 | unsigned char *md = HMAC( | 146 | unsigned char *md = HMAC( |
151 | EVP_sha1(), | 147 | EVP_sha1(), |
152 | &(secret.front()),secret.size(), | 148 | &(secret.front()),secret.size(), |
153 | (const unsigned char *)kv.data(),kv.length(), | 149 | (const unsigned char *)kv.data(),kv.length(), |
154 | 0,&md_len); | 150 | 0,&md_len); |
155 | pout.clear(); | 151 | pout.clear(); |
156 | if(sig.size()==md_len && !memcmp(&(sig.front()),md,md_len)) { | 152 | if(sig.size()==md_len && !memcmp(&(sig.front()),md,md_len)) { |
157 | pout["is_valid"]="true"; | 153 | pout["is_valid"]="true"; |
158 | pout["lifetime"]="60"; /* TODO: eventually remove deprecated stuff */ | 154 | pout["lifetime"]="60"; /* TODO: eventually remove deprecated stuff */ |
159 | }else{ | 155 | }else{ |
160 | pout["is_valid"]="false"; | 156 | pout["is_valid"]="false"; |
161 | pout["lifetime"]="0"; /* TODO: eventually remove deprecated stuff */ | 157 | pout["lifetime"]="0"; /* TODO: eventually remove deprecated stuff */ |
162 | } | 158 | } |
163 | if(pin.has_param("openid.invalidate_handle")) { | 159 | if(pin.has_param("openid.invalidate_handle")) { |
164 | string h = pin.get_param("openid.invalidate_handle"); | 160 | string h = pin.get_param("openid.invalidate_handle"); |
165 | try { | 161 | try { |
166 | assoc_t assoc = retrieve_assoc(h); | 162 | assoc_t assoc = retrieve_assoc(h); |
167 | }catch(invalid_handle& ih) { | 163 | }catch(invalid_handle& ih) { |
168 | pout["invalidate_handle"] = h; | 164 | pout["invalidate_handle"] = h; |
169 | }catch(failed_lookup& fl) { } | 165 | }catch(failed_lookup& fl) { } |
170 | } | 166 | } |
171 | } | 167 | } |
172 | 168 | ||
173 | } | 169 | } |
diff --git a/lib/util.cc b/lib/util.cc index d78b5e0..d9abca7 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,113 +1,158 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <vector> | 3 | #include <vector> |
4 | #include <string> | 4 | #include <string> |
5 | #include <mimetic/mimetic.h> | 5 | #include <openssl/bio.h> |
6 | #include <openssl/evp.h> | ||
6 | #include <curl/curl.h> | 7 | #include <curl/curl.h> |
7 | #include "opkele/util.h" | 8 | #include "opkele/util.h" |
8 | #include "opkele/exception.h" | 9 | #include "opkele/exception.h" |
9 | 10 | ||
10 | namespace opkele { | 11 | namespace opkele { |
11 | using namespace std; | 12 | using namespace std; |
12 | 13 | ||
13 | namespace util { | 14 | namespace util { |
14 | 15 | ||
15 | /* | 16 | /* |
17 | * base64 | ||
18 | */ | ||
19 | string encode_base64(const void *data,size_t length) { | ||
20 | BIO *b64 = 0, *bmem = 0; | ||
21 | try { | ||
22 | b64 = BIO_new(BIO_f_base64()); | ||
23 | if(!b64) | ||
24 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | ||
25 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | ||
26 | bmem = BIO_new(BIO_s_mem()); | ||
27 | BIO_set_flags(b64,BIO_CLOSE); | ||
28 | if(!bmem) | ||
29 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | ||
30 | BIO_push(b64,bmem); | ||
31 | if(BIO_write(b64,data,length)!=length) | ||
32 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | ||
33 | BIO_flush(b64); | ||
34 | char *rvd; | ||
35 | long rvl = BIO_get_mem_data(bmem,&rvd); | ||
36 | string rv(rvd,rvl); | ||
37 | BIO_free_all(b64); | ||
38 | return rv; | ||
39 | }catch(...) { | ||
40 | if(b64) BIO_free_all(b64); | ||
41 | throw; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | void decode_base64(const string& data,vector<unsigned char>& rv) { | ||
46 | BIO *b64 = 0, *bmem = 0; | ||
47 | rv.clear(); | ||
48 | try { | ||
49 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | ||
50 | if(!bmem) | ||
51 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | ||
52 | b64 = BIO_new(BIO_f_base64()); | ||
53 | if(!b64) | ||
54 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | ||
55 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | ||
56 | BIO_push(b64,bmem); | ||
57 | unsigned char tmp[512]; | ||
58 | size_t rb = 0; | ||
59 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | ||
60 | rv.insert(rv.end(),tmp,&tmp[rb]); | ||
61 | BIO_free_all(b64); | ||
62 | }catch(...) { | ||
63 | if(b64) BIO_free_all(b64); | ||
64 | throw; | ||
65 | } | ||
66 | } | ||
67 | |||
68 | /* | ||
16 | * big numerics | 69 | * big numerics |
17 | */ | 70 | */ |
18 | 71 | ||
19 | BIGNUM *base64_to_bignum(const string& b64) { | 72 | BIGNUM *base64_to_bignum(const string& b64) { |
20 | vector<unsigned char> bin; | 73 | vector<unsigned char> bin; |
21 | mimetic::Base64::Decoder b; | 74 | decode_base64(b64,bin); |
22 | mimetic::decode( | ||
23 | b64.begin(),b64.end(), b, | ||
24 | back_insert_iterator<vector<unsigned char> >(bin) ); | ||
25 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 75 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
26 | if(!rv) | 76 | if(!rv) |
27 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 77 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
28 | return rv; | 78 | return rv; |
29 | } | 79 | } |
30 | 80 | ||
31 | BIGNUM *dec_to_bignum(const string& dec) { | 81 | BIGNUM *dec_to_bignum(const string& dec) { |
32 | BIGNUM *rv = 0; | 82 | BIGNUM *rv = 0; |
33 | if(!BN_dec2bn(&rv,dec.c_str())) | 83 | if(!BN_dec2bn(&rv,dec.c_str())) |
34 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 84 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
35 | return rv; | 85 | return rv; |
36 | } | 86 | } |
37 | 87 | ||
38 | string bignum_to_base64(const BIGNUM *bn) { | 88 | string bignum_to_base64(const BIGNUM *bn) { |
39 | vector<unsigned char> bin(BN_num_bytes(bn)); | 89 | vector<unsigned char> bin(BN_num_bytes(bn)); |
40 | int l = BN_bn2bin(bn,&(bin.front())); | 90 | int l = BN_bn2bin(bn,&(bin.front())); |
41 | string rv; | 91 | return encode_base64(&(bin.front()),l); |
42 | mimetic::Base64::Encoder b(0); | ||
43 | mimetic::encode( | ||
44 | bin.begin(),bin.begin()+l, b, | ||
45 | back_insert_iterator<string>(rv) ); | ||
46 | return rv; | ||
47 | } | 92 | } |
48 | 93 | ||
49 | /* | 94 | /* |
50 | * w3c times | 95 | * w3c times |
51 | */ | 96 | */ |
52 | 97 | ||
53 | string time_to_w3c(time_t t) { | 98 | string time_to_w3c(time_t t) { |
54 | struct tm tm_t; | 99 | struct tm tm_t; |
55 | if(!gmtime_r(&t,&tm_t)) | 100 | if(!gmtime_r(&t,&tm_t)) |
56 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 101 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
57 | char rv[25]; | 102 | char rv[25]; |
58 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 103 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
59 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 104 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
60 | return rv; | 105 | return rv; |
61 | } | 106 | } |
62 | 107 | ||
63 | time_t w3c_to_time(const string& w) { | 108 | time_t w3c_to_time(const string& w) { |
64 | struct tm tm_t; | 109 | struct tm tm_t; |
65 | memset(&tm_t,0,sizeof(tm_t)); | 110 | memset(&tm_t,0,sizeof(tm_t)); |
66 | if( | 111 | if( |
67 | sscanf( | 112 | sscanf( |
68 | w.c_str(), | 113 | w.c_str(), |
69 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 114 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
70 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 115 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
71 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 116 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
72 | ) != 6 ) | 117 | ) != 6 ) |
73 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
74 | tm_t.tm_mon--; | 119 | tm_t.tm_mon--; |
75 | tm_t.tm_year-=1900; | 120 | tm_t.tm_year-=1900; |
76 | time_t rv = mktime(&tm_t); | 121 | time_t rv = mktime(&tm_t); |
77 | if(rv==(time_t)-1) | 122 | if(rv==(time_t)-1) |
78 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 123 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
79 | return rv; | 124 | return rv; |
80 | } | 125 | } |
81 | 126 | ||
82 | /* | 127 | /* |
83 | * | 128 | * |
84 | */ | 129 | */ |
85 | 130 | ||
86 | string url_encode(const string& str) { | 131 | string url_encode(const string& str) { |
87 | char * t = curl_escape(str.c_str(),str.length()); | 132 | char * t = curl_escape(str.c_str(),str.length()); |
88 | if(!t) | 133 | if(!t) |
89 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 134 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
90 | string rv(t); | 135 | string rv(t); |
91 | curl_free(t); | 136 | curl_free(t); |
92 | return rv; | 137 | return rv; |
93 | } | 138 | } |
94 | 139 | ||
95 | string long_to_string(long l) { | 140 | string long_to_string(long l) { |
96 | char rv[32]; | 141 | char rv[32]; |
97 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 142 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
98 | if(r<0 || r>=sizeof(rv)) | 143 | if(r<0 || r>=sizeof(rv)) |
99 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 144 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
100 | return rv; | 145 | return rv; |
101 | } | 146 | } |
102 | 147 | ||
103 | long string_to_long(const string& s) { | 148 | long string_to_long(const string& s) { |
104 | char *endptr = 0; | 149 | char *endptr = 0; |
105 | long rv = strtol(s.c_str(),&endptr,10); | 150 | long rv = strtol(s.c_str(),&endptr,10); |
106 | if((!endptr) || endptr==s.c_str()) | 151 | if((!endptr) || endptr==s.c_str()) |
107 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 152 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
108 | return rv; | 153 | return rv; |
109 | } | 154 | } |
110 | 155 | ||
111 | } | 156 | } |
112 | 157 | ||
113 | } | 158 | } |