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.
22 #include <boost/algorithm/string.hpp>
23 #include <openssl/x509.h>
24 #include <openssl/ssl.h>
25 #include <openssl/asn1.h>
26 #include <libxml++/nodes/element.h>
28 #include "certificates.h"
29 #include "exceptions.h"
33 using std::stringstream;
35 using boost::shared_ptr;
36 using namespace libdcp;
38 /** @param c X509 certificate, which this object will take ownership of */
39 Certificate::Certificate (X509* c)
45 Certificate::~Certificate ()
47 X509_free (_certificate);
51 Certificate::certificate () const
53 BIO* bio = BIO_new (BIO_s_mem ());
55 throw MiscError ("could not create memory BIO");
58 PEM_write_bio_X509 (bio, _certificate);
62 long int const data_length = BIO_get_mem_data (bio, &data);
63 for (long int i = 0; i < data_length; ++i) {
69 boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
70 boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
75 Certificate::issuer () const
77 X509_NAME* n = X509_get_issuer_name (_certificate);
81 X509_NAME_oneline (n, b, 256);
86 Certificate::name_for_xml (string const & n)
91 boost::split (p, n, boost::is_any_of ("/"));
92 for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
97 boost::replace_all (s, "+", "\\+");
99 return s.substr(0, s.length() - 2);
103 Certificate::subject () const
105 X509_NAME* n = X509_get_subject_name (_certificate);
109 X509_NAME_oneline (n, b, 256);
114 Certificate::serial () const
116 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
119 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
120 char* c = BN_bn2dec (b);
130 Certificate::thumbprint () const
132 uint8_t buffer[8192];
134 i2d_X509_CINF (_certificate->cert_info, &p);
135 int const length = p - buffer;
137 throw MiscError ("buffer too small to generate thumbprint");
142 SHA1_Update (&sha, buffer, length);
144 SHA1_Final (digest, &sha);
146 char digest_base64[64];
147 return Kumu::base64encode (digest, 20, digest_base64, 64);
150 /** @param filename Text file of PEM-format certificates,
153 * 1. self-signed root certificate
154 * 2. intermediate certificate signed by root certificate
156 * n. leaf certificate signed by previous intermediate.
159 CertificateChain::CertificateChain (string const & filename)
161 FILE* f = fopen (filename.c_str(), "r");
163 throw FileError ("could not open file", filename);
168 if (!PEM_read_X509 (f, &c, 0, 0)) {
172 _certificates.push_back (shared_ptr<Certificate> (new Certificate (c)));
176 shared_ptr<Certificate>
177 CertificateChain::root () const
179 assert (!_certificates.empty());
180 return _certificates.front ();
183 shared_ptr<Certificate>
184 CertificateChain::leaf () const
186 assert (_certificates.size() >= 2);
187 return _certificates.back ();
190 list<shared_ptr<Certificate> >
191 CertificateChain::leaf_to_root () const
193 list<shared_ptr<Certificate> > c = _certificates;