Skip to content

Commit

Permalink
add info command
Browse files Browse the repository at this point in the history
  • Loading branch information
yakuter committed Apr 16, 2022
1 parent ef11bec commit f79df0b
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 29 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ gossl key --bits 2048 --out private.key
gossl key --bits 2048 --out private.key --withpub
```

### info
`info` displays information about x509 certificate. Thanks [grantae](https://github.com/grantae) for great [certinfo](https://github.com/grantae/certinfo) tool which is used here.

```bash
gossl info cert.pem
```

### cert
`cert` command generates x509 SSL/TLS Certificate Request (CSR), Root CA and Certificate with provided private key.

Expand Down Expand Up @@ -136,3 +143,5 @@ gossl ssh-copy --pubkey /home/user/.ssh/id_rsa.pub --password passw@rd123 remote
1. Add generate command for generating private key, root ca and x509 certificates in one command
2. Add cert template format read from yaml file
3. Add certificate converter command like DER to PEM etc.
4. Add test for info command
5. Add test for CertFromFile function at utils package
59 changes: 59 additions & 0 deletions commands/info/info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package info

import (
"errors"
"fmt"
"log"

"github.com/yakuter/gossl/pkg/utils"

"github.com/grantae/certinfo"
"github.com/urfave/cli/v2"
)

// Remote commands
const (
CmdInfo = "info"
)

func Command() *cli.Command {
return &cli.Command{
Name: CmdInfo,
HelpName: CmdInfo,
Action: Action,
ArgsUsage: `[cert file path]`,
Usage: `displays information about certificate.`,
Description: `Displays information about x509 certificate.`,
Flags: Flags(),
}
}

func Flags() []cli.Flag {
return []cli.Flag{}
}

func Action(c *cli.Context) error {
if c.Args().Len() == 0 {
err := errors.New("cert file argument is not found")
log.Printf("%v", err)
return err
}

// Get certificate from file
certFilePath := c.Args().First()
cert, err := utils.CertFromFile(certFilePath)
if err != nil {
log.Printf("Failed to get cert from file %s CAs error: %v", certFilePath, err)
return err
}

// Print the certificate
result, err := certinfo.CertificateText(cert)
if err != nil {
log.Printf("Failed to get cert info from cert error: %v", err)
return err
}

fmt.Println(result)
return nil
}
31 changes: 3 additions & 28 deletions commands/verify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package verify
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log"
"net/http"
"os"

"github.com/yakuter/gossl/pkg/utils"

"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -79,7 +80,7 @@ func Action(c *cli.Context) error {

// Verify cert file
if c.IsSet(flagCertFile) {
cert, err := x509Cert(c.String(flagCertFile))
cert, err := utils.CertFromFile(c.String(flagCertFile))
if err != nil {
log.Printf("Failed to get cert from file %s CAs error: %v", c.String(flagCertFile), err)
return err
Expand Down Expand Up @@ -124,32 +125,6 @@ func rootCAs(caFilePath string) (*x509.CertPool, error) {
return roots, nil
}

func x509Cert(certFilePath string) (*x509.Certificate, error) {
// Read cert file
certFileBytes, err := os.ReadFile(certFilePath)
if err != nil {
log.Printf("Failed to read cert file %q error: %v", certFilePath, err)
return nil, err
}

// Decode PEM encoded cert file
block, _ := pem.Decode(certFileBytes)
if block == nil {
err = errors.New("block is nil")
log.Printf("Failed to decode PEM encoded cert file %q error: %v", certFilePath, err)
return nil, err
}

// Parse x509 certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Printf("Failed to parse x509 certificate from cert file %q error: %v", certFilePath, err)
return nil, err
}

return cert, nil
}

func verifyCertWithCA(c *cli.Context, cert *x509.Certificate, roots *x509.CertPool) error {
// Set verification options
opts := x509.VerifyOptions{
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/yakuter/gossl/commands/cert"
"github.com/yakuter/gossl/commands/help"
"github.com/yakuter/gossl/commands/info"
"github.com/yakuter/gossl/commands/key"
"github.com/yakuter/gossl/commands/ssh"
"github.com/yakuter/gossl/commands/ssh_copy"
Expand All @@ -33,9 +34,10 @@ func main() {
func Commands(reader io.Reader) []*cli.Command {
return []*cli.Command{
help.Command(),
verify.Command(),
key.Command(),
cert.Command(reader),
info.Command(),
verify.Command(),
ssh.Command(),
ssh_copy.Command(ssh_copy.StdinPasswordReader{}),
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,32 @@ func PrivateKeyFromPEMFile(keyFilePath string) (*rsa.PrivateKey, error) {
return key, nil
}

func CertFromFile(certFilePath string) (*x509.Certificate, error) {
// Read cert file
certFileBytes, err := os.ReadFile(certFilePath)
if err != nil {
log.Printf("Failed to read cert file %q error: %v", certFilePath, err)
return nil, err
}

// Decode PEM encoded cert file
block, _ := pem.Decode(certFileBytes)
if block == nil {
err = errors.New("block is nil")
log.Printf("Failed to decode PEM encoded cert file %q error: %v", certFilePath, err)
return nil, err
}

// Parse x509 certificate
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
log.Printf("Failed to parse x509 certificate from cert file %q error: %v", certFilePath, err)
return nil, err
}

return cert, nil
}

func ReadInputs(questions []string, reader io.Reader) ([]string, error) {
answers := make([]string, len(questions))
scanner := bufio.NewScanner(reader)
Expand Down

0 comments on commit f79df0b

Please sign in to comment.