2 Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic 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.
11 DCP-o-matic 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.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #ifdef DCPOMATIC_VARIANT_SWAROOP
23 #include "encrypted_ecinema_kdm.h"
24 #include "ecinema_kdm_data.h"
25 #include "exceptions.h"
28 #include <dcp/certificate.h>
30 #include <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32 #include <openssl/rsa.h>
33 #include <openssl/err.h>
38 using boost::shared_ptr;
39 using boost::optional;
40 using dcp::Certificate;
42 EncryptedECinemaKDM::EncryptedECinemaKDM (string id, string name, dcp::Key content_key, optional<dcp::LocalTime> not_valid_before, optional<dcp::LocalTime> not_valid_after, Certificate recipient)
46 RSA* rsa = recipient.public_key ();
47 _data = dcp::Data (RSA_size(rsa));
49 int input_size = ECINEMA_KDM_KEY_LENGTH;
50 if (not_valid_before && not_valid_after) {
51 input_size += ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH + ECINEMA_KDM_NOT_VALID_AFTER_LENGTH;
54 dcp::Data input (input_size);
55 memcpy (input.data().get(), content_key.value(), ECINEMA_KDM_KEY_LENGTH);
56 if (not_valid_before && not_valid_after) {
57 memcpy (input.data().get() + ECINEMA_KDM_NOT_VALID_BEFORE, not_valid_before->as_string().c_str(), ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH);
58 memcpy (input.data().get() + ECINEMA_KDM_NOT_VALID_AFTER, not_valid_after->as_string().c_str(), ECINEMA_KDM_NOT_VALID_AFTER_LENGTH);
61 int const N = RSA_public_encrypt (input_size, input.data().get(), _data.data().get(), rsa, RSA_PKCS1_OAEP_PADDING);
63 throw KDMError ("Could not encrypt ECinema KDM", ERR_error_string(ERR_get_error(), 0));
68 EncryptedECinemaKDM::EncryptedECinemaKDM (string xml)
70 cxml::Document doc ("ECinemaSecurityMessage");
71 doc.read_string (xml);
72 _id = doc.string_child ("Id");
73 _name = doc.string_child ("Name");
74 _data = dcp::Data (256);
75 int const len = dcp::base64_decode (doc.string_child("Data"), _data.data().get(), _data.size());
80 EncryptedECinemaKDM::as_xml () const
84 /* Lazy overallocation */
85 char out[_data.size() * 2];
86 Kumu::base64encode (_data.data().get(), _data.size(), out, _data.size() * 2);
87 int const N = strlen (out);
89 for (int i = 0; i < N; ++i) {
90 if (i > 0 && (i % 64) == 0) {
96 xmlpp::Document document;
97 xmlpp::Element* root = document.create_root_node ("ECinemaSecurityMessage");
98 root->add_child("Id")->add_child_text(_id);
99 root->add_child("Name")->add_child_text(_name);
100 root->add_child("Data")->add_child_text(lines);
101 return document.write_to_string ("UTF-8");
105 EncryptedECinemaKDM::as_xml (boost::filesystem::path path) const
107 FILE* f = fopen_boost (path, "w");
108 string const x = as_xml ();
109 fwrite (x.c_str(), 1, x.length(), f);