53aabd87b9c5a094495fa7bbda98f115575b6ea5
[libdcp.git] / test / kdm_test.cc
1 /*
2     Copyright (C) 2013-2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "encrypted_kdm.h"
21 #include "decrypted_kdm.h"
22 #include "certificate_chain.h"
23 #include "util.h"
24 #include "test.h"
25 #include <libcxml/cxml.h>
26 #include <libxml++/libxml++.h>
27 #include <boost/test/unit_test.hpp>
28 #include <boost/foreach.hpp>
29
30 using std::list;
31 using std::string;
32 using std::vector;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 /** Check reading and decryption of a KDM */
37 BOOST_AUTO_TEST_CASE (kdm_test)
38 {
39         dcp::DecryptedKDM kdm (
40                 dcp::EncryptedKDM (
41                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
42                         ),
43                 dcp::file_to_string ("test/data/private.key")
44                 );
45
46         list<dcp::DecryptedKDMKey> keys = kdm.keys ();
47
48         BOOST_CHECK_EQUAL (keys.size(), 2);
49
50         BOOST_CHECK_EQUAL (keys.front().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
51         BOOST_CHECK_EQUAL (keys.front().id(), "4ac4f922-8239-4831-b23b-31426d0542c4");
52         BOOST_CHECK_EQUAL (keys.front().key().hex(), "8a2729c3e5b65c45d78305462104c3fb");
53
54         BOOST_CHECK_EQUAL (keys.back().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
55         BOOST_CHECK_EQUAL (keys.back().id(), "73baf5de-e195-4542-ab28-8a465f7d4079");
56         BOOST_CHECK_EQUAL (keys.back().key().hex(), "5327fb7ec2e807bd57059615bf8a169d");
57 }
58
59 /** Check that we can read in a KDM and then write it back out again the same */
60 BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
61 {
62         dcp::EncryptedKDM kdm (
63                 dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
64                 );
65
66         shared_ptr<xmlpp::DomParser> parser (new xmlpp::DomParser ());
67         parser->parse_memory (kdm.as_xml ());
68         parser->get_document()->write_to_file_formatted ("build/kdm.xml", "UTF-8");
69         int const r = system (
70                 "xmldiff -c test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml build/kdm.xml"
71                 );
72
73 #ifdef LIBDCP_WINDOWS
74         BOOST_CHECK_EQUAL (r, 0);
75 #else
76         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
77 #endif
78 }
79
80 /** Test some of the utility methods of DecryptedKDM */
81 BOOST_AUTO_TEST_CASE (decrypted_kdm_test)
82 {
83         uint8_t* data = new uint8_t[16];
84         uint8_t* p = data;
85         dcp::DecryptedKDM::put_uuid (&p, "8971c838-d0c3-405d-bc57-43afa9d91242");
86
87         BOOST_CHECK_EQUAL (data[0], 0x89);
88         BOOST_CHECK_EQUAL (data[1], 0x71);
89         BOOST_CHECK_EQUAL (data[2], 0xc8);
90         BOOST_CHECK_EQUAL (data[3], 0x38);
91         BOOST_CHECK_EQUAL (data[4], 0xd0);
92         BOOST_CHECK_EQUAL (data[5], 0xc3);
93         BOOST_CHECK_EQUAL (data[6], 0x40);
94         BOOST_CHECK_EQUAL (data[7], 0x5d);
95         BOOST_CHECK_EQUAL (data[8], 0xbc);
96         BOOST_CHECK_EQUAL (data[9], 0x57);
97         BOOST_CHECK_EQUAL (data[10], 0x43);
98         BOOST_CHECK_EQUAL (data[11], 0xaf);
99         BOOST_CHECK_EQUAL (data[12], 0xa9);
100         BOOST_CHECK_EQUAL (data[13], 0xd9);
101         BOOST_CHECK_EQUAL (data[14], 0x12);
102         BOOST_CHECK_EQUAL (data[15], 0x42);
103
104         p = data;
105         BOOST_CHECK_EQUAL (dcp::DecryptedKDM::get_uuid (&p), "8971c838-d0c3-405d-bc57-43afa9d91242");
106
107         delete[] data;
108 }
109
110 /** Check that <KeyType> tags have the scope attribute.
111  *  Wolfgang Woehl believes this is compulsory and I am more-or-less inclined to agree.
112  */
113 BOOST_AUTO_TEST_CASE (kdm_key_type_scope)
114 {
115         dcp::EncryptedKDM kdm (
116                 dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
117                 );
118
119         cxml::Document doc;
120         doc.read_string (kdm.as_xml ());
121
122         list<cxml::NodePtr> typed_key_ids = doc.node_child("AuthenticatedPublic")->
123                 node_child("RequiredExtensions")->
124                 node_child("KDMRequiredExtensions")->
125                 node_child("KeyIdList")->
126                 node_children("TypedKeyId");
127
128         BOOST_FOREACH (cxml::NodePtr i, typed_key_ids) {
129                 BOOST_FOREACH (cxml::NodePtr j, i->node_children("KeyType")) {
130                         BOOST_CHECK (j->string_attribute("scope") == "http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
131                 }
132         }
133 }
134
135 static cxml::ConstNodePtr
136 kdm_forensic_test (cxml::Document& doc, bool picture, optional<int> audio)
137 {
138         dcp::DecryptedKDM decrypted (
139                 dcp::EncryptedKDM (
140                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
141                         ),
142                 dcp::file_to_string ("test/data/private.key")
143                 );
144
145         shared_ptr<dcp::CertificateChain> signer(new dcp::CertificateChain(dcp::file_to_string("test/data/certificate_chain")));
146         signer->set_key(dcp::file_to_string("test/data/private.key"));
147
148         dcp::EncryptedKDM kdm = decrypted.encrypt (
149                 signer, signer->leaf(), vector<dcp::Certificate>(), dcp::MODIFIED_TRANSITIONAL_1, picture, audio
150                 );
151
152         /* Check that we can pass this through correctly */
153         BOOST_CHECK_EQUAL (kdm.as_xml(), dcp::EncryptedKDM(kdm.as_xml()).as_xml());
154
155         doc.read_string (kdm.as_xml());
156
157         return doc.node_child("AuthenticatedPublic")->
158                 node_child("RequiredExtensions")->
159                 node_child("KDMRequiredExtensions")->
160                 optional_node_child("ForensicMarkFlagList");
161 }
162
163 /** Check ForensicMarkFlagList handling: disable picture and all audio */
164 BOOST_AUTO_TEST_CASE (kdm_forensic_test1)
165 {
166         cxml::Document doc;
167         cxml::ConstNodePtr forensic = kdm_forensic_test(doc, true, 0);
168         BOOST_REQUIRE (forensic);
169         list<cxml::NodePtr> flags = forensic->node_children("ForensicMarkFlag");
170         BOOST_REQUIRE_EQUAL (flags.size(), 2);
171         BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
172         BOOST_CHECK_EQUAL (flags.back()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
173 }
174
175 /** Check ForensicMarkFlagList handling: disable picture but not audio */
176 BOOST_AUTO_TEST_CASE (kdm_forensic_test2)
177 {
178         cxml::Document doc;
179         cxml::ConstNodePtr forensic = kdm_forensic_test(doc, true, optional<int>());
180         BOOST_REQUIRE (forensic);
181         list<cxml::NodePtr> flags = forensic->node_children("ForensicMarkFlag");
182         BOOST_REQUIRE_EQUAL (flags.size(), 1);
183         BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
184 }
185
186 /** Check ForensicMarkFlagList handling: disable audio but not picture */
187 BOOST_AUTO_TEST_CASE (kdm_forensic_test3)
188 {
189         cxml::Document doc;
190         cxml::ConstNodePtr forensic = kdm_forensic_test(doc, false, 0);
191         BOOST_REQUIRE (forensic);
192         list<cxml::NodePtr> flags = forensic->node_children("ForensicMarkFlag");
193         BOOST_REQUIRE_EQUAL (flags.size(), 1);
194         BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
195 }
196
197 /** Check ForensicMarkFlagList handling: disable picture and audio above channel 3 */
198 BOOST_AUTO_TEST_CASE (kdm_forensic_test4)
199 {
200         cxml::Document doc;
201         cxml::ConstNodePtr forensic = kdm_forensic_test(doc, true, 3);
202         BOOST_REQUIRE (forensic);
203         list<cxml::NodePtr> flags = forensic->node_children("ForensicMarkFlag");
204         BOOST_REQUIRE_EQUAL (flags.size(), 2);
205         BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
206         BOOST_CHECK_EQUAL (flags.back()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable-above-channel-3");
207 }
208
209 /** Check ForensicMarkFlagList handling: disable neither */
210 BOOST_AUTO_TEST_CASE (kdm_forensic_test5)
211 {
212         cxml::Document doc;
213         cxml::ConstNodePtr forensic = kdm_forensic_test(doc, false, optional<int>());
214         BOOST_CHECK (!forensic);
215 }