Fix build error.
[libdcp.git] / src / encrypted_kdm.cc
index 054162cc638070d7a7b576fcbeb9a5787ede3c1d..7a7d98c1fb16858d2f26a9d52042217caafbaf62 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of libdcp.
 
 #include "util.h"
 #include "certificate_chain.h"
 #include "exceptions.h"
+#include "compose.hpp"
 #include <libcxml/cxml.h>
 #include <libxml++/document.h>
 #include <libxml++/nodes/element.h>
 #include <libxml/parser.h>
+#include <boost/algorithm/string.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/foreach.hpp>
+#include <boost/format.hpp>
 
 using std::list;
 using std::vector;
@@ -49,6 +52,7 @@ using std::map;
 using std::pair;
 using boost::shared_ptr;
 using boost::optional;
+using boost::starts_with;
 using namespace dcp;
 
 namespace dcp {
@@ -381,7 +385,25 @@ public:
                , authorized_device_info (node->node_child ("AuthorizedDeviceInfo"))
                , key_id_list (node->node_child ("KeyIdList"))
        {
-
+               disable_forensic_marking_picture = false;
+               disable_forensic_marking_audio = optional<int>();
+               if (node->optional_node_child("ForensicMarkFlagList")) {
+                       BOOST_FOREACH (cxml::ConstNodePtr i, node->node_child("ForensicMarkFlagList")->node_children("ForensicMarkFlag")) {
+                               if (i->content() == picture_disable) {
+                                       disable_forensic_marking_picture = true;
+                               } else if (starts_with(i->content(), audio_disable)) {
+                                       disable_forensic_marking_audio = 0;
+                                       string const above = audio_disable + "-above-channel-";
+                                       if (starts_with(i->content(), above)) {
+                                               string above_number = i->content().substr(above.length());
+                                               if (above_number == "") {
+                                                       throw KDMFormatError("Badly-formatted ForensicMarkFlag");
+                                               }
+                                               disable_forensic_marking_audio = atoi(above_number.c_str());
+                                       }
+                               }
+                       }
+               }
        }
 
        void as_xml (xmlpp::Element* node) const
@@ -401,9 +423,19 @@ public:
                }
                key_id_list.as_xml (node->add_child ("KeyIdList"));
 
-               xmlpp::Element* forensic_mark_flag_list = node->add_child ("ForensicMarkFlagList");
-               forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
-               forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text ("http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
+               if (disable_forensic_marking_picture || disable_forensic_marking_audio) {
+                       xmlpp::Element* forensic_mark_flag_list = node->add_child ("ForensicMarkFlagList");
+                       if (disable_forensic_marking_picture) {
+                               forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text(picture_disable);
+                       }
+                       if (disable_forensic_marking_audio) {
+                               string mrkflg = audio_disable;
+                               if (*disable_forensic_marking_audio > 0) {
+                                       mrkflg += String::compose ("-above-channel-%1", *disable_forensic_marking_audio);
+                               }
+                               forensic_mark_flag_list->add_child("ForensicMarkFlag")->add_child_text (mrkflg);
+                       }
+               }
        }
 
        Recipient recipient;
@@ -412,10 +444,19 @@ public:
        string content_title_text;
        LocalTime not_valid_before;
        LocalTime not_valid_after;
+       bool disable_forensic_marking_picture;
+       optional<int> disable_forensic_marking_audio;
        boost::optional<AuthorizedDeviceInfo> authorized_device_info;
        KeyIdList key_id_list;
+
+private:
+       static const string picture_disable;
+       static const string audio_disable;
 };
 
+const string KDMRequiredExtensions::picture_disable = "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable";
+const string KDMRequiredExtensions::audio_disable = "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable";
+
 class RequiredExtensions
 {
 public:
@@ -545,6 +586,8 @@ EncryptedKDM::EncryptedKDM (
        LocalTime not_valid_before,
        LocalTime not_valid_after,
        Formulation formulation,
+       bool disable_forensic_marking_picture,
+       optional<int> disable_forensic_marking_audio,
        list<pair<string, string> > key_ids,
        list<string> keys
        )
@@ -552,12 +595,13 @@ EncryptedKDM::EncryptedKDM (
 {
        /* Fill our XML-ish description in with the juicy bits that the caller has given */
 
-       /* Our ideas about the KDM types are:
+       /* Our ideas, based on http://isdcf.com/papers/ISDCF-Doc5-kdm-certs.pdf, about the KDM types are:
         *
-        * Type                      Trusted-device thumb  ContentAuthenticator
-        * MODIFIED_TRANSITIONAL_1   assume-trust          No
-        * DCI_ANY                   assume-trust          Yes
-        * DCI_SPECIFIC              as specified          Yes
+        * Type                               Trusted-device thumb  ContentAuthenticator
+        * MODIFIED_TRANSITIONAL_1            assume-trust          No
+        * MULTIPLE_MODIFIED_TRANSITIONAL_1   as specified          No
+        * DCI_ANY                            assume-trust          Yes
+        * DCI_SPECIFIC                       as specified          Yes
         */
 
        data::AuthenticatedPublic& aup = _data->authenticated_public;
@@ -576,6 +620,8 @@ EncryptedKDM::EncryptedKDM (
        kre.content_title_text = content_title_text;
        kre.not_valid_before = not_valid_before;
        kre.not_valid_after = not_valid_after;
+       kre.disable_forensic_marking_picture = disable_forensic_marking_picture;
+       kre.disable_forensic_marking_audio = disable_forensic_marking_audio;
 
        if (formulation != MODIFIED_TRANSITIONAL_TEST) {
                kre.authorized_device_info = data::AuthorizedDeviceInfo ();
@@ -589,16 +635,25 @@ EncryptedKDM::EncryptedKDM (
                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 ());
+               } else if (formulation == MULTIPLE_MODIFIED_TRANSITIONAL_1 || formulation == DCI_SPECIFIC) {
+                       if (trusted_devices.empty ()) {
+                               /* Fall back on the "assume trust" thumbprint so we
+                                  can generate "modified-transitional-1" KDMs
+                                  together with "multiple-modified-transitional-1"
+                                  KDMs in one go, and similarly for "dci-any" etc.
+                               */
+                               kre.authorized_device_info->certificate_thumbprints.push_back ("2jmj7l5rSw0yVb/vlWAYkK/YBwk=");
+                       } else {
+                               /* 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 ());
+                               }
                        }
                }
        }
@@ -667,6 +722,12 @@ EncryptedKDM::keys () const
        return _data->authenticated_private.encrypted_key;
 }
 
+string
+EncryptedKDM::id () const
+{
+       return _data->authenticated_public.message_id;
+}
+
 optional<string>
 EncryptedKDM::annotation_text () const
 {