Remake the certificate chain if any part of it is missing.
[dcpomatic.git] / src / lib / util.cc
index 4e4493ac785ba1edecb3bdd68a2df1f6c03af676..badbda4ab685c775f9633eb5ce4b7ebae661f9ad 100644 (file)
@@ -46,6 +46,8 @@
 #include <magick/version.h>
 #include <libdcp/version.h>
 #include <libdcp/util.h>
+#include <libdcp/signer_chain.h>
+#include <libdcp/signer.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -63,6 +65,7 @@ extern "C" {
 #include "config.h"
 #include "ratio.h"
 #include "job.h"
+#include "cross.h"
 #ifdef DCPOMATIC_WINDOWS
 #include "stack.hpp"
 #endif
@@ -275,6 +278,8 @@ dcpomatic_setup ()
 #endif 
        
        avfilter_register_all ();
+
+       libdcp::init ();
        
        Ratio::setup_ratios ();
        DCPContentType::setup_dcp_content_types ();
@@ -805,7 +810,7 @@ tidy_for_filename (string f)
 {
        string t;
        for (size_t i = 0; i < f.length(); ++i) {
-               if (isalpha (f[i]) || f[i] == '_' || f[i] == '-') {
+               if (isalnum (f[i]) || f[i] == '_' || f[i] == '-') {
                        t += f[i];
                } else {
                        t += '_';
@@ -814,4 +819,56 @@ tidy_for_filename (string f)
 
        return t;
 }
+
+shared_ptr<const libdcp::Signer>
+make_signer ()
+{
+       boost::filesystem::path const sd = Config::instance()->signer_chain_directory ();
+
+       /* Remake the chain if any of it is missing */
        
+       list<boost::filesystem::path> files;
+       files.push_back ("ca.self-signed.pem");
+       files.push_back ("intermediate.signed.pem");
+       files.push_back ("leaf.signed.pem");
+       files.push_back ("leaf.key");
+
+       list<boost::filesystem::path>::const_iterator i = files.begin();
+       while (i != files.end()) {
+               boost::filesystem::path p (sd);
+               sd /= *i;
+               if (!boost::filesystem::exists (sd)) {
+                       boost::filesystem::remove_all (sd);
+                       libdcp::make_signer_chain (sd, openssl_path ());
+                       break;
+               }
+
+               ++i;
+       }
+       
+       libdcp::CertificateChain chain;
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "ca.self-signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "intermediate.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       {
+               boost::filesystem::path p (sd);
+               p /= "leaf.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
+
+       boost::filesystem::path signer_key (sd);
+       signer_key /= "leaf.key";
+
+       return shared_ptr<const libdcp::Signer> (new libdcp::Signer (chain, signer_key));
+}
+