author | Michael Krelin <hacker@klever.net> | 2008-01-20 21:08:05 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-01-20 21:08:05 (UTC) |
commit | 9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc (patch) (unidiff) | |
tree | 702473142242e80538c4801cc379ec98fba199dd /lib/util.cc | |
parent | 395a126cbf59b7a50f44da3096b68bab412ab33d (diff) | |
download | libopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.zip libopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.tar.gz libopkele-9bfb6fadf71c46bf4cb5adabba0c96c32e84c1bc.tar.bz2 |
the whole library rewritten
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/lib/util.cc b/lib/util.cc index a9b9bed..54d6535 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,61 +1,67 @@ | |||
1 | #include <errno.h> | 1 | #include <errno.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <cctype> | 3 | #include <cctype> |
4 | #include <cstring> | 4 | #include <cstring> |
5 | #include <vector> | 5 | #include <vector> |
6 | #include <string> | 6 | #include <string> |
7 | #include <stack> | 7 | #include <stack> |
8 | #include <openssl/bio.h> | 8 | #include <openssl/bio.h> |
9 | #include <openssl/evp.h> | 9 | #include <openssl/evp.h> |
10 | #include <openssl/hmac.h> | ||
10 | #include <curl/curl.h> | 11 | #include <curl/curl.h> |
11 | #include "opkele/util.h" | 12 | #include "opkele/util.h" |
12 | #include "opkele/exception.h" | 13 | #include "opkele/exception.h" |
13 | 14 | ||
15 | #include <config.h> | ||
16 | #ifdef HAVE_DEMANGLE | ||
17 | # include <cxxabi.h> | ||
18 | #endif | ||
19 | |||
14 | namespace opkele { | 20 | namespace opkele { |
15 | using namespace std; | 21 | using namespace std; |
16 | 22 | ||
17 | namespace util { | 23 | namespace util { |
18 | 24 | ||
19 | /* | 25 | /* |
20 | * base64 | 26 | * base64 |
21 | */ | 27 | */ |
22 | string encode_base64(const void *data,size_t length) { | 28 | string encode_base64(const void *data,size_t length) { |
23 | BIO *b64 = 0, *bmem = 0; | 29 | BIO *b64 = 0, *bmem = 0; |
24 | try { | 30 | try { |
25 | b64 = BIO_new(BIO_f_base64()); | 31 | b64 = BIO_new(BIO_f_base64()); |
26 | if(!b64) | 32 | if(!b64) |
27 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 33 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
28 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 34 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
29 | bmem = BIO_new(BIO_s_mem()); | 35 | bmem = BIO_new(BIO_s_mem()); |
30 | BIO_set_flags(b64,BIO_CLOSE); | 36 | BIO_set_flags(b64,BIO_CLOSE); |
31 | if(!bmem) | 37 | if(!bmem) |
32 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 38 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
33 | BIO_push(b64,bmem); | 39 | BIO_push(b64,bmem); |
34 | if(((size_t)BIO_write(b64,data,length))!=length) | 40 | if(((size_t)BIO_write(b64,data,length))!=length) |
35 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
36 | if(BIO_flush(b64)!=1) | 42 | if(BIO_flush(b64)!=1) |
37 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 43 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
38 | char *rvd; | 44 | char *rvd; |
39 | long rvl = BIO_get_mem_data(bmem,&rvd); | 45 | long rvl = BIO_get_mem_data(bmem,&rvd); |
40 | string rv(rvd,rvl); | 46 | string rv(rvd,rvl); |
41 | BIO_free_all(b64); | 47 | BIO_free_all(b64); |
42 | return rv; | 48 | return rv; |
43 | }catch(...) { | 49 | }catch(...) { |
44 | if(b64) BIO_free_all(b64); | 50 | if(b64) BIO_free_all(b64); |
45 | throw; | 51 | throw; |
46 | } | 52 | } |
47 | } | 53 | } |
48 | 54 | ||
49 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 55 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
50 | BIO *b64 = 0, *bmem = 0; | 56 | BIO *b64 = 0, *bmem = 0; |
51 | rv.clear(); | 57 | rv.clear(); |
52 | try { | 58 | try { |
53 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 59 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
54 | if(!bmem) | 60 | if(!bmem) |
55 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 61 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
56 | b64 = BIO_new(BIO_f_base64()); | 62 | b64 = BIO_new(BIO_f_base64()); |
57 | if(!b64) | 63 | if(!b64) |
58 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
59 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 65 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
60 | BIO_push(b64,bmem); | 66 | BIO_push(b64,bmem); |
61 | unsigned char tmp[512]; | 67 | unsigned char tmp[512]; |
@@ -160,98 +166,97 @@ namespace opkele { | |||
160 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 166 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
161 | if(r<0 || r>=(int)sizeof(rv)) | 167 | if(r<0 || r>=(int)sizeof(rv)) |
162 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 168 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
163 | return rv; | 169 | return rv; |
164 | } | 170 | } |
165 | 171 | ||
166 | long string_to_long(const string& s) { | 172 | long string_to_long(const string& s) { |
167 | char *endptr = 0; | 173 | char *endptr = 0; |
168 | long rv = strtol(s.c_str(),&endptr,10); | 174 | long rv = strtol(s.c_str(),&endptr,10); |
169 | if((!endptr) || endptr==s.c_str()) | 175 | if((!endptr) || endptr==s.c_str()) |
170 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 176 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
171 | return rv; | 177 | return rv; |
172 | } | 178 | } |
173 | 179 | ||
174 | /* | 180 | /* |
175 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 181 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
176 | * | 182 | * |
177 | * - uppercase hex triplets (e.g. %ab -> %AB) | 183 | * - uppercase hex triplets (e.g. %ab -> %AB) |
178 | * - lowercase scheme and host | 184 | * - lowercase scheme and host |
179 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 185 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
180 | * that is - [:alpha:][:digit:]._~- | 186 | * that is - [:alpha:][:digit:]._~- |
181 | * - remove dot segments | 187 | * - remove dot segments |
182 | * - remove empty and default ports | 188 | * - remove empty and default ports |
183 | * - if there's no path component, add '/' | 189 | * - if there's no path component, add '/' |
184 | */ | 190 | */ |
185 | string rfc_3986_normalize_uri(const string& uri) { | 191 | string rfc_3986_normalize_uri(const string& uri) { |
186 | static const char *whitespace = " \t\r\n"; | 192 | static const char *whitespace = " \t\r\n"; |
187 | string rv; | 193 | string rv; |
188 | string::size_type ns = uri.find_first_not_of(whitespace); | 194 | string::size_type ns = uri.find_first_not_of(whitespace); |
189 | if(ns==string::npos) | 195 | if(ns==string::npos) |
190 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 196 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
191 | string::size_type colon = uri.find(':',ns); | 197 | string::size_type colon = uri.find(':',ns); |
192 | if(colon==string::npos) | 198 | if(colon==string::npos) |
193 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 199 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
194 | transform( | 200 | transform( |
195 | uri.begin()+ns, uri.begin()+colon+1, | 201 | uri.begin()+ns, uri.begin()+colon+1, |
196 | back_inserter(rv), ::tolower ); | 202 | back_inserter(rv), ::tolower ); |
197 | bool s; | 203 | bool s; |
198 | string::size_type ul = uri.find_last_not_of(whitespace)+1; | 204 | string::size_type ul = uri.find_last_not_of(whitespace)+1; |
199 | if(ul <= (colon+3)) | 205 | if(ul <= (colon+3)) |
200 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 206 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
201 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 207 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
202 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 208 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
203 | if(rv=="http:") | 209 | if(rv=="http:") |
204 | s = false; | 210 | s = false; |
205 | else if(rv=="https:") | 211 | else if(rv=="https:") |
206 | s = true; | 212 | s = true; |
207 | else{ | 213 | else{ |
208 | /* TODO: support more schemes. | 214 | /* TODO: support more schemes. e.g. xri. How do we normalize |
209 | * e.g. xri. How do we normalize | ||
210 | * xri? | 215 | * xri? |
211 | */ | 216 | */ |
212 | rv.append(uri,colon+1,ul-colon-1); | 217 | rv.append(uri,colon+1,ul-colon-1); |
213 | return rv; | 218 | return rv; |
214 | } | 219 | } |
215 | rv += "//"; | 220 | rv += "//"; |
216 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 221 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
217 | if(interesting==string::npos) { | 222 | if(interesting==string::npos) { |
218 | transform( | 223 | transform( |
219 | uri.begin()+colon+3,uri.begin()+ul, | 224 | uri.begin()+colon+3,uri.begin()+ul, |
220 | back_inserter(rv), ::tolower ); | 225 | back_inserter(rv), ::tolower ); |
221 | rv += '/'; return rv; | 226 | rv += '/'; return rv; |
222 | } | 227 | } |
223 | transform( | 228 | transform( |
224 | uri.begin()+colon+3,uri.begin()+interesting, | 229 | uri.begin()+colon+3,uri.begin()+interesting, |
225 | back_inserter(rv), ::tolower ); | 230 | back_inserter(rv), ::tolower ); |
226 | bool qf = false; | 231 | bool qf = false; |
227 | char ic = uri[interesting]; | 232 | char ic = uri[interesting]; |
228 | if(ic==':') { | 233 | if(ic==':') { |
229 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 234 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
230 | const char *nptr = uri.data()+interesting+1; | 235 | const char *nptr = uri.data()+interesting+1; |
231 | char *eptr = 0; | 236 | char *eptr = 0; |
232 | long port = strtol(nptr,&eptr,10); | 237 | long port = strtol(nptr,&eptr,10); |
233 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 238 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
234 | char tmp[8]; | 239 | char tmp[8]; |
235 | snprintf(tmp,sizeof(tmp),":%ld",port); | 240 | snprintf(tmp,sizeof(tmp),":%ld",port); |
236 | rv += tmp; | 241 | rv += tmp; |
237 | } | 242 | } |
238 | if(ni==string::npos) { | 243 | if(ni==string::npos) { |
239 | rv += '/'; return rv; | 244 | rv += '/'; return rv; |
240 | } | 245 | } |
241 | interesting = ni; | 246 | interesting = ni; |
242 | }else if(ic!='/') { | 247 | }else if(ic!='/') { |
243 | rv += '/'; rv += ic; | 248 | rv += '/'; rv += ic; |
244 | qf = true; | 249 | qf = true; |
245 | ++interesting; | 250 | ++interesting; |
246 | } | 251 | } |
247 | string::size_type n = interesting; | 252 | string::size_type n = interesting; |
248 | char tmp[3] = { 0,0,0 }; | 253 | char tmp[3] = { 0,0,0 }; |
249 | stack<string::size_type> psegs; psegs.push(rv.length()); | 254 | stack<string::size_type> psegs; psegs.push(rv.length()); |
250 | string pseg; | 255 | string pseg; |
251 | for(;n<ul;) { | 256 | for(;n<ul;) { |
252 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 257 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
253 | if(unsafe==string::npos) { | 258 | if(unsafe==string::npos) { |
254 | pseg.append(uri,n,ul-n-1); n = ul-1; | 259 | pseg.append(uri,n,ul-n-1); n = ul-1; |
255 | }else{ | 260 | }else{ |
256 | pseg.append(uri,n,unsafe-n); | 261 | pseg.append(uri,n,unsafe-n); |
257 | n = unsafe; | 262 | n = unsafe; |
@@ -266,51 +271,113 @@ namespace opkele { | |||
266 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 271 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
267 | int cc = strtol(tmp,0,16); | 272 | int cc = strtol(tmp,0,16); |
268 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 273 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
269 | pseg += cc; | 274 | pseg += cc; |
270 | else{ | 275 | else{ |
271 | pseg += '%'; | 276 | pseg += '%'; |
272 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 277 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
273 | } | 278 | } |
274 | }else if(qf) { | 279 | }else if(qf) { |
275 | rv += pseg; rv += c; | 280 | rv += pseg; rv += c; |
276 | pseg.clear(); | 281 | pseg.clear(); |
277 | }else if(n>=ul || strchr("?/#",c)) { | 282 | }else if(n>=ul || strchr("?/#",c)) { |
278 | if(pseg.empty() || pseg==".") { | 283 | if(pseg.empty() || pseg==".") { |
279 | }else if(pseg=="..") { | 284 | }else if(pseg=="..") { |
280 | if(psegs.size()>1) { | 285 | if(psegs.size()>1) { |
281 | rv.resize(psegs.top()); psegs.pop(); | 286 | rv.resize(psegs.top()); psegs.pop(); |
282 | } | 287 | } |
283 | }else{ | 288 | }else{ |
284 | psegs.push(rv.length()); | 289 | psegs.push(rv.length()); |
285 | if(c!='/') { | 290 | if(c!='/') { |
286 | pseg += c; | 291 | pseg += c; |
287 | qf = true; | 292 | qf = true; |
288 | } | 293 | } |
289 | rv += '/'; rv += pseg; | 294 | rv += '/'; rv += pseg; |
290 | } | 295 | } |
291 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 296 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
292 | rv += '/'; | 297 | rv += '/'; |
293 | if(n<ul) | 298 | if(n<ul) |
294 | qf = true; | 299 | qf = true; |
295 | }else if(strchr("?#",c)) { | 300 | }else if(strchr("?#",c)) { |
296 | if(psegs.size()==1 && psegs.top()==rv.length()) | 301 | if(psegs.size()==1 && psegs.top()==rv.length()) |
297 | rv += '/'; | 302 | rv += '/'; |
298 | if(pseg.empty()) | 303 | if(pseg.empty()) |
299 | rv += c; | 304 | rv += c; |
300 | qf = true; | 305 | qf = true; |
301 | } | 306 | } |
302 | pseg.clear(); | 307 | pseg.clear(); |
303 | }else{ | 308 | }else{ |
304 | pseg += c; | 309 | pseg += c; |
305 | } | 310 | } |
306 | } | 311 | } |
307 | if(!pseg.empty()) { | 312 | if(!pseg.empty()) { |
308 | if(!qf) rv += '/'; | 313 | if(!qf) rv += '/'; |
309 | rv += pseg; | 314 | rv += pseg; |
310 | } | 315 | } |
311 | return rv; | 316 | return rv; |
312 | } | 317 | } |
313 | 318 | ||
319 | string& strip_uri_fragment_part(string& u) { | ||
320 | string::size_type q = u.find('?'), f = u.find('#'); | ||
321 | if(q==string::npos) { | ||
322 | if(f!=string::npos) | ||
323 | u.erase(f); | ||
324 | }else{ | ||
325 | if(f!=string::npos) { | ||
326 | if(f<q) | ||
327 | u.erase(f,q-f); | ||
328 | else | ||
329 | u.erase(f); | ||
330 | } | ||
331 | } | ||
332 | return u; | ||
333 | } | ||
334 | |||
335 | string abi_demangle(const char *mn) { | ||
336 | #ifndef HAVE_DEMANGLE | ||
337 | return mn; | ||
338 | #else /* !HAVE_DEMANGLE */ | ||
339 | int dstat; | ||
340 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | ||
341 | if(dstat) | ||
342 | return mn; | ||
343 | string rv = demangled; | ||
344 | free(demangled); | ||
345 | return rv; | ||
346 | #endif /* !HAVE_DEMANGLE */ | ||
347 | } | ||
348 | |||
349 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | ||
350 | const string& slist = om.get_field("signed"); | ||
351 | string kv; | ||
352 | string::size_type p=0; | ||
353 | while(true) { | ||
354 | string::size_type co = slist.find(',',p); | ||
355 | string f = (co==string::npos) | ||
356 | ?slist.substr(p):slist.substr(p,co-p); | ||
357 | kv += f; | ||
358 | kv += ':'; | ||
359 | kv += om.get_field(f); | ||
360 | kv += '\n'; | ||
361 | if(co==string::npos) break; | ||
362 | p = co+1; | ||
363 | } | ||
364 | const secret_t& secret = assoc->secret(); | ||
365 | const EVP_MD *evpmd; | ||
366 | const string& at = assoc->assoc_type(); | ||
367 | if(at=="HMAC-SHA256") | ||
368 | evpmd = EVP_sha256(); | ||
369 | else if(at=="HMAC-SHA1") | ||
370 | evpmd = EVP_sha1(); | ||
371 | else | ||
372 | throw unsupported(OPKELE_CP_ "unknown association type"); | ||
373 | unsigned int md_len = 0; | ||
374 | unsigned char *md = HMAC(evpmd, | ||
375 | &(secret.front()),secret.size(), | ||
376 | (const unsigned char*)kv.data(),kv.length(), | ||
377 | 0,&md_len); | ||
378 | return encode_base64(md,md_len); | ||
379 | } | ||
380 | |||
314 | } | 381 | } |
315 | 382 | ||
316 | } | 383 | } |