summaryrefslogtreecommitdiff
path: root/test/kdm_test.cc
blob: 714d195c391ea05977ccbf5eae9c56cdc1d7e19d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
    Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>

    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.

    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 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 "certificate_chain.h"
#include "cpl.h"
#include "decrypted_kdm.h"
#include "encrypted_kdm.h"
#include "mono_j2k_picture_asset.h"
#include "j2k_picture_asset_writer.h"
#include "reel.h"
#include "reel_mono_picture_asset.h"
#include "reel_sound_asset.h"
#include "reel_smpte_text_asset.h"
#include "smpte_text_asset.h"
#include "test.h"
#include "types.h"
#include "util.h"
#include "warnings.h"
#include <libcxml/cxml.h>
LIBDCP_DISABLE_WARNINGS
#include <libxml++/libxml++.h>
LIBDCP_ENABLE_WARNINGS
#include <boost/test/unit_test.hpp>


using std::dynamic_pointer_cast;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;


/** Check reading and decryption of a KDM */
BOOST_AUTO_TEST_CASE (kdm_test)
{
	dcp::DecryptedKDM kdm (
		dcp::EncryptedKDM (
			dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
			),
		dcp::file_to_string ("test/data/private.key")
		);

	auto keys = kdm.keys ();

	BOOST_CHECK_EQUAL (keys.size(), 2U);

	BOOST_CHECK_EQUAL (keys.front().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
	BOOST_CHECK_EQUAL (keys.front().id(), "4ac4f922-8239-4831-b23b-31426d0542c4");
	BOOST_CHECK_EQUAL (keys.front().key().hex(), "8a2729c3e5b65c45d78305462104c3fb");

	BOOST_CHECK_EQUAL (keys.back().cpl_id(), "eece17de-77e8-4a55-9347-b6bab5724b9f");
	BOOST_CHECK_EQUAL (keys.back().id(), "73baf5de-e195-4542-ab28-8a465f7d4079");
	BOOST_CHECK_EQUAL (keys.back().key().hex(), "5327fb7ec2e807bd57059615bf8a169d");
}


/** Check that we can read in a KDM and then write it back out again the same */
BOOST_AUTO_TEST_CASE (kdm_passthrough_test)
{
	dcp::EncryptedKDM kdm (
		dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
		);

	auto parser = make_shared<xmlpp::DomParser>();
	parser->parse_memory (kdm.as_xml ());
	parser->get_document()->write_to_file_formatted ("build/kdm.xml", "UTF-8");
	check_xml (
		dcp::file_to_string("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml"),
		dcp::file_to_string("build/kdm.xml"),
		{},
		true
		);
}


/** Test some of the utility methods of DecryptedKDM */
BOOST_AUTO_TEST_CASE (decrypted_kdm_test)
{
	auto data = new uint8_t[16];
	auto p = data;
	dcp::DecryptedKDM::put_uuid (&p, "8971c838-d0c3-405d-bc57-43afa9d91242");

	BOOST_CHECK_EQUAL (data[0], 0x89);
	BOOST_CHECK_EQUAL (data[1], 0x71);
	BOOST_CHECK_EQUAL (data[2], 0xc8);
	BOOST_CHECK_EQUAL (data[3], 0x38);
	BOOST_CHECK_EQUAL (data[4], 0xd0);
	BOOST_CHECK_EQUAL (data[5], 0xc3);
	BOOST_CHECK_EQUAL (data[6], 0x40);
	BOOST_CHECK_EQUAL (data[7], 0x5d);
	BOOST_CHECK_EQUAL (data[8], 0xbc);
	BOOST_CHECK_EQUAL (data[9], 0x57);
	BOOST_CHECK_EQUAL (data[10], 0x43);
	BOOST_CHECK_EQUAL (data[11], 0xaf);
	BOOST_CHECK_EQUAL (data[12], 0xa9);
	BOOST_CHECK_EQUAL (data[13], 0xd9);
	BOOST_CHECK_EQUAL (data[14], 0x12);
	BOOST_CHECK_EQUAL (data[15], 0x42);

	p = data;
	BOOST_CHECK_EQUAL (dcp::DecryptedKDM::get_uuid (&p), "8971c838-d0c3-405d-bc57-43afa9d91242");

	delete[] data;
}


/** Check that <KeyType> tags have the scope attribute.
 *  Wolfgang Woehl believes this is compulsory and I am more-or-less inclined to agree.
 */
BOOST_AUTO_TEST_CASE (kdm_key_type_scope)
{
	dcp::EncryptedKDM kdm (
		dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
		);

	cxml::Document doc;
	doc.read_string (kdm.as_xml ());

	auto typed_key_ids = doc.node_child("AuthenticatedPublic")->
		node_child("RequiredExtensions")->
		node_child("KDMRequiredExtensions")->
		node_child("KeyIdList")->
		node_children("TypedKeyId");

	for (auto i: typed_key_ids) {
		for (auto j: i->node_children("KeyType")) {
			BOOST_CHECK (j->string_attribute("scope") == "http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type");
		}
	}
}


static cxml::ConstNodePtr
kdm_forensic_test (cxml::Document& doc, bool picture, optional<int> audio)
{
	dcp::DecryptedKDM decrypted (
		dcp::EncryptedKDM (
			dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
			),
		dcp::file_to_string ("test/data/private.key")
		);

	auto signer = make_shared<dcp::CertificateChain>(dcp::file_to_string("test/data/certificate_chain"));
	signer->set_key(dcp::file_to_string("test/data/private.key"));

	dcp::EncryptedKDM kdm = decrypted.encrypt (
		signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, picture, audio
		);

	/* Check that we can pass this through correctly */
	BOOST_CHECK_EQUAL (kdm.as_xml(), dcp::EncryptedKDM(kdm.as_xml()).as_xml());

	doc.read_string (kdm.as_xml());

	return doc.node_child("AuthenticatedPublic")->
		node_child("RequiredExtensions")->
		node_child("KDMRequiredExtensions")->
		optional_node_child("ForensicMarkFlagList");
}


/** Check ForensicMarkFlagList handling: disable picture and all audio */
BOOST_AUTO_TEST_CASE (kdm_forensic_test1)
{
	cxml::Document doc;
	auto forensic = kdm_forensic_test(doc, true, 0);
	BOOST_REQUIRE (forensic);
	auto flags = forensic->node_children("ForensicMarkFlag");
	BOOST_REQUIRE_EQUAL (flags.size(), 2U);
	BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
	BOOST_CHECK_EQUAL (flags.back()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
}


/** Check ForensicMarkFlagList handling: disable picture but not audio */
BOOST_AUTO_TEST_CASE (kdm_forensic_test2)
{
	cxml::Document doc;
	auto forensic = kdm_forensic_test(doc, true, optional<int>());
	BOOST_REQUIRE (forensic);
	auto flags = forensic->node_children("ForensicMarkFlag");
	BOOST_REQUIRE_EQUAL (flags.size(), 1U);
	BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
}


/** Check ForensicMarkFlagList handling: disable audio but not picture */
BOOST_AUTO_TEST_CASE (kdm_forensic_test3)
{
	cxml::Document doc;
	auto forensic = kdm_forensic_test(doc, false, 0);
	BOOST_REQUIRE (forensic);
	auto flags = forensic->node_children("ForensicMarkFlag");
	BOOST_REQUIRE_EQUAL (flags.size(), 1U);
	BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable");
}


/** Check ForensicMarkFlagList handling: disable picture and audio above channel 3 */
BOOST_AUTO_TEST_CASE (kdm_forensic_test4)
{
	cxml::Document doc;
	auto forensic = kdm_forensic_test(doc, true, 3);
	BOOST_REQUIRE (forensic);
	auto flags = forensic->node_children("ForensicMarkFlag");
	BOOST_REQUIRE_EQUAL (flags.size(), 2U);
	BOOST_CHECK_EQUAL (flags.front()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-picture-disable");
	BOOST_CHECK_EQUAL (flags.back()->content(), "http://www.smpte-ra.org/430-1/2006/KDM#mrkflg-audio-disable-above-channel-3");
}


/** Check ForensicMarkFlagList handling: disable neither */
BOOST_AUTO_TEST_CASE (kdm_forensic_test5)
{
	cxml::Document doc;
	auto forensic = kdm_forensic_test(doc, false, optional<int>());
	BOOST_CHECK (!forensic);
}


/** Check that KDM validity periods are checked for being within the certificate validity */
BOOST_AUTO_TEST_CASE (validity_period_test1)
{
	auto signer = make_shared<dcp::CertificateChain>(dcp::file_to_string("test/data/certificate_chain"));
	signer->set_key(dcp::file_to_string("test/data/private.key"));

	auto asset = make_shared<dcp::MonoJ2KPictureAsset>(dcp::Fraction(24, 1), dcp::Standard::SMPTE);
	asset->set_key (dcp::Key());
	auto writer = asset->start_write("build/test/validity_period_test1.mxf", dcp::Behaviour::MAKE_NEW);
	dcp::ArrayData frame ("test/data/flat_red.j2c");
	writer->write (frame.data(), frame.size());
	writer->finalize ();
	auto reel = make_shared<dcp::Reel>();
	reel->add(make_shared<dcp::ReelMonoPictureAsset>(asset, 0));
	auto cpl = make_shared<dcp::CPL>("test", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
	cpl->add(reel);

	/* This certificate_chain is valid from 22/12/2022 to 19/12/2032 */

	/* Inside */
	BOOST_CHECK_NO_THROW(
		dcp::DecryptedKDM(
			cpl, dcp::Key(dcp::file_to_string("test/data/private.key")), dcp::LocalTime("2025-01-01T00:00:00"), dcp::LocalTime("2027-07-31T00:00:00"), "", "", ""
			).encrypt(signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, optional<int>())
		);

	/* Starts too early */
	BOOST_CHECK_THROW(
		dcp::DecryptedKDM(
			cpl, dcp::Key(dcp::file_to_string("test/data/private.key")), dcp::LocalTime("1981-01-01T00:00:00"), dcp::LocalTime("2017-07-31T00:00:00"), "", "", ""
			).encrypt(signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, optional<int>()),
		dcp::BadKDMDateError
		);

	/* Finishes too late */
	BOOST_CHECK_THROW(
		dcp::DecryptedKDM(
			cpl, dcp::Key(dcp::file_to_string("test/data/private.key")), dcp::LocalTime("2015-01-01T00:00:00"), dcp::LocalTime("2035-07-31T00:00:00"), "", "", ""
			).encrypt(signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, optional<int>()),
		dcp::BadKDMDateError
		);

	/* Starts too early and finishes too late */
	BOOST_CHECK_THROW(
		dcp::DecryptedKDM(
			cpl, dcp::Key(dcp::file_to_string("test/data/private.key")), dcp::LocalTime("1981-01-01T00:00:00"), dcp::LocalTime("2035-07-31T00:00:00"), "", "", ""
			).encrypt(signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, optional<int>()),
		dcp::BadKDMDateError
		);
}


/** Test the case where we have:
 *  - a OV + VF
 *  - a single KDM which has keys for both OV and VF assets
 *  and we load VF, then KDM, then the OV.  The OV's assets should have their
 *  KDMs properly assigned.
 */
BOOST_AUTO_TEST_CASE (vf_kdm_test)
{
	/* Make OV */

	boost::filesystem::path const ov_path = "build/test/vf_kdm_test_ov";
	boost::filesystem::path const vf_path = "build/test/vf_kdm_test_vf";
	dcp::Key key;

	auto ov = make_simple(ov_path, 1, 48, dcp::Standard::SMPTE, key);
	ov->write_xml ();

	auto ov_reel = ov->cpls()[0]->reels()[0];
	auto ov_reel_picture = make_shared<dcp::ReelMonoPictureAsset>(dynamic_pointer_cast<dcp::ReelMonoPictureAsset>(ov_reel->main_picture())->mono_j2k_asset(), 0);
	auto ov_reel_sound = make_shared<dcp::ReelSoundAsset>(ov_reel->main_sound()->asset(), 0);

	/* Make VF */

	auto subs = make_shared<dcp::SMPTETextAsset>();
	subs->add(simple_text());
	subs->set_key(key);

	boost::filesystem::remove_all (vf_path);
	boost::filesystem::create_directory (vf_path);
	dcp::ArrayData data(4096);
	subs->add_font ("afont", data);
	subs->write (vf_path / "subs.xml");

	auto reel_subs = make_shared<dcp::ReelSMPTETextAsset>(dcp::TextType::OPEN_SUBTITLE, subs, dcp::Fraction(24, 1), 192, 0);

	auto reel = make_shared<dcp::Reel>(ov_reel_picture, ov_reel_sound, reel_subs);

	auto cpl = make_shared<dcp::CPL>("A Test DCP", dcp::ContentKind::TRAILER, dcp::Standard::SMPTE);
	cpl->add (reel);

	auto vf = make_shared<dcp::DCP>("build/test/vf_kdm_test_vf");
	vf->add (cpl);
	vf->write_xml ();

	/* Make KDM for VF */

	auto kdm = dcp::DecryptedKDM(
		cpl,
		key,
		dcp::LocalTime ("2016-01-01T00:00:00+00:00"),
		dcp::LocalTime ("2017-01-08T00:00:00+00:00"),
		"libdcp",
		"test",
		"2012-07-17T04:45:18+00:00"
		);

	/* Decrypt VF with the KDM */

	dcp::DCP reload_vf(vf_path);
	reload_vf.read();
	reload_vf.add (kdm);

	/* Add the OV */

	dcp::DCP reload_ov(ov_path);
	reload_ov.read();
	reload_vf.resolve_refs(reload_ov.assets());

	/* Check that we can decrypt the VF */

	BOOST_REQUIRE_EQUAL(reload_vf.cpls().size(), 1U);
	BOOST_REQUIRE_EQUAL(reload_vf.cpls()[0]->reels().size(), 1U);
	BOOST_REQUIRE(reload_vf.cpls()[0]->reels()[0]->main_picture());
	BOOST_REQUIRE(reload_vf.cpls()[0]->reels()[0]->main_picture()->asset());
	auto mono_asset = dynamic_pointer_cast<dcp::MonoJ2KPictureAsset>(reload_vf.cpls()[0]->reels()[0]->main_picture()->asset());
	BOOST_REQUIRE(mono_asset);
	auto reader = mono_asset->start_read();
	reader->set_check_hmac(false);
	reader->get_frame(0)->xyz_image();
}