summaryrefslogtreecommitdiffabout
authorMichael Krelin <hacker@klever.net>2007-12-04 19:02:15 (UTC)
committer Michael Krelin <hacker@klever.net>2007-12-04 19:02:15 (UTC)
commit7a6a6fbcf7e20f0d7da5f625a73c865b361f16aa (patch) (unidiff)
tree42a2983bd4fdbba0b49d9a5329e5f819ad73d5ec
parent206de587733c5dbde280fb6e9f1eab2f02de7f54 (diff)
downloadlibopkele-7a6a6fbcf7e20f0d7da5f625a73c865b361f16aa.zip
libopkele-7a6a6fbcf7e20f0d7da5f625a73c865b361f16aa.tar.gz
libopkele-7a6a6fbcf7e20f0d7da5f625a73c865b361f16aa.tar.bz2
Minor improvements
- almost irrelevant documentation updates - introduced id_res_expired_on_delivery exception to be thrown if the association has expired right before it could've been used for signature verification. Signed-off-by: Michael Krelin <hacker@klever.net>
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--include/opkele/consumer.h6
-rw-r--r--include/opkele/exception.h9
-rw-r--r--lib/consumer.cc6
3 files changed, 18 insertions, 3 deletions
diff --git a/include/opkele/consumer.h b/include/opkele/consumer.h
index 50ff692..c463787 100644
--- a/include/opkele/consumer.h
+++ b/include/opkele/consumer.h
@@ -1,168 +1,174 @@
1#ifndef __OPKELE_CONSUMER_H 1#ifndef __OPKELE_CONSUMER_H
2#define __OPKELE_CONSUMER_H 2#define __OPKELE_CONSUMER_H
3 3
4#include <opkele/types.h> 4#include <opkele/types.h>
5#include <opkele/extension.h> 5#include <opkele/extension.h>
6 6
7/** 7/**
8 * @file 8 * @file
9 * @brief OpenID consumer-side functionality 9 * @brief OpenID consumer-side functionality
10 */ 10 */
11 11
12namespace opkele { 12namespace opkele {
13 13
14 /** 14 /**
15 * implementation of basic consumer functionality 15 * implementation of basic consumer functionality
16 * 16 *
17 * @note 17 * @note
18 * The consumer uses libcurl internally, which means that if you're using 18 * The consumer uses libcurl internally, which means that if you're using
19 * libopkele in multithreaded environment you should call curl_global_init 19 * libopkele in multithreaded environment you should call curl_global_init
20 * yourself before spawning any threads. 20 * yourself before spawning any threads.
21 */ 21 */
22 class consumer_t { 22 class consumer_t {
23 public: 23 public:
24 24
25 virtual ~consumer_t() { } 25 virtual ~consumer_t() { }
26 26
27 /** 27 /**
28 * store association. The function should be overridden in the real 28 * store association. The function should be overridden in the real
29 * implementation to provide persistent associations store. 29 * implementation to provide persistent associations store.
30 * @param server the OpenID server 30 * @param server the OpenID server
31 * @param handle association handle 31 * @param handle association handle
32 * @param secret the secret associated with the server and handle 32 * @param secret the secret associated with the server and handle
33 * @param expires_in the number of seconds until the handle is expired 33 * @param expires_in the number of seconds until the handle is expired
34 * @return the auto_ptr<> for the newly allocated association_t object 34 * @return the auto_ptr<> for the newly allocated association_t object
35 */ 35 */
36 virtual assoc_t store_assoc(const string& server,const string& handle,const secret_t& secret,int expires_in) = 0; 36 virtual assoc_t store_assoc(const string& server,const string& handle,const secret_t& secret,int expires_in) = 0;
37 /** 37 /**
38 * retrieve stored association. The function should be overridden 38 * retrieve stored association. The function should be overridden
39 * in the real implementation to provide persistent assocations 39 * in the real implementation to provide persistent assocations
40 * store. 40 * store.
41 * 41 *
42 * @note 42 * @note
43 * The user is responsible for handling associations expiry and 43 * The user is responsible for handling associations expiry and
44 * this function should never return an expired or invalidated 44 * this function should never return an expired or invalidated
45 * association. 45 * association.
46 * 46 *
47 * @param server the OpenID server 47 * @param server the OpenID server
48 * @param handle association handle 48 * @param handle association handle
49 * @return the autho_ptr<> for the newly allocated association_t object 49 * @return the autho_ptr<> for the newly allocated association_t object
50 * @throw failed_lookup if no unexpired association found 50 * @throw failed_lookup if no unexpired association found
51 */ 51 */
52 virtual assoc_t retrieve_assoc(const string& server,const string& handle) = 0; 52 virtual assoc_t retrieve_assoc(const string& server,const string& handle) = 0;
53 /** 53 /**
54 * invalidate stored association. The function should be overridden 54 * invalidate stored association. The function should be overridden
55 * in the real implementation of the consumer. 55 * in the real implementation of the consumer.
56 * @param server the OpenID server 56 * @param server the OpenID server
57 * @param handle association handle 57 * @param handle association handle
58 */ 58 */
59 virtual void invalidate_assoc(const string& server,const string& handle) = 0; 59 virtual void invalidate_assoc(const string& server,const string& handle) = 0;
60 /** 60 /**
61 * retrieve any unexpired association for the server. If the 61 * retrieve any unexpired association for the server. If the
62 * function is not overridden in the real implementation, the new 62 * function is not overridden in the real implementation, the new
63 * association will be established for each request. 63 * association will be established for each request.
64 * 64 *
65 * @note 65 * @note
66 * The user is responsible for handling associations and this 66 * The user is responsible for handling associations and this
67 * function should never return an expired or invalidated 67 * function should never return an expired or invalidated
68 * association. 68 * association.
69 * 69 *
70 * @note
71 * It may be a good idea to pre-expire associations shortly before
72 * their time is really up to avoid association expiry in the
73 * middle of negotiations.
74 *
70 * @param server the OpenID server 75 * @param server the OpenID server
71 * @return the auto_ptr<> for the newly allocated association_t object 76 * @return the auto_ptr<> for the newly allocated association_t object
72 * @throw failed_lookup in case of absence of the handle 77 * @throw failed_lookup in case of absence of the handle
73 */ 78 */
74 virtual assoc_t find_assoc(const string& server); 79 virtual assoc_t find_assoc(const string& server);
75 80
76 /** 81 /**
77 * retrieve the metainformation contained in link tags from the 82 * retrieve the metainformation contained in link tags from the
78 * page pointed by url. the function may implement caching of the 83 * page pointed by url. the function may implement caching of the
79 * information. 84 * information.
80 * @param url url to harvest for link tags 85 * @param url url to harvest for link tags
81 * @param server reference to the string object where to put 86 * @param server reference to the string object where to put
82 * openid.server value 87 * openid.server value
83 * @param delegate reference to the string object where to put the 88 * @param delegate reference to the string object where to put the
84 * openid.delegate value (if any) 89 * openid.delegate value (if any)
85 */ 90 */
86 virtual void retrieve_links(const string& url,string& server,string& delegate); 91 virtual void retrieve_links(const string& url,string& server,string& delegate);
87 92
88 /** 93 /**
89 * perform the associate request to OpenID server. 94 * perform the associate request to OpenID server.
90 * @param server the OpenID server 95 * @param server the OpenID server
91 * @return the auto_ptr<> for the newly allocated association_t 96 * @return the auto_ptr<> for the newly allocated association_t
92 * object, representing established association 97 * object, representing established association
93 * @throw exception in case of error 98 * @throw exception in case of error
94 */ 99 */
95 assoc_t associate(const string& server); 100 assoc_t associate(const string& server);
96 /** 101 /**
97 * prepare the parameters for the checkid_immediate 102 * prepare the parameters for the checkid_immediate
98 * request. 103 * request.
99 * @param identity the identity to verify 104 * @param identity the identity to verify
100 * @param return_to the return_to url to pass with the request 105 * @param return_to the return_to url to pass with the request
101 * @param trust_root the trust root to advertise with the request 106 * @param trust_root the trust root to advertise with the request
102 * @param ext pointer to an extension(s) hooks object 107 * @param ext pointer to an extension(s) hooks object
103 * @return the location string 108 * @return the location string
104 * @throw exception in case of error 109 * @throw exception in case of error
105 */ 110 */
106 virtual string checkid_immediate(const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0); 111 virtual string checkid_immediate(const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0);
107 /** 112 /**
108 * prepare the parameters for the checkid_setup 113 * prepare the parameters for the checkid_setup
109 * request. 114 * request.
110 * @param identity the identity to verify 115 * @param identity the identity to verify
111 * @param return_to the return_to url to pass with the request 116 * @param return_to the return_to url to pass with the request
112 * @param trust_root the trust root to advertise with the request 117 * @param trust_root the trust root to advertise with the request
113 * @param ext pointer to an extension(s) hooks object 118 * @param ext pointer to an extension(s) hooks object
114 * @return the location string 119 * @return the location string
115 * @throw exception in case of error 120 * @throw exception in case of error
116 */ 121 */
117 virtual string checkid_setup(const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0); 122 virtual string checkid_setup(const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0);
118 /** 123 /**
119 * the actual implementation behind checkid_immediate() and 124 * the actual implementation behind checkid_immediate() and
120 * checkid_setup() functions. 125 * checkid_setup() functions.
121 * @param mode checkid_* mode - either mode_checkid_immediate or mode_checkid_setup 126 * @param mode checkid_* mode - either mode_checkid_immediate or mode_checkid_setup
122 * @param identity the identity to verify 127 * @param identity the identity to verify
123 * @param return_to the return_to url to pass with the request 128 * @param return_to the return_to url to pass with the request
124 * @param trust_root the trust root to advertise with the request 129 * @param trust_root the trust root to advertise with the request
125 * @param ext pointer to an extension(s) hooks object 130 * @param ext pointer to an extension(s) hooks object
126 * @return the location string 131 * @return the location string
127 * @throw exception in case of error 132 * @throw exception in case of error
128 */ 133 */
129 virtual string checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0); 134 virtual string checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root="",extension_t *ext=0);
130 /** 135 /**
131 * verify the id_res response 136 * verify the id_res response
132 * @param pin the response parameters 137 * @param pin the response parameters
133 * @param identity the identity being checked (if not specified, 138 * @param identity the identity being checked (if not specified,
134 * @param ext pointer to an extension(s) hooks object 139 * @param ext pointer to an extension(s) hooks object
135 * extracted from the openid.identity parameter 140 * extracted from the openid.identity parameter
136 * @throw id_res_mismatch in case of signature mismatch 141 * @throw id_res_mismatch in case of signature mismatch
137 * @throw id_res_setup in case of openid.user_setup_url failure 142 * @throw id_res_setup in case of openid.user_setup_url failure
138 * (supposedly checkid_immediate only) 143 * (supposedly checkid_immediate only)
139 * @throw id_res_failed in case of failure 144 * @throw id_res_failed in case of failure
145 * @throw id_res_expired_on_delivery if the association expired before it could've been verified
140 * @throw exception in case of other failures 146 * @throw exception in case of other failures
141 */ 147 */
142 virtual void id_res(const params_t& pin,const string& identity="",extension_t *ext=0); 148 virtual void id_res(const params_t& pin,const string& identity="",extension_t *ext=0);
143 /** 149 /**
144 * perform a check_authentication request. 150 * perform a check_authentication request.
145 * @param server the OpenID server 151 * @param server the OpenID server
146 * @param p request parameters 152 * @param p request parameters
147 */ 153 */
148 void check_authentication(const string& server,const params_t& p); 154 void check_authentication(const string& server,const params_t& p);
149 155
150 /** 156 /**
151 * normalize URL by adding http:// and trailing slash if needed. 157 * normalize URL by adding http:// and trailing slash if needed.
152 * @param url 158 * @param url
153 * @return normalized url 159 * @return normalized url
154 */ 160 */
155 static string normalize(const string& url); 161 static string normalize(const string& url);
156 162
157 /** 163 /**
158 * Canonicalize URL, by normalizing its appearance and following redirects. 164 * Canonicalize URL, by normalizing its appearance and following redirects.
159 * @param url 165 * @param url
160 * @return canonicalized url 166 * @return canonicalized url
161 */ 167 */
162 virtual string canonicalize(const string& url); 168 virtual string canonicalize(const string& url);
163 169
164 }; 170 };
165 171
166} 172}
167 173
168#endif /* __OPKELE_CONSUMER_H */ 174#endif /* __OPKELE_CONSUMER_H */
diff --git a/include/opkele/exception.h b/include/opkele/exception.h
index 753a818..2ff44b7 100644
--- a/include/opkele/exception.h
+++ b/include/opkele/exception.h
@@ -77,150 +77,159 @@ namespace opkele {
77 exception(const string& w) 77 exception(const string& w)
78 : _what(w) { } 78 : _what(w) { }
79 virtual ~exception() throw(); 79 virtual ~exception() throw();
80 virtual const char * what() const throw(); 80 virtual const char * what() const throw();
81# endif /* OPKELE_HAVE_KONFORKA */ 81# endif /* OPKELE_HAVE_KONFORKA */
82 }; 82 };
83 83
84 /** 84 /**
85 * thrown in case of failed conversion 85 * thrown in case of failed conversion
86 */ 86 */
87 class failed_conversion : public exception { 87 class failed_conversion : public exception {
88 public: 88 public:
89 failed_conversion(OPKELE_E_PARS) 89 failed_conversion(OPKELE_E_PARS)
90 : exception(OPKELE_E_CONS) { } 90 : exception(OPKELE_E_CONS) { }
91 }; 91 };
92 /** 92 /**
93 * thrown in case of failed lookup (either parameter or persistent store) 93 * thrown in case of failed lookup (either parameter or persistent store)
94 */ 94 */
95 class failed_lookup : public exception { 95 class failed_lookup : public exception {
96 public: 96 public:
97 failed_lookup(OPKELE_E_PARS) 97 failed_lookup(OPKELE_E_PARS)
98 : exception(OPKELE_E_CONS) { } 98 : exception(OPKELE_E_CONS) { }
99 }; 99 };
100 /** 100 /**
101 * thrown in case of bad input (either local or network) 101 * thrown in case of bad input (either local or network)
102 */ 102 */
103 class bad_input : public exception { 103 class bad_input : public exception {
104 public: 104 public:
105 bad_input(OPKELE_E_PARS) 105 bad_input(OPKELE_E_PARS)
106 : exception(OPKELE_E_CONS) { } 106 : exception(OPKELE_E_CONS) { }
107 }; 107 };
108 108
109 /** 109 /**
110 * thrown on failed assertion 110 * thrown on failed assertion
111 */ 111 */
112 class failed_assertion : public exception { 112 class failed_assertion : public exception {
113 public: 113 public:
114 failed_assertion(OPKELE_E_PARS) 114 failed_assertion(OPKELE_E_PARS)
115 : exception(OPKELE_E_CONS) { } 115 : exception(OPKELE_E_CONS) { }
116 }; 116 };
117 117
118 /** 118 /**
119 * thrown if the handle being retrieved is invalid 119 * thrown if the handle being retrieved is invalid
120 */ 120 */
121 class invalid_handle : public exception { 121 class invalid_handle : public exception {
122 public: 122 public:
123 invalid_handle(OPKELE_E_PARS) 123 invalid_handle(OPKELE_E_PARS)
124 : exception(OPKELE_E_CONS) { } 124 : exception(OPKELE_E_CONS) { }
125 }; 125 };
126 /** 126 /**
127 * thrown if the handle passed to check_authentication request is not 127 * thrown if the handle passed to check_authentication request is not
128 * stateless 128 * stateless
129 */ 129 */
130 class stateful_handle : public exception { 130 class stateful_handle : public exception {
131 public: 131 public:
132 stateful_handle(OPKELE_E_PARS) 132 stateful_handle(OPKELE_E_PARS)
133 : exception(OPKELE_E_CONS) { } 133 : exception(OPKELE_E_CONS) { }
134 }; 134 };
135 135
136 /** 136 /**
137 * thrown if check_authentication request fails 137 * thrown if check_authentication request fails
138 */ 138 */
139 class failed_check_authentication : public exception { 139 class failed_check_authentication : public exception {
140 public: 140 public:
141 failed_check_authentication(OPKELE_E_PARS) 141 failed_check_authentication(OPKELE_E_PARS)
142 : exception(OPKELE_E_CONS) { } 142 : exception(OPKELE_E_CONS) { }
143 }; 143 };
144 144
145 /** 145 /**
146 * thrown if the id_res request result is negative 146 * thrown if the id_res request result is negative
147 */ 147 */
148 class id_res_failed : public exception { 148 class id_res_failed : public exception {
149 public: 149 public:
150 id_res_failed(OPKELE_E_PARS) 150 id_res_failed(OPKELE_E_PARS)
151 : exception(OPKELE_E_CONS) { } 151 : exception(OPKELE_E_CONS) { }
152 }; 152 };
153 /** 153 /**
154 * thrown if the user_setup_url is provided with negative response 154 * thrown if the user_setup_url is provided with negative response
155 */ 155 */
156 class id_res_setup : public id_res_failed { 156 class id_res_setup : public id_res_failed {
157 public: 157 public:
158 string setup_url; 158 string setup_url;
159 id_res_setup(OPKELE_E_PARS,const string& su) 159 id_res_setup(OPKELE_E_PARS,const string& su)
160 : id_res_failed(OPKELE_E_CONS), setup_url(su) { } 160 : id_res_failed(OPKELE_E_CONS), setup_url(su) { }
161 ~id_res_setup() throw() { } 161 ~id_res_setup() throw() { }
162 }; 162 };
163 /** 163 /**
164 * thrown in case of signature mismatch 164 * thrown in case of signature mismatch
165 */ 165 */
166 class id_res_mismatch : public id_res_failed { 166 class id_res_mismatch : public id_res_failed {
167 public: 167 public:
168 id_res_mismatch(OPKELE_E_PARS) 168 id_res_mismatch(OPKELE_E_PARS)
169 : id_res_failed(OPKELE_E_CONS) { } 169 : id_res_failed(OPKELE_E_CONS) { }
170 }; 170 };
171 171
172 /** 172 /**
173 * thrown if the association has expired before it could've been verified.
174 */
175 class id_res_expired_on_delivery : public id_res_failed {
176 public:
177 id_res_expired_on_delivery(OPKELE_E_PARS)
178 : id_res_failed(OPKELE_E_CONS) { }
179 };
180
181 /**
173 * openssl malfunction occured 182 * openssl malfunction occured
174 */ 183 */
175 class exception_openssl : public exception { 184 class exception_openssl : public exception {
176 public: 185 public:
177 unsigned long _error; 186 unsigned long _error;
178 string _ssl_string; 187 string _ssl_string;
179 exception_openssl(OPKELE_E_PARS); 188 exception_openssl(OPKELE_E_PARS);
180 ~exception_openssl() throw() { } 189 ~exception_openssl() throw() { }
181 }; 190 };
182 191
183 /** 192 /**
184 * network operation related error occured 193 * network operation related error occured
185 */ 194 */
186 class exception_network : public exception { 195 class exception_network : public exception {
187 public: 196 public:
188 exception_network(OPKELE_E_PARS) 197 exception_network(OPKELE_E_PARS)
189 : exception(OPKELE_E_CONS) { } 198 : exception(OPKELE_E_CONS) { }
190 }; 199 };
191 200
192 /** 201 /**
193 * network operation related error occured, specifically, related to 202 * network operation related error occured, specifically, related to
194 * libcurl 203 * libcurl
195 */ 204 */
196 class exception_curl : public exception_network { 205 class exception_curl : public exception_network {
197 public: 206 public:
198 CURLcode _error; 207 CURLcode _error;
199 string _curl_string; 208 string _curl_string;
200 exception_curl(OPKELE_E_PARS); 209 exception_curl(OPKELE_E_PARS);
201 exception_curl(OPKELE_E_PARS,CURLcode e); 210 exception_curl(OPKELE_E_PARS,CURLcode e);
202 ~exception_curl() throw() { } 211 ~exception_curl() throw() { }
203 }; 212 };
204 213
205 /** 214 /**
206 * not implemented (think pure virtual) member function executed, signfies 215 * not implemented (think pure virtual) member function executed, signfies
207 * programmer error 216 * programmer error
208 */ 217 */
209 class not_implemented : public exception { 218 class not_implemented : public exception {
210 public: 219 public:
211 not_implemented(OPKELE_E_PARS) 220 not_implemented(OPKELE_E_PARS)
212 : exception(OPKELE_E_CONS) { } 221 : exception(OPKELE_E_CONS) { }
213 }; 222 };
214 223
215 /** 224 /**
216 * internal error, indicates internal libopkele problem 225 * internal error, indicates internal libopkele problem
217 */ 226 */
218 class internal_error : public exception { 227 class internal_error : public exception {
219 public: 228 public:
220 internal_error(OPKELE_E_PARS) 229 internal_error(OPKELE_E_PARS)
221 : exception(OPKELE_E_CONS) { } 230 : exception(OPKELE_E_CONS) { }
222 }; 231 };
223 232
224} 233}
225 234
226#endif /* __OPKELE_EXCEPTION_H */ 235#endif /* __OPKELE_EXCEPTION_H */
diff --git a/lib/consumer.cc b/lib/consumer.cc
index 66db7dd..9f7530f 100644
--- a/lib/consumer.cc
+++ b/lib/consumer.cc
@@ -91,223 +91,223 @@ namespace opkele {
91 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()"); 91 throw exception_openssl(OPKELE_CP_ "failed to DH_generate_key()");
92 string request = 92 string request =
93 "openid.mode=associate" 93 "openid.mode=associate"
94 "&openid.assoc_type=HMAC-SHA1" 94 "&openid.assoc_type=HMAC-SHA1"
95 "&openid.session_type=DH-SHA1" 95 "&openid.session_type=DH-SHA1"
96 "&openid.dh_consumer_public="; 96 "&openid.dh_consumer_public=";
97 request += util::url_encode(util::bignum_to_base64(dh->pub_key)); 97 request += util::url_encode(util::bignum_to_base64(dh->pub_key));
98 curl_pick_t curl = curl_pick_t::easy_init(); 98 curl_pick_t curl = curl_pick_t::easy_init();
99 if(!curl) 99 if(!curl)
100 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 100 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
101 CURLcode r; 101 CURLcode r;
102 (r=curl.misc_sets()) 102 (r=curl.misc_sets())
103 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 103 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
104 || (r=curl.easy_setopt(CURLOPT_POST,1)) 104 || (r=curl.easy_setopt(CURLOPT_POST,1))
105 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 105 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
106 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 106 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
107 || (r=curl.set_write()) 107 || (r=curl.set_write())
108 ; 108 ;
109 if(r) 109 if(r)
110 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 110 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
111 if( (r=curl.easy_perform()) ) 111 if( (r=curl.easy_perform()) )
112 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 112 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
113 params_t p; p.parse_keyvalues(curl.response); 113 params_t p; p.parse_keyvalues(curl.response);
114 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1") 114 if(p.has_param("assoc_type") && p.get_param("assoc_type")!="HMAC-SHA1")
115 throw bad_input(OPKELE_CP_ "unsupported assoc_type"); 115 throw bad_input(OPKELE_CP_ "unsupported assoc_type");
116 string st; 116 string st;
117 if(p.has_param("session_type")) st = p.get_param("session_type"); 117 if(p.has_param("session_type")) st = p.get_param("session_type");
118 if((!st.empty()) && st!="DH-SHA1") 118 if((!st.empty()) && st!="DH-SHA1")
119 throw bad_input(OPKELE_CP_ "unsupported session_type"); 119 throw bad_input(OPKELE_CP_ "unsupported session_type");
120 secret_t secret; 120 secret_t secret;
121 if(st.empty()) { 121 if(st.empty()) {
122 secret.from_base64(p.get_param("mac_key")); 122 secret.from_base64(p.get_param("mac_key"));
123 }else{ 123 }else{
124 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public")); 124 util::bignum_t s_pub = util::base64_to_bignum(p.get_param("dh_server_public"));
125 vector<unsigned char> ck(DH_size(dh)+1); 125 vector<unsigned char> ck(DH_size(dh)+1);
126 unsigned char *ckptr = &(ck.front())+1; 126 unsigned char *ckptr = &(ck.front())+1;
127 int cklen = DH_compute_key(ckptr,s_pub,dh); 127 int cklen = DH_compute_key(ckptr,s_pub,dh);
128 if(cklen<0) 128 if(cklen<0)
129 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()"); 129 throw exception_openssl(OPKELE_CP_ "failed to DH_compute_key()");
130 if(cklen && (*ckptr)&0x80) { 130 if(cklen && (*ckptr)&0x80) {
131 (*(--ckptr)) = 0; ++cklen; 131 (*(--ckptr)) = 0; ++cklen;
132 } 132 }
133 unsigned char key_sha1[SHA_DIGEST_LENGTH]; 133 unsigned char key_sha1[SHA_DIGEST_LENGTH];
134 SHA1(ckptr,cklen,key_sha1); 134 SHA1(ckptr,cklen,key_sha1);
135 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key")); 135 secret.enxor_from_base64(key_sha1,p.get_param("enc_mac_key"));
136 } 136 }
137 int expires_in = 0; 137 int expires_in = 0;
138 if(p.has_param("expires_in")) { 138 if(p.has_param("expires_in")) {
139 expires_in = util::string_to_long(p.get_param("expires_in")); 139 expires_in = util::string_to_long(p.get_param("expires_in"));
140 }else if(p.has_param("issued") && p.has_param("expiry")) { 140 }else if(p.has_param("issued") && p.has_param("expiry")) {
141 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued")); 141 expires_in = util::w3c_to_time(p.get_param("expiry"))-util::w3c_to_time(p.get_param("issued"));
142 }else 142 }else
143 throw bad_input(OPKELE_CP_ "no expiration information"); 143 throw bad_input(OPKELE_CP_ "no expiration information");
144 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in); 144 return store_assoc(server,p.get_param("assoc_handle"),secret,expires_in);
145 } 145 }
146 146
147 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 147 string consumer_t::checkid_immediate(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
148 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext); 148 return checkid_(mode_checkid_immediate,identity,return_to,trust_root,ext);
149 } 149 }
150 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 150 string consumer_t::checkid_setup(const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
151 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext); 151 return checkid_(mode_checkid_setup,identity,return_to,trust_root,ext);
152 } 152 }
153 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) { 153 string consumer_t::checkid_(mode_t mode,const string& identity,const string& return_to,const string& trust_root,extension_t *ext) {
154 params_t p; 154 params_t p;
155 if(mode==mode_checkid_immediate) 155 if(mode==mode_checkid_immediate)
156 p["mode"]="checkid_immediate"; 156 p["mode"]="checkid_immediate";
157 else if(mode==mode_checkid_setup) 157 else if(mode==mode_checkid_setup)
158 p["mode"]="checkid_setup"; 158 p["mode"]="checkid_setup";
159 else 159 else
160 throw bad_input(OPKELE_CP_ "unknown checkid_* mode"); 160 throw bad_input(OPKELE_CP_ "unknown checkid_* mode");
161 string iurl = canonicalize(identity); 161 string iurl = canonicalize(identity);
162 string server, delegate; 162 string server, delegate;
163 retrieve_links(iurl,server,delegate); 163 retrieve_links(iurl,server,delegate);
164 p["identity"] = delegate.empty()?iurl:delegate; 164 p["identity"] = delegate.empty()?iurl:delegate;
165 if(!trust_root.empty()) 165 if(!trust_root.empty())
166 p["trust_root"] = trust_root; 166 p["trust_root"] = trust_root;
167 p["return_to"] = return_to; 167 p["return_to"] = return_to;
168 try { 168 try {
169 string ah = find_assoc(server)->handle(); 169 string ah = find_assoc(server)->handle();
170 p["assoc_handle"] = ah; 170 p["assoc_handle"] = ah;
171 }catch(failed_lookup& fl) { 171 }catch(failed_lookup& fl) {
172 string ah = associate(server)->handle(); 172 string ah = associate(server)->handle();
173 p["assoc_handle"] = ah; 173 p["assoc_handle"] = ah;
174 } 174 }
175 if(ext) ext->checkid_hook(p,identity); 175 if(ext) ext->checkid_hook(p,identity);
176 return p.append_query(server); 176 return p.append_query(server);
177 } 177 }
178 178
179 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) { 179 void consumer_t::id_res(const params_t& pin,const string& identity,extension_t *ext) {
180 if(pin.has_param("openid.user_setup_url")) 180 if(pin.has_param("openid.user_setup_url"))
181 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url")); 181 throw id_res_setup(OPKELE_CP_ "assertion failed, setup url provided",pin.get_param("openid.user_setup_url"));
182 string server,delegate; 182 string server,delegate;
183 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate); 183 retrieve_links(identity.empty()?pin.get_param("openid.identity"):canonicalize(identity),server,delegate);
184 params_t ps; 184 params_t ps;
185 try { 185 try {
186 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle")); 186 assoc_t assoc = retrieve_assoc(server,pin.get_param("openid.assoc_handle"));
187 if(assoc->is_expired()) /* TODO: or should I throw some other exception to force programmer fix his implementation? */ 187 if(assoc->is_expired())
188 throw failed_lookup(OPKELE_CP_ "retrieve_assoc() has returned expired handle"); 188 throw id_res_expired_on_delivery(OPKELE_CP_ "retrieve_assoc() has returned expired handle");
189 const string& sigenc = pin.get_param("openid.sig"); 189 const string& sigenc = pin.get_param("openid.sig");
190 vector<unsigned char> sig; 190 vector<unsigned char> sig;
191 util::decode_base64(sigenc,sig); 191 util::decode_base64(sigenc,sig);
192 const string& slist = pin.get_param("openid.signed"); 192 const string& slist = pin.get_param("openid.signed");
193 string kv; 193 string kv;
194 string::size_type p = 0; 194 string::size_type p = 0;
195 while(true) { 195 while(true) {
196 string::size_type co = slist.find(',',p); 196 string::size_type co = slist.find(',',p);
197 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p); 197 string f = (co==string::npos)?slist.substr(p):slist.substr(p,co-p);
198 kv += f; 198 kv += f;
199 kv += ':'; 199 kv += ':';
200 f.insert(0,"openid."); 200 f.insert(0,"openid.");
201 kv += pin.get_param(f); 201 kv += pin.get_param(f);
202 kv += '\n'; 202 kv += '\n';
203 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f); 203 if(ext) ps[f.substr(sizeof("openid.")-1)] = pin.get_param(f);
204 if(co==string::npos) 204 if(co==string::npos)
205 break; 205 break;
206 p = co+1; 206 p = co+1;
207 } 207 }
208 secret_t secret = assoc->secret(); 208 secret_t secret = assoc->secret();
209 unsigned int md_len = 0; 209 unsigned int md_len = 0;
210 unsigned char *md = HMAC( 210 unsigned char *md = HMAC(
211 EVP_sha1(), 211 EVP_sha1(),
212 &(secret.front()),secret.size(), 212 &(secret.front()),secret.size(),
213 (const unsigned char *)kv.data(),kv.length(), 213 (const unsigned char *)kv.data(),kv.length(),
214 0,&md_len); 214 0,&md_len);
215 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len)) 215 if(sig.size()!=md_len || memcmp(&(sig.front()),md,md_len))
216 throw id_res_mismatch(OPKELE_CP_ "signature mismatch"); 216 throw id_res_mismatch(OPKELE_CP_ "signature mismatch");
217 }catch(failed_lookup& e) { /* XXX: more specific? */ 217 }catch(failed_lookup& e) {
218 const string& slist = pin.get_param("openid.signed"); 218 const string& slist = pin.get_param("openid.signed");
219 string::size_type pp = 0; 219 string::size_type pp = 0;
220 params_t p; 220 params_t p;
221 while(true) { 221 while(true) {
222 string::size_type co = slist.find(',',pp); 222 string::size_type co = slist.find(',',pp);
223 string f = "openid."; 223 string f = "openid.";
224 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp); 224 f += (co==string::npos)?slist.substr(pp):slist.substr(pp,co-pp);
225 p[f] = pin.get_param(f); 225 p[f] = pin.get_param(f);
226 if(co==string::npos) 226 if(co==string::npos)
227 break; 227 break;
228 pp = co+1; 228 pp = co+1;
229 } 229 }
230 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle"); 230 p["openid.assoc_handle"] = pin.get_param("openid.assoc_handle");
231 p["openid.sig"] = pin.get_param("openid.sig"); 231 p["openid.sig"] = pin.get_param("openid.sig");
232 p["openid.signed"] = pin.get_param("openid.signed"); 232 p["openid.signed"] = pin.get_param("openid.signed");
233 try { 233 try {
234 string ih = pin.get_param("openid.invalidate_handle"); 234 string ih = pin.get_param("openid.invalidate_handle");
235 p["openid.invalidate_handle"] = ih; 235 p["openid.invalidate_handle"] = ih;
236 }catch(failed_lookup& fl) { } 236 }catch(failed_lookup& fl) { }
237 try { 237 try {
238 check_authentication(server,p); 238 check_authentication(server,p);
239 }catch(failed_check_authentication& fca) { 239 }catch(failed_check_authentication& fca) {
240 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()"); 240 throw id_res_failed(OPKELE_CP_ "failed to check_authentication()");
241 } 241 }
242 } 242 }
243 if(ext) ext->id_res_hook(pin,ps,identity); 243 if(ext) ext->id_res_hook(pin,ps,identity);
244 } 244 }
245 245
246 void consumer_t::check_authentication(const string& server,const params_t& p) { 246 void consumer_t::check_authentication(const string& server,const params_t& p) {
247 string request = "openid.mode=check_authentication"; 247 string request = "openid.mode=check_authentication";
248 for(params_t::const_iterator i=p.begin();i!=p.end();++i) { 248 for(params_t::const_iterator i=p.begin();i!=p.end();++i) {
249 if(i->first!="openid.mode") { 249 if(i->first!="openid.mode") {
250 request += '&'; 250 request += '&';
251 request += i->first; 251 request += i->first;
252 request += '='; 252 request += '=';
253 request += util::url_encode(i->second); 253 request += util::url_encode(i->second);
254 } 254 }
255 } 255 }
256 curl_pick_t curl = curl_pick_t::easy_init(); 256 curl_pick_t curl = curl_pick_t::easy_init();
257 if(!curl) 257 if(!curl)
258 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 258 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
259 CURLcode r; 259 CURLcode r;
260 (r=curl.misc_sets()) 260 (r=curl.misc_sets())
261 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str())) 261 || (r=curl.easy_setopt(CURLOPT_URL,server.c_str()))
262 || (r=curl.easy_setopt(CURLOPT_POST,1)) 262 || (r=curl.easy_setopt(CURLOPT_POST,1))
263 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data())) 263 || (r=curl.easy_setopt(CURLOPT_POSTFIELDS,request.data()))
264 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length())) 264 || (r=curl.easy_setopt(CURLOPT_POSTFIELDSIZE,request.length()))
265 || (r=curl.set_write()) 265 || (r=curl.set_write())
266 ; 266 ;
267 if(r) 267 if(r)
268 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 268 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
269 if( (r=curl.easy_perform()) ) 269 if( (r=curl.easy_perform()) )
270 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 270 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
271 params_t pp; pp.parse_keyvalues(curl.response); 271 params_t pp; pp.parse_keyvalues(curl.response);
272 if(pp.has_param("invalidate_handle")) 272 if(pp.has_param("invalidate_handle"))
273 invalidate_assoc(server,pp.get_param("invalidate_handle")); 273 invalidate_assoc(server,pp.get_param("invalidate_handle"));
274 if(pp.has_param("is_valid")) { 274 if(pp.has_param("is_valid")) {
275 if(pp.get_param("is_valid")=="true") 275 if(pp.get_param("is_valid")=="true")
276 return; 276 return;
277 }else if(pp.has_param("lifetime")) { 277 }else if(pp.has_param("lifetime")) {
278 if(util::string_to_long(pp.get_param("lifetime"))) 278 if(util::string_to_long(pp.get_param("lifetime")))
279 return; 279 return;
280 } 280 }
281 throw failed_check_authentication(OPKELE_CP_ "failed to verify response"); 281 throw failed_check_authentication(OPKELE_CP_ "failed to verify response");
282 } 282 }
283 283
284 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) { 284 void consumer_t::retrieve_links(const string& url,string& server,string& delegate) {
285 server.erase(); 285 server.erase();
286 delegate.erase(); 286 delegate.erase();
287 curl_pick_t curl = curl_pick_t::easy_init(); 287 curl_pick_t curl = curl_pick_t::easy_init();
288 if(!curl) 288 if(!curl)
289 throw exception_curl(OPKELE_CP_ "failed to initialize curl"); 289 throw exception_curl(OPKELE_CP_ "failed to initialize curl");
290 string& html = curl.response; 290 string& html = curl.response;
291 CURLcode r; 291 CURLcode r;
292 (r=curl.misc_sets()) 292 (r=curl.misc_sets())
293 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str())) 293 || (r=curl.easy_setopt(CURLOPT_URL,url.c_str()))
294 || (r=curl.set_write()); 294 || (r=curl.set_write());
295 ; 295 ;
296 if(r) 296 if(r)
297 throw exception_curl(OPKELE_CP_ "failed to set curly options",r); 297 throw exception_curl(OPKELE_CP_ "failed to set curly options",r);
298 r = curl.easy_perform(); 298 r = curl.easy_perform();
299 if(r && r!=CURLE_WRITE_ERROR) 299 if(r && r!=CURLE_WRITE_ERROR)
300 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r); 300 throw exception_curl(OPKELE_CP_ "failed to perform curly request",r);
301 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>", 301 static const char *re_bre = "<\\s*body\\b", *re_hdre = "<\\s*head[^>]*>",
302 *re_lre = "<\\s*link\\b([^>]+)>", 302 *re_lre = "<\\s*link\\b([^>]+)>",
303 *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]", 303 *re_rre = "\\brel\\s*=\\s*['\"]([^'\"]+)['\"]",
304 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]"; 304 *re_hre = "\\bhref\\s*=\\s*['\"]\\s*([^'\"\\s]+)\\s*['\"]";
305 pcre_matches_t m1(3), m2(3); 305 pcre_matches_t m1(3), m2(3);
306 pcre_t bre(re_bre,PCRE_CASELESS); 306 pcre_t bre(re_bre,PCRE_CASELESS);
307 if(bre.exec(html,m1)>0) 307 if(bre.exec(html,m1)>0)
308 html.erase(m1.begin(0)); 308 html.erase(m1.begin(0));
309 pcre_t hdre(re_hdre,PCRE_CASELESS); 309 pcre_t hdre(re_hdre,PCRE_CASELESS);
310 if(hdre.exec(html,m1)<=0) 310 if(hdre.exec(html,m1)<=0)
311 throw bad_input(OPKELE_CP_ "failed to find <head>"); 311 throw bad_input(OPKELE_CP_ "failed to find <head>");
312 html.erase(0,m1.end(0)+1); 312 html.erase(0,m1.end(0)+1);
313 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS); 313 pcre_t lre(re_lre,PCRE_CASELESS), rre(re_rre,PCRE_CASELESS), hre(re_hre,PCRE_CASELESS);