Verified Commit 7693d16c authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Add info command

Add new command `info` which will print information about each
certificate for the given path.
parent 0107a271
package main
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/urfave/cli"
)
func makeInfoCMD() cli.Command {
return cli.Command{
Name: "info",
Usage: "Print information about each cert in a chain.",
Flags: []cli.Flag{
cli.StringFlag{Name: "path"},
},
Action: infoAction,
}
}
func infoAction(c *cli.Context) error {
CAChainPath := c.String("path")
if CAChainPath == "" {
return errors.New("--path not defined")
}
chain, err := ioutil.ReadFile(CAChainPath)
if err != nil {
return fmt.Errorf("read CA Chain in %s: %w", CAChainPath, err)
}
for {
var block *pem.Block
block, chain = pem.Decode(chain)
if block == nil {
break
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("parsing pem block: %v", err)
}
_, _ = fmt.Fprintln(os.Stdout, "---")
_, _ = fmt.Fprintf(os.Stdout, "Serial: %x\n", cert.SerialNumber)
_, _ = fmt.Fprintf(os.Stdout, "Subject: %v\n", cert.Subject)
_, _ = fmt.Fprintf(os.Stdout, "Issuer: %v\n", cert.Issuer)
_, _ = fmt.Fprintf(os.Stdout, "Valid: from %v to %v\n", cert.NotBefore, cert.NotAfter)
_, _ = fmt.Fprintf(os.Stdout, "Issuer URLs: %v\n", cert.IssuingCertificateURL)
_, _ = fmt.Fprintf(os.Stdout, "SelfSigned: %v\n", cert.CheckSignatureFrom(cert) == nil)
if chain == nil || len(chain) < 1 {
break
}
}
return nil
}
......@@ -15,6 +15,7 @@ func main() {
app.Commands = []cli.Command{
makeSaveCMD(),
makeInfoCMD(),
}
err := app.Run(os.Args)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment