swaroop: basics of encrypted MP4 playback.
[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 "exceptions.h"
26 #include <dcp/key.h>
27 #include <dcp/util.h>
28 #include <dcp/certificate.h>
29 #include <openssl/rsa.h>
30 #include <openssl/pem.h>
31 #include <openssl/err.h>
32
33 using std::string;
34 using std::runtime_error;
35 using dcp::Certificate;
36
37 DecryptedECinemaKDM::DecryptedECinemaKDM (string id, dcp::Key content_key)
38         : _id (id)
39         , _content_key (content_key)
40 {
41
42 }
43
44 DecryptedECinemaKDM::DecryptedECinemaKDM (EncryptedECinemaKDM kdm, string private_key)
45         : _id (kdm.id())
46 {
47         /* Read the private key */
48
49         BIO* bio = BIO_new_mem_buf (const_cast<char *> (private_key.c_str()), -1);
50         if (!bio) {
51                 throw runtime_error ("could not create memory BIO");
52         }
53
54         RSA* rsa = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
55         if (!rsa) {
56                 throw FileError ("could not read RSA private key file", private_key);
57         }
58
59         uint8_t value[RSA_size(rsa)];
60         int const len = RSA_private_decrypt (kdm.key().size(), kdm.key().data().get(), value, rsa, RSA_PKCS1_OAEP_PADDING);
61         if (len == -1) {
62                 throw KDMError (ERR_error_string(ERR_get_error(), 0), "");
63         }
64
65         _content_key = dcp::Key (value, len);
66 }
67
68 EncryptedECinemaKDM
69 DecryptedECinemaKDM::encrypt (Certificate recipient)
70 {
71         return EncryptedECinemaKDM (_id, _content_key, recipient);
72 }
73
74 #endif