summaryrefslogtreecommitdiff
path: root/tools/dcpkdm.cc
blob: c2a4859bb297a6c315fac5e67cbabe1c555b796a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
    Copyright (C) 2017-2021 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.

    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
*/


#include "certificate_chain.h"
#include "decrypted_kdm.h"
#include "encrypted_kdm.h"
#include "exceptions.h"
#include "util.h"
#include <getopt.h>


using std::cerr;
using std::cout;
using std::string;
using boost::optional;


static void
help (string n)
{
	cerr << "Syntax: " << n << " [OPTION] <KDM>\n"
	     << "  -h, --help         show this help\n"
	     << "  -p, --private-key  private key file\n";
}


int
main (int argc, char* argv[])
try
{
	dcp::init ();

	optional<boost::filesystem::path> private_key_file;

	int option_index = 0;
	while (true) {
		struct option long_options[] = {
			{ "help", no_argument, 0, 'h' },
			{ "private-key", required_argument, 0, 'p'},
			{ 0, 0, 0, 0 }
		};

		int c = getopt_long (argc, argv, "hp:", long_options, &option_index);

		if (c == -1) {
			break;
		}

		switch (c) {
		case 'h':
			help (argv[0]);
			exit (EXIT_SUCCESS);
		case 'p':
			private_key_file = optarg;
			break;
		}
	}

	if (optind >= argc) {
		help (argv[0]);
		exit (EXIT_FAILURE);
	}

	for (int kdm_index = optind; kdm_index < argc; ++kdm_index) {

		boost::filesystem::path kdm_file = argv[optind];

		dcp::EncryptedKDM enc_kdm (dcp::file_to_string (kdm_file));

		if (enc_kdm.annotation_text()) {
			cout << "Annotation:       " << enc_kdm.annotation_text().get() << "\n";
		}
		cout << "Content title:    " << enc_kdm.content_title_text() << "\n";
		cout << "CPL id:           " << enc_kdm.cpl_id() << "\n";
		cout << "Recipient:        " << enc_kdm.recipient_x509_subject_name() << "\n";
		cout << "Not valid before: " << enc_kdm.not_valid_before().as_string() << "\n";
		cout << "Not valid after:  " << enc_kdm.not_valid_after().as_string() << "\n";

		cout << "Signer chain:\n";
		dcp::CertificateChain signer = enc_kdm.signer_certificate_chain ();
		for (auto const& i: signer.root_to_leaf()) {
			cout << "\tCertificate:\n";
			cout << "\t\tSubject: " << i.subject() << "\n";
			cout << "\t\tSubject common name: " << i.subject_common_name() << "\n";
			cout << "\t\tSubject organization name: " << i.subject_organization_name() << "\n";
			cout << "\t\tSubject organizational unit name: " << i.subject_organizational_unit_name() << "\n";
			cout << "\t\tNot before: " << i.not_before().as_string() << "\n";
			cout << "\t\tNot after:  " << i.not_after().as_string() << "\n";
			if (i.has_utf8_strings()) {
				cout << "\t\tUSES INCORRECT (UTF8) STRING ENCODING\n";
			}
		}

		if (private_key_file) {
			try {
				dcp::DecryptedKDM dec_kdm (enc_kdm, dcp::file_to_string (private_key_file.get()));
				cout << "\nKeys:";
				for (auto i: dec_kdm.keys()) {
					cout << "\n";
					cout << "\tID:       " << i.id() << "\n";
					cout << "\tStandard: " << (i.standard() == dcp::Standard::SMPTE ? "SMPTE" : "Interop") << "\n";
					cout << "\tCPL ID:   " << i.cpl_id() << "\n";
					if (i.type()) {
						cout << "\tType:     " << i.type().get() << "\n";
					}
					cout << "\tKey:      " << i.key().hex() << "\n";
				}
			} catch (dcp::KDMDecryptionError& e) {
				cerr << e.what() << "\n";
				exit (EXIT_FAILURE);
			}
		}
	}

	return 0;
}
catch (dcp::KDMFormatError& e)
{
	cerr << "Could not parse KDM file.  Is it a valid KDM?" << "\n";
	exit(EXIT_FAILURE);
}
catch (std::exception& e)
{
	cerr << "Error: " << e.what() << "\n";
	exit (EXIT_FAILURE);
}