2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * @brief A class to create a DCP.
29 #include <boost/filesystem.hpp>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <libxml++/libxml++.h>
33 #include <xmlsec/xmldsig.h>
34 #include <xmlsec/app.h>
37 #include "sound_asset.h"
38 #include "picture_asset.h"
39 #include "subtitle_asset.h"
42 #include "exceptions.h"
45 #include "asset_map.h"
50 using std::stringstream;
53 using boost::shared_ptr;
54 using namespace libdcp;
56 DCP::DCP (string directory)
57 : _directory (directory)
59 boost::filesystem::create_directories (directory);
63 DCP::write_xml (shared_ptr<Encryption> crypt) const
65 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
66 (*i)->write_xml (crypt);
69 string pkl_uuid = make_uuid ();
70 string pkl_path = write_pkl (pkl_uuid, crypt);
73 write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
77 DCP::write_pkl (string pkl_uuid, shared_ptr<Encryption> crypt) const
79 assert (!_cpls.empty ());
81 boost::filesystem::path p;
84 s << pkl_uuid << "_pkl.xml";
88 xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
90 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
93 pkl->add_child("Id")->add_child_text ("urn:uuid:" + pkl_uuid);
94 /* XXX: this is a bit of a hack */
95 pkl->add_child("AnnotationText")->add_child_text(_cpls.front()->name());
96 pkl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
97 pkl->add_child("Issuer")->add_child_text (Metadata::instance()->issuer);
98 pkl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
101 xmlpp::Element* asset_list = pkl->add_child("AssetList");
102 list<shared_ptr<const Asset> > a = assets ();
103 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
104 (*i)->write_to_pkl (asset_list);
107 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
108 (*i)->write_to_pkl (asset_list);
113 sign (pkl, crypt->certificates, crypt->signer_key);
116 doc.write_to_file_formatted (p.string(), "UTF-8");
122 DCP::write_volindex () const
124 boost::filesystem::path p;
127 ofstream vi (p.string().c_str());
129 vi << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
130 << "<VolumeIndex xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
131 << " <Index>1</Index>\n"
132 << "</VolumeIndex>\n";
136 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
138 boost::filesystem::path p;
141 ofstream am (p.string().c_str());
143 am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
144 << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
145 << " <Id>urn:uuid:" << make_uuid() << "</Id>\n"
146 << " <Creator>" << Metadata::instance()->creator << "</Creator>\n"
147 << " <VolumeCount>1</VolumeCount>\n"
148 << " <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
149 << " <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
153 << " <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
154 << " <PackingList>true</PackingList>\n"
157 << " <Path>" << pkl_uuid << "_pkl.xml</Path>\n"
158 << " <VolumeIndex>1</VolumeIndex>\n"
159 << " <Offset>0</Offset>\n"
160 << " <Length>" << pkl_length << "</Length>\n"
165 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
166 (*i)->write_to_assetmap (am);
169 list<shared_ptr<const Asset> > a = assets ();
170 for (list<shared_ptr<const Asset> >::const_iterator i = a.begin(); i != a.end(); ++i) {
171 (*i)->write_to_assetmap (am);
174 am << " </AssetList>\n"
180 DCP::read (bool require_mxfs)
184 shared_ptr<AssetMap> asset_map;
186 boost::filesystem::path p = _directory;
188 if (boost::filesystem::exists (p)) {
189 asset_map.reset (new AssetMap (p.string ()));
193 if (boost::filesystem::exists (p)) {
194 asset_map.reset (new AssetMap (p.string ()));
196 throw DCPReadError ("could not find AssetMap file");
200 } catch (FileError& e) {
201 throw FileError ("could not load AssetMap file", files.asset_map);
204 for (list<shared_ptr<AssetMapAsset> >::const_iterator i = asset_map->assets.begin(); i != asset_map->assets.end(); ++i) {
205 if ((*i)->chunks.size() != 1) {
206 throw XMLError ("unsupported asset chunk count");
209 boost::filesystem::path t = _directory;
210 t /= (*i)->chunks.front()->path;
212 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
216 xmlpp::DomParser* p = new xmlpp::DomParser;
218 p->parse_file (t.string());
219 } catch (std::exception& e) {
224 string const root = p->get_document()->get_root_node()->get_name ();
227 if (root == "CompositionPlaylist") {
228 files.cpls.push_back (t.string());
229 } else if (root == "PackingList") {
230 if (files.pkl.empty ()) {
231 files.pkl = t.string();
233 throw DCPReadError ("duplicate PKLs found");
238 if (files.cpls.empty ()) {
239 throw FileError ("no CPL files found", "");
242 if (files.pkl.empty ()) {
243 throw FileError ("no PKL file found", "");
246 shared_ptr<PKLFile> pkl;
248 pkl.reset (new PKLFile (files.pkl));
249 } catch (FileError& e) {
250 throw FileError ("could not load PKL file", files.pkl);
256 for (list<string>::iterator i = files.cpls.begin(); i != files.cpls.end(); ++i) {
257 _cpls.push_back (shared_ptr<CPL> (new CPL (_directory, *i, asset_map, require_mxfs)));
262 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
264 if (_cpls.size() != other._cpls.size()) {
265 notes.push_back ("CPL counts differ");
269 list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
270 list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
272 while (a != _cpls.end ()) {
273 if (!(*a)->equals (*b->get(), opt, notes)) {
285 DCP::add_cpl (shared_ptr<CPL> cpl)
287 _cpls.push_back (cpl);
290 class AssetComparator
293 bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
294 return a->uuid() < b->uuid();
298 list<shared_ptr<const Asset> >
301 list<shared_ptr<const Asset> > a;
302 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
303 list<shared_ptr<const Asset> > t = (*i)->assets ();
307 a.sort (AssetComparator ());
312 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
313 : _directory (directory)
315 , _content_kind (content_kind)
317 , _fps (frames_per_second)
319 _uuid = make_uuid ();
322 /** Construct a CPL object from a XML file.
323 * @param directory The directory containing this CPL's DCP.
324 * @param file The CPL XML filename.
325 * @param asset_map The corresponding asset map.
326 * @param require_mxfs true to throw an exception if a required MXF file does not exist.
328 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
329 : _directory (directory)
330 , _content_kind (FEATURE)
335 shared_ptr<CPLFile> cpl;
337 cpl.reset (new CPLFile (file));
338 } catch (FileError& e) {
339 throw FileError ("could not load CPL file", file);
342 /* Now cherry-pick the required bits into our own data structure */
344 _name = cpl->annotation_text;
345 _content_kind = cpl->content_kind;
347 for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
349 shared_ptr<Picture> p;
351 if ((*i)->asset_list->main_picture) {
352 p = (*i)->asset_list->main_picture;
354 p = (*i)->asset_list->main_stereoscopic_picture;
357 _fps = p->edit_rate.numerator;
358 _length += p->duration;
360 shared_ptr<PictureAsset> picture;
361 shared_ptr<SoundAsset> sound;
362 shared_ptr<SubtitleAsset> subtitle;
364 /* Some rather twisted logic to decide if we are 3D or not;
365 some DCPs give a MainStereoscopicPicture to indicate 3D, others
366 just have a FrameRate twice the EditRate and apparently
367 expect you to divine the fact that they are hence 3D.
370 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
373 picture.reset (new MonoPictureAsset (
375 asset_map->asset_from_id (p->id)->chunks.front()->path,
377 (*i)->asset_list->main_picture->entry_point,
378 (*i)->asset_list->main_picture->duration
381 } catch (MXFFileError) {
390 picture.reset (new StereoPictureAsset (
392 asset_map->asset_from_id (p->id)->chunks.front()->path,
398 } catch (MXFFileError) {
406 if ((*i)->asset_list->main_sound) {
409 sound.reset (new SoundAsset (
411 asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
413 (*i)->asset_list->main_sound->entry_point,
414 (*i)->asset_list->main_sound->duration
417 } catch (MXFFileError) {
424 if ((*i)->asset_list->main_subtitle) {
426 subtitle.reset (new SubtitleAsset (
428 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
433 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
438 CPL::add_reel (shared_ptr<const Reel> reel)
440 _reels.push_back (reel);
444 CPL::write_xml (shared_ptr<Encryption> crypt) const
446 boost::filesystem::path p;
449 s << _uuid << "_cpl.xml";
453 xmlpp::Element* cpl = doc.create_root_node("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
456 cpl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
459 cpl->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
460 cpl->add_child("AnnotationText")->add_child_text (_name);
461 cpl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
462 cpl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
463 cpl->add_child("ContentTitleText")->add_child_text (_name);
464 cpl->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
467 xmlpp::Element* cv = cpl->add_child ("ContentVersion");
468 cv->add_child("Id")->add_child_text ("urn:uri:" + _uuid + "_" + Metadata::instance()->issue_date);
469 cv->add_child("LabelText")->add_child_text (_uuid + "_" + Metadata::instance()->issue_date);
472 cpl->add_child("RatingList");
474 xmlpp::Element* reel_list = cpl->add_child("ReelList");
475 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
476 (*i)->write_to_cpl (reel_list);
480 sign (cpl, crypt->certificates, crypt->signer_key);
483 doc.write_to_file_formatted (p.string(), "UTF-8");
485 _digest = make_digest (p.string ());
486 _length = boost::filesystem::file_size (p.string ());
490 CPL::write_to_pkl (xmlpp::Element* p) const
492 xmlpp::Element* asset = p->add_child("Asset");
493 asset->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
494 asset->add_child("Hash")->add_child_text(_digest);
495 asset->add_child("Size")->add_child_text(boost::lexical_cast<string> (_length));
496 asset->add_child("Type")->add_child_text("text/xml");
499 list<shared_ptr<const Asset> >
502 list<shared_ptr<const Asset> > a;
503 for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
504 if ((*i)->main_picture ()) {
505 a.push_back ((*i)->main_picture ());
507 if ((*i)->main_sound ()) {
508 a.push_back ((*i)->main_sound ());
510 if ((*i)->main_subtitle ()) {
511 a.push_back ((*i)->main_subtitle ());
519 CPL::write_to_assetmap (ostream& s) const
522 << " <Id>urn:uuid:" << _uuid << "</Id>\n"
525 << " <Path>" << _uuid << "_cpl.xml</Path>\n"
526 << " <VolumeIndex>1</VolumeIndex>\n"
527 << " <Offset>0</Offset>\n"
528 << " <Length>" << _length << "</Length>\n"
537 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
539 if (_name != other._name) {
540 notes.push_back ("names differ");
544 if (_content_kind != other._content_kind) {
545 notes.push_back ("content kinds differ");
549 if (_fps != other._fps) {
550 notes.push_back ("frames per second differ");
554 if (_length != other._length) {
555 notes.push_back ("lengths differ");
559 if (_reels.size() != other._reels.size()) {
560 notes.push_back ("reel counts differ");
564 list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
565 list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
567 while (a != _reels.end ()) {
568 if (!(*a)->equals (*b, opt, notes)) {
578 shared_ptr<xmlpp::Document>
580 CertificateChain const & certificates,
581 string const & signer_key,
582 shared_ptr<const Certificate> recipient_cert,
583 boost::posix_time::ptime from,
584 boost::posix_time::ptime until
587 assert (recipient_cert);
589 shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
590 xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
591 root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
592 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
593 root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
596 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
597 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
598 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
600 authenticated_public->add_child("MessageId")->add_child_text("urn:uuid:" + make_uuid());
601 authenticated_public->add_child("MessageType")->add_child_text("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
602 authenticated_public->add_child("AnnotationText")->add_child_text(Metadata::instance()->product_name);
603 authenticated_public->add_child("IssueDate")->add_child_text(Metadata::instance()->issue_date);
606 xmlpp::Element* signer = authenticated_public->add_child("Signer");
607 signer->add_child("X509IssuerName", "ds")->add_child_text (
608 Certificate::name_for_xml (recipient_cert->issuer())
610 signer->add_child("X509SerialNumber", "ds")->add_child_text (
611 recipient_cert->serial()
616 xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
619 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
620 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
622 xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
624 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
625 serial_element->add_child("X509IssuerName", "ds")->add_child_text (
626 Certificate::name_for_xml (recipient_cert->issuer())
628 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (
629 recipient_cert->serial()
633 recipient->add_child("X509SubjectName")->add_child_text (Certificate::name_for_xml (recipient_cert->subject()));
636 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("urn:uuid:" + _uuid);
637 kdm_required_extensions->add_child("ContentTitleText")->add_child_text(_name);
638 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(certificates.leaf()->thumbprint());
639 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
640 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
643 xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
644 authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
645 authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
647 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
648 device_list->add_child("CertificateThumbprint")->add_child_text(recipient_cert->thumbprint());
653 xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
654 list<shared_ptr<const Asset> > a = assets();
655 for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
656 /* XXX: non-MXF assets? */
657 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
659 mxf->add_typed_key_id (key_id_list);
665 xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
666 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
667 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
669 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (
670 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
676 authenticated_public->add_child("NonCriticalExtensions");
680 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
681 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
682 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
684 xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
686 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
687 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
688 encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
691 xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
692 cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
696 /* XXX: x2 one for each mxf? */
699 xmlpp::Element* signature = root->add_child("Signature", "ds");
702 xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
703 signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
704 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
706 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
707 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
710 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
711 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
712 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
713 reference->add_child("DigestValue", "ds");
717 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
718 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
719 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
720 reference->add_child("DigestValue", "ds");
724 add_signature_value (signature, certificates, signer_key, "ds");