2 Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
35 /** @file src/certificate_chain.h
36 * @brief CertificateChain class
40 #ifndef LIBDCP_CERTIFICATE_CHAIN_H
41 #define LIBDCP_CERTIFICATE_CHAIN_H
44 #include "certificate.h"
46 #include <boost/filesystem.hpp>
47 #include <boost/optional.hpp>
55 struct certificates_validation1;
56 struct certificates_validation2;
57 struct certificates_validation3;
58 struct certificates_validation4;
59 struct certificates_validation5;
60 struct certificates_validation6;
61 struct certificates_validation7;
62 struct certificates_validation8;
68 /** @class CertificateChain
69 * @brief A chain of any number of certificates, from root to leaf.
71 class CertificateChain
74 CertificateChain () {}
76 /** Create a chain of certificates for signing things.
77 * @param openssl Name of openssl binary (if it is on the path) or full path.
78 * @return Directory (which should be deleted by the caller) containing:
79 * - ca.self-signed.pem self-signed root certificate
80 * - intermediate.signed.pem intermediate certificate
81 * - leaf.key leaf certificate private key
82 * - leaf.signed.pem leaf certificate
85 boost::filesystem::path openssl,
86 std::string organisation = "example.org",
87 std::string organisational_unit = "example.org",
88 std::string root_common_name = ".smpte-430-2.ROOT.NOT_FOR_PRODUCTION",
89 std::string intermediate_common_name = ".smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION",
90 std::string leaf_common_name = "CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION"
93 explicit CertificateChain (std::string);
95 /** Add a certificate to the chain.
96 * @param c Certificate to add.
98 void add (Certificate c);
100 /** Remove a certificate from the chain.
101 * @param c Certificate to remove.
103 void remove (Certificate c);
105 /** Remove the i'th certificate in the chain, as listed
110 /** @return Root certificate */
111 Certificate root () const;
113 /** @return Leaf certificate */
114 Certificate leaf () const;
116 typedef std::vector<Certificate> List;
118 /** @return Certificates in order from leaf to root */
119 List leaf_to_root () const;
120 /** @return Certificates in order from root to leaf */
121 List root_to_leaf () const;
122 List unordered () const;
124 bool valid (std::string* reason = nullptr) const;
126 /** Check to see if the chain is valid (i.e. root signs the intermediate, intermediate
127 * signs the leaf and so on) and that the private key (if there is one) matches the
129 * @return true if it's ok, false if not.
131 bool chain_valid () const;
133 /** Check that there is a valid private key for the leaf certificate.
134 * Will return true if there are no certificates.
136 bool private_key_valid () const;
138 /** Add a <Signer> and <ds:Signature> nodes to an XML node.
139 * @param parent XML node to add to.
140 * @param standard INTEROP or SMPTE.
142 void sign (xmlpp::Element* parent, Standard standard) const;
144 /** Sign an XML node.
146 * @param parent Node to sign.
147 * @param ns Namespace to use for the signature XML nodes.
149 void add_signature_value (xmlpp::Element* parent, std::string ns, bool add_indentation) const;
151 boost::optional<std::string> key () const {
155 void set_key (std::string k) {
159 std::string chain () const;
162 friend struct ::certificates_validation1;
163 friend struct ::certificates_validation2;
164 friend struct ::certificates_validation3;
165 friend struct ::certificates_validation4;
166 friend struct ::certificates_validation5;
167 friend struct ::certificates_validation6;
168 friend struct ::certificates_validation7;
169 friend struct ::certificates_validation8;
171 bool chain_valid (List const & chain) const;
173 /** Our certificates, not in any particular order */
175 /** Leaf certificate's private key, if known */
176 boost::optional<std::string> _key;