Simple pass-through of <Ruby> tags in subtitles.
[libdcp.git] / test / encryption_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 "metadata.h"
35 #include "certificate.h"
36 #include "dcp.h"
37 #include "certificate_chain.h"
38 #include "cpl.h"
39 #include "filesystem.h"
40 #include "mono_picture_asset.h"
41 #include "picture_asset_writer.h"
42 #include "sound_asset_writer.h"
43 #include "sound_asset.h"
44 #include "reel.h"
45 #include "test.h"
46 #include "subtitle_asset.h"
47 #include "reel_mono_picture_asset.h"
48 #include "reel_sound_asset.h"
49 #include "encrypted_kdm.h"
50 #include "decrypted_kdm.h"
51 #include <asdcp/KM_util.h>
52 #include <sndfile.h>
53 #include <boost/test/unit_test.hpp>
54 #include <memory>
55
56
57 using std::vector;
58 using std::string;
59 using std::shared_ptr;
60 using std::make_shared;
61
62
63 /** Load a certificate chain from build/test/data/ *.pem and then build
64  *  an encrypted DCP and a KDM using it.
65  */
66 BOOST_AUTO_TEST_CASE (encryption_test)
67 {
68         boost::filesystem::remove_all ("build/test/signer");
69         boost::filesystem::create_directory ("build/test/signer");
70
71         RNGFixer fix;
72
73         dcp::MXFMetadata mxf_metadata;
74         mxf_metadata.company_name = "OpenDCP";
75         mxf_metadata.product_name = "OpenDCP";
76         mxf_metadata.product_version = "0.0.25";
77
78         boost::filesystem::remove_all ("build/test/DCP/encryption_test");
79         boost::filesystem::create_directories ("build/test/DCP/encryption_test");
80         dcp::DCP d ("build/test/DCP/encryption_test");
81
82         /* Use test/ref/crypt so this test is repeatable */
83         auto signer = make_shared<dcp::CertificateChain>();
84         signer->add (dcp::Certificate(dcp::file_to_string("test/ref/crypt/ca.self-signed.pem")));
85         signer->add (dcp::Certificate(dcp::file_to_string("test/ref/crypt/intermediate.signed.pem")));
86         signer->add (dcp::Certificate(dcp::file_to_string("test/ref/crypt/leaf.signed.pem")));
87         signer->set_key (dcp::file_to_string("test/ref/crypt/leaf.key"));
88
89         auto cpl = make_shared<dcp::CPL>("A Test DCP", dcp::ContentKind::FEATURE, dcp::Standard::SMPTE);
90
91         dcp::Key key;
92
93         auto mp = make_shared<dcp::MonoPictureAsset>(dcp::Fraction (24, 1), dcp::Standard::SMPTE);
94         mp->set_metadata (mxf_metadata);
95         mp->set_key (key);
96
97         auto writer = mp->start_write("build/test/DCP/encryption_test/video.mxf", dcp::PictureAsset::Behaviour::MAKE_NEW);
98         dcp::ArrayData j2c ("test/data/flat_red.j2c");
99         for (int i = 0; i < 24; ++i) {
100                 writer->write (j2c.data (), j2c.size ());
101         }
102         writer->finalize ();
103
104         auto ms = make_shared<dcp::SoundAsset>(dcp::Fraction (24, 1), 48000, 1, dcp::LanguageTag("en-GB"), dcp::Standard::SMPTE);
105         ms->set_metadata (mxf_metadata);
106         ms->set_key (key);
107         auto sound_writer = ms->start_write("build/test/DCP/encryption_test/audio.mxf", {}, dcp::SoundAsset::AtmosSync::DISABLED, dcp::SoundAsset::MCASubDescriptors::ENABLED);
108
109         SF_INFO info;
110         info.format = 0;
111         auto sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
112         BOOST_CHECK (sndfile);
113         float buffer[4096*6];
114         float* channels[1];
115         channels[0] = buffer;
116         while (true) {
117                 auto N = sf_readf_float (sndfile, buffer, 4096);
118                 sound_writer->write(channels, 1, N);
119                 if (N < 4096) {
120                         break;
121                 }
122         }
123
124         sound_writer->finalize ();
125
126         cpl->add (make_shared<dcp::Reel>(
127                         make_shared<dcp::ReelMonoPictureAsset>(mp, 0),
128                         make_shared<dcp::ReelSoundAsset>(ms, 0),
129                         shared_ptr<dcp::ReelSubtitleAsset>()
130                         ));
131         cpl->set_content_version (
132                 dcp::ContentVersion("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00", "81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00")
133                 );
134         cpl->set_annotation_text ("A Test DCP");
135         cpl->set_issuer ("OpenDCP 0.0.25");
136         cpl->set_creator ("OpenDCP 0.0.25");
137         cpl->set_issue_date ("2012-07-17T04:45:18+00:00");
138
139         d.add (cpl);
140
141         d.set_issuer("OpenDCP 0.0.25");
142         d.set_creator("OpenDCP 0.0.25");
143         d.set_issue_date("2012-07-17T04:45:18+00:00");
144         d.set_annotation_text("Created by libdcp");
145         d.write_xml(signer);
146
147         dcp::DecryptedKDM kdm (
148                 cpl,
149                 key,
150                 dcp::LocalTime ("2016-01-01T00:00:00+00:00"),
151                 dcp::LocalTime ("2017-01-08T00:00:00+00:00"),
152                 "libdcp",
153                 "test",
154                 "2012-07-17T04:45:18+00:00"
155                 );
156
157         kdm.encrypt (signer, signer->leaf(), vector<string>(), dcp::Formulation::MODIFIED_TRANSITIONAL_1, true, 0).as_xml("build/test/encryption_test.kdm.xml");
158
159         /* Make sure we aren't in a UNC current working directory otherwise the use of cmd.exe
160          * in system() below will fail.
161          */
162         boost::filesystem::current_path(dcp::filesystem::unfix_long_path(boost::filesystem::current_path()));
163
164         int r = system (
165                 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/encryption_test.kdm.xml "
166 #ifndef LIBDCP_WINDOWS
167                 "> build/test/xmllint.log 2>&1 < /dev/null"
168 #endif
169                 );
170
171 #ifdef LIBDCP_WINDOWS
172         BOOST_CHECK_EQUAL (r, 0);
173 #else
174         BOOST_CHECK_EQUAL (WEXITSTATUS(r), 0);
175 #endif
176
177         r = system ("xmlsec1 verify "
178                 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
179                 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
180                 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
181                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
182                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate --crypto openssl "
183                 "build/test/encryption_test.kdm.xml "
184 #ifndef LIBDCP_WINDOWS
185                 "> build/test/xmlsec1.log 2>&1 < /dev/null"
186 #endif
187                    );
188
189 #ifdef LIBDCP_WINDOWS
190         BOOST_CHECK_EQUAL (r, 0);
191 #else
192         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
193 #endif
194 }