dbf013111e3465637ddcd210405fe5536e5aa9ee
[dcpomatic.git] / src / lib / screen.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "cinema.h"
23 #include "config.h"
24 #include "film.h"
25 #include "kdm_util.h"
26 #include "kdm_with_metadata.h"
27 #include "screen.h"
28 #include <libxml++/libxml++.h>
29 #include <boost/algorithm/string.hpp>
30 #include <boost/date_time/posix_time/posix_time.hpp>
31
32
33 using std::list;
34 using std::make_shared;
35 using std::shared_ptr;
36 using std::string;
37 using std::vector;
38 using boost::optional;
39 using namespace dcpomatic;
40
41
42 Screen::Screen (cxml::ConstNodePtr node)
43         : KDMRecipient (node)
44 {
45         for (auto i: node->node_children ("TrustedDevice")) {
46                 if (boost::algorithm::starts_with(i->content(), "-----BEGIN CERTIFICATE-----")) {
47                         trusted_devices.push_back (TrustedDevice(dcp::Certificate(i->content())));
48                 } else {
49                         trusted_devices.push_back (TrustedDevice(i->content()));
50                 }
51         }
52 }
53
54
55 void
56 Screen::as_xml (xmlpp::Element* parent) const
57 {
58         KDMRecipient::as_xml (parent);
59         for (auto i: trusted_devices) {
60                 parent->add_child("TrustedDevice")->add_child_text(i.as_string());
61         }
62 }
63
64
65 vector<string>
66 Screen::trusted_device_thumbprints () const
67 {
68         vector<string> t;
69         for (auto i: trusted_devices) {
70                 t.push_back (i.thumbprint());
71         }
72         return t;
73 }
74
75
76 KDMWithMetadataPtr
77 kdm_for_screen (
78         std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm,
79         shared_ptr<const dcpomatic::Screen> screen,
80         boost::posix_time::ptime valid_from,
81         boost::posix_time::ptime valid_to,
82         dcp::Formulation formulation,
83         bool disable_forensic_marking_picture,
84         optional<int> disable_forensic_marking_audio,
85         vector<KDMCertificatePeriod>& period_checks
86         )
87 {
88         if (!screen->recipient) {
89                 return {};
90         }
91
92         auto cinema = screen->cinema;
93         dcp::LocalTime const begin(valid_from, dcp::UTCOffset(cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0));
94         dcp::LocalTime const end  (valid_to,   dcp::UTCOffset(cinema ? cinema->utc_offset_hour() : 0, cinema ? cinema->utc_offset_minute() : 0));
95
96         period_checks.push_back(check_kdm_and_certificate_validity_periods(screen->recipient.get(), begin, end));
97
98         auto signer = Config::instance()->signer_chain();
99         if (!signer->valid()) {
100                 throw InvalidSignerError();
101         }
102
103         auto kdm = make_kdm(begin, end).encrypt(
104                 signer, screen->recipient.get(), screen->trusted_device_thumbprints(), formulation, disable_forensic_marking_picture, disable_forensic_marking_audio
105                 );
106
107         dcp::NameFormat::Map name_values;
108         if (cinema) {
109                 name_values['c'] = cinema->name;
110         } else {
111                 name_values['c'] = "";
112         }
113         name_values['s'] = screen->name;
114         name_values['f'] = kdm.content_title_text();
115         name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
116         name_values['e'] = end.date() + " " + end.time_of_day(true, false);
117         name_values['i'] = kdm.cpl_id();
118
119         return make_shared<KDMWithMetadata>(name_values, cinema.get(), cinema ? cinema->emails : list<string>(), kdm);
120 }
121