Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(598)

Side by Side Diff: media/webm/webm_cluster_parser.cc

Issue 10823110: Add support for v0.3 of the encrypted WebM specification. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressing comments from Patch Set 3. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/webm/webm_cluster_parser.h" 5 #include "media/webm/webm_cluster_parser.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/sys_byteorder.h" 8 #include "base/sys_byteorder.h"
9 #include "media/base/data_buffer.h" 9 #include "media/base/data_buffer.h"
10 #include "media/base/decrypt_config.h" 10 #include "media/base/decrypt_config.h"
11 #include "media/webm/webm_constants.h" 11 #include "media/webm/webm_constants.h"
12 12
13 namespace media { 13 namespace media {
14 14
15 // Generates a 16 byte CTR counter block. The CTR counter block format is a 15 // Generates a 16 byte CTR counter block. The CTR counter block format is a
16 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV. 16 // CTR IV appended with a CTR block counter. |iv| is an 8 byte CTR IV.
17 // Always returns a valid pointer to a buffer of kDecryptionKeySize bytes. 17 // Returns a string of kDecryptionKeySize bytes.
18 static scoped_array<uint8> GenerateCounterBlock(uint64 iv) { 18 static std::string GenerateCounterBlock(uint64 iv) {
19 scoped_array<uint8> counter_block_data( 19 char counter_block_data[DecryptConfig::kDecryptionKeySize];
20 new uint8[DecryptConfig::kDecryptionKeySize]);
21 20
22 // Set the IV. 21 // Set the IV.
23 memcpy(counter_block_data.get(), &iv, sizeof(iv)); 22 memcpy(counter_block_data, &iv, sizeof(iv));
24 23
25 // Set block counter to all 0's. 24 // Set block counter to all 0's.
26 memset(counter_block_data.get() + sizeof(iv), 25 memset(counter_block_data + sizeof(iv), 0,
27 0,
28 DecryptConfig::kDecryptionKeySize - sizeof(iv)); 26 DecryptConfig::kDecryptionKeySize - sizeof(iv));
29 27
30 return counter_block_data.Pass(); 28 return std::string(counter_block_data, DecryptConfig::kDecryptionKeySize);
xhwang 2012/08/01 04:38:23 Can we do something like this: std::string counter
fgalligan1 2012/08/01 15:07:38 I changed to this: std::string counter_block(reint
31 } 29 }
32 30
33 WebMClusterParser::WebMClusterParser(int64 timecode_scale, 31 WebMClusterParser::WebMClusterParser(int64 timecode_scale,
34 int audio_track_num, 32 int audio_track_num,
35 int video_track_num, 33 int video_track_num,
36 const uint8* video_encryption_key_id, 34 const uint8* video_encryption_key_id,
37 int video_encryption_key_id_size) 35 int video_encryption_key_id_size)
38 : timecode_multiplier_(timecode_scale / 1000.0), 36 : timecode_multiplier_(timecode_scale / 1000.0),
39 video_encryption_key_id_size_(video_encryption_key_id_size), 37 video_encryption_key_id_size_(video_encryption_key_id_size),
40 parser_(kWebMIdCluster, this), 38 parser_(kWebMIdCluster, this),
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 } 211 }
214 212
215 last_block_timecode_ = timecode; 213 last_block_timecode_ = timecode;
216 214
217 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds( 215 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds(
218 (cluster_timecode_ + timecode) * timecode_multiplier_); 216 (cluster_timecode_ + timecode) * timecode_multiplier_);
219 217
220 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted 218 // Every encrypted Block has an HMAC and IV prepended to it. Current encrypted
221 // WebM request for comments specification is here 219 // WebM request for comments specification is here
222 // http://wiki.webmproject.org/encryption/webm-encryption-rfc 220 // http://wiki.webmproject.org/encryption/webm-encryption-rfc
223 bool encrypted = track_num == video_.track_num() && 221 bool is_track_encrypted = track_num == video_.track_num() &&
224 video_encryption_key_id_.get(); 222 video_encryption_key_id_.get();
225 // If encrypted skip past the HMAC. Encrypted buffers must include the IV and 223
226 // the encrypted frame because the decryptor will verify this data before 224 // If stream is encrypted skip past the HMAC. Encrypted buffers must include
227 // decryption. The HMAC and IV will be copied into DecryptConfig. 225 // the signal byte, the IV (if frame is encrypted) and
228 int offset = (encrypted) ? kWebMHmacSize : 0; 226 // the frame because the decryptor will verify this data before decryption.
227 // The HMAC and IV will be copied into DecryptConfig.
228 int offset = (is_track_encrypted) ? kWebMHmacSize : 0;
229 229
230 // The first bit of the flags is set when the block contains only keyframes. 230 // The first bit of the flags is set when the block contains only keyframes.
231 // http://www.matroska.org/technical/specs/index.html 231 // http://www.matroska.org/technical/specs/index.html
232 bool is_keyframe = (flags & 0x80) != 0; 232 bool is_keyframe = (flags & 0x80) != 0;
233 scoped_refptr<StreamParserBuffer> buffer = 233 scoped_refptr<StreamParserBuffer> buffer =
234 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe); 234 StreamParserBuffer::CopyFrom(data + offset, size - offset, is_keyframe);
235 235
236 if (encrypted) { 236 if (is_track_encrypted) {
237 uint64 network_iv; 237 uint8 signal_byte = data[kWebMHmacSize];
238 memcpy(&network_iv, data + kWebMHmacSize, sizeof(network_iv)); 238 int data_offset = sizeof(signal_byte);
239 const uint64 iv = base::NetToHost64(network_iv);
240 239
241 scoped_array<uint8> counter_block(GenerateCounterBlock(iv)); 240 // Setting the DecryptConfig object of the buffer while leaving the
241 // initialization vector empty will tell the decryptor that the frame is
242 // unencrypted but integrity should still be checked.
243 std::string counter_block;
244
245 if (signal_byte & kWebMFlagEncryptedFrame) {
246 uint64 network_iv;
247 memcpy(&network_iv, data + kWebMHmacSize + data_offset,
248 sizeof(network_iv));
249 const uint64 iv = base::NetToHost64(network_iv);
250 counter_block = GenerateCounterBlock(iv);
251 data_offset += sizeof(iv);
252 }
253
242 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig( 254 buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>(new DecryptConfig(
243 std::string( 255 std::string(
244 reinterpret_cast<const char*>(video_encryption_key_id_.get()), 256 reinterpret_cast<const char*>(video_encryption_key_id_.get()),
245 video_encryption_key_id_size_), 257 video_encryption_key_id_size_),
246 std::string( 258 counter_block,
247 reinterpret_cast<const char*>(counter_block.get()),
248 DecryptConfig::kDecryptionKeySize),
249 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize), 259 std::string(reinterpret_cast<const char*>(data), kWebMHmacSize),
250 sizeof(iv), 260 data_offset,
251 std::vector<SubsampleEntry>()))); 261 std::vector<SubsampleEntry>())));
252 } 262 }
253 263
254 buffer->SetTimestamp(timestamp); 264 buffer->SetTimestamp(timestamp);
255 if (cluster_start_time_ == kNoTimestamp()) 265 if (cluster_start_time_ == kNoTimestamp())
256 cluster_start_time_ = timestamp; 266 cluster_start_time_ = timestamp;
257 267
258 if (block_duration >= 0) { 268 if (block_duration >= 0) {
259 buffer->SetDuration(base::TimeDelta::FromMicroseconds( 269 buffer->SetDuration(base::TimeDelta::FromMicroseconds(
260 block_duration * timecode_multiplier_)); 270 block_duration * timecode_multiplier_));
(...skipping 25 matching lines...) Expand all
286 296
287 buffers_.push_back(buffer); 297 buffers_.push_back(buffer);
288 return true; 298 return true;
289 } 299 }
290 300
291 void WebMClusterParser::Track::Reset() { 301 void WebMClusterParser::Track::Reset() {
292 buffers_.clear(); 302 buffers_.clear();
293 } 303 }
294 304
295 } // namespace media 305 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698