2 Copyright (C) 2012-2015 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/certificate.cc
21 * @brief Certificate class.
25 #include "certificate.h"
26 #include "compose.hpp"
27 #include "exceptions.h"
29 #include "dcp_assert.h"
30 #include <libxml++/nodes/element.h>
31 #include <openssl/x509.h>
32 #include <openssl/ssl.h>
33 #include <openssl/asn1.h>
34 #include <openssl/err.h>
35 #include <boost/algorithm/string.hpp>
43 using std::stringstream;
46 static string const begin_certificate = "-----BEGIN CERTIFICATE-----";
47 static string const end_certificate = "-----END CERTIFICATE-----";
49 /** @param c X509 certificate, which this object will take ownership of */
50 Certificate::Certificate (X509* c)
57 /** Load an X509 certificate from a string.
58 * @param cert String to read from.
60 Certificate::Certificate (string cert)
68 * @param other Certificate to copy.
70 Certificate::Certificate (Certificate const & other)
74 if (other._certificate) {
75 read_string (other.certificate (true));
79 /** Read a certificate from a string.
80 * @param cert String to read.
83 Certificate::read_string (string cert)
85 /* Reformat cert so that it has line breaks every 64 characters.
86 See http://comments.gmane.org/gmane.comp.encryption.openssl.user/55593
89 stringstream s (cert);
95 boost::algorithm::trim (line);
96 } while (s.good() && line != begin_certificate);
98 if (line != begin_certificate) {
99 throw MiscError ("missing BEGIN line in certificate");
102 /* The base64 data */
103 bool got_end = false;
105 while (getline (s, line)) {
106 boost::algorithm::trim (line);
107 if (line == end_certificate) {
115 throw MiscError ("missing END line in certificate");
118 /* Make up the fixed version */
120 string fixed = begin_certificate + "\n";
121 while (!base64.empty ()) {
122 size_t const t = min (size_t(64), base64.length());
123 fixed += base64.substr (0, t) + "\n";
124 base64 = base64.substr (t, base64.length() - t);
127 fixed += end_certificate;
129 BIO* bio = BIO_new_mem_buf (const_cast<char *> (fixed.c_str ()), -1);
131 throw MiscError ("could not create memory BIO");
134 _certificate = PEM_read_bio_X509 (bio, 0, 0, 0);
136 throw MiscError ("could not read X509 certificate from memory BIO");
143 Certificate::~Certificate ()
145 X509_free (_certificate);
146 RSA_free (_public_key);
149 /** operator= for Certificate.
150 * @param other Certificate to read from.
153 Certificate::operator= (Certificate const & other)
155 if (this == &other) {
159 X509_free (_certificate);
161 RSA_free (_public_key);
164 read_string (other.certificate (true));
169 /** Return the certificate as a string.
170 * @param with_begin_end true to include the -----BEGIN CERTIFICATE--- / -----END CERTIFICATE----- markers.
171 * @return Certificate string.
174 Certificate::certificate (bool with_begin_end) const
176 DCP_ASSERT (_certificate);
178 BIO* bio = BIO_new (BIO_s_mem ());
180 throw MiscError ("could not create memory BIO");
183 PEM_write_bio_X509 (bio, _certificate);
187 long int const data_length = BIO_get_mem_data (bio, &data);
188 for (long int i = 0; i < data_length; ++i) {
194 if (!with_begin_end) {
195 boost::replace_all (s, begin_certificate + "\n", "");
196 boost::replace_all (s, "\n" + end_certificate + "\n", "");
202 /** @return Certificate's issuer, in the form
203 * dnqualifier=<dnQualififer>,CN=<commonName>,OU=<organizationalUnitName>,O=<organizationName>
204 * and with + signs escaped to \+
207 Certificate::issuer () const
209 DCP_ASSERT (_certificate);
210 return name_for_xml (X509_get_issuer_name (_certificate));
214 Certificate::asn_to_utf8 (ASN1_STRING* s)
216 unsigned char* buf = 0;
217 ASN1_STRING_to_UTF8 (&buf, s);
218 string const u (reinterpret_cast<char *> (buf));
224 Certificate::get_name_part (X509_NAME* n, int nid)
227 p = X509_NAME_get_index_by_NID (n, nid, p);
231 return asn_to_utf8 (X509_NAME_ENTRY_get_data (X509_NAME_get_entry (n, p)));
235 Certificate::name_for_xml (X509_NAME* name)
239 BIO* bio = BIO_new (BIO_s_mem ());
241 throw MiscError ("could not create memory BIO");
244 X509_NAME_print_ex (bio, name, 0, XN_FLAG_RFC2253);
245 int n = BIO_pending (bio);
246 char* result = new char[n + 1];
247 n = BIO_read (bio, result, n);
259 Certificate::subject () const
261 DCP_ASSERT (_certificate);
263 return name_for_xml (X509_get_subject_name (_certificate));
267 Certificate::subject_common_name () const
269 DCP_ASSERT (_certificate);
271 return get_name_part (X509_get_subject_name (_certificate), NID_commonName);
275 Certificate::subject_organization_name () const
277 DCP_ASSERT (_certificate);
279 return get_name_part (X509_get_subject_name (_certificate), NID_organizationName);
283 Certificate::subject_organizational_unit_name () const
285 DCP_ASSERT (_certificate);
287 return get_name_part (X509_get_subject_name (_certificate), NID_organizationalUnitName);
291 Certificate::serial () const
293 DCP_ASSERT (_certificate);
295 ASN1_INTEGER* s = X509_get_serialNumber (_certificate);
298 BIGNUM* b = ASN1_INTEGER_to_BN (s, 0);
299 char* c = BN_bn2dec (b);
309 Certificate::thumbprint () const
311 DCP_ASSERT (_certificate);
313 uint8_t buffer[8192];
315 i2d_X509_CINF (_certificate->cert_info, &p);
316 unsigned int const length = p - buffer;
317 if (length > sizeof (buffer)) {
318 throw MiscError ("buffer too small to generate thumbprint");
323 SHA1_Update (&sha, buffer, length);
325 SHA1_Final (digest, &sha);
327 char digest_base64[64];
328 return Kumu::base64encode (digest, 20, digest_base64, 64);
331 /** @return RSA public key from this Certificate. Caller must not free the returned value. */
333 Certificate::public_key () const
335 DCP_ASSERT (_certificate);
341 EVP_PKEY* key = X509_get_pubkey (_certificate);
343 throw MiscError ("could not get public key from certificate");
346 _public_key = EVP_PKEY_get1_RSA (key);
348 throw MiscError (String::compose ("could not get RSA public key (%1)", ERR_error_string (ERR_get_error(), 0)));
355 dcp::operator== (Certificate const & a, Certificate const & b)
357 return a.certificate() == b.certificate();
361 dcp::operator< (Certificate const & a, Certificate const & b)
363 return a.certificate() < b.certificate();
367 dcp::operator<< (ostream& s, Certificate const & c)
369 s << c.certificate();