Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / encrypted_ecinema_kdm.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #ifdef DCPOMATIC_VARIANT_SWAROOP
22
23 #include "encrypted_ecinema_kdm.h"
24 #include "ecinema_kdm_data.h"
25 #include "exceptions.h"
26 #include "cross.h"
27 #include <dcp/key.h>
28 #include <dcp/certificate.h>
29 #include <dcp/util.h>
30 #include <libcxml/cxml.h>
31 #include <libxml++/libxml++.h>
32 #include <openssl/rsa.h>
33 #include <openssl/err.h>
34 #include <iostream>
35
36 using std::cout;
37 using std::string;
38 using boost::shared_ptr;
39 using boost::optional;
40 using dcp::Certificate;
41
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)
43         : _id (id)
44         , _name (name)
45 {
46         RSA* rsa = recipient.public_key ();
47         _data = dcp::Data (RSA_size(rsa));
48
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;
52         }
53
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);
59         }
60
61         int const N = RSA_public_encrypt (input_size, input.data().get(), _data.data().get(), rsa, RSA_PKCS1_OAEP_PADDING);
62         if (N == -1) {
63                 throw KDMError ("Could not encrypt ECinema KDM", ERR_error_string(ERR_get_error(), 0));
64         }
65
66 }
67
68 EncryptedECinemaKDM::EncryptedECinemaKDM (string xml)
69 {
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());
76         _data.set_size (len);
77 }
78
79 string
80 EncryptedECinemaKDM::as_xml () const
81 {
82         string key;
83
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);
88         string lines;
89         for (int i = 0; i < N; ++i) {
90                 if (i > 0 && (i % 64) == 0) {
91                         lines += "\n";
92                 }
93                 lines += out[i];
94         }
95
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");
102 }
103
104 void
105 EncryptedECinemaKDM::as_xml (boost::filesystem::path path) const
106 {
107         FILE* f = fopen_boost (path, "w");
108         string const x = as_xml ();
109         fwrite (x.c_str(), 1, x.length(), f);
110         fclose (f);
111 }
112
113 #endif