Partial tidy-up of KDM classes.
[libdcp.git] / src / mxf.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/asset.cc
21  *  @brief Parent class for assets of DCPs made up of MXF files.
22  */
23
24 #include "AS_DCP.h"
25 #include "KM_prng.h"
26 #include "KM_util.h"
27 #include "mxf.h"
28 #include "util.h"
29 #include "metadata.h"
30 #include "exceptions.h"
31 #include "kdm.h"
32 #include "compose.hpp"
33 #include <libxml++/nodes/element.h>
34 #include <boost/filesystem.hpp>
35 #include <iostream>
36
37 using std::string;
38 using std::list;
39 using std::pair;
40 using boost::shared_ptr;
41 using boost::lexical_cast;
42 using boost::dynamic_pointer_cast;
43 using namespace dcp;
44
45 MXF::MXF (Fraction edit_rate)
46         : Content (edit_rate)
47         , _encryption_context (0)
48         , _decryption_context (0)
49 {
50
51 }
52
53 MXF::MXF (boost::filesystem::path file)
54         : Content (file)
55         , _encryption_context (0)
56         , _decryption_context (0)
57 {
58
59 }
60
61 MXF::~MXF ()
62 {
63         delete _encryption_context;
64         delete _decryption_context;
65 }
66
67 void
68 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, Standard standard)
69 {
70         writer_info->ProductVersion = _metadata.product_version;
71         writer_info->CompanyName = _metadata.company_name;
72         writer_info->ProductName = _metadata.product_name.c_str();
73
74         if (standard == INTEROP) {
75                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
76         } else {
77                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
78         }
79         unsigned int c;
80         Kumu::hex2bin (_id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
81         assert (c == Kumu::UUID_Length);
82
83         if (_key) {
84                 Kumu::GenRandomUUID (writer_info->ContextID);
85                 writer_info->EncryptedEssence = true;
86
87                 unsigned int c;
88                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
89                 assert (c == Kumu::UUID_Length);
90         }
91 }
92
93 bool
94 MXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
95 {
96         if (!Content::equals (other, opt, note)) {
97                 return false;
98         }
99         
100         shared_ptr<const MXF> other_mxf = dynamic_pointer_cast<const MXF> (other);
101         if (!other_mxf) {
102                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
103                 return false;
104         }
105         
106         if (_file != other_mxf->file ()) {
107                 note (ERROR, "MXF names differ");
108                 if (!opt.mxf_names_can_differ) {
109                         return false;
110                 }
111         }
112         
113         return true;
114 }
115
116 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
117  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
118  *  via Key Delivery Messages (KDMs).
119  *  @param key Key to use.
120  */
121 void
122 MXF::set_key (Key key)
123 {
124         _key = key;
125
126         if (_key_id.empty ()) {
127                 /* No key ID so far; we now need one */
128                 _key_id = make_uuid ();
129         }
130         
131         _decryption_context = new ASDCP::AESDecContext;
132         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
133                 throw MiscError ("could not set up decryption context");
134         }
135
136         _encryption_context = new ASDCP::AESEncContext;
137         if (ASDCP_FAILURE (_encryption_context->InitKey (_key->value ()))) {
138                 throw MiscError ("could not set up encryption context");
139         }
140         
141         uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
142         
143         Kumu::FortunaRNG rng;
144         if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
145                 throw MiscError ("could not set up CBC initialization vector");
146         }
147 }
148
149 void
150 MXF::read_writer_info (ASDCP::WriterInfo const & info)
151 {
152         char buffer[64];
153         Kumu::bin2UUIDhex (info.AssetUUID, 16, buffer, 64);
154         _id = buffer;
155 }
156
157 string
158 MXF::pkl_type (Standard standard) const
159 {
160         switch (standard) {
161         case INTEROP:
162                 return String::compose ("application/x-smpte-mxf;asdcpKind=%1", asdcp_kind ());
163         case SMPTE:
164                 return "application/mxf";
165         default:
166                 assert (false);
167         }
168 }