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