| 1 | package certificate |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "encoding/pem" |
| 6 | "io/ioutil" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/pkg/errors" |
| 10 | "github.com/smallstep/cli/errs" |
| 11 | "github.com/smallstep/cli/flags" |
| 12 | zx509 "github.com/smallstep/zcrypto/x509" |
| 13 | "github.com/smallstep/zlint" |
| 14 | "github.com/urfave/cli" |
| 15 | ) |
| 16 | |
| 17 | func lintCommand() cli.Command { |
| 18 | return cli.Command{ |
| 19 | Name: "lint", |
| 20 | Action: cli.ActionFunc(lintAction), |
| 21 | Usage: `lint certificate details`, |
| 22 | UsageText: `**step certificate lint** <crt_file> [**--roots**=<root-bundle>] |
| 23 | [**--servername**=<servername>]`, |
| 24 | Description: `**step certificate lint** checks a certificate for common |
| 25 | errors and outputs the result in JSON format. |
| 26 | |
| 27 | ## POSITIONAL ARGUMENTS |
| 28 | |
| 29 | <crt_file> |
| 30 | : Path to a certificate or certificate signing request (CSR) to lint. |
| 31 | |
| 32 | ## EXIT CODES |
| 33 | |
| 34 | This command returns 0 on success and \>0 if any error occurs. |
| 35 | |
| 36 | ## EXAMPLES |
| 37 | |
| 38 | ''' |
| 39 | $ step certificate lint ./certificate.crt |
| 40 | ''' |
| 41 | |
| 42 | Lint a remote certificate (using the default root certificate bundle to verify the server): |
| 43 | |
| 44 | ''' |
| 45 | $ step certificate lint https://smallstep.com |
| 46 | ''' |
| 47 | |
| 48 | Lint a remote certificate using a custom root certificate to verify the server: |
| 49 | |
| 50 | ''' |
| 51 | $ step certificate lint https://smallstep.com --roots ./certificate.crt |
| 52 | ''' |
| 53 | |
| 54 | Lint a remote certificate using a custom list of root certificates to verify the server: |
| 55 | |
| 56 | ''' |
| 57 | $ step certificate lint https://smallstep.com \ |
| 58 | --roots "./certificate.crt,./certificate2.crt,/certificate3.crt" |
| 59 | ''' |
| 60 | |
| 61 | Lint a remote certificate using a custom directory of root certificates to verify the server: |
| 62 | |
| 63 | ''' |
| 64 | $ step certificate lint https://smallstep.com --roots "./path/to/certificates/" |
| 65 | ''' |
| 66 | `, |
| 67 | Flags: []cli.Flag{ |
| 68 | cli.StringFlag{ |
| 69 | Name: "roots", |
| 70 | Usage: `Root certificate(s) that will be used to verify the |
| 71 | authenticity of the remote server. |
| 72 | |
| 73 | : <roots> is a case-sensitive string and may be one of: |
| 74 | |
| 75 | **file** |
| 76 | : Relative or full path to a file. All certificates in the file will be used for path validation. |
| 77 | |
| 78 | **list of files** |
| 79 | : Comma-separated list of relative or full file paths. Every PEM encoded certificate from each file will be used for path validation. |
| 80 | |
| 81 | **directory** |
| 82 | : Relative or full path to a directory. Every PEM encoded certificate from each file in the directory will be used for path validation.`, |
| 83 | }, |
| 84 | cli.BoolFlag{ |
| 85 | Name: "insecure", |
| 86 | Usage: `Use an insecure client to retrieve a remote peer certificate. Useful for |
| 87 | debugging invalid certificates remotely.`, |
| 88 | }, |
| 89 | flags.ServerName, |
| 90 | }, |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func lintAction(ctx *cli.Context) error { |
| 95 | if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | |
| 99 | var ( |
| 100 | crtFile = ctx.Args().Get(0) |
| 101 | roots = ctx.String("roots") |
| 102 | serverName = ctx.String("servername") |
| 103 | insecure = ctx.Bool("insecure") |
| 104 | block *pem.Block |
| 105 | ) |
| 106 | if addr, isURL, err := trimURL(crtFile); err != nil { |
| 107 | return err |
| 108 | } else if isURL { |
| 109 | peerCertificates, err := getPeerCertificates(addr, serverName, roots, insecure) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | crt := peerCertificates[0] |
| 114 | block = &pem.Block{ |
| 115 | Type: "CERTIFICATE", |
| 116 | Bytes: crt.Raw, |
| 117 | } |
| 118 | } else { |
| 119 | crtBytes, err := ioutil.ReadFile(crtFile) |
| 120 | if err != nil { |
| 121 | return errs.FileError(err, crtFile) |
| 122 | } |
| 123 | block, _ = pem.Decode(crtBytes) |
| 124 | if block == nil { |
| 125 | return errors.Errorf("could not parse certificate file '%s'", crtFile) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | zcrt, err := zx509.ParseCertificate(block.Bytes) |
| 130 | if err != nil { |
| 131 | return errors.WithStack(err) |
| 132 | } |
| 133 | zlintResult := zlint.LintCertificate(zcrt) |
| 134 | b, err := json.MarshalIndent(struct { |
| 135 | *zlint.ResultSet |
| 136 | }{zlintResult}, "", " ") |
| 137 | if err != nil { |
| 138 | return errors.WithStack(err) |
| 139 | } |
| 140 | os.Stdout.Write(b) |
| 141 | |
| 142 | return nil |
| 143 | } |