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