4ac95dc6fbc867f7369dae4463f91882b9afdd8b
[libdcp.git] / src / decrypted_kdm.h
1 /*
2     Copyright (C) 2013-2016 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 #ifndef LIBDCP_DECRYPTED_KDM_H
35 #define LIBDCP_DECRYPTED_KDM_H
36
37 /** @file  src/decrypted_kdm.h
38  *  @brief DecryptedKDM class.
39  */
40
41 #include "key.h"
42 #include "local_time.h"
43 #include "decrypted_kdm_key.h"
44 #include "types.h"
45 #include "certificate.h"
46 #include <boost/filesystem.hpp>
47 #include <boost/optional.hpp>
48
49 namespace dcp {
50
51 class DecryptedKDMKey;
52 class EncryptedKDM;
53 class CertificateChain;
54 class CPL;
55
56 /** @class DecryptedKDM
57  *  @brief A decrypted KDM.
58  *
59  *  This is a KDM that has either been decrypted by a target private key, or one which
60  *  has been created (by some other means) ready for encryption later.
61  *
62  *  A DecryptedKDM object can be created either from an EncryptedKDM and private key file,
63  *  or from the details of the assets that the KDM should protect.
64  */
65 class DecryptedKDM
66 {
67 public:
68         /** @param kdm Encrypted KDM.
69          *  @param private_key Private key as a PEM-format string.
70          */
71         DecryptedKDM (EncryptedKDM const & kdm, std::string private_key);
72
73         /** Create an empty DecryptedKDM.  After creation you must call
74          *  add_key() to add each key that you want in the KDM.
75          *
76          *  @param not_valid_before Start time for the KDM.
77          *  @param not_valid_after End time for the KDM.
78          */
79         DecryptedKDM (
80                 LocalTime not_valid_before,
81                 LocalTime not_valid_after,
82                 std::string annotation_text,
83                 std::string content_title_text,
84                 std::string issue_date
85                 );
86
87         /** Create a DecryptedKDM by taking a CPL and setting up to encrypt each of its
88          *  assets with the same symmetric key.
89          *
90          *  @param cpl CPL that the keys are for.
91          *  @param key Key that was used to encrypt the assets.
92          *  @param not_valid_before Start time for the KDM.
93          *  @param not_valid_after End time for the KDM.
94          */
95         DecryptedKDM (
96                 boost::shared_ptr<const CPL> cpl,
97                 Key key,
98                 LocalTime not_valid_before,
99                 LocalTime not_valid_after,
100                 std::string annotation_text,
101                 std::string content_title_text,
102                 std::string issue_date
103                 );
104
105         /** Encrypt this KDM's keys and sign the whole KDM.
106          *  @param signer Chain to sign with.
107          *  @param recipient Certificate of the projector/server which should receive this KDM's keys.
108          *  @param trusted_devices Extra trusted devices which should be written to the KDM (recipient will be written
109          *  as a trusted device automatically and does not need to be included in this list).
110          *  @param formulation Formulation to use for the encrypted KDM.
111          *  @return Encrypted KDM.
112          */
113         EncryptedKDM encrypt (
114                 boost::shared_ptr<const CertificateChain> signer,
115                 Certificate recipient,
116                 std::vector<Certificate> trusted_devices,
117                 Formulation formulation
118                 ) const;
119
120         void add_key (std::string type, std::string key_id, Key key, std::string cpl_id);
121         void add_key (DecryptedKDMKey key);
122
123         /** @return This KDM's (decrypted) keys, which could be used to decrypt assets. */
124         std::list<DecryptedKDMKey> keys () const {
125                 return _keys;
126         }
127
128         boost::optional<std::string> annotation_text () const {
129                 return _annotation_text;
130         }
131
132         std::string content_title_text () const {
133                 return _content_title_text;
134         }
135
136         std::string issue_date () const {
137                 return _issue_date;
138         }
139
140 private:
141         LocalTime _not_valid_before;
142         LocalTime _not_valid_after;
143         boost::optional<std::string> _annotation_text;
144         std::string _content_title_text;
145         std::string _issue_date;
146         std::list<DecryptedKDMKey> _keys;
147 };
148
149 }
150
151 #endif