2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <curl/curl.h>
22 #include "lib/compose.hpp"
24 #include "doremi_certificate_dialog.h"
28 using boost::function;
30 DoremiCertificateDialog::DoremiCertificateDialog (wxWindow* parent, function<void (boost::filesystem::path)> load)
31 : DownloadCertificateDialog (parent, load)
33 add (_("Server serial number"), true);
34 _serial = add (new wxTextCtrl (this, wxID_ANY));
36 add_common_widgets ();
42 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
44 FILE* f = reinterpret_cast<FILE*> (stream);
45 return fwrite (buffer, size, nmemb, f);
49 DoremiCertificateDialog::download ()
51 string const serial = wx_to_std (_serial->GetValue ());
52 if (serial.length() != 6) {
53 _message->SetLabel (_("Doremi serial numbers must have 6 digits"));
57 CURL* curl = curl_easy_init ();
59 _message->SetLabel (N_("Could not set up libcurl"));
63 string const url = String::compose (
64 "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
65 serial.substr(0, 3), serial
68 curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
70 ScopedTemporary temp_zip;
71 FILE* f = temp_zip.open ("wb");
72 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data);
73 curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
75 _message->SetLabel (_("Downloading certificate from Doremi"));
78 CURLcode const cr = curl_easy_perform (curl);
80 _gauge->SetValue (50);
84 curl_easy_cleanup (curl);
86 _message->SetLabel (wxString::Format (_("Certificate download failed (%d)"), cr));
90 _message->SetLabel (_("Unpacking"));
93 struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
95 _message->SetLabel (N_("Could not open certificate ZIP file"));
99 string const name_in_zip = String::compose ("dcp2000-%1.cert.sha256.pem", serial);
100 struct zip_file* zip_file = zip_fopen (zip, name_in_zip.c_str(), 0);
102 _message->SetLabel (N_("Could not find certificate in ZIP file"));
106 ScopedTemporary temp_cert;
107 f = temp_cert.open ("wb");
110 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
111 fwrite (buffer, 1, N, f);
112 if (N < int (sizeof (buffer))) {
118 _gauge->SetValue (100);
119 _message->SetLabel (_("OK"));
120 _load (temp_cert.file ());