X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fdecrypted_kdm.cc;h=9a9f2c2268bb1cd9a769321df83baee16c286f0d;hb=093525165f6397ba9eaffd19833dc978bcb32d55;hp=a928c0a6a966fdcf5a09beb3f44aebb8983c356c;hpb=363b3241b452fac11dfd202cc5b368bd4cd672c3;p=libdcp.git diff --git a/src/decrypted_kdm.cc b/src/decrypted_kdm.cc index a928c0a6..9a9f2c22 100644 --- a/src/decrypted_kdm.cc +++ b/src/decrypted_kdm.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2013-2016 Carl Hetherington + Copyright (C) 2013-2021 Carl Hetherington This file is part of libdcp. @@ -31,10 +31,16 @@ files in the program, then also delete it here. */ + +/** @file src/decrypted_kdm.cc + * @brief DecryptedKDM class + */ + + #include "decrypted_kdm.h" #include "decrypted_kdm_key.h" #include "encrypted_kdm.h" -#include "reel_mxf.h" +#include "reel_encryptable_asset.h" #include "reel_asset.h" #include "util.h" #include "exceptions.h" @@ -47,7 +53,7 @@ #include #include #include -#include + using std::list; using std::vector; @@ -56,9 +62,16 @@ using std::setw; using std::setfill; using std::hex; using std::pair; -using boost::shared_ptr; +using std::map; +using std::shared_ptr; +using boost::optional; using namespace dcp; + +/* Magic value specified by SMPTE S430-1-2006 */ +static uint8_t smpte_structure_id[] = { 0xf1, 0xdc, 0x12, 0x44, 0x60, 0x16, 0x9a, 0x0e, 0x85, 0xbc, 0x30, 0x06, 0x42, 0xf8, 0x66, 0xab }; + + static void put (uint8_t ** d, string s) { @@ -66,6 +79,7 @@ put (uint8_t ** d, string s) (*d) += s.length(); } + static void put (uint8_t ** d, uint8_t const * s, int N) { @@ -73,6 +87,7 @@ put (uint8_t ** d, uint8_t const * s, int N) (*d) += N; } + void DecryptedKDM::put_uuid (uint8_t ** d, string id) { @@ -92,11 +107,16 @@ DecryptedKDM::put_uuid (uint8_t ** d, string id) *d += 16; } + string DecryptedKDM::get_uuid (unsigned char ** p) { char buffer[37]; +#ifdef LIBDCP_WINDOWS + __mingw_snprintf ( +#else snprintf ( +#endif buffer, sizeof(buffer), "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx", (*p)[0], (*p)[1], (*p)[2], (*p)[3], (*p)[4], (*p)[5], (*p)[6], (*p)[7], (*p)[8], (*p)[9], (*p)[10], (*p)[11], (*p)[12], (*p)[13], (*p)[14], (*p)[15] @@ -106,6 +126,7 @@ DecryptedKDM::get_uuid (unsigned char ** p) return buffer; } + static string get (uint8_t ** p, int N) { @@ -118,33 +139,38 @@ get (uint8_t ** p, int N) return g; } + DecryptedKDM::DecryptedKDM (EncryptedKDM const & kdm, string private_key) { /* Read the private key */ - BIO* bio = BIO_new_mem_buf (const_cast (private_key.c_str ()), -1); + auto bio = BIO_new_mem_buf (const_cast(private_key.c_str()), -1); if (!bio) { throw MiscError ("could not create memory BIO"); } - RSA* rsa = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0); + auto rsa = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0); if (!rsa) { throw FileError ("could not read RSA private key file", private_key, errno); } /* Use the private key to decrypt the keys */ - BOOST_FOREACH (string const & i, kdm.keys ()) { + for (auto const& i: kdm.keys()) { /* Decode the base-64-encoded cipher value from the KDM */ unsigned char cipher_value[256]; int const cipher_value_len = base64_decode (i, cipher_value, sizeof (cipher_value)); /* Decrypt it */ - unsigned char * decrypted = new unsigned char[RSA_size(rsa)]; + auto decrypted = new unsigned char[RSA_size(rsa)]; int const decrypted_len = RSA_private_decrypt (cipher_value_len, cipher_value, decrypted, rsa, RSA_PKCS1_OAEP_PADDING); if (decrypted_len == -1) { delete[] decrypted; - throw KDMDecryptionError (ERR_error_string (ERR_get_error(), 0)); +#if OPENSSL_VERSION_NUMBER > 0x10100000L + throw KDMDecryptionError (ERR_error_string (ERR_get_error(), 0), cipher_value_len, RSA_bits(rsa)); +#else + throw KDMDecryptionError (ERR_error_string (ERR_get_error(), 0), cipher_value_len, rsa->n->dmax); +#endif } unsigned char* p = decrypted; @@ -165,13 +191,14 @@ DecryptedKDM::DecryptedKDM (EncryptedKDM const & kdm, string private_key) /* 93 is not-valid-after (a string) [25 bytes] */ p += 25; /* 118 is the key [ASDCP::KeyLen bytes] */ - _keys.push_back (DecryptedKDMKey ("", key_id, Key (p), cpl_id)); + add_key (optional(), key_id, Key(p), cpl_id, Standard::INTEROP); break; } case 138: { /* SMPTE */ /* 0 is structure id (fixed sequence specified by standard) [16 bytes] */ + DCP_ASSERT (memcmp (p, smpte_structure_id, 16) == 0); p += 16; /* 16 is is signer thumbprint [20 bytes] */ p += 20; @@ -186,7 +213,7 @@ DecryptedKDM::DecryptedKDM (EncryptedKDM const & kdm, string private_key) /* 97 is not-valid-after (a string) [25 bytes] */ p += 25; /* 112 is the key [ASDCP::KeyLen bytes] */ - _keys.push_back (DecryptedKDMKey (key_type, key_id, Key (p), cpl_id)); + add_key (key_type, key_id, Key(p), cpl_id, Standard::SMPTE); break; } default: @@ -204,6 +231,7 @@ DecryptedKDM::DecryptedKDM (EncryptedKDM const & kdm, string private_key) _issue_date = kdm.issue_date (); } + DecryptedKDM::DecryptedKDM ( LocalTime not_valid_before, LocalTime not_valid_after, @@ -220,8 +248,30 @@ DecryptedKDM::DecryptedKDM ( } + DecryptedKDM::DecryptedKDM ( - boost::shared_ptr cpl, + string cpl_id, + map, Key> keys, + LocalTime not_valid_before, + LocalTime not_valid_after, + string annotation_text, + string content_title_text, + string issue_date + ) + : _not_valid_before (not_valid_before) + , _not_valid_after (not_valid_after) + , _annotation_text (annotation_text) + , _content_title_text (content_title_text) + , _issue_date (issue_date) +{ + for (map, Key>::const_iterator i = keys.begin(); i != keys.end(); ++i) { + add_key (i->first->key_type(), i->first->key_id().get(), i->second, cpl_id, Standard::SMPTE); + } +} + + +DecryptedKDM::DecryptedKDM ( + shared_ptr cpl, Key key, LocalTime not_valid_before, LocalTime not_valid_after, @@ -237,11 +287,9 @@ DecryptedKDM::DecryptedKDM ( { /* Create DecryptedKDMKey objects for each encryptable asset */ bool did_one = false; - BOOST_FOREACH(shared_ptr i, cpl->reel_assets ()) { - shared_ptr mxf = boost::dynamic_pointer_cast (i); - shared_ptr asset = boost::dynamic_pointer_cast (i); - if (asset && mxf && mxf->key_id ()) { - _keys.push_back (DecryptedKDMKey (mxf->key_type(), mxf->key_id().get(), key, cpl->id ())); + for (auto i: cpl->reel_file_assets()) { + if (i->key_id()) { + add_key (i->key_type(), i->key_id().get(), key, cpl->id(), Standard::SMPTE); did_one = true; } } @@ -251,44 +299,59 @@ DecryptedKDM::DecryptedKDM ( } } -/** @param type (MDIK, MDAK etc.) - * @param key_id Key ID. - * @param key The actual symmetric key. - * @param cpl_id ID of CPL that the key is for. - */ + void -DecryptedKDM::add_key (string type, string key_id, Key key, string cpl_id) +DecryptedKDM::add_key (optional type, string key_id, Key key, string cpl_id, Standard standard) { - _keys.push_back (DecryptedKDMKey (type, key_id, key, cpl_id)); + _keys.push_back (DecryptedKDMKey (type, key_id, key, cpl_id, standard)); } + void DecryptedKDM::add_key (DecryptedKDMKey key) { _keys.push_back (key); } + EncryptedKDM -DecryptedKDM::encrypt (shared_ptr signer, Certificate recipient, vector trusted_devices, Formulation formulation) const +DecryptedKDM::encrypt ( + shared_ptr signer, + Certificate recipient, + vector trusted_devices, + Formulation formulation, + bool disable_forensic_marking_picture, + optional disable_forensic_marking_audio + ) const { - list > key_ids; - list keys; - BOOST_FOREACH (DecryptedKDMKey const & i, _keys) { - key_ids.push_back (make_pair (i.type(), i.id ())); + DCP_ASSERT (!_keys.empty ()); + + for (auto i: signer->leaf_to_root()) { + if (day_greater_than_or_equal(dcp::LocalTime(i.not_before()), _not_valid_before)) { + throw BadKDMDateError (true); + } else if (day_less_than_or_equal(dcp::LocalTime(i.not_after()), _not_valid_after)) { + throw BadKDMDateError (false); + } + } + + vector> key_ids; + vector keys; + for (auto const& i: _keys) { + /* We're making SMPTE keys so we must have a type for each one */ + DCP_ASSERT (i.type()); + key_ids.push_back (make_pair (i.type().get(), i.id ())); /* XXX: SMPTE only */ uint8_t block[138]; uint8_t* p = block; - /* Magic value specified by SMPTE S430-1-2006 */ - uint8_t structure_id[] = { 0xf1, 0xdc, 0x12, 0x44, 0x60, 0x16, 0x9a, 0x0e, 0x85, 0xbc, 0x30, 0x06, 0x42, 0xf8, 0x66, 0xab }; - put (&p, structure_id, 16); + put (&p, smpte_structure_id, 16); base64_decode (signer->leaf().thumbprint (), p, 20); p += 20; put_uuid (&p, i.cpl_id ()); - put (&p, i.type ()); + put (&p, i.type().get()); put_uuid (&p, i.id ()); put (&p, _not_valid_before.as_string ()); put (&p, _not_valid_after.as_string ()); @@ -332,6 +395,8 @@ DecryptedKDM::encrypt (shared_ptr signer, Certificate re _not_valid_before, _not_valid_after, formulation, + disable_forensic_marking_picture, + disable_forensic_marking_audio, key_ids, keys );