Catch read errors from verify_cpl (e.g. basic failures to read a video frame).
[libdcp.git] / src / decrypted_kdm.h
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34
35 /** @file  src/decrypted_kdm.h
36  *  @brief DecryptedKDM class
37  */
38
39
40 #ifndef LIBDCP_DECRYPTED_KDM_H
41 #define LIBDCP_DECRYPTED_KDM_H
42
43
44 #include "key.h"
45 #include "local_time.h"
46 #include "decrypted_kdm_key.h"
47 #include "certificate.h"
48 #include <boost/filesystem.hpp>
49 #include <boost/optional.hpp>
50
51
52 struct decrypted_kdm_test;
53
54
55 namespace dcp {
56
57
58 class DecryptedKDMKey;
59 class EncryptedKDM;
60 class CertificateChain;
61 class CPL;
62 class ReelFileAsset;
63
64
65 /** @class DecryptedKDM
66  *  @brief A decrypted KDM
67  *
68  *  This is a KDM that has either been decrypted by a target private key, or one which
69  *  has been created (by some other means) ready for encryption later.
70  *
71  *  A DecryptedKDM object can be created either from an EncryptedKDM and private key file,
72  *  or from the details of the assets that the KDM should protect.
73  */
74 class DecryptedKDM
75 {
76 public:
77         /** @param kdm Encrypted KDM.
78          *  @param private_key Private key as a PEM-format string.
79          */
80         DecryptedKDM (EncryptedKDM const & kdm, std::string private_key);
81
82         /** Create an empty DecryptedKDM.  After creation you must call
83          *  add_key() to add each key that you want in the KDM.
84          *
85          *  @param not_valid_before Start time for the KDM.
86          *  @param not_valid_after End time for the KDM.
87          */
88         DecryptedKDM (
89                 LocalTime not_valid_before,
90                 LocalTime not_valid_after,
91                 std::string annotation_text,
92                 std::string content_title_text,
93                 std::string issue_date
94                 );
95
96         /** Construct a DecryptedKDM containing a given set of keys.
97          *  @param keys Keys to be included in the DecryptedKDM.
98          */
99         DecryptedKDM (
100                 std::string cpl_id,
101                 std::map<std::shared_ptr<const ReelFileAsset>, Key> keys,
102                 LocalTime not_valid_before,
103                 LocalTime not_valid_after,
104                 std::string annotation_text,
105                 std::string content_title_text,
106                 std::string issue_date
107                 );
108
109         /** Create a DecryptedKDM by taking a CPL and setting up to encrypt each of its
110          *  assets with the same symmetric key.
111          *
112          *  @param cpl CPL that the keys are for.
113          *  @param key Key that was used to encrypt the assets.
114          *  @param not_valid_before Start time for the KDM.
115          *  @param not_valid_after End time for the KDM.
116          */
117         DecryptedKDM (
118                 std::shared_ptr<const CPL> cpl,
119                 Key key,
120                 LocalTime not_valid_before,
121                 LocalTime not_valid_after,
122                 std::string annotation_text,
123                 std::string content_title_text,
124                 std::string issue_date
125                 );
126
127         /** Encrypt this KDM's keys and sign the whole KDM.
128          *  @param signer Chain to sign with.
129          *  @param recipient Certificate of the projector/server which should receive this KDM's keys.
130          *  @param trusted_devices Thumbprints of extra trusted devices which should be written to the KDM (recipient will be written
131          *  as a trusted device automatically and does not need to be included in this list).
132          *  @param formulation Formulation to use for the encrypted KDM.
133          *  @param disable_forensic_marking_picture true to disable forensic marking of picture.
134          *  @param disable_forensic_marking_audio if not set, don't disable forensic marking of audio.  If set to 0,
135          *  disable all forensic marking; if set above 0, disable forensic marking above that channel.
136          *  @return Encrypted KDM.
137          */
138         EncryptedKDM encrypt (
139                 std::shared_ptr<const CertificateChain> signer,
140                 Certificate recipient,
141                 std::vector<std::string> trusted_devices,
142                 Formulation formulation,
143                 bool disable_forensic_marking_picture,
144                 boost::optional<int> disable_forensic_marking_audio
145                 ) const;
146
147         /** @param type (MDIK, MDAK etc.)
148          *  @param key_id Key ID
149          *  @param key The actual symmetric key
150          *  @param cpl_id ID of CPL that the key is for
151          */
152         void add_key (boost::optional<std::string> type, std::string key_id, Key key, std::string cpl_id, Standard standard);
153
154         void add_key (DecryptedKDMKey key);
155
156         /** @return This KDM's (decrypted) keys, which could be used to decrypt assets. */
157         std::vector<DecryptedKDMKey> keys () const {
158                 return _keys;
159         }
160
161         boost::optional<std::string> annotation_text () const {
162                 return _annotation_text;
163         }
164
165         std::string content_title_text () const {
166                 return _content_title_text;
167         }
168
169         std::string issue_date () const {
170                 return _issue_date;
171         }
172
173 private:
174
175         friend struct ::decrypted_kdm_test;
176
177         static void put_uuid (uint8_t ** d, std::string id);
178         static std::string get_uuid (unsigned char ** p);
179
180         LocalTime _not_valid_before;
181         LocalTime _not_valid_after;
182         boost::optional<std::string> _annotation_text;
183         std::string _content_title_text;
184         std::string _issue_date;
185         std::vector<DecryptedKDMKey> _keys;
186 };
187
188
189 }
190
191
192 #endif