Tidying a few things up.
[libdcp.git] / src / certificate_chain.cc
index 4f92d8d9983ccd7e7a5d19cfdf33f9028dc9b324..4f86ba0db6275a3db0e0b38b03c06dc0d0dde9fa 100644 (file)
@@ -1,42 +1,71 @@
 /*
-    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
 
-    This program is free software; you can redistribute it and/or modify
+    This file is part of libdcp.
+
+    libdcp is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
 
-    This program is distributed in the hope that it will be useful,
+    libdcp is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
+    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
+
+    In addition, as a special exception, the copyright holders give
+    permission to link the code of portions of this program with the
+    OpenSSL library under certain conditions as described in each
+    individual source file, and distribute linked combinations
+    including the two.
+
+    You must obey the GNU General Public License in all respects
+    for all of the code used other than OpenSSL.  If you modify
+    file(s) with this exception, you may extend this exception to your
+    version of the file(s), but you are not obligated to do so.  If you
+    do not wish to do so, delete this exception statement from your
+    version.  If you delete this exception statement from all source
+    files in the program, then also delete it here.
 */
 
-/** @file  src/signer_chain.cc
- *  @brief Functions to make signer chains.
+
+/** @file  src/certificate_chain.cc
+ *  @brief CertificateChain class
  */
 
+
 #include "certificate_chain.h"
 #include "exceptions.h"
 #include "util.h"
-#include "KM_util.h"
+#include "dcp_assert.h"
+#include "compose.hpp"
+#include <asdcp/KM_util.h>
+#include <libcxml/cxml.h>
+#include <libxml++/libxml++.h>
+#include <xmlsec/xmldsig.h>
+#include <xmlsec/dl.h>
+#include <xmlsec/app.h>
+#include <xmlsec/crypto.h>
 #include <openssl/sha.h>
 #include <openssl/bio.h>
 #include <openssl/evp.h>
+#include <openssl/pem.h>
+#include <openssl/rsa.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
 #include <fstream>
-#include <sstream>
+#include <iostream>
+
 
 using std::string;
 using std::ofstream;
 using std::ifstream;
-using std::stringstream;
+using std::runtime_error;
+using namespace dcp;
+
 
 /** Run a shell command.
  *  @param cmd Command to run (UTF8-encoded).
@@ -49,7 +78,7 @@ command (string cmd)
           is handled correctly.
        */
        int const wn = MultiByteToWideChar (CP_UTF8, 0, cmd.c_str(), -1, 0, 0);
-       wchar_t* buffer = new wchar_t[wn];
+       auto buffer = new wchar_t[wn];
        if (MultiByteToWideChar (CP_UTF8, 0, cmd.c_str(), -1, buffer, wn) == 0) {
                delete[] buffer;
                return;
@@ -82,12 +111,11 @@ command (string cmd)
        int const code = WEXITSTATUS (r);
 #endif
        if (code) {
-               stringstream s;
-               s << "error " << code << " in " << cmd << " within " << boost::filesystem::current_path();
-               throw dcp::MiscError (s.str());
+               throw dcp::MiscError (String::compose ("error %1 in %2 within %3", code, cmd, boost::filesystem::current_path().string()));
        }
 }
 
+
 /** Extract a public key from a private key and create a SHA1 digest of it.
  *  @param private_key Private key
  *  @param openssl openssl binary name (or full path if openssl is not on the system path).
@@ -99,14 +127,12 @@ public_key_digest (boost::filesystem::path private_key, boost::filesystem::path
        boost::filesystem::path public_name = private_key.string() + ".public";
 
        /* Create the public key from the private key */
-       stringstream s;
-       s << "\"" << openssl.string() << "\" rsa -outform PEM -pubout -in " << private_key.string() << " -out " << public_name.string ();
-       command (s.str().c_str ());
+       command (String::compose("\"%1\" rsa -outform PEM -pubout -in %2 -out %3", openssl.string(), private_key.string(), public_name.string()));
 
        /* Read in the public key from the file */
 
        string pub;
-       ifstream f (public_name.string().c_str ());
+       ifstream f (public_name.string().c_str());
        if (!f.good ()) {
                throw dcp::MiscError ("public key not found");
        }
@@ -125,7 +151,7 @@ public_key_digest (boost::filesystem::path private_key, boost::filesystem::path
        }
 
        /* Decode the base64 of the public key */
-               
+
        unsigned char buffer[512];
        int const N = dcp::base64_decode (pub, buffer, 1024);
 
@@ -149,14 +175,14 @@ public_key_digest (boost::filesystem::path private_key, boost::filesystem::path
        string dig = Kumu::base64encode (digest, SHA_DIGEST_LENGTH, digest_base64, 64);
 #ifdef LIBDCP_WINDOWS
        boost::replace_all (dig, "/", "\\/");
-#else  
+#else
        boost::replace_all (dig, "/", "\\\\/");
-#endif 
+#endif
        return dig;
 }
 
-boost::filesystem::path
-dcp::make_certificate_chain (
+
+CertificateChain::CertificateChain (
        boost::filesystem::path openssl,
        string organisation,
        string organisational_unit,
@@ -165,10 +191,13 @@ dcp::make_certificate_chain (
        string leaf_common_name
        )
 {
-       boost::filesystem::path directory = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path ();
+       /* Valid for 40 years */
+       int const days = 365 * 40;
+
+       auto directory = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path ();
        boost::filesystem::create_directories (directory);
-       
-       boost::filesystem::path const cwd = boost::filesystem::current_path ();
+
+       auto const cwd = boost::filesystem::current_path ();
        boost::filesystem::current_path (directory);
 
        string quoted_openssl = "\"" + openssl.string() + "\"";
@@ -180,6 +209,7 @@ dcp::make_certificate_chain (
                f << "[ req ]\n"
                  << "distinguished_name = req_distinguished_name\n"
                  << "x509_extensions   = v3_ca\n"
+                 << "string_mask = nombstr\n"
                  << "[ v3_ca ]\n"
                  << "basicConstraints = critical,CA:true,pathlen:3\n"
                  << "keyUsage = keyCertSign,cRLSign\n"
@@ -197,11 +227,13 @@ dcp::make_certificate_chain (
                "/dnQualifier=" + public_key_digest ("ca.key", openssl);
 
        {
-               stringstream c;
-               c << quoted_openssl
-                 << " req -new -x509 -sha256 -config ca.cnf -days 3650 -set_serial 5"
-                 << " -subj \"" << ca_subject << "\" -key ca.key -outform PEM -out ca.self-signed.pem";
-               command (c.str().c_str());
+               command (
+                       String::compose (
+                               "%1 req -new -x509 -sha256 -config ca.cnf -days %2 -set_serial 5"
+                               " -subj \"%3\" -key ca.key -outform PEM -out ca.self-signed.pem",
+                               quoted_openssl, days, ca_subject
+                               )
+                       );
        }
 
        command (quoted_openssl + " genrsa -out intermediate.key 2048");
@@ -211,6 +243,7 @@ dcp::make_certificate_chain (
                f << "[ default ]\n"
                  << "distinguished_name = req_distinguished_name\n"
                  << "x509_extensions = v3_ca\n"
+                 << "string_mask = nombstr\n"
                  << "[ v3_ca ]\n"
                  << "basicConstraints = critical,CA:true,pathlen:2\n"
                  << "keyUsage = keyCertSign,cRLSign\n"
@@ -221,24 +254,27 @@ dcp::make_certificate_chain (
                  << "OU = Organization unit\n"
                  << "CN = Entity and dnQualifier\n";
        }
-               
+
        string const inter_subject = "/O=" + organisation +
                "/OU=" + organisational_unit +
                "/CN=" + intermediate_common_name +
                "/dnQualifier=" + public_key_digest ("intermediate.key", openssl);
 
        {
-               stringstream s;
-               s << quoted_openssl
-                 << " req -new -config intermediate.cnf -days 3649 -subj \"" << inter_subject << "\" -key intermediate.key -out intermediate.csr";
-               command (s.str().c_str());
+               command (
+                       String::compose (
+                               "%1 req -new -config intermediate.cnf -days %2 -subj \"%3\" -key intermediate.key -out intermediate.csr",
+                               quoted_openssl, days - 1, inter_subject
+                               )
+                       );
        }
 
-       
        command (
-               quoted_openssl +
-               " x509 -req -sha256 -days 3649 -CA ca.self-signed.pem -CAkey ca.key -set_serial 6"
-               " -in intermediate.csr -extfile intermediate.cnf -extensions v3_ca -out intermediate.signed.pem"
+               String::compose (
+                       "%1 x509 -req -sha256 -days %2 -CA ca.self-signed.pem -CAkey ca.key -set_serial 6"
+                       " -in intermediate.csr -extfile intermediate.cnf -extensions v3_ca -out intermediate.signed.pem",
+                       quoted_openssl, days - 1
+                       )
                );
 
        command (quoted_openssl + " genrsa -out leaf.key 2048");
@@ -248,6 +284,7 @@ dcp::make_certificate_chain (
                f << "[ default ]\n"
                  << "distinguished_name = req_distinguished_name\n"
                  << "x509_extensions   = v3_ca\n"
+                 << "string_mask = nombstr\n"
                  << "[ v3_ca ]\n"
                  << "basicConstraints = critical,CA:false\n"
                  << "keyUsage = digitalSignature,keyEncipherment\n"
@@ -265,18 +302,374 @@ dcp::make_certificate_chain (
                "/dnQualifier=" + public_key_digest ("leaf.key", openssl);
 
        {
-               stringstream s;
-               s << quoted_openssl << " req -new -config leaf.cnf -days 3648 -subj \"" << leaf_subject << "\" -key leaf.key -outform PEM -out leaf.csr";
-               command (s.str().c_str());
+               command (
+                       String::compose (
+                               "%1 req -new -config leaf.cnf -days %2 -subj \"%3\" -key leaf.key -outform PEM -out leaf.csr",
+                               quoted_openssl, days - 2, leaf_subject
+                               )
+                       );
        }
 
        command (
-               quoted_openssl +
-               " x509 -req -sha256 -days 3648 -CA intermediate.signed.pem -CAkey intermediate.key"
-               " -set_serial 7 -in leaf.csr -extfile leaf.cnf -extensions v3_ca -out leaf.signed.pem"
+               String::compose (
+                       "%1 x509 -req -sha256 -days %2 -CA intermediate.signed.pem -CAkey intermediate.key"
+                       " -set_serial 7 -in leaf.csr -extfile leaf.cnf -extensions v3_ca -out leaf.signed.pem",
+                       quoted_openssl, days - 2
+                       )
                );
 
        boost::filesystem::current_path (cwd);
 
-       return directory;
+       _certificates.push_back (dcp::Certificate(dcp::file_to_string(directory / "ca.self-signed.pem")));
+       _certificates.push_back (dcp::Certificate(dcp::file_to_string(directory / "intermediate.signed.pem")));
+       _certificates.push_back (dcp::Certificate(dcp::file_to_string(directory / "leaf.signed.pem")));
+
+       _key = dcp::file_to_string (directory / "leaf.key");
+
+       boost::filesystem::remove_all (directory);
+}
+
+
+CertificateChain::CertificateChain (string s)
+{
+       while (true) {
+               try {
+                       Certificate c;
+                       s = c.read_string (s);
+                       _certificates.push_back (c);
+               } catch (MiscError& e) {
+                       /* Failed to read a certificate, just stop */
+                       break;
+               }
+       }
+
+       /* This will throw an exception if the chain cannot be ordered */
+       leaf_to_root ();
+}
+
+
+Certificate
+CertificateChain::root () const
+{
+       DCP_ASSERT (!_certificates.empty());
+       return root_to_leaf().front();
+}
+
+
+Certificate
+CertificateChain::leaf () const
+{
+       DCP_ASSERT (!_certificates.empty());
+       return root_to_leaf().back();
+}
+
+
+CertificateChain::List
+CertificateChain::leaf_to_root () const
+{
+       auto l = root_to_leaf ();
+       std::reverse (l.begin(), l.end());
+       return l;
+}
+
+
+CertificateChain::List
+CertificateChain::unordered () const
+{
+       return _certificates;
+}
+
+
+void
+CertificateChain::add (Certificate c)
+{
+       _certificates.push_back (c);
+}
+
+
+void
+CertificateChain::remove (Certificate c)
+{
+       auto i = std::find(_certificates.begin(), _certificates.end(), c);
+       if (i != _certificates.end()) {
+               _certificates.erase (i);
+       }
+}
+
+
+void
+CertificateChain::remove (int i)
+{
+       auto j = _certificates.begin ();
+        while (j != _certificates.end () && i > 0) {
+               --i;
+               ++j;
+       }
+
+       if (j != _certificates.end ()) {
+               _certificates.erase (j);
+       }
+}
+
+
+bool
+CertificateChain::chain_valid () const
+{
+       return chain_valid (_certificates);
+}
+
+
+bool
+CertificateChain::chain_valid (List const & chain) const
+{
+        /* Here I am taking a chain of certificates A/B/C/D and checking validity of B wrt A,
+          C wrt B and D wrt C.  It also appears necessary to check the issuer of B/C/D matches
+          the subject of A/B/C; I don't understand why.  I'm sure there's a better way of doing
+          this with OpenSSL but the documentation does not appear not likely to reveal it
+          any time soon.
+       */
+
+       auto store = X509_STORE_new ();
+       if (!store) {
+               throw MiscError ("could not create X509 store");
+       }
+
+       /* Put all the certificates into the store */
+       for (auto const& i: chain) {
+               if (!X509_STORE_add_cert(store, i.x509())) {
+                       X509_STORE_free(store);
+                       return false;
+               }
+       }
+
+       /* Verify each one */
+       for (auto i = chain.begin(); i != chain.end(); ++i) {
+
+               auto j = i;
+               ++j;
+               if (j == chain.end ()) {
+                       break;
+               }
+
+               auto ctx = X509_STORE_CTX_new ();
+               if (!ctx) {
+                       X509_STORE_free (store);
+                       throw MiscError ("could not create X509 store context");
+               }
+
+               X509_STORE_set_flags (store, 0);
+               if (!X509_STORE_CTX_init (ctx, store, j->x509(), 0)) {
+                       X509_STORE_CTX_free (ctx);
+                       X509_STORE_free (store);
+                       throw MiscError ("could not initialise X509 store context");
+               }
+
+               int const v = X509_verify_cert (ctx);
+               X509_STORE_CTX_free (ctx);
+
+               if (v != 1) {
+                       X509_STORE_free (store);
+                       return false;
+               }
+
+               /* I don't know why OpenSSL doesn't check this stuff
+                  in verify_cert, but without these checks the
+                  certificates_validation8 test fails.
+               */
+               if (j->issuer() != i->subject() || j->subject() == i->subject()) {
+                       X509_STORE_free (store);
+                       return false;
+               }
+
+       }
+
+       X509_STORE_free (store);
+
+       return true;
+}
+
+
+bool
+CertificateChain::private_key_valid () const
+{
+       if (_certificates.empty ()) {
+               return true;
+       }
+
+       if (!_key) {
+               return false;
+       }
+
+       auto bio = BIO_new_mem_buf (const_cast<char *> (_key->c_str ()), -1);
+       if (!bio) {
+               throw MiscError ("could not create memory BIO");
+       }
+
+       auto private_key = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
+       if (!private_key) {
+               return false;
+       }
+
+       auto public_key = leaf().public_key ();
+
+#if OPENSSL_VERSION_NUMBER > 0x10100000L
+       BIGNUM const * private_key_n;
+       RSA_get0_key(private_key, &private_key_n, 0, 0);
+       BIGNUM const * public_key_n;
+       RSA_get0_key(public_key, &public_key_n, 0, 0);
+       if (!private_key_n || !public_key_n) {
+               return false;
+       }
+       bool const valid = !BN_cmp (private_key_n, public_key_n);
+#else
+       bool const valid = !BN_cmp (private_key->n, public_key->n);
+#endif
+       BIO_free (bio);
+
+       return valid;
+}
+
+
+bool
+CertificateChain::valid (string* reason) const
+{
+       try {
+               root_to_leaf ();
+       } catch (CertificateChainError& e) {
+               if (reason) {
+                       *reason = "certificates do not form a chain";
+               }
+               return false;
+       }
+
+       if (!private_key_valid ()) {
+               if (reason) {
+                       *reason = "private key does not exist, or does not match leaf certificate";
+               }
+               return false;
+       }
+
+       return true;
+}
+
+
+CertificateChain::List
+CertificateChain::root_to_leaf () const
+{
+       auto rtl = _certificates;
+       std::sort (rtl.begin(), rtl.end());
+       do {
+               if (chain_valid (rtl)) {
+                       return rtl;
+               }
+       } while (std::next_permutation (rtl.begin(), rtl.end()));
+
+       throw CertificateChainError ("certificate chain is not consistent");
+}
+
+
+void
+CertificateChain::sign (xmlpp::Element* parent, Standard standard) const
+{
+       /* <Signer> */
+
+       parent->add_child_text("  ");
+       auto signer = parent->add_child("Signer");
+       signer->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
+       auto data = signer->add_child("X509Data", "dsig");
+       auto serial_element = data->add_child("X509IssuerSerial", "dsig");
+       serial_element->add_child("X509IssuerName", "dsig")->add_child_text (leaf().issuer());
+       serial_element->add_child("X509SerialNumber", "dsig")->add_child_text (leaf().serial());
+       data->add_child("X509SubjectName", "dsig")->add_child_text (leaf().subject());
+
+       indent (signer, 2);
+
+       /* <Signature> */
+
+       parent->add_child_text("\n  ");
+       auto signature = parent->add_child("Signature");
+       signature->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
+       signature->set_namespace ("dsig");
+       parent->add_child_text("\n");
+
+       auto signed_info = signature->add_child ("SignedInfo", "dsig");
+       signed_info->add_child("CanonicalizationMethod", "dsig")->set_attribute ("Algorithm", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315");
+
+       if (standard == Standard::INTEROP) {
+               signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#rsa-sha1");
+       } else {
+               signed_info->add_child("SignatureMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
+       }
+
+       auto reference = signed_info->add_child("Reference", "dsig");
+       reference->set_attribute ("URI", "");
+
+       auto transforms = reference->add_child("Transforms", "dsig");
+       transforms->add_child("Transform", "dsig")->set_attribute (
+               "Algorithm", "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
+               );
+
+       reference->add_child("DigestMethod", "dsig")->set_attribute("Algorithm", "http://www.w3.org/2000/09/xmldsig#sha1");
+       /* This will be filled in by the signing later */
+       reference->add_child("DigestValue", "dsig");
+
+       signature->add_child("SignatureValue", "dsig");
+       signature->add_child("KeyInfo", "dsig");
+       add_signature_value (signature, "dsig", true);
+}
+
+
+void
+CertificateChain::add_signature_value (xmlpp::Element* parent, string ns, bool add_indentation) const
+{
+       cxml::Node cp (parent);
+       auto key_info = cp.node_child("KeyInfo")->node();
+
+       /* Add the certificate chain to the KeyInfo child node of parent */
+       for (auto const& i: leaf_to_root()) {
+               auto data = key_info->add_child("X509Data", ns);
+
+               {
+                       auto serial = data->add_child("X509IssuerSerial", ns);
+                       serial->add_child("X509IssuerName", ns)->add_child_text (i.issuer ());
+                       serial->add_child("X509SerialNumber", ns)->add_child_text (i.serial ());
+               }
+
+               data->add_child("X509Certificate", ns)->add_child_text (i.certificate());
+       }
+
+       auto signature_context = xmlSecDSigCtxCreate (0);
+       if (signature_context == 0) {
+               throw MiscError ("could not create signature context");
+       }
+
+       signature_context->signKey = xmlSecCryptoAppKeyLoadMemory (
+               reinterpret_cast<const unsigned char *> (_key->c_str()), _key->size(), xmlSecKeyDataFormatPem, 0, 0, 0
+               );
+
+       if (signature_context->signKey == 0) {
+               throw runtime_error ("could not read private key");
+       }
+
+       if (add_indentation) {
+               indent (parent, 2);
+       }
+       int const r = xmlSecDSigCtxSign (signature_context, parent->cobj ());
+       if (r < 0) {
+               throw MiscError (String::compose ("could not sign (%1)", r));
+       }
+
+       xmlSecDSigCtxDestroy (signature_context);
+}
+
+
+string
+CertificateChain::chain () const
+{
+       string o;
+       for (auto const& i: root_to_leaf()) {
+               o += i.certificate(true);
+       }
+
+       return o;
 }