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& signed_tree_head, |
| 313 std::string* output) { |
| 314 WriteUint(kVersionLength, signed_tree_head.version, output); |
| 315 WriteUint(kSignatureTypeLength, TREE_HASH, output); |
| 316 WriteTimeSinceEpoch(signed_tree_head.timestamp, output); |
| 317 WriteUint(kTreeSizeLength, signed_tree_head.tree_size, output); |
| 318 WriteEncodedBytes( |
| 319 base::StringPiece(signed_tree_head.sha256_root_hash, kSthRootHashLength), |
| 320 output); |
| 321 } |
| 322 |
305 bool DecodeSCTList(base::StringPiece* input, | 323 bool DecodeSCTList(base::StringPiece* input, |
306 std::vector<base::StringPiece>* output) { | 324 std::vector<base::StringPiece>* output) { |
307 std::vector<base::StringPiece> result; | 325 std::vector<base::StringPiece> result; |
308 if (!ReadList(kSCTListLengthBytes, kSerializedSCTLengthBytes, | 326 if (!ReadList(kSCTListLengthBytes, kSerializedSCTLengthBytes, |
309 input, &result)) { | 327 input, &result)) { |
310 return false; | 328 return false; |
311 } | 329 } |
312 | 330 |
313 if (!input->empty() || result.empty()) | 331 if (!input->empty() || result.empty()) |
314 return false; | 332 return false; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 bool EncodeSCTListForTesting(const base::StringPiece& sct, | 377 bool EncodeSCTListForTesting(const base::StringPiece& sct, |
360 std::string* output) { | 378 std::string* output) { |
361 std::string encoded_sct; | 379 std::string encoded_sct; |
362 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && | 380 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && |
363 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); | 381 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); |
364 } | 382 } |
365 | 383 |
366 } // namespace ct | 384 } // namespace ct |
367 | 385 |
368 } // namespace net | 386 } // namespace net |
OLD | NEW |