summaryrefslogtreecommitdiff
path: root/test/decryption_test.cc
blob: 2b34cd256037c6d3ea8e0f046dccc24864879830 (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
/*
    Copyright (C) 2013-2019 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 "colour_conversion.h"
#include "cpl.h"
#include "dcp.h"
#include "decrypted_kdm.h"
#include "encrypted_kdm.h"
#include "mono_j2k_picture_asset.h"
#include "mono_j2k_picture_asset_reader.h"
#include "mono_j2k_picture_frame.h"
#include "openjpeg_image.h"
#include "j2k_picture_asset_writer.h"
#include "reel.h"
#include "reel_file_asset.h"
#include "reel_mono_picture_asset.h"
#include "reel_picture_asset.h"
#include "reel_sound_asset.h"
#include "reel_smpte_text_asset.h"
#include "rgb_xyz.h"
#include "smpte_text_asset.h"
#include "sound_asset.h"
#include "sound_asset_writer.h"
#include "stream_operators.h"
#include "test.h"
#include <boost/test/unit_test.hpp>
#include <boost/scoped_array.hpp>


#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif


using std::dynamic_pointer_cast;
using std::make_pair;
using std::map;
using std::pair;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;
using boost::scoped_array;


pair<vector<uint8_t>, dcp::Size>
get_frame (dcp::DCP const & dcp)
{
	auto reel = dcp.cpls().front()->reels()[0];
	auto picture = reel->main_picture()->asset();
	BOOST_CHECK (picture);

	auto mono_picture = dynamic_pointer_cast<const dcp::MonoJ2KPictureAsset>(picture);
	auto reader = mono_picture->start_read();
	auto j2k_frame = reader->get_frame(0);
	auto xyz = j2k_frame->xyz_image();

	std::vector<uint8_t> argb(xyz->size().width * xyz->size().height * 4);
	dcp::xyz_to_rgba(j2k_frame->xyz_image(), dcp::ColourConversion::srgb_to_xyz(), argb.data(), xyz->size().width * 4);
	return make_pair (argb, xyz->size ());
}

/** Decrypt an encrypted test DCP and check that its first frame is the same as the unencrypted version */
BOOST_AUTO_TEST_CASE (decryption_test1)
{
	auto plaintext_path = private_test;
	plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
	dcp::DCP plaintext (plaintext_path.string ());
	plaintext.read ();
	BOOST_CHECK_EQUAL (plaintext.any_encrypted(), false);

	auto encrypted_path = private_test;
	encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
	dcp::DCP encrypted (encrypted_path.string ());
	encrypted.read ();
	BOOST_CHECK_EQUAL (encrypted.any_encrypted(), true);

	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")
		);

	encrypted.add (kdm);

	auto plaintext_frame = get_frame(plaintext);
	auto encrypted_frame = get_frame(encrypted);

	/* Check that plaintext and encrypted are the same */

	BOOST_CHECK_EQUAL (plaintext_frame.second, encrypted_frame.second);

	BOOST_CHECK_EQUAL (
		memcmp (
			plaintext_frame.first.data(),
			encrypted_frame.first.data(),
			plaintext_frame.second.width * plaintext_frame.second.height * 4
			),
		0
		);
}

/** Load in a KDM that didn't work at first */
BOOST_AUTO_TEST_CASE (failing_kdm_test)
{
	dcp::DecryptedKDM kdm (
		dcp::EncryptedKDM (dcp::file_to_string ("test/data/target.pem.crt.de5d4eba-e683-41ca-bdda-aa4ad96af3f4.kdm.xml")),
		dcp::file_to_string("test/data/failing-private.key")
		);
}


/** Make an encrypted SMPTE DCP with picture, sound and subs and check that the keys get distributed to the assets
 *  when we read it back in.
 */
BOOST_AUTO_TEST_CASE (decryption_test2)
{
	boost::filesystem::path dir = "build/test/decryption_test2";
	boost::filesystem::create_directory(dir);

	auto context_id = dcp::make_uuid();
	dcp::Key key;

	auto picture_asset = std::make_shared<dcp::MonoJ2KPictureAsset>(dcp::Fraction(24, 1), dcp::Standard::SMPTE);
	picture_asset->set_key (key);
	picture_asset->set_context_id (context_id);
	auto picture_writer = picture_asset->start_write(dir / "picture.mxf", dcp::Behaviour::MAKE_NEW);
	dcp::ArrayData picture("test/data/flat_red.j2c");
	for (int i = 0; i < 24; ++i) {
		picture_writer->write(picture);
	}
	picture_writer->finalize();

	auto sound_asset = std::make_shared<dcp::SoundAsset>(dcp::Fraction(24, 1), 48000, 2, dcp::LanguageTag("en-GB"), dcp::Standard::SMPTE);
	sound_asset->set_key (key);
	sound_asset->set_context_id (context_id);
	auto sound_writer = sound_asset->start_write(dir / "sound.mxf", {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
	std::array<float, 48000> left;
	std::array<float, 48000> right;
	for (int i = 0; i < 48000; ++i) {
		left[i] = sin (2 * M_PI * i * 440 / 48000) * 0.25;
		right[i] = sin (2 * M_PI * i * 880 / 48000) * 0.25;
	}
	std::array<float*, 2> audio;
	audio[0] = left.data();
	audio[1] = right.data();
	sound_writer->write(audio.data(), 2, 48000);
	sound_writer->finalize ();

	auto subs_asset = std::make_shared<dcp::SMPTETextAsset>();
	subs_asset->set_key (key);
	subs_asset->set_context_id (context_id);
	subs_asset->add(std::make_shared<dcp::TextString>(
		optional<string>(),
		false, false, false,
		dcp::Colour(255, 255, 255),
		42,
		1,
		dcp::Time(),
		dcp::Time(0, 0, 5, 0, 24),
		0.5, dcp::HAlign::CENTER,
		0.5, dcp::VAlign::CENTER,
		0, vector<dcp::Text::VariableZPosition>(),
		dcp::Direction::LTR,
		"Hello world",
		dcp::Effect::NONE,
		dcp::Colour(0, 0, 0),
		dcp::Time(), dcp::Time(), 0, std::vector<dcp::Ruby>()
		));
	subs_asset->write (dir / "subs.mxf");

	auto reel = std::make_shared<dcp::Reel>();
	auto reel_picture_asset = std::make_shared<dcp::ReelMonoPictureAsset>(picture_asset, 0);
	auto reel_sound_asset = std::make_shared<dcp::ReelSoundAsset>(sound_asset, 0);
	auto reel_subs_asset = std::make_shared<dcp::ReelSMPTETextAsset>(dcp::TextType::OPEN_SUBTITLE, subs_asset, dcp::Fraction(24, 1), 120, 0);
	reel->add(reel_picture_asset);
	reel->add(reel_sound_asset);
	reel->add(reel_subs_asset);

	auto cpl = std::make_shared<dcp::CPL>("My film", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
	cpl->add(reel);

	dcp::DCP dcp (dir);
	dcp.add (cpl);
	dcp.write_xml ();

	map<shared_ptr<const dcp::ReelFileAsset>, dcp::Key> keys;
	keys[reel_picture_asset] = key;
	keys[reel_sound_asset] = key;
	keys[reel_subs_asset] = key;

	dcp::DecryptedKDM kdm (cpl->id(), keys, dcp::LocalTime(), dcp::LocalTime(), "foo", "bar", "baz");

	dcp::DCP dcp_read (dir);
	dcp_read.read ();
	dcp_read.add (kdm);

	BOOST_REQUIRE_EQUAL (dcp_read.cpls().size(), 1U);
	auto cpl_read = dcp_read.cpls()[0];
	BOOST_REQUIRE_EQUAL (cpl_read->reels().size(), 1U);
	auto reel_read = cpl_read->reels()[0];

	BOOST_REQUIRE (reel_read->main_picture());
	BOOST_CHECK (reel_read->main_picture()->asset()->key());
	BOOST_REQUIRE (reel_read->main_sound());
	BOOST_CHECK (reel_read->main_sound()->asset()->key());
	BOOST_REQUIRE (reel_read->main_subtitle());
	auto smpte_sub = dynamic_pointer_cast<dcp::SMPTETextAsset>(reel_read->main_subtitle()->asset());
	BOOST_REQUIRE (smpte_sub);
	BOOST_CHECK (smpte_sub->key());
}