More encryption fixes.
[libdcp.git] / src / cpl.cc
index 8d8f6a00df7e11e74b6cd572af353a3df6d8186a..a3d2ecfa9fbbaa831f5597ce3a0cd1f5d9b618db 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2013 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -28,7 +28,7 @@
 #include "parse/asset_map.h"
 #include "reel.h"
 #include "metadata.h"
-#include "encryption.h"
+#include "signer.h"
 #include "exceptions.h"
 #include "compose.hpp"
 
@@ -37,11 +37,14 @@ using std::stringstream;
 using std::ofstream;
 using std::ostream;
 using std::list;
+using std::pair;
+using std::make_pair;
 using boost::shared_ptr;
 using boost::lexical_cast;
+using boost::optional;
 using namespace libdcp;
 
-CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
+CPL::CPL (boost::filesystem::path directory, string name, ContentKind content_kind, int length, int frames_per_second)
        : _directory (directory)
        , _name (name)
        , _content_kind (content_kind)
@@ -54,10 +57,10 @@ CPL::CPL (string directory, string name, ContentKind content_kind, int length, i
 /** Construct a CPL object from a XML file.
  *  @param directory The directory containing this CPL's DCP.
  *  @param file The CPL XML filename.
- *  @param asset_map The corresponding asset map.
+ *  @param asset_maps AssetMaps to look for assets in.
  *  @param require_mxfs true to throw an exception if a required MXF file does not exist.
  */
-CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMap> asset_map, bool require_mxfs)
+CPL::CPL (boost::filesystem::path directory, string file, list<PathAssetMap> asset_maps, bool require_mxfs)
        : _directory (directory)
        , _content_kind (FEATURE)
        , _length (0)
@@ -79,7 +82,7 @@ CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMa
        /* Trim urn:uuid: off the front */
        _id = cpl->id.substr (9);
 
-       for (list<shared_ptr<libdcp::parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
+       for (list<shared_ptr<parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
 
                shared_ptr<parse::Picture> p;
 
@@ -104,10 +107,12 @@ CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMa
 
                if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
 
+                       pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
+
                        try {
                                picture.reset (new MonoPictureAsset (
-                                                      _directory,
-                                                      asset_map->asset_from_id (p->id)->chunks.front()->path
+                                                      asset.first,
+                                                      asset.second->chunks.front()->path
                                                       )
                                        );
 
@@ -125,9 +130,11 @@ CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMa
                        
                } else {
                        try {
+                               pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
+
                                picture.reset (new StereoPictureAsset (
-                                                      _directory,
-                                                      asset_map->asset_from_id (p->id)->chunks.front()->path,
+                                                      asset.first,
+                                                      asset.second->chunks.front()->path,
                                                       _fps,
                                                       p->duration
                                                       )
@@ -151,9 +158,11 @@ CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMa
                if ((*i)->asset_list->main_sound) {
                        
                        try {
+                               pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_sound->id);
+                       
                                sound.reset (new SoundAsset (
-                                                    _directory,
-                                                    asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path
+                                                    asset.first,
+                                                    asset.second->chunks.front()->path
                                                     )
                                        );
 
@@ -174,9 +183,11 @@ CPL::CPL (string directory, string file, shared_ptr<const libdcp::parse::AssetMa
 
                if ((*i)->asset_list->main_subtitle) {
                        
+                       pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_subtitle->id);
+
                        subtitle.reset (new SubtitleAsset (
-                                               _directory,
-                                               asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
+                                               asset.first,
+                                               asset.second->chunks.front()->path
                                                )
                                );
 
@@ -195,7 +206,7 @@ CPL::add_reel (shared_ptr<Reel> reel)
 }
 
 void
-CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryption> crypt) const
+CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<const Signer> signer) const
 {
        boost::filesystem::path p;
        p /= _directory;
@@ -211,7 +222,7 @@ CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryptio
                root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
        }
 
-       if (crypt) {
+       if (signer) {
                root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
        }
        
@@ -229,19 +240,19 @@ CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<Encryptio
        }
        root->add_child("RatingList");
 
-       xmlpp::Node* reel_list = root->add_child ("ReelList");
+       xmlpp::Element* reel_list = root->add_child ("ReelList");
        
        for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
                (*i)->write_to_cpl (reel_list, interop);
        }
 
-       if (crypt) {
-               sign (root, crypt->certificates, crypt->signer_key, interop);
+       if (signer) {
+               signer->sign (root, interop);
        }
 
        doc.write_to_file_formatted (p.string (), "UTF-8");
 
-       _digest = make_digest (p.string ());
+       _digest = make_digest (p.string (), 0);
        _length = boost::filesystem::file_size (p.string ());
 }
 
@@ -335,9 +346,9 @@ CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteT
 
 shared_ptr<xmlpp::Document>
 CPL::make_kdm (
-       CertificateChain const & certificates,
-       string const & signer_key,
+       shared_ptr<const Signer> signer,
        shared_ptr<const Certificate> recipient_cert,
+       Key key,
        boost::posix_time::ptime from,
        boost::posix_time::ptime until,
        bool interop,
@@ -359,14 +370,15 @@ CPL::make_kdm (
                xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
                
                authenticated_public->add_child("MessageId")->add_child_text ("urn:uuid:" + make_uuid());
+               /* XXX: this should probably be different if interop == true */
                authenticated_public->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
                authenticated_public->add_child("AnnotationText")->add_child_text (mxf_metadata.product_name);
                authenticated_public->add_child("IssueDate")->add_child_text (xml_metadata.issue_date);
 
                {
-                       xmlpp::Element* signer = authenticated_public->add_child("Signer");
-                       signer->add_child("X509IssuerName", "ds")->add_child_text (recipient_cert->issuer());
-                       signer->add_child("X509SerialNumber", "ds")->add_child_text (recipient_cert->serial());
+                       xmlpp::Element* xml_signer = authenticated_public->add_child("Signer");
+                       xml_signer->add_child("X509IssuerName", "ds")->add_child_text (signer->certificates().leaf()->issuer());
+                       xml_signer->add_child("X509SerialNumber", "ds")->add_child_text (signer->certificates().leaf()->serial());
                }
 
                {
@@ -388,9 +400,9 @@ CPL::make_kdm (
 
                                kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("urn:uuid:" + _id);
                                kdm_required_extensions->add_child("ContentTitleText")->add_child_text(_name);
-                               kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(certificates.leaf()->thumbprint());
-                               kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
-                               kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
+                               kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(signer->certificates().leaf()->thumbprint());
+                               kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text(ptime_to_string (from));
+                               kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text(ptime_to_string (until));
 
                                {
                                        xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
@@ -433,21 +445,15 @@ CPL::make_kdm (
                xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
                authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
                xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
-               {
-                       xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
-                       {
-                               xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
-                               encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
-                               encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
-                       }
 
-                       xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
-                       cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
-               }
+               xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
+               xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
+               encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
+               encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
+               xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
+               cipher_data->add_child("CipherValue", "enc")->add_child_text(key.hex());
        }
        
-       /* XXX: x2 one for each mxf? */
-
        {
                xmlpp::Element* signature = root->add_child("Signature", "ds");
                
@@ -482,7 +488,7 @@ CPL::make_kdm (
                        }
                }
                
-               add_signature_value (signature, certificates, signer_key, "ds");
+               signer->add_signature_value (signature, "ds");
        }
 
        return doc;
@@ -508,3 +514,16 @@ CPL::add_kdm (KDM const & kdm)
                (*i)->add_kdm (kdm);
        }
 }
+
+pair<string, shared_ptr<const parse::AssetMapAsset> >
+CPL::asset_from_id (list<PathAssetMap> asset_maps, string id) const
+{
+       for (list<PathAssetMap>::const_iterator i = asset_maps.begin(); i != asset_maps.end(); ++i) {
+               shared_ptr<parse::AssetMapAsset> a = i->second->asset_from_id (id);
+               if (a) {
+                       return make_pair (i->first, a);
+               }
+       }
+
+       return make_pair ("", shared_ptr<const parse::AssetMapAsset> ());
+}