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 assert (_certificate);
68 BIO* bio = BIO_new (BIO_s_mem ());
70 throw MiscError ("could not create memory BIO");
73 PEM_write_bio_X509 (bio, _certificate);
77 long int const data_length = BIO_get_mem_data (bio, &data);
78 for (long int i = 0; i < data_length; ++i) {
84 boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
85 boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
90 Certificate::issuer () const
92 assert (_certificate);
94 X509_NAME* n = X509_get_issuer_name (_certificate);
98 X509_NAME_oneline (n, b, 256);
103 Certificate::name_for_xml (string const & n)
108 boost::split (p, n, boost::is_any_of ("/"));
109 for (vector<string>::const_reverse_iterator i = p.rbegin(); i != p.rend(); ++i) {
114 boost::replace_all (s, "+", "\\+");
116 return s.substr(0, s.length() - 2);
120 Certificate::subject () const
122 assert (_certificate);
124 X509_NAME* n = X509_get_subject_name (_certificate);
128 X509_NAME_oneline (n, b, 256);
133 Certificate::serial () const
135 assert (_certificate);
137 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
140 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
141 char* c = BN_bn2dec (b);
151 Certificate::thumbprint () const
153 assert (_certificate);
155 uint8_t buffer[8192];
157 i2d_X509_CINF (_certificate->cert_info, &p);
158 int const length = p - buffer;
160 throw MiscError ("buffer too small to generate thumbprint");
165 SHA1_Update (&sha, buffer, length);
167 SHA1_Final (digest, &sha);
169 char digest_base64[64];
170 return Kumu::base64encode (digest, 20, digest_base64, 64);
173 shared_ptr<Certificate>
174 CertificateChain::root () const
176 assert (!_certificates.empty());
177 return _certificates.front ();
180 shared_ptr<Certificate>
181 CertificateChain::leaf () const
183 assert (_certificates.size() >= 2);
184 return _certificates.back ();
187 list<shared_ptr<Certificate> >
188 CertificateChain::leaf_to_root () const
190 list<shared_ptr<Certificate> > c = _certificates;
196 CertificateChain::add (shared_ptr<Certificate> c)
198 _certificates.push_back (c);