- Main Page
- Classes
- Files

# Ap4Protection.h

Go to the documentation of this file.

    00001 /*****************************************************************
    00002 |
    00003 |    AP4 - Protected Streams 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_PROTECTION_H_
    00030 #define _AP4_PROTECTION_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
    00042 /*----------------------------------------------------------------------
    00043 |   classes
    00044 +---------------------------------------------------------------------*/
    00045 class AP4_StreamCipher;
    00046
    00047 /*----------------------------------------------------------------------
    00048 |   constants
    00049 +---------------------------------------------------------------------*/
    00050 // this is fixed for now
    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 |   AP4_EncaSampleEntry
    00057 +---------------------------------------------------------------------*/
    00058 class AP4_EncaSampleEntry : public AP4_AudioSampleEntry
    00059 {
    00060 public:
    00061     // methods
    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     // methods
    00071     AP4_SampleDescription* ToSampleDescription();
    00072 };
    00073
    00074 /*----------------------------------------------------------------------
    00075 |   AP4_EncvSampleEntry
    00076 +---------------------------------------------------------------------*/
    00077 class AP4_EncvSampleEntry : public AP4_VisualSampleEntry
    00078 {
    00079 public:
    00080     // constructors
    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     // methods
    00090     AP4_SampleDescription* ToSampleDescription();
    00091 };
    00092
    00093 /*----------------------------------------------------------------------
    00094 |   AP4_DrmsSampleEntry
    00095 +---------------------------------------------------------------------*/
    00096 class AP4_DrmsSampleEntry : public AP4_EncaSampleEntry
    00097 {
    00098 public:
    00099     // methods
    00100     AP4_DrmsSampleEntry(AP4_Size         size,
    00101                         AP4_ByteStream&  stream,
    00102                         AP4_AtomFactory& atom_factory);
    00103 };
    00104
    00105 /*----------------------------------------------------------------------
    00106 |   AP4_DrmiSampleEntry
    00107 +---------------------------------------------------------------------*/
    00108 class AP4_DrmiSampleEntry : public AP4_EncvSampleEntry
    00109 {
    00110 public:
    00111     // methods
    00112     AP4_DrmiSampleEntry(AP4_Size         size,
    00113                         AP4_ByteStream&  stream,
    00114                         AP4_AtomFactory& atom_factory);
    00115 };
    00116
    00117 /*----------------------------------------------------------------------
    00118 |   AP4_ProtectionKeyMap
    00119 +---------------------------------------------------------------------*/
    00120 class AP4_ProtectionKeyMap
    00121 {
    00122 public:
    00123     // constructors and destructor
    00124     AP4_ProtectionKeyMap();
    00125     ~AP4_ProtectionKeyMap();
    00126
    00127     // methods
    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     // types
    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     // methods
    00145     KeyEntry* GetEntry(AP4_UI32 track_id) const;
    00146
    00147     // members
    00148     AP4_List<KeyEntry> m_KeyEntries;
    00149 };
    00150
    00151 /*----------------------------------------------------------------------
    00152 |   AP4_ProtectionSchemeInfo
    00153 +---------------------------------------------------------------------*/
    00154 class AP4_ProtectionSchemeInfo
    00155 {
    00156 public:
    00157     // constructors and destructor
    00158     AP4_ProtectionSchemeInfo(AP4_ContainerAtom* schi);
    00159     virtual ~AP4_ProtectionSchemeInfo();
    00160
    00161     // accessors
    00162     AP4_ContainerAtom* GetSchiAtom() { return m_SchiAtom; }
    00163
    00164 protected:
    00165     AP4_ContainerAtom* m_SchiAtom;
    00166 };
    00167
    00168 /*----------------------------------------------------------------------
    00169 |   AP4_ProtectedSampleDescription
    00170 +---------------------------------------------------------------------*/
    00171 class AP4_ProtectedSampleDescription : public AP4_SampleDescription
    00172 {
    00173 public:
    00174     // constructor and destructor
    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     // accessors
    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     // implementation of abstract base class methods
    00197     virtual AP4_Atom* ToAtom() const;
    00198
    00199 private:
    00200     // members
    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 |   AP4_BlockCipher
    00211 +---------------------------------------------------------------------*/
    00212 class AP4_BlockCipher
    00213 {
    00214 public:
    00215     // types
    00216     typedef enum {
    00217         ENCRYPT,
    00218         DECRYPT
    00219     } CipherDirection;
    00220
    00221     typedef enum {
    00222         AES_128
    00223     } CipherType;
    00224
    00225     // constructor and destructor
    00226     virtual ~AP4_BlockCipher() {}
    00227
    00228     // methods
    00229     virtual AP4_Result ProcessBlock(const AP4_UI08* block_in, AP4_UI08* block_out) = 0;
    00230 };
    00231
    00232 /*----------------------------------------------------------------------
    00233 |   AP4_BlockCipherFactory
    00234 +---------------------------------------------------------------------*/
    00235 class AP4_BlockCipherFactory
    00236 {
    00237 public:
    00238     // methods
    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 |   AP4_DefaultBlockCipherFactory
    00249 +---------------------------------------------------------------------*/
    00250 class AP4_DefaultBlockCipherFactory : public AP4_BlockCipherFactory
    00251 {
    00252 public:
    00253     // class variables
    00254     static AP4_DefaultBlockCipherFactory Instance;
    00255
    00256     // methods
    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 |   AP4_SampleDecrypter
    00266 +---------------------------------------------------------------------*/
    00267 class AP4_SampleDecrypter
    00268 {
    00269 public:
    00270     // factory
    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     // destructor
    00277     virtual ~AP4_SampleDecrypter() {}
    00278
    00279     // methods
    00280     virtual AP4_Result DecryptSampleData(AP4_DataBuffer& data_in,
    00281                                          AP4_DataBuffer& data_out) = 0;
    00282 };
    00283
    00284 /*----------------------------------------------------------------------
    00285 |   AP4_StandardDecryptingProcessor
    00286 +---------------------------------------------------------------------*/
    00287 class AP4_StandardDecryptingProcessor : public AP4_Processor
    00288 {
    00289 public:
    00290     // constructor
    00291     AP4_StandardDecryptingProcessor(const AP4_ProtectionKeyMap* key_map = NULL,
    00292                                     AP4_BlockCipherFactory*     block_cipher_factory = NULL);
    00293
    00294     // accessors
    00295     AP4_ProtectionKeyMap& GetKeyMap() { return m_KeyMap; }
    00296
    00297     // methods
    00298     virtual AP4_Processor::TrackHandler* CreateTrackHandler(AP4_TrakAtom* trak);
    00299
    00300 private:
    00301     // members
    00302     AP4_BlockCipherFactory* m_BlockCipherFactory;
    00303     AP4_ProtectionKeyMap    m_KeyMap;
    00304 };
    00305
    00306 /*----------------------------------------------------------------------
    00307 |   AP4_DecryptingStream
    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     // AP4_ByteStream methods
    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     // AP4_Referenceable methods
    00338     virtual void AddReference();
    00339     virtual void Release();
    00340
    00341 private:
    00342     // methods
    00343     AP4_DecryptingStream() {} // private constructor, use the factory instead
    00344     ~AP4_DecryptingStream();
    00345
    00346     // members
    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 |   AP4_EncryptingStream
    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     // AP4_ByteStream methods
    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     // AP4_Referenceable methods
    00392     virtual void AddReference();
    00393     virtual void Release();
    00394
    00395 private:
    00396     // methods
    00397     AP4_EncryptingStream() {} // private constructor, use the factory instead
    00398     ~AP4_EncryptingStream();
    00399
    00400     // members
    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_