From f05f58c30cacb5f1114e619f9ea6088ebaa90b96 Mon Sep 17 00:00:00 2001 From: Hiroyuki Date: Thu, 25 Mar 2021 22:12:07 -0400 Subject: [PATCH] Add support for the bit sizes of the public keys --- routes/get.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/routes/get.go b/routes/get.go index 47564c1..0c0bbce 100644 --- a/routes/get.go +++ b/routes/get.go @@ -1,6 +1,9 @@ package routes import ( + "crypto/ecdsa" + "crypto/ed25519" + "crypto/rsa" "crypto/sha1" "crypto/tls" "crypto/x509" @@ -125,6 +128,25 @@ func GetCertificateInformationEncoded(c *gin.Context) { sum := sha1.Sum(certificate.Raw) + var bitLength int + + switch certificate.PublicKeyAlgorithm { + case x509.RSA: + if rsaKey, ok := certificate.PublicKey.(*rsa.PublicKey); ok { + bitLength = rsaKey.N.BitLen() + } else { + panic("expected rsa.PublicKey for type x509.RSA") + } + case x509.ECDSA: + if ecdsaKey, ok := certificate.PublicKey.(*ecdsa.PublicKey); ok { + bitLength = ecdsaKey.Params().BitSize + } else { + panic("expected ecdsa.PublicKey for type x509.ECDSA") + } + case x509.Ed25519: + bitLength = ed25519.PublicKeySize + } + c.JSON(http.StatusOK, gin.H{ "status": true, "subject": gin.H{ @@ -153,6 +175,7 @@ func GetCertificateInformationEncoded(c *gin.Context) { "extendedKeyUsageAsText": extendedKeyUsagesText, "san": certificate.DNSNames, "fingerprint": hex.EncodeToString(sum[:]), + "bitLength": bitLength, }) } @@ -271,6 +294,23 @@ func GetCertificateInfo(c *gin.Context) { sum := sha1.Sum(certificate.Raw) + var bitLength int + + switch certificate.PublicKeyAlgorithm { + case x509.RSA: + if rsaKey, ok := certificate.PublicKey.(*rsa.PublicKey); ok { + bitLength = rsaKey.N.BitLen() + } else { + panic("expected rsa.PublicKey for type x509.RSA") + } + case x509.ECDSA: + if ecdsaKey, ok := certificate.PublicKey.(*ecdsa.PublicKey); ok { + bitLength = ecdsaKey.Params().BitSize + } else { + panic("expected ecdsa.PublicKey for type x509.ECDSA") + } + } + c.JSON(http.StatusOK, gin.H{ "status": true, "subject": gin.H{ @@ -306,6 +346,7 @@ func GetCertificateInfo(c *gin.Context) { "extendedKeyUsageAsText": extendedKeyUsagesText, "san": certificate.DNSNames, "fingerprint": hex.EncodeToString(sum[:]), + "bitLength": bitLength, "connection": gin.H{ "tlsVersion": tlsVersion, "cipherSuite": cipherSuite,