crypto: add Certificate::getId(), Certificate::getUID()

This commit is contained in:
Adrien Béraud committed 2015-02-12 14:50:50 -05:00
1 parent 1883d7f387
commit 2c8a9c25ed
2 files changed
+36 -4

No files matched your search

+9 -3
View File
@@ -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;
+27 -1
View File
@@ -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)
{