Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / mxf.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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     libdcp 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 libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 /** @file  src/asset.cc
35  *  @brief Parent class for assets of DCPs made up of MXF files.
36  */
37
38 #include "raw_convert.h"
39 #include "mxf.h"
40 #include "util.h"
41 #include "metadata.h"
42 #include "exceptions.h"
43 #include "dcp_assert.h"
44 #include "compose.hpp"
45 #include <asdcp/AS_DCP.h>
46 #include <asdcp/KM_prng.h>
47 #include <asdcp/KM_util.h>
48 #include <libxml++/nodes/element.h>
49 #include <boost/filesystem.hpp>
50 #include <iostream>
51
52 using std::string;
53 using std::cout;
54 using std::list;
55 using std::pair;
56 using boost::shared_ptr;
57 using boost::dynamic_pointer_cast;
58 using namespace dcp;
59
60 MXF::MXF ()
61         : _context_id (make_uuid ())
62 {
63         /* Subclasses can create MXFs with unspecified _standard but are expected to fill
64            _standard in once the MXF is read.
65         */
66 }
67
68 MXF::MXF (Standard standard)
69         : _context_id (make_uuid ())
70         , _standard (standard)
71 {
72
73 }
74
75 void
76 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, string id) const
77 {
78         writer_info->ProductVersion = _metadata.product_version;
79         writer_info->CompanyName = _metadata.company_name;
80         writer_info->ProductName = _metadata.product_name.c_str();
81
82         DCP_ASSERT (_standard);
83         if (_standard == INTEROP) {
84                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
85         } else {
86                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
87         }
88         unsigned int c;
89         Kumu::hex2bin (id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
90         DCP_ASSERT (c == Kumu::UUID_Length);
91
92         writer_info->UsesHMAC = true;
93
94         if (_key_id) {
95                 Kumu::hex2bin (_context_id.c_str(), writer_info->ContextID, Kumu::UUID_Length, &c);
96                 writer_info->EncryptedEssence = true;
97
98                 unsigned int c;
99                 Kumu::hex2bin (_key_id.get().c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
100                 DCP_ASSERT (c == Kumu::UUID_Length);
101         }
102 }
103
104 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
105  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
106  *  via Key Delivery Messages (KDMs).
107  *  @param key Key to use.
108  */
109 void
110 MXF::set_key (Key key)
111 {
112         _key = key;
113
114         if (!_key_id) {
115                 /* No key ID so far; we now need one */
116                 _key_id = make_uuid ();
117         }
118 }
119
120 string
121 MXF::read_writer_info (ASDCP::WriterInfo const & info)
122 {
123         char buffer[64];
124
125         if (info.EncryptedEssence) {
126                 Kumu::bin2UUIDhex (info.CryptographicKeyID, ASDCP::UUIDlen, buffer, sizeof (buffer));
127                 _key_id = buffer;
128         }
129
130         switch (info.LabelSetType) {
131         case ASDCP::LS_MXF_INTEROP:
132                 _standard = INTEROP;
133                 break;
134         case ASDCP::LS_MXF_SMPTE:
135                 _standard = SMPTE;
136                 break;
137         default:
138                 throw DCPReadError ("Unrecognised label set type in MXF");
139         }
140
141         _metadata.read (info);
142
143         Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
144         return buffer;
145 }