0f32a6f46bc2e9109f77896e3ef1199ae6e65318
[libdcp.git] / test / decryption_test.cc
1 /*
2     Copyright (C) 2013-2019 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     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "colour_conversion.h"
35 #include "cpl.h"
36 #include "dcp.h"
37 #include "decrypted_kdm.h"
38 #include "encrypted_kdm.h"
39 #include "mono_picture_asset.h"
40 #include "mono_picture_asset_reader.h"
41 #include "mono_picture_frame.h"
42 #include "openjpeg_image.h"
43 #include "picture_asset_writer.h"
44 #include "reel.h"
45 #include "reel_file_asset.h"
46 #include "reel_mono_picture_asset.h"
47 #include "reel_picture_asset.h"
48 #include "reel_sound_asset.h"
49 #include "reel_smpte_subtitle_asset.h"
50 #include "rgb_xyz.h"
51 #include "smpte_subtitle_asset.h"
52 #include "sound_asset.h"
53 #include "sound_asset_writer.h"
54 #include "stream_operators.h"
55 #include "test.h"
56 #include <boost/test/unit_test.hpp>
57 #include <boost/scoped_array.hpp>
58
59
60 #ifndef M_PI
61 #define M_PI (3.14159265358979323846)
62 #endif
63
64
65 using std::dynamic_pointer_cast;
66 using std::make_pair;
67 using std::make_shared;
68 using std::map;
69 using std::pair;
70 using std::shared_ptr;
71 using std::string;
72 using boost::optional;
73 using boost::scoped_array;
74
75
76 pair<uint8_t*, dcp::Size>
77 get_frame (dcp::DCP const & dcp)
78 {
79         shared_ptr<const dcp::Reel> reel = dcp.cpls().front()->reels().front ();
80         shared_ptr<dcp::PictureAsset> picture = reel->main_picture()->asset ();
81         BOOST_CHECK (picture);
82
83         shared_ptr<const dcp::MonoPictureAsset> mono_picture = dynamic_pointer_cast<const dcp::MonoPictureAsset> (picture);
84         shared_ptr<const dcp::MonoPictureAssetReader> reader = mono_picture->start_read ();
85         shared_ptr<const dcp::MonoPictureFrame> j2k_frame = reader->get_frame (0);
86         shared_ptr<dcp::OpenJPEGImage> xyz = j2k_frame->xyz_image();
87
88         uint8_t* argb = new uint8_t[xyz->size().width * xyz->size().height * 4];
89         dcp::xyz_to_rgba (j2k_frame->xyz_image(), dcp::ColourConversion::srgb_to_xyz(), argb, xyz->size().width * 4);
90         return make_pair (argb, xyz->size ());
91 }
92
93 /** Decrypt an encrypted test DCP and check that its first frame is the same as the unencrypted version */
94 BOOST_AUTO_TEST_CASE (decryption_test1)
95 {
96         boost::filesystem::path plaintext_path = private_test;
97         plaintext_path /= "TONEPLATES-SMPTE-PLAINTEXT_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
98         dcp::DCP plaintext (plaintext_path.string ());
99         plaintext.read ();
100         BOOST_CHECK_EQUAL (plaintext.any_encrypted(), false);
101
102         boost::filesystem::path encrypted_path = private_test;
103         encrypted_path /= "TONEPLATES-SMPTE-ENCRYPTED_TST_F_XX-XX_ITL-TD_51-XX_2K_WOE_20111001_WOE_OV";
104         dcp::DCP encrypted (encrypted_path.string ());
105         encrypted.read ();
106         BOOST_CHECK_EQUAL (encrypted.any_encrypted(), true);
107
108         dcp::DecryptedKDM kdm (
109                 dcp::EncryptedKDM (
110                         dcp::file_to_string ("test/data/kdm_TONEPLATES-SMPTE-ENC_.smpte-430-2.ROOT.NOT_FOR_PRODUCTION_20130706_20230702_CAR_OV_t1_8971c838.xml")
111                         ),
112                 dcp::file_to_string ("test/data/private.key")
113                 );
114
115         encrypted.add (kdm);
116
117         pair<uint8_t *, dcp::Size> plaintext_frame = get_frame (plaintext);
118         pair<uint8_t *, dcp::Size> encrypted_frame = get_frame (encrypted);
119
120         /* Check that plaintext and encrypted are the same */
121
122         BOOST_CHECK_EQUAL (plaintext_frame.second, encrypted_frame.second);
123
124         BOOST_CHECK_EQUAL (
125                 memcmp (
126                         plaintext_frame.first,
127                         encrypted_frame.first,
128                         plaintext_frame.second.width * plaintext_frame.second.height * 4
129                         ),
130                 0
131                 );
132
133         delete[] plaintext_frame.first;
134         delete[] encrypted_frame.first;
135 }
136
137 /** Load in a KDM that didn't work at first */
138 BOOST_AUTO_TEST_CASE (failing_kdm_test)
139 {
140         dcp::DecryptedKDM kdm (
141                 dcp::EncryptedKDM (dcp::file_to_string ("test/data/target.pem.crt.de5d4eba-e683-41ca-bdda-aa4ad96af3f4.kdm.xml")),
142                 dcp::file_to_string ("test/data/private.key")
143                 );
144 }
145
146
147 /** Make an encrypted SMPTE DCP with picture, sound and subs and check that the keys get distributed to the assets
148  *  when we read it back in.
149  */
150 BOOST_AUTO_TEST_CASE (decryption_test2)
151 {
152         boost::filesystem::path dir = "build/test/decryption_test2";
153         boost::filesystem::create_directory(dir);
154
155         auto context_id = dcp::make_uuid();
156         dcp::Key key;
157
158         auto picture_asset = make_shared<dcp::MonoPictureAsset>(dcp::Fraction(24, 1), dcp::Standard::SMPTE);
159         picture_asset->set_key (key);
160         picture_asset->set_context_id (context_id);
161         auto picture_writer = picture_asset->start_write(dir / "picture.mxf", false);
162         dcp::ArrayData picture("test/data/flat_red.j2c");
163         for (int i = 0; i < 24; ++i) {
164                 picture_writer->write(picture);
165         }
166         picture_writer->finalize();
167
168         auto sound_asset = make_shared<dcp::SoundAsset>(dcp::Fraction(24, 1), 48000, 2, dcp::LanguageTag("en-GB"), dcp::Standard::SMPTE);
169         sound_asset->set_key (key);
170         sound_asset->set_context_id (context_id);
171         auto sound_writer = sound_asset->start_write(dir / "sound.mxf");
172         std::array<float, 48000> left;
173         std::array<float, 48000> right;
174         for (int i = 0; i < 48000; ++i) {
175                 left[i] = sin (2 * M_PI * i * 440 / 48000) * 0.25;
176                 right[i] = sin (2 * M_PI * i * 880 / 48000) * 0.25;
177         }
178         std::array<float*, 2> audio;
179         audio[0] = left.data();
180         audio[1] = right.data();
181         sound_writer->write (audio.data(), 48000);
182         sound_writer->finalize ();
183
184         auto subs_asset = make_shared<dcp::SMPTESubtitleAsset>();
185         subs_asset->set_key (key);
186         subs_asset->set_context_id (context_id);
187         subs_asset->add(make_shared<dcp::SubtitleString>(
188                 optional<string>(),
189                 false, false, false,
190                 dcp::Colour(255, 255, 255),
191                 42,
192                 1,
193                 dcp::Time(),
194                 dcp::Time(0, 0, 5, 0, 24),
195                 0.5, dcp::HAlign::CENTER,
196                 0.5, dcp::VAlign::CENTER,
197                 0,
198                 dcp::Direction::LTR,
199                 "Hello world",
200                 dcp::Effect::NONE,
201                 dcp::Colour(0, 0, 0),
202                 dcp::Time(), dcp::Time(), 0
203                 ));
204         subs_asset->write (dir / "subs.mxf");
205
206         auto reel = make_shared<dcp::Reel>();
207         auto reel_picture_asset = make_shared<dcp::ReelMonoPictureAsset>(picture_asset, 0);
208         auto reel_sound_asset = make_shared<dcp::ReelSoundAsset>(sound_asset, 0);
209         auto reel_subs_asset = make_shared<dcp::ReelSMPTESubtitleAsset>(subs_asset, dcp::Fraction(24, 1), 120, 0);
210         reel->add(reel_picture_asset);
211         reel->add(reel_sound_asset);
212         reel->add(reel_subs_asset);
213
214         auto cpl = std::make_shared<dcp::CPL>("My film", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
215         cpl->add(reel);
216
217         dcp::DCP dcp (dir);
218         dcp.add (cpl);
219         dcp.write_xml ();
220
221         map<shared_ptr<const dcp::ReelFileAsset>, dcp::Key> keys;
222         keys[reel_picture_asset] = key;
223         keys[reel_sound_asset] = key;
224         keys[reel_subs_asset] = key;
225
226         dcp::DecryptedKDM kdm (cpl->id(), keys, dcp::LocalTime(), dcp::LocalTime(), "foo", "bar", "baz");
227
228         dcp::DCP dcp_read (dir);
229         dcp_read.read ();
230         dcp_read.add (kdm);
231
232         BOOST_REQUIRE_EQUAL (dcp_read.cpls().size(), 1U);
233         auto cpl_read = dcp_read.cpls()[0];
234         BOOST_REQUIRE_EQUAL (cpl_read->reels().size(), 1U);
235         auto reel_read = cpl_read->reels()[0];
236
237         BOOST_REQUIRE (reel_read->main_picture());
238         BOOST_CHECK (reel_read->main_picture()->asset()->key());
239         BOOST_REQUIRE (reel_read->main_sound());
240         BOOST_CHECK (reel_read->main_sound()->asset()->key());
241         BOOST_REQUIRE (reel_read->main_subtitle());
242         auto smpte_sub = dynamic_pointer_cast<dcp::SMPTESubtitleAsset>(reel_read->main_subtitle()->asset());
243         BOOST_REQUIRE (smpte_sub);
244         BOOST_CHECK (smpte_sub->key());
245 }
246