- Main Page
- Classes
- Files

# Ap4OmaDcf.h

Go to the documentation of this file.

    00001 /*****************************************************************
    00002 |
    00003 |    AP4 - OMA DCF support
    00004 |
    00005 |    Copyright 2002-2006 Gilles Boccon-Gibod & Julien Boeuf
    00006 |
    00007 |
    00008 |    This file is part of Bento4/AP4 (MP4 Atom Processing Library).
    00009 |
    00010 |    Unless you have obtained Bento4 under a difference license,
    00011 |    this version of Bento4 is Bento4|GPL.
    00012 |    Bento4|GPL is free software; you can redistribute it and/or modify
    00013 |    it under the terms of the GNU General Public License as published by
    00014 |    the Free Software Foundation; either version 2, or (at your option)
    00015 |    any later version.
    00016 |
    00017 |    Bento4|GPL is distributed in the hope that it will be useful,
    00018 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00019 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00020 |    GNU General Public License for more details.
    00021 |
    00022 |    You should have received a copy of the GNU General Public License
    00023 |    along with Bento4|GPL; see the file COPYING.  If not, write to the
    00024 |    Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
    00025 |    02111-1307, USA.
    00026 |
    00027 ****************************************************************/
    00028
    00029 #ifndef _AP4_OMA_DCF_H_
    00030 #define _AP4_OMA_DCF_H_
    00031
    00032 /*----------------------------------------------------------------------
    00033 |   includes
    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 |   class references
    00045 +---------------------------------------------------------------------*/
    00046 class AP4_CtrStreamCipher;
    00047 class AP4_OdafAtom;
    00048 class AP4_StreamCipher;
    00049 class AP4_CbcStreamCipher;
    00050 class AP4_CtrStreamCipher;
    00051
    00052 /*----------------------------------------------------------------------
    00053 |   constants
    00054 +---------------------------------------------------------------------*/
    00055 const AP4_UI32 AP4_PROTECTION_SCHEME_TYPE_OMA = AP4_ATOM_TYPE('o','d','k','m');
    00056 const AP4_UI32 AP4_PROTECTION_SCHEME_VERSION_OMA_20 = 0x00000200;
    00057 const AP4_UI32 AP4_OMA_DCF_BRAND_ODCF = AP4_ATOM_TYPE('o','d','c','f');
    00058 const AP4_UI32 AP4_OMA_DCF_BRAND_OPF2 = AP4_ATOM_TYPE('o','p','f','2');
    00059
    00060 typedef enum {
    00061     AP4_OMA_DCF_CIPHER_MODE_CTR,
    00062     AP4_OMA_DCF_CIPHER_MODE_CBC
    00063 } AP4_OmaDcfCipherMode;
    00064
    00065 /*----------------------------------------------------------------------
    00066 |   AP4_OmaDcfAtomDecrypter
    00067 +---------------------------------------------------------------------*/
    00068 class AP4_OmaDcfAtomDecrypter {
    00069 public:
    00070     // class methods
    00071     static AP4_Result DecryptAtoms(AP4_AtomParent&                  atoms,
    00072                                    AP4_Processor::ProgressListener* listener,
    00073                                    AP4_BlockCipherFactory*          block_cipher_factory,
    00074                                    AP4_ProtectionKeyMap&            key_map);
    00075
    00076     // Returns a byte stream that will produce the decrypted data found
    00077     // in the 'odda' child atom of an 'odrm' atom
    00078     static AP4_Result CreateDecryptingStream(AP4_ContainerAtom&      odrm_atom,
    00079                                              const AP4_UI08*         key,
    00080                                              AP4_Size                key_size,
    00081                                              AP4_BlockCipherFactory* block_cipher_factory,
    00082                                              AP4_ByteStream*&        stream);
    00083
    00084     // Returns a byte stream that will produce the decrypted data from
    00085     // an encrypted stream where the IV follows the encrypted bytes.
    00086     // This method is normally not called directly: most callers will call
    00087     // the stream factory that takes an 'odrm' atom as an input parameter
    00088     static AP4_Result CreateDecryptingStream(AP4_OmaDcfCipherMode    mode,
    00089                                              AP4_ByteStream&         encrypted_stream,
    00090                                              AP4_LargeSize           cleartext_size,
    00091                                              const AP4_UI08*         key,
    00092                                              AP4_Size                key_size,
    00093                                              AP4_BlockCipherFactory* block_cipher_factory,
    00094                                              AP4_ByteStream*&        stream);
    00095 };
    00096
    00097 /*----------------------------------------------------------------------
    00098 |   AP4_OmaDcfSampleDecrypter
    00099 +---------------------------------------------------------------------*/
    00100 class AP4_OmaDcfSampleDecrypter : public AP4_SampleDecrypter
    00101 {
    00102 public:
    00103     // factory
    00104     static AP4_Result Create(AP4_ProtectedSampleDescription* sample_description,
    00105                              const AP4_UI08*                 key,
    00106                              AP4_Size                        key_size,
    00107                              AP4_BlockCipherFactory*         block_cipher_factory,
    00108                              AP4_OmaDcfSampleDecrypter*&     cipher);
    00109
    00110     // constructor and destructor
    00111     AP4_OmaDcfSampleDecrypter(AP4_Size iv_length,
    00112                               bool     selective_encryption) :
    00113         m_IvLength(iv_length),
    00114         m_SelectiveEncryption(selective_encryption) {}
    00115
    00116     // methods
    00117     virtual AP4_Size GetDecryptedSampleSize(AP4_Sample& sample) = 0;
    00118
    00119 protected:
    00120     AP4_Size m_IvLength;
    00121     AP4_Size m_KeyIndicatorLength;
    00122     bool     m_SelectiveEncryption;
    00123 };
    00124
    00125 /*----------------------------------------------------------------------
    00126 |   AP4_OmaDcfCtrSampleDecrypter
    00127 +---------------------------------------------------------------------*/
    00128 class AP4_OmaDcfCtrSampleDecrypter : public AP4_OmaDcfSampleDecrypter
    00129 {
    00130 public:
    00131     // constructor and destructor
    00132     AP4_OmaDcfCtrSampleDecrypter(AP4_BlockCipher* block_cipher,
    00133                                  AP4_Size         iv_length,
    00134                                  bool             selective_encryption);
    00135     ~AP4_OmaDcfCtrSampleDecrypter();
    00136
    00137     // methods
    00138     virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
    00139                                          AP4_DataBuffer& data_out);
    00140     virtual AP4_Size   GetDecryptedSampleSize(AP4_Sample& sample);
    00141
    00142 private:
    00143     // members
    00144     AP4_CtrStreamCipher* m_Cipher;
    00145 };
    00146
    00147 /*----------------------------------------------------------------------
    00148 |   AP4_OmaDcfCbcSampleDecrypter
    00149 +---------------------------------------------------------------------*/
    00150 class AP4_OmaDcfCbcSampleDecrypter : public AP4_OmaDcfSampleDecrypter
    00151 {
    00152 public:
    00153     // constructor and destructor
    00154     AP4_OmaDcfCbcSampleDecrypter(AP4_BlockCipher* block_cipher,
    00155                                  bool             selective_encryption);
    00156     ~AP4_OmaDcfCbcSampleDecrypter();
    00157
    00158     // methods
    00159     virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
    00160                                          AP4_DataBuffer& data_out);
    00161     virtual AP4_Size   GetDecryptedSampleSize(AP4_Sample& sample);
    00162
    00163 private:
    00164     // members
    00165     AP4_CbcStreamCipher* m_Cipher;
    00166 };
    00167
    00168 /*----------------------------------------------------------------------
    00169 |   AP4_OmaDcfTrackDecrypter
    00170 +---------------------------------------------------------------------*/
    00171 class AP4_OmaDcfTrackDecrypter : public AP4_Processor::TrackHandler {
    00172 public:
    00173     // constructor
    00174     static AP4_Result Create(const AP4_UI08*                 key,
    00175                              AP4_Size                        key_size,
    00176                              AP4_ProtectedSampleDescription* sample_description,
    00177                              AP4_SampleEntry*                sample_entry,
    00178                              AP4_BlockCipherFactory*         block_cipher_factory,
    00179                              AP4_OmaDcfTrackDecrypter*&      decrypter);
    00180     virtual ~AP4_OmaDcfTrackDecrypter();
    00181
    00182     // methods
    00183     virtual AP4_Size   GetProcessedSampleSize(AP4_Sample& sample);
    00184     virtual AP4_Result ProcessTrack();
    00185     virtual AP4_Result ProcessSample(AP4_DataBuffer& data_in,
    00186                                      AP4_DataBuffer& data_out);
    00187
    00188 private:
    00189     // constructor
    00190     AP4_OmaDcfTrackDecrypter(AP4_OmaDcfSampleDecrypter* cipher,
    00191                              AP4_SampleEntry*           sample_entry,
    00192                              AP4_UI32                   original_format);
    00193
    00194     // members
    00195     AP4_OmaDcfSampleDecrypter* m_Cipher;
    00196     AP4_SampleEntry*           m_SampleEntry;
    00197     AP4_UI32                   m_OriginalFormat;
    00198 };
    00199
    00200 /*----------------------------------------------------------------------
    00201 |   AP4_OmaDcfSampleEncrypter
    00202 +---------------------------------------------------------------------*/
    00203 class AP4_OmaDcfSampleEncrypter
    00204 {
    00205 public:
    00206     // constructor and destructor
    00207     AP4_OmaDcfSampleEncrypter(const AP4_UI08* salt);
    00208     virtual ~AP4_OmaDcfSampleEncrypter() {}
    00209
    00210     // methods
    00211     virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
    00212                                          AP4_DataBuffer& data_out,
    00213                                          AP4_UI64        bso,
    00214                                          bool            skip_encryption) = 0;
    00215     virtual AP4_Size   GetEncryptedSampleSize(AP4_Sample& sample) = 0;
    00216
    00217 protected:
    00218     // members
    00219     AP4_UI08 m_Salt[16];
    00220 };
    00221
    00222 /*----------------------------------------------------------------------
    00223 |   AP4_OmaDcfCtrSampleEncrypter
    00224 +---------------------------------------------------------------------*/
    00225 class AP4_OmaDcfCtrSampleEncrypter : public AP4_OmaDcfSampleEncrypter
    00226 {
    00227 public:
    00228     // constructor and destructor
    00229     AP4_OmaDcfCtrSampleEncrypter(AP4_BlockCipher* block_cipher,
    00230                                  const AP4_UI08*  salt);
    00231     ~AP4_OmaDcfCtrSampleEncrypter();
    00232
    00233     // methods
    00234     virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
    00235                                          AP4_DataBuffer& data_out,
    00236                                          AP4_UI64        bso,
    00237                                          bool            skip_encryption);
    00238     virtual AP4_Size   GetEncryptedSampleSize(AP4_Sample& sample);
    00239
    00240 private:
    00241     // members
    00242     AP4_CtrStreamCipher* m_Cipher;
    00243 };
    00244
    00245 /*----------------------------------------------------------------------
    00246 |   AP4_OmaDcfCbcSampleEncrypter
    00247 +---------------------------------------------------------------------*/
    00248 class AP4_OmaDcfCbcSampleEncrypter : public AP4_OmaDcfSampleEncrypter
    00249 {
    00250 public:
    00251     // constructor and destructor
    00252     AP4_OmaDcfCbcSampleEncrypter(AP4_BlockCipher* block_cipher,
    00253                                  const AP4_UI08*  salt);
    00254     ~AP4_OmaDcfCbcSampleEncrypter();
    00255
    00256     // methods
    00257     virtual AP4_Result EncryptSampleData(AP4_DataBuffer& data_in,
    00258                                          AP4_DataBuffer& data_out,
    00259                                          AP4_UI64        bso,
    00260                                          bool            skip_encryption);
    00261     virtual AP4_Size   GetEncryptedSampleSize(AP4_Sample& sample);
    00262
    00263 private:
    00264     // members
    00265     AP4_CbcStreamCipher* m_Cipher;
    00266 };
    00267
    00268 /*----------------------------------------------------------------------
    00269 |   AP4_TrackPropertyMap
    00270 +---------------------------------------------------------------------*/
    00271 class AP4_TrackPropertyMap
    00272 {
    00273 public:
    00274     // methods
    00275     AP4_Result  SetProperty(AP4_UI32 track_id, const char* name, const char* value);
    00276     AP4_Result  SetProperties(const AP4_TrackPropertyMap& properties);
    00277     const char* GetProperty(AP4_UI32 track_id, const char* name);
    00278     AP4_Result  GetTextualHeaders(AP4_UI32 track_id, AP4_DataBuffer& buffer);
    00279
    00280
    00281     // destructor
    00282     virtual ~AP4_TrackPropertyMap();
    00283
    00284 private:
    00285     // types
    00286     class Entry {
    00287     public:
    00288         Entry(AP4_UI32 track_id, const char* name, const char* value) :
    00289           m_TrackId(track_id), m_Name(name), m_Value(value) {}
    00290         AP4_UI32   m_TrackId;
    00291         AP4_String m_Name;
    00292         AP4_String m_Value;
    00293     };
    00294
    00295     // members
    00296     AP4_List<Entry> m_Entries;
    00297 };
    00298
    00299 /*----------------------------------------------------------------------
    00300 |   AP4_OmaDcfDecryptingProcessor
    00301 +---------------------------------------------------------------------*/
    00306 class AP4_OmaDcfDecryptingProcessor : public AP4_Processor
    00307 {
    00308 public:
    00309     // constructor
    00310     AP4_OmaDcfDecryptingProcessor(const AP4_ProtectionKeyMap* key_map = NULL,
    00311                                   AP4_BlockCipherFactory*     block_cipher_factory = NULL);
    00312
    00313     // accessors
    00314     AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
    00315
    00316     // methods
    00317     virtual AP4_Result Initialize(AP4_AtomParent&   top_level,
    00318                                   AP4_ByteStream&   stream,
    00319                                   ProgressListener* listener);
    00320
    00321 private:
    00322     // members
    00323     AP4_BlockCipherFactory* m_BlockCipherFactory;
    00324     AP4_ProtectionKeyMap    m_KeyMap;
    00325 };
    00326
    00327 /*----------------------------------------------------------------------
    00328 |   AP4_OmaDcfEncryptingProcessor
    00329 +---------------------------------------------------------------------*/
    00330 class AP4_OmaDcfEncryptingProcessor : public AP4_Processor
    00331 {
    00332 public:
    00333     // constructor
    00334     AP4_OmaDcfEncryptingProcessor(AP4_OmaDcfCipherMode    cipher_mode,
    00335                                   AP4_BlockCipherFactory* block_cipher_factory = NULL);
    00336
    00337     // accessors
    00338     AP4_ProtectionKeyMap& GetKeyMap()      { return m_KeyMap;      }
    00339     AP4_TrackPropertyMap& GetPropertyMap() { return m_PropertyMap; }
    00340
    00341     // AP4_Processor methods
    00342     virtual AP4_Result Initialize(AP4_AtomParent&   top_level,
    00343                                   AP4_ByteStream&   stream,
    00344                                   AP4_Processor::ProgressListener* listener = NULL);
    00345     virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
    00346
    00347 private:
    00348     // members
    00349     AP4_OmaDcfCipherMode    m_CipherMode;
    00350     AP4_BlockCipherFactory* m_BlockCipherFactory;
    00351     AP4_ProtectionKeyMap    m_KeyMap;
    00352     AP4_TrackPropertyMap    m_PropertyMap;
    00353 };
    00354
    00355 #endif // _AP4_OMA_DCF_H_