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