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 & 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_CbcStreamCipher;
00049 class AP4_CtrStreamCipher;
00050 
00051 /*----------------------------------------------------------------------
00052 |   constants
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 |   AP4_OmaCbcDecryptingStream
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     // AP4_ByteStream metods
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     // AP4_Referenceable methods
00090     virtual void AddReference();
00091     virtual void Release();
00092 
00093 private:
00094     // methods
00095     AP4_OmaCbcDecryptingStream() {} // use the factory instead
00096 
00097     // members
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 |   AP4_OmaDcfAtomDecrypter
00112 +---------------------------------------------------------------------*/
00113 class AP4_OmaDcfAtomDecrypter {
00114 public:
00115     // class methods
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 |   AP4_OmaDcfSampleDecrypter
00130 +---------------------------------------------------------------------*/
00131 class AP4_OmaDcfSampleDecrypter : public AP4_SampleDecrypter
00132 {
00133 public:
00134     // factory
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     // constructor and destructor
00142     AP4_OmaDcfSampleDecrypter(AP4_Size iv_length,
00143                               bool     selective_encryption) :
00144         m_IvLength(iv_length),
00145         m_SelectiveEncryption(selective_encryption) {}
00146 
00147     // methods
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 |   AP4_OmaDcfCtrSampleDecrypter
00158 +---------------------------------------------------------------------*/
00159 class AP4_OmaDcfCtrSampleDecrypter : public AP4_OmaDcfSampleDecrypter
00160 {
00161 public:
00162     // constructor and destructor
00163     AP4_OmaDcfCtrSampleDecrypter(AP4_BlockCipher* block_cipher,
00164                                  AP4_Size         iv_length,
00165                                  bool             selective_encryption);
00166     ~AP4_OmaDcfCtrSampleDecrypter();
00167 
00168     // methods
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     // members
00175     AP4_CtrStreamCipher* m_Cipher;
00176 };
00177 
00178 /*----------------------------------------------------------------------
00179 |   AP4_OmaDcfCbcSampleDecrypter
00180 +---------------------------------------------------------------------*/
00181 class AP4_OmaDcfCbcSampleDecrypter : public AP4_OmaDcfSampleDecrypter
00182 {
00183 public:
00184     // constructor and destructor
00185     AP4_OmaDcfCbcSampleDecrypter(AP4_BlockCipher* block_cipher,
00186                                  bool             selective_encryption);
00187     ~AP4_OmaDcfCbcSampleDecrypter();
00188 
00189     // methods
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     // members
00196     AP4_CbcStreamCipher* m_Cipher;
00197 };
00198 
00199 /*----------------------------------------------------------------------
00200 |   AP4_OmaDcfTrackDecrypter
00201 +---------------------------------------------------------------------*/
00202 class AP4_OmaDcfTrackDecrypter : public AP4_Processor::TrackHandler {
00203 public:
00204     // constructor
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     // methods
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     // constructor
00221     AP4_OmaDcfTrackDecrypter(AP4_OmaDcfSampleDecrypter* cipher,
00222                              AP4_SampleEntry*           sample_entry,
00223                              AP4_UI32                   original_format);
00224 
00225     // members
00226     AP4_OmaDcfSampleDecrypter* m_Cipher;
00227     AP4_SampleEntry*           m_SampleEntry;
00228     AP4_UI32                   m_OriginalFormat;
00229 };
00230 
00231 /*----------------------------------------------------------------------
00232 |   AP4_OmaDcfSampleEncrypter
00233 +---------------------------------------------------------------------*/
00234 class AP4_OmaDcfSampleEncrypter
00235 {
00236 public:
00237     // constructor and destructor
00238     AP4_OmaDcfSampleEncrypter(const AP4_UI08* salt);
00239     virtual ~AP4_OmaDcfSampleEncrypter() {}
00240 
00241     // methods
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     // members
00250     AP4_UI08 m_Salt[16];
00251 };
00252 
00253 /*----------------------------------------------------------------------
00254 |   AP4_OmaDcfCtrSampleEncrypter
00255 +---------------------------------------------------------------------*/
00256 class AP4_OmaDcfCtrSampleEncrypter : public AP4_OmaDcfSampleEncrypter
00257 {
00258 public:
00259     // constructor and destructor
00260     AP4_OmaDcfCtrSampleEncrypter(AP4_BlockCipher* block_cipher,
00261                                  const AP4_UI08*  salt);
00262     ~AP4_OmaDcfCtrSampleEncrypter();
00263 
00264     // methods
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     // members
00273     AP4_CtrStreamCipher* m_Cipher;
00274 };
00275 
00276 /*----------------------------------------------------------------------
00277 |   AP4_OmaDcfCbcSampleEncrypter
00278 +---------------------------------------------------------------------*/
00279 class AP4_OmaDcfCbcSampleEncrypter : public AP4_OmaDcfSampleEncrypter
00280 {
00281 public:
00282     // constructor and destructor
00283     AP4_OmaDcfCbcSampleEncrypter(AP4_BlockCipher* block_cipher,
00284                                  const AP4_UI08*  salt);
00285     ~AP4_OmaDcfCbcSampleEncrypter();
00286 
00287     // methods
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     // members
00296     AP4_CbcStreamCipher* m_Cipher;
00297 };
00298 
00299 /*----------------------------------------------------------------------
00300 |   AP4_TrackPropertyMap
00301 +---------------------------------------------------------------------*/
00302 class AP4_TrackPropertyMap
00303 {
00304 public:
00305     // methods
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     // destructor
00311     virtual ~AP4_TrackPropertyMap();
00312 
00313 private:
00314     // types
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     // members
00325     AP4_List<Entry> m_Entries;
00326 };
00327 
00328 /*----------------------------------------------------------------------
00329 |   AP4_OmaDcfEncryptingProcessor
00330 +---------------------------------------------------------------------*/
00331 class AP4_OmaDcfEncryptingProcessor : public AP4_Processor
00332 {
00333 public:
00334     // constructor
00335     AP4_OmaDcfEncryptingProcessor(AP4_OmaDcfCipherMode    cipher_mode,
00336                                   AP4_BlockCipherFactory* block_cipher_factory = NULL);
00337 
00338     // accessors
00339     AP4_ProtectionKeyMap& GetKeyMap()      { return m_KeyMap;      }
00340     AP4_TrackPropertyMap& GetPropertyMap() { return m_PropertyMap; }
00341 
00342     // methods
00343     virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
00344 
00345 private:
00346     // members
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_

Generated on Thu Mar 15 16:06:11 2007 for Bento4 MP4 SDK by  doxygen 1.5.1-p1