Warn/error on making KDMs using recipient certs whose validity periods
[dcpomatic.git] / src / lib / kdm_util.cc
1 /*
2     Copyright (C) 2023 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 "kdm_util.h"
23 #include "screen.h"
24 #include <dcp/certificate.h>
25 #include <boost/optional.hpp>
26
27 #include "i18n.h"
28
29
30 using std::list;
31 using std::pair;
32 using std::shared_ptr;
33 using std::string;
34 using boost::optional;
35
36
37 KDMCertificatePeriod
38 check_kdm_and_certificate_validity_periods(dcp::Certificate const& recipient, dcp::LocalTime kdm_from, dcp::LocalTime kdm_to)
39 {
40         auto overlaps = [](dcp::LocalTime from_a, dcp::LocalTime to_a, dcp::LocalTime from_b, dcp::LocalTime to_b) {
41                 return std::max(from_a, from_b) < std::min(to_a, to_b);
42         };
43
44         auto contains = [](dcp::LocalTime bigger_from, dcp::LocalTime bigger_to, dcp::LocalTime smaller_from, dcp::LocalTime smaller_to) {
45                 return bigger_from <= smaller_from && bigger_to >= smaller_to;
46         };
47
48         if (contains(recipient.not_before(), recipient.not_after(), kdm_from, kdm_to)) {
49                 return KDMCertificatePeriod::KDM_WITHIN_CERTIFICATE;
50         }
51
52         if (overlaps(recipient.not_before(), recipient.not_after(), kdm_from, kdm_to)) {
53                 /* The KDM overlaps the certificate validity: maybe not the end of the world */
54                 return KDMCertificatePeriod::KDM_OVERLAPS_CERTIFICATE;
55         } else {
56                 /* The KDM validity is totally outside the certificate validity: bad news */
57                 return KDMCertificatePeriod::KDM_OUTSIDE_CERTIFICATE;
58         }
59 }
60