For testing: add a KDM formulation which omits AuthorizedDeviceInfo.
[libdcp.git] / src / encrypted_kdm.cc
index 13a9eb059e924a22366764a89b0bdd80b3d2d2cf..80c5e2ef9f9c4c4fa27eb534c8515c5daef59502 100644 (file)
@@ -1,20 +1,34 @@
 /*
-    Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2016 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
+    libdcp is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
 #include "encrypted_kdm.h"
@@ -33,6 +47,7 @@ using std::string;
 using std::map;
 using std::pair;
 using boost::shared_ptr;
+using boost::optional;
 using namespace dcp;
 
 namespace dcp {
@@ -45,7 +60,7 @@ class Signer
 public:
        Signer () {}
 
-       Signer (shared_ptr<const cxml::Node> node)
+       explicit Signer (shared_ptr<const cxml::Node> node)
                : x509_issuer_name (node->string_child ("X509IssuerName"))
                , x509_serial_number (node->string_child ("X509SerialNumber"))
        {
@@ -67,7 +82,7 @@ class X509Data
 public:
        X509Data () {}
 
-       X509Data (boost::shared_ptr<const cxml::Node> node)
+       explicit X509Data (boost::shared_ptr<const cxml::Node> node)
                : x509_issuer_serial (Signer (node->node_child ("X509IssuerSerial")))
                , x509_certificate (node->string_child ("X509Certificate"))
        {
@@ -89,11 +104,11 @@ class Reference
 public:
        Reference () {}
 
-       Reference (string u)
+       explicit Reference (string u)
                : uri (u)
        {}
 
-       Reference (shared_ptr<const cxml::Node> node)
+       explicit Reference (shared_ptr<const cxml::Node> node)
                : uri (node->string_attribute ("URI"))
                , digest_value (node->string_child ("DigestValue"))
        {
@@ -119,7 +134,7 @@ public:
                , authenticated_private ("#ID_AuthenticatedPrivate")
        {}
 
-       SignedInfo (shared_ptr<const cxml::Node> node)
+       explicit SignedInfo (shared_ptr<const cxml::Node> node)
        {
                list<shared_ptr<cxml::Node> > references = node->node_children ("Reference");
                for (list<shared_ptr<cxml::Node> >::const_iterator i = references.begin(); i != references.end(); ++i) {
@@ -157,7 +172,7 @@ class Signature
 public:
        Signature () {}
 
-       Signature (shared_ptr<const cxml::Node> node)
+       explicit Signature (shared_ptr<const cxml::Node> node)
                : signed_info (node->node_child ("SignedInfo"))
                , signature_value (node->string_child ("SignatureValue"))
        {
@@ -188,7 +203,7 @@ class AuthenticatedPrivate
 public:
        AuthenticatedPrivate () {}
 
-       AuthenticatedPrivate (shared_ptr<const cxml::Node> node)
+       explicit AuthenticatedPrivate (shared_ptr<const cxml::Node> node)
        {
                list<shared_ptr<cxml::Node> > encrypted_key_nodes = node->node_children ("EncryptedKey");
                for (list<shared_ptr<cxml::Node> >::const_iterator i = encrypted_key_nodes.begin(); i != encrypted_key_nodes.end(); ++i) {
@@ -202,9 +217,13 @@ public:
 
                for (list<string>::const_iterator i = encrypted_key.begin(); i != encrypted_key.end(); ++i) {
                        xmlpp::Element* encrypted_key = node->add_child ("EncryptedKey", "enc");
+                       /* XXX: hack for testing with Dolby */
+                       encrypted_key->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
                        xmlpp::Element* encryption_method = encrypted_key->add_child ("EncryptionMethod", "enc");
                        encryption_method->set_attribute ("Algorithm", "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p");
                        xmlpp::Element* digest_method = encryption_method->add_child ("DigestMethod", "ds");
+                       /* XXX: hack for testing with Dolby */
+                       digest_method->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
                        digest_method->set_attribute ("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
                        xmlpp::Element* cipher_data = encrypted_key->add_child ("CipherData", "enc");
                        cipher_data->add_child("CipherValue", "enc")->add_child_text (*i);
@@ -219,9 +238,9 @@ class TypedKeyId
 public:
        TypedKeyId () {}
 
-       TypedKeyId (shared_ptr<const cxml::Node> node)
+       explicit TypedKeyId (shared_ptr<const cxml::Node> node)
                : key_type (node->string_child ("KeyType"))
-               , key_id (node->string_child ("KeyId").substr (9))
+               , key_id (remove_urn_uuid (node->string_child ("KeyId")))
        {
 
        }
@@ -251,7 +270,7 @@ class KeyIdList
 public:
        KeyIdList () {}
 
-       KeyIdList (shared_ptr<const cxml::Node> node)
+       explicit KeyIdList (shared_ptr<const cxml::Node> node)
        {
                list<shared_ptr<cxml::Node> > typed_key_id_nodes = node->node_children ("TypedKeyId");
                for (list<shared_ptr<cxml::Node> >::const_iterator i = typed_key_id_nodes.begin(); i != typed_key_id_nodes.end(); ++i) {
@@ -274,8 +293,8 @@ class AuthorizedDeviceInfo
 public:
        AuthorizedDeviceInfo () {}
 
-       AuthorizedDeviceInfo (shared_ptr<const cxml::Node> node)
-               : device_list_identifier (node->string_child ("DeviceListIdentifier").substr (9))
+       explicit AuthorizedDeviceInfo (shared_ptr<const cxml::Node> node)
+               : device_list_identifier (remove_urn_uuid (node->string_child ("DeviceListIdentifier")))
                , device_list_description (node->optional_string_child ("DeviceListDescription"))
        {
                BOOST_FOREACH (cxml::ConstNodePtr i, node->node_child("DeviceList")->node_children("CertificateThumbprint")) {
@@ -306,7 +325,7 @@ class X509IssuerSerial
 public:
        X509IssuerSerial () {}
 
-       X509IssuerSerial (shared_ptr<const cxml::Node> node)
+       explicit X509IssuerSerial (shared_ptr<const cxml::Node> node)
                : x509_issuer_name (node->string_child ("X509IssuerName"))
                , x509_serial_number (node->string_child ("X509SerialNumber"))
        {
@@ -328,7 +347,7 @@ class Recipient
 public:
        Recipient () {}
 
-       Recipient (shared_ptr<const cxml::Node> node)
+       explicit Recipient (shared_ptr<const cxml::Node> node)
                : x509_issuer_serial (node->node_child ("X509IssuerSerial"))
                , x509_subject_name (node->string_child ("X509SubjectName"))
        {
@@ -350,9 +369,9 @@ class KDMRequiredExtensions
 public:
        KDMRequiredExtensions () {}
 
-       KDMRequiredExtensions (shared_ptr<const cxml::Node> node)
+       explicit KDMRequiredExtensions (shared_ptr<const cxml::Node> node)
                : recipient (node->node_child ("Recipient"))
-               , composition_playlist_id (node->string_child ("CompositionPlaylistId").substr (9))
+               , composition_playlist_id (remove_urn_uuid (node->string_child ("CompositionPlaylistId")))
                , content_title_text (node->string_child ("ContentTitleText"))
                , not_valid_before (node->string_child ("ContentKeysNotValidBefore"))
                , not_valid_after (node->string_child ("ContentKeysNotValidAfter"))
@@ -374,7 +393,9 @@ public:
                }
                node->add_child("ContentKeysNotValidBefore")->add_child_text (not_valid_before.as_string ());
                node->add_child("ContentKeysNotValidAfter")->add_child_text (not_valid_after.as_string ());
-               authorized_device_info.as_xml (node->add_child ("AuthorizedDeviceInfo"));
+               if (authorized_device_info) {
+                       authorized_device_info->as_xml (node->add_child ("AuthorizedDeviceInfo"));
+               }
                key_id_list.as_xml (node->add_child ("KeyIdList"));
 
                xmlpp::Element* forensic_mark_flag_list = node->add_child ("ForensicMarkFlagList");
@@ -388,7 +409,7 @@ public:
        string content_title_text;
        LocalTime not_valid_before;
        LocalTime not_valid_after;
-       AuthorizedDeviceInfo authorized_device_info;
+       boost::optional<AuthorizedDeviceInfo> authorized_device_info;
        KeyIdList key_id_list;
 };
 
@@ -397,7 +418,7 @@ class RequiredExtensions
 public:
        RequiredExtensions () {}
 
-       RequiredExtensions (shared_ptr<const cxml::Node> node)
+       explicit RequiredExtensions (shared_ptr<const cxml::Node> node)
                : kdm_required_extensions (node->node_child ("KDMRequiredExtensions"))
        {
 
@@ -416,12 +437,14 @@ class AuthenticatedPublic
 public:
        AuthenticatedPublic ()
                : message_id (make_uuid ())
+                 /* XXX: hack for Dolby to see if there must be a not-empty annotation text */
+               , annotation_text ("none")
                , issue_date (LocalTime().as_string ())
        {}
 
-       AuthenticatedPublic (shared_ptr<const cxml::Node> node)
-               : message_id (node->string_child ("MessageId").substr (9))
-               , annotation_text (node->string_child ("AnnotationText"))
+       explicit AuthenticatedPublic (shared_ptr<const cxml::Node> node)
+               : message_id (remove_urn_uuid (node->string_child ("MessageId")))
+               , annotation_text (node->optional_string_child ("AnnotationText"))
                , issue_date (node->string_child ("IssueDate"))
                , signer (node->node_child ("Signer"))
                , required_extensions (node->node_child ("RequiredExtensions"))
@@ -435,7 +458,9 @@ public:
 
                node->add_child("MessageId")->add_child_text ("urn:uuid:" + message_id);
                node->add_child("MessageType")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
-               node->add_child("AnnotationText")->add_child_text (annotation_text);
+               if (annotation_text) {
+                       node->add_child("AnnotationText")->add_child_text (annotation_text.get ());
+               }
                node->add_child("IssueDate")->add_child_text (issue_date);
 
                signer.as_xml (node->add_child ("Signer"));
@@ -445,7 +470,7 @@ public:
        }
 
        string message_id;
-       string annotation_text;
+       optional<string> annotation_text;
        string issue_date;
        Signer signer;
        RequiredExtensions required_extensions;
@@ -462,7 +487,7 @@ public:
 
        }
 
-       EncryptedKDMData (shared_ptr<const cxml::Node> node)
+       explicit EncryptedKDMData (shared_ptr<const cxml::Node> node)
                : authenticated_public (node->node_child ("AuthenticatedPublic"))
                , authenticated_private (node->node_child ("AuthenticatedPrivate"))
                , signature (node->node_child ("Signature"))
@@ -507,9 +532,9 @@ EncryptedKDM::EncryptedKDM (
        shared_ptr<const CertificateChain> signer,
        Certificate recipient,
        vector<Certificate> trusted_devices,
-       string device_list_description,
        string cpl_id,
        string content_title_text,
+       optional<string> annotation_text,
        LocalTime not_valid_before,
        LocalTime not_valid_after,
        Formulation formulation,
@@ -523,12 +548,12 @@ EncryptedKDM::EncryptedKDM (
        data::AuthenticatedPublic& aup = _data->authenticated_public;
        aup.signer.x509_issuer_name = signer->leaf().issuer ();
        aup.signer.x509_serial_number = signer->leaf().serial ();
+       aup.annotation_text = annotation_text;
 
        data::KDMRequiredExtensions& kre = _data->authenticated_public.required_extensions.kdm_required_extensions;
        kre.recipient.x509_issuer_serial.x509_issuer_name = recipient.issuer ();
        kre.recipient.x509_issuer_serial.x509_serial_number = recipient.serial ();
        kre.recipient.x509_subject_name = recipient.subject ();
-       kre.authorized_device_info.device_list_description = device_list_description;
        kre.composition_playlist_id = cpl_id;
        if (formulation == DCI_ANY || formulation == DCI_SPECIFIC) {
                kre.content_authenticator = signer->leaf().thumbprint ();
@@ -536,21 +561,30 @@ EncryptedKDM::EncryptedKDM (
        kre.content_title_text = content_title_text;
        kre.not_valid_before = not_valid_before;
        kre.not_valid_after = not_valid_after;
-       kre.authorized_device_info.device_list_identifier = make_uuid ();
-       string n = recipient.subject_common_name ();
-       if (n.find (".") != string::npos) {
-               n = n.substr (n.find (".") + 1);
-       }
-       kre.authorized_device_info.device_list_description = n;
-
-       if (formulation == MODIFIED_TRANSITIONAL_1 || formulation == DCI_ANY) {
-               /* Use the "assume trust" thumbprint */
-               kre.authorized_device_info.certificate_thumbprints.push_back ("2jmj7l5rSw0yVb/vlWAYkK/YBwk=");
-       } else if (formulation == DCI_SPECIFIC) {
-               /* Use the recipient and other trusted device thumbprints */
-               kre.authorized_device_info.certificate_thumbprints.push_back (recipient.thumbprint ());
-               BOOST_FOREACH (Certificate const & i, trusted_devices) {
-                       kre.authorized_device_info.certificate_thumbprints.push_back (i.thumbprint ());
+
+       if (formulation != MODIFIED_TRANSITIONAL_TEST) {
+               kre.authorized_device_info = data::AuthorizedDeviceInfo ();
+               kre.authorized_device_info->device_list_identifier = make_uuid ();
+               string n = recipient.subject_common_name ();
+               if (n.find (".") != string::npos) {
+                       n = n.substr (n.find (".") + 1);
+               }
+               kre.authorized_device_info->device_list_description = n;
+
+               if (formulation == MODIFIED_TRANSITIONAL_1 || formulation == DCI_ANY) {
+                       /* Use the "assume trust" thumbprint */
+                       kre.authorized_device_info->certificate_thumbprints.push_back ("2jmj7l5rSw0yVb/vlWAYkK/YBwk=");
+               } else if (formulation == DCI_SPECIFIC) {
+                       /* As I read the standard we should use the recipient
+                          /and/ other trusted device thumbprints here.  MJD
+                          reports that this doesn't work with his setup;
+                          a working KDM does not include the recipient's
+                          thumbprint (recipient.thumbprint()).
+                          Waimea uses only the trusted devices here, too.
+                       */
+                       BOOST_FOREACH (Certificate const & i, trusted_devices) {
+                               kre.authorized_device_info->certificate_thumbprints.push_back (i.thumbprint ());
+                       }
                }
        }
 
@@ -609,11 +643,6 @@ EncryptedKDM::as_xml (boost::filesystem::path path) const
 string
 EncryptedKDM::as_xml () const
 {
-       xmlpp::Document document;
-       xmlpp::Element* root = document.create_root_node ("DCinemaSecurityMessage", "http://www.smpte-ra.org/schemas/430-3/2006/ETM");
-       root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "ds");
-       root->set_namespace_declaration ("http://www.w3.org/2001/04/xmlenc#", "enc");
-
        return _data->as_xml()->write_to_string ("UTF-8");
 }
 
@@ -623,7 +652,7 @@ EncryptedKDM::keys () const
        return _data->authenticated_private.encrypted_key;
 }
 
-string
+optional<string>
 EncryptedKDM::annotation_text () const
 {
        return _data->authenticated_public.annotation_text;
@@ -635,12 +664,30 @@ EncryptedKDM::content_title_text () const
        return _data->authenticated_public.required_extensions.kdm_required_extensions.content_title_text;
 }
 
+string
+EncryptedKDM::cpl_id () const
+{
+       return _data->authenticated_public.required_extensions.kdm_required_extensions.composition_playlist_id;
+}
+
 string
 EncryptedKDM::issue_date () const
 {
        return _data->authenticated_public.issue_date;
 }
 
+LocalTime
+EncryptedKDM::not_valid_before () const
+{
+       return _data->authenticated_public.required_extensions.kdm_required_extensions.not_valid_before;
+}
+
+LocalTime
+EncryptedKDM::not_valid_after () const
+{
+       return _data->authenticated_public.required_extensions.kdm_required_extensions.not_valid_after;
+}
+
 bool
 dcp::operator== (EncryptedKDM const & a, EncryptedKDM const & b)
 {