Allow certificates with preamble before BEGIN CERTIFICATE (dcpomatic #774).
[libdcp.git] / test / certificates_test.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <boost/test/unit_test.hpp>
21 #include "certificate.h"
22 #include "certificate_chain.h"
23 #include "util.h"
24 #include "exceptions.h"
25 #include "test.h"
26
27 using std::list;
28 using std::string;
29 using boost::shared_ptr;
30
31 /** Check that loading certificates from files via strings works */
32 BOOST_AUTO_TEST_CASE (certificates1)
33 {
34         dcp::CertificateChain c;
35
36         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
37         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
38         c.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
39
40         dcp::CertificateChain::List leaf_to_root = c.leaf_to_root ();
41
42         dcp::CertificateChain::List::iterator i = leaf_to_root.begin ();
43
44         /* Leaf */
45         BOOST_CHECK_EQUAL (*i, c.leaf ());
46
47         BOOST_CHECK_EQUAL (
48                 c.leaf().issuer(),
49                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
50                 );
51
52         BOOST_CHECK_EQUAL (
53                 c.leaf().subject(),
54                 "dnQualifier=QFVlym7fuql6bPOnY38aaO1ZPW4=,CN=CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
55                 );
56
57         ++i;
58
59         /* Intermediate */
60         BOOST_CHECK_EQUAL (
61                 i->issuer(),
62                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
63                 );
64
65         BOOST_CHECK_EQUAL (
66                 i->subject(),
67                 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
68                 );
69
70         ++i;
71
72         /* Root */
73         BOOST_CHECK_EQUAL (*i, c.root ());
74         BOOST_CHECK_EQUAL (
75                 c.root().issuer(),
76                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
77                 );
78
79         BOOST_CHECK_EQUAL (c.root().serial(), "5");
80
81         BOOST_CHECK_EQUAL (
82                 c.root().subject(),
83                 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
84                 );
85
86         /* Check that reconstruction from a string works */
87         dcp::Certificate test (c.root().certificate (true));
88         BOOST_CHECK_EQUAL (test.certificate(), c.root().certificate());
89 }
90
91 /** Check some more certificate-from-strings */
92 BOOST_AUTO_TEST_CASE (certificates2)
93 {
94         {
95                 dcp::Certificate c (dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt"));
96                 BOOST_CHECK_EQUAL (c.certificate(true), dcp::file_to_string (private_test / "CA.GDC-TECH.COM_SA2100_A14903.crt.crt.reformatted"));
97         }
98
99         {
100                 dcp::Certificate c (dcp::file_to_string (private_test / "usl-cert.pem"));
101                 BOOST_CHECK_EQUAL (c.certificate(true), dcp::file_to_string (private_test / "usl-cert.pem.trimmed"));
102         }
103
104         BOOST_CHECK_THROW (dcp::Certificate (dcp::file_to_string (private_test / "no-begin.pem")), dcp::MiscError);
105         BOOST_CHECK_THROW (dcp::Certificate ("foo"), dcp::MiscError);
106 }
107
108 /** Check that dcp::CertificateChain::valid() and ::attempt_reorder() basically work */
109 BOOST_AUTO_TEST_CASE (certificates_validation)
110 {
111         dcp::CertificateChain good1;
112         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
113         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
114         good1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
115         BOOST_CHECK (good1.valid ());
116
117         dcp::CertificateChain good2;
118         good2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
119         BOOST_CHECK (good2.valid ());
120
121         dcp::CertificateChain bad1;
122         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
123         bad1.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
124         BOOST_CHECK (!bad1.valid ());
125         BOOST_CHECK (!bad1.attempt_reorder ());
126
127         dcp::CertificateChain bad2;
128         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
129         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
130         bad2.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
131         BOOST_CHECK (!bad2.valid ());
132         BOOST_CHECK (bad2.attempt_reorder ());
133
134         dcp::CertificateChain bad3;
135         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
136         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
137         bad3.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
138         BOOST_CHECK (!bad3.valid ());
139         BOOST_CHECK (bad3.attempt_reorder ());
140
141         dcp::CertificateChain bad4;
142         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
143         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
144         bad4.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
145         BOOST_CHECK (!bad4.valid ());
146         BOOST_CHECK (bad4.attempt_reorder ());
147
148         dcp::CertificateChain bad5;
149         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
150         bad5.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
151         BOOST_CHECK (!bad5.valid ());
152         BOOST_CHECK (!bad5.attempt_reorder ());
153 }
154
155 /** Check that dcp::Signer::valid() basically works */
156 BOOST_AUTO_TEST_CASE (signer_validation)
157 {
158         /* Check a valid signer */
159         dcp::CertificateChain chain;
160         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/ca.self-signed.pem")));
161         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/intermediate.signed.pem")));
162         chain.add (dcp::Certificate (dcp::file_to_string ("test/ref/crypt/leaf.signed.pem")));
163         chain.set_key (dcp::file_to_string ("test/ref/crypt/leaf.key"));
164         BOOST_CHECK (chain.valid ());
165
166         /* Put in an unrelated key and the signer should no longer be valid */
167         dcp::CertificateChain another_chain ("openssl");
168         chain.set_key (another_chain.key().get ());
169         BOOST_CHECK (!chain.valid ());
170 }