aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_client.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2012-06-09 07:14:36 +0000
committerlloyd <[email protected]>2012-06-09 07:14:36 +0000
commit55538ea412ef728f04ca4d86ec6b34360f7bb92c (patch)
treefe3a934f8e66642b67120cc022b8776c363f112a /src/tls/tls_client.cpp
parent6af7a5414fde971efd767f3e881a16acdda6f497 (diff)
m_ namespace Channel, Client, and Server.
Fix printing of Camellia ciphersuites.
Diffstat (limited to 'src/tls/tls_client.cpp')
-rw-r--r--src/tls/tls_client.cpp354
1 files changed, 177 insertions, 177 deletions
diff --git a/src/tls/tls_client.cpp b/src/tls/tls_client.cpp
index ec3d9953f..1a515b1f6 100644
--- a/src/tls/tls_client.cpp
+++ b/src/tls/tls_client.cpp
@@ -28,59 +28,59 @@ Client::Client(std::function<void (const byte[], size_t)> output_fn,
const std::string& hostname,
std::function<std::string (std::vector<std::string>)> next_protocol) :
Channel(output_fn, proc_fn, handshake_fn),
- policy(policy),
- rng(rng),
- session_manager(session_manager),
- creds(creds),
+ m_policy(policy),
+ m_rng(rng),
+ m_session_manager(session_manager),
+ m_creds(creds),
m_hostname(hostname)
{
- writer.set_version(Protocol_Version::SSL_V3);
+ m_writer.set_version(Protocol_Version::SSL_V3);
- state = new Handshake_State(new Stream_Handshake_Reader);
- state->set_expected_next(SERVER_HELLO);
+ m_state = new Handshake_State(new Stream_Handshake_Reader);
+ m_state->set_expected_next(SERVER_HELLO);
- state->client_npn_cb = next_protocol;
+ m_state->client_npn_cb = next_protocol;
- const std::string srp_identifier = creds.srp_identifier("tls-client", hostname);
+ const std::string srp_identifier = m_creds.srp_identifier("tls-client", hostname);
const bool send_npn_request = static_cast<bool>(next_protocol);
if(hostname != "")
{
Session session_info;
- if(session_manager.load_from_host_info(hostname, 0, session_info))
+ if(m_session_manager.load_from_host_info(m_hostname, 0, session_info))
{
if(session_info.srp_identifier() == srp_identifier)
{
- state->client_hello = new Client_Hello(
- writer,
- state->hash,
- policy,
- rng,
- secure_renegotiation.for_client_hello(),
+ m_state->client_hello = new Client_Hello(
+ m_writer,
+ m_state->hash,
+ m_policy,
+ m_rng,
+ m_secure_renegotiation.for_client_hello(),
session_info,
send_npn_request);
- state->resume_master_secret = session_info.master_secret();
+ m_state->resume_master_secret = session_info.master_secret();
}
}
}
- if(!state->client_hello) // not resuming
+ if(!m_state->client_hello) // not resuming
{
- state->client_hello = new Client_Hello(
- writer,
- state->hash,
- policy.pref_version(),
- policy,
- rng,
- secure_renegotiation.for_client_hello(),
+ m_state->client_hello = new Client_Hello(
+ m_writer,
+ m_state->hash,
+ m_policy.pref_version(),
+ m_policy,
+ m_rng,
+ m_secure_renegotiation.for_client_hello(),
send_npn_request,
- hostname,
+ m_hostname,
srp_identifier);
}
- secure_renegotiation.update(state->client_hello);
+ m_secure_renegotiation.update(m_state->client_hello);
}
/*
@@ -88,53 +88,53 @@ Client::Client(std::function<void (const byte[], size_t)> output_fn,
*/
void Client::renegotiate(bool force_full_renegotiation)
{
- if(state && state->client_hello)
+ if(m_state && m_state->client_hello)
return; // currently in active handshake
- delete state;
- state = new Handshake_State(new Stream_Handshake_Reader);
+ delete m_state;
+ m_state = new Handshake_State(new Stream_Handshake_Reader);
- state->set_expected_next(SERVER_HELLO);
+ m_state->set_expected_next(SERVER_HELLO);
if(!force_full_renegotiation)
{
Session session_info;
- if(session_manager.load_from_host_info(m_hostname, 0, session_info))
+ if(m_session_manager.load_from_host_info(m_hostname, 0, session_info))
{
- state->client_hello = new Client_Hello(
- writer,
- state->hash,
- policy,
- rng,
- secure_renegotiation.for_client_hello(),
+ m_state->client_hello = new Client_Hello(
+ m_writer,
+ m_state->hash,
+ m_policy,
+ m_rng,
+ m_secure_renegotiation.for_client_hello(),
session_info);
- state->resume_master_secret = session_info.master_secret();
+ m_state->resume_master_secret = session_info.master_secret();
}
}
- if(!state->client_hello)
+ if(!m_state->client_hello)
{
- state->client_hello = new Client_Hello(
- writer,
- state->hash,
- reader.get_version(),
- policy,
- rng,
- secure_renegotiation.for_client_hello());
+ m_state->client_hello = new Client_Hello(
+ m_writer,
+ m_state->hash,
+ m_reader.get_version(),
+ m_policy,
+ m_rng,
+ m_secure_renegotiation.for_client_hello());
}
- secure_renegotiation.update(state->client_hello);
+ m_secure_renegotiation.update(m_state->client_hello);
}
void Client::alert_notify(const Alert& alert)
{
if(alert.type() == Alert::NO_RENEGOTIATION)
{
- if(handshake_completed && state)
+ if(m_handshake_completed && m_state)
{
- delete state;
- state = nullptr;
+ delete m_state;
+ m_state = nullptr;
}
}
}
@@ -145,7 +145,7 @@ void Client::alert_notify(const Alert& alert)
void Client::process_handshake_msg(Handshake_Type type,
const std::vector<byte>& contents)
{
- if(!state)
+ if(!m_state)
throw Unexpected_Message("Unexpected handshake message from server");
if(type == HELLO_REQUEST)
@@ -153,13 +153,13 @@ void Client::process_handshake_msg(Handshake_Type type,
Hello_Request hello_request(contents);
// Ignore request entirely if we are currently negotiating a handshake
- if(state->client_hello)
+ if(m_state->client_hello)
return;
- if(!secure_renegotiation.supported() && !policy.allow_insecure_renegotiation())
+ if(!m_secure_renegotiation.supported() && !m_policy.allow_insecure_renegotiation())
{
- delete state;
- state = nullptr;
+ delete m_state;
+ m_state = nullptr;
// RFC 5746 section 4.2
send_alert(Alert(Alert::NO_RENEGOTIATION));
@@ -171,57 +171,57 @@ void Client::process_handshake_msg(Handshake_Type type,
return;
}
- state->confirm_transition_to(type);
+ m_state->confirm_transition_to(type);
if(type != HANDSHAKE_CCS && type != FINISHED)
- state->hash.update(type, contents);
+ m_state->hash.update(type, contents);
if(type == SERVER_HELLO)
{
- state->server_hello = new Server_Hello(contents);
+ m_state->server_hello = new Server_Hello(contents);
- if(!state->client_hello->offered_suite(state->server_hello->ciphersuite()))
+ if(!m_state->client_hello->offered_suite(m_state->server_hello->ciphersuite()))
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server replied with ciphersuite we didn't send");
}
- if(!value_exists(state->client_hello->compression_methods(),
- state->server_hello->compression_method()))
+ if(!value_exists(m_state->client_hello->compression_methods(),
+ m_state->server_hello->compression_method()))
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server replied with compression method we didn't send");
}
- if(!state->client_hello->next_protocol_notification() &&
- state->server_hello->next_protocol_notification())
+ if(!m_state->client_hello->next_protocol_notification() &&
+ m_state->server_hello->next_protocol_notification())
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server sent next protocol but we didn't request it");
}
- if(state->server_hello->supports_session_ticket())
+ if(m_state->server_hello->supports_session_ticket())
{
- if(!state->client_hello->supports_session_ticket())
+ if(!m_state->client_hello->supports_session_ticket())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server sent session ticket extension but we did not");
}
- state->set_version(state->server_hello->version());
+ m_state->set_version(m_state->server_hello->version());
- writer.set_version(state->version());
- reader.set_version(state->version());
+ m_writer.set_version(m_state->version());
+ m_reader.set_version(m_state->version());
- secure_renegotiation.update(state->server_hello);
+ m_secure_renegotiation.update(m_state->server_hello);
- m_peer_supports_heartbeats = state->server_hello->supports_heartbeats();
- m_heartbeat_sending_allowed = state->server_hello->peer_can_send_heartbeats();
+ m_peer_supports_heartbeats = m_state->server_hello->supports_heartbeats();
+ m_heartbeat_sending_allowed = m_state->server_hello->peer_can_send_heartbeats();
- state->suite = Ciphersuite::by_id(state->server_hello->ciphersuite());
+ m_state->suite = Ciphersuite::by_id(m_state->server_hello->ciphersuite());
const bool server_returned_same_session_id =
- !state->server_hello->session_id().empty() &&
- (state->server_hello->session_id() == state->client_hello->session_id());
+ !m_state->server_hello->session_id().empty() &&
+ (m_state->server_hello->session_id() == m_state->client_hello->session_id());
if(server_returned_same_session_id)
{
@@ -231,41 +231,41 @@ void Client::process_handshake_msg(Handshake_Type type,
* In this case, we offered the version used in the original
* session, and the server must resume with the same version.
*/
- if(state->server_hello->version() != state->client_hello->version())
+ if(m_state->server_hello->version() != m_state->client_hello->version())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Server resumed session but with wrong version");
- state->keys = Session_Keys(state,
- state->resume_master_secret,
+ m_state->keys = Session_Keys(m_state,
+ m_state->resume_master_secret,
true);
// The server is not strictly required to send us a new ticket
- if(state->server_hello->supports_session_ticket())
- state->set_expected_next(NEW_SESSION_TICKET);
+ if(m_state->server_hello->supports_session_ticket())
+ m_state->set_expected_next(NEW_SESSION_TICKET);
- state->set_expected_next(HANDSHAKE_CCS);
+ m_state->set_expected_next(HANDSHAKE_CCS);
}
else
{
// new session
- if(state->version() > state->client_hello->version())
+ if(m_state->version() > m_state->client_hello->version())
{
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Client: Server replied with bad version");
}
- if(state->version() < policy.min_version())
+ if(m_state->version() < m_policy.min_version())
{
throw TLS_Exception(Alert::PROTOCOL_VERSION,
"Client: Server is too old for specified policy");
}
- if(state->suite.sig_algo() != "")
+ if(m_state->suite.sig_algo() != "")
{
- state->set_expected_next(CERTIFICATE);
+ m_state->set_expected_next(CERTIFICATE);
}
- else if(state->suite.kex_algo() == "PSK")
+ else if(m_state->suite.kex_algo() == "PSK")
{
/* PSK is anonymous so no certificate/cert req message is
ever sent. The server may or may not send a server kex,
@@ -275,67 +275,67 @@ void Client::process_handshake_msg(Handshake_Type type,
DH exchange portion.
*/
- state->set_expected_next(SERVER_KEX);
- state->set_expected_next(SERVER_HELLO_DONE);
+ m_state->set_expected_next(SERVER_KEX);
+ m_state->set_expected_next(SERVER_HELLO_DONE);
}
- else if(state->suite.kex_algo() != "RSA")
+ else if(m_state->suite.kex_algo() != "RSA")
{
- state->set_expected_next(SERVER_KEX);
+ m_state->set_expected_next(SERVER_KEX);
}
else
{
- state->set_expected_next(CERTIFICATE_REQUEST); // optional
- state->set_expected_next(SERVER_HELLO_DONE);
+ m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
+ m_state->set_expected_next(SERVER_HELLO_DONE);
}
}
}
else if(type == CERTIFICATE)
{
- if(state->suite.kex_algo() != "RSA")
+ if(m_state->suite.kex_algo() != "RSA")
{
- state->set_expected_next(SERVER_KEX);
+ m_state->set_expected_next(SERVER_KEX);
}
else
{
- state->set_expected_next(CERTIFICATE_REQUEST); // optional
- state->set_expected_next(SERVER_HELLO_DONE);
+ m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
+ m_state->set_expected_next(SERVER_HELLO_DONE);
}
- state->server_certs = new Certificate(contents);
+ m_state->server_certs = new Certificate(contents);
- peer_certs = state->server_certs->cert_chain();
- if(peer_certs.size() == 0)
+ m_peer_certs = m_state->server_certs->cert_chain();
+ if(m_peer_certs.empty())
throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
"Client: No certificates sent by server");
try
{
- creds.verify_certificate_chain("tls-client", m_hostname, peer_certs);
+ m_creds.verify_certificate_chain("tls-client", m_hostname, m_peer_certs);
}
catch(std::exception& e)
{
throw TLS_Exception(Alert::BAD_CERTIFICATE, e.what());
}
- std::unique_ptr<Public_Key> peer_key(peer_certs[0].subject_public_key());
+ std::unique_ptr<Public_Key> peer_key(m_peer_certs[0].subject_public_key());
- if(peer_key->algo_name() != state->suite.sig_algo())
+ if(peer_key->algo_name() != m_state->suite.sig_algo())
throw TLS_Exception(Alert::ILLEGAL_PARAMETER,
"Certificate key type did not match ciphersuite");
}
else if(type == SERVER_KEX)
{
- state->set_expected_next(CERTIFICATE_REQUEST); // optional
- state->set_expected_next(SERVER_HELLO_DONE);
+ m_state->set_expected_next(CERTIFICATE_REQUEST); // optional
+ m_state->set_expected_next(SERVER_HELLO_DONE);
- state->server_kex = new Server_Key_Exchange(contents,
- state->suite.kex_algo(),
- state->suite.sig_algo(),
- state->version());
+ m_state->server_kex = new Server_Key_Exchange(contents,
+ m_state->suite.kex_algo(),
+ m_state->suite.sig_algo(),
+ m_state->version());
- if(state->suite.sig_algo() != "")
+ if(m_state->suite.sig_algo() != "")
{
- if(!state->server_kex->verify(peer_certs[0], state))
+ if(!m_state->server_kex->verify(m_peer_certs[0], m_state))
{
throw TLS_Exception(Alert::DECRYPT_ERROR,
"Bad signature on server key exchange");
@@ -344,141 +344,141 @@ void Client::process_handshake_msg(Handshake_Type type,
}
else if(type == CERTIFICATE_REQUEST)
{
- state->set_expected_next(SERVER_HELLO_DONE);
- state->cert_req = new Certificate_Req(contents, state->version());
+ m_state->set_expected_next(SERVER_HELLO_DONE);
+ m_state->cert_req = new Certificate_Req(contents, m_state->version());
}
else if(type == SERVER_HELLO_DONE)
{
- state->server_hello_done = new Server_Hello_Done(contents);
+ m_state->server_hello_done = new Server_Hello_Done(contents);
- if(state->received_handshake_msg(CERTIFICATE_REQUEST))
+ if(m_state->received_handshake_msg(CERTIFICATE_REQUEST))
{
const std::vector<std::string>& types =
- state->cert_req->acceptable_cert_types();
+ m_state->cert_req->acceptable_cert_types();
std::vector<X509_Certificate> client_certs =
- creds.cert_chain(types,
- "tls-client",
- m_hostname);
+ m_creds.cert_chain(types,
+ "tls-client",
+ m_hostname);
- state->client_certs = new Certificate(writer,
- state->hash,
+ m_state->client_certs = new Certificate(m_writer,
+ m_state->hash,
client_certs);
}
- state->client_kex =
- new Client_Key_Exchange(writer,
- state,
- creds,
- peer_certs,
+ m_state->client_kex =
+ new Client_Key_Exchange(m_writer,
+ m_state,
+ m_creds,
+ m_peer_certs,
m_hostname,
- rng);
+ m_rng);
- state->keys = Session_Keys(state,
- state->client_kex->pre_master_secret(),
+ m_state->keys = Session_Keys(m_state,
+ m_state->client_kex->pre_master_secret(),
false);
- if(state->received_handshake_msg(CERTIFICATE_REQUEST) &&
- !state->client_certs->empty())
+ if(m_state->received_handshake_msg(CERTIFICATE_REQUEST) &&
+ !m_state->client_certs->empty())
{
Private_Key* private_key =
- creds.private_key_for(state->client_certs->cert_chain()[0],
- "tls-client",
- m_hostname);
+ m_creds.private_key_for(m_state->client_certs->cert_chain()[0],
+ "tls-client",
+ m_hostname);
- state->client_verify = new Certificate_Verify(writer,
- state,
- rng,
+ m_state->client_verify = new Certificate_Verify(m_writer,
+ m_state,
+ m_rng,
private_key);
}
- writer.send(CHANGE_CIPHER_SPEC, 1);
+ m_writer.send(CHANGE_CIPHER_SPEC, 1);
- writer.activate(CLIENT, state->suite, state->keys,
- state->server_hello->compression_method());
+ m_writer.activate(CLIENT, m_state->suite, m_state->keys,
+ m_state->server_hello->compression_method());
- if(state->server_hello->next_protocol_notification())
+ if(m_state->server_hello->next_protocol_notification())
{
const std::string protocol =
- state->client_npn_cb(state->server_hello->next_protocols());
+ m_state->client_npn_cb(m_state->server_hello->next_protocols());
- state->next_protocol = new Next_Protocol(writer, state->hash, protocol);
+ m_state->next_protocol = new Next_Protocol(m_writer, m_state->hash, protocol);
}
- state->client_finished = new Finished(writer, state, CLIENT);
+ m_state->client_finished = new Finished(m_writer, m_state, CLIENT);
- if(state->server_hello->supports_session_ticket())
- state->set_expected_next(NEW_SESSION_TICKET);
+ if(m_state->server_hello->supports_session_ticket())
+ m_state->set_expected_next(NEW_SESSION_TICKET);
else
- state->set_expected_next(HANDSHAKE_CCS);
+ m_state->set_expected_next(HANDSHAKE_CCS);
}
else if(type == NEW_SESSION_TICKET)
{
- state->new_session_ticket = new New_Session_Ticket(contents);
+ m_state->new_session_ticket = new New_Session_Ticket(contents);
- state->set_expected_next(HANDSHAKE_CCS);
+ m_state->set_expected_next(HANDSHAKE_CCS);
}
else if(type == HANDSHAKE_CCS)
{
- state->set_expected_next(FINISHED);
+ m_state->set_expected_next(FINISHED);
- reader.activate(CLIENT, state->suite, state->keys,
- state->server_hello->compression_method());
+ m_reader.activate(CLIENT, m_state->suite, m_state->keys,
+ m_state->server_hello->compression_method());
}
else if(type == FINISHED)
{
- state->set_expected_next(HELLO_REQUEST);
+ m_state->set_expected_next(HELLO_REQUEST);
- state->server_finished = new Finished(contents);
+ m_state->server_finished = new Finished(contents);
- if(!state->server_finished->verify(state, SERVER))
+ if(!m_state->server_finished->verify(m_state, SERVER))
throw TLS_Exception(Alert::DECRYPT_ERROR,
"Finished message didn't verify");
- state->hash.update(type, contents);
+ m_state->hash.update(type, contents);
- if(!state->client_finished) // session resume case
+ if(!m_state->client_finished) // session resume case
{
- writer.send(CHANGE_CIPHER_SPEC, 1);
+ m_writer.send(CHANGE_CIPHER_SPEC, 1);
- writer.activate(CLIENT, state->suite, state->keys,
- state->server_hello->compression_method());
+ m_writer.activate(CLIENT, m_state->suite, m_state->keys,
+ m_state->server_hello->compression_method());
- state->client_finished = new Finished(writer, state, CLIENT);
+ m_state->client_finished = new Finished(m_writer, m_state, CLIENT);
}
- secure_renegotiation.update(state->client_finished, state->server_finished);
+ m_secure_renegotiation.update(m_state->client_finished, m_state->server_finished);
- std::vector<byte> session_id = state->server_hello->session_id();
+ std::vector<byte> session_id = m_state->server_hello->session_id();
- const std::vector<byte>& session_ticket = state->session_ticket();
+ const std::vector<byte>& session_ticket = m_state->session_ticket();
if(session_id.empty() && !session_ticket.empty())
- session_id = make_hello_random(rng);
+ session_id = make_hello_random(m_rng);
Session session_info(
session_id,
- state->keys.master_secret(),
- state->server_hello->version(),
- state->server_hello->ciphersuite(),
- state->server_hello->compression_method(),
+ m_state->keys.master_secret(),
+ m_state->server_hello->version(),
+ m_state->server_hello->ciphersuite(),
+ m_state->server_hello->compression_method(),
CLIENT,
- secure_renegotiation.supported(),
- state->server_hello->fragment_size(),
- peer_certs,
+ m_secure_renegotiation.supported(),
+ m_state->server_hello->fragment_size(),
+ m_peer_certs,
session_ticket,
m_hostname,
""
);
- if(handshake_fn(session_info))
- session_manager.save(session_info);
+ if(m_handshake_fn(session_info))
+ m_session_manager.save(session_info);
else
- session_manager.remove_entry(session_info.session_id());
+ m_session_manager.remove_entry(session_info.session_id());
- delete state;
- state = nullptr;
- handshake_completed = true;
+ delete m_state;
+ m_state = nullptr;
+ m_handshake_completed = true;
}
else
throw Unexpected_Message("Unknown handshake message received");