Allow reading of certificate chains from strings.
[libdcp.git] / test / certificates_test.cc
1 /*
2     Copyright (C) 2012 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
20 #include "certificate.h"
21 #include "certificate_chain.h"
22 #include "util.h"
23 #include "exceptions.h"
24 #include "test.h"
25 #include <boost/test/unit_test.hpp>
26 #include <iostream>
27
28 using std::list;
29 using std::string;
30 using boost::shared_ptr;
31
32 /** Check that loading certificates from files via strings works */
33 BOOST_AUTO_TEST_CASE (certificates1)
34 {
35         dcp::CertificateChain c;
36
37         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
38         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
39         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
40
41         dcp::CertificateChain::List leaf_to_root = c.leaf_to_root ();
42
43         dcp::CertificateChain::List::iterator i = leaf_to_root.begin ();
44
45         /* Leaf */
46         BOOST_CHECK_EQUAL (*i, c.leaf ());
47
48         BOOST_CHECK_EQUAL (
49                 c.leaf().issuer(),
50                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
51                 );
52
53         BOOST_CHECK_EQUAL (
54                 c.leaf().subject(),
55                 "dnQualifier=QFVlym7fuql6bPOnY38aaO1ZPW4=,CN=CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
56                 );
57
58         ++i;
59
60         /* Intermediate */
61         BOOST_CHECK_EQUAL (
62                 i->issuer(),
63                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
64                 );
65
66         BOOST_CHECK_EQUAL (
67                 i->subject(),
68                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
69                 );
70
71         ++i;
72
73         /* Root */
74         BOOST_CHECK_EQUAL (*i, c.root ());
75         BOOST_CHECK_EQUAL (
76                 c.root().issuer(),
77                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
78                 );
79
80         BOOST_CHECK_EQUAL (c.root().serial(), "5");
81
82         BOOST_CHECK_EQUAL (
83                 c.root().subject(),
84                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
85                 );
86
87         /* Check that reconstruction from a string works */
88         dcp::Certificate test (c.root().certificate (true));
89         BOOST_CHECK_EQUAL (test.certificate(), c.root().certificate());
90 }
91
92 /** Check some more certificate-from-strings */
93 BOOST_AUTO_TEST_CASE (certificates2)
94 {
95         {
96                 dcp::Certificate c (dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt"));
97                 BOOST_CHECK_EQUAL (c.certificate(true), dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt.reformatted"));
98         }
99
100         {
101                 dcp::Certificate c (dcp::file_to_string (private_test / "usl-cert.pem"));
102                 BOOST_CHECK_EQUAL (c.certificate(true), dcp::file_to_string (private_test / "usl-cert.pem.trimmed"));
103         }
104
105         {
106                 /* This is a chain, not an individual certificate, so it should throw an exception */
107                 BOOST_CHECK_THROW (dcp::Certificate (dcp::file_to_string (private_test / "chain.pem")), dcp::MiscError);
108         }
109
110         BOOST_CHECK_THROW (dcp::Certificate (dcp::file_to_string (private_test / "no-begin.pem")), dcp::MiscError);
111         BOOST_CHECK_THROW (dcp::Certificate ("foo"), dcp::MiscError);
112 }
113
114 /** Check that dcp::CertificateChain::valid() and ::attempt_reorder() basically work */
115 BOOST_AUTO_TEST_CASE (certificates_validation)
116 {
117         dcp::CertificateChain good1;
118         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
119         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
120         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
121         BOOST_CHECK (good1.valid ());
122
123         dcp::CertificateChain good2;
124         good2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
125         BOOST_CHECK (good2.valid ());
126
127         dcp::CertificateChain bad1;
128         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
129         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
130         BOOST_CHECK (!bad1.valid ());
131         BOOST_CHECK (!bad1.attempt_reorder ());
132
133         dcp::CertificateChain bad2;
134         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
135         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
136         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
137         BOOST_CHECK (!bad2.valid ());
138         BOOST_CHECK (bad2.attempt_reorder ());
139
140         dcp::CertificateChain bad3;
141         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
142         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
143         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
144         BOOST_CHECK (!bad3.valid ());
145         BOOST_CHECK (bad3.attempt_reorder ());
146
147         dcp::CertificateChain bad4;
148         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
149         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
150         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
151         BOOST_CHECK (!bad4.valid ());
152         BOOST_CHECK (bad4.attempt_reorder ());
153
154         dcp::CertificateChain bad5;
155         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
156         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
157         BOOST_CHECK (!bad5.valid ());
158         BOOST_CHECK (!bad5.attempt_reorder ());
159 }
160
161 /** Check that dcp::Signer::valid() basically works */
162 BOOST_AUTO_TEST_CASE (signer_validation)
163 {
164         /* Check a valid signer */
165         dcp::CertificateChain chain;
166         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
167         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
168         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
169         chain.set_key (dcp::file_to_string ("test/ref/crypt/leaf.key"));
170         BOOST_CHECK (chain.valid ());
171
172         /* Put in an unrelated key and the signer should no longer be valid */
173         dcp::CertificateChain another_chain (boost::filesystem::path ("openssl"));
174         chain.set_key (another_chain.key().get ());
175         BOOST_CHECK (!chain.valid ());
176 }
177
178 /** Check reading of a certificate chain from a string */
179 BOOST_AUTO_TEST_CASE (certificate_chain_from_string)
180 {
181         dcp::CertificateChain a (dcp::file_to_string (private_test / "chain.pem"));
182         BOOST_CHECK_EQUAL (a.root_to_leaf().size(), 3);
183
184         dcp::CertificateChain b (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem"));
185         BOOST_CHECK_EQUAL (b.root_to_leaf().size(), 1);
186 }