diff options
| author | Carl Hetherington <cth@carlh.net> | 2013-09-19 20:44:42 +0100 |
|---|---|---|
| committer | Carl Hetherington <cth@carlh.net> | 2013-09-19 20:44:42 +0100 |
| commit | 8d6c3c9ae554430582dcb016897e87f6d04d5d78 (patch) | |
| tree | 5b329eeb60fe7372cc89740d4cf8fa1d5fe51614 /src/certificates.cc | |
| parent | 827901db3d834465b1121c9f8041b9faf4923ec9 (diff) | |
Various encryption-related stuff.
Diffstat (limited to 'src/certificates.cc')
| -rw-r--r-- | src/certificates.cc | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/src/certificates.cc b/src/certificates.cc index 3566fba0..6d9c449d 100644 --- a/src/certificates.cc +++ b/src/certificates.cc @@ -43,7 +43,7 @@ Certificate::Certificate (X509* c) } -Certificate::Certificate (string const & filename) +Certificate::Certificate (boost::filesystem::path filename) : _certificate (0) { FILE* f = fopen (filename.c_str(), "r"); @@ -56,13 +56,52 @@ Certificate::Certificate (string const & filename) } } +Certificate::Certificate (string cert) +{ + read_string (cert); +} + +Certificate::Certificate (Certificate const & other) +{ + read_string (other.certificate (true)); +} + +void +Certificate::read_string (string cert) +{ + BIO* bio = BIO_new_mem_buf (const_cast<char *> (cert.c_str ()), -1); + if (!bio) { + throw MiscError ("could not create memory BIO"); + } + + _certificate = PEM_read_bio_X509 (bio, 0, 0, 0); + if (!_certificate) { + throw MiscError ("could not read X509 certificate from memory BIO"); + } + + BIO_free (bio); +} + Certificate::~Certificate () { X509_free (_certificate); } +Certificate & +Certificate::operator= (Certificate const & other) +{ + if (this == &other) { + return *this; + } + + X509_free (_certificate); + read_string (other.certificate ()); + + return *this; +} + string -Certificate::certificate () const +Certificate::certificate (bool with_begin_end) const { assert (_certificate); @@ -82,8 +121,11 @@ Certificate::certificate () const BIO_free (bio); - boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", ""); - boost::replace_all (s, "\n-----END CERTIFICATE-----\n", ""); + if (!with_begin_end) { + boost::replace_all (s, "-----BEGIN CERTIFICATE-----\n", ""); + boost::replace_all (s, "\n-----END CERTIFICATE-----\n", ""); + } + return s; } |
