Clean up certificate download a bit and fix CAT745 and CP850 downloads.
[dcpomatic.git] / src / wx / dolby_doremi_certificate_panel.cc
1 /*
2     Copyright (C) 2014-2015 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 #include "dolby_doremi_certificate_panel.h"
21 #include "download_certificate_dialog.h"
22 #include "wx_util.h"
23 #include "lib/compose.hpp"
24 #include "lib/util.h"
25 #include "lib/signal_manager.h"
26 #include "lib/internet.h"
27 #include <dcp/raw_convert.h>
28 #include <curl/curl.h>
29 #include <zip.h>
30 #include <boost/foreach.hpp>
31 #include <iostream>
32
33 using std::string;
34 using std::cout;
35 using std::list;
36 using boost::function;
37 using boost::optional;
38
39 DolbyDoremiCertificatePanel::DolbyDoremiCertificatePanel (wxWindow* parent, DownloadCertificateDialog* dialog)
40         : DownloadCertificatePanel (parent, dialog)
41 {
42         add_label_to_sizer (_table, this, _("Serial number"), true);
43         _serial = new wxTextCtrl (this, wxID_ANY, wxT (""), wxDefaultPosition, wxSize (300, -1));
44         _table->Add (_serial, 1, wxEXPAND);
45
46         _serial->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&DownloadCertificateDialog::setup_sensitivity, _dialog));
47
48         layout ();
49 }
50
51 void
52 DolbyDoremiCertificatePanel::download (wxStaticText* message)
53 {
54         message->SetLabel (_("Downloading certificate"));
55
56         /* Hack: without this the SetLabel() above has no visible effect */
57         wxMilliSleep (200);
58
59         signal_manager->when_idle (boost::bind (&DolbyDoremiCertificatePanel::finish_download, this, wx_to_std (_serial->GetValue ()), message));
60 }
61
62 static void
63 try_dcp2000 (list<string>& urls, list<string>& files, string prefix, string serial)
64 {
65         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
66         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
67
68         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
69         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
70
71         urls.push_back (String::compose ("%1%2xxx/dcp2000-%3.certs.zip", prefix, serial.substr(0, 3), serial));
72         files.push_back (String::compose ("dcp2000-%1.cert.sha256.pem", serial));
73 }
74
75 static void
76 try_imb (list<string>& urls, list<string>& files, string prefix, string serial)
77 {
78         urls.push_back (String::compose ("%1%2xxx/imb-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
79         files.push_back (String::compose ("imb-%1.cert.sha256.pem", serial));
80 }
81
82 static void
83 try_ims (list<string>& urls, list<string>& files, string prefix, string serial)
84 {
85         urls.push_back (String::compose ("%1%2xxx/ims-%3.dcicerts.zip", prefix, serial.substr(0, 3), serial));
86         files.push_back (String::compose ("ims-%1.cert.sha256.pem", serial));
87 }
88
89 static void
90 try_cat862 (list<string>& urls, list<string>& files, string prefix, string serial)
91 {
92         int const serial_int = dcp::raw_convert<int> (serial);
93
94         string cat862;
95         if (serial_int <= 510999) {
96                 cat862 = "CAT862_510999_and_lower";
97         } else if (serial_int >= 617000) {
98                 cat862 = "CAT862_617000_and_higher";
99         } else {
100                 int const lower = serial_int - (serial_int % 1000);
101                 cat862 = String::compose ("CAT862_%1-%2", lower, lower + 999);
102         }
103
104         urls.push_back (String::compose ("%1%2/cert_Dolby256-CAT862-%3.zip", prefix, cat862, serial_int));
105         files.push_back (String::compose ("cert_Dolby256-CAT862-%1.pem.crt", serial_int));
106 }
107
108 static void
109 try_dsp100 (list<string>& urls, list<string>& files, string prefix, string serial)
110 {
111         int const serial_int = dcp::raw_convert<int> (serial);
112
113         string dsp100;
114         if (serial_int <= 999) {
115                 dsp100 = "DSP100_053_thru_999";
116         } else if (serial_int >= 3000) {
117                 dsp100 = "DSP100_3000_and_higher";
118         } else {
119                 int const lower = serial_int - (serial_int % 1000);
120                 dsp100 = String::compose ("DSP100_%1_thru_%2", lower, lower + 999);
121         }
122
123         urls.push_back (String::compose ("%1%2/cert_Dolby256-DSP100-%3.zip", prefix, dsp100, serial_int));
124         files.push_back (String::compose ("cert_Dolby256-DSP100-%1.pem.crt", serial_int));
125 }
126
127 static void
128 try_cat745 (list<string>& urls, list<string>& files, string prefix, string serial)
129 {
130         int const serial_int = dcp::raw_convert<int> (serial.substr (1));
131
132         string cat745;
133         if (serial_int <= 999) {
134                 cat745 = "CAT745_1_thru_999";
135         } else if (serial_int >= 6000) {
136                 cat745 = "CAT745_6000_and_higher";
137         } else {
138                 int const lower = serial_int - (serial_int % 1000);
139                 cat745 = String::compose ("CAT745_%1_thru_%2", lower, lower + 999);
140         }
141
142         urls.push_back (String::compose ("%1%2/cert_Dolby-CAT745-%3.zip", prefix, cat745, serial_int));
143         files.push_back (String::compose ("cert_Dolby-CAT745-%1.pem.crt", serial_int));
144 }
145
146 static void
147 try_cp850 (list<string>& urls, list<string>& files, string prefix, string serial)
148 {
149         int const serial_int = dcp::raw_convert<int> (serial.substr (1));
150
151         int const lower = serial_int - (serial_int % 1000);
152         urls.push_back (String::compose ("%1CP850_CAT1600_F%2-F%3/cert_RMB_SPB_MDE_FMA.Dolby-CP850-F%4.zip", prefix, lower, lower + 999, serial_int));
153         files.push_back (String::compose ("cert_RMB_SPB_MDE_FMA.Dolby-CP850-F%1.pem.crt", serial_int));
154 }
155
156 void
157 DolbyDoremiCertificatePanel::finish_download (string serial, wxStaticText* message)
158 {
159         /* Try dcp2000, imb and ims prefixes (see mantis #375) */
160
161         string const prefix = "ftp://anonymous@ftp.cinema.dolby.com/Certificates/";
162         list<string> urls;
163         list<string> files;
164
165         bool starts_with_digit = false;
166         optional<char> starting_char;
167
168         if (!serial.empty()) {
169                 if (isdigit (serial[0])) {
170                         starts_with_digit = true;
171                 } else {
172                         starting_char = serial[0];
173                 }
174         }
175
176         if (starts_with_digit) {
177                 try_dcp2000 (urls, files, prefix, serial);
178                 try_imb (urls, files, prefix, serial);
179                 try_ims (urls, files, prefix, serial);
180                 try_cat862 (urls, files, prefix, serial);
181                 try_dsp100 (urls, files, prefix, serial);
182         } else if (starting_char == 'H') {
183                 try_cat745 (urls, files, prefix, serial);
184         } else if (starting_char == 'F') {
185                 try_cp850 (urls, files, prefix, serial);
186         }
187
188         list<string> errors;
189         bool ok = false;
190         list<string>::const_iterator i = urls.begin ();
191         list<string>::const_iterator j = files.begin ();
192         while (!ok && i != urls.end ()) {
193                 optional<string> error = get_from_zip_url (*i++, *j++, true, boost::bind (&DownloadCertificatePanel::load, this, _1));
194                 if (error) {
195                         errors.push_back (error.get ());
196                 } else {
197                         ok = true;
198                 }
199         }
200
201         if (ok) {
202                 message->SetLabel (_("Certificate downloaded"));
203                 _dialog->setup_sensitivity ();
204         } else {
205                 message->SetLabel (wxT (""));
206
207                 SafeStringStream s;
208                 BOOST_FOREACH (string e, errors) {
209                         s << e << "\n";
210                 }
211
212                 error_dialog (this, std_to_wx (s.str ()));
213         }
214 }
215
216 bool
217 DolbyDoremiCertificatePanel::ready_to_download () const
218 {
219         return !_serial->IsEmpty ();
220 }