diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-20 16:30:19 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-20 16:30:19 +0000 |
commit | d0672be51d1a4c25f3dd7d91f9d9c1293b33adc6 (patch) | |
tree | 93a2a7f1b8d3426a4e65692b56e12e4011b60241 /net/socket/ssl_host_info.cc | |
parent | a5e842bf6fda331cdacf3a8d56da19a3ea8d3d0b (diff) | |
download | chromium_src-d0672be51d1a4c25f3dd7d91f9d9c1293b33adc6.zip chromium_src-d0672be51d1a4c25f3dd7d91f9d9c1293b33adc6.tar.gz chromium_src-d0672be51d1a4c25f3dd7d91f9d9c1293b33adc6.tar.bz2 |
net: move SSL host info serialisation into SSLHostInfo
This is a prelude to SSLHostInfo being able to kick off certificate
validation based on predicted certificates.
In order for that to happen, SSLHostInfo has to be able to parse the
contents of the saved data, therefore the
serialisation/deserialisation is moved into SSLHostInfo and we use a
protocol buffer to clean things up.
TEST=net_unittests
BUG=none
Review URL: http://codereview.chromium.org/3915001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63221 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket/ssl_host_info.cc')
-rw-r--r-- | net/socket/ssl_host_info.cc | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/net/socket/ssl_host_info.cc b/net/socket/ssl_host_info.cc new file mode 100644 index 0000000..4b5bb02 --- /dev/null +++ b/net/socket/ssl_host_info.cc @@ -0,0 +1,100 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "net/socket/ssl_host_info.h" + +#include "net/socket/ssl_client_socket.h" +#include "net/socket/ssl_host_info.pb.h" + +namespace net { + +SSLHostInfo::SSLHostInfo() { + state_.npn_valid = false; +} + +SSLHostInfo::~SSLHostInfo() {} + +// This array and the next two functions serve to map between the internal NPN +// status enum (which might change across versions) and the protocol buffer +// based enum (which will not). +static const struct { + SSLClientSocket::NextProtoStatus npn_status; + SSLHostInfoProto::NextProtoStatus proto_status; +} kNPNStatusMapping[] = { + { SSLClientSocket::kNextProtoUnsupported, SSLHostInfoProto::UNSUPPORTED }, + { SSLClientSocket::kNextProtoNegotiated, SSLHostInfoProto::NEGOTIATED }, + { SSLClientSocket::kNextProtoNoOverlap, SSLHostInfoProto::NO_OVERLAP }, +}; + +static SSLClientSocket::NextProtoStatus NPNStatusFromProtoStatus( + SSLHostInfoProto::NextProtoStatus proto_status) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNPNStatusMapping) - 1; i++) { + if (kNPNStatusMapping[i].proto_status == proto_status) + return kNPNStatusMapping[i].npn_status; + } + return kNPNStatusMapping[ARRAYSIZE_UNSAFE(kNPNStatusMapping) - 1].npn_status; +} + +static SSLHostInfoProto::NextProtoStatus ProtoStatusFromNPNStatus( + SSLClientSocket::NextProtoStatus npn_status) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kNPNStatusMapping) - 1; i++) { + if (kNPNStatusMapping[i].npn_status == npn_status) + return kNPNStatusMapping[i].proto_status; + } + return kNPNStatusMapping[ARRAYSIZE_UNSAFE(kNPNStatusMapping)-1].proto_status; +} + +const SSLHostInfo::State& SSLHostInfo::state() const { + return state_; +} + +SSLHostInfo::State* SSLHostInfo::mutable_state() { + return &state_; +} + +bool SSLHostInfo::Parse(const std::string& data) { + SSLHostInfoProto proto; + State* state = mutable_state(); + + state->certs.clear(); + state->server_hello.clear(); + state->npn_valid = false; + + if (!proto.ParseFromString(data)) + return false; + + for (int i = 0; i < proto.certificate_der_size(); i++) + state->certs.push_back(proto.certificate_der(i)); + if (proto.has_server_hello()) + state->server_hello = proto.server_hello(); + if (proto.has_npn_status() && proto.has_npn_protocol()) { + state->npn_valid = true; + state->npn_status = NPNStatusFromProtoStatus(proto.npn_status()); + state->npn_protocol = proto.npn_protocol(); + } + + return true; +} + +std::string SSLHostInfo::Serialize() const { + SSLHostInfoProto proto; + + for (std::vector<std::string>::const_iterator + i = state_.certs.begin(); i != state_.certs.end(); i++) { + proto.add_certificate_der(*i); + } + if (!state_.server_hello.empty()) + proto.set_server_hello(state_.server_hello); + + if (state_.npn_valid) { + proto.set_npn_status(ProtoStatusFromNPNStatus(state_.npn_status)); + proto.set_npn_protocol(state_.npn_protocol); + } + + return proto.SerializeAsString(); +} + +SSLHostInfoFactory::~SSLHostInfoFactory() {} + +} // namespace net |