Add Time constructor from ticks; fix Time::to_ticks().
[libdcp.git] / src / kdm.cc
index b9e598b913604c2fe4476b25262abac30a0b1786..2005f58a49e3aa82754c426dfe20287f62db22dd 100644 (file)
@@ -46,7 +46,7 @@ using boost::shared_ptr;
 using namespace libdcp;
 
 KDM::KDM (boost::filesystem::path kdm, boost::filesystem::path private_key)
-       : xml_kdm (new xml::DCinemaSecurityMessage (kdm))
+       : _xml_kdm (new xml::DCinemaSecurityMessage (kdm))
 {
        /* Read the private key */
           
@@ -63,7 +63,7 @@ KDM::KDM (boost::filesystem::path kdm, boost::filesystem::path private_key)
 
        /* Use it to decrypt the keys */
 
-       list<string> encrypted_keys = xml_kdm->authenticated_private.encrypted_keys;
+       list<string> encrypted_keys = _xml_kdm->authenticated_private.encrypted_keys;
 
        for (list<string>::iterator i = encrypted_keys.begin(); i != encrypted_keys.end(); ++i) {
 
@@ -89,31 +89,41 @@ KDM::KDM (boost::filesystem::path kdm, boost::filesystem::path private_key)
 KDM::KDM (
        shared_ptr<const CPL> cpl, shared_ptr<const Signer> signer, shared_ptr<const Certificate> recipient_cert,
        boost::posix_time::ptime not_valid_before, boost::posix_time::ptime not_valid_after,
-       MXFMetadata mxf_metadata, XMLMetadata xml_metadata
+       string annotation_text, string issue_date
        )
-       : xml_kdm (new xml::DCinemaSecurityMessage)
+       : _xml_kdm (new xml::DCinemaSecurityMessage)
 {
-       xml::AuthenticatedPublic& apu = xml_kdm->authenticated_public;
+       xml::AuthenticatedPublic& apu = _xml_kdm->authenticated_public;
 
        /* AuthenticatedPublic */
 
        apu.message_id = "urn:uuid:" + make_uuid ();
        apu.message_type = "http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type";
-       apu.annotation_text = mxf_metadata.product_name;
-       apu.issue_date = xml_metadata.issue_date;
+       apu.annotation_text = annotation_text;
+       apu.issue_date = issue_date;
        apu.signer.x509_issuer_name = signer->certificates().leaf()->issuer ();
        apu.signer.x509_serial_number = signer->certificates().leaf()->serial ();
        apu.recipient.x509_issuer_serial.x509_issuer_name = recipient_cert->issuer ();
        apu.recipient.x509_issuer_serial.x509_serial_number = recipient_cert->serial ();
        apu.recipient.x509_subject_name = recipient_cert->subject ();
        apu.composition_playlist_id = "urn:uuid:" + cpl->id ();
+//     apu.content_authenticator = signer->certificates().leaf()->thumbprint ();
        apu.content_title_text = cpl->name ();
        apu.content_keys_not_valid_before = ptime_to_string (not_valid_before);
        apu.content_keys_not_valid_after = ptime_to_string (not_valid_after);
        apu.authorized_device_info.device_list_identifier = "urn:uuid:" + make_uuid ();
-       apu.authorized_device_info.device_list_description = recipient_cert->subject ();
-       apu.authorized_device_info.device_list.push_back (recipient_cert->thumbprint ());
-       
+       string n = recipient_cert->common_name ();
+       if (n.find (".") != string::npos) {
+               n = n.substr (n.find (".") + 1);
+       }
+       apu.authorized_device_info.device_list_description = n;
+//     apu.authorized_device_info.device_list.push_back (recipient_cert->thumbprint ());
+
+       /* Sometimes digital_cinema_tools uses this magic thumbprint instead of that from an actual
+          recipient certificate.  KDMs delivered to City Screen appear to use the same thing.
+       */
+       apu.authorized_device_info.device_list.push_back ("2jmj7l5rSw0yVb/vlWAYkK/YBwk=");
+
        list<shared_ptr<const Asset> > assets = cpl->assets ();
        for (list<shared_ptr<const Asset> >::iterator i = assets.begin(); i != assets.end(); ++i) {
                /* XXX: non-MXF assets? */
@@ -132,30 +142,70 @@ KDM::KDM (
                /* XXX: non-MXF assets? */
                shared_ptr<const MXFAsset> mxf = boost::dynamic_pointer_cast<const MXFAsset> (*i);
                if (mxf) {
-                       xml_kdm->authenticated_private.encrypted_keys.push_back (
-                               KDMKey (signer, cpl->id (), mxf->key_id (), not_valid_before, not_valid_after, mxf->key().get()).base64 ()
+                       KDMKey kkey (
+                                       signer, cpl->id (), mxf->key_type (), mxf->key_id (),
+                                       not_valid_before, not_valid_after, mxf->key().get()
                                );
+
+                       _keys.push_back (kkey);
+                       _xml_kdm->authenticated_private.encrypted_keys.push_back (kkey.encrypted_base64 (recipient_cert));
                }
        }
 
        /* Signature */
 
-       shared_ptr<xmlpp::Document> doc = xml_kdm->as_xml ();
+       shared_ptr<xmlpp::Document> doc = _xml_kdm->as_xml ();
        shared_ptr<cxml::Node> root (new cxml::Node (doc->get_root_node ()));
        xmlpp::Node* signature = root->node_child("Signature")->node();
        signer->add_signature_value (signature, "ds");
-       xml_kdm->signature = xml::Signature (shared_ptr<cxml::Node> (new cxml::Node (signature)));
+       _xml_kdm->signature = xml::Signature (shared_ptr<cxml::Node> (new cxml::Node (signature)));
+}
+
+KDM::KDM (KDM const & other)
+       : _keys (other._keys)
+       , _xml_kdm (new xml::DCinemaSecurityMessage (*other._xml_kdm.get()))
+{
+
 }
 
+KDM &
+KDM::operator= (KDM const & other)
+{
+       if (this == &other) {
+               return *this;
+       }
+
+       _keys = other._keys;
+       _xml_kdm.reset (new xml::DCinemaSecurityMessage (*other._xml_kdm.get ()));
+
+       return *this;
+}
+     
 void
 KDM::as_xml (boost::filesystem::path path) const
 {
-       shared_ptr<xmlpp::Document> doc = xml_kdm->as_xml ();
-       doc->write_to_file_formatted (path.string(), "UTF-8");
+       shared_ptr<xmlpp::Document> doc = _xml_kdm->as_xml ();
+       /* This must *not* be the _formatted version, otherwise the signature
+          will be wrong.
+       */
+       doc->write_to_file (path.string(), "UTF-8");
 }
 
-KDMKey::KDMKey (shared_ptr<const Signer> signer, string cpl_id, string key_id, boost::posix_time::ptime from, boost::posix_time::ptime until, Key key)
+string
+KDM::as_xml () const
+{
+       shared_ptr<xmlpp::Document> doc = _xml_kdm->as_xml ();
+       /* This must *not* be the _formatted version, otherwise the signature
+          will be wrong.
+       */
+       return doc->write_to_string ("UTF-8");
+}
+
+KDMKey::KDMKey (
+       shared_ptr<const Signer> signer, string cpl_id, string key_type, string key_id, boost::posix_time::ptime from, boost::posix_time::ptime until, Key key
+       )
        : _cpl_id (cpl_id)
+       , _key_type (key_type)
        , _key_id (key_id)
        , _not_valid_before (ptime_to_string (from))
        , _not_valid_after (ptime_to_string (until))
@@ -212,7 +262,7 @@ KDMKey::operator= (KDMKey const & other)
        if (&other == this) {
                return *this;
        }
-       
+
        _cpl_id = other._cpl_id;
        _key_type = other._key_type;
        _key_id = other._key_id;
@@ -225,8 +275,12 @@ KDMKey::operator= (KDMKey const & other)
 }
 
 string
-KDMKey::base64 () const
+KDMKey::encrypted_base64 (shared_ptr<const Certificate> recipient_cert) const
 {
+       assert (_key_type.length() == 4);
+       assert (_not_valid_before.length() == 25);
+       assert (_not_valid_after.length() == 25);
+       
        /* XXX: SMPTE only */
        uint8_t block[138];
        uint8_t* p = block;
@@ -242,9 +296,27 @@ KDMKey::base64 () const
        put (&p, _not_valid_after);
        put (&p, _key.value(), ASDCP::KeyLen);
 
+       /* Encrypt using the projector's public key */
+       RSA* rsa = recipient_cert->public_key ();
+       unsigned char encrypted[RSA_size(rsa)];
+       int const encrypted_len = RSA_public_encrypt (p - block, block, encrypted, rsa, RSA_PKCS1_OAEP_PADDING);
+       if (encrypted_len == -1) {
+               throw MiscError (String::compose ("Could not encrypt KDM (%1)", ERR_error_string (ERR_get_error(), 0)));
+       }
+
        /* Lazy overallocation */
-       char string[138 * 2];
-       return Kumu::base64encode (block, 138, string, 138 * 2);
+       char out[encrypted_len * 2];
+       Kumu::base64encode (encrypted, encrypted_len, out, encrypted_len * 2);
+       int const N = strlen (out);
+       stringstream lines;
+       for (int i = 0; i < N; ++i) {
+               if (i > 0 && (i % 64) == 0) {
+                       lines << "\n";
+               }
+               lines << out[i];
+       }
+
+       return lines.str ();
 }
 
 string
@@ -304,8 +376,25 @@ KDMKey::put_uuid (uint8_t ** d, string id) const
                stringstream s;
                s << id[i] << id[i + 1];
                int h;
-               s >> h;
+               s >> hex >> h;
                **d = h;
                (*d)++;
        }
 }
+
+bool
+libdcp::operator== (libdcp::KDMKey const & a, libdcp::KDMKey const & b)
+{
+       if (memcmp (a._signer_thumbprint, b._signer_thumbprint, 20) != 0) {
+               return false;
+       }
+
+       return (
+               a._cpl_id == b._cpl_id &&
+               a._key_type == b._key_type &&
+               a._key_id == b._key_id &&
+               a._not_valid_before == b._not_valid_before &&
+               a._not_valid_after == b._not_valid_after &&
+               a._key == b._key
+               );
+}