From 2c8a9c25eddf30a7c36034f8de76f635496cd9b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Thu, 12 Feb 2015 14:50:50 -0500 Subject: [PATCH] crypto: add Certificate::getId(), Certificate::getUID() --- include/opendht/crypto.h | 12 +++++++++--- src/crypto.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index e3cabf90..54839474 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -122,13 +122,19 @@ struct Certificate : public Serializable { Certificate(const Blob& crt); Certificate(Certificate&& o) noexcept : cert(o.cert) { o.cert = nullptr; }; Certificate& operator=(Certificate&& o) noexcept; - ~Certificate(); - operator bool() const { return cert; } - PublicKey getPublicKey() const; + void pack(Blob& b) const override; void unpack(Blob::const_iterator& begin, Blob::const_iterator& end) override; + operator bool() const { return cert; } + PublicKey getPublicKey() const; + + /** Same as getPublicKey().getId() */ + InfoHash getId() const; + + std::string getUID() const; + gnutls_x509_crt_t cert {}; private: Certificate(const Certificate&) = delete; diff --git a/src/crypto.cpp b/src/crypto.cpp index 60c30712..948544a5 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -312,7 +312,8 @@ PublicKey::getId() const { InfoHash id; size_t sz = id.size(); - gnutls_pubkey_get_key_id(pk, 0, id.data(), &sz); + if (gnutls_pubkey_get_key_id(pk, 0, id.data(), &sz) != GNUTLS_E_SUCCESS || sz != id.size()) + return {}; return id; } @@ -380,6 +381,31 @@ Certificate::getPublicKey() const return pk_ret; } +InfoHash +Certificate::getId() const +{ + if (not cert) + return {}; + InfoHash id; + size_t sz = id.size(); + if (gnutls_x509_crt_get_key_id(cert, 0, id.data(), &sz) != GNUTLS_E_SUCCESS || sz != id.size()) + throw std::runtime_error("Can't get certificate public key ID."); + return id; +} + +std::string +Certificate::getUID() const +{ + std::string uid; + uid.resize(512); + size_t uid_sz = uid.size(); + int ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_LDAP_UID, 0, 0, &(*uid.begin()), &uid_sz); + if (ret != GNUTLS_E_SUCCESS) + return {}; + uid.resize(uid_sz); + return uid; +} + PrivateKey PrivateKey::generate(unsigned key_length) {