Fix use of Z as a timezone (meaning UTC).
[libdcp.git] / tools / dcpkdm.cc
1 /*
2     Copyright (C) 2017-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 #include "certificate_chain.h"
36 #include "decrypted_kdm.h"
37 #include "encrypted_kdm.h"
38 #include "exceptions.h"
39 #include "util.h"
40 #include <getopt.h>
41
42
43 using std::cerr;
44 using std::cout;
45 using std::string;
46 using boost::optional;
47
48
49 static void
50 help (string n)
51 {
52         cerr << "Syntax: " << n << " [OPTION] <KDM>\n"
53              << "  -h, --help         show this help\n"
54              << "  -p, --private-key  private key file\n";
55 }
56
57
58 int
59 main (int argc, char* argv[])
60 try
61 {
62         dcp::init ();
63
64         optional<boost::filesystem::path> private_key_file;
65
66         int option_index = 0;
67         while (true) {
68                 struct option long_options[] = {
69                         { "help", no_argument, 0, 'h' },
70                         { "private-key", required_argument, 0, 'p'},
71                         { 0, 0, 0, 0 }
72                 };
73
74                 int c = getopt_long (argc, argv, "hp:", long_options, &option_index);
75
76                 if (c == -1) {
77                         break;
78                 }
79
80                 switch (c) {
81                 case 'h':
82                         help (argv[0]);
83                         exit (EXIT_SUCCESS);
84                 case 'p':
85                         private_key_file = optarg;
86                         break;
87                 }
88         }
89
90         if (optind >= argc) {
91                 help (argv[0]);
92                 exit (EXIT_FAILURE);
93         }
94
95         boost::filesystem::path kdm_file = argv[optind];
96
97         dcp::EncryptedKDM enc_kdm (dcp::file_to_string (kdm_file));
98
99         if (enc_kdm.annotation_text()) {
100                 cout << "Annotation:       " << enc_kdm.annotation_text().get() << "\n";
101         }
102         cout << "Content title:    " << enc_kdm.content_title_text() << "\n";
103         cout << "CPL id:           " << enc_kdm.cpl_id() << "\n";
104         cout << "Recipient:        " << enc_kdm.recipient_x509_subject_name() << "\n";
105         cout << "Not valid before: " << enc_kdm.not_valid_before().as_string() << "\n";
106         cout << "Not valid after:  " << enc_kdm.not_valid_after().as_string() << "\n";
107
108         cout << "Signer chain:\n";
109         dcp::CertificateChain signer = enc_kdm.signer_certificate_chain ();
110         for (auto const& i: signer.root_to_leaf()) {
111                 cout << "\tCertificate:\n";
112                 cout << "\t\tSubject: " << i.subject() << "\n";
113                 cout << "\t\tSubject common name: " << i.subject_common_name() << "\n";
114                 cout << "\t\tSubject organization name: " << i.subject_organization_name() << "\n";
115                 cout << "\t\tSubject organizational unit name: " << i.subject_organizational_unit_name() << "\n";
116                 cout << "\t\tNot before: " << i.not_before().as_string() << "\n";
117                 cout << "\t\tNot after:  " << i.not_after().as_string() << "\n";
118                 if (i.has_utf8_strings()) {
119                         cout << "\t\tUSES INCORRECT (UTF8) STRING ENCODING\n";
120                 }
121         }
122
123         if (private_key_file) {
124                 try {
125                         dcp::DecryptedKDM dec_kdm (enc_kdm, dcp::file_to_string (private_key_file.get()));
126                         cout << "\nKeys:";
127                         for (auto i: dec_kdm.keys()) {
128                                 cout << "\n";
129                                 cout << "\tID:       " << i.id() << "\n";
130                                 cout << "\tStandard: " << (i.standard() == dcp::Standard::SMPTE ? "SMPTE" : "Interop") << "\n";
131                                 cout << "\tCPL ID:   " << i.cpl_id() << "\n";
132                                 if (i.type()) {
133                                         cout << "\tType:     " << i.type().get() << "\n";
134                                 }
135                                 cout << "\tKey:      " << i.key().hex() << "\n";
136                         }
137                 } catch (dcp::KDMDecryptionError& e) {
138                         cerr << e.what() << "\n";
139                         exit (EXIT_FAILURE);
140                 }
141         }
142
143         return 0;
144 }
145 catch (dcp::KDMFormatError& e)
146 {
147         cerr << "Could not parse KDM file.  Is it a valid KDM?" << "\n";
148         exit(EXIT_FAILURE);
149 }
150 catch (std::exception& e)
151 {
152         cerr << "Error: " << e.what() << "\n";
153         exit (EXIT_FAILURE);
154 }