Chromium Code Reviews| 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 <cstdint> |
|
Ryan Sleevi
2016/01/08 19:46:31
Chromium code recommends stdint.h (and, in general
Rob Percival
2016/01/10 03:48:56
No, I just wasn't aware of that recommendation - I
| |
| 8 | |
| 9 #include <limits> | 8 #include <limits> |
| 10 | 9 |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 | 11 |
| 13 namespace net { | 12 namespace net { |
| 14 | 13 |
| 15 namespace ct { | 14 namespace ct { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 T result = 0; | 58 T result = 0; |
| 60 for (size_t i = 0; i < length; ++i) { | 59 for (size_t i = 0; i < length; ++i) { |
| 61 result = (result << 8) | static_cast<unsigned char>((*in)[i]); | 60 result = (result << 8) | static_cast<unsigned char>((*in)[i]); |
| 62 } | 61 } |
| 63 in->remove_prefix(length); | 62 in->remove_prefix(length); |
| 64 *out = result; | 63 *out = result; |
| 65 return true; | 64 return true; |
| 66 } | 65 } |
| 67 | 66 |
| 68 // Reads a TLS-encoded field length from |in|. | 67 // Reads a TLS-encoded field length from |in|. |
| 69 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 68 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed). |
| 70 // |prefix_length| indicates the bytes needed to represent the length (e.g. 3) | 69 // |prefix_length| indicates the bytes needed to represent the length (e.g. 3). |
| 70 // Max |prefix_length| is 8. | |
| 71 // success, returns true and stores the result in |*out|. | 71 // success, returns true and stores the result in |*out|. |
| 72 bool ReadLength(size_t prefix_length, base::StringPiece* in, size_t* out) { | 72 bool ReadLength(size_t prefix_length, base::StringPiece* in, size_t* out) { |
| 73 size_t length; | 73 uint64_t length = 0; |
| 74 if (!ReadUint(prefix_length, in, &length)) | 74 if (!ReadUint(prefix_length, in, &length)) |
| 75 return false; | 75 return false; |
| 76 *out = length; | 76 if (length > std::numeric_limits<size_t>::max()) { |
| 77 return false; | |
| 78 } | |
|
Ryan Sleevi
2016/01/08 19:46:31
CONSISTENCY: No braces for one-line conditionals
Rob Percival
2016/01/10 03:48:56
Done.
| |
| 79 *out = static_cast<size_t>(length); | |
| 77 return true; | 80 return true; |
| 78 } | 81 } |
| 79 | 82 |
| 80 // Reads |length| bytes from |*in|. If |*in| is too small, returns false. | 83 // Reads |length| bytes from |*in|. If |*in| is too small, returns false. |
| 81 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 84 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 82 bool ReadFixedBytes(size_t length, | 85 bool ReadFixedBytes(size_t length, |
| 83 base::StringPiece* in, | 86 base::StringPiece* in, |
| 84 base::StringPiece* out) { | 87 base::StringPiece* out) { |
| 85 if (in->length() < length) | 88 if (in->length() < length) |
| 86 return false; | 89 return false; |
| 87 out->set(in->data(), length); | 90 out->set(in->data(), length); |
| 88 in->remove_prefix(length); | 91 in->remove_prefix(length); |
| 89 return true; | 92 return true; |
| 90 } | 93 } |
| 91 | 94 |
| 92 // Reads a length-prefixed variable amount of bytes from |in|, updating |out| | 95 // Reads a length-prefixed variable amount of bytes from |in|, updating |out| |
| 93 // on success. |prefix_length| indicates the number of bytes needed to represent | 96 // on success. |prefix_length| indicates the number of bytes needed to represent |
| 94 // the length. | 97 // the length. |
| 95 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 98 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 96 bool ReadVariableBytes(size_t prefix_length, | 99 bool ReadVariableBytes(size_t prefix_length, |
| 97 base::StringPiece* in, | 100 base::StringPiece* in, |
| 98 base::StringPiece* out) { | 101 base::StringPiece* out) { |
| 99 size_t length; | 102 size_t length = 0; |
|
Ryan Sleevi
2016/01/08 19:46:31
Why? (I mean, on a mere pedantry that adds an extr
Rob Percival
2016/01/10 03:48:56
It's a little more defensive against bugs than tru
| |
| 100 if (!ReadLength(prefix_length, in, &length)) | 103 if (!ReadLength(prefix_length, in, &length)) |
| 101 return false; | 104 return false; |
| 102 return ReadFixedBytes(length, in, out); | 105 return ReadFixedBytes(length, in, out); |
| 103 } | 106 } |
| 104 | 107 |
| 105 // Reads a variable-length list that has been TLS encoded. | 108 // Reads a variable-length list that has been TLS encoded. |
| 106 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 109 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 107 // |max_list_length| contains the overall length of the encoded list. | 110 // |max_list_length| contains the overall length of the encoded list. |
| 108 // |max_item_length| contains the maximum length of a single item. | 111 // |max_item_length| contains the maximum length of a single item. |
| 109 // On success, returns true and updates |*out| with the encoded list. | 112 // On success, returns true and updates |*out| with the encoded list. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 | 180 |
| 178 // Writes a TLS-encoded variable length unsigned integer to |output|. | 181 // Writes a TLS-encoded variable length unsigned integer to |output|. |
| 179 // |length| indicates the size (in bytes) of the integer. | 182 // |length| indicates the size (in bytes) of the integer. |
| 180 // |value| the value itself to be written. | 183 // |value| the value itself to be written. |
| 181 template <typename T> | 184 template <typename T> |
| 182 void WriteUint(size_t length, T value, std::string* output) { | 185 void WriteUint(size_t length, T value, std::string* output) { |
| 183 DCHECK_LE(length, sizeof(T)); | 186 DCHECK_LE(length, sizeof(T)); |
| 184 DCHECK(length == sizeof(T) || value >> (length * 8) == 0); | 187 DCHECK(length == sizeof(T) || value >> (length * 8) == 0); |
| 185 | 188 |
| 186 for (; length > 0; --length) { | 189 for (; length > 0; --length) { |
| 187 output->push_back((value >> ((length - 1)* 8)) & 0xFF); | 190 output->push_back((value >> ((length - 1) * 8)) & 0xFF); |
| 188 } | 191 } |
| 189 } | 192 } |
| 190 | 193 |
| 191 // Writes an array to |output| from |input|. | 194 // Writes an array to |output| from |input|. |
| 192 // Should be used in one of two cases: | 195 // Should be used in one of two cases: |
| 193 // * The length of |input| has already been encoded into the |output| stream. | 196 // * The length of |input| has already been encoded into the |output| stream. |
| 194 // * The length of |input| is fixed and the reader is expected to specify that | 197 // * The length of |input| is fixed and the reader is expected to specify that |
| 195 // length when reading. | 198 // length when reading. |
| 196 // If the length of |input| is dynamic and data is expected to follow it, | 199 // If the length of |input| is dynamic and data is expected to follow it, |
| 197 // WriteVariableBytes must be used. | 200 // WriteVariableBytes must be used. |
| 198 void WriteEncodedBytes(const base::StringPiece& input, std::string* output) { | 201 // Returns the number of bytes written (the length of |input|). |
| 202 size_t WriteEncodedBytes(const base::StringPiece& input, std::string* output) { | |
| 199 input.AppendToString(output); | 203 input.AppendToString(output); |
| 204 return input.size(); | |
| 200 } | 205 } |
| 201 | 206 |
| 202 // Writes a variable-length array to |output|. | 207 // Writes a variable-length array to |output|. |
| 203 // |prefix_length| indicates the number of bytes needed to represnt the length. | 208 // |prefix_length| indicates the number of bytes needed to represnt the length. |
| 204 // |input| is the array itself. | 209 // |input| is the array itself. |
| 205 // If the size of |input| is less than 2^|prefix_length| - 1, encode the | 210 // If 1 <= |prefix_length| <= 8 and the size of |input| is less than |
| 206 // length and data and return true. Otherwise, return false. | 211 // 2^|prefix_length| - 1, encode the length and data and return true. |
| 212 // Otherwise, return false. | |
| 207 bool WriteVariableBytes(size_t prefix_length, | 213 bool WriteVariableBytes(size_t prefix_length, |
| 208 const base::StringPiece& input, | 214 const base::StringPiece& input, |
| 209 std::string* output) { | 215 std::string* output) { |
| 210 size_t input_size = input.size(); | 216 // Use uint64_t instead of size_t to allow a |prefix_length| up to 8 bytes, |
| 211 size_t max_allowed_input_size = | 217 // even on 32-bit builds. |
|
Ryan Sleevi
2016/01/08 19:46:31
Why? input.size() will never exceed 4 bytes on 32-
Rob Percival
2016/01/10 03:48:56
The catch here is that WriteUint (below) is a temp
Ryan Sleevi
2016/01/12 00:10:15
Isn't this moreso a bug in line 186/187 having bad
Rob Percival
2016/01/13 00:55:52
How about we replace the preconditions of WriteUin
| |
| 212 static_cast<size_t>(((1 << (prefix_length * 8)) - 1)); | 218 uint64_t input_size = input.size(); |
| 219 if (prefix_length > sizeof(input_size)) { | |
| 220 return false; | |
| 221 } | |
|
Ryan Sleevi
2016/01/08 19:46:31
Consistency: Braces
Rob Percival
2016/01/10 03:48:56
Done.
| |
| 222 | |
| 223 uint64_t max_allowed_input_size = ((1 << (prefix_length * 8)) - 1); | |
| 213 if (input_size > max_allowed_input_size) | 224 if (input_size > max_allowed_input_size) |
| 214 return false; | 225 return false; |
| 215 | 226 |
| 216 WriteUint(prefix_length, input.size(), output); | 227 WriteUint(prefix_length, input_size, output); |
| 217 WriteEncodedBytes(input, output); | 228 WriteEncodedBytes(input, output); |
| 218 | 229 |
| 219 return true; | 230 return true; |
| 220 } | 231 } |
| 221 | 232 |
| 222 // Writes a LogEntry of type X.509 cert to |output|. | 233 // Writes a LogEntry of type X.509 cert to |output|. |
| 223 // |input| is the LogEntry containing the certificate. | 234 // |input| is the LogEntry containing the certificate. |
| 224 // Returns true if the leaf_certificate in the LogEntry does not exceed | 235 // Returns true if the leaf_certificate in the LogEntry does not exceed |
| 225 // kMaxAsn1CertificateLength and so can be written to |output|. | 236 // kMaxAsn1CertificateLength and so can be written to |output|. |
| 226 bool EncodeAsn1CertLogEntry(const LogEntry& input, std::string* output) { | 237 bool EncodeAsn1CertLogEntry(const LogEntry& input, std::string* output) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 WriteUint(kLogEntryTypeLength, input.type, output); | 295 WriteUint(kLogEntryTypeLength, input.type, output); |
| 285 switch (input.type) { | 296 switch (input.type) { |
| 286 case LogEntry::LOG_ENTRY_TYPE_X509: | 297 case LogEntry::LOG_ENTRY_TYPE_X509: |
| 287 return EncodeAsn1CertLogEntry(input, output); | 298 return EncodeAsn1CertLogEntry(input, output); |
| 288 case LogEntry::LOG_ENTRY_TYPE_PRECERT: | 299 case LogEntry::LOG_ENTRY_TYPE_PRECERT: |
| 289 return EncodePrecertLogEntry(input, output); | 300 return EncodePrecertLogEntry(input, output); |
| 290 } | 301 } |
| 291 return false; | 302 return false; |
| 292 } | 303 } |
| 293 | 304 |
| 305 static bool ReadTimeSinceEpoch(base::StringPiece* input, base::Time* output) { | |
| 306 uint64_t time_since_epoch = 0; | |
| 307 if (!ReadUint(kTimestampLength, input, &time_since_epoch)) { | |
| 308 return false; | |
| 309 } | |
| 310 | |
| 311 if (time_since_epoch > | |
| 312 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | |
|
Ryan Sleevi
2016/01/08 19:46:31
There's stuff in base/safe_numerics to help with t
Rob Percival
2016/01/10 03:48:56
Done.
| |
| 313 DVLOG(1) << "Timestamp value too big to cast to int64_t: " | |
| 314 << time_since_epoch; | |
| 315 return false; | |
| 316 } | |
| 317 | |
| 318 *output = | |
| 319 base::Time::UnixEpoch() + | |
| 320 base::TimeDelta::FromMilliseconds(static_cast<int64_t>(time_since_epoch)); | |
| 321 | |
| 322 return true; | |
| 323 } | |
| 324 | |
| 294 static void WriteTimeSinceEpoch(const base::Time& timestamp, | 325 static void WriteTimeSinceEpoch(const base::Time& timestamp, |
| 295 std::string* output) { | 326 std::string* output) { |
| 296 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); | 327 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); |
| 297 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); | 328 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); |
| 298 } | 329 } |
| 299 | 330 |
| 300 bool EncodeV1SCTSignedData(const base::Time& timestamp, | 331 bool EncodeV1SCTSignedData(const base::Time& timestamp, |
| 301 const std::string& serialized_log_entry, | 332 const std::string& serialized_log_entry, |
| 302 const std::string& extensions, | 333 const std::string& extensions, |
| 303 std::string* output) { | 334 std::string* output) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 344 new SignedCertificateTimestamp()); | 375 new SignedCertificateTimestamp()); |
| 345 unsigned version; | 376 unsigned version; |
| 346 if (!ReadUint(kVersionLength, input, &version)) | 377 if (!ReadUint(kVersionLength, input, &version)) |
| 347 return false; | 378 return false; |
| 348 if (version != SignedCertificateTimestamp::SCT_VERSION_1) { | 379 if (version != SignedCertificateTimestamp::SCT_VERSION_1) { |
| 349 DVLOG(1) << "Unsupported/invalid version " << version; | 380 DVLOG(1) << "Unsupported/invalid version " << version; |
| 350 return false; | 381 return false; |
| 351 } | 382 } |
| 352 | 383 |
| 353 result->version = SignedCertificateTimestamp::SCT_VERSION_1; | 384 result->version = SignedCertificateTimestamp::SCT_VERSION_1; |
| 354 uint64_t timestamp; | |
| 355 base::StringPiece log_id; | 385 base::StringPiece log_id; |
| 356 base::StringPiece extensions; | 386 base::StringPiece extensions; |
| 357 if (!ReadFixedBytes(kLogIdLength, input, &log_id) || | 387 if (!ReadFixedBytes(kLogIdLength, input, &log_id) || |
| 358 !ReadUint(kTimestampLength, input, ×tamp) || | 388 !ReadTimeSinceEpoch(input, &result->timestamp) || |
| 359 !ReadVariableBytes(kExtensionsLengthBytes, input, | 389 !ReadVariableBytes(kExtensionsLengthBytes, input, &extensions) || |
| 360 &extensions) || | |
| 361 !DecodeDigitallySigned(input, &result->signature)) { | 390 !DecodeDigitallySigned(input, &result->signature)) { |
| 362 return false; | 391 return false; |
| 363 } | 392 } |
| 364 | 393 |
| 365 if (timestamp > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | |
| 366 DVLOG(1) << "Timestamp value too big to cast to int64_t: " << timestamp; | |
| 367 return false; | |
| 368 } | |
| 369 | |
| 370 log_id.CopyToString(&result->log_id); | 394 log_id.CopyToString(&result->log_id); |
| 371 extensions.CopyToString(&result->extensions); | 395 extensions.CopyToString(&result->extensions); |
| 372 result->timestamp = | |
| 373 base::Time::UnixEpoch() + | |
| 374 base::TimeDelta::FromMilliseconds(static_cast<int64_t>(timestamp)); | |
| 375 | |
| 376 output->swap(result); | 396 output->swap(result); |
| 377 return true; | 397 return true; |
| 378 } | 398 } |
| 379 | 399 |
| 380 bool EncodeSCTListForTesting(const base::StringPiece& sct, | 400 bool EncodeSCTListForTesting(const base::StringPiece& sct, |
| 381 std::string* output) { | 401 std::string* output) { |
| 382 std::string encoded_sct; | 402 std::string encoded_sct; |
| 383 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && | 403 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && |
| 384 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); | 404 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); |
| 385 } | 405 } |
| 386 | 406 |
| 387 } // namespace ct | 407 } // namespace ct |
| 388 | 408 |
| 389 } // namespace net | 409 } // namespace net |
| OLD | NEW |