Move CertificateChain into the right header.
authorCarl Hetherington <cth@carlh.net>
Sun, 12 Jul 2015 19:54:06 +0000 (20:54 +0100)
committerCarl Hetherington <cth@carlh.net>
Sun, 12 Jul 2015 19:54:06 +0000 (20:54 +0100)
src/certificate_chain.cc
src/certificate_chain.h
src/certificates.cc
src/certificates.h
src/signer.h

index 22754f4e339403c0228a0ccc736633941b5a1117..e7de5bd8a2fe39fd7abae5c8411007dd22ebd553 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2013-2014 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@
 #include "certificate_chain.h"
 #include "exceptions.h"
 #include "util.h"
+#include "dcp_assert.h"
 #include "KM_util.h"
 #include <openssl/sha.h>
 #include <openssl/bio.h>
@@ -37,6 +38,7 @@ using std::string;
 using std::ofstream;
 using std::ifstream;
 using std::stringstream;
+using namespace dcp;
 
 /** Run a shell command.
  *  @param cmd Command to run (UTF8-encoded).
@@ -280,3 +282,139 @@ dcp::make_certificate_chain (
 
        return directory;
 }
+
+/** @return Root certificate */
+Certificate
+CertificateChain::root () const
+{
+       DCP_ASSERT (!_certificates.empty());
+       return _certificates.front ();
+}
+
+/** @return Leaf certificate */
+Certificate
+CertificateChain::leaf () const
+{
+       DCP_ASSERT (_certificates.size() >= 2);
+       return _certificates.back ();
+}
+
+/** @return Certificates in order from root to leaf */
+CertificateChain::List
+CertificateChain::root_to_leaf () const
+{
+       return _certificates;
+}
+
+/** @return Certificates in order from leaf to root */
+CertificateChain::List
+CertificateChain::leaf_to_root () const
+{
+       List c = _certificates;
+       c.reverse ();
+       return c;
+}
+
+/** Add a certificate to the end of the chain.
+ *  @param c Certificate to add.
+ */
+void
+CertificateChain::add (Certificate c)
+{
+       _certificates.push_back (c);
+}
+
+/** Remove a certificate from the chain.
+ *  @param c Certificate to remove.
+ */
+void
+CertificateChain::remove (Certificate c)
+{
+       _certificates.remove (c);
+}
+
+/** Remove the i'th certificate in the list, as listed
+ *  from root to leaf.
+ */
+void
+CertificateChain::remove (int i)
+{
+       List::iterator j = _certificates.begin ();
+        while (j != _certificates.end () && i > 0) {
+               --i;
+               ++j;
+       }
+
+       if (j != _certificates.end ()) {
+               _certificates.erase (j);
+       }
+}
+
+/** Check to see if the chain is valid (i.e. root signs the intermediate, intermediate
+ *  signs the leaf and so on).
+ *  @return true if it's ok, false if not.
+ */
+bool
+CertificateChain::valid () const
+{
+       X509_STORE* store = X509_STORE_new ();
+       if (!store) {
+               return false;
+       }
+
+       for (List::const_iterator i = _certificates.begin(); i != _certificates.end(); ++i) {
+
+               List::const_iterator j = i;
+               ++j;
+               if (j ==  _certificates.end ()) {
+                       break;
+               }
+
+               if (!X509_STORE_add_cert (store, i->x509 ())) {
+                       X509_STORE_free (store);
+                       return false;
+               }
+
+               X509_STORE_CTX* ctx = X509_STORE_CTX_new ();
+               if (!ctx) {
+                       X509_STORE_free (store);
+                       return false;
+               }
+
+               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);
+                       return false;
+               }
+
+               int v = X509_verify_cert (ctx);
+               X509_STORE_CTX_free (ctx);
+
+               if (v == 0) {
+                       X509_STORE_free (store);
+                       return false;
+               }
+       }
+
+       X509_STORE_free (store);
+       return true;
+}
+
+/** @return true if the chain is now in order from root to leaf,
+ *  false if no correct order was found.
+ */
+bool
+CertificateChain::attempt_reorder ()
+{
+       List original = _certificates;
+       _certificates.sort ();
+       do {
+               if (valid ()) {
+                       return true;
+               }
+       } while (std::next_permutation (_certificates.begin(), _certificates.end ()));
+
+       _certificates = original;
+       return false;
+}
index dcb9c1418e07b6d1443f7afe04ad218b97b9e26f..8ebf854ce31f79a1274aa7ea8976e323ca93daab 100644 (file)
  *  @brief Functions to make signer chains.
  */
 
+#ifndef LIBDCP_CERTIFICATE_CHAIN_H
+#define LIBDCP_CERTIFICATE_CHAIN_H
+
+#include "certificates.h"
 #include <boost/filesystem.hpp>
 
 namespace dcp {
 
+/** @class CertificateChain
+ *  @brief A chain of any number of certificates, from root to leaf.
+ */
+class CertificateChain
+{
+public:
+       CertificateChain () {}
+
+       void add (Certificate c);
+       void remove (Certificate c);
+       void remove (int);
+
+       Certificate root () const;
+       Certificate leaf () const;
+
+       typedef std::list<Certificate> List;
+
+       List leaf_to_root () const;
+       List root_to_leaf () const;
+
+       bool valid () const;
+       bool attempt_reorder ();
+
+private:
+       friend class ::certificates;
+
+       List _certificates;
+};
+
 /** Create a chain of certificates for signing things.
  *  @param openssl Name of openssl binary (if it is on the path) or full path.
  *  @return Directory (which should be deleted by the caller) containing:
@@ -43,3 +76,5 @@ boost::filesystem::path make_certificate_chain (
        );
 
 }
+
+#endif
index e5acdd253a4f0f26d3c7b5b3b252f049352be179..37b5776769f2133b046ceeeb45accc2f48bdc1cb 100644 (file)
@@ -316,139 +316,3 @@ dcp::operator<< (ostream& s, Certificate const & c)
        s << c.certificate();
        return s;
 }
-
-/** @return Root certificate */
-Certificate
-CertificateChain::root () const
-{
-       DCP_ASSERT (!_certificates.empty());
-       return _certificates.front ();
-}
-
-/** @return Leaf certificate */
-Certificate
-CertificateChain::leaf () const
-{
-       DCP_ASSERT (_certificates.size() >= 2);
-       return _certificates.back ();
-}
-
-/** @return Certificates in order from root to leaf */
-CertificateChain::List
-CertificateChain::root_to_leaf () const
-{
-       return _certificates;
-}
-
-/** @return Certificates in order from leaf to root */
-CertificateChain::List
-CertificateChain::leaf_to_root () const
-{
-       List c = _certificates;
-       c.reverse ();
-       return c;
-}
-
-/** Add a certificate to the end of the chain.
- *  @param c Certificate to add.
- */
-void
-CertificateChain::add (Certificate c)
-{
-       _certificates.push_back (c);
-}
-
-/** Remove a certificate from the chain.
- *  @param c Certificate to remove.
- */
-void
-CertificateChain::remove (Certificate c)
-{
-       _certificates.remove (c);
-}
-
-/** Remove the i'th certificate in the list, as listed
- *  from root to leaf.
- */
-void
-CertificateChain::remove (int i)
-{
-       List::iterator j = _certificates.begin ();
-        while (j != _certificates.end () && i > 0) {
-               --i;
-               ++j;
-       }
-
-       if (j != _certificates.end ()) {
-               _certificates.erase (j);
-       }
-}
-
-/** Check to see if the chain is valid (i.e. root signs the intermediate, intermediate
- *  signs the leaf and so on).
- *  @return true if it's ok, false if not.
- */
-bool
-CertificateChain::valid () const
-{
-       X509_STORE* store = X509_STORE_new ();
-       if (!store) {
-               return false;
-       }
-
-       for (List::const_iterator i = _certificates.begin(); i != _certificates.end(); ++i) {
-
-               List::const_iterator j = i;
-               ++j;
-               if (j ==  _certificates.end ()) {
-                       break;
-               }
-
-               if (!X509_STORE_add_cert (store, i->x509 ())) {
-                       X509_STORE_free (store);
-                       return false;
-               }
-
-               X509_STORE_CTX* ctx = X509_STORE_CTX_new ();
-               if (!ctx) {
-                       X509_STORE_free (store);
-                       return false;
-               }
-
-               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);
-                       return false;
-               }
-
-               int v = X509_verify_cert (ctx);
-               X509_STORE_CTX_free (ctx);
-
-               if (v == 0) {
-                       X509_STORE_free (store);
-                       return false;
-               }
-       }
-
-       X509_STORE_free (store);
-       return true;
-}
-
-/** @return true if the chain is now in order from root to leaf,
- *  false if no correct order was found.
- */
-bool
-CertificateChain::attempt_reorder ()
-{
-       List original = _certificates;
-       _certificates.sort ();
-       do {
-               if (valid ()) {
-                       return true;
-               }
-       } while (std::next_permutation (_certificates.begin(), _certificates.end ()));
-
-       _certificates = original;
-       return false;
-}
index 2ece6f7820ca1fa3bf88369176e345471682bcb6..c2121a8315d77e0f21c97309ed45492c645a4117 100644 (file)
@@ -91,35 +91,6 @@ bool operator== (Certificate const & a, Certificate const & b);
 bool operator< (Certificate const & a, Certificate const & b);
 std::ostream& operator<< (std::ostream&s, Certificate const & c);
 
-/** @class CertificateChain
- *  @brief A chain of any number of certificates, from root to leaf.
- */
-class CertificateChain
-{
-public:
-       CertificateChain () {}
-
-       void add (Certificate c);
-       void remove (Certificate c);
-       void remove (int);
-
-       Certificate root () const;
-       Certificate leaf () const;
-
-       typedef std::list<Certificate> List;
-
-       List leaf_to_root () const;
-       List root_to_leaf () const;
-
-       bool valid () const;
-       bool attempt_reorder ();
-
-private:
-       friend class ::certificates;
-
-       List _certificates;
-};
-
 }
 
 #endif
index 97cfa6d14c096402837e4d74d781f589bb783c63..a65de11f2f631d793ecf380d29c47bde91f3e7f1 100644 (file)
@@ -25,6 +25,7 @@
  */
 
 #include "certificates.h"
+#include "certificate_chain.h"
 #include "types.h"
 #include <boost/filesystem.hpp>