summaryrefslogtreecommitdiffabout
path: root/include/opkele/util.h
Unidiff
Diffstat (limited to 'include/opkele/util.h') (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/util.h92
1 files changed, 11 insertions, 81 deletions
diff --git a/include/opkele/util.h b/include/opkele/util.h
index bc1a0ea..60955e1 100644
--- a/include/opkele/util.h
+++ b/include/opkele/util.h
@@ -1,184 +1,114 @@
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>
8#include <openssl/dh.h>
9#include <opkele/types.h> 7#include <opkele/types.h>
10 8
11namespace opkele { 9namespace opkele {
12 using std::string; 10 using std::string;
13 using std::vector; 11 using std::vector;
14 12
15 /** 13 /**
16 * @brief opkele utils namespace 14 * @brief opkele utils namespace
17 */ 15 */
18 namespace util { 16 namespace util {
19 17
20 /** 18 /**
21 * Convenience class encapsulating SSL BIGNUM object for the purpose of
22 * automatical freeing.
23 */
24 class bignum_t {
25 public:
26 BIGNUM *_bn;
27
28 bignum_t() : _bn(0) { }
29 bignum_t(BIGNUM *bn) : _bn(bn) { }
30 ~bignum_t() throw() { if(_bn) BN_free(_bn); }
31
32 bignum_t& operator=(BIGNUM *bn) { if(_bn) BN_free(_bn); _bn = bn; return *this; }
33
34 operator const BIGNUM*(void) const { return _bn; }
35 operator BIGNUM*(void) { return _bn; }
36 };
37 /**
38 * Convenience clas encapsulating SSL DH object for the purpose of
39 * automatic freeing.
40 */
41 class dh_t {
42 public:
43 DH *_dh;
44
45 dh_t() : _dh(0) { }
46 dh_t(DH *dh) : _dh(dh) { }
47 ~dh_t() throw() { if(_dh) DH_free(_dh); }
48
49 dh_t& operator=(DH *dh) { if(_dh) DH_free(_dh); _dh = dh; return *this; }
50
51 operator const DH*(void) const { return _dh; }
52 operator DH*(void) { return _dh; }
53
54 DH* operator->() { return _dh; }
55 const DH* operator->() const { return _dh; }
56 };
57
58 /**
59 * Convert base64-encoded SSL BIGNUM to internal representation.
60 * @param b64 base64-encoded number
61 * @return SSL BIGNUM
62 * @throw failed_conversion in case of error
63 */
64 BIGNUM *base64_to_bignum(const string& b64);
65 /**
66 * Convert decimal representation to SSL BIGNUM.
67 * @param dec decimal representation
68 * @return resulting BIGNUM
69 * @throw failed_conversion in case of error
70 */
71 BIGNUM *dec_to_bignum(const string& dec);
72 /**
73 * Convert SSL BIGNUM data to base64 encoded string.
74 * @param bn BIGNUM
75 * @return base64encoded string
76 */
77 string bignum_to_base64(const BIGNUM *bn);
78
79 /**
80 * Convert internal time representation to w3c format 19 * Convert internal time representation to w3c format
81 * @param t internal representation 20 * @param t internal representation
82 * @return w3c time 21 * @return w3c time
83 * @throw failed_conversion in case of error 22 * @throw failed_conversion in case of error
84 */ 23 */
85 string time_to_w3c(time_t t); 24 string time_to_w3c(time_t t);
86 /** 25 /**
87 * Convert W3C time representation to internal time_t 26 * Convert W3C time representation to internal time_t
88 * @param w w3c representation 27 * @param w w3c representation
89 * @return converted time 28 * @return converted time
90 * @throw failed_conversion in case of error 29 * @throw failed_conversion in case of error
91 */ 30 */
92 time_t w3c_to_time(const string& w); 31 time_t w3c_to_time(const string& w);
93 32
94 /** 33 /**
95 * Encode string to the representation suitable for using in URL. 34 * Encode string to the representation suitable for using in URL.
96 * @param str string to encode 35 * @param str string to encode
97 * @return encoded string 36 * @return encoded string
98 * @throw failed_conversion in case of failure 37 * @throw failed_conversion in case of failure
99 */ 38 */
100 string url_encode(const string& str); 39 string url_encode(const string& str);
101 40
102 /** 41 /**
103 * Make string suitable for using as x(ht)ml attribute. 42 * Make string suitable for using as x(ht)ml attribute.
104 * @param str string to escape 43 * @param str string to escape
105 * @return escaped string 44 * @return escaped string
106 */ 45 */
107 string attr_escape(const string& str); 46 string attr_escape(const string& str);
108 47
109 /** 48 /**
110 * Convert number to string 49 * Convert number to string
111 * @param l number 50 * @param l number
112 * @return string representation 51 * @return string representation
113 * @throw failed_conversion in case of failure 52 * @throw failed_conversion in case of failure
114 */ 53 */
115 string long_to_string(long l); 54 string long_to_string(long l);
116 /** 55 /**
117 * Convert string to number 56 * Convert string to number
118 * @param s string, containing the number 57 * @param s string, containing the number
119 * @return the number 58 * @return the number
120 * @throw failed_conversion in case of failure 59 * @throw failed_conversion in case of failure
121 */ 60 */
122 long string_to_long(const string& s); 61 long string_to_long(const string& s);
123 62
124 /** 63 /**
125 * Encode binary data using base64. 64 * Encode binary data using base64.
126 * @param data pointer to binary data 65 * @param data pointer to binary data
127 * @param length length of data 66 * @param length length of data
128 * @return encoded data 67 * @return encoded data
129 */ 68 */
130 string encode_base64(const void *data,size_t length); 69 string encode_base64(const void *data,size_t length);
131 /** 70 /**
132 * Decode binary data from base64 representation. 71 * Decode binary data from base64 representation.
133 * @param data base64-encoded data 72 * @param data base64-encoded data
134 * @param rv container for decoded binary 73 * @param rv container for decoded binary
135 */ 74 */
136 void decode_base64(const string& data,vector<unsigned char>& rv); 75 void decode_base64(const string& data,vector<unsigned char>& rv);
137 76
138 /** 77 /**
139 * Normalize http(s) URI according to RFC3986, section 6. URI is 78 * Normalize http(s) URI according to RFC3986, section 6. URI is
140 * expected to have scheme: in front of it. 79 * expected to have scheme: in front of it.
141 * @param uri URI 80 * @param uri URI
142 * @return normalized URI 81 * @return normalized URI
143 * @throw not_implemented in case of non-httpi(s) URI 82 * @throw not_implemented in case of non-httpi(s) URI
144 * @throw bad_input in case of malformed URI 83 * @throw bad_input in case of malformed URI
145 */ 84 */
146 string rfc_3986_normalize_uri(const string& uri); 85 string rfc_3986_normalize_uri(const string& uri);
147 86
148 /** 87 /**
149 * Match URI against realm 88 * Match URI against realm
150 * @param uri URI to match 89 * @param uri URI to match
151 * @param realm realm to match against 90 * @param realm realm to match against
152 * @return true if URI matches realm 91 * @return true if URI matches realm
153 */ 92 */
154 bool uri_matches_realm(const string& uri,const string& realm); 93 bool uri_matches_realm(const string& uri,const string& realm);
155 94
95 /**
96 * Strip fragment part from URI
97 * @param uri input/output parameter containing the URI
98 * @return reference to uri
99 */
156 string& strip_uri_fragment_part(string& uri); 100 string& strip_uri_fragment_part(string& uri);
157 101
158 string abi_demangle(const char* mn); 102 /**
159 103 * Calculate signature and encode it using base64
104 * @param assoc association being used for signing
105 * @param om openid message
106 * @return base64 representation of the signature
107 */
160 string base64_signature(const assoc_t& assoc,const basic_openid_message& om); 108 string base64_signature(const assoc_t& assoc,const basic_openid_message& om);
161 109
162 class change_mode_message_proxy : public basic_openid_message {
163 public:
164 const basic_openid_message& x;
165 const string& mode;
166
167 change_mode_message_proxy(const basic_openid_message& xx,const string& m) : x(xx), mode(m) { }
168
169 bool has_field(const string& n) const { return x.has_field(n); }
170 const string& get_field(const string& n) const {
171 return (n=="mode")?mode:x.get_field(n); }
172 bool has_ns(const string& uri) const {return x.has_ns(uri); }
173 string get_ns(const string& uri) const { return x.get_ns(uri); }
174 fields_iterator fields_begin() const {
175 return x.fields_begin(); }
176 fields_iterator fields_end() const {
177 return x.fields_end(); }
178 };
179
180 } 110 }
181 111
182} 112}
183 113
184#endif /* __OPKELE_UTIL_H */ 114#endif /* __OPKELE_UTIL_H */