author | Michael Krelin <hacker@klever.net> | 2008-02-19 10:51:12 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2008-02-19 10:51:12 (UTC) |
commit | a3db32747e8370cab8cfdcc382fee875613b7b77 (patch) (unidiff) | |
tree | 2d11728a195a85907f06c3f920e405f1d1c769cd | |
parent | c18b77c610d0f963a274420a6558629d198818ee (diff) | |
download | libopkele-a3db32747e8370cab8cfdcc382fee875613b7b77.zip libopkele-a3db32747e8370cab8cfdcc382fee875613b7b77.tar.gz libopkele-a3db32747e8370cab8cfdcc382fee875613b7b77.tar.bz2 |
use local array for hmac when calculating signature
Signed-off-by: Michael Krelin <hacker@klever.net>
-rw-r--r-- | lib/util.cc | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/util.cc b/lib/util.cc index b85a377..bb8a2e8 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,432 +1,435 @@ | |||
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 <algorithm> | 8 | #include <algorithm> |
9 | #include <openssl/bio.h> | 9 | #include <openssl/bio.h> |
10 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
11 | #include <openssl/sha.h> | ||
11 | #include <openssl/hmac.h> | 12 | #include <openssl/hmac.h> |
12 | #include <curl/curl.h> | 13 | #include <curl/curl.h> |
13 | #include "opkele/util.h" | 14 | #include <opkele/util.h> |
14 | #include "opkele/exception.h" | 15 | #include <opkele/exception.h> |
16 | #include <opkele/debug.h> | ||
15 | 17 | ||
16 | #include <config.h> | 18 | #include <config.h> |
17 | #ifdef HAVE_DEMANGLE | 19 | #ifdef HAVE_DEMANGLE |
18 | # include <cxxabi.h> | 20 | # include <cxxabi.h> |
19 | #endif | 21 | #endif |
20 | 22 | ||
21 | namespace opkele { | 23 | namespace opkele { |
22 | using namespace std; | 24 | using namespace std; |
23 | 25 | ||
24 | namespace util { | 26 | namespace util { |
25 | 27 | ||
26 | /* | 28 | /* |
27 | * base64 | 29 | * base64 |
28 | */ | 30 | */ |
29 | string encode_base64(const void *data,size_t length) { | 31 | string encode_base64(const void *data,size_t length) { |
30 | BIO *b64 = 0, *bmem = 0; | 32 | BIO *b64 = 0, *bmem = 0; |
31 | try { | 33 | try { |
32 | b64 = BIO_new(BIO_f_base64()); | 34 | b64 = BIO_new(BIO_f_base64()); |
33 | if(!b64) | 35 | if(!b64) |
34 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 36 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
35 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 37 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
36 | bmem = BIO_new(BIO_s_mem()); | 38 | bmem = BIO_new(BIO_s_mem()); |
37 | BIO_set_flags(b64,BIO_CLOSE); | 39 | BIO_set_flags(b64,BIO_CLOSE); |
38 | if(!bmem) | 40 | if(!bmem) |
39 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
40 | BIO_push(b64,bmem); | 42 | BIO_push(b64,bmem); |
41 | if(((size_t)BIO_write(b64,data,length))!=length) | 43 | if(((size_t)BIO_write(b64,data,length))!=length) |
42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
43 | if(BIO_flush(b64)!=1) | 45 | if(BIO_flush(b64)!=1) |
44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 46 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
45 | char *rvd; | 47 | char *rvd; |
46 | long rvl = BIO_get_mem_data(bmem,&rvd); | 48 | long rvl = BIO_get_mem_data(bmem,&rvd); |
47 | string rv(rvd,rvl); | 49 | string rv(rvd,rvl); |
48 | BIO_free_all(b64); | 50 | BIO_free_all(b64); |
49 | return rv; | 51 | return rv; |
50 | }catch(...) { | 52 | }catch(...) { |
51 | if(b64) BIO_free_all(b64); | 53 | if(b64) BIO_free_all(b64); |
52 | throw; | 54 | throw; |
53 | } | 55 | } |
54 | } | 56 | } |
55 | 57 | ||
56 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 58 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
57 | BIO *b64 = 0, *bmem = 0; | 59 | BIO *b64 = 0, *bmem = 0; |
58 | rv.clear(); | 60 | rv.clear(); |
59 | try { | 61 | try { |
60 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 62 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
61 | if(!bmem) | 63 | if(!bmem) |
62 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
63 | b64 = BIO_new(BIO_f_base64()); | 65 | b64 = BIO_new(BIO_f_base64()); |
64 | if(!b64) | 66 | if(!b64) |
65 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 67 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
66 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 68 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
67 | BIO_push(b64,bmem); | 69 | BIO_push(b64,bmem); |
68 | unsigned char tmp[512]; | 70 | unsigned char tmp[512]; |
69 | size_t rb = 0; | 71 | size_t rb = 0; |
70 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 72 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
71 | rv.insert(rv.end(),tmp,&tmp[rb]); | 73 | rv.insert(rv.end(),tmp,&tmp[rb]); |
72 | BIO_free_all(b64); | 74 | BIO_free_all(b64); |
73 | }catch(...) { | 75 | }catch(...) { |
74 | if(b64) BIO_free_all(b64); | 76 | if(b64) BIO_free_all(b64); |
75 | throw; | 77 | throw; |
76 | } | 78 | } |
77 | } | 79 | } |
78 | 80 | ||
79 | /* | 81 | /* |
80 | * big numerics | 82 | * big numerics |
81 | */ | 83 | */ |
82 | 84 | ||
83 | BIGNUM *base64_to_bignum(const string& b64) { | 85 | BIGNUM *base64_to_bignum(const string& b64) { |
84 | vector<unsigned char> bin; | 86 | vector<unsigned char> bin; |
85 | decode_base64(b64,bin); | 87 | decode_base64(b64,bin); |
86 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 88 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
87 | if(!rv) | 89 | if(!rv) |
88 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 90 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
89 | return rv; | 91 | return rv; |
90 | } | 92 | } |
91 | 93 | ||
92 | BIGNUM *dec_to_bignum(const string& dec) { | 94 | BIGNUM *dec_to_bignum(const string& dec) { |
93 | BIGNUM *rv = 0; | 95 | BIGNUM *rv = 0; |
94 | if(!BN_dec2bn(&rv,dec.c_str())) | 96 | if(!BN_dec2bn(&rv,dec.c_str())) |
95 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 97 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
96 | return rv; | 98 | return rv; |
97 | } | 99 | } |
98 | 100 | ||
99 | string bignum_to_base64(const BIGNUM *bn) { | 101 | string bignum_to_base64(const BIGNUM *bn) { |
100 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 102 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
101 | unsigned char *binptr = &(bin.front())+1; | 103 | unsigned char *binptr = &(bin.front())+1; |
102 | int l = BN_bn2bin(bn,binptr); | 104 | int l = BN_bn2bin(bn,binptr); |
103 | if(l && (*binptr)&0x80){ | 105 | if(l && (*binptr)&0x80){ |
104 | (*(--binptr)) = 0; ++l; | 106 | (*(--binptr)) = 0; ++l; |
105 | } | 107 | } |
106 | return encode_base64(binptr,l); | 108 | return encode_base64(binptr,l); |
107 | } | 109 | } |
108 | 110 | ||
109 | /* | 111 | /* |
110 | * w3c times | 112 | * w3c times |
111 | */ | 113 | */ |
112 | 114 | ||
113 | string time_to_w3c(time_t t) { | 115 | string time_to_w3c(time_t t) { |
114 | struct tm tm_t; | 116 | struct tm tm_t; |
115 | if(!gmtime_r(&t,&tm_t)) | 117 | if(!gmtime_r(&t,&tm_t)) |
116 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 118 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
117 | char rv[25]; | 119 | char rv[25]; |
118 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 120 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
119 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 121 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
120 | return rv; | 122 | return rv; |
121 | } | 123 | } |
122 | 124 | ||
123 | time_t w3c_to_time(const string& w) { | 125 | time_t w3c_to_time(const string& w) { |
124 | int fraction; | 126 | int fraction; |
125 | struct tm tm_t; | 127 | struct tm tm_t; |
126 | memset(&tm_t,0,sizeof(tm_t)); | 128 | memset(&tm_t,0,sizeof(tm_t)); |
127 | if( ( | 129 | if( ( |
128 | sscanf( | 130 | sscanf( |
129 | w.c_str(), | 131 | w.c_str(), |
130 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 132 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
131 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 133 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
132 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec | 134 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
133 | ) != 6 | 135 | ) != 6 |
134 | ) && ( | 136 | ) && ( |
135 | sscanf( | 137 | sscanf( |
136 | w.c_str(), | 138 | w.c_str(), |
137 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 139 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
138 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 140 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, |
139 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, | 141 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
140 | &fraction | 142 | &fraction |
141 | ) != 7 | 143 | ) != 7 |
142 | ) ) | 144 | ) ) |
143 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 145 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
144 | tm_t.tm_mon--; | 146 | tm_t.tm_mon--; |
145 | tm_t.tm_year-=1900; | 147 | tm_t.tm_year-=1900; |
146 | time_t rv = mktime(&tm_t); | 148 | time_t rv = mktime(&tm_t); |
147 | if(rv==(time_t)-1) | 149 | if(rv==(time_t)-1) |
148 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 150 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
149 | return rv-timezone; | 151 | return rv-timezone; |
150 | } | 152 | } |
151 | 153 | ||
152 | /* | 154 | /* |
153 | * | 155 | * |
154 | */ | 156 | */ |
155 | 157 | ||
156 | string url_encode(const string& str) { | 158 | string url_encode(const string& str) { |
157 | char * t = curl_escape(str.c_str(),str.length()); | 159 | char * t = curl_escape(str.c_str(),str.length()); |
158 | if(!t) | 160 | if(!t) |
159 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 161 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
160 | string rv(t); | 162 | string rv(t); |
161 | curl_free(t); | 163 | curl_free(t); |
162 | return rv; | 164 | return rv; |
163 | } | 165 | } |
164 | 166 | ||
165 | string attr_escape(const string& str) { | 167 | string attr_escape(const string& str) { |
166 | static const char *unsafechars = "<>&\n\"'"; | 168 | static const char *unsafechars = "<>&\n\"'"; |
167 | string rv; | 169 | string rv; |
168 | string::size_type p=0; | 170 | string::size_type p=0; |
169 | while(true) { | 171 | while(true) { |
170 | string::size_type us = str.find_first_of(unsafechars,p); | 172 | string::size_type us = str.find_first_of(unsafechars,p); |
171 | if(us==string::npos) { | 173 | if(us==string::npos) { |
172 | if(p!=str.length()) | 174 | if(p!=str.length()) |
173 | rv.append(str,p,str.length()-p); | 175 | rv.append(str,p,str.length()-p); |
174 | return rv; | 176 | return rv; |
175 | } | 177 | } |
176 | rv.append(str,p,us-p); | 178 | rv.append(str,p,us-p); |
177 | rv += "&#"; | 179 | rv += "&#"; |
178 | rv += long_to_string((long)str[us]); | 180 | rv += long_to_string((long)str[us]); |
179 | rv += ';'; | 181 | rv += ';'; |
180 | p = us+1; | 182 | p = us+1; |
181 | } | 183 | } |
182 | } | 184 | } |
183 | 185 | ||
184 | string long_to_string(long l) { | 186 | string long_to_string(long l) { |
185 | char rv[32]; | 187 | char rv[32]; |
186 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 188 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
187 | if(r<0 || r>=(int)sizeof(rv)) | 189 | if(r<0 || r>=(int)sizeof(rv)) |
188 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 190 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
189 | return rv; | 191 | return rv; |
190 | } | 192 | } |
191 | 193 | ||
192 | long string_to_long(const string& s) { | 194 | long string_to_long(const string& s) { |
193 | char *endptr = 0; | 195 | char *endptr = 0; |
194 | long rv = strtol(s.c_str(),&endptr,10); | 196 | long rv = strtol(s.c_str(),&endptr,10); |
195 | if((!endptr) || endptr==s.c_str()) | 197 | if((!endptr) || endptr==s.c_str()) |
196 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 198 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
197 | return rv; | 199 | return rv; |
198 | } | 200 | } |
199 | 201 | ||
200 | /* | 202 | /* |
201 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 203 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
202 | * | 204 | * |
203 | * - uppercase hex triplets (e.g. %ab -> %AB) | 205 | * - uppercase hex triplets (e.g. %ab -> %AB) |
204 | * - lowercase scheme and host | 206 | * - lowercase scheme and host |
205 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 207 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
206 | * that is - [:alpha:][:digit:]._~- | 208 | * that is - [:alpha:][:digit:]._~- |
207 | * - remove dot segments | 209 | * - remove dot segments |
208 | * - remove empty and default ports | 210 | * - remove empty and default ports |
209 | * - if there's no path component, add '/' | 211 | * - if there's no path component, add '/' |
210 | */ | 212 | */ |
211 | string rfc_3986_normalize_uri(const string& uri) { | 213 | string rfc_3986_normalize_uri(const string& uri) { |
212 | static const char *whitespace = " \t\r\n"; | 214 | static const char *whitespace = " \t\r\n"; |
213 | string rv; | 215 | string rv; |
214 | string::size_type ns = uri.find_first_not_of(whitespace); | 216 | string::size_type ns = uri.find_first_not_of(whitespace); |
215 | if(ns==string::npos) | 217 | if(ns==string::npos) |
216 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 218 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
217 | string::size_type colon = uri.find(':',ns); | 219 | string::size_type colon = uri.find(':',ns); |
218 | if(colon==string::npos) | 220 | if(colon==string::npos) |
219 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 221 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
220 | transform( | 222 | transform( |
221 | uri.begin()+ns, uri.begin()+colon+1, | 223 | uri.begin()+ns, uri.begin()+colon+1, |
222 | back_inserter(rv), ::tolower ); | 224 | back_inserter(rv), ::tolower ); |
223 | bool s; | 225 | bool s; |
224 | string::size_type ul = uri.find_last_not_of(whitespace)+1; | 226 | string::size_type ul = uri.find_last_not_of(whitespace)+1; |
225 | if(ul <= (colon+3)) | 227 | if(ul <= (colon+3)) |
226 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 228 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
227 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 229 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
228 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 230 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
229 | if(rv=="http:") | 231 | if(rv=="http:") |
230 | s = false; | 232 | s = false; |
231 | else if(rv=="https:") | 233 | else if(rv=="https:") |
232 | s = true; | 234 | s = true; |
233 | else{ | 235 | else{ |
234 | /* TODO: support more schemes. e.g. xri. How do we normalize | 236 | /* TODO: support more schemes. e.g. xri. How do we normalize |
235 | * xri? | 237 | * xri? |
236 | */ | 238 | */ |
237 | rv.append(uri,colon+1,ul-colon-1); | 239 | rv.append(uri,colon+1,ul-colon-1); |
238 | return rv; | 240 | return rv; |
239 | } | 241 | } |
240 | rv += "//"; | 242 | rv += "//"; |
241 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 243 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
242 | if(interesting==string::npos) { | 244 | if(interesting==string::npos) { |
243 | transform( | 245 | transform( |
244 | uri.begin()+colon+3,uri.begin()+ul, | 246 | uri.begin()+colon+3,uri.begin()+ul, |
245 | back_inserter(rv), ::tolower ); | 247 | back_inserter(rv), ::tolower ); |
246 | rv += '/'; return rv; | 248 | rv += '/'; return rv; |
247 | } | 249 | } |
248 | transform( | 250 | transform( |
249 | uri.begin()+colon+3,uri.begin()+interesting, | 251 | uri.begin()+colon+3,uri.begin()+interesting, |
250 | back_inserter(rv), ::tolower ); | 252 | back_inserter(rv), ::tolower ); |
251 | bool qf = false; | 253 | bool qf = false; |
252 | char ic = uri[interesting]; | 254 | char ic = uri[interesting]; |
253 | if(ic==':') { | 255 | if(ic==':') { |
254 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 256 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
255 | const char *nptr = uri.data()+interesting+1; | 257 | const char *nptr = uri.data()+interesting+1; |
256 | char *eptr = 0; | 258 | char *eptr = 0; |
257 | long port = strtol(nptr,&eptr,10); | 259 | long port = strtol(nptr,&eptr,10); |
258 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 260 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
259 | char tmp[8]; | 261 | char tmp[8]; |
260 | snprintf(tmp,sizeof(tmp),":%ld",port); | 262 | snprintf(tmp,sizeof(tmp),":%ld",port); |
261 | rv += tmp; | 263 | rv += tmp; |
262 | } | 264 | } |
263 | if(ni==string::npos) { | 265 | if(ni==string::npos) { |
264 | rv += '/'; return rv; | 266 | rv += '/'; return rv; |
265 | } | 267 | } |
266 | interesting = ni; | 268 | interesting = ni; |
267 | }else if(ic!='/') { | 269 | }else if(ic!='/') { |
268 | rv += '/'; rv += ic; | 270 | rv += '/'; rv += ic; |
269 | qf = true; | 271 | qf = true; |
270 | ++interesting; | 272 | ++interesting; |
271 | } | 273 | } |
272 | string::size_type n = interesting; | 274 | string::size_type n = interesting; |
273 | char tmp[3] = { 0,0,0 }; | 275 | char tmp[3] = { 0,0,0 }; |
274 | stack<string::size_type> psegs; psegs.push(rv.length()); | 276 | stack<string::size_type> psegs; psegs.push(rv.length()); |
275 | string pseg; | 277 | string pseg; |
276 | for(;n<ul;) { | 278 | for(;n<ul;) { |
277 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 279 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
278 | if(unsafe==string::npos) { | 280 | if(unsafe==string::npos) { |
279 | pseg.append(uri,n,ul-n-1); n = ul-1; | 281 | pseg.append(uri,n,ul-n-1); n = ul-1; |
280 | }else{ | 282 | }else{ |
281 | pseg.append(uri,n,unsafe-n); | 283 | pseg.append(uri,n,unsafe-n); |
282 | n = unsafe; | 284 | n = unsafe; |
283 | } | 285 | } |
284 | char c = uri[n++]; | 286 | char c = uri[n++]; |
285 | if(c=='%') { | 287 | if(c=='%') { |
286 | if((n+1)>=ul) | 288 | if((n+1)>=ul) |
287 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 289 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
288 | tmp[0] = uri[n++]; | 290 | tmp[0] = uri[n++]; |
289 | tmp[1] = uri[n++]; | 291 | tmp[1] = uri[n++]; |
290 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 292 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
291 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 293 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
292 | int cc = strtol(tmp,0,16); | 294 | int cc = strtol(tmp,0,16); |
293 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 295 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
294 | pseg += cc; | 296 | pseg += cc; |
295 | else{ | 297 | else{ |
296 | pseg += '%'; | 298 | pseg += '%'; |
297 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 299 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
298 | } | 300 | } |
299 | }else if(qf) { | 301 | }else if(qf) { |
300 | rv += pseg; rv += c; | 302 | rv += pseg; rv += c; |
301 | pseg.clear(); | 303 | pseg.clear(); |
302 | }else if(n>=ul || strchr("?/#",c)) { | 304 | }else if(n>=ul || strchr("?/#",c)) { |
303 | if(pseg.empty() || pseg==".") { | 305 | if(pseg.empty() || pseg==".") { |
304 | }else if(pseg=="..") { | 306 | }else if(pseg=="..") { |
305 | if(psegs.size()>1) { | 307 | if(psegs.size()>1) { |
306 | rv.resize(psegs.top()); psegs.pop(); | 308 | rv.resize(psegs.top()); psegs.pop(); |
307 | } | 309 | } |
308 | }else{ | 310 | }else{ |
309 | psegs.push(rv.length()); | 311 | psegs.push(rv.length()); |
310 | if(c!='/') { | 312 | if(c!='/') { |
311 | pseg += c; | 313 | pseg += c; |
312 | qf = true; | 314 | qf = true; |
313 | } | 315 | } |
314 | rv += '/'; rv += pseg; | 316 | rv += '/'; rv += pseg; |
315 | } | 317 | } |
316 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 318 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
317 | rv += '/'; | 319 | rv += '/'; |
318 | if(n<ul) | 320 | if(n<ul) |
319 | qf = true; | 321 | qf = true; |
320 | }else if(strchr("?#",c)) { | 322 | }else if(strchr("?#",c)) { |
321 | if(psegs.size()==1 && psegs.top()==rv.length()) | 323 | if(psegs.size()==1 && psegs.top()==rv.length()) |
322 | rv += '/'; | 324 | rv += '/'; |
323 | if(pseg.empty()) | 325 | if(pseg.empty()) |
324 | rv += c; | 326 | rv += c; |
325 | qf = true; | 327 | qf = true; |
326 | } | 328 | } |
327 | pseg.clear(); | 329 | pseg.clear(); |
328 | }else{ | 330 | }else{ |
329 | pseg += c; | 331 | pseg += c; |
330 | } | 332 | } |
331 | } | 333 | } |
332 | if(!pseg.empty()) { | 334 | if(!pseg.empty()) { |
333 | if(!qf) rv += '/'; | 335 | if(!qf) rv += '/'; |
334 | rv += pseg; | 336 | rv += pseg; |
335 | } | 337 | } |
336 | return rv; | 338 | return rv; |
337 | } | 339 | } |
338 | 340 | ||
339 | string& strip_uri_fragment_part(string& u) { | 341 | string& strip_uri_fragment_part(string& u) { |
340 | string::size_type q = u.find('?'), f = u.find('#'); | 342 | string::size_type q = u.find('?'), f = u.find('#'); |
341 | if(q==string::npos) { | 343 | if(q==string::npos) { |
342 | if(f!=string::npos) | 344 | if(f!=string::npos) |
343 | u.erase(f); | 345 | u.erase(f); |
344 | }else{ | 346 | }else{ |
345 | if(f!=string::npos) { | 347 | if(f!=string::npos) { |
346 | if(f<q) | 348 | if(f<q) |
347 | u.erase(f,q-f); | 349 | u.erase(f,q-f); |
348 | else | 350 | else |
349 | u.erase(f); | 351 | u.erase(f); |
350 | } | 352 | } |
351 | } | 353 | } |
352 | return u; | 354 | return u; |
353 | } | 355 | } |
354 | 356 | ||
355 | bool uri_matches_realm(const string& uri,const string& realm) { | 357 | bool uri_matches_realm(const string& uri,const string& realm) { |
356 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 358 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); |
357 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | 359 | string nu = opkele::util::rfc_3986_normalize_uri(uri); |
358 | string::size_type pr = nrealm.find("://"); | 360 | string::size_type pr = nrealm.find("://"); |
359 | string::size_type pu = nu.find("://"); | 361 | string::size_type pu = nu.find("://"); |
360 | assert(!(pr==string::npos || pu==string::npos)); | 362 | assert(!(pr==string::npos || pu==string::npos)); |
361 | pr += sizeof("://")-1; | 363 | pr += sizeof("://")-1; |
362 | pu += sizeof("://")-1; | 364 | pu += sizeof("://")-1; |
363 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | 365 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { |
364 | pr = nrealm.find('.',pr); | 366 | pr = nrealm.find('.',pr); |
365 | pu = nu.find('.',pu); | 367 | pu = nu.find('.',pu); |
366 | assert(pr!=string::npos); | 368 | assert(pr!=string::npos); |
367 | if(pu==string::npos) | 369 | if(pu==string::npos) |
368 | return false; | 370 | return false; |
369 | // TODO: check for overgeneralized realm | 371 | // TODO: check for overgeneralized realm |
370 | } | 372 | } |
371 | string::size_type lr = nrealm.length(); | 373 | string::size_type lr = nrealm.length(); |
372 | string::size_type lu = nu.length(); | 374 | string::size_type lu = nu.length(); |
373 | if( (lu-pu) < (lr-pr) ) | 375 | if( (lu-pu) < (lr-pr) ) |
374 | return false; | 376 | return false; |
375 | pair<const char*,const char*> mp = mismatch( | 377 | pair<const char*,const char*> mp = mismatch( |
376 | nrealm.c_str()+pr,nrealm.c_str()+lr, | 378 | nrealm.c_str()+pr,nrealm.c_str()+lr, |
377 | nu.c_str()+pu); | 379 | nu.c_str()+pu); |
378 | if( (*(mp.first-1))!='/' | 380 | if( (*(mp.first-1))!='/' |
379 | && !strchr("/?#",*mp.second) ) | 381 | && !strchr("/?#",*mp.second) ) |
380 | return false; | 382 | return false; |
381 | return true; | 383 | return true; |
382 | } | 384 | } |
383 | 385 | ||
384 | string abi_demangle(const char *mn) { | 386 | string abi_demangle(const char *mn) { |
385 | #ifndef HAVE_DEMANGLE | 387 | #ifndef HAVE_DEMANGLE |
386 | return mn; | 388 | return mn; |
387 | #else /* !HAVE_DEMANGLE */ | 389 | #else /* !HAVE_DEMANGLE */ |
388 | int dstat; | 390 | int dstat; |
389 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 391 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
390 | if(dstat) | 392 | if(dstat) |
391 | return mn; | 393 | return mn; |
392 | string rv = demangled; | 394 | string rv = demangled; |
393 | free(demangled); | 395 | free(demangled); |
394 | return rv; | 396 | return rv; |
395 | #endif /* !HAVE_DEMANGLE */ | 397 | #endif /* !HAVE_DEMANGLE */ |
396 | } | 398 | } |
397 | 399 | ||
398 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 400 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
399 | const string& slist = om.get_field("signed"); | 401 | const string& slist = om.get_field("signed"); |
400 | string kv; | 402 | string kv; |
401 | string::size_type p=0; | 403 | string::size_type p=0; |
402 | while(true) { | 404 | while(true) { |
403 | string::size_type co = slist.find(',',p); | 405 | string::size_type co = slist.find(',',p); |
404 | string f = (co==string::npos) | 406 | string f = (co==string::npos) |
405 | ?slist.substr(p):slist.substr(p,co-p); | 407 | ?slist.substr(p):slist.substr(p,co-p); |
406 | kv += f; | 408 | kv += f; |
407 | kv += ':'; | 409 | kv += ':'; |
408 | kv += om.get_field(f); | 410 | kv += om.get_field(f); |
409 | kv += '\n'; | 411 | kv += '\n'; |
410 | if(co==string::npos) break; | 412 | if(co==string::npos) break; |
411 | p = co+1; | 413 | p = co+1; |
412 | } | 414 | } |
413 | const secret_t& secret = assoc->secret(); | 415 | const secret_t& secret = assoc->secret(); |
414 | const EVP_MD *evpmd; | 416 | const EVP_MD *evpmd; |
415 | const string& at = assoc->assoc_type(); | 417 | const string& at = assoc->assoc_type(); |
416 | if(at=="HMAC-SHA256") | 418 | if(at=="HMAC-SHA256") |
417 | evpmd = EVP_sha256(); | 419 | evpmd = EVP_sha256(); |
418 | else if(at=="HMAC-SHA1") | 420 | else if(at=="HMAC-SHA1") |
419 | evpmd = EVP_sha1(); | 421 | evpmd = EVP_sha1(); |
420 | else | 422 | else |
421 | throw unsupported(OPKELE_CP_ "unknown association type"); | 423 | throw unsupported(OPKELE_CP_ "unknown association type"); |
422 | unsigned int md_len = 0; | 424 | unsigned int md_len = 0; |
423 | unsigned char *md = HMAC(evpmd, | 425 | unsigned char md[SHA256_DIGEST_LENGTH]; |
426 | HMAC(evpmd, | ||
424 | &(secret.front()),secret.size(), | 427 | &(secret.front()),secret.size(), |
425 | (const unsigned char*)kv.data(),kv.length(), | 428 | (const unsigned char*)kv.data(),kv.length(), |
426 | 0,&md_len); | 429 | md,&md_len); |
427 | return encode_base64(md,md_len); | 430 | return encode_base64(md,md_len); |
428 | } | 431 | } |
429 | 432 | ||
430 | } | 433 | } |
431 | 434 | ||
432 | } | 435 | } |