00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _AP4_PROTECTION_H_
00030 #define _AP4_PROTECTION_H_
00031
00032
00033
00034
00035 #include "Ap4Types.h"
00036 #include "Ap4SampleEntry.h"
00037 #include "Ap4Atom.h"
00038 #include "Ap4AtomFactory.h"
00039 #include "Ap4SampleDescription.h"
00040 #include "Ap4Processor.h"
00041
00042
00043
00044
00045 class AP4_StreamCipher;
00046
00047
00048
00049
00050
00051 const unsigned int AP4_PROTECTION_KEY_LENGTH = 16;
00052
00053 const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_ITUNES = AP4_ATOM_TYPE('i','t','u','n');
00054
00055
00056
00057
00058 class AP4_EncaSampleEntry : public AP4_AudioSampleEntry
00059 {
00060 public:
00061
00062 AP4_EncaSampleEntry(AP4_Size size,
00063 AP4_ByteStream& stream,
00064 AP4_AtomFactory& atom_factory);
00065 AP4_EncaSampleEntry(AP4_UI32 type,
00066 AP4_Size size,
00067 AP4_ByteStream& stream,
00068 AP4_AtomFactory& atom_factory);
00069
00070
00071 AP4_SampleDescription* ToSampleDescription();
00072 };
00073
00074
00075
00076
00077 class AP4_EncvSampleEntry : public AP4_VisualSampleEntry
00078 {
00079 public:
00080
00081 AP4_EncvSampleEntry(AP4_Size size,
00082 AP4_ByteStream& stream,
00083 AP4_AtomFactory& atom_factory);
00084 AP4_EncvSampleEntry(AP4_UI32 type,
00085 AP4_Size size,
00086 AP4_ByteStream& stream,
00087 AP4_AtomFactory& atom_factory);
00088
00089
00090 AP4_SampleDescription* ToSampleDescription();
00091 };
00092
00093
00094
00095
00096 class AP4_DrmsSampleEntry : public AP4_EncaSampleEntry
00097 {
00098 public:
00099
00100 AP4_DrmsSampleEntry(AP4_Size size,
00101 AP4_ByteStream& stream,
00102 AP4_AtomFactory& atom_factory);
00103 };
00104
00105
00106
00107
00108 class AP4_DrmiSampleEntry : public AP4_EncvSampleEntry
00109 {
00110 public:
00111
00112 AP4_DrmiSampleEntry(AP4_Size size,
00113 AP4_ByteStream& stream,
00114 AP4_AtomFactory& atom_factory);
00115 };
00116
00117
00118
00119
00120 class AP4_ProtectionKeyMap
00121 {
00122 public:
00123
00124 AP4_ProtectionKeyMap();
00125 ~AP4_ProtectionKeyMap();
00126
00127
00128 AP4_Result SetKey(AP4_UI32 track_id, const AP4_UI08* key, const AP4_UI08* iv = NULL);
00129 AP4_Result SetKeys(const AP4_ProtectionKeyMap& key_map);
00130 AP4_Result GetKeyAndIv(AP4_UI32 track_id, const AP4_UI08*& key, const AP4_UI08*& iv);
00131 const AP4_UI08* GetKey(AP4_UI32 track_id) const;
00132
00133 private:
00134
00135 class KeyEntry {
00136 public:
00137 KeyEntry(AP4_UI32 track_id, const AP4_UI08* key, const AP4_UI08* iv = NULL);
00138 void SetKey(const AP4_UI08* key, const AP4_UI08* iv);
00139 AP4_Ordinal m_TrackId;
00140 AP4_UI08 m_Key[AP4_PROTECTION_KEY_LENGTH];
00141 AP4_UI08 m_IV[AP4_PROTECTION_KEY_LENGTH];
00142 };
00143
00144
00145 KeyEntry* GetEntry(AP4_UI32 track_id) const;
00146
00147
00148 AP4_List<KeyEntry> m_KeyEntries;
00149 };
00150
00151
00152
00153
00154 class AP4_ProtectionSchemeInfo
00155 {
00156 public:
00157
00158 AP4_ProtectionSchemeInfo(AP4_ContainerAtom* schi);
00159 virtual ~AP4_ProtectionSchemeInfo();
00160
00161
00162 AP4_ContainerAtom* GetSchiAtom() { return m_SchiAtom; }
00163
00164 protected:
00165 AP4_ContainerAtom* m_SchiAtom;
00166 };
00167
00168
00169
00170
00171 class AP4_ProtectedSampleDescription : public AP4_SampleDescription
00172 {
00173 public:
00174
00175 AP4_ProtectedSampleDescription(AP4_UI32 format,
00176 AP4_SampleDescription* original_sample_description,
00177 AP4_UI32 original_format,
00178 AP4_UI32 scheme_type,
00179 AP4_UI32 scheme_version,
00180 const char* scheme_uri,
00181 AP4_ContainerAtom* schi_atom);
00182 ~AP4_ProtectedSampleDescription();
00183
00184
00185 AP4_SampleDescription* GetOriginalSampleDescription() {
00186 return m_OriginalSampleDescription;
00187 }
00188 AP4_UI32 GetOriginalFormat() const { return m_OriginalFormat; }
00189 AP4_UI32 GetSchemeType() const { return m_SchemeType; }
00190 AP4_UI32 GetSchemeVersion() const { return m_SchemeVersion; }
00191 const AP4_String& GetSchemeUri() const { return m_SchemeUri; }
00192 AP4_ProtectionSchemeInfo* GetSchemeInfo() const {
00193 return m_SchemeInfo;
00194 }
00195
00196
00197 virtual AP4_Atom* ToAtom() const;
00198
00199 private:
00200
00201 AP4_SampleDescription* m_OriginalSampleDescription;
00202 AP4_UI32 m_OriginalFormat;
00203 AP4_UI32 m_SchemeType;
00204 AP4_UI32 m_SchemeVersion;
00205 AP4_String m_SchemeUri;
00206 AP4_ProtectionSchemeInfo* m_SchemeInfo;
00207 };
00208
00209
00210
00211
00212 class AP4_BlockCipher
00213 {
00214 public:
00215
00216 typedef enum {
00217 ENCRYPT,
00218 DECRYPT
00219 } CipherDirection;
00220
00221 typedef enum {
00222 AES_128
00223 } CipherType;
00224
00225
00226 virtual ~AP4_BlockCipher() {}
00227
00228
00229 virtual AP4_Result ProcessBlock(const AP4_UI08* block_in, AP4_UI08* block_out) = 0;
00230 };
00231
00232
00233
00234
00235 class AP4_BlockCipherFactory
00236 {
00237 public:
00238
00239 virtual ~AP4_BlockCipherFactory() {}
00240 virtual AP4_Result Create(AP4_BlockCipher::CipherType type,
00241 AP4_BlockCipher::CipherDirection direction,
00242 const AP4_UI08* key,
00243 AP4_Size key_size,
00244 AP4_BlockCipher*& cipher) = 0;
00245 };
00246
00247
00248
00249
00250 class AP4_DefaultBlockCipherFactory : public AP4_BlockCipherFactory
00251 {
00252 public:
00253
00254 static AP4_DefaultBlockCipherFactory Instance;
00255
00256
00257 virtual AP4_Result Create(AP4_BlockCipher::CipherType type,
00258 AP4_BlockCipher::CipherDirection direction,
00259 const AP4_UI08* key,
00260 AP4_Size key_size,
00261 AP4_BlockCipher*& cipher);
00262 };
00263
00264
00265
00266
00267 class AP4_SampleDecrypter
00268 {
00269 public:
00270
00271 static AP4_SampleDecrypter* Create(AP4_ProtectedSampleDescription* sample_description,
00272 const AP4_UI08* key,
00273 AP4_Size key_size,
00274 AP4_BlockCipherFactory* block_cipher_factory = NULL);
00275
00276
00277 virtual ~AP4_SampleDecrypter() {}
00278
00279
00280 virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
00281 AP4_DataBuffer& data_out) = 0;
00282 };
00283
00284
00285
00286
00287 class AP4_StandardDecryptingProcessor : public AP4_Processor
00288 {
00289 public:
00290
00291 AP4_StandardDecryptingProcessor(const AP4_ProtectionKeyMap* key_map = NULL,
00292 AP4_BlockCipherFactory* block_cipher_factory = NULL);
00293
00294
00295 AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
00296
00297
00298 virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
00299
00300 private:
00301
00302 AP4_BlockCipherFactory* m_BlockCipherFactory;
00303 AP4_ProtectionKeyMap m_KeyMap;
00304 };
00305
00306
00307
00308
00309 class AP4_DecryptingStream : public AP4_ByteStream {
00310 public:
00311 typedef enum {
00312 CIPHER_MODE_CTR,
00313 CIPHER_MODE_CBC
00314 } CipherMode;
00315
00316 static AP4_Result Create(CipherMode mode,
00317 AP4_ByteStream& encrypted_stream,
00318 AP4_LargeSize cleartext_size,
00319 const AP4_UI08* iv,
00320 AP4_Size iv_size,
00321 const AP4_UI08* key,
00322 AP4_Size key_size,
00323 AP4_BlockCipherFactory* block_cipher_factory,
00324 AP4_ByteStream*& stream);
00325
00326
00327 virtual AP4_Result ReadPartial(void* buffer,
00328 AP4_Size bytes_to_read,
00329 AP4_Size& bytes_read);
00330 virtual AP4_Result WritePartial(const void* buffer,
00331 AP4_Size bytes_to_write,
00332 AP4_Size& bytes_written);
00333 virtual AP4_Result Seek(AP4_Position position);
00334 virtual AP4_Result Tell(AP4_Position& position);
00335 virtual AP4_Result GetSize(AP4_LargeSize& size);
00336
00337
00338 virtual void AddReference();
00339 virtual void Release();
00340
00341 private:
00342
00343 AP4_DecryptingStream() {}
00344 ~AP4_DecryptingStream();
00345
00346
00347 CipherMode m_Mode;
00348 AP4_LargeSize m_CleartextSize;
00349 AP4_Position m_CleartextPosition;
00350 AP4_ByteStream* m_EncryptedStream;
00351 AP4_LargeSize m_EncryptedSize;
00352 AP4_Position m_EncryptedPosition;
00353 AP4_StreamCipher* m_StreamCipher;
00354 AP4_UI08 m_Buffer[16];
00355 AP4_Size m_BufferFullness;
00356 AP4_Size m_BufferOffset;
00357 AP4_Cardinal m_ReferenceCount;
00358 };
00359
00360
00361
00362
00363 class AP4_EncryptingStream : public AP4_ByteStream {
00364 public:
00365 typedef enum {
00366 CIPHER_MODE_CTR,
00367 CIPHER_MODE_CBC
00368 } CipherMode;
00369
00370 static AP4_Result Create(CipherMode mode,
00371 AP4_ByteStream& cleartext_stream,
00372 const AP4_UI08* iv,
00373 AP4_Size iv_size,
00374 const AP4_UI08* key,
00375 AP4_Size key_size,
00376 bool prepend_iv,
00377 AP4_BlockCipherFactory* block_cipher_factory,
00378 AP4_ByteStream*& stream);
00379
00380
00381 virtual AP4_Result ReadPartial(void* buffer,
00382 AP4_Size bytes_to_read,
00383 AP4_Size& bytes_read);
00384 virtual AP4_Result WritePartial(const void* buffer,
00385 AP4_Size bytes_to_write,
00386 AP4_Size& bytes_written);
00387 virtual AP4_Result Seek(AP4_Position position);
00388 virtual AP4_Result Tell(AP4_Position& position);
00389 virtual AP4_Result GetSize(AP4_LargeSize& size);
00390
00391
00392 virtual void AddReference();
00393 virtual void Release();
00394
00395 private:
00396
00397 AP4_EncryptingStream() {}
00398 ~AP4_EncryptingStream();
00399
00400
00401 CipherMode m_Mode;
00402 AP4_LargeSize m_CleartextSize;
00403 AP4_Position m_CleartextPosition;
00404 AP4_ByteStream* m_CleartextStream;
00405 AP4_LargeSize m_EncryptedSize;
00406 AP4_Position m_EncryptedPosition;
00407 AP4_StreamCipher* m_StreamCipher;
00408 AP4_UI08 m_Buffer[16];
00409 AP4_Size m_BufferFullness;
00410 AP4_Size m_BufferOffset;
00411 AP4_Cardinal m_ReferenceCount;
00412 };
00413
00414
00415 #endif // _AP4_PROTECTION_H_