Cleanup: header sorting.
[libdcp.git] / examples / make_kdm.cc
1 /*
2     Copyright (C) 2012-2022 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
20
21 /* If you are using an installed libdcp, these #includes would need to be changed to
22 #include <dcp/decrypted_kdm.h>
23 ... etc. ...
24 */
25
26 #include "certificate_chain.h"
27 #include "decrypted_kdm.h"
28 #include "encrypted_kdm.h"
29 #include "key.h"
30 #include "util.h"
31
32
33 constexpr char recipient_certificate[] = "-----BEGIN CERTIFICATE-----\n"
34 "MIIEaTCCA1GgAwIBAgIBBzANBgkqhkiG9w0BAQsFADCBhTEWMBQGA1UEChMNZGNw\n"
35 "b21hdGljLmNvbTEWMBQGA1UECxMNZGNwb21hdGljLmNvbTEsMCoGA1UEAxMjLmRj\n"
36 "cG9tYXRpYy5zbXB0ZS00MzAtMi5JTlRFUk1FRElBVEUxJTAjBgNVBC4THEJyRE1x\n"
37 "TjF4bytQcy9ZZTdLTmVhNzRHdlI5Yz0wHhcNMjIwOTIwMTk1MTQxWhcNMzIwOTIy\n"
38 "MTk1MTQxWjB/MRYwFAYDVQQKEw1kY3BvbWF0aWMuY29tMRYwFAYDVQQLEw1kY3Bv\n"
39 "bWF0aWMuY29tMSYwJAYDVQQDEx1DUy5kY3BvbWF0aWMuc21wdGUtNDMwLTIuTEVB\n"
40 "RjElMCMGA1UELhMcSmR4aEVZdURUR05RQlh2TFpsWEZReVVGSzdZPTCCASIwDQYJ\n"
41 "KoZIhvcNAQEBBQADggEPADCCAQoCggE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H74QRPvrVNOC4vcjnHnBtlWmFzGOi+1e4stWupL7IYd\n"
57 "Apivqyi9TqCUHkjLyuZPjEU30borxqrl918Z/Co=\n"
58 "-----END CERTIFICATE-----\n";
59
60
61 int main()
62 {
63         /* The parameter to this call specifies where resources can be found, i.e.
64          * the tags and schema directories.
65          */
66         dcp::init(boost::filesystem::path("."));
67
68         /* Make a KDM to hold one or more asset keys */
69         dcp::DecryptedKDM decrypted_kdm(
70                 // valid from time
71                 dcp::LocalTime("2023-01-20T09:30:00"),
72                 // valid to time
73                 dcp::LocalTime("2023-11-01T09:30:00"),
74                 // annotation text
75                 "KDM annotation",
76                 // content title text
77                 "KDM content title",
78                 // issue date; using dcp::LocalTime() gives the time when the code is run
79                 dcp::LocalTime().as_string()
80                 );
81
82         /* Add a key which can decrypt an asset; you should do this for each encrypted asset
83          * that the KDM must unlock.
84          */
85         decrypted_kdm.add_key(
86                 // key type (MDIK is for image MXFs)
87                 std::string{"MDIK"},
88                 // key ID
89                 "01234567-89ab-cdef-0123-456789abcdef",
90                 // key
91                 dcp::Key("00112233445566778899aabbccddeeff"),
92                 // CPL ID
93                 "fedcba98-7654-3210-fedc-ba9876543210",
94                 // Standard for KDM (interop/SMPTE)
95                 dcp::Standard::SMPTE
96                 );
97
98         /* Make a certificate chain to sign the KDM */
99         auto signer = std::make_shared<dcp::CertificateChain>("/usr/bin/openssl", 365);
100
101         /* Certificate of the recipient projector/media block */
102         dcp::Certificate recipient(recipient_certificate);
103
104         /* Encrypt the KDM */
105         auto encrypted_kdm = decrypted_kdm.encrypt(
106                 signer,
107                 recipient,
108                 {},
109                 dcp::Formulation::MODIFIED_TRANSITIONAL_1,
110                 false,
111                 {}
112                 );
113
114         /* Dump the XML to the console */
115         std::cout << encrypted_kdm.as_xml();
116
117         return 0;
118 }
119