e0256cb4061cd629db5f59be32b6233905ee37b3
[libdcp.git] / test / encryption_test.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "KM_util.h"
21 #include "metadata.h"
22 #include "certificates.h"
23 #include "dcp.h"
24 #include "signer.h"
25 #include "cpl.h"
26 #include "mono_picture_mxf.h"
27 #include "picture_mxf_writer.h"
28 #include "sound_mxf_writer.h"
29 #include "sound_mxf.h"
30 #include "reel.h"
31 #include "test.h"
32 #include "file.h"
33 #include "subtitle_content.h"
34 #include "reel_mono_picture_asset.h"
35 #include "reel_sound_asset.h"
36 #include "encrypted_kdm.h"
37 #include "decrypted_kdm.h"
38 #include <sndfile.h>
39 #include <boost/test/unit_test.hpp>
40 #include <boost/shared_ptr.hpp>
41
42 using boost::shared_ptr;
43
44 /* Load a certificate chain from build/test/data/ *.pem and then build
45    an encrypted DCP and a KDM using it.
46 */
47 BOOST_AUTO_TEST_CASE (encryption_test)
48 {
49         boost::filesystem::remove_all ("build/test/signer");
50         boost::filesystem::create_directory ("build/test/signer");
51         
52         Kumu::libdcp_test = true;
53
54         dcp::MXFMetadata mxf_metadata;
55         mxf_metadata.company_name = "OpenDCP";
56         mxf_metadata.product_name = "OpenDCP";
57         mxf_metadata.product_version = "0.0.25";
58
59         dcp::XMLMetadata xml_metadata;
60         xml_metadata.issuer = "OpenDCP 0.0.25";
61         xml_metadata.creator = "OpenDCP 0.0.25";
62         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
63         
64         boost::filesystem::remove_all ("build/test/DCP/encryption_test");
65         boost::filesystem::create_directories ("build/test/DCP/encryption_test");
66         dcp::DCP d ("build/test/DCP/encryption_test");
67
68         /* Use test/ref/crypt so this test is repeatable */
69         dcp::CertificateChain chain;
70         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
71         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
72         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
73
74         shared_ptr<dcp::Signer> signer (
75                 new dcp::Signer (
76                         chain,
77                         dcp::file_to_string ("test/ref/crypt/leaf.key")
78                         )
79                 );
80
81         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("A Test DCP", dcp::FEATURE));
82
83         dcp::Key key;
84         
85         shared_ptr<dcp::MonoPictureMXF> mp (new dcp::MonoPictureMXF (dcp::Fraction (24, 1)));
86         mp->set_metadata (mxf_metadata);
87         mp->set_key (key);
88
89         shared_ptr<dcp::PictureMXFWriter> writer = mp->start_write ("build/test/DCP/encryption_test/video.mxf", dcp::SMPTE, false);
90         dcp::File j2c ("test/data/32x32_red_square.j2c");
91         for (int i = 0; i < 24; ++i) {
92                 writer->write (j2c.data (), j2c.size ());
93         }
94         writer->finalize ();
95
96         shared_ptr<dcp::SoundMXF> ms (new dcp::SoundMXF (dcp::Fraction (24, 1), 48000, 1));
97         ms->set_key (key);
98         shared_ptr<dcp::SoundMXFWriter> sound_writer = ms->start_write ("build/test/DCP/encryption_test/audio.mxf", dcp::SMPTE);
99         
100         SF_INFO info;
101         info.format = 0;
102         SNDFILE* sndfile = sf_open ("test/data/1s_24-bit_48k_silence.wav", SFM_READ, &info);
103         BOOST_CHECK (sndfile);
104         float buffer[4096*6];
105         float* channels[1];
106         channels[0] = buffer;
107         while (1) {
108                 sf_count_t N = sf_readf_float (sndfile, buffer, 4096);
109                 sound_writer->write (channels, N);
110                 if (N < 4096) {
111                         break;
112                 }
113         }
114         
115         sound_writer->finalize ();      
116
117         cpl->add (shared_ptr<dcp::Reel> (new dcp::Reel (
118                                                  shared_ptr<dcp::ReelMonoPictureAsset> (new dcp::ReelMonoPictureAsset (mp, 0)),
119                                                  shared_ptr<dcp::ReelSoundAsset> (new dcp::ReelSoundAsset (ms, 0)),
120                                                  shared_ptr<dcp::ReelSubtitleAsset> ()
121                                                  )));
122         cpl->set_content_version_id ("urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
123         cpl->set_content_version_label_text ("81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00");
124         cpl->set_metadata (xml_metadata);
125         d.add (cpl);
126         d.write_xml (dcp::SMPTE, xml_metadata, signer);
127
128         dcp::DecryptedKDM kdm (
129                 cpl,
130                 key,
131                 dcp::LocalTime ("2013-01-01T00:00:00+00:00"),
132                 dcp::LocalTime ("2017-01-08T00:00:00+00:00"),
133                 "libdcp",
134                 "test",
135                 "2012-07-17T04:45:18+00:00"
136                 );
137
138         kdm.encrypt(signer, signer->certificates().leaf(), dcp::MODIFIED_TRANSITIONAL_1).as_xml ("build/test/encryption_test.kdm.xml");
139         
140         int r = system (
141                 "xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/encryption_test.kdm.xml "
142                 "> build/test/xmllint.log 2>&1 < /dev/null"
143                 );
144
145 #ifdef LIBDCP_POSIX     
146         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
147 #else
148         BOOST_CHECK_EQUAL (r, 0);
149 #endif  
150                 
151         r = system ("xmlsec1 verify "
152                 "--pubkey-cert-pem test/ref/crypt/leaf.signed.pem "
153                 "--trusted-pem test/ref/crypt/intermediate.signed.pem "
154                 "--trusted-pem test/ref/crypt/ca.self-signed.pem "
155                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
156                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
157                     "build/test/encryption_test.kdm.xml > build/test/xmlsec1.log 2>&1 < /dev/null");
158         
159 #ifdef LIBDCP_POSIX     
160         BOOST_CHECK_EQUAL (WEXITSTATUS (r), 0);
161 #else
162         BOOST_CHECK_EQUAL (r, 0);
163 #endif  
164 }