2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include <boost/test/unit_test.hpp>
21 #include "certificate.h"
22 #include "certificate_chain.h"
24 #include "exceptions.h"
29 using boost::shared_ptr;
31 /** Check that loading certificates from files via strings works */
32 BOOST_AUTO_TEST_CASE (certificates1)
34 dcp::CertificateChain c;
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")));
40 dcp::CertificateChain::List leaf_to_root = c.leaf_to_root ();
42 dcp::CertificateChain::List::iterator i = leaf_to_root.begin ();
45 BOOST_CHECK_EQUAL (*i, c.leaf ());
49 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
54 "dnQualifier=QFVlym7fuql6bPOnY38aaO1ZPW4=,CN=CS.smpte-430-2.LEAF.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
62 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
67 "dnQualifier=6eat8r33US71avuQEojmH\\+bjk84=,CN=.smpte-430-2.INTERMEDIATE.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
73 BOOST_CHECK_EQUAL (*i, c.root ());
76 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
79 BOOST_CHECK_EQUAL (c.root().serial(), "5");
83 "dnQualifier=DCnRdHFbcv4ANVUq2\\+wMVALFSec=,CN=.smpte-430-2.ROOT.NOT_FOR_PRODUCTION,OU=example.org,O=example.org"
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());
91 /** Check some more certificate-from-strings */
92 BOOST_AUTO_TEST_CASE (certificates2)
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"));
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"));
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);
108 /** Check that dcp::CertificateChain::valid() and ::attempt_reorder() basically work */
109 BOOST_AUTO_TEST_CASE (certificates_validation)
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 ());
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 ());
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 ());
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 ());
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 ());
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 ());
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 ());
155 /** Check that dcp::Signer::valid() basically works */
156 BOOST_AUTO_TEST_CASE (signer_validation)
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 ());
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 ());