| 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 <limits> | 10 #include <limits> |
| 10 | 11 |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/numerics/safe_math.h" |
| 12 | 14 |
| 13 namespace net { | 15 namespace net { |
| 14 | 16 |
| 15 namespace ct { | 17 namespace ct { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 // Note: length is always specified in bytes. | 21 // Note: length is always specified in bytes. |
| 20 // Signed Certificate Timestamp (SCT) Version length | 22 // Signed Certificate Timestamp (SCT) Version length |
| 21 const size_t kVersionLength = 1; | 23 const size_t kVersionLength = 1; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 47 | 49 |
| 48 // Reads a TLS-encoded variable length unsigned integer from |in|. | 50 // Reads a TLS-encoded variable length unsigned integer from |in|. |
| 49 // The integer is expected to be in big-endian order, which is used by TLS. | 51 // The integer is expected to be in big-endian order, which is used by TLS. |
| 50 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 52 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 51 // |length| indicates the size (in bytes) of the integer. On success, returns | 53 // |length| indicates the size (in bytes) of the integer. On success, returns |
| 52 // true and stores the result in |*out|. | 54 // true and stores the result in |*out|. |
| 53 template <typename T> | 55 template <typename T> |
| 54 bool ReadUint(size_t length, base::StringPiece* in, T* out) { | 56 bool ReadUint(size_t length, base::StringPiece* in, T* out) { |
| 55 if (in->size() < length) | 57 if (in->size() < length) |
| 56 return false; | 58 return false; |
| 59 DCHECK_NE(length, 0u); |
| 57 DCHECK_LE(length, sizeof(T)); | 60 DCHECK_LE(length, sizeof(T)); |
| 58 | 61 |
| 59 T result = 0; | 62 T result = static_cast<uint8_t>((*in)[0]); |
| 60 for (size_t i = 0; i < length; ++i) { | 63 // This loop only executes if sizeof(T) > 1, because the first operation is |
| 61 result = (result << 8) | static_cast<unsigned char>((*in)[i]); | 64 // to shift left by 1 byte, which is undefined behaviour if T is a 1 byte |
| 65 // integer. |
| 66 for (size_t i = 1; i < length; ++i) { |
| 67 result = (result << 8) | static_cast<uint8_t>((*in)[i]); |
| 62 } | 68 } |
| 63 in->remove_prefix(length); | 69 in->remove_prefix(length); |
| 64 *out = result; | 70 *out = result; |
| 65 return true; | 71 return true; |
| 66 } | 72 } |
| 67 | 73 |
| 68 // Reads a TLS-encoded field length from |in|. | 74 // Reads a TLS-encoded field length from |in|. |
| 69 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 75 // 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) | 76 // |prefix_length| indicates the bytes needed to represent the length (e.g. 3). |
| 77 // Max |prefix_length| is 8. |
| 71 // success, returns true and stores the result in |*out|. | 78 // success, returns true and stores the result in |*out|. |
| 72 bool ReadLength(size_t prefix_length, base::StringPiece* in, size_t* out) { | 79 bool ReadLength(size_t prefix_length, base::StringPiece* in, size_t* out) { |
| 73 size_t length; | 80 uint64_t length = 0; |
| 74 if (!ReadUint(prefix_length, in, &length)) | 81 if (!ReadUint(prefix_length, in, &length)) |
| 75 return false; | 82 return false; |
| 76 *out = length; | 83 base::CheckedNumeric<size_t> checked_length = length; |
| 84 if (!checked_length.IsValid()) |
| 85 return false; |
| 86 *out = checked_length.ValueOrDie(); |
| 77 return true; | 87 return true; |
| 78 } | 88 } |
| 79 | 89 |
| 80 // Reads |length| bytes from |*in|. If |*in| is too small, returns false. | 90 // 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) | 91 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 82 bool ReadFixedBytes(size_t length, | 92 bool ReadFixedBytes(size_t length, |
| 83 base::StringPiece* in, | 93 base::StringPiece* in, |
| 84 base::StringPiece* out) { | 94 base::StringPiece* out) { |
| 85 if (in->length() < length) | 95 if (in->length() < length) |
| 86 return false; | 96 return false; |
| 87 out->set(in->data(), length); | 97 out->set(in->data(), length); |
| 88 in->remove_prefix(length); | 98 in->remove_prefix(length); |
| 89 return true; | 99 return true; |
| 90 } | 100 } |
| 91 | 101 |
| 92 // Reads a length-prefixed variable amount of bytes from |in|, updating |out| | 102 // 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 | 103 // on success. |prefix_length| indicates the number of bytes needed to represent |
| 94 // the length. | 104 // the length. |
| 95 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 105 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) |
| 96 bool ReadVariableBytes(size_t prefix_length, | 106 bool ReadVariableBytes(size_t prefix_length, |
| 97 base::StringPiece* in, | 107 base::StringPiece* in, |
| 98 base::StringPiece* out) { | 108 base::StringPiece* out) { |
| 99 size_t length; | 109 size_t length = 0; |
| 100 if (!ReadLength(prefix_length, in, &length)) | 110 if (!ReadLength(prefix_length, in, &length)) |
| 101 return false; | 111 return false; |
| 102 return ReadFixedBytes(length, in, out); | 112 return ReadFixedBytes(length, in, out); |
| 103 } | 113 } |
| 104 | 114 |
| 105 // Reads a variable-length list that has been TLS encoded. | 115 // Reads a variable-length list that has been TLS encoded. |
| 106 // The bytes read from |in| are discarded (i.e. |in|'s prefix removed) | 116 // 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. | 117 // |max_list_length| contains the overall length of the encoded list. |
| 108 // |max_item_length| contains the maximum length of a single item. | 118 // |max_item_length| contains the maximum length of a single item. |
| 109 // On success, returns true and updates |*out| with the encoded list. | 119 // On success, returns true and updates |*out| with the encoded list. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 case DigitallySigned::SIG_ALGO_ECDSA: | 179 case DigitallySigned::SIG_ALGO_ECDSA: |
| 170 break; | 180 break; |
| 171 default: | 181 default: |
| 172 return false; | 182 return false; |
| 173 } | 183 } |
| 174 *out = static_cast<DigitallySigned::SignatureAlgorithm>(in); | 184 *out = static_cast<DigitallySigned::SignatureAlgorithm>(in); |
| 175 return true; | 185 return true; |
| 176 } | 186 } |
| 177 | 187 |
| 178 // Writes a TLS-encoded variable length unsigned integer to |output|. | 188 // Writes a TLS-encoded variable length unsigned integer to |output|. |
| 179 // |length| indicates the size (in bytes) of the integer. | 189 // |length| indicates the size (in bytes) of the integer. This must be able to |
| 190 // accomodate |value|. |
| 180 // |value| the value itself to be written. | 191 // |value| the value itself to be written. |
| 181 template <typename T> | 192 void WriteUint(size_t length, uint64_t value, std::string* output) { |
| 182 void WriteUint(size_t length, T value, std::string* output) { | 193 // Check that |value| fits into |length| bytes. |
| 183 DCHECK_LE(length, sizeof(T)); | 194 DCHECK(length >= sizeof(value) || value >> (length * 8) == 0); |
| 184 DCHECK(length == sizeof(T) || value >> (length * 8) == 0); | |
| 185 | 195 |
| 186 for (; length > 0; --length) { | 196 for (; length > 0; --length) { |
| 187 output->push_back((value >> ((length - 1)* 8)) & 0xFF); | 197 output->push_back((value >> ((length - 1) * 8)) & 0xFF); |
| 188 } | 198 } |
| 189 } | 199 } |
| 190 | 200 |
| 191 // Writes an array to |output| from |input|. | 201 // Writes an array to |output| from |input|. |
| 192 // Should be used in one of two cases: | 202 // Should be used in one of two cases: |
| 193 // * The length of |input| has already been encoded into the |output| stream. | 203 // * 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 | 204 // * The length of |input| is fixed and the reader is expected to specify that |
| 195 // length when reading. | 205 // length when reading. |
| 196 // If the length of |input| is dynamic and data is expected to follow it, | 206 // If the length of |input| is dynamic and data is expected to follow it, |
| 197 // WriteVariableBytes must be used. | 207 // WriteVariableBytes must be used. |
| 198 void WriteEncodedBytes(const base::StringPiece& input, std::string* output) { | 208 // Returns the number of bytes written (the length of |input|). |
| 209 size_t WriteEncodedBytes(const base::StringPiece& input, std::string* output) { |
| 199 input.AppendToString(output); | 210 input.AppendToString(output); |
| 211 return input.size(); |
| 200 } | 212 } |
| 201 | 213 |
| 202 // Writes a variable-length array to |output|. | 214 // Writes a variable-length array to |output|. |
| 203 // |prefix_length| indicates the number of bytes needed to represnt the length. | 215 // |prefix_length| indicates the number of bytes needed to represent the length. |
| 204 // |input| is the array itself. | 216 // |input| is the array itself. |
| 205 // If the size of |input| is less than 2^|prefix_length| - 1, encode the | 217 // If 1 <= |prefix_length| <= 8 and the size of |input| is less than |
| 206 // length and data and return true. Otherwise, return false. | 218 // 2^|prefix_length| - 1, encode the length and data and return true. |
| 219 // Otherwise, return false. |
| 207 bool WriteVariableBytes(size_t prefix_length, | 220 bool WriteVariableBytes(size_t prefix_length, |
| 208 const base::StringPiece& input, | 221 const base::StringPiece& input, |
| 209 std::string* output) { | 222 std::string* output) { |
| 210 size_t input_size = input.size(); | 223 DCHECK_GE(prefix_length, 1u); |
| 211 size_t max_allowed_input_size = | 224 DCHECK_LE(prefix_length, 8u); |
| 212 static_cast<size_t>(((1 << (prefix_length * 8)) - 1)); | 225 |
| 213 if (input_size > max_allowed_input_size) | 226 uint64_t input_size = input.size(); |
| 227 uint64_t max_input_size = (prefix_length == 8) |
| 228 ? UINT64_MAX |
| 229 : ((UINT64_C(1) << (prefix_length * 8)) - 1); |
| 230 |
| 231 if (input_size > max_input_size) |
| 214 return false; | 232 return false; |
| 215 | 233 |
| 216 WriteUint(prefix_length, input.size(), output); | 234 WriteUint(prefix_length, input_size, output); |
| 217 WriteEncodedBytes(input, output); | 235 WriteEncodedBytes(input, output); |
| 218 | 236 |
| 219 return true; | 237 return true; |
| 220 } | 238 } |
| 221 | 239 |
| 222 // Writes a LogEntry of type X.509 cert to |output|. | 240 // Writes a LogEntry of type X.509 cert to |output|. |
| 223 // |input| is the LogEntry containing the certificate. | 241 // |input| is the LogEntry containing the certificate. |
| 224 // Returns true if the leaf_certificate in the LogEntry does not exceed | 242 // Returns true if the leaf_certificate in the LogEntry does not exceed |
| 225 // kMaxAsn1CertificateLength and so can be written to |output|. | 243 // kMaxAsn1CertificateLength and so can be written to |output|. |
| 226 bool EncodeAsn1CertLogEntry(const LogEntry& input, std::string* output) { | 244 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); | 302 WriteUint(kLogEntryTypeLength, input.type, output); |
| 285 switch (input.type) { | 303 switch (input.type) { |
| 286 case LogEntry::LOG_ENTRY_TYPE_X509: | 304 case LogEntry::LOG_ENTRY_TYPE_X509: |
| 287 return EncodeAsn1CertLogEntry(input, output); | 305 return EncodeAsn1CertLogEntry(input, output); |
| 288 case LogEntry::LOG_ENTRY_TYPE_PRECERT: | 306 case LogEntry::LOG_ENTRY_TYPE_PRECERT: |
| 289 return EncodePrecertLogEntry(input, output); | 307 return EncodePrecertLogEntry(input, output); |
| 290 } | 308 } |
| 291 return false; | 309 return false; |
| 292 } | 310 } |
| 293 | 311 |
| 312 static bool ReadTimeSinceEpoch(base::StringPiece* input, base::Time* output) { |
| 313 uint64_t time_since_epoch = 0; |
| 314 if (!ReadUint(kTimestampLength, input, &time_since_epoch)) |
| 315 return false; |
| 316 |
| 317 base::CheckedNumeric<int64_t> time_since_epoch_signed = time_since_epoch; |
| 318 |
| 319 if (!time_since_epoch_signed.IsValid()) { |
| 320 DVLOG(1) << "Timestamp value too big to cast to int64_t: " |
| 321 << time_since_epoch; |
| 322 return false; |
| 323 } |
| 324 |
| 325 *output = |
| 326 base::Time::UnixEpoch() + |
| 327 base::TimeDelta::FromMilliseconds(time_since_epoch_signed.ValueOrDie()); |
| 328 |
| 329 return true; |
| 330 } |
| 331 |
| 294 static void WriteTimeSinceEpoch(const base::Time& timestamp, | 332 static void WriteTimeSinceEpoch(const base::Time& timestamp, |
| 295 std::string* output) { | 333 std::string* output) { |
| 296 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); | 334 base::TimeDelta time_since_epoch = timestamp - base::Time::UnixEpoch(); |
| 297 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); | 335 WriteUint(kTimestampLength, time_since_epoch.InMilliseconds(), output); |
| 298 } | 336 } |
| 299 | 337 |
| 300 bool EncodeV1SCTSignedData(const base::Time& timestamp, | 338 bool EncodeV1SCTSignedData(const base::Time& timestamp, |
| 301 const std::string& serialized_log_entry, | 339 const std::string& serialized_log_entry, |
| 302 const std::string& extensions, | 340 const std::string& extensions, |
| 303 std::string* output) { | 341 std::string* output) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 new SignedCertificateTimestamp()); | 382 new SignedCertificateTimestamp()); |
| 345 unsigned version; | 383 unsigned version; |
| 346 if (!ReadUint(kVersionLength, input, &version)) | 384 if (!ReadUint(kVersionLength, input, &version)) |
| 347 return false; | 385 return false; |
| 348 if (version != SignedCertificateTimestamp::SCT_VERSION_1) { | 386 if (version != SignedCertificateTimestamp::SCT_VERSION_1) { |
| 349 DVLOG(1) << "Unsupported/invalid version " << version; | 387 DVLOG(1) << "Unsupported/invalid version " << version; |
| 350 return false; | 388 return false; |
| 351 } | 389 } |
| 352 | 390 |
| 353 result->version = SignedCertificateTimestamp::SCT_VERSION_1; | 391 result->version = SignedCertificateTimestamp::SCT_VERSION_1; |
| 354 uint64_t timestamp; | |
| 355 base::StringPiece log_id; | 392 base::StringPiece log_id; |
| 356 base::StringPiece extensions; | 393 base::StringPiece extensions; |
| 357 if (!ReadFixedBytes(kLogIdLength, input, &log_id) || | 394 if (!ReadFixedBytes(kLogIdLength, input, &log_id) || |
| 358 !ReadUint(kTimestampLength, input, ×tamp) || | 395 !ReadTimeSinceEpoch(input, &result->timestamp) || |
| 359 !ReadVariableBytes(kExtensionsLengthBytes, input, | 396 !ReadVariableBytes(kExtensionsLengthBytes, input, &extensions) || |
| 360 &extensions) || | |
| 361 !DecodeDigitallySigned(input, &result->signature)) { | 397 !DecodeDigitallySigned(input, &result->signature)) { |
| 362 return false; | 398 return false; |
| 363 } | 399 } |
| 364 | 400 |
| 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); | 401 log_id.CopyToString(&result->log_id); |
| 371 extensions.CopyToString(&result->extensions); | 402 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); | 403 output->swap(result); |
| 377 return true; | 404 return true; |
| 378 } | 405 } |
| 379 | 406 |
| 380 bool EncodeSCTListForTesting(const base::StringPiece& sct, | 407 bool EncodeSCTListForTesting(const base::StringPiece& sct, |
| 381 std::string* output) { | 408 std::string* output) { |
| 382 std::string encoded_sct; | 409 std::string encoded_sct; |
| 383 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && | 410 return WriteVariableBytes(kSerializedSCTLengthBytes, sct, &encoded_sct) && |
| 384 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); | 411 WriteVariableBytes(kSCTListLengthBytes, encoded_sct, output); |
| 385 } | 412 } |
| 386 | 413 |
| 387 } // namespace ct | 414 } // namespace ct |
| 388 | 415 |
| 389 } // namespace net | 416 } // namespace net |
| OLD | NEW |