Assorted c++11 cleanups.
[libdcp.git] / src / certificate_chain.cc
index ebc3cc7ff215d5e2691a296f73fdaecc6e1dd1bd..9f4c5ea7fa1830b186572c54b70e24bac5b0cfb5 100644 (file)
@@ -54,7 +54,6 @@
 #include <openssl/rsa.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
 #include <fstream>
 #include <iostream>
 
@@ -186,6 +185,9 @@ CertificateChain::CertificateChain (
        string leaf_common_name
        )
 {
+       /* Valid for 40 years */
+       int const days = 365 * 40;
+
        boost::filesystem::path directory = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path ();
        boost::filesystem::create_directories (directory);
 
@@ -221,9 +223,9 @@ CertificateChain::CertificateChain (
        {
                command (
                        String::compose (
-                               "%1 req -new -x509 -sha256 -config ca.cnf -days 3650 -set_serial 5"
-                               " -subj \"%2\" -key ca.key -outform PEM -out ca.self-signed.pem",
-                               quoted_openssl, ca_subject
+                               "%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
                                )
                        );
        }
@@ -255,16 +257,18 @@ CertificateChain::CertificateChain (
        {
                command (
                        String::compose (
-                               "%1 req -new -config intermediate.cnf -days 3649 -subj \"%2\" -key intermediate.key -out intermediate.csr",
-                               quoted_openssl, inter_subject
+                               "%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");
@@ -294,16 +298,18 @@ CertificateChain::CertificateChain (
        {
                command (
                        String::compose (
-                               "%1 req -new -config leaf.cnf -days 3648 -subj \"%2\" -key leaf.key -outform PEM -out leaf.csr",
-                               quoted_openssl, leaf_subject
+                               "%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);
@@ -355,7 +361,7 @@ CertificateChain::List
 CertificateChain::leaf_to_root () const
 {
        List l = root_to_leaf ();
-       l.reverse ();
+       std::reverse (l.begin(), l.end());
        return l;
 }
 
@@ -380,7 +386,10 @@ CertificateChain::add (Certificate c)
 void
 CertificateChain::remove (Certificate c)
 {
-       _certificates.remove (c);
+       auto i = std::find(_certificates.begin(), _certificates.end(), c);
+       if (i != _certificates.end()) {
+               _certificates.erase (i);
+       }
 }
 
 /** Remove the i'th certificate in the list, as listed
@@ -464,10 +473,11 @@ CertificateChain::chain_valid (List const & chain) const
                        return false;
                }
 
-               /* I don't know why OpenSSL doesn't check this in verify_cert, but without this check
-                  the certificates_validation8 test fails.
+               /* 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()) {
+               if (j->issuer() != i->subject() || j->subject() == i->subject()) {
                        X509_STORE_free (store);
                        return false;
                }
@@ -499,6 +509,10 @@ CertificateChain::private_key_valid () const
        }
 
        RSA* private_key = PEM_read_bio_RSAPrivateKey (bio, 0, 0, 0);
+       if (!private_key) {
+               return false;
+       }
+
        RSA* public_key = leaf().public_key ();
 
 #if OPENSSL_VERSION_NUMBER > 0x10100000L
@@ -506,6 +520,9 @@ CertificateChain::private_key_valid () const
        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);
@@ -542,7 +559,7 @@ CertificateChain::List
 CertificateChain::root_to_leaf () const
 {
        List rtl = _certificates;
-       rtl.sort ();
+       std::sort (rtl.begin(), rtl.end());
        do {
                if (chain_valid (rtl)) {
                        return rtl;
@@ -552,33 +569,6 @@ CertificateChain::root_to_leaf () const
        throw CertificateChainError ("certificate chain is not consistent");
 }
 
-static string
-spaces (int n)
-{
-       string s = "";
-       for (int i = 0; i < n; ++i) {
-               s += " ";
-       }
-       return s;
-}
-
-static void
-indent (xmlpp::Element* element, int initial)
-{
-       xmlpp::Node* last = 0;
-       BOOST_FOREACH (xmlpp::Node * n, element->get_children()) {
-               xmlpp::Element* e = dynamic_cast<xmlpp::Element*>(n);
-               if (e) {
-                       element->add_child_text_before (e, "\n" + spaces(initial + 2));
-                       indent (e, initial + 2);
-                       last = n;
-               }
-       }
-       if (last) {
-               element->add_child_text (last, "\n" + spaces(initial));
-       }
-}
-
 /** Add a &lt;Signer&gt; and &lt;ds:Signature&gt; nodes to an XML node.
  *  @param parent XML node to add to.
  *  @param standard INTEROP or SMPTE.
@@ -610,7 +600,7 @@ CertificateChain::sign (xmlpp::Element* parent, Standard standard) const
        xmlpp::Element* 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 == INTEROP) {
+       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");
@@ -630,7 +620,7 @@ CertificateChain::sign (xmlpp::Element* parent, Standard standard) const
 
        signature->add_child("SignatureValue", "dsig");
        signature->add_child("KeyInfo", "dsig");
-       add_signature_value (signature, "dsig");
+       add_signature_value (signature, "dsig", true);
 }
 
 
@@ -640,17 +630,17 @@ CertificateChain::sign (xmlpp::Element* parent, Standard standard) const
  *  @param ns Namespace to use for the signature XML nodes.
  */
 void
-CertificateChain::add_signature_value (xmlpp::Element* parent, string ns) const
+CertificateChain::add_signature_value (xmlpp::Element* parent, string ns, bool add_indentation) const
 {
        cxml::Node cp (parent);
-       xmlpp::Node* key_info = cp.node_child("KeyInfo")->node ();
+       auto key_info = cp.node_child("KeyInfo")->node();
 
        /* Add the certificate chain to the KeyInfo child node of parent */
-       BOOST_FOREACH (Certificate const & i, leaf_to_root ()) {
-               xmlpp::Element* data = key_info->add_child("X509Data", ns);
+       for (auto const& i: leaf_to_root()) {
+               auto data = key_info->add_child("X509Data", ns);
 
                {
-                       xmlpp::Element* serial = data->add_child("X509IssuerSerial", 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 ());
                }
@@ -658,7 +648,7 @@ CertificateChain::add_signature_value (xmlpp::Element* parent, string ns) const
                data->add_child("X509Certificate", ns)->add_child_text (i.certificate());
        }
 
-       xmlSecDSigCtxPtr signature_context = xmlSecDSigCtxCreate (0);
+       auto signature_context = xmlSecDSigCtxCreate (0);
        if (signature_context == 0) {
                throw MiscError ("could not create signature context");
        }
@@ -671,7 +661,9 @@ CertificateChain::add_signature_value (xmlpp::Element* parent, string ns) const
                throw runtime_error ("could not read private key");
        }
 
-       indent (parent, 2);
+       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));
@@ -684,7 +676,7 @@ string
 CertificateChain::chain () const
 {
        string o;
-       BOOST_FOREACH (Certificate const &i, root_to_leaf ()) {
+       for (auto const& i: root_to_leaf()) {
                o += i.certificate(true);
        }