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.
5 #include "content/common/ssl_status_serialization.h"
7 #include "base/logging.h"
8 #include "base/pickle.h"
12 // Checks that an integer |security_style| is a valid SecurityStyle enum
13 // value. Returns true if valid, false otherwise.
14 bool CheckSecurityStyle(int security_style
) {
15 switch (security_style
) {
16 case content::SECURITY_STYLE_UNKNOWN
:
17 case content::SECURITY_STYLE_UNAUTHENTICATED
:
18 case content::SECURITY_STYLE_AUTHENTICATION_BROKEN
:
19 case content::SECURITY_STYLE_WARNING
:
20 case content::SECURITY_STYLE_AUTHENTICATED
:
30 std::string
SerializeSecurityInfo(const SSLStatus
& ssl_status
) {
32 pickle
.WriteInt(ssl_status
.security_style
);
33 pickle
.WriteInt(ssl_status
.cert_id
);
34 pickle
.WriteUInt32(ssl_status
.cert_status
);
35 pickle
.WriteInt(ssl_status
.security_bits
);
36 pickle
.WriteInt(ssl_status
.connection_status
);
37 pickle
.WriteInt(ssl_status
.signed_certificate_timestamp_ids
.size());
38 for (SignedCertificateTimestampIDStatusList::const_iterator iter
=
39 ssl_status
.signed_certificate_timestamp_ids
.begin();
40 iter
!= ssl_status
.signed_certificate_timestamp_ids
.end(); ++iter
) {
41 pickle
.WriteInt(iter
->id
);
42 pickle
.WriteUInt16(iter
->status
);
44 return std::string(static_cast<const char*>(pickle
.data()), pickle
.size());
47 bool DeserializeSecurityInfo(const std::string
& state
, SSLStatus
* ssl_status
) {
48 *ssl_status
= SSLStatus();
55 base::Pickle
pickle(state
.data(), static_cast<int>(state
.size()));
56 base::PickleIterator
iter(pickle
);
59 if (!iter
.ReadInt(&security_style
) || !iter
.ReadInt(&ssl_status
->cert_id
) ||
60 !iter
.ReadUInt32(&ssl_status
->cert_status
) ||
61 !iter
.ReadInt(&ssl_status
->security_bits
) ||
62 !iter
.ReadInt(&ssl_status
->connection_status
) ||
63 !iter
.ReadInt(&num_scts_to_read
)) {
64 *ssl_status
= SSLStatus();
68 if (!CheckSecurityStyle(security_style
)) {
69 *ssl_status
= SSLStatus();
73 ssl_status
->security_style
= static_cast<SecurityStyle
>(security_style
);
75 // Sanity check |security_bits|: the only allowed negative value is -1.
76 if (ssl_status
->security_bits
< -1) {
77 *ssl_status
= SSLStatus();
81 for (; num_scts_to_read
> 0; --num_scts_to_read
) {
84 if (!iter
.ReadInt(&id
) || !iter
.ReadUInt16(&status
)) {
85 *ssl_status
= SSLStatus();
89 ssl_status
->signed_certificate_timestamp_ids
.push_back(
90 SignedCertificateTimestampIDAndStatus(
91 id
, static_cast<net::ct::SCTVerifyStatus
>(status
)));
97 } // namespace content