Check that we can decrypt a DKDM when it is loaded (#1011).
[dcpomatic.git] / src / tools / dcpomatic_kdm.cc
index ee2e87161abb2377a70eeb11c827dd568b4369ff..9995d80a9819111056549285610c7740d44df378 100644 (file)
 #include "lib/compose.hpp"
 #include "lib/cinema.h"
 #include "lib/dkdm_wrapper.h"
+#include "lib/cross.h"
 #include <dcp/encrypted_kdm.h>
 #include <dcp/decrypted_kdm.h>
 #include <dcp/exceptions.h>
 #include <wx/wx.h>
 #include <wx/preferences.h>
+#include <wx/splash.h>
 #include <wx/filepicker.h>
 #ifdef __WXOSX__
 #include <ApplicationServices/ApplicationServices.h>
@@ -404,9 +406,20 @@ private:
                if (d->ShowModal() == wxID_OK) {
                        shared_ptr<DKDMBase> new_dkdm;
                        try {
-                               new_dkdm.reset (
-                                       new DKDM (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)))
-                                       );
+                               dcp::EncryptedKDM ekdm(dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE));
+
+                               /* Decrypt the DKDM to make sure that we can */
+                               shared_ptr<const dcp::CertificateChain> chain = Config::instance()->decryption_chain();
+                               DCPOMATIC_ASSERT (chain->key());
+                               dcp::DecryptedKDM dkdm(ekdm, chain->key().get());
+
+                               new_dkdm.reset(new DKDM(ekdm));
+                               shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
+                               if (!group) {
+                                       group = Config::instance()->dkdms ();
+                               }
+                               add_dkdm_model (new_dkdm, group);
+                               add_dkdm_view (new_dkdm);
                        } catch (dcp::KDMFormatError& e) {
                                error_dialog (
                                        this,
@@ -416,14 +429,12 @@ private:
                                                )
                                        );
                                return;
+                       } catch (dcp::KDMDecryptionError) {
+                               error_dialog (
+                                       this,
+                                       _("Could not decrypt the DKDM.  Perhaps it was not created with the correct certificate.")
+                                       );
                        }
-
-                       shared_ptr<DKDMGroup> group = dynamic_pointer_cast<DKDMGroup> (selected_dkdm ());
-                       if (!group) {
-                               group = Config::instance()->dkdms ();
-                       }
-                       add_dkdm_model (new_dkdm, group);
-                       add_dkdm_view (new_dkdm);
                }
                d->Destroy ();
        }
@@ -536,6 +547,11 @@ private:
        {
                wxInitAllImageHandlers ();
 
+               Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
+               Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
+
+               wxSplashScreen* splash = maybe_show_splash ();
+
                SetAppName (_("DCP-o-matic KDM Creator"));
 
                if (!wxApp::OnInit()) {
@@ -575,6 +591,9 @@ private:
                _frame = new DOMFrame (_("DCP-o-matic KDM Creator"));
                SetTopWindow (_frame);
                _frame->Maximize ();
+               if (splash) {
+                       splash->Destroy ();
+               }
                _frame->Show ();
 
                signal_manager = new wxSignalManager (this);
@@ -628,6 +647,16 @@ private:
                signal_manager->ui_idle ();
        }
 
+       void config_failed_to_load ()
+       {
+               message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
+       }
+
+       void config_warning (string m)
+       {
+               message_dialog (_frame, std_to_wx (m));
+       }
+
        DOMFrame* _frame;
 };