xref: /aosp_15_r20/external/cronet/net/cert/ct_serialization.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2013 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_CERT_CT_SERIALIZATION_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_CERT_CT_SERIALIZATION_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <string>
9*6777b538SAndroid Build Coastguard Worker #include <string_view>
10*6777b538SAndroid Build Coastguard Worker #include <vector>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include "base/memory/scoped_refptr.h"
13*6777b538SAndroid Build Coastguard Worker #include "base/time/time.h"
14*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker // Utility functions for encoding/decoding structures used by Certificate
17*6777b538SAndroid Build Coastguard Worker // Transparency to/from the TLS wire format encoding.
18*6777b538SAndroid Build Coastguard Worker namespace net::ct {
19*6777b538SAndroid Build Coastguard Worker 
20*6777b538SAndroid Build Coastguard Worker struct DigitallySigned;
21*6777b538SAndroid Build Coastguard Worker struct MerkleTreeLeaf;
22*6777b538SAndroid Build Coastguard Worker struct SignedCertificateTimestamp;
23*6777b538SAndroid Build Coastguard Worker struct SignedEntryData;
24*6777b538SAndroid Build Coastguard Worker struct SignedTreeHead;
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker // If |input.signature_data| is less than kMaxSignatureLength, encodes the
27*6777b538SAndroid Build Coastguard Worker // |input| to |output| and returns true. Otherwise, returns false.
28*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool EncodeDigitallySigned(const DigitallySigned& input,
29*6777b538SAndroid Build Coastguard Worker                                               std::string* output);
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker // Reads and decodes a DigitallySigned object from |input|.
32*6777b538SAndroid Build Coastguard Worker // The bytes read from |input| are discarded (i.e. |input|'s prefix removed)
33*6777b538SAndroid Build Coastguard Worker // Returns true and fills |output| if all fields can be read, false otherwise.
34*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool DecodeDigitallySigned(std::string_view* input,
35*6777b538SAndroid Build Coastguard Worker                                               DigitallySigned* output);
36*6777b538SAndroid Build Coastguard Worker 
37*6777b538SAndroid Build Coastguard Worker // Encodes the |input| SignedEntryData to |output|. Returns true if the entry
38*6777b538SAndroid Build Coastguard Worker // size does not exceed allowed size in RFC6962, false otherwise.
39*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool EncodeSignedEntry(const SignedEntryData& input,
40*6777b538SAndroid Build Coastguard Worker                                           std::string* output);
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker // Serialises the Merkle tree |leaf|, appending it to |output|.
43*6777b538SAndroid Build Coastguard Worker // These bytes can be hashed for use with audit proof fetching.
44*6777b538SAndroid Build Coastguard Worker // Note that |leaf.log_id| is not part of the TLS encoding, and so will not be
45*6777b538SAndroid Build Coastguard Worker // serialized.
46*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool EncodeTreeLeaf(const MerkleTreeLeaf& leaf, std::string* output);
47*6777b538SAndroid Build Coastguard Worker 
48*6777b538SAndroid Build Coastguard Worker // Encodes the data signed by a Signed Certificate Timestamp (SCT) into
49*6777b538SAndroid Build Coastguard Worker // |output|. The signature included in the SCT is then verified over these
50*6777b538SAndroid Build Coastguard Worker // bytes.
51*6777b538SAndroid Build Coastguard Worker // |timestamp| timestamp from the SCT.
52*6777b538SAndroid Build Coastguard Worker // |serialized_log_entry| the log entry signed by the SCT.
53*6777b538SAndroid Build Coastguard Worker // |extensions| CT extensions.
54*6777b538SAndroid Build Coastguard Worker // Returns true if the extensions' length does not exceed
55*6777b538SAndroid Build Coastguard Worker // kMaxExtensionsLength, false otherwise.
56*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool EncodeV1SCTSignedData(
57*6777b538SAndroid Build Coastguard Worker     const base::Time& timestamp,
58*6777b538SAndroid Build Coastguard Worker     const std::string& serialized_log_entry,
59*6777b538SAndroid Build Coastguard Worker     const std::string& extensions,
60*6777b538SAndroid Build Coastguard Worker     std::string* output);
61*6777b538SAndroid Build Coastguard Worker 
62*6777b538SAndroid Build Coastguard Worker // Encodes the data signed by a Signed Tree Head (STH) |signed_tree_head| into
63*6777b538SAndroid Build Coastguard Worker // |output|. The signature included in the |signed_tree_head| can then be
64*6777b538SAndroid Build Coastguard Worker // verified over these bytes.
65*6777b538SAndroid Build Coastguard Worker // Returns true if the data could be encoded successfully, false
66*6777b538SAndroid Build Coastguard Worker // otherwise.
67*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool EncodeTreeHeadSignature(
68*6777b538SAndroid Build Coastguard Worker     const SignedTreeHead& signed_tree_head,
69*6777b538SAndroid Build Coastguard Worker     std::string* output);
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker // Decode a list of Signed Certificate Timestamps
72*6777b538SAndroid Build Coastguard Worker // (SignedCertificateTimestampList as defined in RFC6962): from a single
73*6777b538SAndroid Build Coastguard Worker // string in |input| to a vector of individually-encoded SCTs |output|.
74*6777b538SAndroid Build Coastguard Worker // This list is typically obtained from the CT extension in a certificate.
75*6777b538SAndroid Build Coastguard Worker // Returns true if the list could be read and decoded successfully, false
76*6777b538SAndroid Build Coastguard Worker // otherwise (note that the validity of each individual SCT should be checked
77*6777b538SAndroid Build Coastguard Worker // separately).
78*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool DecodeSCTList(std::string_view input,
79*6777b538SAndroid Build Coastguard Worker                                       std::vector<std::string_view>* output);
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker // Decodes a single SCT from |input| to |output|.
82*6777b538SAndroid Build Coastguard Worker // Returns true if all fields in the SCT could be read and decoded, false
83*6777b538SAndroid Build Coastguard Worker // otherwise.
84*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool DecodeSignedCertificateTimestamp(
85*6777b538SAndroid Build Coastguard Worker     std::string_view* input,
86*6777b538SAndroid Build Coastguard Worker     scoped_refptr<ct::SignedCertificateTimestamp>* output);
87*6777b538SAndroid Build Coastguard Worker 
88*6777b538SAndroid Build Coastguard Worker // Serializes a Signed Certificate Timestamp (SCT) into |output|.
89*6777b538SAndroid Build Coastguard Worker // Returns true if the SCT could be encoded successfully, false
90*6777b538SAndroid Build Coastguard Worker // otherwise.
91*6777b538SAndroid Build Coastguard Worker NET_EXPORT bool EncodeSignedCertificateTimestamp(
92*6777b538SAndroid Build Coastguard Worker     const scoped_refptr<ct::SignedCertificateTimestamp>& input,
93*6777b538SAndroid Build Coastguard Worker     std::string* output);
94*6777b538SAndroid Build Coastguard Worker 
95*6777b538SAndroid Build Coastguard Worker // Writes an SCTList into |output|, containing |scts|.
96*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE bool EncodeSCTListForTesting(
97*6777b538SAndroid Build Coastguard Worker     const std::vector<std::string>& scts,
98*6777b538SAndroid Build Coastguard Worker     std::string* output);
99*6777b538SAndroid Build Coastguard Worker }  // namespace net::ct
100*6777b538SAndroid Build Coastguard Worker 
101*6777b538SAndroid Build Coastguard Worker #endif  // NET_CERT_CT_SERIALIZATION_H_
102