Another macOS std::list boost::thread SNAFU.
[dcpomatic.git] / src / lib / decrypted_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 "decrypted_ecinema_kdm.h"
25 #include "ecinema_kdm_data.h"
26 #include "exceptions.h"
27 #include "compose.hpp"
28 #include <dcp/key.h>
29 #include <dcp/util.h>
30 #include <dcp/certificate.h>
31 #include <openssl/rsa.h>
32 #include <openssl/pem.h>
33 #include <openssl/err.h>
34
35 using std::string;
36 using std::runtime_error;
37 using dcp::Certificate;
38 using boost::optional;
39
40 DecryptedECinemaKDM::DecryptedECinemaKDM (string id, string name, dcp::Key content_key, optional<dcp::LocalTime> not_valid_before, optional<dcp::LocalTime> not_valid_after)
41         : _id (id)
42         , _name (name)
43         , _content_key (content_key)
44         , _not_valid_before (not_valid_before)
45         , _not_valid_after (not_valid_after)
46 {
47
48 }
49
50 DecryptedECinemaKDM::DecryptedECinemaKDM (EncryptedECinemaKDM kdm, string private_key)
51         : _id (kdm.id())
52         , _name (kdm.name())
53 {
54         /* Read the private key */
55
56         BIO* bio = BIO_new_mem_buf (const_cast<char *> (private_key.c_str()), -1);
57         if (!bio) {
58                 throw runtime_error ("could not create memory BIO");
59         }
60
61         RSA* rsa = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
62         if (!rsa) {
63                 throw FileError ("could not read RSA private key file", private_key);
64         }
65
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);
68         if (len == -1) {
69                 throw KDMError (ERR_error_string(ERR_get_error(), 0), "");
70         }
71
72         if (len != ECINEMA_KDM_KEY_LENGTH && len != (ECINEMA_KDM_KEY_LENGTH + ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH + ECINEMA_KDM_NOT_VALID_AFTER_LENGTH)) {
73                 throw KDMError (
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)
76                         );
77         }
78
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;
82                 string b;
83                 for (int i = 0; i < ECINEMA_KDM_NOT_VALID_BEFORE_LENGTH; ++i) {
84                         b += *p++;
85                 }
86                 _not_valid_before = dcp::LocalTime (b);
87                 string a;
88                 for (int i = 0; i < ECINEMA_KDM_NOT_VALID_AFTER_LENGTH; ++i) {
89                         a += *p++;
90                 }
91                 _not_valid_after = dcp::LocalTime (a);
92         }
93 }
94
95 EncryptedECinemaKDM
96 DecryptedECinemaKDM::encrypt (Certificate recipient)
97 {
98         return EncryptedECinemaKDM (_id, _name, _content_key, _not_valid_before, _not_valid_after, recipient);
99 }
100
101 #endif