| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/cert/ct_serialization.h" | 5 #include "net/cert/ct_serialization.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 // Members of the digitally-signed struct of a V1 SCT | 28 // Members of the digitally-signed struct of a V1 SCT |
| 29 const size_t kSignatureTypeLength = 1; | 29 const size_t kSignatureTypeLength = 1; |
| 30 const size_t kLogEntryTypeLength = 2; | 30 const size_t kLogEntryTypeLength = 2; |
| 31 const size_t kAsn1CertificateLengthBytes = 3; | 31 const size_t kAsn1CertificateLengthBytes = 3; |
| 32 const size_t kTbsCertificateLengthBytes = 3; | 32 const size_t kTbsCertificateLengthBytes = 3; |
| 33 | 33 |
| 34 const size_t kSCTListLengthBytes = 2; | 34 const size_t kSCTListLengthBytes = 2; |
| 35 const size_t kSerializedSCTLengthBytes = 2; | 35 const size_t kSerializedSCTLengthBytes = 2; |
| 36 | 36 |
| 37 // Members of digitally-signed struct of a STH |
| 38 const size_t kTreeSizeLength = 8; |
| 39 |
| 37 enum SignatureType { | 40 enum SignatureType { |
| 38 SIGNATURE_TYPE_CERTIFICATE_TIMESTAMP = 0, | 41 SIGNATURE_TYPE_CERTIFICATE_TIMESTAMP = 0, |
| 39 TREE_HASH = 1, | 42 TREE_HASH = 1, |
| 40 }; | 43 }; |
| 41 | 44 |
| 42 // Reads a TLS-encoded variable length unsigned integer from |in|. | 45 // Reads a TLS-encoded variable length unsigned integer from |in|. |
| 43 // The integer is expected to be in big-endian order, which is used by TLS. | 46 // The integer is expected to be in big-endian order, which is used by TLS. |
| 44 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 47 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 45 // |length| indicates the size (in bytes) of the integer. On success, returns | 48 // |length| indicates the size (in bytes) of the integer. On success, returns |
| 46 // true and stores the result in |*out|. | 49 // true and stores the result in |*out|. |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 WriteUint(kLogEntryTypeLength, input.type, output); | 281 WriteUint(kLogEntryTypeLength, input.type, output); |
| 279 switch (input.type) { | 282 switch (input.type) { |
| 280 case LogEntry::LOG_ENTRY_TYPE_X509: | 283 case LogEntry::LOG_ENTRY_TYPE_X509: |
| 281 return EncodeAsn1CertLogEntry(input, output); | 284 return EncodeAsn1CertLogEntry(input, output); |
| 282 case LogEntry::LOG_ENTRY_TYPE_PRECERT: | 285 case LogEntry::LOG_ENTRY_TYPE_PRECERT: |
| 283 return EncodePrecertLogEntry(input, output); | 286 return EncodePrecertLogEntry(input, output); |
| 284 } | 287 } |
| 285 return false; | 288 return false; |
| 286 } | 289 } |
| 287 | 290 |
| 291 static void WriteTimeSinceEpoch(const base::Time& timestamp, |
| 292 std::string* output) { |
| 293 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); |
| 294 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); |
| 295 } |
| 296 |
| 288 bool EncodeV1SCTSignedData(const base::Time& timestamp, | 297 bool EncodeV1SCTSignedData(const base::Time& timestamp, |
| 289 const std::string& serialized_log_entry, | 298 const std::string& serialized_log_entry, |
| 290 const std::string& extensions, | 299 const std::string& extensions, |
| 291 std::string* output) { | 300 std::string* output) { |
| 292 WriteUint(kVersionLength, SignedCertificateTimestamp::SCT_VERSION_1, | 301 WriteUint(kVersionLength, SignedCertificateTimestamp::SCT_VERSION_1, |
| 293 output); | 302 output); |
| 294 WriteUint(kSignatureTypeLength, SIGNATURE_TYPE_CERTIFICATE_TIMESTAMP, | 303 WriteUint(kSignatureTypeLength, SIGNATURE_TYPE_CERTIFICATE_TIMESTAMP, |
| 295 output); | 304 output); |
| 296 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); | 305 WriteTimeSinceEpoch(timestamp, output); |
| 297 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), | |
| 298 output); | |
| 299 // NOTE: serialized_log_entry must already be serialized and contain the | 306 // NOTE: serialized_log_entry must already be serialized and contain the |
| 300 // length as the prefix. | 307 // length as the prefix. |
| 301 WriteEncodedBytes(serialized_log_entry, output); | 308 WriteEncodedBytes(serialized_log_entry, output); |
| 302 return WriteVariableBytes(kExtensionsLengthBytes, extensions, output); | 309 return WriteVariableBytes(kExtensionsLengthBytes, extensions, output); |
| 303 } | 310 } |
| 304 | 311 |
| 312 void EncodeTreeHeadSignature(const SignedTreeHead& sth, std::string* output) { |
| 313 WriteUint(kVersionLength, sth.version, output); |
| 314 WriteUint(kSignatureTypeLength, TREE_HASH, output); |
| 315 WriteTimeSinceEpoch(sth.timestamp, output); |
| 316 WriteUint(kTreeSizeLength, sth.tree_size, output); |
| 317 WriteEncodedBytes(base::StringPiece(sth.sha256_root_hash, kSthRootHashLength), |
| 318 output); |
| 319 } |
| 320 |
| 305 bool DecodeSCTList(base::StringPiece* input, | 321 bool DecodeSCTList(base::StringPiece* input, |
| 306 std::vector<base::StringPiece>* output) { | 322 std::vector<base::StringPiece>* output) { |
| 307 std::vector<base::StringPiece> result; | 323 std::vector<base::StringPiece> result; |
| 308 if (!ReadList(kSCTListLengthBytes, kSerializedSCTLengthBytes, | 324 if (!ReadList(kSCTListLengthBytes, kSerializedSCTLengthBytes, |
| 309 input, &result)) { | 325 input, &result)) { |
| 310 return false; | 326 return false; |
| 311 } | 327 } |
| 312 | 328 |
| 313 if (!input->empty() || result.empty()) | 329 if (!input->empty() || result.empty()) |
| 314 return false; | 330 return false; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 bool EncodeSCTListForTesting(const base::StringPiece& sct, | 375 bool EncodeSCTListForTesting(const base::StringPiece& sct, |
| 360 std::string* output) { | 376 std::string* output) { |
| 361 std::string encoded_sct; | 377 std::string encoded_sct; |
| 362 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && | 378 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && |
| 363 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); | 379 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); |
| 364 } | 380 } |
| 365 | 381 |
| 366 } // namespace ct | 382 } // namespace ct |
| 367 | 383 |
| 368 } // namespace net | 384 } // namespace net |
| OLD | NEW |