Missing audio asset decryption.
[libdcp.git] / src / dcp.cc
index c7634b5d3385d1a9cafd5b847dffd41dd4540d66..684e249d300512252539cd3a808bd270996a543c 100644 (file)
 #include <cassert>
 #include <iostream>
 #include <boost/filesystem.hpp>
+#include <boost/lexical_cast.hpp>
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <libxml++/libxml++.h>
+#include <xmlsec/xmldsig.h>
+#include <xmlsec/app.h>
 #include "dcp.h"
 #include "asset.h"
 #include "sound_asset.h"
@@ -42,6 +45,8 @@
 #include "parse/asset_map.h"
 #include "reel.h"
 #include "cpl.h"
+#include "encryption.h"
+#include "kdm.h"
 
 using std::string;
 using std::list;
@@ -59,21 +64,21 @@ DCP::DCP (string directory)
 }
 
 void
-DCP::write_xml (XMLMetadata const & metadata) const
+DCP::write_xml (XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
 {
-       for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
-               (*i)->write_xml (metadata);
+       for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+               (*i)->write_xml (metadata, crypt);
        }
 
        string pkl_uuid = make_uuid ();
-       string pkl_path = write_pkl (pkl_uuid, metadata);
+       string pkl_path = write_pkl (pkl_uuid, metadata, crypt);
        
        write_volindex ();
        write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path), metadata);
 }
 
 std::string
-DCP::write_pkl (string pkl_uuid, XMLMetadata const & metadata) const
+DCP::write_pkl (string pkl_uuid, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
 {
        assert (!_cpls.empty ());
        
@@ -84,26 +89,32 @@ DCP::write_pkl (string pkl_uuid, XMLMetadata const & metadata) const
        p /= s.str();
 
        xmlpp::Document doc;
-       xmlpp::Element* root = doc.create_root_node ("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
+       xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
+       if (crypt) {
+               pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
+       }
 
-       root->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
+       pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
        /* XXX: this is a bit of a hack */
-       root->add_child("AnnotationText")->add_child_text (_cpls.front()->name());
-       root->add_child("IssueDate")->add_child_text (metadata.issue_date);
-       root->add_child("Issuer")->add_child_text (metadata.issuer);
-       root->add_child("Creator")->add_child_text (metadata.creator);
-
-       xmlpp::Node* asset_list = root->add_child ("AssetList");
+       pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
+       pkl->add_child("IssueDate")->add_child_text (metadata.issue_date);
+       pkl->add_child("Issuer")->add_child_text (metadata.issuer);
+       pkl->add_child("Creator")->add_child_text (metadata.creator);
 
+       xmlpp::Element* asset_list = pkl->add_child("AssetList");
        list<shared_ptr<const Asset> > a = assets ();
        for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
                (*i)->write_to_pkl (asset_list);
        }
-
-       for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+       
+       for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
                (*i)->write_to_pkl (asset_list);
        }
 
+       if (crypt) {
+               sign (pkl, crypt->certificates, crypt->signer_key);
+       }
+               
        doc.write_to_file_formatted (p.string (), "UTF-8");
        return p.string ();
 }
@@ -148,7 +159,7 @@ DCP::write_assetmap (string pkl_uuid, int pkl_length, XMLMetadata const & metada
        chunk->add_child("Offset")->add_child_text ("0");
        chunk->add_child("Length")->add_child_text (lexical_cast<string> (pkl_length));
        
-       for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+       for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
                (*i)->write_to_assetmap (asset_list);
        }
 
@@ -251,8 +262,8 @@ DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteT
                return false;
        }
 
-       list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
-       list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
+       list<shared_ptr<CPL> >::const_iterator a = _cpls.begin ();
+       list<shared_ptr<CPL> >::const_iterator b = other._cpls.begin ();
 
        while (a != _cpls.end ()) {
                if (!(*a)->equals (*b->get(), opt, note)) {
@@ -265,7 +276,6 @@ DCP::equals (DCP const & other, EqualityOptions opt, boost::function<void (NoteT
        return true;
 }
 
-
 void
 DCP::add_cpl (shared_ptr<CPL> cpl)
 {
@@ -284,13 +294,38 @@ list<shared_ptr<const Asset> >
 DCP::assets () const
 {
        list<shared_ptr<const Asset> > a;
-       for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+       for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
                list<shared_ptr<const Asset> > t = (*i)->assets ();
                a.merge (t);
        }
 
-       a.sort ();
+       a.sort (AssetComparator ());
        a.unique ();
        return a;
 }
 
+bool
+DCP::encrypted () const
+{
+       for (list<shared_ptr<CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+               if ((*i)->encrypted ()) {
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+void
+DCP::add_kdm (KDM const & kdm)
+{
+       list<KDMCipher> ciphers = kdm.ciphers ();
+       
+       for (list<shared_ptr<CPL> >::iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
+               for (list<KDMCipher>::iterator j = ciphers.begin(); j != ciphers.end(); ++j) {
+                       if (j->cpl_id() == (*i)->id()) {
+                               (*i)->add_kdm (kdm);
+                       }                               
+               }
+       }
+}