ct_serialization.h revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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" 140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace net { 160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Utility functions for encoding/decoding structures used by Certificate 180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Transparency to/from the TLS wire format encoding. 190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace ct { 200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// If |input.signature_data| is less than kMaxSignatureLength, encodes the 220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |input| to |output| and returns true. Otherwise, returns false. 230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeDigitallySigned(const DigitallySigned& input, 240f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Reads and decodes a DigitallySigned object from |input|. 270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// The bytes read from |input| are discarded (i.e. |input|'s prefix removed) 280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true and fills |output| if all fields can be read, false otherwise. 290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeDigitallySigned(base::StringPiece* input, 300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DigitallySigned* output); 310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Encodes the |input| LogEntry to |output|. Returns true if the entry size 330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// does not exceed allowed size in RFC6962, false otherwise. 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeLogEntry(const LogEntry& input, 350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Encodes the data signed by a Signed Certificate Timestamp (SCT) into 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |output|. The signature included in the SCT is then verified over these 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// bytes. 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |timestamp| timestamp from the SCT. 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |serialized_log_entry| the log entry signed by the SCT. 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// |extensions| CT extensions. 430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if the extensions' length does not exceed 440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// kMaxExtensionsLength, false otherwise. 450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool EncodeV1SCTSignedData( 460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const base::Time& timestamp, 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const std::string& serialized_log_entry, 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const std::string& extensions, 490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::string* output); 500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Decode a list of Signed Certificate Timestamps 520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// (SignedCertificateTimestampList as defined in RFC6962): from a single 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// string in |input| to a vector of individually-encoded SCTs |output|. 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// This list is typically obtained from the CT extension in a certificate. 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if the list could be read and decoded successfully, false 560f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// otherwise (note that the validity of each individual SCT should be checked 570f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// separately). 580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeSCTList(base::StringPiece* input, 590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) std::vector<base::StringPiece>* output); 600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Decodes a single SCT from |input| to |output|. 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Returns true if all fields in the SCT could be read and decoded, false 630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// otherwise. 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)NET_EXPORT_PRIVATE bool DecodeSignedCertificateTimestamp( 650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) base::StringPiece* input, 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) ct::SignedCertificateTimestamp* output); 670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace ct 690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace net 710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif // NET_CERT_CT_SERIALIZATION_H_ 73