Merge branch 'master' into 1.0
[libdcp.git] / test / encryption_test.cc
1 /*
2     Copyright (C) 2013 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 <boost/test/unit_test.hpp>
21 #include "kdm.h"
22 #include "KM_util.h"
23 #include "metadata.h"
24 #include "certificates.h"
25 #include "dcp.h"
26 #include "signer.h"
27 #include "cpl.h"
28 #include "mono_picture_asset.h"
29 #include "sound_asset.h"
30 #include "reel.h"
31 #include "test.h"
32 #include "signer_chain.h"
33
34 using boost::shared_ptr;
35
36 /* Load a certificate chain from build/test/data/ *.pem and then build
37    an encrypted DCP and a KDM using it.
38 */
39 BOOST_AUTO_TEST_CASE (encryption)
40 {
41         boost::filesystem::remove_all ("build/test/signer");
42         boost::filesystem::create_directory ("build/test/signer");
43         dcp::make_signer_chain ("build/test/signer", "openssl");
44         
45         Kumu::libdcp_test = true;
46
47         dcp::MXFMetadata mxf_metadata;
48         mxf_metadata.company_name = "OpenDCP";
49         mxf_metadata.product_name = "OpenDCP";
50         mxf_metadata.product_version = "0.0.25";
51
52         dcp::XMLMetadata xml_metadata;
53         xml_metadata.issuer = "OpenDCP 0.0.25";
54         xml_metadata.creator = "OpenDCP 0.0.25";
55         xml_metadata.issue_date = "2012-07-17T04:45:18+00:00";
56         
57         boost::filesystem::remove_all ("build/test/DCP/bar");
58         boost::filesystem::create_directories ("build/test/DCP/bar");
59         dcp::DCP d ("build/test/DCP/bar");
60
61         dcp::CertificateChain chain;
62         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/ca.self-signed.pem"))));
63         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/intermediate.signed.pem"))));
64         chain.add (shared_ptr<dcp::Certificate> (new dcp::Certificate (boost::filesystem::path ("build/test/signer/leaf.signed.pem"))));
65
66         shared_ptr<dcp::Signer> signer (
67                 new dcp::Signer (
68                         chain,
69                         "build/test/signer/leaf.key"
70                         )
71                 );
72
73         shared_ptr<dcp::CPL> cpl (new dcp::CPL ("build/test/DCP/bar", "A Test DCP", dcp::FEATURE, 24, 24));
74
75         dcp::Key key;
76         
77         shared_ptr<dcp::MonoPictureAsset> mp (new dcp::MonoPictureAsset ("build/test/DCP/bar", "video.mxf"));
78         mp->set_progress (&d.Progress);
79         mp->set_edit_rate (24);
80         mp->set_intrinsic_duration (24);
81         mp->set_duration (24);
82         mp->set_size (dcp::Size (32, 32));
83         mp->set_metadata (mxf_metadata);
84         mp->set_key (key);
85         mp->create (j2c);
86
87         shared_ptr<dcp::SoundAsset> ms (new dcp::SoundAsset ("build/test/DCP/bar", "audio.mxf"));
88         ms->set_progress (&d.Progress);
89         ms->set_edit_rate (24);
90         ms->set_intrinsic_duration (24);
91         mp->set_duration (24);
92         ms->set_channels (2);
93         ms->set_metadata (mxf_metadata);
94         ms->set_key (key);
95         ms->create (wav);
96         
97         cpl->add_reel (shared_ptr<dcp::Reel> (new dcp::Reel (mp, ms, shared_ptr<dcp::SubtitleAsset> ())));
98         d.add_cpl (cpl);
99
100         d.write_xml (false, xml_metadata, signer);
101
102         dcp::KDM kdm (
103                 cpl,
104                 signer,
105                 signer->certificates().leaf(),
106                 boost::posix_time::time_from_string ("2013-01-01 00:00:00"),
107                 boost::posix_time::time_from_string ("2013-01-08 00:00:00"),
108                 "libdcp",
109                 "2012-07-17T04:45:18+00:00"
110                 );
111
112         kdm.as_xml ("build/test/bar.kdm.xml");
113         system ("xmllint --path schema --nonet --noout --schema schema/SMPTE-430-1-2006-Amd-1-2009-KDM.xsd build/test/bar.kdm.xml");
114         system ("xmlsec1 verify "
115                 "--pubkey-cert-pem build/test/signer/leaf.signed.pem "
116                 "--trusted-pem build/test/signer/intermediate.signed.pem "
117                 "--trusted-pem build/test/signer/ca.self-signed.pem "
118                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPublic "
119                 "--id-attr:Id http://www.smpte-ra.org/schemas/430-3/2006/ETM:AuthenticatedPrivate "
120                 "build/test/bar.kdm.xml");
121 }