10f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 20f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 30f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// found in the LICENSE file. 40f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 50f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#ifndef NET_CERT_CT_SERIALIZATION_H_ 60f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#define NET_CERT_CT_SERIALIZATION_H_ 70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 80f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include <string> 90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include <vector> 100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "base/strings/string_piece.h" 120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "net/base/net_export.h" 130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "net/cert/signed_certificate_timestamp.h" 14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/cert/signed_tree_head.h" 150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace net { 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Utility functions for encoding/decoding structures used by Certificate 190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Transparency to/from the TLS wire format encoding. 200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace ct { 210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// If |input.signature_data| is less than kMaxSignatureLength, encodes the 230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |input| to |output| and returns true. Otherwise, returns false. 240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeDigitallySigned(const DigitallySigned& input, 250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Reads and decodes a DigitallySigned object from |input|. 280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// The bytes read from |input| are discarded (i.e. |input|'s prefix removed) 290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true and fills |output| if all fields can be read, false otherwise. 300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeDigitallySigned(base::StringPiece* input, 310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DigitallySigned* output); 320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Encodes the |input| LogEntry to |output|. Returns true if the entry size 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// does not exceed allowed size in RFC6962, false otherwise. 350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeLogEntry(const LogEntry& input, 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Encodes the data signed by a Signed Certificate Timestamp (SCT) into 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |output|. The signature included in the SCT is then verified over these 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// bytes. 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |timestamp| timestamp from the SCT. 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |serialized_log_entry| the log entry signed by the SCT. 430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |extensions| CT extensions. 440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if the extensions' length does not exceed 450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// kMaxExtensionsLength, false otherwise. 460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeV1SCTSignedData( 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const base::Time& timestamp, 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const std::string& serialized_log_entry, 490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const std::string& extensions, 500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Encodes the data signed by a Signed Tree Head (STH) |signed_tree_head| into 53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// |output|. The signature included in the |signed_tree_head| can then be 54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// verified over these bytes. 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)NET_EXPORT_PRIVATE void EncodeTreeHeadSignature( 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const SignedTreeHead& signed_tree_head, 57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::string* output); 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Decode a list of Signed Certificate Timestamps 600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// (SignedCertificateTimestampList as defined in RFC6962): from a single 610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// string in |input| to a vector of individually-encoded SCTs |output|. 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// This list is typically obtained from the CT extension in a certificate. 630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if the list could be read and decoded successfully, false 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// otherwise (note that the validity of each individual SCT should be checked 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// separately). 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeSCTList(base::StringPiece* input, 670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::vector<base::StringPiece>* output); 680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Decodes a single SCT from |input| to |output|. 700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if all fields in the SCT could be read and decoded, false 710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// otherwise. 720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeSignedCertificateTimestamp( 730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::StringPiece* input, 74f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) scoped_refptr<ct::SignedCertificateTimestamp>* output); 750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 76f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Writes an SCTList into |output|, containing a single |sct|. 77f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeSCTListForTesting(const base::StringPiece& sct, 78f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) std::string* output); 790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace ct 800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 810f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace net 820f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 830f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif // NET_CERT_CT_SERIALIZATION_H_ 84