Various encryption-related stuff.
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012-2013 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 #include <fstream>
21 #include <libxml/parser.h>
22 #include "cpl.h"
23 #include "parse/cpl.h"
24 #include "util.h"
25 #include "picture_asset.h"
26 #include "sound_asset.h"
27 #include "subtitle_asset.h"
28 #include "parse/asset_map.h"
29 #include "reel.h"
30 #include "metadata.h"
31 #include "signer.h"
32 #include "exceptions.h"
33 #include "compose.hpp"
34
35 using std::string;
36 using std::stringstream;
37 using std::ofstream;
38 using std::ostream;
39 using std::list;
40 using std::pair;
41 using std::make_pair;
42 using boost::shared_ptr;
43 using boost::lexical_cast;
44 using boost::optional;
45 using namespace libdcp;
46
47 CPL::CPL (string directory, string name, ContentKind content_kind, int length, int frames_per_second)
48         : _directory (directory)
49         , _name (name)
50         , _content_kind (content_kind)
51         , _length (length)
52         , _fps (frames_per_second)
53 {
54         _id = make_uuid ();
55 }
56
57 /** Construct a CPL object from a XML file.
58  *  @param directory The directory containing this CPL's DCP.
59  *  @param file The CPL XML filename.
60  *  @param asset_maps AssetMaps to look for assets in.
61  *  @param require_mxfs true to throw an exception if a required MXF file does not exist.
62  */
63 CPL::CPL (string directory, string file, list<PathAssetMap> asset_maps, bool require_mxfs)
64         : _directory (directory)
65         , _content_kind (FEATURE)
66         , _length (0)
67         , _fps (0)
68 {
69         /* Read the XML */
70         shared_ptr<parse::CPL> cpl;
71         try {
72                 cpl.reset (new parse::CPL (file));
73         } catch (FileError& e) {
74                 boost::throw_exception (FileError ("could not load CPL file", file));
75         }
76         
77         /* Now cherry-pick the required bits into our own data structure */
78         
79         _name = cpl->annotation_text;
80         _content_kind = cpl->content_kind;
81
82         /* Trim urn:uuid: off the front */
83         _id = cpl->id.substr (9);
84
85         for (list<shared_ptr<parse::Reel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
86
87                 shared_ptr<parse::Picture> p;
88
89                 if ((*i)->asset_list->main_picture) {
90                         p = (*i)->asset_list->main_picture;
91                 } else {
92                         p = (*i)->asset_list->main_stereoscopic_picture;
93                 }
94                 
95                 _fps = p->edit_rate.numerator;
96                 _length += p->duration;
97
98                 shared_ptr<PictureAsset> picture;
99                 shared_ptr<SoundAsset> sound;
100                 shared_ptr<SubtitleAsset> subtitle;
101
102                 /* Some rather twisted logic to decide if we are 3D or not;
103                    some DCPs give a MainStereoscopicPicture to indicate 3D, others
104                    just have a FrameRate twice the EditRate and apparently
105                    expect you to divine the fact that they are hence 3D.
106                 */
107
108                 if (!(*i)->asset_list->main_stereoscopic_picture && p->edit_rate == p->frame_rate) {
109
110                         pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
111
112                         try {
113                                 picture.reset (new MonoPictureAsset (
114                                                        asset.first,
115                                                        asset.second->chunks.front()->path
116                                                        )
117                                         );
118
119                                 picture->set_entry_point (p->entry_point);
120                                 picture->set_duration (p->duration);
121                                 if (p->key_id.length() > 9) {
122                                         /* Trim urn:uuid: */
123                                         picture->set_key_id (p->key_id.substr (9));
124                                 }
125                         } catch (MXFFileError) {
126                                 if (require_mxfs) {
127                                         throw;
128                                 }
129                         }
130                         
131                 } else {
132                         try {
133                                 pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, p->id);
134
135                                 picture.reset (new StereoPictureAsset (
136                                                        asset.first,
137                                                        asset.second->chunks.front()->path,
138                                                        _fps,
139                                                        p->duration
140                                                        )
141                                         );
142
143                                 picture->set_entry_point (p->entry_point);
144                                 picture->set_duration (p->duration);
145                                 if (p->key_id.length() > 9) {
146                                         /* Trim urn:uuid: */
147                                         picture->set_key_id (p->key_id.substr (9));
148                                 }
149                                 
150                         } catch (MXFFileError) {
151                                 if (require_mxfs) {
152                                         throw;
153                                 }
154                         }
155                         
156                 }
157                 
158                 if ((*i)->asset_list->main_sound) {
159                         
160                         try {
161                                 pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_sound->id);
162                         
163                                 sound.reset (new SoundAsset (
164                                                      asset.first,
165                                                      asset.second->chunks.front()->path
166                                                      )
167                                         );
168
169                                 shared_ptr<parse::MainSound> s = (*i)->asset_list->main_sound;
170
171                                 sound->set_entry_point (s->entry_point);
172                                 sound->set_duration (s->duration);
173                                 if (s->key_id.length() > 9) {
174                                         /* Trim urn:uuid: */
175                                         sound->set_key_id (s->key_id.substr (9));
176                                 }
177                         } catch (MXFFileError) {
178                                 if (require_mxfs) {
179                                         throw;
180                                 }
181                         }
182                 }
183
184                 if ((*i)->asset_list->main_subtitle) {
185                         
186                         pair<string, shared_ptr<const parse::AssetMapAsset> > asset = asset_from_id (asset_maps, (*i)->asset_list->main_subtitle->id);
187
188                         subtitle.reset (new SubtitleAsset (
189                                                 asset.first,
190                                                 asset.second->chunks.front()->path
191                                                 )
192                                 );
193
194                         subtitle->set_entry_point ((*i)->asset_list->main_subtitle->entry_point);
195                         subtitle->set_duration ((*i)->asset_list->main_subtitle->duration);
196                 }
197                         
198                 _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
199         }
200 }
201
202 void
203 CPL::add_reel (shared_ptr<Reel> reel)
204 {
205         _reels.push_back (reel);
206 }
207
208 void
209 CPL::write_xml (bool interop, XMLMetadata const & metadata, shared_ptr<const Signer> signer) const
210 {
211         boost::filesystem::path p;
212         p /= _directory;
213         stringstream s;
214         s << _id << "_cpl.xml";
215         p /= s.str();
216
217         xmlpp::Document doc;
218         xmlpp::Element* root;
219         if (interop) {
220                 root = doc.create_root_node ("CompositionPlaylist", "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#");
221         } else {
222                 root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
223         }
224
225         if (signer) {
226                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
227         }
228         
229         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
230         root->add_child("AnnotationText")->add_child_text (_name);
231         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
232         root->add_child("Issuer")->add_child_text (metadata.issuer);
233         root->add_child("Creator")->add_child_text (metadata.creator);
234         root->add_child("ContentTitleText")->add_child_text (_name);
235         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
236         {
237                 xmlpp::Node* cv = root->add_child ("ContentVersion");
238                 cv->add_child ("Id")->add_child_text ("urn:uri:" + _id + "_" + metadata.issue_date);
239                 cv->add_child ("LabelText")->add_child_text (_id + "_" + metadata.issue_date);
240         }
241         root->add_child("RatingList");
242
243         xmlpp::Element* reel_list = root->add_child ("ReelList");
244         
245         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
246                 (*i)->write_to_cpl (reel_list, interop);
247         }
248
249         if (signer) {
250                 signer->sign (root, interop);
251         }
252
253         doc.write_to_file_formatted (p.string (), "UTF-8");
254
255         _digest = make_digest (p.string (), 0);
256         _length = boost::filesystem::file_size (p.string ());
257 }
258
259 void
260 CPL::write_to_pkl (xmlpp::Node* node) const
261 {
262         xmlpp::Node* asset = node->add_child ("Asset");
263         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
264         asset->add_child("Hash")->add_child_text (_digest);
265         asset->add_child("Size")->add_child_text (lexical_cast<string> (_length));
266         asset->add_child("Type")->add_child_text ("text/xml");
267 }
268
269 list<shared_ptr<const Asset> >
270 CPL::assets () const
271 {
272         list<shared_ptr<const Asset> > a;
273         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
274                 if ((*i)->main_picture ()) {
275                         a.push_back ((*i)->main_picture ());
276                 }
277                 if ((*i)->main_sound ()) {
278                         a.push_back ((*i)->main_sound ());
279                 }
280                 if ((*i)->main_subtitle ()) {
281                         a.push_back ((*i)->main_subtitle ());
282                 }
283         }
284
285         return a;
286 }
287
288 void
289 CPL::write_to_assetmap (xmlpp::Node* node) const
290 {
291         xmlpp::Node* asset = node->add_child ("Asset");
292         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
293         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
294         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
295         chunk->add_child("Path")->add_child_text (_id + "_cpl.xml");
296         chunk->add_child("VolumeIndex")->add_child_text ("1");
297         chunk->add_child("Offset")->add_child_text("0");
298         chunk->add_child("Length")->add_child_text(lexical_cast<string> (_length));
299 }
300         
301         
302         
303 bool
304 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
305 {
306         if (_name != other._name && !opt.cpl_names_can_differ) {
307                 stringstream s;
308                 s << "names differ: " << _name << " vs " << other._name << "\n";
309                 note (ERROR, s.str ());
310                 return false;
311         }
312
313         if (_content_kind != other._content_kind) {
314                 note (ERROR, "content kinds differ");
315                 return false;
316         }
317
318         if (_fps != other._fps) {
319                 note (ERROR, String::compose ("frames per second differ (%1 vs %2)", _fps, other._fps));
320                 return false;
321         }
322
323         if (_length != other._length) {
324                 stringstream s;
325                 note (ERROR, String::compose ("lengths differ (%1 vs %2)", _length, other._length));
326         }
327
328         if (_reels.size() != other._reels.size()) {
329                 note (ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
330                 return false;
331         }
332         
333         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
334         list<shared_ptr<Reel> >::const_iterator b = other._reels.begin ();
335         
336         while (a != _reels.end ()) {
337                 if (!(*a)->equals (*b, opt, note)) {
338                         return false;
339                 }
340                 ++a;
341                 ++b;
342         }
343
344         return true;
345 }
346
347 shared_ptr<xmlpp::Document>
348 CPL::make_kdm (
349         shared_ptr<const Signer> signer,
350         shared_ptr<const Certificate> recipient_cert,
351         boost::posix_time::ptime from,
352         boost::posix_time::ptime until,
353         bool interop,
354         MXFMetadata const & mxf_metadata,
355         XMLMetadata const & xml_metadata
356         ) const
357 {
358         assert (recipient_cert);
359         
360         shared_ptr<xmlpp::Document> doc (new xmlpp::Document);
361         xmlpp::Element* root = doc->create_root_node ("DCinemaSecurityMessage");
362         root->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-3/2006/ETM", "");
363         root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
364         root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
365
366         {
367                 xmlpp::Element* authenticated_public = root->add_child("AuthenticatedPublic");
368                 authenticated_public->set_attribute("Id", "ID_AuthenticatedPublic");
369                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPublic", authenticated_public->get_attribute("Id")->cobj());
370                 
371                 authenticated_public->add_child("MessageId")->add_child_text ("urn:uuid:" + make_uuid());
372                 /* XXX: this should probably be different if interop == true */
373                 authenticated_public->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
374                 authenticated_public->add_child("AnnotationText")->add_child_text (mxf_metadata.product_name);
375                 authenticated_public->add_child("IssueDate")->add_child_text (xml_metadata.issue_date);
376
377                 {
378                         xmlpp::Element* xml_signer = authenticated_public->add_child("Signer");
379                         xml_signer->add_child("X509IssuerName", "ds")->add_child_text (signer->certificates().leaf()->issuer());
380                         xml_signer->add_child("X509SerialNumber", "ds")->add_child_text (signer->certificates().leaf()->serial());
381                 }
382
383                 {
384                         xmlpp::Element* required_extensions = authenticated_public->add_child("RequiredExtensions");
385
386                         {
387                                 xmlpp::Element* kdm_required_extensions = required_extensions->add_child("KDMRequiredExtensions");
388                                 kdm_required_extensions->set_namespace_declaration ("http://www.smpte-ra.org/schemas/430-1/2006/KDM");
389                                 {
390                                         xmlpp::Element* recipient = kdm_required_extensions->add_child("Recipient");
391                                         {
392                                                 xmlpp::Element* serial_element = recipient->add_child("X509IssuerSerial");
393                                                 serial_element->add_child("X509IssuerName", "ds")->add_child_text (recipient_cert->issuer());
394                                                 serial_element->add_child("X509SerialNumber", "ds")->add_child_text (recipient_cert->serial());
395                                         }
396
397                                         recipient->add_child("X509SubjectName")->add_child_text (recipient_cert->subject());
398                                 }
399
400                                 kdm_required_extensions->add_child("CompositionPlaylistId")->add_child_text("urn:uuid:" + _id);
401                                 kdm_required_extensions->add_child("ContentTitleText")->add_child_text(_name);
402                                 kdm_required_extensions->add_child("ContentAuthenticator")->add_child_text(signer->certificates().leaf()->thumbprint());
403                                 kdm_required_extensions->add_child("ContentKeysNotValidBefore")->add_child_text(ptime_to_string (from));
404                                 kdm_required_extensions->add_child("ContentKeysNotValidAfter")->add_child_text(ptime_to_string (until));
405
406                                 {
407                                         xmlpp::Element* authorized_device_info = kdm_required_extensions->add_child("AuthorizedDeviceInfo");
408                                         authorized_device_info->add_child("DeviceListIdentifier")->add_child_text("urn:uuid:" + make_uuid());
409                                         authorized_device_info->add_child("DeviceListDescription")->add_child_text(recipient_cert->subject());
410                                         {
411                                                 xmlpp::Element* device_list = authorized_device_info->add_child("DeviceList");
412                                                 device_list->add_child("CertificateThumbprint")->add_child_text(recipient_cert->thumbprint());
413                                         }
414                                 }
415
416                                 {
417                                         xmlpp::Element* key_id_list = kdm_required_extensions->add_child("KeyIdList");
418                                         list<shared_ptr<const Asset> > a = assets();
419                                         for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
420                                                 /* XXX: non-MXF assets? */
421                                                 shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
422                                                 if (mxf) {
423                                                         mxf->add_typed_key_id (key_id_list);
424                                                 }
425                                         }
426                                 }
427
428                                 {
429                                         xmlpp::Element* forensic_mark_flag_list = kdm_required_extensions->add_child("ForensicMarkFlagList");
430                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
431                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable"
432                                                 );
433                                         forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ( 
434                                                 "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable"
435                                                 );
436                                 }
437                         }
438                 }
439                                          
440                 authenticated_public->add_child("NonCriticalExtensions");
441         }
442
443         {
444                 xmlpp::Element* authenticated_private = root->add_child("AuthenticatedPrivate");
445                 authenticated_private->set_attribute ("Id", "ID_AuthenticatedPrivate");
446                 xmlAddID (0, doc->cobj(), (const xmlChar *) "ID_AuthenticatedPrivate", authenticated_private->get_attribute("Id")->cobj());
447
448                 /* Hex keys that we have already written into the node */
449                 list<Key> written_keys;
450
451                 list<shared_ptr<const Asset> > a = assets();
452                 for (list<shared_ptr<const Asset> >::iterator i = a.begin(); i != a.end(); ++i) {
453                         /* XXX: non-MXF assets? */
454                         shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
455                         if (!mxf || find (written_keys.begin(), written_keys.end(), mxf->key ()) != written_keys.end ()) {
456                                 continue;
457                         }
458                         
459                         xmlpp::Element* encrypted_key = authenticated_private->add_child ("EncryptedKey", "enc");
460                         xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
461                         encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
462                         encryption_method->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
463                         xmlpp::Element* cipher_data = authenticated_private->add_child ("CipherData", "enc");
464                         cipher_data->add_child("CipherValue", "enc")->add_child_text(mxf->key()->hex());
465
466                         written_keys.push_back (mxf->key().get());
467                 }
468         }
469         
470         {
471                 xmlpp::Element* signature = root->add_child("Signature", "ds");
472                 
473                 {
474                         xmlpp::Element* signed_info = signature->add_child("SignedInfo", "ds");
475                         signed_info->add_child("CanonicalizationMethod", "ds")->set_attribute(
476                                 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
477                                 );
478
479                         if (interop) {
480                                 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
481                                         "Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
482                                         );
483                         } else {
484                                 signed_info->add_child("SignatureMethod", "ds")->set_attribute(
485                                         "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
486                                         );
487                         }
488                         
489                         {
490                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
491                                 reference->set_attribute("URI", "#ID_AuthenticatedPublic");
492                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
493                                 reference->add_child("DigestValue", "ds");
494                         }
495                         
496                         {                               
497                                 xmlpp::Element* reference = signed_info->add_child("Reference", "ds");
498                                 reference->set_attribute("URI", "#ID_AuthenticatedPrivate");
499                                 reference->add_child("DigestMethod", "ds")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
500                                 reference->add_child("DigestValue", "ds");
501                         }
502                 }
503                 
504                 signer->add_signature_value (signature, "ds");
505         }
506
507         return doc;
508 }
509
510 /** @return true if we have any encrypted content */
511 bool
512 CPL::encrypted () const
513 {
514         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
515                 if ((*i)->encrypted ()) {
516                         return true;
517                 }
518         }
519
520         return false;
521 }
522
523 void
524 CPL::add_kdm (KDM const & kdm)
525 {
526         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
527                 (*i)->add_kdm (kdm);
528         }
529 }
530
531 pair<string, shared_ptr<const parse::AssetMapAsset> >
532 CPL::asset_from_id (list<PathAssetMap> asset_maps, string id) const
533 {
534         for (list<PathAssetMap>::const_iterator i = asset_maps.begin(); i != asset_maps.end(); ++i) {
535                 shared_ptr<parse::AssetMapAsset> a = i->second->asset_from_id (id);
536                 if (a) {
537                         return make_pair (i->first, a);
538                 }
539         }
540
541         return make_pair ("", shared_ptr<const parse::AssetMapAsset> ());
542 }