-rw-r--r-- | include/opkele/util.h | 8 | ||||
-rw-r--r-- | lib/basic_op.cc | 35 | ||||
-rw-r--r-- | lib/util.cc | 30 |
3 files changed, 41 insertions, 32 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h index 719f951..bc1a0ea 100644 --- a/include/opkele/util.h +++ b/include/opkele/util.h | |||
@@ -1,176 +1,184 @@ | |||
1 | #ifndef __OPKELE_UTIL_H | 1 | #ifndef __OPKELE_UTIL_H |
2 | #define __OPKELE_UTIL_H | 2 | #define __OPKELE_UTIL_H |
3 | 3 | ||
4 | #include <time.h> | 4 | #include <time.h> |
5 | #include <string> | 5 | #include <string> |
6 | #include <vector> | 6 | #include <vector> |
7 | #include <openssl/bn.h> | 7 | #include <openssl/bn.h> |
8 | #include <openssl/dh.h> | 8 | #include <openssl/dh.h> |
9 | #include <opkele/types.h> | 9 | #include <opkele/types.h> |
10 | 10 | ||
11 | namespace opkele { | 11 | namespace opkele { |
12 | using std::string; | 12 | using std::string; |
13 | using std::vector; | 13 | using std::vector; |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * @brief opkele utils namespace | 16 | * @brief opkele utils namespace |
17 | */ | 17 | */ |
18 | namespace util { | 18 | namespace util { |
19 | 19 | ||
20 | /** | 20 | /** |
21 | * Convenience class encapsulating SSL BIGNUM object for the purpose of | 21 | * Convenience class encapsulating SSL BIGNUM object for the purpose of |
22 | * automatical freeing. | 22 | * automatical freeing. |
23 | */ | 23 | */ |
24 | class bignum_t { | 24 | class bignum_t { |
25 | public: | 25 | public: |
26 | BIGNUM *_bn; | 26 | BIGNUM *_bn; |
27 | 27 | ||
28 | bignum_t() : _bn(0) { } | 28 | bignum_t() : _bn(0) { } |
29 | bignum_t(BIGNUM *bn) : _bn(bn) { } | 29 | bignum_t(BIGNUM *bn) : _bn(bn) { } |
30 | ~bignum_t() throw() { if(_bn) BN_free(_bn); } | 30 | ~bignum_t() throw() { if(_bn) BN_free(_bn); } |
31 | 31 | ||
32 | bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; } | 32 | bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; } |
33 | 33 | ||
34 | operator const BIGNUM*(void) const { return _bn; } | 34 | operator const BIGNUM*(void) const { return _bn; } |
35 | operator BIGNUM*(void) { return _bn; } | 35 | operator BIGNUM*(void) { return _bn; } |
36 | }; | 36 | }; |
37 | /** | 37 | /** |
38 | * Convenience clas encapsulating SSL DH object for the purpose of | 38 | * Convenience clas encapsulating SSL DH object for the purpose of |
39 | * automatic freeing. | 39 | * automatic freeing. |
40 | */ | 40 | */ |
41 | class dh_t { | 41 | class dh_t { |
42 | public: | 42 | public: |
43 | DH *_dh; | 43 | DH *_dh; |
44 | 44 | ||
45 | dh_t() : _dh(0) { } | 45 | dh_t() : _dh(0) { } |
46 | dh_t(DH *dh) : _dh(dh) { } | 46 | dh_t(DH *dh) : _dh(dh) { } |
47 | ~dh_t() throw() { if(_dh) DH_free(_dh); } | 47 | ~dh_t() throw() { if(_dh) DH_free(_dh); } |
48 | 48 | ||
49 | dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; } | 49 | dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; } |
50 | 50 | ||
51 | operator const DH*(void) const { return _dh; } | 51 | operator const DH*(void) const { return _dh; } |
52 | operator DH*(void) { return _dh; } | 52 | operator DH*(void) { return _dh; } |
53 | 53 | ||
54 | DH* operator->() { return _dh; } | 54 | DH* operator->() { return _dh; } |
55 | const DH* operator->() const { return _dh; } | 55 | const DH* operator->() const { return _dh; } |
56 | }; | 56 | }; |
57 | 57 | ||
58 | /** | 58 | /** |
59 | * Convert base64-encoded SSL BIGNUM to internal representation. | 59 | * Convert base64-encoded SSL BIGNUM to internal representation. |
60 | * @param b64 base64-encoded number | 60 | * @param b64 base64-encoded number |
61 | * @return SSL BIGNUM | 61 | * @return SSL BIGNUM |
62 | * @throw failed_conversion in case of error | 62 | * @throw failed_conversion in case of error |
63 | */ | 63 | */ |
64 | BIGNUM *base64_to_bignum(const string& b64); | 64 | BIGNUM *base64_to_bignum(const string& b64); |
65 | /** | 65 | /** |
66 | * Convert decimal representation to SSL BIGNUM. | 66 | * Convert decimal representation to SSL BIGNUM. |
67 | * @param dec decimal representation | 67 | * @param dec decimal representation |
68 | * @return resulting BIGNUM | 68 | * @return resulting BIGNUM |
69 | * @throw failed_conversion in case of error | 69 | * @throw failed_conversion in case of error |
70 | */ | 70 | */ |
71 | BIGNUM *dec_to_bignum(const string& dec); | 71 | BIGNUM *dec_to_bignum(const string& dec); |
72 | /** | 72 | /** |
73 | * Convert SSL BIGNUM data to base64 encoded string. | 73 | * Convert SSL BIGNUM data to base64 encoded string. |
74 | * @param bn BIGNUM | 74 | * @param bn BIGNUM |
75 | * @return base64encoded string | 75 | * @return base64encoded string |
76 | */ | 76 | */ |
77 | string bignum_to_base64(const BIGNUM *bn); | 77 | string bignum_to_base64(const BIGNUM *bn); |
78 | 78 | ||
79 | /** | 79 | /** |
80 | * Convert internal time representation to w3c format | 80 | * Convert internal time representation to w3c format |
81 | * @param t internal representation | 81 | * @param t internal representation |
82 | * @return w3c time | 82 | * @return w3c time |
83 | * @throw failed_conversion in case of error | 83 | * @throw failed_conversion in case of error |
84 | */ | 84 | */ |
85 | string time_to_w3c(time_t t); | 85 | string time_to_w3c(time_t t); |
86 | /** | 86 | /** |
87 | * Convert W3C time representation to internal time_t | 87 | * Convert W3C time representation to internal time_t |
88 | * @param w w3c representation | 88 | * @param w w3c representation |
89 | * @return converted time | 89 | * @return converted time |
90 | * @throw failed_conversion in case of error | 90 | * @throw failed_conversion in case of error |
91 | */ | 91 | */ |
92 | time_t w3c_to_time(const string& w); | 92 | time_t w3c_to_time(const string& w); |
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Encode string to the representation suitable for using in URL. | 95 | * Encode string to the representation suitable for using in URL. |
96 | * @param str string to encode | 96 | * @param str string to encode |
97 | * @return encoded string | 97 | * @return encoded string |
98 | * @throw failed_conversion in case of failure | 98 | * @throw failed_conversion in case of failure |
99 | */ | 99 | */ |
100 | string url_encode(const string& str); | 100 | string url_encode(const string& str); |
101 | 101 | ||
102 | /** | 102 | /** |
103 | * Make string suitable for using as x(ht)ml attribute. | 103 | * Make string suitable for using as x(ht)ml attribute. |
104 | * @param str string to escape | 104 | * @param str string to escape |
105 | * @return escaped string | 105 | * @return escaped string |
106 | */ | 106 | */ |
107 | string attr_escape(const string& str); | 107 | string attr_escape(const string& str); |
108 | 108 | ||
109 | /** | 109 | /** |
110 | * Convert number to string | 110 | * Convert number to string |
111 | * @param l number | 111 | * @param l number |
112 | * @return string representation | 112 | * @return string representation |
113 | * @throw failed_conversion in case of failure | 113 | * @throw failed_conversion in case of failure |
114 | */ | 114 | */ |
115 | string long_to_string(long l); | 115 | string long_to_string(long l); |
116 | /** | 116 | /** |
117 | * Convert string to number | 117 | * Convert string to number |
118 | * @param s string, containing the number | 118 | * @param s string, containing the number |
119 | * @return the number | 119 | * @return the number |
120 | * @throw failed_conversion in case of failure | 120 | * @throw failed_conversion in case of failure |
121 | */ | 121 | */ |
122 | long string_to_long(const string& s); | 122 | long string_to_long(const string& s); |
123 | 123 | ||
124 | /** | 124 | /** |
125 | * Encode binary data using base64. | 125 | * Encode binary data using base64. |
126 | * @param data pointer to binary data | 126 | * @param data pointer to binary data |
127 | * @param length length of data | 127 | * @param length length of data |
128 | * @return encoded data | 128 | * @return encoded data |
129 | */ | 129 | */ |
130 | string encode_base64(const void *data,size_t length); | 130 | string encode_base64(const void *data,size_t length); |
131 | /** | 131 | /** |
132 | * Decode binary data from base64 representation. | 132 | * Decode binary data from base64 representation. |
133 | * @param data base64-encoded data | 133 | * @param data base64-encoded data |
134 | * @param rv container for decoded binary | 134 | * @param rv container for decoded binary |
135 | */ | 135 | */ |
136 | void decode_base64(const string& data,vector<unsigned char>& rv); | 136 | void decode_base64(const string& data,vector<unsigned char>& rv); |
137 | 137 | ||
138 | /** | 138 | /** |
139 | * Normalize http(s) URI according to RFC3986, section 6. URI is | 139 | * Normalize http(s) URI according to RFC3986, section 6. URI is |
140 | * expected to have scheme: in front of it. | 140 | * expected to have scheme: in front of it. |
141 | * @param uri URI | 141 | * @param uri URI |
142 | * @return normalized URI | 142 | * @return normalized URI |
143 | * @throw not_implemented in case of non-httpi(s) URI | 143 | * @throw not_implemented in case of non-httpi(s) URI |
144 | * @throw bad_input in case of malformed URI | 144 | * @throw bad_input in case of malformed URI |
145 | */ | 145 | */ |
146 | string rfc_3986_normalize_uri(const string& uri); | 146 | string rfc_3986_normalize_uri(const string& uri); |
147 | 147 | ||
148 | /** | ||
149 | * Match URI against realm | ||
150 | * @param uri URI to match | ||
151 | * @param realm realm to match against | ||
152 | * @return true if URI matches realm | ||
153 | */ | ||
154 | bool uri_matches_realm(const string& uri,const string& realm); | ||
155 | |||
148 | string& strip_uri_fragment_part(string& uri); | 156 | string& strip_uri_fragment_part(string& uri); |
149 | 157 | ||
150 | string abi_demangle(const char* mn); | 158 | string abi_demangle(const char* mn); |
151 | 159 | ||
152 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om); | 160 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om); |
153 | 161 | ||
154 | class change_mode_message_proxy : public basic_openid_message { | 162 | class change_mode_message_proxy : public basic_openid_message { |
155 | public: | 163 | public: |
156 | const basic_openid_message& x; | 164 | const basic_openid_message& x; |
157 | const string& mode; | 165 | const string& mode; |
158 | 166 | ||
159 | change_mode_message_proxy(const basic_openid_message& xx,const string& m) : x(xx), mode(m) { } | 167 | change_mode_message_proxy(const basic_openid_message& xx,const string& m) : x(xx), mode(m) { } |
160 | 168 | ||
161 | bool has_field(const string& n) const { return x.has_field(n); } | 169 | bool has_field(const string& n) const { return x.has_field(n); } |
162 | const string& get_field(const string& n) const { | 170 | const string& get_field(const string& n) const { |
163 | return (n=="mode")?mode:x.get_field(n); } | 171 | return (n=="mode")?mode:x.get_field(n); } |
164 | bool has_ns(const string& uri) const {return x.has_ns(uri); } | 172 | bool has_ns(const string& uri) const {return x.has_ns(uri); } |
165 | string get_ns(const string& uri) const { return x.get_ns(uri); } | 173 | string get_ns(const string& uri) const { return x.get_ns(uri); } |
166 | fields_iterator fields_begin() const { | 174 | fields_iterator fields_begin() const { |
167 | return x.fields_begin(); } | 175 | return x.fields_begin(); } |
168 | fields_iterator fields_end() const { | 176 | fields_iterator fields_end() const { |
169 | return x.fields_end(); } | 177 | return x.fields_end(); } |
170 | }; | 178 | }; |
171 | 179 | ||
172 | } | 180 | } |
173 | 181 | ||
174 | } | 182 | } |
175 | 183 | ||
176 | #endif /* __OPKELE_UTIL_H */ | 184 | #endif /* __OPKELE_UTIL_H */ |
diff --git a/lib/basic_op.cc b/lib/basic_op.cc index f7573aa..11ffb48 100644 --- a/lib/basic_op.cc +++ b/lib/basic_op.cc | |||
@@ -1,356 +1,327 @@ | |||
1 | #include <time.h> | 1 | #include <time.h> |
2 | #include <cassert> | 2 | #include <cassert> |
3 | #include <algorithm> | ||
4 | #include <openssl/sha.h> | 3 | #include <openssl/sha.h> |
5 | #include <openssl/hmac.h> | 4 | #include <openssl/hmac.h> |
6 | #include <opkele/data.h> | 5 | #include <opkele/data.h> |
7 | #include <opkele/basic_op.h> | 6 | #include <opkele/basic_op.h> |
8 | #include <opkele/exception.h> | 7 | #include <opkele/exception.h> |
9 | #include <opkele/util.h> | 8 | #include <opkele/util.h> |
10 | #include <opkele/uris.h> | 9 | #include <opkele/uris.h> |
11 | 10 | ||
12 | namespace opkele { | 11 | namespace opkele { |
13 | using std::pair; | ||
14 | using std::mismatch; | ||
15 | 12 | ||
16 | void basic_op::reset_vars() { | 13 | void basic_op::reset_vars() { |
17 | assoc.reset(); | 14 | assoc.reset(); |
18 | return_to.clear(); realm.clear(); | 15 | return_to.clear(); realm.clear(); |
19 | claimed_id.clear(); identity.clear(); | 16 | claimed_id.clear(); identity.clear(); |
20 | invalidate_handle.clear(); | 17 | invalidate_handle.clear(); |
21 | } | 18 | } |
22 | 19 | ||
23 | bool basic_op::has_return_to() const { | 20 | bool basic_op::has_return_to() const { |
24 | return !return_to.empty(); | 21 | return !return_to.empty(); |
25 | } | 22 | } |
26 | const string& basic_op::get_return_to() const { | 23 | const string& basic_op::get_return_to() const { |
27 | if(return_to.empty()) | 24 | if(return_to.empty()) |
28 | throw no_return_to(OPKELE_CP_ "No return_to URL provided with request"); | 25 | throw no_return_to(OPKELE_CP_ "No return_to URL provided with request"); |
29 | return return_to; | 26 | return return_to; |
30 | } | 27 | } |
31 | 28 | ||
32 | const string& basic_op::get_realm() const { | 29 | const string& basic_op::get_realm() const { |
33 | assert(!realm.empty()); | 30 | assert(!realm.empty()); |
34 | return realm; | 31 | return realm; |
35 | } | 32 | } |
36 | 33 | ||
37 | bool basic_op::has_identity() const { | 34 | bool basic_op::has_identity() const { |
38 | return !identity.empty(); | 35 | return !identity.empty(); |
39 | } | 36 | } |
40 | const string& basic_op::get_claimed_id() const { | 37 | const string& basic_op::get_claimed_id() const { |
41 | if(claimed_id.empty()) | 38 | if(claimed_id.empty()) |
42 | throw non_identity(OPKELE_CP_ "attempting to retrieve claimed_id of non-identity related request"); | 39 | throw non_identity(OPKELE_CP_ "attempting to retrieve claimed_id of non-identity related request"); |
43 | assert(!identity.empty()); | 40 | assert(!identity.empty()); |
44 | return claimed_id; | 41 | return claimed_id; |
45 | } | 42 | } |
46 | const string& basic_op::get_identity() const { | 43 | const string& basic_op::get_identity() const { |
47 | if(identity.empty()) | 44 | if(identity.empty()) |
48 | throw non_identity(OPKELE_CP_ "attempting to retrieve identity of non-identity related request"); | 45 | throw non_identity(OPKELE_CP_ "attempting to retrieve identity of non-identity related request"); |
49 | assert(!claimed_id.empty()); | 46 | assert(!claimed_id.empty()); |
50 | return identity; | 47 | return identity; |
51 | } | 48 | } |
52 | 49 | ||
53 | bool basic_op::is_id_select() const { | 50 | bool basic_op::is_id_select() const { |
54 | return identity==IDURI_SELECT20; | 51 | return identity==IDURI_SELECT20; |
55 | } | 52 | } |
56 | 53 | ||
57 | void basic_op::select_identity(const string& c,const string& i) { | 54 | void basic_op::select_identity(const string& c,const string& i) { |
58 | claimed_id = c; identity = i; | 55 | claimed_id = c; identity = i; |
59 | } | 56 | } |
60 | void basic_op::set_claimed_id(const string& c) { | 57 | void basic_op::set_claimed_id(const string& c) { |
61 | claimed_id = c; | 58 | claimed_id = c; |
62 | } | 59 | } |
63 | 60 | ||
64 | basic_openid_message& basic_op::associate( | 61 | basic_openid_message& basic_op::associate( |
65 | basic_openid_message& oum, | 62 | basic_openid_message& oum, |
66 | const basic_openid_message& inm) try { | 63 | const basic_openid_message& inm) try { |
67 | assert(inm.get_field("mode")=="associate"); | 64 | assert(inm.get_field("mode")=="associate"); |
68 | util::dh_t dh; | 65 | util::dh_t dh; |
69 | util::bignum_t c_pub; | 66 | util::bignum_t c_pub; |
70 | unsigned char key_digest[SHA256_DIGEST_LENGTH]; | 67 | unsigned char key_digest[SHA256_DIGEST_LENGTH]; |
71 | size_t d_len = 0; | 68 | size_t d_len = 0; |
72 | enum { | 69 | enum { |
73 | sess_cleartext, sess_dh_sha1, sess_dh_sha256 | 70 | sess_cleartext, sess_dh_sha1, sess_dh_sha256 |
74 | } st = sess_cleartext; | 71 | } st = sess_cleartext; |
75 | string sts = inm.get_field("session_type"); | 72 | string sts = inm.get_field("session_type"); |
76 | string ats = inm.get_field("assoc_type"); | 73 | string ats = inm.get_field("assoc_type"); |
77 | if(sts=="DH-SHA1" || sts=="DH-SHA256") { | 74 | if(sts=="DH-SHA1" || sts=="DH-SHA256") { |
78 | if(!(dh = DH_new())) | 75 | if(!(dh = DH_new())) |
79 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); | 76 | throw exception_openssl(OPKELE_CP_ "failed to DH_new()"); |
80 | c_pub = util::base64_to_bignum(inm.get_field("dh_consumer_public")); | 77 | c_pub = util::base64_to_bignum(inm.get_field("dh_consumer_public")); |
81 | try { dh->p = util::base64_to_bignum(inm.get_field("dh_modulus")); | 78 | try { dh->p = util::base64_to_bignum(inm.get_field("dh_modulus")); |
82 | }catch(failed_lookup&) { | 79 | }catch(failed_lookup&) { |
83 | dh->p = util::dec_to_bignum(data::_default_p); } | 80 | dh->p = util::dec_to_bignum(data::_default_p); } |
84 | try { dh->g = util::base64_to_bignum(inm.get_field("dh_gen")); | 81 | try { dh->g = util::base64_to_bignum(inm.get_field("dh_gen")); |
85 | }catch(failed_lookup&) { | 82 | }catch(failed_lookup&) { |
86 | dh->g = util::dec_to_bignum(data::_default_g); } | 83 | dh->g = util::dec_to_bignum(data::_default_g); } |
87 | if(!DH_generate_key(dh)) | 84 | if(!DH_generate_key(dh)) |
88 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); | 85 | throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); |
89 | vector<unsigned char> ck(DH_size(dh)+1); | 86 | vector<unsigned char> ck(DH_size(dh)+1); |
90 | unsigned char *ckptr = &(ck.front())+1; | 87 | unsigned char *ckptr = &(ck.front())+1; |
91 | int cklen = DH_compute_key(ckptr,c_pub,dh); | 88 | int cklen = DH_compute_key(ckptr,c_pub,dh); |
92 | if(cklen<0) | 89 | if(cklen<0) |
93 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); | 90 | throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); |
94 | if(cklen && (*ckptr)&0x80) { | 91 | if(cklen && (*ckptr)&0x80) { |
95 | (*(--ckptr)) = 0; ++cklen; } | 92 | (*(--ckptr)) = 0; ++cklen; } |
96 | if(sts=="DH-SHA1") { | 93 | if(sts=="DH-SHA1") { |
97 | SHA1(ckptr,cklen,key_digest); d_len = SHA_DIGEST_LENGTH; | 94 | SHA1(ckptr,cklen,key_digest); d_len = SHA_DIGEST_LENGTH; |
98 | }else if(sts=="DH-SHA256") { | 95 | }else if(sts=="DH-SHA256") { |
99 | SHA256(ckptr,cklen,key_digest); d_len = SHA256_DIGEST_LENGTH; | 96 | SHA256(ckptr,cklen,key_digest); d_len = SHA256_DIGEST_LENGTH; |
100 | }else | 97 | }else |
101 | throw internal_error(OPKELE_CP_ "I thought I knew the session type"); | 98 | throw internal_error(OPKELE_CP_ "I thought I knew the session type"); |
102 | }else | 99 | }else |
103 | throw unsupported(OPKELE_CP_ "Unsupported session_type"); | 100 | throw unsupported(OPKELE_CP_ "Unsupported session_type"); |
104 | assoc_t assoc; | 101 | assoc_t assoc; |
105 | if(ats=="HMAC-SHA1") | 102 | if(ats=="HMAC-SHA1") |
106 | assoc = alloc_assoc(ats,SHA_DIGEST_LENGTH,true); | 103 | assoc = alloc_assoc(ats,SHA_DIGEST_LENGTH,true); |
107 | else if(ats=="HMAC-SHA256") | 104 | else if(ats=="HMAC-SHA256") |
108 | assoc = alloc_assoc(ats,SHA256_DIGEST_LENGTH,true); | 105 | assoc = alloc_assoc(ats,SHA256_DIGEST_LENGTH,true); |
109 | else | 106 | else |
110 | throw unsupported(OPKELE_CP_ "Unsupported assoc_type"); | 107 | throw unsupported(OPKELE_CP_ "Unsupported assoc_type"); |
111 | oum.reset_fields(); | 108 | oum.reset_fields(); |
112 | oum.set_field("ns",OIURI_OPENID20); | 109 | oum.set_field("ns",OIURI_OPENID20); |
113 | oum.set_field("assoc_type",assoc->assoc_type()); | 110 | oum.set_field("assoc_type",assoc->assoc_type()); |
114 | oum.set_field("assoc_handle",assoc->handle()); | 111 | oum.set_field("assoc_handle",assoc->handle()); |
115 | oum.set_field("expires_in",util::long_to_string(assoc->expires_in())); | 112 | oum.set_field("expires_in",util::long_to_string(assoc->expires_in())); |
116 | secret_t secret = assoc->secret(); | 113 | secret_t secret = assoc->secret(); |
117 | if(sts=="DH-SHA1" || sts=="DH-SHA256") { | 114 | if(sts=="DH-SHA1" || sts=="DH-SHA256") { |
118 | if(d_len != secret.size()) | 115 | if(d_len != secret.size()) |
119 | throw bad_input(OPKELE_CP_ "Association secret and session MAC are not of the same size"); | 116 | throw bad_input(OPKELE_CP_ "Association secret and session MAC are not of the same size"); |
120 | oum.set_field("session_type",sts); | 117 | oum.set_field("session_type",sts); |
121 | oum.set_field("dh_server_public",util::bignum_to_base64(dh->pub_key)); | 118 | oum.set_field("dh_server_public",util::bignum_to_base64(dh->pub_key)); |
122 | string b64; secret.enxor_to_base64(key_digest,b64); | 119 | string b64; secret.enxor_to_base64(key_digest,b64); |
123 | oum.set_field("enc_mac_key",b64); | 120 | oum.set_field("enc_mac_key",b64); |
124 | }else /* TODO: support cleartext over encrypted connection */ | 121 | }else /* TODO: support cleartext over encrypted connection */ |
125 | throw unsupported(OPKELE_CP_ "Unsupported session type"); | 122 | throw unsupported(OPKELE_CP_ "Unsupported session type"); |
126 | return oum; | 123 | return oum; |
127 | } catch(unsupported& u) { | 124 | } catch(unsupported& u) { |
128 | oum.reset_fields(); | 125 | oum.reset_fields(); |
129 | oum.set_field("ns",OIURI_OPENID20); | 126 | oum.set_field("ns",OIURI_OPENID20); |
130 | oum.set_field("error",u.what()); | 127 | oum.set_field("error",u.what()); |
131 | oum.set_field("error_code","unsupported-type"); | 128 | oum.set_field("error_code","unsupported-type"); |
132 | oum.set_field("session_type","DH-SHA256"); | 129 | oum.set_field("session_type","DH-SHA256"); |
133 | oum.set_field("assoc_type","HMAC-SHA256"); | 130 | oum.set_field("assoc_type","HMAC-SHA256"); |
134 | return oum; | 131 | return oum; |
135 | } | 132 | } |
136 | 133 | ||
137 | void basic_op::checkid_(const basic_openid_message& inm, | 134 | void basic_op::checkid_(const basic_openid_message& inm, |
138 | extension_t *ext) { | 135 | extension_t *ext) { |
139 | reset_vars(); | 136 | reset_vars(); |
140 | string mode = inm.get_field("mode"); | 137 | string mode = inm.get_field("mode"); |
141 | if(mode=="checkid_setup") | 138 | if(mode=="checkid_setup") |
142 | mode = mode_checkid_setup; | 139 | mode = mode_checkid_setup; |
143 | else if(mode=="checkid_immediate") | 140 | else if(mode=="checkid_immediate") |
144 | mode = mode_checkid_immediate; | 141 | mode = mode_checkid_immediate; |
145 | else | 142 | else |
146 | throw bad_input(OPKELE_CP_ "Invalid checkid_* mode"); | 143 | throw bad_input(OPKELE_CP_ "Invalid checkid_* mode"); |
147 | try { | 144 | try { |
148 | assoc = retrieve_assoc(invalidate_handle=inm.get_field("assoc_handle")); | 145 | assoc = retrieve_assoc(invalidate_handle=inm.get_field("assoc_handle")); |
149 | invalidate_handle.clear(); | 146 | invalidate_handle.clear(); |
150 | }catch(failed_lookup&) { | 147 | }catch(failed_lookup&) { |
151 | // no handle specified or no valid assoc found, go dumb | 148 | // no handle specified or no valid assoc found, go dumb |
152 | assoc = alloc_assoc("HMAC-SHA256",SHA256_DIGEST_LENGTH,true); | 149 | assoc = alloc_assoc("HMAC-SHA256",SHA256_DIGEST_LENGTH,true); |
153 | } | 150 | } |
154 | try { | 151 | try { |
155 | openid2 = (inm.get_field("ns")==OIURI_OPENID20); | 152 | openid2 = (inm.get_field("ns")==OIURI_OPENID20); |
156 | }catch(failed_lookup&) { openid2 = false; } | 153 | }catch(failed_lookup&) { openid2 = false; } |
157 | try { | 154 | try { |
158 | return_to = inm.get_field("return_to"); | 155 | return_to = inm.get_field("return_to"); |
159 | }catch(failed_lookup&) { } | 156 | }catch(failed_lookup&) { } |
160 | if(openid2) { | 157 | if(openid2) { |
161 | try { | 158 | try { |
162 | realm = inm.get_field("realm"); | 159 | realm = inm.get_field("realm"); |
163 | }catch(failed_lookup&) { | 160 | }catch(failed_lookup&) { |
164 | try { | 161 | try { |
165 | realm = inm.get_field("trust_root"); | 162 | realm = inm.get_field("trust_root"); |
166 | }catch(failed_lookup&) { | 163 | }catch(failed_lookup&) { |
167 | if(return_to.empty()) | 164 | if(return_to.empty()) |
168 | throw bad_input(OPKELE_CP_ | 165 | throw bad_input(OPKELE_CP_ |
169 | "Both realm and return_to are unset"); | 166 | "Both realm and return_to are unset"); |
170 | realm = return_to; | 167 | realm = return_to; |
171 | } | 168 | } |
172 | } | 169 | } |
173 | }else{ | 170 | }else{ |
174 | try { | 171 | try { |
175 | realm = inm.get_field("trust_root"); | 172 | realm = inm.get_field("trust_root"); |
176 | }catch(failed_lookup&) { | 173 | }catch(failed_lookup&) { |
177 | if(return_to.empty()) | 174 | if(return_to.empty()) |
178 | throw bad_input(OPKELE_CP_ | 175 | throw bad_input(OPKELE_CP_ |
179 | "Both realm and return_to are unset"); | 176 | "Both realm and return_to are unset"); |
180 | realm = return_to; | 177 | realm = return_to; |
181 | } | 178 | } |
182 | } | 179 | } |
183 | try { | 180 | try { |
184 | identity = inm.get_field("identity"); | 181 | identity = inm.get_field("identity"); |
185 | try { | 182 | try { |
186 | claimed_id = inm.get_field("claimed_id"); | 183 | claimed_id = inm.get_field("claimed_id"); |
187 | }catch(failed_lookup&) { | 184 | }catch(failed_lookup&) { |
188 | if(openid2) | 185 | if(openid2) |
189 | throw bad_input(OPKELE_CP_ | 186 | throw bad_input(OPKELE_CP_ |
190 | "claimed_id and identity must be either both present or both absent"); | 187 | "claimed_id and identity must be either both present or both absent"); |
191 | } | 188 | } |
192 | }catch(failed_lookup&) { | 189 | }catch(failed_lookup&) { |
193 | if(openid2 && inm.has_field("claimed_id")) | 190 | if(openid2 && inm.has_field("claimed_id")) |
194 | throw bad_input(OPKELE_CP_ | 191 | throw bad_input(OPKELE_CP_ |
195 | "claimed_id and identity must be either both present or both absent"); | 192 | "claimed_id and identity must be either both present or both absent"); |
196 | } | 193 | } |
197 | verify_return_to(); | 194 | verify_return_to(); |
198 | } | 195 | } |
199 | 196 | ||
200 | basic_openid_message& basic_op::id_res(basic_openid_message& om) { | 197 | basic_openid_message& basic_op::id_res(basic_openid_message& om) { |
201 | assert(assoc); | 198 | assert(assoc); |
202 | assert(!return_to.empty()); | 199 | assert(!return_to.empty()); |
203 | assert(!is_id_select()); | 200 | assert(!is_id_select()); |
204 | time_t now = time(0); | 201 | time_t now = time(0); |
205 | struct tm gmt; gmtime_r(&now,&gmt); | 202 | struct tm gmt; gmtime_r(&now,&gmt); |
206 | char w3timestr[24]; | 203 | char w3timestr[24]; |
207 | if(!strftime(w3timestr,sizeof(w3timestr),"%Y-%m-%dT%H:%M:%SZ",&gmt)) | 204 | if(!strftime(w3timestr,sizeof(w3timestr),"%Y-%m-%dT%H:%M:%SZ",&gmt)) |
208 | throw failed_conversion(OPKELE_CP_ | 205 | throw failed_conversion(OPKELE_CP_ |
209 | "Failed to build time string for nonce" ); | 206 | "Failed to build time string for nonce" ); |
210 | om.set_field("ns",OIURI_OPENID20); | 207 | om.set_field("ns",OIURI_OPENID20); |
211 | om.set_field("mode","id_res"); | 208 | om.set_field("mode","id_res"); |
212 | om.set_field("op_endpoint",get_op_endpoint()); | 209 | om.set_field("op_endpoint",get_op_endpoint()); |
213 | string ats = "ns,mode,op_endpoint,return_to,response_nonce," | 210 | string ats = "ns,mode,op_endpoint,return_to,response_nonce," |
214 | "assoc_handle,signed"; | 211 | "assoc_handle,signed"; |
215 | if(!identity.empty()) { | 212 | if(!identity.empty()) { |
216 | om.set_field("identity",identity); | 213 | om.set_field("identity",identity); |
217 | om.set_field("claimed_id",claimed_id); | 214 | om.set_field("claimed_id",claimed_id); |
218 | ats += ",identity,claimed_id"; | 215 | ats += ",identity,claimed_id"; |
219 | } | 216 | } |
220 | om.set_field("return_to",return_to); | 217 | om.set_field("return_to",return_to); |
221 | string nonce = w3timestr; | 218 | string nonce = w3timestr; |
222 | om.set_field("response_nonce",alloc_nonce(nonce,assoc->stateless())); | 219 | om.set_field("response_nonce",alloc_nonce(nonce,assoc->stateless())); |
223 | if(!invalidate_handle.empty()) { | 220 | if(!invalidate_handle.empty()) { |
224 | om.set_field("invalidate_handle",invalidate_handle); | 221 | om.set_field("invalidate_handle",invalidate_handle); |
225 | ats += ",invalidate_handle"; | 222 | ats += ",invalidate_handle"; |
226 | } | 223 | } |
227 | om.set_field("assoc_handle",assoc->handle()); | 224 | om.set_field("assoc_handle",assoc->handle()); |
228 | om.add_to_signed(ats); | 225 | om.add_to_signed(ats); |
229 | om.set_field("sig",util::base64_signature(assoc,om)); | 226 | om.set_field("sig",util::base64_signature(assoc,om)); |
230 | return om; | 227 | return om; |
231 | } | 228 | } |
232 | 229 | ||
233 | basic_openid_message& basic_op::cancel(basic_openid_message& om) { | 230 | basic_openid_message& basic_op::cancel(basic_openid_message& om) { |
234 | assert(!return_to.empty()); | 231 | assert(!return_to.empty()); |
235 | om.set_field("ns",OIURI_OPENID20); | 232 | om.set_field("ns",OIURI_OPENID20); |
236 | om.set_field("mode","cancel"); | 233 | om.set_field("mode","cancel"); |
237 | return om; | 234 | return om; |
238 | } | 235 | } |
239 | 236 | ||
240 | basic_openid_message& basic_op::error(basic_openid_message& om, | 237 | basic_openid_message& basic_op::error(basic_openid_message& om, |
241 | const string& error,const string& contact, | 238 | const string& error,const string& contact, |
242 | const string& reference ) { | 239 | const string& reference ) { |
243 | assert(!return_to.empty()); | 240 | assert(!return_to.empty()); |
244 | om.set_field("ns",OIURI_OPENID20); | 241 | om.set_field("ns",OIURI_OPENID20); |
245 | om.set_field("mode","error"); | 242 | om.set_field("mode","error"); |
246 | om.set_field("error",error); | 243 | om.set_field("error",error); |
247 | om.set_field("contact",contact); | 244 | om.set_field("contact",contact); |
248 | om.set_field("reference",reference); | 245 | om.set_field("reference",reference); |
249 | return om; | 246 | return om; |
250 | } | 247 | } |
251 | 248 | ||
252 | basic_openid_message& basic_op::setup_needed( | 249 | basic_openid_message& basic_op::setup_needed( |
253 | basic_openid_message& oum,const basic_openid_message& inm) { | 250 | basic_openid_message& oum,const basic_openid_message& inm) { |
254 | assert(mode==mode_checkid_immediate); | 251 | assert(mode==mode_checkid_immediate); |
255 | assert(!return_to.empty()); | 252 | assert(!return_to.empty()); |
256 | if(openid2) { | 253 | if(openid2) { |
257 | oum.set_field("ns",OIURI_OPENID20); | 254 | oum.set_field("ns",OIURI_OPENID20); |
258 | oum.set_field("mode","setup_needed"); | 255 | oum.set_field("mode","setup_needed"); |
259 | }else{ | 256 | }else{ |
260 | oum.set_field("mode","id_res"); | 257 | oum.set_field("mode","id_res"); |
261 | static const string setupmode = "checkid_setup"; | 258 | static const string setupmode = "checkid_setup"; |
262 | oum.set_field("user_setup_url", | 259 | oum.set_field("user_setup_url", |
263 | util::change_mode_message_proxy(inm,setupmode) | 260 | util::change_mode_message_proxy(inm,setupmode) |
264 | .append_query(get_op_endpoint())); | 261 | .append_query(get_op_endpoint())); |
265 | } | 262 | } |
266 | return oum; | 263 | return oum; |
267 | } | 264 | } |
268 | 265 | ||
269 | basic_openid_message& basic_op::check_authentication( | 266 | basic_openid_message& basic_op::check_authentication( |
270 | basic_openid_message& oum, | 267 | basic_openid_message& oum, |
271 | const basic_openid_message& inm) try { | 268 | const basic_openid_message& inm) try { |
272 | assert(inm.get_field("mode")=="check_authentication"); | 269 | assert(inm.get_field("mode")=="check_authentication"); |
273 | oum.reset_fields(); | 270 | oum.reset_fields(); |
274 | oum.set_field("ns",OIURI_OPENID20); | 271 | oum.set_field("ns",OIURI_OPENID20); |
275 | bool o2; | 272 | bool o2; |
276 | try { | 273 | try { |
277 | o2 = (inm.get_field("ns")==OIURI_OPENID20); | 274 | o2 = (inm.get_field("ns")==OIURI_OPENID20); |
278 | }catch(failed_lookup&) { o2 = false; } | 275 | }catch(failed_lookup&) { o2 = false; } |
279 | string nonce; | 276 | string nonce; |
280 | if(o2) { | 277 | if(o2) { |
281 | try { | 278 | try { |
282 | if(!check_nonce(nonce = inm.get_field("response_nonce"))) | 279 | if(!check_nonce(nonce = inm.get_field("response_nonce"))) |
283 | throw failed_check_authentication(OPKELE_CP_ "Invalid nonce"); | 280 | throw failed_check_authentication(OPKELE_CP_ "Invalid nonce"); |
284 | }catch(failed_lookup&) { | 281 | }catch(failed_lookup&) { |
285 | throw failed_check_authentication(OPKELE_CP_ "No nonce provided with check_authentication request"); | 282 | throw failed_check_authentication(OPKELE_CP_ "No nonce provided with check_authentication request"); |
286 | } | 283 | } |
287 | } | 284 | } |
288 | try { | 285 | try { |
289 | assoc = retrieve_assoc(inm.get_field("assoc_handle")); | 286 | assoc = retrieve_assoc(inm.get_field("assoc_handle")); |
290 | if(!assoc->stateless()) | 287 | if(!assoc->stateless()) |
291 | throw failed_check_authentication(OPKELE_CP_ "Will not do check_authentication on a stateful handle"); | 288 | throw failed_check_authentication(OPKELE_CP_ "Will not do check_authentication on a stateful handle"); |
292 | }catch(failed_lookup&) { | 289 | }catch(failed_lookup&) { |
293 | throw failed_check_authentication(OPKELE_CP_ "No assoc_handle or invalid assoc_handle specified with check_authentication request"); | 290 | throw failed_check_authentication(OPKELE_CP_ "No assoc_handle or invalid assoc_handle specified with check_authentication request"); |
294 | } | 291 | } |
295 | static const string idresmode = "id_res"; | 292 | static const string idresmode = "id_res"; |
296 | try { | 293 | try { |
297 | if(util::base64_signature(assoc,util::change_mode_message_proxy(inm,idresmode))!=inm.get_field("sig")) | 294 | if(util::base64_signature(assoc,util::change_mode_message_proxy(inm,idresmode))!=inm.get_field("sig")) |
298 | throw failed_check_authentication(OPKELE_CP_ "Signature mismatch"); | 295 | throw failed_check_authentication(OPKELE_CP_ "Signature mismatch"); |
299 | }catch(failed_lookup&) { | 296 | }catch(failed_lookup&) { |
300 | throw failed_check_authentication(OPKELE_CP_ "failed to calculate signature"); | 297 | throw failed_check_authentication(OPKELE_CP_ "failed to calculate signature"); |
301 | } | 298 | } |
302 | oum.set_field("is_valid","true"); | 299 | oum.set_field("is_valid","true"); |
303 | try { | 300 | try { |
304 | string h = inm.get_field("invalidate_handle"); | 301 | string h = inm.get_field("invalidate_handle"); |
305 | try { | 302 | try { |
306 | assoc_t ih = retrieve_assoc(h); | 303 | assoc_t ih = retrieve_assoc(h); |
307 | }catch(invalid_handle& ih) { | 304 | }catch(invalid_handle& ih) { |
308 | oum.set_field("invalidate_handle",h); | 305 | oum.set_field("invalidate_handle",h); |
309 | }catch(failed_lookup& ih) { | 306 | }catch(failed_lookup& ih) { |
310 | oum.set_field("invalidate_handle",h); | 307 | oum.set_field("invalidate_handle",h); |
311 | } | 308 | } |
312 | }catch(failed_lookup&) { } | 309 | }catch(failed_lookup&) { } |
313 | if(o2) { | 310 | if(o2) { |
314 | assert(!nonce.empty()); | 311 | assert(!nonce.empty()); |
315 | invalidate_nonce(nonce); | 312 | invalidate_nonce(nonce); |
316 | } | 313 | } |
317 | return oum; | 314 | return oum; |
318 | }catch(failed_check_authentication& ) { | 315 | }catch(failed_check_authentication& ) { |
319 | oum.set_field("is_valid","false"); | 316 | oum.set_field("is_valid","false"); |
320 | return oum; | 317 | return oum; |
321 | } | 318 | } |
322 | 319 | ||
323 | void basic_op::verify_return_to() { | 320 | void basic_op::verify_return_to() { |
324 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | 321 | if(realm.find('#')!=string::npos) |
325 | if(nrealm.find('#')!=string::npos) | ||
326 | throw opkele::bad_realm(OPKELE_CP_ "authentication realm contains URI fragment"); | 322 | throw opkele::bad_realm(OPKELE_CP_ "authentication realm contains URI fragment"); |
327 | string nrt = opkele::util::rfc_3986_normalize_uri(return_to); | 323 | if(!util::uri_matches_realm(return_to,realm)) |
328 | string::size_type pr = nrealm.find("://"); | 324 | throw bad_return_to(OPKELE_CP_ "return_to URL doesn't match realm"); |
329 | string::size_type prt = nrt.find("://"); | ||
330 | assert(!(pr==string::npos || prt==string::npos)); | ||
331 | pr += sizeof("://")-1; | ||
332 | prt += sizeof("://")-1; | ||
333 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | ||
334 | pr = nrealm.find('.',pr); | ||
335 | prt = nrt.find('.',prt); | ||
336 | assert(pr!=string::npos); | ||
337 | if(prt==string::npos) | ||
338 | throw bad_return_to( | ||
339 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
340 | // TODO: check for overgeneralized realm | ||
341 | } | ||
342 | string::size_type lr = nrealm.length(); | ||
343 | string::size_type lrt = nrt.length(); | ||
344 | if( (lrt-prt) < (lr-pr) ) | ||
345 | throw bad_return_to( | ||
346 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
347 | pair<const char*,const char*> mp = mismatch( | ||
348 | nrealm.c_str()+pr,nrealm.c_str()+lr, | ||
349 | nrt.c_str()+prt); | ||
350 | if( (*(mp.first-1))!='/' | ||
351 | && !strchr("/?#",*mp.second) ) | ||
352 | throw bad_return_to( | ||
353 | OPKELE_CP_ "return_to URL doesn't match realm"); | ||
354 | } | 325 | } |
355 | 326 | ||
356 | } | 327 | } |
diff --git a/lib/util.cc b/lib/util.cc index b7bc437..b85a377 100644 --- a/lib/util.cc +++ b/lib/util.cc | |||
@@ -1,402 +1,432 @@ | |||
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 <openssl/bio.h> | 9 | #include <openssl/bio.h> |
9 | #include <openssl/evp.h> | 10 | #include <openssl/evp.h> |
10 | #include <openssl/hmac.h> | 11 | #include <openssl/hmac.h> |
11 | #include <curl/curl.h> | 12 | #include <curl/curl.h> |
12 | #include "opkele/util.h" | 13 | #include "opkele/util.h" |
13 | #include "opkele/exception.h" | 14 | #include "opkele/exception.h" |
14 | 15 | ||
15 | #include <config.h> | 16 | #include <config.h> |
16 | #ifdef HAVE_DEMANGLE | 17 | #ifdef HAVE_DEMANGLE |
17 | # include <cxxabi.h> | 18 | # include <cxxabi.h> |
18 | #endif | 19 | #endif |
19 | 20 | ||
20 | namespace opkele { | 21 | namespace opkele { |
21 | using namespace std; | 22 | using namespace std; |
22 | 23 | ||
23 | namespace util { | 24 | namespace util { |
24 | 25 | ||
25 | /* | 26 | /* |
26 | * base64 | 27 | * base64 |
27 | */ | 28 | */ |
28 | string encode_base64(const void *data,size_t length) { | 29 | string encode_base64(const void *data,size_t length) { |
29 | BIO *b64 = 0, *bmem = 0; | 30 | BIO *b64 = 0, *bmem = 0; |
30 | try { | 31 | try { |
31 | b64 = BIO_new(BIO_f_base64()); | 32 | b64 = BIO_new(BIO_f_base64()); |
32 | if(!b64) | 33 | if(!b64) |
33 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); | 34 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 encoder"); |
34 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 35 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
35 | bmem = BIO_new(BIO_s_mem()); | 36 | bmem = BIO_new(BIO_s_mem()); |
36 | BIO_set_flags(b64,BIO_CLOSE); | 37 | BIO_set_flags(b64,BIO_CLOSE); |
37 | if(!bmem) | 38 | if(!bmem) |
38 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); | 39 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() memory buffer"); |
39 | BIO_push(b64,bmem); | 40 | BIO_push(b64,bmem); |
40 | if(((size_t)BIO_write(b64,data,length))!=length) | 41 | if(((size_t)BIO_write(b64,data,length))!=length) |
41 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); | 42 | throw exception_openssl(OPKELE_CP_ "failed to BIO_write()"); |
42 | if(BIO_flush(b64)!=1) | 43 | if(BIO_flush(b64)!=1) |
43 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); | 44 | throw exception_openssl(OPKELE_CP_ "failed to BIO_flush()"); |
44 | char *rvd; | 45 | char *rvd; |
45 | long rvl = BIO_get_mem_data(bmem,&rvd); | 46 | long rvl = BIO_get_mem_data(bmem,&rvd); |
46 | string rv(rvd,rvl); | 47 | string rv(rvd,rvl); |
47 | BIO_free_all(b64); | 48 | BIO_free_all(b64); |
48 | return rv; | 49 | return rv; |
49 | }catch(...) { | 50 | }catch(...) { |
50 | if(b64) BIO_free_all(b64); | 51 | if(b64) BIO_free_all(b64); |
51 | throw; | 52 | throw; |
52 | } | 53 | } |
53 | } | 54 | } |
54 | 55 | ||
55 | void decode_base64(const string& data,vector<unsigned char>& rv) { | 56 | void decode_base64(const string& data,vector<unsigned char>& rv) { |
56 | BIO *b64 = 0, *bmem = 0; | 57 | BIO *b64 = 0, *bmem = 0; |
57 | rv.clear(); | 58 | rv.clear(); |
58 | try { | 59 | try { |
59 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); | 60 | bmem = BIO_new_mem_buf((void*)data.data(),data.size()); |
60 | if(!bmem) | 61 | if(!bmem) |
61 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); | 62 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new_mem_buf()"); |
62 | b64 = BIO_new(BIO_f_base64()); | 63 | b64 = BIO_new(BIO_f_base64()); |
63 | if(!b64) | 64 | if(!b64) |
64 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); | 65 | throw exception_openssl(OPKELE_CP_ "failed to BIO_new() base64 decoder"); |
65 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); | 66 | BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL); |
66 | BIO_push(b64,bmem); | 67 | BIO_push(b64,bmem); |
67 | unsigned char tmp[512]; | 68 | unsigned char tmp[512]; |
68 | size_t rb = 0; | 69 | size_t rb = 0; |
69 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) | 70 | while((rb=BIO_read(b64,tmp,sizeof(tmp)))>0) |
70 | rv.insert(rv.end(),tmp,&tmp[rb]); | 71 | rv.insert(rv.end(),tmp,&tmp[rb]); |
71 | BIO_free_all(b64); | 72 | BIO_free_all(b64); |
72 | }catch(...) { | 73 | }catch(...) { |
73 | if(b64) BIO_free_all(b64); | 74 | if(b64) BIO_free_all(b64); |
74 | throw; | 75 | throw; |
75 | } | 76 | } |
76 | } | 77 | } |
77 | 78 | ||
78 | /* | 79 | /* |
79 | * big numerics | 80 | * big numerics |
80 | */ | 81 | */ |
81 | 82 | ||
82 | BIGNUM *base64_to_bignum(const string& b64) { | 83 | BIGNUM *base64_to_bignum(const string& b64) { |
83 | vector<unsigned char> bin; | 84 | vector<unsigned char> bin; |
84 | decode_base64(b64,bin); | 85 | decode_base64(b64,bin); |
85 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); | 86 | BIGNUM *rv = BN_bin2bn(&(bin.front()),bin.size(),0); |
86 | if(!rv) | 87 | if(!rv) |
87 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); | 88 | throw failed_conversion(OPKELE_CP_ "failed to BN_bin2bn()"); |
88 | return rv; | 89 | return rv; |
89 | } | 90 | } |
90 | 91 | ||
91 | BIGNUM *dec_to_bignum(const string& dec) { | 92 | BIGNUM *dec_to_bignum(const string& dec) { |
92 | BIGNUM *rv = 0; | 93 | BIGNUM *rv = 0; |
93 | if(!BN_dec2bn(&rv,dec.c_str())) | 94 | if(!BN_dec2bn(&rv,dec.c_str())) |
94 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 95 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
95 | return rv; | 96 | return rv; |
96 | } | 97 | } |
97 | 98 | ||
98 | string bignum_to_base64(const BIGNUM *bn) { | 99 | string bignum_to_base64(const BIGNUM *bn) { |
99 | vector<unsigned char> bin(BN_num_bytes(bn)+1); | 100 | vector<unsigned char> bin(BN_num_bytes(bn)+1); |
100 | unsigned char *binptr = &(bin.front())+1; | 101 | unsigned char *binptr = &(bin.front())+1; |
101 | int l = BN_bn2bin(bn,binptr); | 102 | int l = BN_bn2bin(bn,binptr); |
102 | if(l && (*binptr)&0x80){ | 103 | if(l && (*binptr)&0x80){ |
103 | (*(--binptr)) = 0; ++l; | 104 | (*(--binptr)) = 0; ++l; |
104 | } | 105 | } |
105 | return encode_base64(binptr,l); | 106 | return encode_base64(binptr,l); |
106 | } | 107 | } |
107 | 108 | ||
108 | /* | 109 | /* |
109 | * w3c times | 110 | * w3c times |
110 | */ | 111 | */ |
111 | 112 | ||
112 | string time_to_w3c(time_t t) { | 113 | string time_to_w3c(time_t t) { |
113 | struct tm tm_t; | 114 | struct tm tm_t; |
114 | if(!gmtime_r(&t,&tm_t)) | 115 | if(!gmtime_r(&t,&tm_t)) |
115 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); | 116 | throw failed_conversion(OPKELE_CP_ "failed to BN_dec2bn()"); |
116 | char rv[25]; | 117 | char rv[25]; |
117 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) | 118 | if(!strftime(rv,sizeof(rv)-1,"%Y-%m-%dT%H:%M:%SZ",&tm_t)) |
118 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); | 119 | throw failed_conversion(OPKELE_CP_ "failed to strftime()"); |
119 | return rv; | 120 | return rv; |
120 | } | 121 | } |
121 | 122 | ||
122 | time_t w3c_to_time(const string& w) { | 123 | time_t w3c_to_time(const string& w) { |
123 | int fraction; | 124 | int fraction; |
124 | struct tm tm_t; | 125 | struct tm tm_t; |
125 | memset(&tm_t,0,sizeof(tm_t)); | 126 | memset(&tm_t,0,sizeof(tm_t)); |
126 | if( ( | 127 | if( ( |
127 | sscanf( | 128 | sscanf( |
128 | w.c_str(), | 129 | w.c_str(), |
129 | "%04d-%02d-%02dT%02d:%02d:%02dZ", | 130 | "%04d-%02d-%02dT%02d:%02d:%02dZ", |
130 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 131 | &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 | 132 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec |
132 | ) != 6 | 133 | ) != 6 |
133 | ) && ( | 134 | ) && ( |
134 | sscanf( | 135 | sscanf( |
135 | w.c_str(), | 136 | w.c_str(), |
136 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", | 137 | "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", |
137 | &tm_t.tm_year,&tm_t.tm_mon,&tm_t.tm_mday, | 138 | &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, | 139 | &tm_t.tm_hour,&tm_t.tm_min,&tm_t.tm_sec, |
139 | &fraction | 140 | &fraction |
140 | ) != 7 | 141 | ) != 7 |
141 | ) ) | 142 | ) ) |
142 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); | 143 | throw failed_conversion(OPKELE_CP_ "failed to sscanf()"); |
143 | tm_t.tm_mon--; | 144 | tm_t.tm_mon--; |
144 | tm_t.tm_year-=1900; | 145 | tm_t.tm_year-=1900; |
145 | time_t rv = mktime(&tm_t); | 146 | time_t rv = mktime(&tm_t); |
146 | if(rv==(time_t)-1) | 147 | if(rv==(time_t)-1) |
147 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); | 148 | throw failed_conversion(OPKELE_CP_ "failed to mktime()"); |
148 | return rv-timezone; | 149 | return rv-timezone; |
149 | } | 150 | } |
150 | 151 | ||
151 | /* | 152 | /* |
152 | * | 153 | * |
153 | */ | 154 | */ |
154 | 155 | ||
155 | string url_encode(const string& str) { | 156 | string url_encode(const string& str) { |
156 | char * t = curl_escape(str.c_str(),str.length()); | 157 | char * t = curl_escape(str.c_str(),str.length()); |
157 | if(!t) | 158 | if(!t) |
158 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); | 159 | throw failed_conversion(OPKELE_CP_ "failed to curl_escape()"); |
159 | string rv(t); | 160 | string rv(t); |
160 | curl_free(t); | 161 | curl_free(t); |
161 | return rv; | 162 | return rv; |
162 | } | 163 | } |
163 | 164 | ||
164 | string attr_escape(const string& str) { | 165 | string attr_escape(const string& str) { |
165 | static const char *unsafechars = "<>&\n\"'"; | 166 | static const char *unsafechars = "<>&\n\"'"; |
166 | string rv; | 167 | string rv; |
167 | string::size_type p=0; | 168 | string::size_type p=0; |
168 | while(true) { | 169 | while(true) { |
169 | string::size_type us = str.find_first_of(unsafechars,p); | 170 | string::size_type us = str.find_first_of(unsafechars,p); |
170 | if(us==string::npos) { | 171 | if(us==string::npos) { |
171 | if(p!=str.length()) | 172 | if(p!=str.length()) |
172 | rv.append(str,p,str.length()-p); | 173 | rv.append(str,p,str.length()-p); |
173 | return rv; | 174 | return rv; |
174 | } | 175 | } |
175 | rv.append(str,p,us-p); | 176 | rv.append(str,p,us-p); |
176 | rv += "&#"; | 177 | rv += "&#"; |
177 | rv += long_to_string((long)str[us]); | 178 | rv += long_to_string((long)str[us]); |
178 | rv += ';'; | 179 | rv += ';'; |
179 | p = us+1; | 180 | p = us+1; |
180 | } | 181 | } |
181 | } | 182 | } |
182 | 183 | ||
183 | string long_to_string(long l) { | 184 | string long_to_string(long l) { |
184 | char rv[32]; | 185 | char rv[32]; |
185 | int r=snprintf(rv,sizeof(rv),"%ld",l); | 186 | int r=snprintf(rv,sizeof(rv),"%ld",l); |
186 | if(r<0 || r>=(int)sizeof(rv)) | 187 | if(r<0 || r>=(int)sizeof(rv)) |
187 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); | 188 | throw failed_conversion(OPKELE_CP_ "failed to snprintf()"); |
188 | return rv; | 189 | return rv; |
189 | } | 190 | } |
190 | 191 | ||
191 | long string_to_long(const string& s) { | 192 | long string_to_long(const string& s) { |
192 | char *endptr = 0; | 193 | char *endptr = 0; |
193 | long rv = strtol(s.c_str(),&endptr,10); | 194 | long rv = strtol(s.c_str(),&endptr,10); |
194 | if((!endptr) || endptr==s.c_str()) | 195 | if((!endptr) || endptr==s.c_str()) |
195 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); | 196 | throw failed_conversion(OPKELE_CP_ "failed to strtol()"); |
196 | return rv; | 197 | return rv; |
197 | } | 198 | } |
198 | 199 | ||
199 | /* | 200 | /* |
200 | * Normalize URL according to the rules, described in rfc 3986, section 6 | 201 | * Normalize URL according to the rules, described in rfc 3986, section 6 |
201 | * | 202 | * |
202 | * - uppercase hex triplets (e.g. %ab -> %AB) | 203 | * - uppercase hex triplets (e.g. %ab -> %AB) |
203 | * - lowercase scheme and host | 204 | * - lowercase scheme and host |
204 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, | 205 | * - decode %-encoded characters, specified as unreserved in rfc 3986, section 2.3, |
205 | * that is - [:alpha:][:digit:]._~- | 206 | * that is - [:alpha:][:digit:]._~- |
206 | * - remove dot segments | 207 | * - remove dot segments |
207 | * - remove empty and default ports | 208 | * - remove empty and default ports |
208 | * - if there's no path component, add '/' | 209 | * - if there's no path component, add '/' |
209 | */ | 210 | */ |
210 | string rfc_3986_normalize_uri(const string& uri) { | 211 | string rfc_3986_normalize_uri(const string& uri) { |
211 | static const char *whitespace = " \t\r\n"; | 212 | static const char *whitespace = " \t\r\n"; |
212 | string rv; | 213 | string rv; |
213 | string::size_type ns = uri.find_first_not_of(whitespace); | 214 | string::size_type ns = uri.find_first_not_of(whitespace); |
214 | if(ns==string::npos) | 215 | if(ns==string::npos) |
215 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); | 216 | throw bad_input(OPKELE_CP_ "Can't normalize empty URI"); |
216 | string::size_type colon = uri.find(':',ns); | 217 | string::size_type colon = uri.find(':',ns); |
217 | if(colon==string::npos) | 218 | if(colon==string::npos) |
218 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); | 219 | throw bad_input(OPKELE_CP_ "No scheme specified in URI"); |
219 | transform( | 220 | transform( |
220 | uri.begin()+ns, uri.begin()+colon+1, | 221 | uri.begin()+ns, uri.begin()+colon+1, |
221 | back_inserter(rv), ::tolower ); | 222 | back_inserter(rv), ::tolower ); |
222 | bool s; | 223 | bool s; |
223 | string::size_type ul = uri.find_last_not_of(whitespace)+1; | 224 | string::size_type ul = uri.find_last_not_of(whitespace)+1; |
224 | if(ul <= (colon+3)) | 225 | if(ul <= (colon+3)) |
225 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); | 226 | throw bad_input(OPKELE_CP_ "Unexpected end of URI being normalized encountered"); |
226 | if(uri[colon+1]!='/' || uri[colon+2]!='/') | 227 | if(uri[colon+1]!='/' || uri[colon+2]!='/') |
227 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); | 228 | throw bad_input(OPKELE_CP_ "Unexpected input in URI being normalized after scheme component"); |
228 | if(rv=="http:") | 229 | if(rv=="http:") |
229 | s = false; | 230 | s = false; |
230 | else if(rv=="https:") | 231 | else if(rv=="https:") |
231 | s = true; | 232 | s = true; |
232 | else{ | 233 | else{ |
233 | /* TODO: support more schemes. e.g. xri. How do we normalize | 234 | /* TODO: support more schemes. e.g. xri. How do we normalize |
234 | * xri? | 235 | * xri? |
235 | */ | 236 | */ |
236 | rv.append(uri,colon+1,ul-colon-1); | 237 | rv.append(uri,colon+1,ul-colon-1); |
237 | return rv; | 238 | return rv; |
238 | } | 239 | } |
239 | rv += "//"; | 240 | rv += "//"; |
240 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); | 241 | string::size_type interesting = uri.find_first_of(":/#?",colon+3); |
241 | if(interesting==string::npos) { | 242 | if(interesting==string::npos) { |
242 | transform( | 243 | transform( |
243 | uri.begin()+colon+3,uri.begin()+ul, | 244 | uri.begin()+colon+3,uri.begin()+ul, |
244 | back_inserter(rv), ::tolower ); | 245 | back_inserter(rv), ::tolower ); |
245 | rv += '/'; return rv; | 246 | rv += '/'; return rv; |
246 | } | 247 | } |
247 | transform( | 248 | transform( |
248 | uri.begin()+colon+3,uri.begin()+interesting, | 249 | uri.begin()+colon+3,uri.begin()+interesting, |
249 | back_inserter(rv), ::tolower ); | 250 | back_inserter(rv), ::tolower ); |
250 | bool qf = false; | 251 | bool qf = false; |
251 | char ic = uri[interesting]; | 252 | char ic = uri[interesting]; |
252 | if(ic==':') { | 253 | if(ic==':') { |
253 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); | 254 | string::size_type ni = uri.find_first_of("/#?%",interesting+1); |
254 | const char *nptr = uri.data()+interesting+1; | 255 | const char *nptr = uri.data()+interesting+1; |
255 | char *eptr = 0; | 256 | char *eptr = 0; |
256 | long port = strtol(nptr,&eptr,10); | 257 | long port = strtol(nptr,&eptr,10); |
257 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { | 258 | if( (port>0) && (port<65535) && port!=(s?443:80) ) { |
258 | char tmp[8]; | 259 | char tmp[8]; |
259 | snprintf(tmp,sizeof(tmp),":%ld",port); | 260 | snprintf(tmp,sizeof(tmp),":%ld",port); |
260 | rv += tmp; | 261 | rv += tmp; |
261 | } | 262 | } |
262 | if(ni==string::npos) { | 263 | if(ni==string::npos) { |
263 | rv += '/'; return rv; | 264 | rv += '/'; return rv; |
264 | } | 265 | } |
265 | interesting = ni; | 266 | interesting = ni; |
266 | }else if(ic!='/') { | 267 | }else if(ic!='/') { |
267 | rv += '/'; rv += ic; | 268 | rv += '/'; rv += ic; |
268 | qf = true; | 269 | qf = true; |
269 | ++interesting; | 270 | ++interesting; |
270 | } | 271 | } |
271 | string::size_type n = interesting; | 272 | string::size_type n = interesting; |
272 | char tmp[3] = { 0,0,0 }; | 273 | char tmp[3] = { 0,0,0 }; |
273 | stack<string::size_type> psegs; psegs.push(rv.length()); | 274 | stack<string::size_type> psegs; psegs.push(rv.length()); |
274 | string pseg; | 275 | string pseg; |
275 | for(;n<ul;) { | 276 | for(;n<ul;) { |
276 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); | 277 | string::size_type unsafe = uri.find_first_of(qf?"%":"%/?#",n); |
277 | if(unsafe==string::npos) { | 278 | if(unsafe==string::npos) { |
278 | pseg.append(uri,n,ul-n-1); n = ul-1; | 279 | pseg.append(uri,n,ul-n-1); n = ul-1; |
279 | }else{ | 280 | }else{ |
280 | pseg.append(uri,n,unsafe-n); | 281 | pseg.append(uri,n,unsafe-n); |
281 | n = unsafe; | 282 | n = unsafe; |
282 | } | 283 | } |
283 | char c = uri[n++]; | 284 | char c = uri[n++]; |
284 | if(c=='%') { | 285 | if(c=='%') { |
285 | if((n+1)>=ul) | 286 | if((n+1)>=ul) |
286 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); | 287 | throw bad_input(OPKELE_CP_ "Unexpected end of URI encountered while parsing percent-encoded character"); |
287 | tmp[0] = uri[n++]; | 288 | tmp[0] = uri[n++]; |
288 | tmp[1] = uri[n++]; | 289 | tmp[1] = uri[n++]; |
289 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) | 290 | if(!( isxdigit(tmp[0]) && isxdigit(tmp[1]) )) |
290 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); | 291 | throw bad_input(OPKELE_CP_ "Invalid percent-encoded character in URI being normalized"); |
291 | int cc = strtol(tmp,0,16); | 292 | int cc = strtol(tmp,0,16); |
292 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) | 293 | if( isalpha(cc) || isdigit(cc) || strchr("._~-",cc) ) |
293 | pseg += cc; | 294 | pseg += cc; |
294 | else{ | 295 | else{ |
295 | pseg += '%'; | 296 | pseg += '%'; |
296 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); | 297 | pseg += toupper(tmp[0]); pseg += toupper(tmp[1]); |
297 | } | 298 | } |
298 | }else if(qf) { | 299 | }else if(qf) { |
299 | rv += pseg; rv += c; | 300 | rv += pseg; rv += c; |
300 | pseg.clear(); | 301 | pseg.clear(); |
301 | }else if(n>=ul || strchr("?/#",c)) { | 302 | }else if(n>=ul || strchr("?/#",c)) { |
302 | if(pseg.empty() || pseg==".") { | 303 | if(pseg.empty() || pseg==".") { |
303 | }else if(pseg=="..") { | 304 | }else if(pseg=="..") { |
304 | if(psegs.size()>1) { | 305 | if(psegs.size()>1) { |
305 | rv.resize(psegs.top()); psegs.pop(); | 306 | rv.resize(psegs.top()); psegs.pop(); |
306 | } | 307 | } |
307 | }else{ | 308 | }else{ |
308 | psegs.push(rv.length()); | 309 | psegs.push(rv.length()); |
309 | if(c!='/') { | 310 | if(c!='/') { |
310 | pseg += c; | 311 | pseg += c; |
311 | qf = true; | 312 | qf = true; |
312 | } | 313 | } |
313 | rv += '/'; rv += pseg; | 314 | rv += '/'; rv += pseg; |
314 | } | 315 | } |
315 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { | 316 | if(c=='/' && (n>=ul || strchr("?#",uri[n])) ) { |
316 | rv += '/'; | 317 | rv += '/'; |
317 | if(n<ul) | 318 | if(n<ul) |
318 | qf = true; | 319 | qf = true; |
319 | }else if(strchr("?#",c)) { | 320 | }else if(strchr("?#",c)) { |
320 | if(psegs.size()==1 && psegs.top()==rv.length()) | 321 | if(psegs.size()==1 && psegs.top()==rv.length()) |
321 | rv += '/'; | 322 | rv += '/'; |
322 | if(pseg.empty()) | 323 | if(pseg.empty()) |
323 | rv += c; | 324 | rv += c; |
324 | qf = true; | 325 | qf = true; |
325 | } | 326 | } |
326 | pseg.clear(); | 327 | pseg.clear(); |
327 | }else{ | 328 | }else{ |
328 | pseg += c; | 329 | pseg += c; |
329 | } | 330 | } |
330 | } | 331 | } |
331 | if(!pseg.empty()) { | 332 | if(!pseg.empty()) { |
332 | if(!qf) rv += '/'; | 333 | if(!qf) rv += '/'; |
333 | rv += pseg; | 334 | rv += pseg; |
334 | } | 335 | } |
335 | return rv; | 336 | return rv; |
336 | } | 337 | } |
337 | 338 | ||
338 | string& strip_uri_fragment_part(string& u) { | 339 | string& strip_uri_fragment_part(string& u) { |
339 | string::size_type q = u.find('?'), f = u.find('#'); | 340 | string::size_type q = u.find('?'), f = u.find('#'); |
340 | if(q==string::npos) { | 341 | if(q==string::npos) { |
341 | if(f!=string::npos) | 342 | if(f!=string::npos) |
342 | u.erase(f); | 343 | u.erase(f); |
343 | }else{ | 344 | }else{ |
344 | if(f!=string::npos) { | 345 | if(f!=string::npos) { |
345 | if(f<q) | 346 | if(f<q) |
346 | u.erase(f,q-f); | 347 | u.erase(f,q-f); |
347 | else | 348 | else |
348 | u.erase(f); | 349 | u.erase(f); |
349 | } | 350 | } |
350 | } | 351 | } |
351 | return u; | 352 | return u; |
352 | } | 353 | } |
353 | 354 | ||
355 | bool uri_matches_realm(const string& uri,const string& realm) { | ||
356 | string nrealm = opkele::util::rfc_3986_normalize_uri(realm); | ||
357 | string nu = opkele::util::rfc_3986_normalize_uri(uri); | ||
358 | string::size_type pr = nrealm.find("://"); | ||
359 | string::size_type pu = nu.find("://"); | ||
360 | assert(!(pr==string::npos || pu==string::npos)); | ||
361 | pr += sizeof("://")-1; | ||
362 | pu += sizeof("://")-1; | ||
363 | if(!strncmp(nrealm.c_str()+pr,"*.",2)) { | ||
364 | pr = nrealm.find('.',pr); | ||
365 | pu = nu.find('.',pu); | ||
366 | assert(pr!=string::npos); | ||
367 | if(pu==string::npos) | ||
368 | return false; | ||
369 | // TODO: check for overgeneralized realm | ||
370 | } | ||
371 | string::size_type lr = nrealm.length(); | ||
372 | string::size_type lu = nu.length(); | ||
373 | if( (lu-pu) < (lr-pr) ) | ||
374 | return false; | ||
375 | pair<const char*,const char*> mp = mismatch( | ||
376 | nrealm.c_str()+pr,nrealm.c_str()+lr, | ||
377 | nu.c_str()+pu); | ||
378 | if( (*(mp.first-1))!='/' | ||
379 | && !strchr("/?#",*mp.second) ) | ||
380 | return false; | ||
381 | return true; | ||
382 | } | ||
383 | |||
354 | string abi_demangle(const char *mn) { | 384 | string abi_demangle(const char *mn) { |
355 | #ifndef HAVE_DEMANGLE | 385 | #ifndef HAVE_DEMANGLE |
356 | return mn; | 386 | return mn; |
357 | #else /* !HAVE_DEMANGLE */ | 387 | #else /* !HAVE_DEMANGLE */ |
358 | int dstat; | 388 | int dstat; |
359 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); | 389 | char *demangled = abi::__cxa_demangle(mn,0,0,&dstat); |
360 | if(dstat) | 390 | if(dstat) |
361 | return mn; | 391 | return mn; |
362 | string rv = demangled; | 392 | string rv = demangled; |
363 | free(demangled); | 393 | free(demangled); |
364 | return rv; | 394 | return rv; |
365 | #endif /* !HAVE_DEMANGLE */ | 395 | #endif /* !HAVE_DEMANGLE */ |
366 | } | 396 | } |
367 | 397 | ||
368 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { | 398 | string base64_signature(const assoc_t& assoc,const basic_openid_message& om) { |
369 | const string& slist = om.get_field("signed"); | 399 | const string& slist = om.get_field("signed"); |
370 | string kv; | 400 | string kv; |
371 | string::size_type p=0; | 401 | string::size_type p=0; |
372 | while(true) { | 402 | while(true) { |
373 | string::size_type co = slist.find(',',p); | 403 | string::size_type co = slist.find(',',p); |
374 | string f = (co==string::npos) | 404 | string f = (co==string::npos) |
375 | ?slist.substr(p):slist.substr(p,co-p); | 405 | ?slist.substr(p):slist.substr(p,co-p); |
376 | kv += f; | 406 | kv += f; |
377 | kv += ':'; | 407 | kv += ':'; |
378 | kv += om.get_field(f); | 408 | kv += om.get_field(f); |
379 | kv += '\n'; | 409 | kv += '\n'; |
380 | if(co==string::npos) break; | 410 | if(co==string::npos) break; |
381 | p = co+1; | 411 | p = co+1; |
382 | } | 412 | } |
383 | const secret_t& secret = assoc->secret(); | 413 | const secret_t& secret = assoc->secret(); |
384 | const EVP_MD *evpmd; | 414 | const EVP_MD *evpmd; |
385 | const string& at = assoc->assoc_type(); | 415 | const string& at = assoc->assoc_type(); |
386 | if(at=="HMAC-SHA256") | 416 | if(at=="HMAC-SHA256") |
387 | evpmd = EVP_sha256(); | 417 | evpmd = EVP_sha256(); |
388 | else if(at=="HMAC-SHA1") | 418 | else if(at=="HMAC-SHA1") |
389 | evpmd = EVP_sha1(); | 419 | evpmd = EVP_sha1(); |
390 | else | 420 | else |
391 | throw unsupported(OPKELE_CP_ "unknown association type"); | 421 | throw unsupported(OPKELE_CP_ "unknown association type"); |
392 | unsigned int md_len = 0; | 422 | unsigned int md_len = 0; |
393 | unsigned char *md = HMAC(evpmd, | 423 | unsigned char *md = HMAC(evpmd, |
394 | &(secret.front()),secret.size(), | 424 | &(secret.front()),secret.size(), |
395 | (const unsigned char*)kv.data(),kv.length(), | 425 | (const unsigned char*)kv.data(),kv.length(), |
396 | 0,&md_len); | 426 | 0,&md_len); |
397 | return encode_base64(md,md_len); | 427 | return encode_base64(md,md_len); |
398 | } | 428 | } |
399 | 429 | ||
400 | } | 430 | } |
401 | 431 | ||
402 | } | 432 | } |