aabe9f008a1f4c94b9a61fc1d0d098b6dbf296b2
[libdcp.git] / src / certificate_chain.h
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
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.
10
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.
15
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/>.
18
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
23     including the two.
24
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.
32 */
33
34
35 /** @file  src/certificate_chain.h
36  *  @brief CertificateChain class
37  */
38
39
40 #ifndef LIBDCP_CERTIFICATE_CHAIN_H
41 #define LIBDCP_CERTIFICATE_CHAIN_H
42
43
44 #include "certificate.h"
45 #include "types.h"
46 #include <boost/filesystem.hpp>
47 #include <boost/optional.hpp>
48
49
50 namespace xmlpp {
51         class Node;
52 }
53
54
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;
63
64
65 namespace dcp {
66
67
68 /** @class CertificateChain
69  *  @brief A chain of any number of certificates, from root to leaf.
70  *
71  *  A CertificateChain object can also (optionally) hold the private key corresponding
72  *  to the leaf certificate.
73  */
74 class CertificateChain
75 {
76 public:
77         CertificateChain () {}
78
79         /** Create a chain of certificates for signing things.
80          *  @param openssl Name of openssl binary (if it is on the path) or full path.
81          *  @return Directory (which should be deleted by the caller) containing:
82          *    - ca.self-signed.pem      self-signed root certificate
83          *    - intermediate.signed.pem intermediate certificate
84          *    - leaf.key                leaf certificate private key
85          *    - leaf.signed.pem         leaf certificate
86          */
87         CertificateChain (
88                 boost::filesystem::path openssl,
89                 int validity_in_days,
90                 std::string organisation = "example.org",
91                 std::string organisational_unit = "example.org",
92                 std::string root_common_name = ".smpte-430-2.ROOT.NOT_FOR_PRODUCTION",
93                 std::string intermediate_common_name = ".smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION",
94                 std::string leaf_common_name = "CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION"
95                 );
96
97         /** Read a CertificateChain from a string.
98          *  @param s A string containing one or more PEM-encoded certificates.
99          */
100         explicit CertificateChain (std::string s);
101
102         /** Add a certificate to the chain.
103          *  @param c Certificate to add.
104          */
105         void add (Certificate c);
106
107         /** Remove a certificate from the chain.
108          *  @param c Certificate to remove.
109          */
110         void remove (Certificate c);
111
112         /** Remove the i'th certificate in the chain, as listed
113          *  from root to leaf.
114          */
115         void remove (int i);
116
117         /** @return Root certificate */
118         Certificate root () const;
119
120         /** @return Leaf certificate */
121         Certificate leaf () const;
122
123         typedef std::vector<Certificate> List;
124
125         /** @return Certificates in order from leaf to root */
126         List leaf_to_root () const;
127         /** @return Certificates in order from root to leaf */
128         List root_to_leaf () const;
129         List unordered () const;
130
131         /** Check if the certificates form a chain (i.e. root signs intermediate etc.)
132          *  and that the private key matches the leaf certificate.
133          *  @param if not nullptr, filled in with a reason for vailure (or untouched
134          *  if there is no error)
135          *  @return true if the chain is valid, false if not.
136          */
137         bool valid (std::string* reason = nullptr) const;
138
139         /** Check to see if the chain is valid (i.e. root signs the intermediate, intermediate
140          *  signs the leaf and so on) and that the private key (if there is one) matches the
141          *  leaf certificate.
142          *  @return true if it's ok, false if not.
143          */
144         bool chain_valid () const;
145
146         /** Check that there is a valid private key for the leaf certificate.
147          *  Will return true if there are no certificates.
148          */
149         bool private_key_valid () const;
150
151         /** Add a &lt;Signer&gt; and &lt;ds:Signature&gt; nodes to an XML node.
152          *  @param parent XML node to add to.
153          *  @param standard INTEROP or SMPTE.
154          */
155         void sign (xmlpp::Element* parent, Standard standard) const;
156
157         /** Sign an XML node.
158          *
159          *  @param parent Node to sign.
160          *  @param ns Namespace to use for the signature XML nodes.
161          */
162         void add_signature_value (xmlpp::Element* parent, std::string ns, bool add_indentation) const;
163
164         boost::optional<std::string> key () const {
165                 return _key;
166         }
167
168         void set_key (std::string k) {
169                 _key = k;
170         }
171
172         std::string chain () const;
173
174 private:
175         friend struct ::certificates_validation1;
176         friend struct ::certificates_validation2;
177         friend struct ::certificates_validation3;
178         friend struct ::certificates_validation4;
179         friend struct ::certificates_validation5;
180         friend struct ::certificates_validation6;
181         friend struct ::certificates_validation7;
182         friend struct ::certificates_validation8;
183
184         bool chain_valid(List const & chain, std::string* error = nullptr) const;
185
186         /** Our certificates, not in any particular order */
187         List _certificates;
188         /** Leaf certificate's private key, if known, in PEM format */
189         boost::optional<std::string> _key;
190 };
191
192
193 std::string public_key_digest(boost::filesystem::path private_key, boost::filesystem::path openssl);
194
195
196 }
197
198
199 #endif