From 6e01255bc00da5ea75c6ee48526743ba56d1f8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 26 Feb 2015 10:36:14 -0500 Subject: [PATCH] crypto: add Certificate::toString --- include/opendht/crypto.h | 7 ++++++- src/crypto.cpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 1466a4d7..8f0c574a 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -107,7 +107,7 @@ struct PrivateKey /** * Generate a new RSA key pair * @param key_length : size of the modulus in bits - * Recommended values: 2048, 4096, 8192 + * Recommended values: 4096, 8192 */ static PrivateKey generate(unsigned key_length = 4096); @@ -139,6 +139,11 @@ struct Certificate : public Serializable { std::string getUID() const; + /** + * PEM encoded certificate + */ + std::string toString() const; + gnutls_x509_crt_t cert {}; private: Certificate(const Certificate&) = delete; diff --git a/src/crypto.cpp b/src/crypto.cpp index 99f5bb3f..7320c512 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -416,6 +416,20 @@ Certificate::getUID() const return uid; } +std::string +Certificate::toString() const +{ + std::string str; + size_t buf_sz = 8192; + str.resize(buf_sz); + int err = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, &(*str.begin()), &buf_sz); + if (err != GNUTLS_E_SUCCESS) { + std::cerr << "Could not export certificate - " << gnutls_strerror(err) << std::endl; + } + str.resize(buf_sz); + return str; +} + PrivateKey PrivateKey::generate(unsigned key_length) { -- GitLab