-rw-r--r-- | include/opkele/util.h | 8 |
1 files changed, 8 insertions, 0 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 */ |