Fix erroneous reports of unresolved assets when checking OV/VF pairs.
[libdcp.git] / src / certificate_chain.h
1 /*
2     Copyright (C) 2013-2016 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 /** @file  src/signer_chain.h
35  *  @brief Functions to make signer chains.
36  */
37
38 #ifndef LIBDCP_CERTIFICATE_CHAIN_H
39 #define LIBDCP_CERTIFICATE_CHAIN_H
40
41 #include "certificate.h"
42 #include "types.h"
43 #include <boost/filesystem.hpp>
44 #include <boost/optional.hpp>
45
46 namespace xmlpp {
47         class Node;
48 }
49
50 struct certificates_validation1;
51 struct certificates_validation2;
52 struct certificates_validation3;
53 struct certificates_validation4;
54 struct certificates_validation5;
55 struct certificates_validation6;
56 struct certificates_validation7;
57 struct certificates_validation8;
58
59 namespace dcp {
60
61 /** @class CertificateChain
62  *  @brief A chain of any number of certificates, from root to leaf.
63  */
64 class CertificateChain
65 {
66 public:
67         CertificateChain () {}
68
69         /** Create a chain of certificates for signing things.
70          *  @param openssl Name of openssl binary (if it is on the path) or full path.
71          *  @return Directory (which should be deleted by the caller) containing:
72          *    - ca.self-signed.pem      self-signed root certificate
73          *    - intermediate.signed.pem intermediate certificate
74          *    - leaf.key                leaf certificate private key
75          *    - leaf.signed.pem         leaf certificate
76          */
77         CertificateChain (
78                 boost::filesystem::path openssl,
79                 std::string organisation = "example.org",
80                 std::string organisational_unit = "example.org",
81                 std::string root_common_name = ".smpte-430-2.ROOT.NOT_FOR_PRODUCTION",
82                 std::string intermediate_common_name = ".smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION",
83                 std::string leaf_common_name = "CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION"
84                 );
85
86         explicit CertificateChain (std::string);
87
88         void add (Certificate c);
89         void remove (Certificate c);
90         void remove (int);
91
92         Certificate root () const;
93         Certificate leaf () const;
94
95         typedef std::list<Certificate> List;
96
97         List leaf_to_root () const;
98         List root_to_leaf () const;
99         List unordered () const;
100
101         bool valid (std::string* reason = 0) const;
102         bool chain_valid () const;
103         bool private_key_valid () const;
104
105         void sign (xmlpp::Element* parent, Standard standard) const;
106         void add_signature_value (xmlpp::Element* parent, std::string ns, bool add_indentation) const;
107
108         boost::optional<std::string> key () const {
109                 return _key;
110         }
111
112         void set_key (std::string k) {
113                 _key = k;
114         }
115
116         std::string chain () const;
117
118 private:
119         friend struct ::certificates_validation1;
120         friend struct ::certificates_validation2;
121         friend struct ::certificates_validation3;
122         friend struct ::certificates_validation4;
123         friend struct ::certificates_validation5;
124         friend struct ::certificates_validation6;
125         friend struct ::certificates_validation7;
126         friend struct ::certificates_validation8;
127
128         bool chain_valid (List const & chain) const;
129
130         /** Our certificates, not in any particular order */
131         List _certificates;
132         /** Leaf certificate's private key, if known */
133         boost::optional<std::string> _key;
134 };
135
136 }
137
138 #endif