Various probably quite untidy progress on KDMs.
[libdcp.git] / src / mxf_asset.cc
index d0e02c8bfd12718379707ab56428549492805501..6ba42d754f5ed7a15edd7ebbddf4bd03edd53175 100644 (file)
 #include <iostream>
 #include <fstream>
 #include <boost/filesystem.hpp>
+#include <libxml++/nodes/element.h>
 #include "AS_DCP.h"
+#include "KM_prng.h"
 #include "KM_util.h"
 #include "mxf_asset.h"
 #include "util.h"
 #include "metadata.h"
+#include "exceptions.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::list;
+using boost::shared_ptr;
+using boost::dynamic_pointer_cast;
 using namespace libdcp;
 
-MXFAsset::MXFAsset (string directory, string file_name, sigc::signal1<void, float>* progress, int fps, int entry_point, int length)
+MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal<void (float)>* progress, int fps, int entry_point, int length, bool encrypted)
        : Asset (directory, file_name)
        , _progress (progress)
        , _fps (fps)
        , _entry_point (entry_point)
        , _length (length)
+       , _encrypted (encrypted)
+       , _encryption_context (0)
 {
-       
+       if (_encrypted) {
+               _key_id = make_uuid ();
+               uint8_t key_buffer[ASDCP::KeyLen];
+               Kumu::FortunaRNG rng;
+               rng.FillRandom (key_buffer, ASDCP::KeyLen);
+               char key_string[ASDCP::KeyLen * 4];
+               Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4);
+               _key_value = key_string;
+                       
+               _encryption_context = new ASDCP::AESEncContext;
+               if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) {
+                       throw MiscError ("could not set up encryption context");
+               }
+
+               uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
+               
+               if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
+                       throw MiscError ("could not set up CBC initialization vector");
+               }
+       }
+}
+
+MXFAsset::~MXFAsset ()
+{
+       delete _encryption_context;
 }
 
 void
@@ -55,10 +86,19 @@ MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info) const
        unsigned int c;
        Kumu::hex2bin (_uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
        assert (c == Kumu::UUID_Length);
+
+       if (_encrypted) {
+               Kumu::GenRandomUUID (writer_info->ContextID);
+               writer_info->EncryptedEssence = true;
+
+               unsigned int c;
+               Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
+               assert (c == Kumu::UUID_Length);
+       }
 }
 
 bool
-MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<string>& notes) const
+MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions, list<string>& notes) const
 {
        shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
        if (!other_mxf) {
@@ -80,41 +120,6 @@ MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, list<strin
                notes.push_back ("MXF lengths differ");
                return false;
        }
-       
-       if (opt.bitwise) {
-
-               if (digest() != other_mxf->digest()) {
-                       notes.push_back ("MXF digests differ");
-                       return false;
-               }
-               
-               if (filesystem::file_size (path()) != filesystem::file_size (other_mxf->path())) {
-                       notes.push_back (path().string() + " and " + other_mxf->path().string() + " sizes differ");
-                       return false;
-               }
-               
-               ifstream a (path().string().c_str(), ios::binary);
-               ifstream b (other_mxf->path().string().c_str(), ios::binary);
-
-               int buffer_size = 65536;
-               char abuffer[buffer_size];
-               char bbuffer[buffer_size];
-
-               int n = filesystem::file_size (path ());
-
-               while (n) {
-                       int const t = min (n, buffer_size);
-                       a.read (abuffer, t);
-                       b.read (bbuffer, t);
-
-                       if (memcmp (abuffer, bbuffer, t) != 0) {
-                               notes.push_back (path().string() + " and " + other_mxf->path().string() + " content differs");
-                               return false;
-                       }
-
-                       n -= t;
-               }
-       }
 
        return true;
 }
@@ -124,3 +129,11 @@ MXFAsset::length () const
 {
        return _length;
 }
+
+void
+MXFAsset::add_typed_key_id (xmlpp::Element* parent) const
+{
+       xmlpp::Element* typed_key_id = parent->add_child("TypedKeyId");
+       typed_key_id->add_child("KeyType")->add_child_text(key_type ());
+       typed_key_id->add_child("KeyId")->add_child_text("urn:uuid:" + _key_id);
+}