2 Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "encrypted_kdm.h"
22 #include "certificate_chain.h"
23 #include <libcxml/cxml.h>
24 #include <libxml++/document.h>
25 #include <libxml++/nodes/element.h>
26 #include <libxml/parser.h>
27 #include <boost/date_time/posix_time/posix_time.hpp>
28 #include <boost/foreach.hpp>
35 using boost::shared_ptr;
40 /** Namespace for classes used to hold our data; they are internal to this .cc file */
48 Signer (shared_ptr<const cxml::Node> node)
49 : x509_issuer_name (node->string_child ("X509IssuerName"))
50 , x509_serial_number (node->string_child ("X509SerialNumber"))
55 void as_xml (xmlpp::Element* node) const
57 node->add_child("X509IssuerName", "ds")->add_child_text (x509_issuer_name);
58 node->add_child("X509SerialNumber", "ds")->add_child_text (x509_serial_number);
61 string x509_issuer_name;
62 string x509_serial_number;
70 X509Data (boost::shared_ptr<const cxml::Node> node)
71 : x509_issuer_serial (Signer (node->node_child ("X509IssuerSerial")))
72 , x509_certificate (node->string_child ("X509Certificate"))
77 void as_xml (xmlpp::Element* node) const
79 x509_issuer_serial.as_xml (node->add_child ("X509IssuerSerial", "ds"));
80 node->add_child("X509Certificate", "ds")->add_child_text (x509_certificate);
83 Signer x509_issuer_serial;
84 std::string x509_certificate;
96 Reference (shared_ptr<const cxml::Node> node)
97 : uri (node->string_attribute ("URI"))
98 , digest_value (node->string_child ("DigestValue"))
103 void as_xml (xmlpp::Element* node) const
105 node->set_attribute ("URI", uri);
106 node->add_child("DigestMethod", "ds")->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#sha256");
107 node->add_child("DigestValue", "ds")->add_child_text (digest_value);
118 : authenticated_public ("#ID_AuthenticatedPublic")
119 , authenticated_private ("#ID_AuthenticatedPrivate")
122 SignedInfo (shared_ptr<const cxml::Node> node)
124 list<shared_ptr<cxml::Node> > references = node->node_children ("Reference");
125 for (list<shared_ptr<cxml::Node> >::const_iterator i = references.begin(); i != references.end(); ++i) {
126 if ((*i)->string_attribute ("URI") == "#ID_AuthenticatedPublic") {
127 authenticated_public = Reference (*i);
128 } else if ((*i)->string_attribute ("URI") == "#ID_AuthenticatedPrivate") {
129 authenticated_private = Reference (*i);
132 /* XXX: do something if we don't recognise the node */
136 void as_xml (xmlpp::Element* node) const
138 node->add_child ("CanonicalizationMethod", "ds")->set_attribute (
139 "Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"
142 node->add_child ("SignatureMethod", "ds")->set_attribute (
143 "Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"
146 authenticated_public.as_xml (node->add_child ("Reference", "ds"));
147 authenticated_private.as_xml (node->add_child ("Reference", "ds"));
151 Reference authenticated_public;
152 Reference authenticated_private;
160 Signature (shared_ptr<const cxml::Node> node)
161 : signed_info (node->node_child ("SignedInfo"))
162 , signature_value (node->string_child ("SignatureValue"))
164 list<shared_ptr<cxml::Node> > x509_data_nodes = node->node_child("KeyInfo")->node_children ("X509Data");
165 for (list<shared_ptr<cxml::Node> >::const_iterator i = x509_data_nodes.begin(); i != x509_data_nodes.end(); ++i) {
166 x509_data.push_back (X509Data (*i));
170 void as_xml (xmlpp::Node* node) const
172 signed_info.as_xml (node->add_child ("SignedInfo", "ds"));
173 node->add_child("SignatureValue", "ds")->add_child_text (signature_value);
175 xmlpp::Element* key_info_node = node->add_child ("KeyInfo", "ds");
176 for (std::list<X509Data>::const_iterator i = x509_data.begin(); i != x509_data.end(); ++i) {
177 i->as_xml (key_info_node->add_child ("X509Data", "ds"));
181 SignedInfo signed_info;
182 string signature_value;
183 list<X509Data> x509_data;
186 class AuthenticatedPrivate
189 AuthenticatedPrivate () {}
191 AuthenticatedPrivate (shared_ptr<const cxml::Node> node)
193 list<shared_ptr<cxml::Node> > encrypted_key_nodes = node->node_children ("EncryptedKey");
194 for (list<shared_ptr<cxml::Node> >::const_iterator i = encrypted_key_nodes.begin(); i != encrypted_key_nodes.end(); ++i) {
195 encrypted_key.push_back ((*i)->node_child("CipherData")->string_child ("CipherValue"));
199 void as_xml (xmlpp::Element* node, map<string, xmlpp::Attribute *>& references) const
201 references["ID_AuthenticatedPrivate"] = node->set_attribute ("Id", "ID_AuthenticatedPrivate");
203 for (list<string>::const_iterator i = encrypted_key.begin(); i != encrypted_key.end(); ++i) {
204 xmlpp::Element* encrypted_key = node->add_child ("EncryptedKey", "enc");
205 xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
206 encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
207 xmlpp::Element* digest_method = encryption_method->add_child ("DigestMethod", "ds");
208 digest_method->set_attribute ("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
209 xmlpp::Element* cipher_data = encrypted_key->add_child ("CipherData", "enc");
210 cipher_data->add_child("CipherValue", "enc")->add_child_text (*i);
214 list<string> encrypted_key;
222 TypedKeyId (shared_ptr<const cxml::Node> node)
223 : key_type (node->string_child ("KeyType"))
224 , key_id (node->string_child ("KeyId").substr (9))
229 TypedKeyId (string type, string id)
234 void as_xml (xmlpp::Element* node) const
236 xmlpp::Element* type = node->add_child("KeyType");
237 type->add_child_text (key_type);
238 node->add_child("KeyId")->add_child_text ("urn:uuid:" + key_id);
239 /* XXX: this feels like a bit of a hack */
240 if (key_type == "MDEK") {
241 type->set_attribute ("scope", "http://www.dolby.com/cp850/2012/KDM#kdm-key-type");
254 KeyIdList (shared_ptr<const cxml::Node> node)
256 list<shared_ptr<cxml::Node> > typed_key_id_nodes = node->node_children ("TypedKeyId");
257 for (list<shared_ptr<cxml::Node> >::const_iterator i = typed_key_id_nodes.begin(); i != typed_key_id_nodes.end(); ++i) {
258 typed_key_id.push_back (TypedKeyId (*i));
262 void as_xml (xmlpp::Element* node) const
264 for (list<TypedKeyId>::const_iterator i = typed_key_id.begin(); i != typed_key_id.end(); ++i) {
265 i->as_xml (node->add_child("TypedKeyId"));
269 list<TypedKeyId> typed_key_id;
272 class AuthorizedDeviceInfo
275 AuthorizedDeviceInfo () {}
277 AuthorizedDeviceInfo (shared_ptr<const cxml::Node> node)
278 : device_list_identifier (node->string_child ("DeviceListIdentifier").substr (9))
279 , device_list_description (node->optional_string_child ("DeviceListDescription"))
281 BOOST_FOREACH (cxml::ConstNodePtr i, node->node_child("DeviceList")->node_children("CertificateThumbprint")) {
282 certificate_thumbprints.push_back (i->content ());
286 void as_xml (xmlpp::Element* node) const
288 node->add_child ("DeviceListIdentifier")->add_child_text ("urn:uuid:" + device_list_identifier);
289 if (device_list_description) {
290 node->add_child ("DeviceListDescription")->add_child_text (device_list_description.get());
292 xmlpp::Element* device_list = node->add_child ("DeviceList");
293 BOOST_FOREACH (string i, certificate_thumbprints) {
294 device_list->add_child("CertificateThumbprint")->add_child_text (i);
298 /** DeviceListIdentifier without the urn:uuid: prefix */
299 string device_list_identifier;
300 boost::optional<string> device_list_description;
301 std::list<string> certificate_thumbprints;
304 class X509IssuerSerial
307 X509IssuerSerial () {}
309 X509IssuerSerial (shared_ptr<const cxml::Node> node)
310 : x509_issuer_name (node->string_child ("X509IssuerName"))
311 , x509_serial_number (node->string_child ("X509SerialNumber"))
316 void as_xml (xmlpp::Element* node) const
318 node->add_child("X509IssuerName", "ds")->add_child_text (x509_issuer_name);
319 node->add_child("X509SerialNumber", "ds")->add_child_text (x509_serial_number);
322 string x509_issuer_name;
323 string x509_serial_number;
331 Recipient (shared_ptr<const cxml::Node> node)
332 : x509_issuer_serial (node->node_child ("X509IssuerSerial"))
333 , x509_subject_name (node->string_child ("X509SubjectName"))
338 void as_xml (xmlpp::Element* node) const
340 x509_issuer_serial.as_xml (node->add_child ("X509IssuerSerial"));
341 node->add_child("X509SubjectName")->add_child_text (x509_subject_name);
344 X509IssuerSerial x509_issuer_serial;
345 string x509_subject_name;
348 class KDMRequiredExtensions
351 KDMRequiredExtensions () {}
353 KDMRequiredExtensions (shared_ptr<const cxml::Node> node)
354 : recipient (node->node_child ("Recipient"))
355 , composition_playlist_id (node->string_child ("CompositionPlaylistId").substr (9))
356 , content_title_text (node->string_child ("ContentTitleText"))
357 , not_valid_before (node->string_child ("ContentKeysNotValidBefore"))
358 , not_valid_after (node->string_child ("ContentKeysNotValidAfter"))
359 , authorized_device_info (node->node_child ("AuthorizedDeviceInfo"))
360 , key_id_list (node->node_child ("KeyIdList"))
365 void as_xml (xmlpp::Element* node) const
367 node->set_attribute ("xmlns", "http://www.smpte-ra.org/schemas/430-1/2006/KDM");
369 recipient.as_xml (node->add_child ("Recipient"));
370 node->add_child("CompositionPlaylistId")->add_child_text ("urn:uuid:" + composition_playlist_id);
371 node->add_child("ContentTitleText")->add_child_text (content_title_text);
372 if (content_authenticator) {
373 node->add_child("ContentAuthenticator")->add_child_text (content_authenticator.get ());
375 node->add_child("ContentKeysNotValidBefore")->add_child_text (not_valid_before.as_string ());
376 node->add_child("ContentKeysNotValidAfter")->add_child_text (not_valid_after.as_string ());
377 authorized_device_info.as_xml (node->add_child ("AuthorizedDeviceInfo"));
378 key_id_list.as_xml (node->add_child ("KeyIdList"));
380 xmlpp::Element* forensic_mark_flag_list = node->add_child ("ForensicMarkFlagList");
381 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
382 forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
386 string composition_playlist_id;
387 boost::optional<string> content_authenticator;
388 string content_title_text;
389 LocalTime not_valid_before;
390 LocalTime not_valid_after;
391 AuthorizedDeviceInfo authorized_device_info;
392 KeyIdList key_id_list;
395 class RequiredExtensions
398 RequiredExtensions () {}
400 RequiredExtensions (shared_ptr<const cxml::Node> node)
401 : kdm_required_extensions (node->node_child ("KDMRequiredExtensions"))
406 void as_xml (xmlpp::Element* node) const
408 kdm_required_extensions.as_xml (node->add_child ("KDMRequiredExtensions"));
411 KDMRequiredExtensions kdm_required_extensions;
414 class AuthenticatedPublic
417 AuthenticatedPublic ()
418 : message_id (make_uuid ())
419 , issue_date (LocalTime().as_string ())
422 AuthenticatedPublic (shared_ptr<const cxml::Node> node)
423 : message_id (node->string_child ("MessageId").substr (9))
424 , annotation_text (node->string_child ("AnnotationText"))
425 , issue_date (node->string_child ("IssueDate"))
426 , signer (node->node_child ("Signer"))
427 , required_extensions (node->node_child ("RequiredExtensions"))
432 void as_xml (xmlpp::Element* node, map<string, xmlpp::Attribute *>& references) const
434 references["ID_AuthenticatedPublic"] = node->set_attribute ("Id", "ID_AuthenticatedPublic");
436 node->add_child("MessageId")->add_child_text ("urn:uuid:" + message_id);
437 node->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
438 node->add_child("AnnotationText")->add_child_text (annotation_text);
439 node->add_child("IssueDate")->add_child_text (issue_date);
441 signer.as_xml (node->add_child ("Signer"));
442 required_extensions.as_xml (node->add_child ("RequiredExtensions"));
444 node->add_child ("NonCriticalExtensions");
448 string annotation_text;
451 RequiredExtensions required_extensions;
454 /** Class to describe our data. We use a class hierarchy as it's a bit nicer
455 * for XML data than a flat description.
457 class EncryptedKDMData
465 EncryptedKDMData (shared_ptr<const cxml::Node> node)
466 : authenticated_public (node->node_child ("AuthenticatedPublic"))
467 , authenticated_private (node->node_child ("AuthenticatedPrivate"))
468 , signature (node->node_child ("Signature"))
473 shared_ptr<xmlpp::Document> as_xml () const
475 shared_ptr<xmlpp::Document> document (new xmlpp::Document ());
476 xmlpp::Element* root = document->create_root_node ("DCinemaSecurityMessage", "http://www.smpte-ra.org/schemas/430-3/2006/ETM");
477 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
478 root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
479 map<string, xmlpp::Attribute *> references;
480 authenticated_public.as_xml (root->add_child ("AuthenticatedPublic"), references);
481 authenticated_private.as_xml (root->add_child ("AuthenticatedPrivate"), references);
482 signature.as_xml (root->add_child ("Signature", "ds"));
484 for (map<string, xmlpp::Attribute*>::const_iterator i = references.begin(); i != references.end(); ++i) {
485 xmlAddID (0, document->cobj(), (const xmlChar *) i->first.c_str(), i->second->cobj ());
491 AuthenticatedPublic authenticated_public;
492 AuthenticatedPrivate authenticated_private;
499 EncryptedKDM::EncryptedKDM (string s)
501 shared_ptr<cxml::Document> doc (new cxml::Document ("DCinemaSecurityMessage"));
502 doc->read_string (s);
503 _data = new data::EncryptedKDMData (doc);
506 EncryptedKDM::EncryptedKDM (
507 shared_ptr<const CertificateChain> signer,
508 Certificate recipient,
509 vector<Certificate> trusted_devices,
510 string device_list_description,
512 string content_title_text,
513 LocalTime not_valid_before,
514 LocalTime not_valid_after,
515 Formulation formulation,
516 list<pair<string, string> > key_ids,
519 : _data (new data::EncryptedKDMData)
521 /* Fill our XML-ish description in with the juicy bits that the caller has given */
523 data::AuthenticatedPublic& aup = _data->authenticated_public;
524 aup.signer.x509_issuer_name = signer->leaf().issuer ();
525 aup.signer.x509_serial_number = signer->leaf().serial ();
527 data::KDMRequiredExtensions& kre = _data->authenticated_public.required_extensions.kdm_required_extensions;
528 kre.recipient.x509_issuer_serial.x509_issuer_name = recipient.issuer ();
529 kre.recipient.x509_issuer_serial.x509_serial_number = recipient.serial ();
530 kre.recipient.x509_subject_name = recipient.subject ();
531 kre.authorized_device_info.device_list_description = device_list_description;
532 kre.composition_playlist_id = cpl_id;
533 if (formulation == DCI_ANY || formulation == DCI_SPECIFIC) {
534 kre.content_authenticator = signer->leaf().thumbprint ();
536 kre.content_title_text = content_title_text;
537 kre.not_valid_before = not_valid_before;
538 kre.not_valid_after = not_valid_after;
539 kre.authorized_device_info.device_list_identifier = make_uuid ();
540 string n = recipient.subject_common_name ();
541 if (n.find (".") != string::npos) {
542 n = n.substr (n.find (".") + 1);
544 kre.authorized_device_info.device_list_description = n;
546 if (formulation == MODIFIED_TRANSITIONAL_1 || formulation == DCI_ANY) {
547 /* Use the "assume trust" thumbprint */
548 kre.authorized_device_info.certificate_thumbprints.push_back ("2jmj7l5rSw0yVb/vlWAYkK/YBwk=");
549 } else if (formulation == DCI_SPECIFIC) {
550 /* As I read the standard we should use the recipient
551 /and/ other trusted device thumbprints here. MJD
552 reports that this doesn't work with his setup;
553 a working KDM does not include the recipient's
554 thumbprint (recipient.thumbprint()).
556 BOOST_FOREACH (Certificate const & i, trusted_devices) {
557 kre.authorized_device_info.certificate_thumbprints.push_back (i.thumbprint ());
561 for (list<pair<string, string> >::const_iterator i = key_ids.begin(); i != key_ids.end(); ++i) {
562 kre.key_id_list.typed_key_id.push_back (data::TypedKeyId (i->first, i->second));
565 _data->authenticated_private.encrypted_key = keys;
567 /* Read the XML so far and sign it */
568 shared_ptr<xmlpp::Document> doc = _data->as_xml ();
569 xmlpp::Node::NodeList children = doc->get_root_node()->get_children ();
570 for (xmlpp::Node::NodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
571 if ((*i)->get_name() == "Signature") {
572 signer->add_signature_value (*i, "ds");
576 /* Read the bits that add_signature_value did back into our variables */
577 shared_ptr<cxml::Node> signed_doc (new cxml::Node (doc->get_root_node ()));
578 _data->signature = data::Signature (signed_doc->node_child ("Signature"));
581 EncryptedKDM::EncryptedKDM (EncryptedKDM const & other)
582 : _data (new data::EncryptedKDMData (*other._data))
588 EncryptedKDM::operator= (EncryptedKDM const & other)
590 if (this == &other) {
595 _data = new data::EncryptedKDMData (*other._data);
599 EncryptedKDM::~EncryptedKDM ()
605 EncryptedKDM::as_xml (boost::filesystem::path path) const
607 FILE* f = fopen_boost (path, "w");
608 string const x = as_xml ();
609 fwrite (x.c_str(), 1, x.length(), f);
614 EncryptedKDM::as_xml () const
616 return _data->as_xml()->write_to_string ("UTF-8");
620 EncryptedKDM::keys () const
622 return _data->authenticated_private.encrypted_key;
626 EncryptedKDM::annotation_text () const
628 return _data->authenticated_public.annotation_text;
632 EncryptedKDM::content_title_text () const
634 return _data->authenticated_public.required_extensions.kdm_required_extensions.content_title_text;
638 EncryptedKDM::cpl_id () const
640 return _data->authenticated_public.required_extensions.kdm_required_extensions.composition_playlist_id;
644 EncryptedKDM::issue_date () const
646 return _data->authenticated_public.issue_date;
650 dcp::operator== (EncryptedKDM const & a, EncryptedKDM const & b)
652 /* Not exactly efficient... */
653 return a.as_xml() == b.as_xml();