certinfo

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 16 Imported by: 1

README

Certificate Information for Go

A golang tool for printing x509 TLS certificates in a format similar to OpenSSL.

Installation

go get github.com/grantae/certinfo

Usage

Print a certificate from a website
package main

import (
  "crypto/tls"
  "fmt"
  "github.com/grantae/certinfo"
  "log"
)

func main() {
  // Connect to google.com
  cfg := tls.Config{}
  conn, err := tls.Dial("tcp", "google.com:443", &cfg)
  if err != nil {
    log.Fatalln("TLS connection failed: " + err.Error())
  }
  // Grab the last certificate in the chain
  certChain := conn.ConnectionState().PeerCertificates
  cert := certChain[len(certChain)-1]

  // Print the certificate
  result, err := certinfo.CertificateText(cert)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Print(result)
}
Print a PEM-encoded certificate from a file
package main

import (
  "crypto/x509"
  "encoding/pem"
  "fmt"
  "github.com/grantae/certinfo"
  "io/ioutil"
  "log"
)

func main() {
  // Read and parse the PEM certificate file
  pemData, err := ioutil.ReadFile("cert.pem")
  if err != nil {
    log.Fatal(err)
  }
  block, rest := pem.Decode([]byte(pemData))
  if block == nil || len(rest) > 0 {
    log.Fatal("Certificate decoding error")
  }
  cert, err := x509.ParseCertificate(block.Bytes)
  if err != nil {
    log.Fatal(err)
  }

  // Print the certificate
  result, err := certinfo.CertificateText(cert)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Print(result)
}

Testing

go test github.com/grantae/certinfo

This compares several PEM-encoded certificates with their expected outputs.

License

MIT -- see LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CertificateRequestShortText

func CertificateRequestShortText(cr *x509.CertificateRequest) (string, error)

CertificateRequestShortText returns the human-readable string representation of the given certificate request using a short and friendly format.

func CertificateRequestText

func CertificateRequestText(csr *x509.CertificateRequest) (string, error)

CertificateRequestText returns a human-readable string representation of the certificate request csr. The format is similar (but not identical) to the OpenSSL way of printing certificates.

func CertificateShortText

func CertificateShortText(cert *x509.Certificate) (string, error)
func toBase64(b []byte) string {
	return base64.StdEncoding.EncodeToString(b)
}
func printSCTSignature(sig ct.DigitallySigned, buf *bytes.Buffer) {
	buf.WriteString(fmt.Sprintf("%20sSignature Algorithm: %s-%s", "", sig.Algorithm.Hash, sig.Algorithm.Signature))
	for i, val := range sig.Signature {
		if (i % 18) == 0 {
			buf.WriteString(fmt.Sprintf("\n%22s", ""))
		}
		buf.WriteString(fmt.Sprintf("%02x", val))
		if i != len(sig.Signature)-1 {
			buf.WriteString(":")
		}
	}
	buf.WriteString("\n")
}

CertificateShortText returns the human-readable string representation of the given cert using a short and friendly format.

func CertificateText

func CertificateText(cert *x509.Certificate) (string, error)

CertificateText returns a human-readable string representation of the certificate cert. The format is similar (but not identical) to the OpenSSL way of printing certificates.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL