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 (string const & filename)
48 FILE* f = fopen (filename.c_str(), "r");
50 throw FileError ("could not open file", filename);
53 if (!PEM_read_X509 (f, &_certificate, 0, 0)) {
54 throw MiscError ("could not read X509 certificate");
58 Certificate::~Certificate ()
60 X509_free (_certificate);
64 Certificate::certificate () const
66 BIO* bio = BIO_new (BIO_s_mem ());
68 throw MiscError ("could not create memory BIO");
71 PEM_write_bio_X509 (bio, _certificate);
75 long int const data_length = BIO_get_mem_data (bio, &data);
76 for (long int i = 0; i < data_length; ++i) {
82 boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
83 boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
88 Certificate::issuer () const
90 X509_NAME* n = X509_get_issuer_name (_certificate);
94 X509_NAME_oneline (n, b, 256);
99 Certificate::name_for_xml (string const & n)
104 boost::split (p, n, boost::is_any_of ("/"));
105 for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
110 boost::replace_all (s, "+", "\\+");
112 return s.substr(0, s.length() - 2);
116 Certificate::subject () const
118 X509_NAME* n = X509_get_subject_name (_certificate);
122 X509_NAME_oneline (n, b, 256);
127 Certificate::serial () const
129 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
132 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
133 char* c = BN_bn2dec (b);
143 Certificate::thumbprint () const
145 uint8_t buffer[8192];
147 i2d_X509_CINF (_certificate->cert_info, &p);
148 int const length = p - buffer;
150 throw MiscError ("buffer too small to generate thumbprint");
155 SHA1_Update (&sha, buffer, length);
157 SHA1_Final (digest, &sha);
159 char digest_base64[64];
160 return Kumu::base64encode (digest, 20, digest_base64, 64);
163 shared_ptr<Certificate>
164 CertificateChain::root () const
166 assert (!_certificates.empty());
167 return _certificates.front ();
170 shared_ptr<Certificate>
171 CertificateChain::leaf () const
173 assert (_certificates.size() >= 2);
174 return _certificates.back ();
177 list<shared_ptr<Certificate> >
178 CertificateChain::leaf_to_root () const
180 list<shared_ptr<Certificate> > c = _certificates;
186 CertificateChain::add (shared_ptr<Certificate> c)
188 _certificates.push_back (c);