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