| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace ct { | 21 namespace ct { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 // Note: length is always specified in bytes. | 25 // Note: length is always specified in bytes. |
| 26 // CT protocol version length | 26 // CT protocol version length |
| 27 const size_t kVersionLength = 1; | 27 const size_t kVersionLength = 1; |
| 28 | 28 |
| 29 // Common V1 struct members | 29 // Common V1 struct members |
| 30 const size_t kTimestampLength = 8; | 30 const size_t kTimestampLength = 8; |
| 31 const size_t kLogEntryTypeLength = 2; | 31 const size_t kSignedEntryTypeLength = 2; |
| 32 const size_t kAsn1CertificateLengthBytes = 3; | 32 const size_t kAsn1CertificateLengthBytes = 3; |
| 33 const size_t kTbsCertificateLengthBytes = 3; | 33 const size_t kTbsCertificateLengthBytes = 3; |
| 34 const size_t kExtensionsLengthBytes = 2; | 34 const size_t kExtensionsLengthBytes = 2; |
| 35 | 35 |
| 36 // Members of a V1 SCT | 36 // Members of a V1 SCT |
| 37 const size_t kLogIdLength = crypto::kSHA256Length; | 37 const size_t kLogIdLength = crypto::kSHA256Length; |
| 38 const size_t kHashAlgorithmLength = 1; | 38 const size_t kHashAlgorithmLength = 1; |
| 39 const size_t kSigAlgorithmLength = 1; | 39 const size_t kSigAlgorithmLength = 1; |
| 40 const size_t kSignatureLengthBytes = 2; | 40 const size_t kSignatureLengthBytes = 2; |
| 41 | 41 |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 240 |
| 241 if (input_size > max_input_size) | 241 if (input_size > max_input_size) |
| 242 return false; | 242 return false; |
| 243 | 243 |
| 244 WriteUint(prefix_length, input_size, output); | 244 WriteUint(prefix_length, input_size, output); |
| 245 WriteEncodedBytes(input, output); | 245 WriteEncodedBytes(input, output); |
| 246 | 246 |
| 247 return true; | 247 return true; |
| 248 } | 248 } |
| 249 | 249 |
| 250 // Writes a LogEntry of type X.509 cert to |output|. | 250 // Writes a SignedEntryData of type X.509 cert to |output|. |
| 251 // |input| is the LogEntry containing the certificate. | 251 // |input| is the SignedEntryData containing the certificate. |
| 252 // Returns true if the leaf_certificate in the LogEntry does not exceed | 252 // Returns true if the leaf_certificate in the SignedEntryData does not exceed |
| 253 // kMaxAsn1CertificateLength and so can be written to |output|. | 253 // kMaxAsn1CertificateLength and so can be written to |output|. |
| 254 bool EncodeAsn1CertLogEntry(const LogEntry& input, std::string* output) { | 254 bool EncodeAsn1CertSignedEntry(const SignedEntryData& input, |
| 255 std::string* output) { |
| 255 return WriteVariableBytes(kAsn1CertificateLengthBytes, | 256 return WriteVariableBytes(kAsn1CertificateLengthBytes, |
| 256 input.leaf_certificate, output); | 257 input.leaf_certificate, output); |
| 257 } | 258 } |
| 258 | 259 |
| 259 // Writes a LogEntry of type PreCertificate to |output|. | 260 // Writes a SignedEntryData of type PreCertificate to |output|. |
| 260 // |input| is the LogEntry containing the TBSCertificate and issuer key hash. | 261 // |input| is the SignedEntryData containing the TBSCertificate and issuer key |
| 261 // Returns true if the TBSCertificate component in the LogEntry does not | 262 // hash. Returns true if the TBSCertificate component in the SignedEntryData |
| 262 // exceed kMaxTbsCertificateLength and so can be written to |output|. | 263 // does not exceed kMaxTbsCertificateLength and so can be written to |output|. |
| 263 bool EncodePrecertLogEntry(const LogEntry& input, std::string* output) { | 264 bool EncodePrecertSignedEntry(const SignedEntryData& input, |
| 265 std::string* output) { |
| 264 WriteEncodedBytes( | 266 WriteEncodedBytes( |
| 265 base::StringPiece( | 267 base::StringPiece( |
| 266 reinterpret_cast<const char*>(input.issuer_key_hash.data), | 268 reinterpret_cast<const char*>(input.issuer_key_hash.data), |
| 267 kIssuerKeyHashLength), | 269 kIssuerKeyHashLength), |
| 268 output); | 270 output); |
| 269 return WriteVariableBytes(kTbsCertificateLengthBytes, | 271 return WriteVariableBytes(kTbsCertificateLengthBytes, |
| 270 input.tbs_certificate, output); | 272 input.tbs_certificate, output); |
| 271 } | 273 } |
| 272 | 274 |
| 273 } // namespace | 275 } // namespace |
| (...skipping 27 matching lines...) Expand all Loading... |
| 301 if (!ConvertSignatureAlgorithm(sig_algo, &result.signature_algorithm)) { | 303 if (!ConvertSignatureAlgorithm(sig_algo, &result.signature_algorithm)) { |
| 302 DVLOG(1) << "Invalid signature algorithm " << sig_algo; | 304 DVLOG(1) << "Invalid signature algorithm " << sig_algo; |
| 303 return false; | 305 return false; |
| 304 } | 306 } |
| 305 sig_data.CopyToString(&result.signature_data); | 307 sig_data.CopyToString(&result.signature_data); |
| 306 | 308 |
| 307 *output = result; | 309 *output = result; |
| 308 return true; | 310 return true; |
| 309 } | 311 } |
| 310 | 312 |
| 311 bool EncodeLogEntry(const LogEntry& input, std::string* output) { | 313 bool EncodeSignedEntry(const SignedEntryData& input, std::string* output) { |
| 312 WriteUint(kLogEntryTypeLength, input.type, output); | 314 WriteUint(kSignedEntryTypeLength, input.type, output); |
| 313 switch (input.type) { | 315 switch (input.type) { |
| 314 case LogEntry::LOG_ENTRY_TYPE_X509: | 316 case SignedEntryData::LOG_ENTRY_TYPE_X509: |
| 315 return EncodeAsn1CertLogEntry(input, output); | 317 return EncodeAsn1CertSignedEntry(input, output); |
| 316 case LogEntry::LOG_ENTRY_TYPE_PRECERT: | 318 case SignedEntryData::LOG_ENTRY_TYPE_PRECERT: |
| 317 return EncodePrecertLogEntry(input, output); | 319 return EncodePrecertSignedEntry(input, output); |
| 318 } | 320 } |
| 319 return false; | 321 return false; |
| 320 } | 322 } |
| 321 | 323 |
| 322 static bool ReadTimeSinceEpoch(base::StringPiece* input, base::Time* output) { | 324 static bool ReadTimeSinceEpoch(base::StringPiece* input, base::Time* output) { |
| 323 uint64_t time_since_epoch = 0; | 325 uint64_t time_since_epoch = 0; |
| 324 if (!ReadUint(kTimestampLength, input, &time_since_epoch)) | 326 if (!ReadUint(kTimestampLength, input, &time_since_epoch)) |
| 325 return false; | 327 return false; |
| 326 | 328 |
| 327 base::CheckedNumeric<int64_t> time_since_epoch_signed = time_since_epoch; | 329 base::CheckedNumeric<int64_t> time_since_epoch_signed = time_since_epoch; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 342 static void WriteTimeSinceEpoch(const base::Time& timestamp, | 344 static void WriteTimeSinceEpoch(const base::Time& timestamp, |
| 343 std::string* output) { | 345 std::string* output) { |
| 344 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); | 346 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); |
| 345 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); | 347 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); |
| 346 } | 348 } |
| 347 | 349 |
| 348 bool EncodeTreeLeaf(const MerkleTreeLeaf& leaf, std::string* output) { | 350 bool EncodeTreeLeaf(const MerkleTreeLeaf& leaf, std::string* output) { |
| 349 WriteUint(kVersionLength, 0, output); // version: 1 | 351 WriteUint(kVersionLength, 0, output); // version: 1 |
| 350 WriteUint(kMerkleLeafTypeLength, 0, output); // type: timestamped entry | 352 WriteUint(kMerkleLeafTypeLength, 0, output); // type: timestamped entry |
| 351 WriteTimeSinceEpoch(leaf.timestamp, output); | 353 WriteTimeSinceEpoch(leaf.timestamp, output); |
| 352 if (!EncodeLogEntry(leaf.log_entry, output)) | 354 if (!EncodeSignedEntry(leaf.signed_entry, output)) |
| 353 return false; | 355 return false; |
| 354 if (!WriteVariableBytes(kExtensionsLengthBytes, leaf.extensions, output)) | 356 if (!WriteVariableBytes(kExtensionsLengthBytes, leaf.extensions, output)) |
| 355 return false; | 357 return false; |
| 356 | 358 |
| 357 return true; | 359 return true; |
| 358 } | 360 } |
| 359 | 361 |
| 360 bool EncodeV1SCTSignedData(const base::Time& timestamp, | 362 bool EncodeV1SCTSignedData(const base::Time& timestamp, |
| 361 const std::string& serialized_log_entry, | 363 const std::string& serialized_log_entry, |
| 362 const std::string& extensions, | 364 const std::string& extensions, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 bool EncodeSCTListForTesting(const base::StringPiece& sct, | 431 bool EncodeSCTListForTesting(const base::StringPiece& sct, |
| 430 std::string* output) { | 432 std::string* output) { |
| 431 std::string encoded_sct; | 433 std::string encoded_sct; |
| 432 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && | 434 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && |
| 433 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); | 435 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); |
| 434 } | 436 } |
| 435 | 437 |
| 436 } // namespace ct | 438 } // namespace ct |
| 437 | 439 |
| 438 } // namespace net | 440 } // namespace net |
| OLD | NEW |