Commit 8dce0396 authored by sigiesec's avatar sigiesec

Problem: inconsistent handling of ZAP replies

Solution: unification, pulled up common behaviour to zap_client_t/zap_client_common_handshake_t
parent 8c58ef7f
...@@ -492,15 +492,9 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_) ...@@ -492,15 +492,9 @@ int zmq::curve_server_t::process_initiate (msg_t *msg_)
if (rc != 0) if (rc != 0)
return -1; return -1;
rc = receive_and_process_zap_reply (); rc = receive_and_process_zap_reply ();
if (rc == 0) if (rc == -1)
handle_zap_status_code ();
else
if (errno == EAGAIN)
state = waiting_for_zap_reply;
else
return -1; return -1;
} } else
else
state = sending_ready; state = sending_ready;
return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128, return parse_metadata (initiate_plaintext + crypto_box_ZEROBYTES + 128,
...@@ -573,12 +567,4 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key) ...@@ -573,12 +567,4 @@ int zmq::curve_server_t::send_zap_request (const uint8_t *key)
crypto_box_PUBLICKEYBYTES); crypto_box_PUBLICKEYBYTES);
} }
int zmq::curve_server_t::receive_and_process_zap_reply ()
{
int rc = zap_client_t::receive_and_process_zap_reply ();
if (rc == -1 && errno == EPROTO)
current_error_detail = zap;
return rc;
}
#endif #endif
...@@ -104,7 +104,6 @@ namespace zmq ...@@ -104,7 +104,6 @@ namespace zmq
int produce_error (msg_t *msg_) const; int produce_error (msg_t *msg_) const;
int send_zap_request (const uint8_t *key); int send_zap_request (const uint8_t *key);
int receive_and_process_zap_reply ();
}; };
} }
......
...@@ -130,7 +130,7 @@ int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_) ...@@ -130,7 +130,7 @@ int zmq::gssapi_server_t::process_handshake_command (msg_t *msg_)
return -1; return -1;
rc = receive_and_process_zap_reply (); rc = receive_and_process_zap_reply ();
if (rc != 0) { if (rc != 0) {
if (errno != EAGAIN) if (rc == -1)
return -1; return -1;
expecting_zap_reply = true; expecting_zap_reply = true;
} }
...@@ -192,7 +192,7 @@ int zmq::gssapi_server_t::zap_msg_available () ...@@ -192,7 +192,7 @@ int zmq::gssapi_server_t::zap_msg_available ()
const int rc = receive_and_process_zap_reply (); const int rc = receive_and_process_zap_reply ();
if (rc == 0) if (rc == 0)
state = send_ready; state = send_ready;
return rc; return rc == -1 ? -1 : 0;
} }
zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const zmq::mechanism_t::status_t zmq::gssapi_server_t::status () const
......
...@@ -79,7 +79,7 @@ int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_) ...@@ -79,7 +79,7 @@ int zmq::null_mechanism_t::next_handshake_command (msg_t *msg_)
return -1; return -1;
zap_request_sent = true; zap_request_sent = true;
rc = receive_and_process_zap_reply (); rc = receive_and_process_zap_reply ();
if (rc != 0) if (rc == -1 || rc == 1)
return -1; return -1;
zap_reply_received = true; zap_reply_received = true;
} }
...@@ -170,7 +170,7 @@ int zmq::null_mechanism_t::zap_msg_available () ...@@ -170,7 +170,7 @@ int zmq::null_mechanism_t::zap_msg_available ()
const int rc = receive_and_process_zap_reply (); const int rc = receive_and_process_zap_reply ();
if (rc == 0) if (rc == 0)
zap_reply_received = true; zap_reply_received = true;
return rc; return rc == -1 ? -1 : 0;
} }
zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const zmq::mechanism_t::status_t zmq::null_mechanism_t::status () const
......
...@@ -172,16 +172,7 @@ int zmq::plain_server_t::process_hello (msg_t *msg_) ...@@ -172,16 +172,7 @@ int zmq::plain_server_t::process_hello (msg_t *msg_)
rc = send_zap_request (username, password); rc = send_zap_request (username, password);
if (rc != 0) if (rc != 0)
return -1; return -1;
rc = receive_and_process_zap_reply (); return receive_and_process_zap_reply () == -1 ? -1 : 0;
if (rc == 0)
handle_zap_status_code ();
else
if (errno == EAGAIN)
state = waiting_for_zap_reply;
else
return -1;
return 0;
} }
int zmq::plain_server_t::produce_welcome (msg_t *msg_) const int zmq::plain_server_t::produce_welcome (msg_t *msg_) const
......
...@@ -159,8 +159,12 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -159,8 +159,12 @@ int zap_client_t::receive_and_process_zap_reply ()
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
rc = session->read_zap_msg (&msg[i]); rc = session->read_zap_msg (&msg[i]);
if (rc == -1) if (rc == -1) {
if (errno == EAGAIN) {
return 1;
}
return close_and_return (msg, -1); return close_and_return (msg, -1);
}
if ((msg[i].flags () & msg_t::more) == (i < 6 ? 0 : msg_t::more)) { if ((msg[i].flags () & msg_t::more) == (i < 6 ? 0 : msg_t::more)) {
// CURVE I : ZAP handler sent incomplete reply message // CURVE I : ZAP handler sent incomplete reply message
errno = EPROTO; errno = EPROTO;
...@@ -209,8 +213,9 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -209,8 +213,9 @@ int zap_client_t::receive_and_process_zap_reply ()
rc = parse_metadata (static_cast<const unsigned char *> (msg[6].data ()), rc = parse_metadata (static_cast<const unsigned char *> (msg[6].data ()),
msg[6].size (), true); msg[6].size (), true);
if (rc != 0) if (rc != 0) {
return close_and_return (msg, -1); return close_and_return (msg, -1);
}
// Close all reply frames // Close all reply frames
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
...@@ -218,6 +223,8 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -218,6 +223,8 @@ int zap_client_t::receive_and_process_zap_reply ()
errno_assert (rc2 == 0); errno_assert (rc2 == 0);
} }
handle_zap_status_code ();
return 0; return 0;
} }
...@@ -253,12 +260,7 @@ int zap_client_common_handshake_t::zap_msg_available () ...@@ -253,12 +260,7 @@ int zap_client_common_handshake_t::zap_msg_available ()
errno = EFSM; errno = EFSM;
return -1; return -1;
} }
const int rc = receive_and_process_zap_reply (); return receive_and_process_zap_reply () == -1 ? -1 : 0;
if (rc == 0)
handle_zap_status_code ();
else if (errno == EPROTO)
current_error_detail = mechanism_t::zap;
return rc;
} }
void zap_client_common_handshake_t::handle_zap_status_code () void zap_client_common_handshake_t::handle_zap_status_code ()
...@@ -295,4 +297,21 @@ mechanism_t::error_detail_t zap_client_common_handshake_t::error_detail () const ...@@ -295,4 +297,21 @@ mechanism_t::error_detail_t zap_client_common_handshake_t::error_detail () const
return current_error_detail; return current_error_detail;
} }
int zap_client_common_handshake_t::receive_and_process_zap_reply ()
{
int rc = zap_client_t::receive_and_process_zap_reply ();
switch (rc) {
case -1:
if (errno == EPROTO)
current_error_detail = mechanism_t::zap;
break;
case 1:
// TODO shouldn't the state already be this?
state = waiting_for_zap_reply;
break;
case 0:
break;
}
return rc;
}
} }
...@@ -54,8 +54,8 @@ class zap_client_t : public virtual mechanism_t ...@@ -54,8 +54,8 @@ class zap_client_t : public virtual mechanism_t
size_t *credentials_sizes, size_t *credentials_sizes,
size_t credentials_count); size_t credentials_count);
virtual int receive_and_process_zap_reply ();
int receive_and_process_zap_reply (); virtual void handle_zap_status_code () {}
protected: protected:
session_base_t *const session; session_base_t *const session;
...@@ -85,12 +85,13 @@ class zap_client_common_handshake_t : public zap_client_t ...@@ -85,12 +85,13 @@ class zap_client_common_handshake_t : public zap_client_t
const options_t &options_, const options_t &options_,
state_t zap_reply_ok_state); state_t zap_reply_ok_state);
// methods from mechanism_t // methods from mechanism_t
status_t status () const; status_t status () const;
int zap_msg_available (); int zap_msg_available ();
error_detail_t error_detail () const; error_detail_t error_detail () const;
// own methods // zap_client_t methods
int receive_and_process_zap_reply ();
void handle_zap_status_code (); void handle_zap_status_code ();
// Current FSM state // Current FSM state
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment