Home | History | Annotate | Download | only in common
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/common/ssl_status_serialization.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/pickle.h"
      9 
     10 namespace content {
     11 
     12 std::string SerializeSecurityInfo(
     13     int cert_id,
     14     net::CertStatus cert_status,
     15     int security_bits,
     16     int ssl_connection_status,
     17     const SignedCertificateTimestampIDStatusList&
     18         signed_certificate_timestamp_ids) {
     19   Pickle pickle;
     20   pickle.WriteInt(cert_id);
     21   pickle.WriteUInt32(cert_status);
     22   pickle.WriteInt(security_bits);
     23   pickle.WriteInt(ssl_connection_status);
     24   pickle.WriteInt(signed_certificate_timestamp_ids.size());
     25   for (SignedCertificateTimestampIDStatusList::const_iterator iter =
     26            signed_certificate_timestamp_ids.begin();
     27        iter != signed_certificate_timestamp_ids.end(); ++iter) {
     28     pickle.WriteInt(iter->id);
     29     pickle.WriteUInt16(iter->status);
     30   }
     31   return std::string(static_cast<const char*>(pickle.data()), pickle.size());
     32 }
     33 
     34 bool DeserializeSecurityInfo(
     35     const std::string& state,
     36     int* cert_id,
     37     net::CertStatus* cert_status,
     38     int* security_bits,
     39     int* ssl_connection_status,
     40     SignedCertificateTimestampIDStatusList* signed_certificate_timestamp_ids) {
     41   DCHECK(cert_id && cert_status && security_bits && ssl_connection_status &&
     42          signed_certificate_timestamp_ids);
     43   if (state.empty()) {
     44     // No SSL used.
     45     *cert_id = 0;
     46     // The following are not applicable and are set to the default values.
     47     *cert_status = 0;
     48     *security_bits = -1;
     49     *ssl_connection_status = 0;
     50     signed_certificate_timestamp_ids->clear();
     51     return false;
     52   }
     53 
     54   Pickle pickle(state.data(), static_cast<int>(state.size()));
     55   PickleIterator iter(pickle);
     56   bool pickle_read_ok = pickle.ReadInt(&iter, cert_id) &&
     57                         pickle.ReadUInt32(&iter, cert_status) &&
     58                         pickle.ReadInt(&iter, security_bits) &&
     59                         pickle.ReadInt(&iter, ssl_connection_status);
     60   if (!pickle_read_ok)
     61     return pickle_read_ok;
     62 
     63   int num_scts_to_read;
     64   pickle_read_ok = pickle.ReadInt(&iter, &num_scts_to_read);
     65   int id;
     66   uint16 status;
     67   for (; pickle_read_ok && num_scts_to_read > 0; --num_scts_to_read) {
     68     pickle_read_ok = pickle.ReadInt(&iter, &id) &&
     69                      pickle.ReadUInt16(&iter, &status);
     70     if (pickle_read_ok) {
     71       signed_certificate_timestamp_ids->push_back(
     72           SignedCertificateTimestampIDAndStatus(
     73               id,
     74               static_cast<net::ct::SCTVerifyStatus>(status)));
     75     }
     76   }
     77 
     78   return pickle_read_ok;
     79 }
     80 
     81 }  // namespace content
     82