No-op; Fix GPL address and mention libdcp by name.
[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 */
20
21 /** @file  src/asset.cc
22  *  @brief Parent class for assets of DCPs made up of MXF files.
23  */
24
25 #include "raw_convert.h"
26 #include "AS_DCP.h"
27 #include "KM_prng.h"
28 #include "KM_util.h"
29 #include "mxf.h"
30 #include "util.h"
31 #include "metadata.h"
32 #include "exceptions.h"
33 #include "dcp_assert.h"
34 #include "compose.hpp"
35 #include <libxml++/nodes/element.h>
36 #include <boost/filesystem.hpp>
37 #include <iostream>
38
39 using std::string;
40 using std::cout;
41 using std::list;
42 using std::pair;
43 using boost::shared_ptr;
44 using boost::dynamic_pointer_cast;
45 using namespace dcp;
46
47 MXF::MXF ()
48         : _decryption_context (0)
49 {
50
51 }
52
53 MXF::~MXF ()
54 {
55         delete _decryption_context;
56 }
57
58 void
59 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, string id, Standard standard) const
60 {
61         writer_info->ProductVersion = _metadata.product_version;
62         writer_info->CompanyName = _metadata.company_name;
63         writer_info->ProductName = _metadata.product_name.c_str();
64
65         if (standard == INTEROP) {
66                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
67         } else {
68                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
69         }
70         unsigned int c;
71         Kumu::hex2bin (id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
72         DCP_ASSERT (c == Kumu::UUID_Length);
73
74         writer_info->UsesHMAC = true;
75
76         if (_key_id) {
77                 Kumu::GenRandomUUID (writer_info->ContextID);
78                 writer_info->EncryptedEssence = true;
79
80                 unsigned int c;
81                 Kumu::hex2bin (_key_id.get().c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
82                 DCP_ASSERT (c == Kumu::UUID_Length);
83         }
84 }
85
86 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
87  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
88  *  via Key Delivery Messages (KDMs).
89  *  @param key Key to use.
90  */
91 void
92 MXF::set_key (Key key)
93 {
94         _key = key;
95
96         if (!_key_id) {
97                 /* No key ID so far; we now need one */
98                 _key_id = make_uuid ();
99         }
100
101         _decryption_context = new ASDCP::AESDecContext;
102         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
103                 throw MiscError ("could not set up decryption context");
104         }
105 }
106
107 string
108 MXF::read_writer_info (ASDCP::WriterInfo const & info)
109 {
110         char buffer[64];
111
112         if (info.EncryptedEssence) {
113                 Kumu::bin2UUIDhex (info.CryptographicKeyID, ASDCP::UUIDlen, buffer, sizeof (buffer));
114                 _key_id = buffer;
115         }
116
117         _metadata.read (info);
118
119         Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
120         return buffer;
121 }