1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/*****************************************************************
|
| AP4 - Protected Streams support
|
| Copyright 2002-2008 Axiomatic Systems, LLC
|
|
| This file is part of Bento4/AP4 (MP4 Atom Processing Library).
|
| Unless you have obtained Bento4 under a difference license,
| this version of Bento4 is Bento4|GPL.
| Bento4|GPL is free software; you can redistribute it and/or modify
| it under the terms of the GNU General Public License as published by
| the Free Software Foundation; either version 2, or (at your option)
| any later version.
|
| Bento4|GPL is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
| GNU General Public License for more details.
|
| You should have received a copy of the GNU General Public License
| along with Bento4|GPL; see the file COPYING. If not, write to the
| Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
| 02111-1307, USA.
|
****************************************************************/
#ifndef _AP4_PROTECTION_H_
#define _AP4_PROTECTION_H_
/*----------------------------------------------------------------------
| includes
+---------------------------------------------------------------------*/
#include "Ap4Types.h"
#include "Ap4SampleEntry.h"
#include "Ap4Atom.h"
#include "Ap4AtomFactory.h"
#include "Ap4SampleDescription.h"
#include "Ap4Processor.h"
#include "Ap4Utils.h"
/*----------------------------------------------------------------------
| classes
+---------------------------------------------------------------------*/
class AP4_StreamCipher;
/*----------------------------------------------------------------------
| constants
+---------------------------------------------------------------------*/
// this is fixed for now
const unsigned int AP4_PROTECTION_KEY_LENGTH = 16;
const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_ITUNES = AP4_ATOM_TYPE('i','t','u','n');
/*----------------------------------------------------------------------
| AP4_EncaSampleEntry
+---------------------------------------------------------------------*/
class AP4_EncaSampleEntry : public AP4_AudioSampleEntry
{
public:
// methods
AP4_EncaSampleEntry(AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
AP4_EncaSampleEntry(AP4_UI32 type,
AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
// methods
AP4_SampleDescription* ToSampleDescription();
// this method is used as a factory by the ISMACryp classes
// NOTE: this should be named ToSampleDescription, but C++ has a
// problem with that because the base class does not have this
// overloaded method, but has another other one by that name
virtual AP4_SampleDescription* ToTargetSampleDescription(AP4_UI32 format);
};
/*----------------------------------------------------------------------
| AP4_EncvSampleEntry
+---------------------------------------------------------------------*/
class AP4_EncvSampleEntry : public AP4_VisualSampleEntry
{
public:
// constructors
AP4_EncvSampleEntry(AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
AP4_EncvSampleEntry(AP4_UI32 type,
AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
// methods
AP4_SampleDescription* ToSampleDescription();
// this method is used as a factory by the ISMACryp classes
// NOTE: this should be named ToSampleDescription, but C++ has a
// problem with that because the base class does not have this
// overloaded method, but has another other one by that name
virtual AP4_SampleDescription* ToTargetSampleDescription(AP4_UI32 format);
};
/*----------------------------------------------------------------------
| AP4_DrmsSampleEntry
+---------------------------------------------------------------------*/
class AP4_DrmsSampleEntry : public AP4_EncaSampleEntry
{
public:
// methods
AP4_DrmsSampleEntry(AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
};
/*----------------------------------------------------------------------
| AP4_DrmiSampleEntry
+---------------------------------------------------------------------*/
class AP4_DrmiSampleEntry : public AP4_EncvSampleEntry
{
public:
// methods
AP4_DrmiSampleEntry(AP4_Size size,
AP4_ByteStream& stream,
AP4_AtomFactory& atom_factory);
};
/*----------------------------------------------------------------------
| AP4_ProtectionKeyMap
+---------------------------------------------------------------------*/
class AP4_ProtectionKeyMap
{
public:
// constructors and destructor
AP4_ProtectionKeyMap();
~AP4_ProtectionKeyMap();
// methods
AP4_Result SetKey(AP4_UI32 track_id,
const AP4_UI08* key,
AP4_Size key_size,
const AP4_UI08* iv = NULL,
AP4_Size iv_size = 0);
AP4_Result SetKeyForKid(const AP4_UI08* kid,
const AP4_UI08* key,
AP4_Size key_size,
const AP4_UI08* iv = NULL,
AP4_Size iv_size = 0);
AP4_Result SetKeys(const AP4_ProtectionKeyMap& key_map);
AP4_Result GetKeyAndIv(AP4_UI32 track_id, const AP4_DataBuffer*& key, const AP4_DataBuffer*& iv);
AP4_Result GetKeyAndIvByKid(const AP4_UI08* kid, const AP4_DataBuffer*& key, const AP4_DataBuffer*& iv);
const AP4_DataBuffer* GetKey(AP4_UI32 track_id) const;
const AP4_DataBuffer* GetKeyByKid(const AP4_UI08* kid) const;
const AP4_Cardinal GetItemCount() const { return m_KeyEntries.ItemCount(); };
const AP4_DataBuffer* GetLastInsertedKey() const { return &m_KeyEntries.LastItem()->GetData()->m_Key;};
private:
// types
class KeyEntry {
public:
explicit
KeyEntry(AP4_UI32 track_id,
const AP4_UI08* key,
AP4_Size key_size,
const AP4_UI08* iv = NULL,
AP4_Size iv_size = 0);
explicit
KeyEntry(const AP4_UI08* kid,
const AP4_UI08* key,
AP4_Size key_size,
const AP4_UI08* iv = NULL,
AP4_Size iv_size = 0);
void SetKey(const AP4_UI08* key, AP4_Size key_size,
const AP4_UI08* iv, AP4_Size iv_size);
AP4_Ordinal m_TrackId;
AP4_UI08 m_KID[16];
AP4_DataBuffer m_Key;
AP4_DataBuffer m_IV;
};
// methods
KeyEntry* GetEntry(AP4_UI32 track_id) const;
KeyEntry* GetEntryByKid(const AP4_UI08* kid) const;
// members
AP4_List<KeyEntry> m_KeyEntries;
};
/*----------------------------------------------------------------------
| AP4_TrackPropertyMap
+---------------------------------------------------------------------*/
class AP4_TrackPropertyMap
{
public:
// types
class Entry {
public:
Entry(AP4_UI32 track_id, const char* name, const char* value) :
m_TrackId(track_id), m_Name(name), m_Value(value) {}
AP4_UI32 m_TrackId;
AP4_String m_Name;
AP4_String m_Value;
};
// methods
AP4_Result SetProperty(AP4_UI32 track_id, const char* name, const char* value);
AP4_Result SetProperties(const AP4_TrackPropertyMap& properties);
const char* GetProperty(AP4_UI32 track_id, const char* name);
AP4_Result GetTextualHeaders(AP4_UI32 track_id, AP4_DataBuffer& buffer);
const AP4_List<Entry>& GetEntries() { return m_Entries; }
// destructor
virtual ~AP4_TrackPropertyMap();
private:
// members
AP4_List<Entry> m_Entries;
};
/*----------------------------------------------------------------------
| AP4_ProtectionSchemeInfo
+---------------------------------------------------------------------*/
class AP4_ProtectionSchemeInfo
{
public:
// constructors and destructor
AP4_ProtectionSchemeInfo(AP4_ContainerAtom* schi);
virtual ~AP4_ProtectionSchemeInfo();
// accessors
AP4_ContainerAtom* GetSchiAtom() { return m_SchiAtom; }
protected:
AP4_ContainerAtom* m_SchiAtom;
};
/*----------------------------------------------------------------------
| AP4_ProtectedSampleDescription
+---------------------------------------------------------------------*/
class AP4_ProtectedSampleDescription : public AP4_SampleDescription
{
public:
AP4_IMPLEMENT_DYNAMIC_CAST_D(AP4_ProtectedSampleDescription, AP4_SampleDescription)
// constructor and destructor
AP4_ProtectedSampleDescription(AP4_UI32 format,
AP4_SampleDescription* original_sample_description,
AP4_UI32 original_format,
AP4_UI32 scheme_type,
AP4_UI32 scheme_version,
const char* scheme_uri,
AP4_ContainerAtom* schi_atom, // will be cloned
bool transfer_ownership_of_original=true);
~AP4_ProtectedSampleDescription();
// accessors
AP4_SampleDescription* GetOriginalSampleDescription() {
return m_OriginalSampleDescription;
}
AP4_UI32 GetOriginalFormat() const { return m_OriginalFormat; }
AP4_UI32 GetSchemeType() const { return m_SchemeType; }
AP4_UI32 GetSchemeVersion() const { return m_SchemeVersion; }
const AP4_String& GetSchemeUri() const { return m_SchemeUri; }
AP4_ProtectionSchemeInfo* GetSchemeInfo() const {
return m_SchemeInfo;
}
// implementation of abstract base class methods
virtual AP4_Atom* ToAtom() const;
private:
// members
AP4_SampleDescription* m_OriginalSampleDescription;
bool m_OriginalSampleDescriptionIsOwned;
AP4_UI32 m_OriginalFormat;
AP4_UI32 m_SchemeType;
AP4_UI32 m_SchemeVersion;
AP4_String m_SchemeUri;
AP4_ProtectionSchemeInfo* m_SchemeInfo;
};
/*----------------------------------------------------------------------
| AP4_BlockCipher
+---------------------------------------------------------------------*/
class AP4_BlockCipher
{
public:
// types
typedef enum {
ENCRYPT,
DECRYPT
} CipherDirection;
typedef enum {
AES_128
} CipherType;
typedef enum {
CBC,
CTR
} CipherMode;
struct CtrParams {
AP4_Size counter_size;
};
// constructor and destructor
virtual ~AP4_BlockCipher() {}
// methods
virtual CipherDirection GetDirection() = 0;
virtual AP4_Result Process(const AP4_UI08* input,
AP4_Size input_size,
AP4_UI08* output,
const AP4_UI08* iv) = 0;
};
/*----------------------------------------------------------------------
| AP4_BlockCipherFactory
+---------------------------------------------------------------------*/
class AP4_BlockCipherFactory
{
public:
// types
struct CtrParams {
AP4_Size counter_size;
};
// methods
virtual ~AP4_BlockCipherFactory() {}
virtual AP4_Result CreateCipher(AP4_BlockCipher::CipherType type,
AP4_BlockCipher::CipherDirection direction,
AP4_BlockCipher::CipherMode mode,
const void* params,
const AP4_UI08* key,
AP4_Size key_size,
AP4_BlockCipher*& cipher) = 0;
};
/*----------------------------------------------------------------------
| AP4_DefaultBlockCipherFactory
+---------------------------------------------------------------------*/
class AP4_DefaultBlockCipherFactory : public AP4_BlockCipherFactory
{
public:
// class variables
static AP4_DefaultBlockCipherFactory Instance;
// this member is used to detect the situation where the
// platform's code loader does not construct static C++ objects
AP4_DefaultBlockCipherFactory();
bool m_Initialized;
AP4_Result Initialize();
// methods
virtual AP4_Result CreateCipher(AP4_BlockCipher::CipherType type,
AP4_BlockCipher::CipherDirection direction,
AP4_BlockCipher::CipherMode mode,
const void* params,
const AP4_UI08* key,
AP4_Size key_size,
AP4_BlockCipher*& cipher);
};
/*----------------------------------------------------------------------
| AP4_SampleDecrypter
+---------------------------------------------------------------------*/
class AP4_SampleDecrypter
{
public:
/**
* Create a sample decrypter given a protected sample description
*/
static AP4_SampleDecrypter* Create(AP4_ProtectedSampleDescription* sample_description,
const AP4_UI08* key,
AP4_Size key_size,
AP4_BlockCipherFactory* block_cipher_factory = NULL);
/**
* Create a fragment sample decrypter given a protected sample description and a track fragment
*/
static AP4_SampleDecrypter* Create(AP4_ProtectedSampleDescription* sample_description,
AP4_ContainerAtom* traf,
AP4_ByteStream& aux_info_data,
AP4_Position aux_info_data_offset,
const AP4_UI08* key,
AP4_Size key_size,
AP4_BlockCipherFactory* block_cipher_factory = NULL);
// destructor
virtual ~AP4_SampleDecrypter() {}
// methods
virtual AP4_Size GetDecryptedSampleSize(AP4_Sample& sample) { return sample.GetSize(); }
virtual AP4_Result SetSampleIndex(AP4_Ordinal /*index*/) { return AP4_SUCCESS; }
virtual AP4_Result DecryptSampleData(AP4_UI32 poolid,
AP4_DataBuffer& data_in,
AP4_DataBuffer& data_out,
const AP4_UI08* iv = NULL) = 0;
};
/*----------------------------------------------------------------------
| AP4_StandardDecryptingProcessor
+---------------------------------------------------------------------*/
class AP4_StandardDecryptingProcessor : public AP4_Processor
{
public:
// constructor
AP4_StandardDecryptingProcessor(const AP4_ProtectionKeyMap* key_map = NULL,
AP4_BlockCipherFactory* block_cipher_factory = NULL);
// accessors
AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
// methods
virtual AP4_Result Initialize(AP4_AtomParent& top_level,
AP4_ByteStream& stream,
ProgressListener* listener);
virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak, AP4_TrexAtom* trex);
private:
// members
AP4_BlockCipherFactory* m_BlockCipherFactory;
AP4_ProtectionKeyMap m_KeyMap;
};
/*----------------------------------------------------------------------
| AP4_DecryptingStream
+---------------------------------------------------------------------*/
class AP4_DecryptingStream : public AP4_ByteStream
{
public:
static AP4_Result Create(AP4_BlockCipher::CipherMode mode,
AP4_ByteStream& encrypted_stream,
AP4_LargeSize cleartext_size,
const AP4_UI08* iv,
AP4_Size iv_size,
const AP4_UI08* key,
AP4_Size key_size,
AP4_BlockCipherFactory* block_cipher_factory,
AP4_ByteStream*& stream);
// AP4_ByteStream methods
virtual AP4_Result ReadPartial(void* buffer,
AP4_Size bytes_to_read,
AP4_Size& bytes_read);
virtual AP4_Result WritePartial(const void* buffer,
AP4_Size bytes_to_write,
AP4_Size& bytes_written);
virtual AP4_Result Seek(AP4_Position position);
virtual AP4_Result Tell(AP4_Position& position);
virtual AP4_Result GetSize(AP4_LargeSize& size);
// AP4_Referenceable methods
virtual void AddReference();
virtual void Release();
private:
// private constructor, use the factory instead
AP4_DecryptingStream(AP4_LargeSize cleartext_size,
AP4_ByteStream* encrypted_stream,
AP4_LargeSize encrypted_size,
AP4_StreamCipher* stream_cipher) :
m_CleartextSize(cleartext_size),
m_CleartextPosition(0),
m_EncryptedStream(encrypted_stream),
m_EncryptedSize(encrypted_size),
m_EncryptedPosition(0),
m_StreamCipher(stream_cipher),
m_BufferFullness(0),
m_BufferOffset(0),
m_ReferenceCount(1)
{
AP4_SetMemory(m_Buffer, 0, sizeof(m_Buffer));
}
~AP4_DecryptingStream();
// members
AP4_LargeSize m_CleartextSize;
AP4_Position m_CleartextPosition;
AP4_ByteStream* m_EncryptedStream;
AP4_LargeSize m_EncryptedSize;
AP4_Position m_EncryptedPosition;
AP4_StreamCipher* m_StreamCipher;
AP4_UI08 m_Buffer[1024];
AP4_Size m_BufferFullness;
AP4_Size m_BufferOffset;
AP4_Cardinal m_ReferenceCount;
};
/*----------------------------------------------------------------------
| AP4_EncryptingStream
+---------------------------------------------------------------------*/
class AP4_EncryptingStream : public AP4_ByteStream
{
public:
static AP4_Result Create(AP4_BlockCipher::CipherMode mode,
AP4_ByteStream& cleartext_stream,
const AP4_UI08* iv,
AP4_Size iv_size,
const AP4_UI08* key,
AP4_Size key_size,
bool prepend_iv,
AP4_BlockCipherFactory* block_cipher_factory,
AP4_ByteStream*& stream);
// AP4_ByteStream methods
virtual AP4_Result ReadPartial(void* buffer,
AP4_Size bytes_to_read,
AP4_Size& bytes_read);
virtual AP4_Result WritePartial(const void* buffer,
AP4_Size bytes_to_write,
AP4_Size& bytes_written);
virtual AP4_Result Seek(AP4_Position position);
virtual AP4_Result Tell(AP4_Position& position);
virtual AP4_Result GetSize(AP4_LargeSize& size);
// AP4_Referenceable methods
virtual void AddReference();
virtual void Release();
private:
// private constructor, use the factory instead
AP4_EncryptingStream(AP4_LargeSize cleartext_size,
AP4_ByteStream* cleartext_stream,
AP4_LargeSize encrypted_size,
AP4_StreamCipher* stream_cipher) :
m_CleartextSize(cleartext_size),
m_CleartextPosition(0),
m_CleartextStream(cleartext_stream),
m_EncryptedSize(encrypted_size),
m_EncryptedPosition(0),
m_StreamCipher(stream_cipher),
m_BufferFullness(0),
m_BufferOffset(0),
m_ReferenceCount(1)
{
AP4_SetMemory(m_Buffer, 0, sizeof(m_Buffer));
}
~AP4_EncryptingStream();
// members
AP4_LargeSize m_CleartextSize;
AP4_Position m_CleartextPosition;
AP4_ByteStream* m_CleartextStream;
AP4_LargeSize m_EncryptedSize;
AP4_Position m_EncryptedPosition;
AP4_StreamCipher* m_StreamCipher;
AP4_UI08 m_Buffer[1024+16];
AP4_Size m_BufferFullness;
AP4_Size m_BufferOffset;
AP4_Cardinal m_ReferenceCount;
};
#endif // _AP4_PROTECTION_H_
|