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.
23 #include <boost/algorithm/string.hpp>
24 #include <openssl/x509.h>
25 #include <openssl/ssl.h>
26 #include <openssl/asn1.h>
27 #include <openssl/err.h>
28 #include <libxml++/nodes/element.h>
30 #include "certificates.h"
31 #include "compose.hpp"
32 #include "exceptions.h"
37 using std::stringstream;
39 using boost::shared_ptr;
40 using namespace libdcp;
42 /** @param c X509 certificate, which this object will take ownership of */
43 Certificate::Certificate (X509* c)
50 Certificate::Certificate (boost::filesystem::path filename)
54 FILE* f = fopen_boost (filename, "r");
56 throw FileError ("could not open file", filename, errno);
59 if (!PEM_read_X509 (f, &_certificate, 0, 0)) {
60 throw MiscError ("could not read X509 certificate");
64 Certificate::Certificate (string cert)
71 Certificate::Certificate (Certificate const & other)
75 read_string (other.certificate (true));
79 Certificate::read_string (string cert)
81 BIO* bio = BIO_new_mem_buf (const_cast<char *> (cert.c_str ()), -1);
83 throw MiscError ("could not create memory BIO");
86 _certificate = PEM_read_bio_X509 (bio, 0, 0, 0);
88 throw MiscError ("could not read X509 certificate from memory BIO");
94 Certificate::~Certificate ()
96 X509_free (_certificate);
97 RSA_free (_public_key);
101 Certificate::operator= (Certificate const & other)
103 if (this == &other) {
107 X509_free (_certificate);
109 RSA_free (_public_key);
112 read_string (other.certificate ());
118 Certificate::certificate (bool with_begin_end) const
120 assert (_certificate);
122 BIO* bio = BIO_new (BIO_s_mem ());
124 throw MiscError ("could not create memory BIO");
127 PEM_write_bio_X509 (bio, _certificate);
131 long int const data_length = BIO_get_mem_data (bio, &data);
132 for (long int i = 0; i < data_length; ++i) {
138 if (!with_begin_end) {
139 boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
140 boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
147 Certificate::issuer () const
149 assert (_certificate);
150 return name_for_xml (X509_get_issuer_name (_certificate));
154 Certificate::asn_to_utf8 (ASN1_STRING* s)
156 unsigned char* buf = 0;
157 ASN1_STRING_to_UTF8 (&buf, s);
158 string const u (reinterpret_cast<char *> (buf));
164 Certificate::get_name_part (X509_NAME* n, int nid)
167 p = X509_NAME_get_index_by_NID (n, nid, p);
169 return asn_to_utf8 (X509_NAME_ENTRY_get_data (X509_NAME_get_entry (n, p)));
174 Certificate::name_for_xml (X509_NAME * n)
178 string s = String::compose (
179 "O=%1,OU=%2,CN=%3,dnQualifier=%4",
180 get_name_part (n, NID_organizationName),
181 get_name_part (n, NID_organizationalUnitName),
182 get_name_part (n, NID_commonName),
183 get_name_part (n, NID_dnQualifier)
186 boost::replace_all (s, "+", "\\+");
191 Certificate::subject () const
193 assert (_certificate);
195 return name_for_xml (X509_get_subject_name (_certificate));
199 Certificate::common_name () const
201 assert (_certificate);
203 return get_name_part (X509_get_subject_name (_certificate), NID_commonName);
207 Certificate::serial () const
209 assert (_certificate);
211 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
214 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
215 char* c = BN_bn2dec (b);
225 Certificate::thumbprint () const
227 assert (_certificate);
229 uint8_t buffer[8192];
231 i2d_X509_CINF (_certificate->cert_info, &p);
232 int const length = p - buffer;
234 throw MiscError ("buffer too small to generate thumbprint");
239 SHA1_Update (&sha, buffer, length);
241 SHA1_Final (digest, &sha);
243 char digest_base64[64];
244 return Kumu::base64encode (digest, 20, digest_base64, 64);
248 Certificate::public_key () const
250 assert (_certificate);
256 EVP_PKEY* key = X509_get_pubkey (_certificate);
258 throw MiscError ("could not get public key from certificate");
261 _public_key = EVP_PKEY_get1_RSA (key);
263 throw MiscError (String::compose ("could not get RSA public key (%1)", ERR_error_string (ERR_get_error(), 0)));
269 shared_ptr<Certificate>
270 CertificateChain::root () const
272 assert (!_certificates.empty());
273 return _certificates.front ();
276 shared_ptr<Certificate>
277 CertificateChain::leaf () const
279 assert (_certificates.size() >= 2);
280 return _certificates.back ();
283 list<shared_ptr<Certificate> >
284 CertificateChain::leaf_to_root () const
286 list<shared_ptr<Certificate> > c = _certificates;
292 CertificateChain::add (shared_ptr<Certificate> c)
294 _certificates.push_back (c);