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_OMA_DCF_H_
00030 #define _AP4_OMA_DCF_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 #include "Ap4Protection.h"
00042
00043
00044
00045
00046 class AP4_CtrStreamCipher;
00047 class AP4_OdafAtom;
00048 class AP4_CbcStreamCipher;
00049 class AP4_CtrStreamCipher;
00050
00051
00052
00053
00054 const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_OMA = AP4_ATOM_TYPE('o','d','k','m');
00055 const AP4_UI32 AP4_PROTECTION_SCHEME_VERSION_OMA_20 = 0x00000200;
00056 const AP4_UI32 AP4_OMA_DCF_BRAND_ODCF = AP4_ATOM_TYPE('o','d','c','f');
00057
00058 typedef enum {
00059 AP4_OMA_DCF_CIPHER_MODE_CTR,
00060 AP4_OMA_DCF_CIPHER_MODE_CBC
00061 } AP4_OmaDcfCipherMode;
00062
00063
00064
00065
00066 class AP4_OmaCbcDecryptingStream : public AP4_ByteStream {
00067 public:
00068 static AP4_Result Create(
00069 AP4_ByteStream* source_stream,
00070 AP4_Position source_position,
00071 const AP4_UI08* key,
00072 AP4_Size key_size,
00073 AP4_BlockCipherFactory* block_cipher_factory,
00074 AP4_LargeSize cleartext_size,
00075 AP4_OmaCbcDecryptingStream** stream);
00076 ~AP4_OmaCbcDecryptingStream();
00077
00078
00079 virtual AP4_Result Read(void* buffer,
00080 AP4_Size bytes_to_read,
00081 AP4_Size* bytes_read);
00082 virtual AP4_Result Write(const void* buffer,
00083 AP4_Size bytes_to_write,
00084 AP4_Size* bytes_written);
00085 virtual AP4_Result Seek(AP4_Position position);
00086 virtual AP4_Result Tell(AP4_Position& position);
00087 virtual AP4_Result GetSize(AP4_LargeSize& size);
00088
00089
00090 virtual void AddReference();
00091 virtual void Release();
00092
00093 private:
00094
00095 AP4_OmaCbcDecryptingStream() {}
00096
00097
00098 AP4_LargeSize m_Size;
00099 AP4_Position m_Position;
00100 AP4_ByteStream* m_SourceStream;
00101 AP4_Position m_SourceStart;
00102 AP4_Position m_SourcePosition;
00103 AP4_CbcStreamCipher* m_StreamCipher;
00104 AP4_UI08 m_Buffer[16];
00105 AP4_Size m_BufferFullness;
00106 AP4_Size m_BufferOffset;
00107 AP4_Cardinal m_ReferenceCount;
00108 };
00109
00110
00111
00112
00113 class AP4_OmaDcfAtomDecrypter {
00114 public:
00115
00116 static AP4_Result DecryptAtoms(AP4_AtomParent& atoms,
00117 AP4_Processor::ProgressListener* listener,
00118 AP4_BlockCipherFactory* block_cipher_factory,
00119 AP4_ProtectionKeyMap& key_map);
00120
00121 static AP4_Result CreateDecryptingStream(AP4_ContainerAtom& odrm_atom,
00122 const AP4_UI08* key,
00123 AP4_Size key_size,
00124 AP4_BlockCipherFactory* block_cipher_factory,
00125 AP4_ByteStream** stream);
00126 };
00127
00128
00129
00130
00131 class AP4_OmaDcfSampleDecrypter : public AP4_SampleDecrypter
00132 {
00133 public:
00134
00135 static AP4_Result Create(AP4_ProtectedSampleDescription* sample_description,
00136 const AP4_UI08* key,
00137 AP4_Size key_size,
00138 AP4_BlockCipherFactory* block_cipher_factory,
00139 AP4_OmaDcfSampleDecrypter** cipher);
00140
00141
00142 AP4_OmaDcfSampleDecrypter(AP4_Size iv_length,
00143 bool selective_encryption) :
00144 m_IvLength(iv_length),
00145 m_SelectiveEncryption(selective_encryption) {}
00146
00147
00148 virtual AP4_Size GetDecryptedSampleSize(AP4_Sample& sample) = 0;
00149
00150 protected:
00151 AP4_Size m_IvLength;
00152 AP4_Size m_KeyIndicatorLength;
00153 bool m_SelectiveEncryption;
00154 };
00155
00156
00157
00158
00159 class AP4_OmaDcfCtrSampleDecrypter : public AP4_OmaDcfSampleDecrypter
00160 {
00161 public:
00162
00163 AP4_OmaDcfCtrSampleDecrypter(AP4_BlockCipher* block_cipher,
00164 AP4_Size iv_length,
00165 bool selective_encryption);
00166 ~AP4_OmaDcfCtrSampleDecrypter();
00167
00168
00169 virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
00170 AP4_DataBuffer& data_out);
00171 virtual AP4_Size GetDecryptedSampleSize(AP4_Sample& sample);
00172
00173 private:
00174
00175 AP4_CtrStreamCipher* m_Cipher;
00176 };
00177
00178
00179
00180
00181 class AP4_OmaDcfCbcSampleDecrypter : public AP4_OmaDcfSampleDecrypter
00182 {
00183 public:
00184
00185 AP4_OmaDcfCbcSampleDecrypter(AP4_BlockCipher* block_cipher,
00186 bool selective_encryption);
00187 ~AP4_OmaDcfCbcSampleDecrypter();
00188
00189
00190 virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
00191 AP4_DataBuffer& data_out);
00192 virtual AP4_Size GetDecryptedSampleSize(AP4_Sample& sample);
00193
00194 private:
00195
00196 AP4_CbcStreamCipher* m_Cipher;
00197 };
00198
00199
00200
00201
00202 class AP4_OmaDcfTrackDecrypter : public AP4_Processor::TrackHandler {
00203 public:
00204
00205 static AP4_Result Create(const AP4_UI08* key,
00206 AP4_Size key_size,
00207 AP4_ProtectedSampleDescription* sample_description,
00208 AP4_SampleEntry* sample_entry,
00209 AP4_BlockCipherFactory* block_cipher_factory,
00210 AP4_OmaDcfTrackDecrypter** decrypter);
00211 virtual ~AP4_OmaDcfTrackDecrypter();
00212
00213
00214 virtual AP4_Size GetProcessedSampleSize(AP4_Sample& sample);
00215 virtual AP4_Result ProcessTrack();
00216 virtual AP4_Result ProcessSample(AP4_DataBuffer& data_in,
00217 AP4_DataBuffer& data_out);
00218
00219 private:
00220
00221 AP4_OmaDcfTrackDecrypter(AP4_OmaDcfSampleDecrypter* cipher,
00222 AP4_SampleEntry* sample_entry,
00223 AP4_UI32 original_format);
00224
00225
00226 AP4_OmaDcfSampleDecrypter* m_Cipher;
00227 AP4_SampleEntry* m_SampleEntry;
00228 AP4_UI32 m_OriginalFormat;
00229 };
00230
00231
00232
00233
00234 class AP4_OmaDcfSampleEncrypter
00235 {
00236 public:
00237
00238 AP4_OmaDcfSampleEncrypter(const AP4_UI08* salt);
00239 virtual ~AP4_OmaDcfSampleEncrypter() {}
00240
00241
00242 virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
00243 AP4_DataBuffer& data_out,
00244 AP4_UI64 bso,
00245 bool skip_encryption) = 0;
00246 virtual AP4_Size GetEncryptedSampleSize(AP4_Sample& sample) = 0;
00247
00248 protected:
00249
00250 AP4_UI08 m_Salt[16];
00251 };
00252
00253
00254
00255
00256 class AP4_OmaDcfCtrSampleEncrypter : public AP4_OmaDcfSampleEncrypter
00257 {
00258 public:
00259
00260 AP4_OmaDcfCtrSampleEncrypter(AP4_BlockCipher* block_cipher,
00261 const AP4_UI08* salt);
00262 ~AP4_OmaDcfCtrSampleEncrypter();
00263
00264
00265 virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
00266 AP4_DataBuffer& data_out,
00267 AP4_UI64 bso,
00268 bool skip_encryption);
00269 virtual AP4_Size GetEncryptedSampleSize(AP4_Sample& sample);
00270
00271 private:
00272
00273 AP4_CtrStreamCipher* m_Cipher;
00274 };
00275
00276
00277
00278
00279 class AP4_OmaDcfCbcSampleEncrypter : public AP4_OmaDcfSampleEncrypter
00280 {
00281 public:
00282
00283 AP4_OmaDcfCbcSampleEncrypter(AP4_BlockCipher* block_cipher,
00284 const AP4_UI08* salt);
00285 ~AP4_OmaDcfCbcSampleEncrypter();
00286
00287
00288 virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
00289 AP4_DataBuffer& data_out,
00290 AP4_UI64 bso,
00291 bool skip_encryption);
00292 virtual AP4_Size GetEncryptedSampleSize(AP4_Sample& sample);
00293
00294 private:
00295
00296 AP4_CbcStreamCipher* m_Cipher;
00297 };
00298
00299
00300
00301
00302 class AP4_TrackPropertyMap
00303 {
00304 public:
00305
00306 AP4_Result SetProperty(AP4_UI32 track_id, const char* name, const char* value);
00307 AP4_Result SetProperties(const AP4_TrackPropertyMap& properties);
00308 const char* GetProperty(AP4_UI32 track_id, const char* name);
00309
00310
00311 virtual ~AP4_TrackPropertyMap();
00312
00313 private:
00314
00315 class Entry {
00316 public:
00317 Entry(AP4_UI32 track_id, const char* name, const char* value) :
00318 m_TrackId(track_id), m_Name(name), m_Value(value) {}
00319 AP4_UI32 m_TrackId;
00320 AP4_String m_Name;
00321 AP4_String m_Value;
00322 };
00323
00324
00325 AP4_List<Entry> m_Entries;
00326 };
00327
00328
00329
00330
00331 class AP4_OmaDcfEncryptingProcessor : public AP4_Processor
00332 {
00333 public:
00334
00335 AP4_OmaDcfEncryptingProcessor(AP4_OmaDcfCipherMode cipher_mode,
00336 AP4_BlockCipherFactory* block_cipher_factory = NULL);
00337
00338
00339 AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
00340 AP4_TrackPropertyMap& GetPropertyMap() { return m_PropertyMap; }
00341
00342
00343 virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
00344
00345 private:
00346
00347 AP4_OmaDcfCipherMode m_CipherMode;
00348 AP4_BlockCipherFactory* m_BlockCipherFactory;
00349 AP4_ProtectionKeyMap m_KeyMap;
00350 AP4_TrackPropertyMap m_PropertyMap;
00351 };
00352
00353 #endif // _AP4_OMA_DCF_H_