| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "media/cdm/cenc_utils.h" | 5 #include "media/cdm/cenc_utils.h" |
| 6 | 6 |
| 7 #include "media/base/bit_reader.h" | 7 #include "base/stl_util.h" |
| 8 #include "media/formats/mp4/box_definitions.h" |
| 9 #include "media/formats/mp4/box_reader.h" |
| 8 | 10 |
| 9 namespace media { | 11 namespace media { |
| 10 | 12 |
| 11 // The initialization data for encrypted media files using the ISO Common | 13 // The initialization data for encrypted media files using the ISO Common |
| 12 // Encryption ('cenc') protection scheme may contain one or more protection | 14 // Encryption ('cenc') protection scheme may contain one or more protection |
| 13 // system specific header ('pssh') boxes. | 15 // system specific header ('pssh') boxes. |
| 14 // ref: https://w3c.github.io/encrypted-media/cenc-format.html | 16 // ref: https://w3c.github.io/encrypted-media/cenc-format.html |
| 15 // | |
| 16 // The format of a 'pssh' box is as follows: | |
| 17 // unsigned int(32) size; | |
| 18 // unsigned int(32) type = "pssh"; | |
| 19 // if (size==1) { | |
| 20 // unsigned int(64) largesize; | |
| 21 // } else if (size==0) { | |
| 22 // -- box extends to end of file | |
| 23 // } | |
| 24 // unsigned int(8) version; | |
| 25 // bit(24) flags; | |
| 26 // unsigned int(8)[16] SystemID; | |
| 27 // if (version > 0) | |
| 28 // { | |
| 29 // unsigned int(32) KID_count; | |
| 30 // { | |
| 31 // unsigned int(8)[16] KID; | |
| 32 // } [KID_count] | |
| 33 // } | |
| 34 // unsigned int(32) DataSize; | |
| 35 // unsigned int(8)[DataSize] Data; | |
| 36 | |
| 37 // Minimum size of a 'pssh' box includes all the required fields (size, type, | |
| 38 // version, flags, SystemID, DataSize). | |
| 39 const int kMinimumBoxSizeInBytes = 32; | |
| 40 | 17 |
| 41 // SystemID for the Common System. | 18 // SystemID for the Common System. |
| 42 // https://w3c.github.io/encrypted-media/cenc-format.html#common-system | 19 // https://w3c.github.io/encrypted-media/cenc-format.html#common-system |
| 43 const uint8_t kCommonSystemId[] = { 0x10, 0x77, 0xef, 0xec, | 20 const uint8_t kCommonSystemId[] = { 0x10, 0x77, 0xef, 0xec, |
| 44 0xc0, 0xb2, 0x4d, 0x02, | 21 0xc0, 0xb2, 0x4d, 0x02, |
| 45 0xac, 0xe3, 0x3c, 0x1e, | 22 0xac, 0xe3, 0x3c, 0x1e, |
| 46 0x52, 0xe2, 0xfb, 0x4b }; | 23 0x52, 0xe2, 0xfb, 0x4b }; |
| 47 | 24 |
| 48 #define RCHECK(x) \ | 25 static bool ReadAllPsshBoxes( |
| 49 do { \ | 26 const std::vector<uint8_t>& input, |
| 50 if (!(x)) \ | 27 std::vector<mp4::FullProtectionSystemSpecificHeader>* pssh_boxes) { |
| 51 return false; \ | 28 DCHECK(!input.empty()); |
| 52 } while (0) | |
| 53 | 29 |
| 54 // Helper function to read up to 32 bits from a bit stream. | 30 // Verify that |input| contains only 'pssh' boxes. ReadAllChildren() is |
| 55 static uint32_t ReadBits(BitReader* reader, int num_bits) { | 31 // templated, so it checks that each box in |input| matches the box type of |
| 56 DCHECK_GE(reader->bits_available(), num_bits); | 32 // the parameter (in this case mp4::ProtectionSystemSpecificHeader is a |
| 57 DCHECK((num_bits > 0) && (num_bits <= 32)); | 33 // 'pssh' box). mp4::ProtectionSystemSpecificHeader doesn't validate the |
| 58 uint32_t value; | 34 // 'pssh' contents, so this simply verifies that |input| only contains |
| 59 reader->ReadBits(num_bits, &value); | 35 // 'pssh' boxes and nothing else. |
| 60 return value; | 36 scoped_ptr<mp4::BoxReader> input_reader( |
| 61 } | 37 mp4::BoxReader::ReadConcatentatedBoxes( |
| 38 vector_as_array(&input), input.size())); |
| 39 std::vector<mp4::ProtectionSystemSpecificHeader> raw_pssh_boxes; |
| 40 if (!input_reader->ReadAllChildren(&raw_pssh_boxes)) |
| 41 return false; |
| 62 | 42 |
| 63 // Checks whether the next 16 bytes matches the Common SystemID. | 43 // Now that we have |input| parsed into |raw_pssh_boxes|, reparse each one |
| 64 // Assumes |reader| has enough data. | 44 // into a mp4::FullProtectionSystemSpecificHeader, which extracts all the |
| 65 static bool IsCommonSystemID(BitReader* reader) { | 45 // relevant fields from the box. Since there may be unparseable 'pssh' boxes |
| 66 for (uint32_t i = 0; i < arraysize(kCommonSystemId); ++i) { | 46 // (due to unsupported version, for example), this is done one by one, |
| 67 if (ReadBits(reader, 8) != kCommonSystemId[i]) | 47 // ignoring any boxes that can't be parsed. |
| 68 return false; | 48 for (const auto& raw_pssh_box : raw_pssh_boxes) { |
| 69 } | 49 scoped_ptr<mp4::BoxReader> raw_pssh_reader( |
| 70 return true; | 50 mp4::BoxReader::ReadConcatentatedBoxes( |
| 71 } | 51 vector_as_array(&raw_pssh_box.raw_box), |
| 72 | 52 raw_pssh_box.raw_box.size())); |
| 73 // Checks that |reader| contains a valid 'ppsh' box header. |reader| is updated | 53 // ReadAllChildren() appends any successfully parsed box onto it's |
| 74 // to point to the content immediately following the box header. Returns true | 54 // parameter, so |pssh_boxes| will contain the collection of successfully |
| 75 // if the header looks valid and |reader| contains enough data for the size of | 55 // parsed 'pssh' boxes. If an error occurs, try the next box. |
| 76 // header. |size| is updated as the computed size of the box header. Otherwise | 56 if (!raw_pssh_reader->ReadAllChildren(pssh_boxes)) |
| 77 // false is returned. | 57 continue; |
| 78 static bool ValidBoxHeader(BitReader* reader, uint32* size) { | |
| 79 // Enough data for a miniumum size 'pssh' box? | |
| 80 uint32 available_bytes = reader->bits_available() / 8; | |
| 81 RCHECK(available_bytes >= kMinimumBoxSizeInBytes); | |
| 82 | |
| 83 *size = ReadBits(reader, 32); | |
| 84 | |
| 85 // Must be a 'pssh' box or else fail. | |
| 86 RCHECK(ReadBits(reader, 8) == 'p'); | |
| 87 RCHECK(ReadBits(reader, 8) == 's'); | |
| 88 RCHECK(ReadBits(reader, 8) == 's'); | |
| 89 RCHECK(ReadBits(reader, 8) == 'h'); | |
| 90 | |
| 91 if (*size == 1) { | |
| 92 // If largesize > 2**32 it is too big. | |
| 93 RCHECK(ReadBits(reader, 32) == 0); | |
| 94 *size = ReadBits(reader, 32); | |
| 95 } else if (*size == 0) { | |
| 96 *size = available_bytes; | |
| 97 } | 58 } |
| 98 | 59 |
| 99 // Check that the buffer contains at least size bytes. | 60 // Must have successfully parsed at least one 'pssh' box. |
| 100 return available_bytes >= *size; | 61 return pssh_boxes->size() > 0; |
| 101 } | 62 } |
| 102 | 63 |
| 103 bool ValidatePsshInput(const std::vector<uint8_t>& input) { | 64 bool ValidatePsshInput(const std::vector<uint8_t>& input) { |
| 104 size_t offset = 0; | 65 // No 'pssh' boxes is considered valid. |
| 105 while (offset < input.size()) { | 66 if (input.empty()) |
| 106 // Create a BitReader over the remaining part of the buffer. | 67 return true; |
| 107 BitReader reader(&input[offset], input.size() - offset); | |
| 108 uint32 size; | |
| 109 RCHECK(ValidBoxHeader(&reader, &size)); | |
| 110 | 68 |
| 111 // Update offset to point at the next 'pssh' box (may not be one). | 69 std::vector<mp4::FullProtectionSystemSpecificHeader> children; |
| 112 offset += size; | 70 return ReadAllPsshBoxes(input, &children); |
| 113 } | |
| 114 | |
| 115 // Only valid if this contains 0 or more 'pssh' boxes. | |
| 116 return offset == input.size(); | |
| 117 } | 71 } |
| 118 | 72 |
| 119 bool GetKeyIdsForCommonSystemId(const std::vector<uint8_t>& input, | 73 bool GetKeyIdsForCommonSystemId(const std::vector<uint8_t>& input, |
| 120 KeyIdList* key_ids) { | 74 KeyIdList* key_ids) { |
| 121 size_t offset = 0; | |
| 122 KeyIdList result; | 75 KeyIdList result; |
| 76 std::vector<uint8_t> common_system_id( |
| 77 kCommonSystemId, kCommonSystemId + arraysize(kCommonSystemId)); |
| 123 | 78 |
| 124 while (offset < input.size()) { | 79 if (!input.empty()) { |
| 125 BitReader reader(&input[offset], input.size() - offset); | 80 std::vector<mp4::FullProtectionSystemSpecificHeader> children; |
| 126 uint32 size; | 81 if (!ReadAllPsshBoxes(input, &children)) |
| 127 RCHECK(ValidBoxHeader(&reader, &size)); | 82 return false; |
| 128 | 83 |
| 129 // Update offset to point at the next 'pssh' box (may not be one). | 84 // Check all children for an appropriate 'pssh' box, concatenating any |
| 130 offset += size; | 85 // key IDs found. |
| 131 | 86 for (const auto& child : children) { |
| 132 // Check the version, as KIDs only available if version > 0. | 87 if (child.system_id == common_system_id && child.key_ids.size() > 0) |
| 133 uint8_t version = ReadBits(&reader, 8); | 88 result.insert(result.end(), child.key_ids.begin(), child.key_ids.end()); |
| 134 if (version == 0) | |
| 135 continue; | |
| 136 | |
| 137 // flags must be 0. If not, assume incorrect 'pssh' box and move to the | |
| 138 // next one. | |
| 139 if (ReadBits(&reader, 24) != 0) | |
| 140 continue; | |
| 141 | |
| 142 // Validate SystemID | |
| 143 RCHECK(static_cast<uint32_t>(reader.bits_available()) >= | |
| 144 arraysize(kCommonSystemId) * 8); | |
| 145 if (!IsCommonSystemID(&reader)) | |
| 146 continue; // Not Common System, so try the next pssh box. | |
| 147 | |
| 148 // Since version > 0, next field is the KID_count. | |
| 149 RCHECK(static_cast<uint32_t>(reader.bits_available()) >= | |
| 150 sizeof(uint32_t) * 8); | |
| 151 uint32_t count = ReadBits(&reader, 32); | |
| 152 | |
| 153 if (count == 0) | |
| 154 continue; | |
| 155 | |
| 156 // Make sure there is enough data for all the KIDs specified, and then | |
| 157 // extract them. | |
| 158 RCHECK(static_cast<uint32_t>(reader.bits_available()) > count * 16 * 8); | |
| 159 while (count > 0) { | |
| 160 std::vector<uint8_t> key; | |
| 161 key.reserve(16); | |
| 162 for (int i = 0; i < 16; ++i) { | |
| 163 key.push_back(ReadBits(&reader, 8)); | |
| 164 } | |
| 165 result.push_back(key); | |
| 166 --count; | |
| 167 } | 89 } |
| 168 | |
| 169 // Don't bother checking DataSize and Data. | |
| 170 } | 90 } |
| 171 | 91 |
| 172 key_ids->swap(result); | 92 // No matching 'pssh' box found. |
| 173 | |
| 174 // TODO(jrummell): This should return true only if there was at least one | 93 // TODO(jrummell): This should return true only if there was at least one |
| 175 // key ID present. However, numerous test files don't contain the 'pssh' box | 94 // key ID present. However, numerous test files don't contain the 'pssh' box |
| 176 // for Common Format, so no keys are found. http://crbug.com/460308 | 95 // for Common Format, so no keys are found. http://crbug.com/460308 |
| 96 key_ids->swap(result); |
| 177 return true; | 97 return true; |
| 178 } | 98 } |
| 179 | 99 |
| 100 bool GetPsshData(const std::vector<uint8_t>& input, |
| 101 const std::vector<uint8_t>& system_id, |
| 102 std::vector<uint8_t>* pssh_data) { |
| 103 if (input.empty()) |
| 104 return false; |
| 105 |
| 106 std::vector<mp4::FullProtectionSystemSpecificHeader> children; |
| 107 if (!ReadAllPsshBoxes(input, &children)) |
| 108 return false; |
| 109 |
| 110 // Check all children for an appropriate 'pssh' box, returning |data| from |
| 111 // the first one found. |
| 112 for (const auto& child : children) { |
| 113 if (child.system_id == system_id) { |
| 114 pssh_data->assign(child.data.begin(), child.data.end()); |
| 115 return true; |
| 116 } |
| 117 } |
| 118 |
| 119 // No matching 'pssh' box found. |
| 120 return false; |
| 121 } |
| 122 |
| 180 } // namespace media | 123 } // namespace media |
| OLD | NEW |