Various probably quite untidy progress on KDMs.
[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 <libxml++/libxml++.h>
32 #include <xmlsec/xmldsig.h>
33 #include <xmlsec/app.h>
34 #include "dcp.h"
35 #include "asset.h"
36 #include "sound_asset.h"
37 #include "picture_asset.h"
38 #include "subtitle_asset.h"
39 #include "util.h"
40 #include "metadata.h"
41 #include "exceptions.h"
42 #include "cpl_file.h"
43 #include "pkl_file.h"
44 #include "asset_map.h"
45 #include "reel.h"
46
47 using std::string;
48 using std::list;
49 using std::stringstream;
50 using std::ofstream;
51 using std::ostream;
52 using boost::shared_ptr;
53 using namespace libdcp;
54
55 DCP::DCP (string directory)
56         : _directory (directory)
57         , _encrypted (false)
58 {
59         boost::filesystem::create_directories (directory);
60 }
61
62 void
63 DCP::write_xml () const
64 {
65         for (list<shared_ptr<const CPL> >::const_iterator i = _cpls.begin(); i != _cpls.end(); ++i) {
66                 (*i)->write_xml (_encrypted, _certificates, _signer_key);
67         }
68
69         string pkl_uuid = make_uuid ();
70         string pkl_path = write_pkl (pkl_uuid);
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) 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 (_encrypted) {
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 (_encrypted) {
113                 sign (pkl, _certificates, _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 (ends_with (t.string(), ".mxf") || 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 CPL::CPL (string directory, string file, shared_ptr<const AssetMap> asset_map, bool require_mxfs)
323         : _directory (directory)
324         , _content_kind (FEATURE)
325         , _length (0)
326         , _fps (0)
327 {
328         /* Read the XML */
329         shared_ptr<CPLFile> cpl;
330         try {
331                 cpl.reset (new CPLFile (file));
332         } catch (FileError& e) {
333                 throw FileError ("could not load CPL file", file);
334         }
335         
336         /* Now cherry-pick the required bits into our own data structure */
337         
338         _name = cpl->annotation_text;
339         _content_kind = cpl->content_kind;
340
341         for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
342
343                 shared_ptr<Picture> p;
344
345                 if ((*i)->asset_list->main_picture) {
346                         p = (*i)->asset_list->main_picture;
347                 } else {
348                         p = (*i)->asset_list->main_stereoscopic_picture;
349                 }
350                 
351                 _fps = p->edit_rate.numerator;
352                 _length += p->duration;
353
354                 shared_ptr<PictureAsset> picture;
355                 shared_ptr<SoundAsset> sound;
356                 shared_ptr<SubtitleAsset> subtitle;
357
358                 /* Some rather twisted logic to decide if we are 3D or not;
359                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
360                    just have a FrameRate twice the EditRate and apparently
361                    expect you to divine the fact that they are hence 3D.
362                 */
363
364                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
365
366                         try {
367                                 picture.reset (new MonoPictureAsset (
368                                                        _directory,
369                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
370                                                        _fps,
371                                                        (*i)->asset_list->main_picture->entry_point,
372                                                        (*i)->asset_list->main_picture->duration
373                                                        )
374                                         );
375                         } catch (MXFFileError) {
376                                 if (require_mxfs) {
377                                         throw;
378                                 }
379                         }
380                         
381                 } else {
382
383                         try {
384                                 picture.reset (new StereoPictureAsset (
385                                                        _directory,
386                                                        asset_map->asset_from_id (p->id)->chunks.front()->path,
387                                                        _fps,
388                                                        p->entry_point,
389                                                        p->duration
390                                                        )
391                                         );
392                         } catch (MXFFileError) {
393                                 if (require_mxfs) {
394                                         throw;
395                                 }
396                         }
397                         
398                 }
399                 
400                 if ((*i)->asset_list->main_sound) {
401                         
402                         try {
403                                 sound.reset (new SoundAsset (
404                                                      _directory,
405                                                      asset_map->asset_from_id ((*i)->asset_list->main_sound->id)->chunks.front()->path,
406                                                      _fps,
407                                                      (*i)->asset_list->main_sound->entry_point,
408                                                      (*i)->asset_list->main_sound->duration
409                                                      )
410                                         );
411                         } catch (MXFFileError) {
412                                 if (require_mxfs) {
413                                         throw;
414                                 }
415                         }
416                 }
417
418                 if ((*i)->asset_list->main_subtitle) {
419                         
420                         subtitle.reset (new SubtitleAsset (
421                                                 _directory,
422                                                 asset_map->asset_from_id ((*i)->asset_list->main_subtitle->id)->chunks.front()->path
423                                                 )
424                                 );
425                 }
426                         
427                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
428         }
429 }
430
431 void
432 CPL::add_reel (shared_ptr<const Reel> reel)
433 {
434         _reels.push_back (reel);
435 }
436
437 void
438 CPL::write_xml (bool encrypted, CertificateChain const & certificates, string const & signer_key) const
439 {
440         boost::filesystem::path p;
441         p /= _directory;
442         stringstream s;
443         s << _uuid << "_cpl.xml";
444         p /= s.str();
445
446         xmlpp::Document doc;
447         xmlpp::Element* cpl = doc.create_root_node("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
448
449         if (encrypted) {
450                 cpl->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
451         }
452
453         cpl->add_child("Id")->add_child_text ("urn:uuid:" + _uuid);
454         cpl->add_child("AnnotationText")->add_child_text (_name);
455         cpl->add_child("IssueDate")->add_child_text (Metadata::instance()->issue_date);
456         cpl->add_child("Creator")->add_child_text (Metadata::instance()->creator);
457         cpl->add_child("ContentTitleText")->add_child_text (_name);
458         cpl->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
459
460         {
461                 xmlpp::Element* cv = cpl->add_child ("ContentVersion");
462                 cv->add_child("Id")->add_child_text ("urn:uri:" + _uuid + "_" + Metadata::instance()->issue_date);
463                 cv->add_child("LabelText")->add_child_text (_uuid + "_" + Metadata::instance()->issue_date);
464         }
465
466         cpl->add_child("RatingList");
467
468         xmlpp::Element* reel_list = cpl->add_child("ReelList");
469         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
470                 (*i)->write_to_cpl (reel_list);
471         }
472
473         if (encrypted) {
474                 sign (cpl, certificates, signer_key);
475         }
476
477         doc.write_to_file_formatted (p.string(), "UTF-8");
478
479         _digest = make_digest (p.string (), 0);
480         _length = boost::filesystem::file_size (p.string ());
481 }
482
483 void
484 CPL::write_to_pkl (xmlpp::Element* p) const
485 {
486         xmlpp::Element* asset = p->add_child("Asset");
487         asset->add_child("Id")->add_child_text("urn:uuid:" + _uuid);
488         asset->add_child("Hash")->add_child_text(_digest);
489         asset->add_child("Size")->add_child_text(boost::lexical_cast<string> (_length));
490         asset->add_child("Type")->add_child_text("text/xml");
491 }
492
493 list<shared_ptr<const Asset> >
494 CPL::assets () const
495 {
496         list<shared_ptr<const Asset> > a;
497         for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
498                 if ((*i)->main_picture ()) {
499                         a.push_back ((*i)->main_picture ());
500                 }
501                 if ((*i)->main_sound ()) {
502                         a.push_back ((*i)->main_sound ());
503                 }
504                 if ((*i)->main_subtitle ()) {
505                         a.push_back ((*i)->main_subtitle ());
506                 }
507         }
508
509         return a;
510 }
511
512 void
513 CPL::write_to_assetmap (ostream& s) const
514 {
515         s << "    <Asset>\n"
516           << "      <Id>urn:uuid:" << _uuid << "</Id>\n"
517           << "      <ChunkList>\n"
518           << "        <Chunk>\n"
519           << "          <Path>" << _uuid << "_cpl.xml</Path>\n"
520           << "          <VolumeIndex>1</VolumeIndex>\n"
521           << "          <Offset>0</Offset>\n"
522           << "          <Length>" << _length << "</Length>\n"
523           << "        </Chunk>\n"
524           << "      </ChunkList>\n"
525           << "    </Asset>\n";
526 }
527         
528         
529         
530 bool
531 CPL::equals (CPL const & other, EqualityOptions opt, list<string>& notes) const
532 {
533         if (_name != other._name) {
534                 notes.push_back ("names differ");
535                 return false;
536         }
537
538         if (_content_kind != other._content_kind) {
539                 notes.push_back ("content kinds differ");
540                 return false;
541         }
542
543         if (_fps != other._fps) {
544                 notes.push_back ("frames per second differ");
545                 return false;
546         }
547
548         if (_length != other._length) {
549                 notes.push_back ("lengths differ");
550                 return false;
551         }
552
553         if (_reels.size() != other._reels.size()) {
554                 notes.push_back ("reel counts differ");
555                 return false;
556         }
557         
558         list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
559         list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
560         
561         while (a != _reels.end ()) {
562                 if (!(*a)->equals (*b, opt, notes)) {
563                         return false;
564                 }
565                 ++a;
566                 ++b;
567         }
568
569         return true;
570 }
571
572 shared_ptr<xmlpp::Document>
573 CPL::make_kdm (CertificateChain const & certificates, string const & signer_key, shared_ptr<const Certificate> recipient_cert) const
574 {
575         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
576         xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
577         root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
578         root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
579         root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
580
581         {
582                 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
583                 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
584                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
585                 
586                 authenticated_public->add_child("MessageId")->add_child_text("urn:uuid:" + make_uuid());
587                 authenticated_public->add_child("MessageType")->add_child_text("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
588                 authenticated_public->add_child("AnnotationText")->add_child_text(Metadata::instance()->product_name);
589                 authenticated_public->add_child("IssueDate")->add_child_text(Metadata::instance()->issue_date);
590
591                 {
592                         xmlpp::Element* signer = authenticated_public->add_child("Signer");
593                         signer->add_child("X509IssuerName", "ds")->add_child_text (
594                                 Certificate::name_for_xml (recipient_cert->issuer())
595                                 );
596                         signer->add_child("X509SerialNumber", "ds")->add_child_text (
597                                 recipient_cert->serial()
598                                 );
599                 }
600
601                 {
602                         xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
603
604                         {
605                                 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
606                                 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
607                                 {
608                                         xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
609                                         {
610                                                 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
611                                                 serial_element->add_child("X509IssuerName", "ds")->add_child_text (
612                                                         Certificate::name_for_xml (recipient_cert->issuer())
613                                                         );
614                                                 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (
615                                                         recipient_cert->serial()
616                                                         );
617                                         }
618
619                                         recipient->add_child("X509SubjectName")->add_child_text (Certificate::name_for_xml (recipient_cert->subject()));
620                                 }
621
622                                 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("XXX");
623                                 kdm_required_extensions->add_child("ContentTitleText")->add_child_text("XXX");
624                                 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text("XXX");
625                                 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text("XXX");
626                                 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text("XXX");
627
628                                 {
629                                         xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
630                                         authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
631                                         authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
632                                         {
633                                                 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
634                                                 device_list->add_child("CertificateThumbprint")->add_child_text("XXX");
635                                         }
636                                 }
637
638                                 {
639                                         xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
640                                         list<shared_ptr<const Asset> > a = assets();
641                                         for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
642                                                 /* XXX: non-MXF assets? */
643                                                 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
644                                                 if (mxf) {
645                                                         mxf->add_typed_key_id (key_id_list);
646                                                 }
647                                         }
648                                 }
649
650                                 {
651                                         xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
652                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
653                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
654                                                 );
655                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
656                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
657                                                 );
658                                 }
659                         }
660                 }
661                                          
662                 authenticated_public->add_child("NonCriticalExtensions");
663         }
664
665         {
666                 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
667                 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
668                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
669                 {
670                         xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
671                         {
672                                 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
673                                 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
674                                 encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
675                         }
676
677                         xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
678                         cipher_data->add_child("CipherValue", "enc")->add_child_text("XXX");
679                 }
680         }
681         
682         /* XXX: x2 one for each mxf? */
683
684         {
685                 xmlpp::Element* signature = root->add_child("Signature", "ds");
686                 
687                 {
688                         xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
689                         signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
690                                 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
691                                 );
692                         signed_info->add_child("SignatureMethod", "ds")->set_attribute(
693                                 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
694                                 );
695                         {
696                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
697                                 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
698                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
699                                 reference->add_child("DigestValue", "ds");
700                         }
701                         
702                         {                               
703                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
704                                 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
705                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
706                                 reference->add_child("DigestValue", "ds");
707                         }
708                 }
709                 
710                 add_signature_value (signature, certificates, signer_key, "ds");
711         }
712
713         return doc;
714 }