2 Copyright (C) 2012-2014 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 /** @file src/certificates.cc
21 * @brief Certificate and CertificateChain classes.
25 #include "certificates.h"
26 #include "compose.hpp"
27 #include "exceptions.h"
29 #include <libxml++/nodes/element.h>
30 #include <openssl/x509.h>
31 #include <openssl/ssl.h>
32 #include <openssl/asn1.h>
33 #include <openssl/err.h>
34 #include <boost/algorithm/string.hpp>
39 using boost::shared_ptr;
42 /** @param c X509 certificate, which this object will take ownership of */
43 Certificate::Certificate (X509* c)
50 /** Load an X509 certificate from a file.
51 * @param filename File to load.
53 Certificate::Certificate (boost::filesystem::path filename)
57 FILE* f = fopen_boost (filename, "r");
59 throw FileError ("could not open file", filename, errno);
62 if (!PEM_read_X509 (f, &_certificate, 0, 0)) {
63 throw MiscError ("could not read X509 certificate");
67 /** Load an X509 certificate from a string.
68 * @param cert String to read from.
70 Certificate::Certificate (string cert)
78 * @param other Certificate to copy.
80 Certificate::Certificate (Certificate const & other)
84 read_string (other.certificate (true));
87 /** Read a certificate from a string.
88 * @param cert String to read.
91 Certificate::read_string (string cert)
93 BIO* bio = BIO_new_mem_buf (const_cast<char *> (cert.c_str ()), -1);
95 throw MiscError ("could not create memory BIO");
98 _certificate = PEM_read_bio_X509 (bio, 0, 0, 0);
100 throw MiscError ("could not read X509 certificate from memory BIO");
107 Certificate::~Certificate ()
109 X509_free (_certificate);
110 RSA_free (_public_key);
113 /** operator= for Certificate.
114 * @param other Certificate to read from.
117 Certificate::operator= (Certificate const & other)
119 if (this == &other) {
123 X509_free (_certificate);
125 RSA_free (_public_key);
128 read_string (other.certificate ());
133 /** Return the certificate as a string.
134 * @param with_begin_end true to include the -----BEGIN CERTIFICATE--- / -----END CERTIFICATE----- markers.
135 * @return Certificate string.
138 Certificate::certificate (bool with_begin_end) const
140 assert (_certificate);
142 BIO* bio = BIO_new (BIO_s_mem ());
144 throw MiscError ("could not create memory BIO");
147 PEM_write_bio_X509 (bio, _certificate);
151 long int const data_length = BIO_get_mem_data (bio, &data);
152 for (long int i = 0; i < data_length; ++i) {
158 if (!with_begin_end) {
159 boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", "");
160 boost::replace_all (s, "\n-----END CERTIFICATE-----\n", "");
166 /** @return Certificate's issuer, in the form
167 * dnqualifier=<dnQualififer>,CN=<commonName>,OU=<organizationalUnitName>,O=<organizationName>
168 * and with + signs escaped to \+
171 Certificate::issuer () const
173 assert (_certificate);
174 return name_for_xml (X509_get_issuer_name (_certificate));
178 Certificate::asn_to_utf8 (ASN1_STRING* s)
180 unsigned char* buf = 0;
181 ASN1_STRING_to_UTF8 (&buf, s);
182 string const u (reinterpret_cast<char *> (buf));
188 Certificate::get_name_part (X509_NAME* n, int nid)
191 p = X509_NAME_get_index_by_NID (n, nid, p);
193 return asn_to_utf8 (X509_NAME_ENTRY_get_data (X509_NAME_get_entry (n, p)));
198 Certificate::name_for_xml (X509_NAME * n)
202 string s = String::compose (
203 "dnQualifier=%1,CN=%2,OU=%3,O=%4",
204 get_name_part (n, NID_dnQualifier),
205 get_name_part (n, NID_commonName),
206 get_name_part (n, NID_organizationalUnitName),
207 get_name_part (n, NID_organizationName)
210 boost::replace_all (s, "+", "\\+");
215 Certificate::subject () const
217 assert (_certificate);
219 return name_for_xml (X509_get_subject_name (_certificate));
223 Certificate::common_name () const
225 assert (_certificate);
227 return get_name_part (X509_get_subject_name (_certificate), NID_commonName);
231 Certificate::serial () const
233 assert (_certificate);
235 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
238 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
239 char* c = BN_bn2dec (b);
249 Certificate::thumbprint () const
251 assert (_certificate);
253 uint8_t buffer[8192];
255 i2d_X509_CINF (_certificate->cert_info, &p);
256 int const length = p - buffer;
258 throw MiscError ("buffer too small to generate thumbprint");
263 SHA1_Update (&sha, buffer, length);
265 SHA1_Final (digest, &sha);
267 char digest_base64[64];
268 return Kumu::base64encode (digest, 20, digest_base64, 64);
271 /** @return RSA public key from this Certificate. Caller must not free the returned value. */
273 Certificate::public_key () const
275 assert (_certificate);
281 EVP_PKEY* key = X509_get_pubkey (_certificate);
283 throw MiscError ("could not get public key from certificate");
286 _public_key = EVP_PKEY_get1_RSA (key);
288 throw MiscError (String::compose ("could not get RSA public key (%1)", ERR_error_string (ERR_get_error(), 0)));
294 /** @return Root certificate */
295 shared_ptr<Certificate>
296 CertificateChain::root () const
298 assert (!_certificates.empty());
299 return _certificates.front ();
302 /** @return Leaf certificate */
303 shared_ptr<Certificate>
304 CertificateChain::leaf () const
306 assert (_certificates.size() >= 2);
307 return _certificates.back ();
310 /** @return Certificates in order from leaf to root */
311 list<shared_ptr<Certificate> >
312 CertificateChain::leaf_to_root () const
314 list<shared_ptr<Certificate> > c = _certificates;
319 /** Add a certificate to the end of the chain.
320 * @param c Certificate to add.
323 CertificateChain::add (shared_ptr<Certificate> c)
325 _certificates.push_back (c);
328 /** Verify the chain.
329 * @return true if it's ok, false if not.
332 CertificateChain::verify () const
334 X509_STORE* store = X509_STORE_new ();
339 for (list<shared_ptr<Certificate> >::const_iterator i = _certificates.begin(); i != _certificates.end(); ++i) {
340 list<shared_ptr<Certificate> >::const_iterator j = i;
342 if (j == _certificates.end ()) {
346 if (!X509_STORE_add_cert (store, (*i)->x509 ())) {
347 X509_STORE_free (store);
351 X509_STORE_CTX* ctx = X509_STORE_CTX_new ();
353 X509_STORE_free (store);
357 X509_STORE_set_flags (store, 0);
358 if (!X509_STORE_CTX_init (ctx, store, (*j)->x509 (), 0)) {
359 X509_STORE_CTX_free (ctx);
360 X509_STORE_free (store);
364 int v = X509_verify_cert (ctx);
365 X509_STORE_CTX_free (ctx);
368 X509_STORE_free (store);
373 X509_STORE_free (store);