Skip to content

Commit

Permalink
add more info for rsa cert
Browse files Browse the repository at this point in the history
  • Loading branch information
nothinux committed Mar 16, 2022
1 parent a6da3a8 commit 8512426
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions certify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
Expand Down Expand Up @@ -114,13 +115,6 @@ func ParseCertificate(cert []byte) (*x509.Certificate, error) {
func CertInfo(cert *x509.Certificate) string {
var buf bytes.Buffer

if cert.PublicKeyAlgorithm != x509.ECDSA {
buf.WriteString(fmt.Sprintf(
"Currently certify only support ECDSA Certificate, this certificate is %v Certificate\n\n",
cert.PublicKeyAlgorithm.String()),
)
}

buf.WriteString("Certificate\n")
buf.WriteString(fmt.Sprintf("%4sData:\n", ""))
buf.WriteString(fmt.Sprintf("%8sVersion: %d\n", "", cert.Version))
Expand All @@ -135,16 +129,22 @@ func CertInfo(cert *x509.Certificate) string {

buf.WriteString(fmt.Sprintf("%8sSubject: %v\n", "", strings.Replace(cert.Subject.String(), ",", ", ", -1)))

buf.WriteString(fmt.Sprintf("%8sSubject Public Key Info:\n", ""))
buf.WriteString(fmt.Sprintf("%12sPublic Key Algorithm: %v\n", "", cert.PublicKeyAlgorithm))
if cert.PublicKeyAlgorithm == x509.ECDSA {
buf.WriteString(fmt.Sprintf("%8sSubject Public Key Info:\n", ""))
buf.WriteString(fmt.Sprintf("%12sPublic Key Algorithm: %v\n", "", cert.PublicKeyAlgorithm))

if ecdsakey, ok := cert.PublicKey.(*ecdsa.PublicKey); ok {
buf.WriteString(fmt.Sprintf("%16sPublic Key: (%d bit)\n", "", ecdsakey.Params().BitSize))
buf.WriteString(fmt.Sprintf("%16sNIST Curve: %s\n", "", ecdsakey.Params().Name))
}
}

if cert.PublicKeyAlgorithm == x509.RSA {
if rsakey, ok := cert.PublicKey.(*rsa.PublicKey); ok {
buf.WriteString(fmt.Sprintf("%16sRSA Public-Key: (%d bit)\n", "", rsakey.N.BitLen()))
buf.WriteString(fmt.Sprintf("%16sExponent: %d (%#x)\n", "", rsakey.E, rsakey.E))
}
}

buf.WriteString(fmt.Sprintf("%8sX509v3 extensions:\n", ""))
buf.WriteString(fmt.Sprintf("%12sX509v3 Extended Key Usage:\n", ""))
buf.WriteString(fmt.Sprintf("%16s%v\n", "", parseExtKeyUsage(cert.ExtKeyUsage)))
Expand Down

0 comments on commit 8512426

Please sign in to comment.