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 "decrypted_ecinema_kdm.h"
25 #include "ecinema_kdm_data.h"
26 #include "exceptions.h"
27 #include "compose.hpp"
30 #include <dcp/certificate.h>
31 #include <openssl/rsa.h>
32 #include <openssl/pem.h>
33 #include <openssl/err.h>
36 using std::runtime_error;
37 using dcp::Certificate;
38 using boost::optional;
40 DecryptedECinemaKDM::DecryptedECinemaKDM (string id, string name, dcp::Key content_key, optional<dcp::LocalTime> not_valid_before, optional<dcp::LocalTime> not_valid_after)
43 , _content_key (content_key)
44 , _not_valid_before (not_valid_before)
45 , _not_valid_after (not_valid_after)
50 DecryptedECinemaKDM::DecryptedECinemaKDM (EncryptedECinemaKDM kdm, string private_key)
54 /* Read the private key */
56 BIO* bio = BIO_new_mem_buf (const_cast<char *> (private_key.c_str()), -1);
58 throw runtime_error ("could not create memory BIO");
61 RSA* rsa = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
63 throw FileError ("could not read RSA private key file", private_key);
66 uint8_t value[RSA_size(rsa)];
67 int const len = RSA_private_decrypt (kdm.data().size(), kdm.data().data().get(), value, rsa, RSA_PKCS1_OAEP_PADDING);
69 throw KDMError (ERR_error_string(ERR_get_error(), 0), "");
72 if (len != ECINEMA_KDM_KEY_LENGTH && len != (ECINEMA_KDM_KEY_LENGTH + ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH + ECINEMA_KDM_NOT_VALID_AFTER_LENGTH)) {
74 "Unexpected data block size in ECinema KDM.",
75 String::compose("Size was %1; expected %2 or %3", ECINEMA_KDM_KEY_LENGTH, ECINEMA_KDM_KEY_LENGTH + ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH + ECINEMA_KDM_NOT_VALID_AFTER_LENGTH)
79 _content_key = dcp::Key (value + ECINEMA_KDM_KEY, ECINEMA_KDM_KEY_LENGTH);
80 if (len > ECINEMA_KDM_KEY_LENGTH) {
81 uint8_t* p = value + ECINEMA_KDM_NOT_VALID_BEFORE;
83 for (int i = 0; i < ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH; ++i) {
86 _not_valid_before = dcp::LocalTime (b);
88 for (int i = 0; i < ECINEMA_KDM_NOT_VALID_AFTER_LENGTH; ++i) {
91 _not_valid_after = dcp::LocalTime (a);
96 DecryptedECinemaKDM::encrypt (Certificate recipient)
98 return EncryptedECinemaKDM (_id, _name, _content_key, _not_valid_before, _not_valid_after, recipient);