2 Copyright (C) 2013-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 #include "certificate.h"
37 #include "certificate_chain.h"
39 #include "mono_picture_asset.h"
40 #include "picture_asset_writer.h"
41 #include "sound_asset_writer.h"
42 #include "sound_asset.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>
53 #include <boost/test/unit_test.hpp>
54 #include <boost/shared_ptr.hpp>
58 using boost::shared_ptr;
60 /** Load a certificate chain from build/test/data/ *.pem and then build
61 * an encrypted DCP and a KDM using it.
63 BOOST_AUTO_TEST_CASE (encryption_test)
65 boost::filesystem::remove_all ("build/test/signer");
66 boost::filesystem::create_directory ("build/test/signer");
70 dcp::MXFMetadata mxf_metadata;
71 mxf_metadata.company_name = "OpenDCP";
72 mxf_metadata.product_name = "OpenDCP";
73 mxf_metadata.product_version = "0.0.25";
75 dcp::XMLMetadata xml_metadata;
76 xml_metadata.annotation_text = "A Test DCP";
77 xml_metadata.issuer = "OpenDCP 0.0.25";
78 xml_metadata.creator = "OpenDCP 0.0.25";
79 xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
81 boost::filesystem::remove_all ("build/test/DCP/encryption_test");
82 boost::filesystem::create_directories ("build/test/DCP/encryption_test");
83 dcp::DCP d ("build/test/DCP/encryption_test");
85 /* Use test/ref/crypt so this test is repeatable */
86 shared_ptr<dcp::CertificateChain> signer (new dcp::CertificateChain ());
87 signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
88 signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
89 signer->add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
90 signer->set_key (dcp::file_to_string ("test/ref/crypt/leaf.key"));
92 shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
96 shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset (dcp::Fraction (24, 1), dcp::SMPTE));
97 mp->set_metadata (mxf_metadata);
100 shared_ptr<dcp::PictureAssetWriter> writer = mp->start_write ("build/test/DCP/encryption_test/video.mxf", false);
101 dcp::File j2c ("test/data/32x32_red_square.j2c");
102 for (int i = 0; i < 24; ++i) {
103 writer->write (j2c.data (), j2c.size ());
107 shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset (dcp::Fraction (24, 1), 48000, 1, dcp::SMPTE));
108 ms->set_metadata (mxf_metadata);
110 shared_ptr<dcp::SoundAssetWriter> sound_writer = ms->start_write ("build/test/DCP/encryption_test/audio.mxf");
114 SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
115 BOOST_CHECK (sndfile);
116 float buffer[4096*6];
118 channels[0] = buffer;
120 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
121 sound_writer->write (channels, N);
127 sound_writer->finalize ();
129 cpl->add (shared_ptr<dcp::Reel> (new dcp::Reel (
130 shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
131 shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
132 shared_ptr<dcp::ReelSubtitleAsset> ()
134 cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
135 cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
136 cpl->set_issuer (xml_metadata.issuer);
137 cpl->set_creator (xml_metadata.creator);
138 cpl->set_issue_date (xml_metadata.issue_date);
139 cpl->set_annotation_text (xml_metadata.annotation_text);
143 xml_metadata.annotation_text = "Created by libdcp";
144 d.write_xml (dcp::SMPTE, xml_metadata.issuer, xml_metadata.creator, xml_metadata.issue_date, xml_metadata.annotation_text, signer);
146 dcp::DecryptedKDM kdm (
149 dcp::LocalTime ("2016-01-01T00:00:00+00:00"),
150 dcp::LocalTime ("2017-01-08T00:00:00+00:00"),
153 "2012-07-17T04:45:18+00:00"
156 kdm.encrypt (signer, signer->leaf(), vector<string>(), dcp::MODIFIED_TRANSITIONAL_1, true, 0).as_xml ("build/test/encryption_test.kdm.xml");
159 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/encryption_test.kdm.xml "
160 "> build/test/xmllint.log 2>&1 < /dev/null"
164 BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
166 BOOST_CHECK_EQUAL (r, 0);
169 r = system ("xmlsec1 verify "
170 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
171 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
172 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
173 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
174 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
175 "build/test/encryption_test.kdm.xml > build/test/xmlsec1.log 2>&1 < /dev/null");
178 BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
180 BOOST_CHECK_EQUAL (r, 0);