73c8a2cd422433c2b65fdac993f23698b4cd618a
[libdcp.git] / src / dcp.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 /** @file  src/dcp.cc
21  *  @brief A class to create a DCP.
22  */
23
24 #include <sstream>
25 #include <fstream>
26 #include <iomanip>
27 #include <cassert>
28 #include <iostream>
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>
35 #include "dcp.h"
36 #include "asset.h"
37 #include "sound_asset.h"
38 #include "picture_asset.h"
39 #include "subtitle_asset.h"
40 #include "util.h"
41 #include "metadata.h"
42 #include "exceptions.h"
43 #include "cpl_file.h"
44 #include "pkl_file.h"
45 #include "asset_map.h"
46 #include "reel.h"
47
48 using std::string;
49 using std::list;
50 using std::stringstream;
51 using std::ofstream;
52 using std::ostream;
53 using boost::shared_ptr;
54 using namespace libdcp;
55
56 DCP::DCP (string directory)
57         : _directory (directory)
58 {
59         boost::filesystem::create_directories (directory);
60 }
61
62 void
63 DCP::write_xml (shared_ptr<Encryption> crypt) const
64 {
65         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
66                 (*i)->write_xml (crypt);
67         }
68
69         string pkl_uuid = make_uuid ();
70         string pkl_path = write_pkl (pkl_uuid, crypt);
71         
72         write_volindex ();
73         write_assetmap (pkl_uuid, boost::filesystem::file_size (pkl_path));
74 }
75
76 std::string
77 DCP::write_pkl (string pkl_uuid, shared_ptr<Encryption> crypt) const
78 {
79         assert (!_cpls.empty ());
80         
81         boost::filesystem::path p;
82         p /= _directory;
83         stringstream s;
84         s << pkl_uuid << "_pkl.xml";
85         p /= s.str();
86
87         xmlpp::Document doc;
88         xmlpp::Element* pkl = doc.create_root_node("PackingList", "http://www.smpte-ra.org/schemas/429-8/2007/PKL");
89         if (crypt) {
90                 pkl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
91         }
92
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);
99
100         {
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);
105                 }
106
107                 for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
108                         (*i)->write_to_pkl (asset_list);
109                 }
110         }
111
112         if (crypt) {
113                 sign (pkl, crypt->certificates, crypt->signer_key);
114         }
115                 
116         doc.write_to_file_formatted (p.string(), "UTF-8");
117
118         return p.string ();
119 }
120
121 void
122 DCP::write_volindex () const
123 {
124         boost::filesystem::path p;
125         p /= _directory;
126         p /= "VOLINDEX.xml";
127         ofstream vi (p.string().c_str());
128
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";
133 }
134
135 void
136 DCP::write_assetmap (string pkl_uuid, int pkl_length) const
137 {
138         boost::filesystem::path p;
139         p /= _directory;
140         p /= "ASSETMAP.xml";
141         ofstream am (p.string().c_str());
142
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"
150            << "  <AssetList>\n";
151
152         am << "    <Asset>\n"
153            << "      <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
154            << "      <PackingList>true</PackingList>\n"
155            << "      <ChunkList>\n"
156            << "        <Chunk>\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"
161            << "        </Chunk>\n"
162            << "      </ChunkList>\n"
163            << "    </Asset>\n";
164         
165         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
166                 (*i)->write_to_assetmap (am);
167         }
168
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);
172         }
173
174         am << "  </AssetList>\n"
175            << "</AssetMap>\n";
176 }
177
178
179 void
180 DCP::read (bool require_mxfs)
181 {
182         Files files;
183
184         shared_ptr<AssetMap> asset_map;
185         try {
186                 boost::filesystem::path p = _directory;
187                 p /= "ASSETMAP";
188                 if (boost::filesystem::exists (p)) {
189                         asset_map.reset (new AssetMap (p.string ()));
190                 } else {
191                         p = _directory;
192                         p /= "ASSETMAP.xml";
193                         if (boost::filesystem::exists (p)) {
194                                 asset_map.reset (new AssetMap (p.string ()));
195                         } else {
196                                 throw DCPReadError ("could not find AssetMap file");
197                         }
198                 }
199                 
200         } catch (FileError& e) {
201                 throw FileError ("could not load AssetMap file", files.asset_map);
202         }
203
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");
207                 }
208
209                 boost::filesystem::path t = _directory;
210                 t /= (*i)->chunks.front()->path;
211                 
212                 if (boost::algorithm::ends_with (t.string(), ".mxf") || boost::algorithm::ends_with (t.string(), ".ttf")) {
213                         continue;
214                 }
215
216                 xmlpp::DomParser* p = new xmlpp::DomParser;
217                 try {
218                         p->parse_file (t.string());
219                 } catch (std::exception& e) {
220                         delete p;
221                         continue;
222                 }
223
224                 string const root = p->get_document()->get_root_node()->get_name ();
225                 delete p;
226
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();
232                         } else {
233                                 throw DCPReadError ("duplicate PKLs found");
234                         }
235                 }
236         }
237         
238         if (files.cpls.empty ()) {
239                 throw FileError ("no CPL files found", "");
240         }
241
242         if (files.pkl.empty ()) {
243                 throw FileError ("no PKL file found", "");
244         }
245
246         shared_ptr<PKLFile> pkl;
247         try {
248                 pkl.reset (new PKLFile (files.pkl));
249         } catch (FileError& e) {
250                 throw FileError ("could not load PKL file", files.pkl);
251         }
252
253         /* Cross-check */
254         /* XXX */
255
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)));
258         }
259 }
260
261 bool
262 DCP::equals (DCP const & other, EqualityOptions opt, list<string>& notes) const
263 {
264         if (_cpls.size() != other._cpls.size()) {
265                 notes.push_back ("CPL counts differ");
266                 return false;
267         }
268
269         list<shared_ptr<const CPL> >::const_iterator a = _cpls.begin ();
270         list<shared_ptr<const CPL> >::const_iterator b = other._cpls.begin ();
271
272         while (a != _cpls.end ()) {
273                 if (!(*a)->equals (*b->get(), opt, notes)) {
274                         return false;
275                 }
276                 ++a;
277                 ++b;
278         }
279
280         return true;
281 }
282
283
284 void
285 DCP::add_cpl (shared_ptr<CPL> cpl)
286 {
287         _cpls.push_back (cpl);
288 }
289
290 class AssetComparator
291 {
292 public:
293         bool operator() (shared_ptr<const Asset> a, shared_ptr<const Asset> b) {
294                 return a->uuid() < b->uuid();
295         }
296 };
297
298 list<shared_ptr<const Asset> >
299 DCP::assets () const
300 {
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 ();
304                 a.merge (t);
305         }
306
307         a.sort (AssetComparator ());
308         a.unique ();
309         return a;
310 }
311
312 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
313         : _directory (directory)
314         , _name (name)
315         , _content_kind (content_kind)
316         , _length (length)
317         , _fps (frames_per_second)
318 {
319         _uuid = make_uuid ();
320 }
321
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.
327  */
328 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
329         : _directory (directory)
330         , _content_kind (FEATURE)
331         , _length (0)
332         , _fps (0)
333 {
334         /* Read the XML */
335         shared_ptr<CPLFile> cpl;
336         try {
337                 cpl.reset (new CPLFile (file));
338         } catch (FileError& e) {
339                 throw FileError ("could not load CPL file", file);
340         }
341         
342         /* Now cherry-pick the required bits into our own data structure */
343         
344         _name = cpl->annotation_text;
345         _content_kind = cpl->content_kind;
346
347         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
348
349                 shared_ptr<Picture> p;
350
351                 if ((*i)->asset_list->main_picture) {
352                         p = (*i)->asset_list->main_picture;
353                 } else {
354                         p = (*i)->asset_list->main_stereoscopic_picture;
355                 }
356                 
357                 _fps = p->edit_rate.numerator;
358                 _length += p->duration;
359
360                 shared_ptr<PictureAsset> picture;
361                 shared_ptr<SoundAsset> sound;
362                 shared_ptr<SubtitleAsset> subtitle;
363
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.
368                 */
369
370                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
371
372                         try {
373                                 picture.reset (new MonoPictureAsset (
374                                                        _directory,
375                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
376                                                        _fps,
377                                                        (*i)->asset_list->main_picture->entry_point,
378                                                        (*i)->asset_list->main_picture->duration
379                                                        )
380                                         );
381                         } catch (MXFFileError) {
382                                 if (require_mxfs) {
383                                         throw;
384                                 }
385                         }
386                         
387                 } else {
388
389                         try {
390                                 picture.reset (new StereoPictureAsset (
391                                                        _directory,
392                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
393                                                        _fps,
394                                                        p->entry_point,
395                                                        p->duration
396                                                        )
397                                         );
398                         } catch (MXFFileError) {
399                                 if (require_mxfs) {
400                                         throw;
401                                 }
402                         }
403                         
404                 }
405                 
406                 if ((*i)->asset_list->main_sound) {
407                         
408                         try {
409                                 sound.reset (new SoundAsset (
410                                                      _directory,
411                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
412                                                      _fps,
413                                                      (*i)->asset_list->main_sound->entry_point,
414                                                      (*i)->asset_list->main_sound->duration
415                                                      )
416                                         );
417                         } catch (MXFFileError) {
418                                 if (require_mxfs) {
419                                         throw;
420                                 }
421                         }
422                 }
423
424                 if ((*i)->asset_list->main_subtitle) {
425                         
426                         subtitle.reset (new SubtitleAsset (
427                                                 _directory,
428                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
429                                                 )
430                                 );
431                 }
432                         
433                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
434         }
435 }
436
437 void
438 CPL::add_reel (shared_ptr<const Reel> reel)
439 {
440         _reels.push_back (reel);
441 }
442
443 void
444 CPL::write_xml (shared_ptr<Encryption> crypt) const
445 {
446         boost::filesystem::path p;
447         p /= _directory;
448         stringstream s;
449         s << _uuid << "_cpl.xml";
450         p /= s.str();
451
452         xmlpp::Document doc;
453         xmlpp::Element* cpl = doc.create_root_node("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
454
455         if (crypt) {
456                 cpl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
457         }
458
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));
465
466         {
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);
470         }
471
472         cpl->add_child("RatingList");
473
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);
477         }
478
479         if (crypt) {
480                 sign (cpl, crypt->certificates, crypt->signer_key);
481         }
482
483         doc.write_to_file_formatted (p.string(), "UTF-8");
484
485         _digest = make_digest (p.string ());
486         _length = boost::filesystem::file_size (p.string ());
487 }
488
489 void
490 CPL::write_to_pkl (xmlpp::Element* p) const
491 {
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");
497 }
498
499 list<shared_ptr<const Asset> >
500 CPL::assets () const
501 {
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 ());
506                 }
507                 if ((*i)->main_sound ()) {
508                         a.push_back ((*i)->main_sound ());
509                 }
510                 if ((*i)->main_subtitle ()) {
511                         a.push_back ((*i)->main_subtitle ());
512                 }
513         }
514
515         return a;
516 }
517
518 void
519 CPL::write_to_assetmap (ostream& s) const
520 {
521         s << "    <Asset>\n"
522           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
523           << "      <ChunkList>\n"
524           << "        <Chunk>\n"
525           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
526           << "          <VolumeIndex>1</VolumeIndex>\n"
527           << "          <Offset>0</Offset>\n"
528           << "          <Length>" << _length << "</Length>\n"
529           << "        </Chunk>\n"
530           << "      </ChunkList>\n"
531           << "    </Asset>\n";
532 }
533         
534         
535         
536 bool
537 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
538 {
539         if (_name != other._name) {
540                 notes.push_back ("names differ");
541                 return false;
542         }
543
544         if (_content_kind != other._content_kind) {
545                 notes.push_back ("content kinds differ");
546                 return false;
547         }
548
549         if (_fps != other._fps) {
550                 notes.push_back ("frames per second differ");
551                 return false;
552         }
553
554         if (_length != other._length) {
555                 notes.push_back ("lengths differ");
556                 return false;
557         }
558
559         if (_reels.size() != other._reels.size()) {
560                 notes.push_back ("reel counts differ");
561                 return false;
562         }
563         
564         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
565         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
566         
567         while (a != _reels.end ()) {
568                 if (!(*a)->equals (*b, opt, notes)) {
569                         return false;
570                 }
571                 ++a;
572                 ++b;
573         }
574
575         return true;
576 }
577
578 shared_ptr<xmlpp::Document>
579 CPL::make_kdm (
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
585         ) const
586 {
587         assert (recipient_cert);
588         
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");
594
595         {
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());
599                 
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);
604
605                 {
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())
609                                 );
610                         signer->add_child("X509SerialNumber", "ds")->add_child_text (
611                                 recipient_cert->serial()
612                                 );
613                 }
614
615                 {
616                         xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
617
618                         {
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");
621                                 {
622                                         xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
623                                         {
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())
627                                                         );
628                                                 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (
629                                                         recipient_cert->serial()
630                                                         );
631                                         }
632
633                                         recipient->add_child("X509SubjectName")->add_child_text (Certificate::name_for_xml (recipient_cert->subject()));
634                                 }
635
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");
641
642                                 {
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());
646                                         {
647                                                 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
648                                                 device_list->add_child("CertificateThumbprint")->add_child_text(recipient_cert->thumbprint());
649                                         }
650                                 }
651
652                                 {
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);
658                                                 if (mxf) {
659                                                         mxf->add_typed_key_id (key_id_list);
660                                                 }
661                                         }
662                                 }
663
664                                 {
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"
668                                                 );
669                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
670                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
671                                                 );
672                                 }
673                         }
674                 }
675                                          
676                 authenticated_public->add_child("NonCriticalExtensions");
677         }
678
679         {
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());
683                 {
684                         xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
685                         {
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");
689                         }
690
691                         xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
692                         cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
693                 }
694         }
695         
696         /* XXX: x2 one for each mxf? */
697
698         {
699                 xmlpp::Element* signature = root->add_child("Signature", "ds");
700                 
701                 {
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"
705                                 );
706                         signed_info->add_child("SignatureMethod", "ds")->set_attribute(
707                                 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
708                                 );
709                         {
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");
714                         }
715                         
716                         {                               
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");
721                         }
722                 }
723                 
724                 add_signature_value (signature, certificates, signer_key, "ds");
725         }
726
727         return doc;
728 }