| 1 |
package certificate |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/x509" |
| 6 |
"encoding/json" |
| 7 |
"encoding/pem" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/smallstep/certinfo" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/flags" |
| 16 |
"github.com/smallstep/cli/utils" |
| 17 |
zx509 "github.com/smallstep/zcrypto/x509" |
| 18 |
"github.com/urfave/cli" |
| 19 |
) |
| 20 |
|
| 21 |
func inspectCommand() cli.Command { |
| 22 |
return cli.Command{ |
| 23 |
Name: "inspect", |
| 24 |
Action: cli.ActionFunc(inspectAction), |
| 25 |
Usage: `print certificate or CSR details in human readable format`, |
| 26 |
UsageText: `**step certificate inspect** <crt_file> |
| 27 |
[**--bundle**] [**--short**] [**--format**=<format>] [**--roots**=<root-bundle>] |
| 28 |
[**--servername**=<servername>]`, |
| 29 |
Description: `**step certificate inspect** prints the details of a certificate |
| 30 |
or CSR in a human readable format. Output from the inspect command is printed to |
| 31 |
STDERR instead of STDOUT. This is an intentional barrier to accidental |
| 32 |
misuse: scripts should never rely on the contents of an unvalidated certificate. |
| 33 |
For scripting purposes, use **step certificate verify**. |
| 34 |
|
| 35 |
If crt_file contains multiple certificates (i.e., it is a certificate "bundle") |
| 36 |
the first certificate in the bundle will be output. Pass the --bundle option to |
| 37 |
print all certificates in the order in which they appear in the bundle. |
| 38 |
|
| 39 |
## POSITIONAL ARGUMENTS |
| 40 |
|
| 41 |
<crt_file> |
| 42 |
: Path to a certificate or certificate signing request (CSR) to inspect. A hyphen ("-") indicates STDIN as <crt_file>. |
| 43 |
|
| 44 |
## EXIT CODES |
| 45 |
|
| 46 |
This command returns 0 on success and \>0 if any error occurs. |
| 47 |
|
| 48 |
## EXAMPLES |
| 49 |
|
| 50 |
Inspect a local certificate (default to text format): |
| 51 |
|
| 52 |
''' |
| 53 |
$ step certificate inspect ./certificate.crt |
| 54 |
''' |
| 55 |
|
| 56 |
Inspect a local certificate bundle (default to text format): |
| 57 |
|
| 58 |
''' |
| 59 |
$ step certificate inspect ./certificate-bundle.crt --bundle |
| 60 |
''' |
| 61 |
|
| 62 |
Inspect a local certificate in json format: |
| 63 |
|
| 64 |
''' |
| 65 |
$ step certificate inspect ./certificate.crt --format json |
| 66 |
''' |
| 67 |
|
| 68 |
Inspect a local certificate bundle in json format: |
| 69 |
|
| 70 |
''' |
| 71 |
$ step certificate inspect ./certificate.crt --format json --bundle |
| 72 |
''' |
| 73 |
|
| 74 |
Inspect a remote certificate (using the default root certificate bundle to verify the server): |
| 75 |
|
| 76 |
''' |
| 77 |
$ step certificate inspect https://smallstep.com |
| 78 |
''' |
| 79 |
|
| 80 |
Inspect an invalid remote certificate: |
| 81 |
|
| 82 |
''' |
| 83 |
$ step certificate inspect --insecure https://expired.badssl.com |
| 84 |
''' |
| 85 |
|
| 86 |
Inspect a remote certificate chain (using the default root certificate bundle to verify the server): |
| 87 |
|
| 88 |
''' |
| 89 |
$ step certificate inspect https://google.com --bundle |
| 90 |
''' |
| 91 |
|
| 92 |
Inspect a remote certificate using a custom root certificate to verify the server: |
| 93 |
|
| 94 |
''' |
| 95 |
$ step certificate inspect https://smallstep.com --roots ./root-ca.crt |
| 96 |
''' |
| 97 |
|
| 98 |
Inspect a remote certificate using a custom list of root certificates to verify the server: |
| 99 |
|
| 100 |
''' |
| 101 |
$ step certificate inspect https://smallstep.com \ |
| 102 |
--roots "./root-ca.crt,./root-ca2.crt,/root-ca3.crt" |
| 103 |
''' |
| 104 |
|
| 105 |
Inspect a remote certificate using a custom directory of root certificates to verify the server: |
| 106 |
|
| 107 |
''' |
| 108 |
$ step certificate inspect https://smallstep.com \ |
| 109 |
--roots "./path/to/root/certificates/" |
| 110 |
''' |
| 111 |
|
| 112 |
Inspect a remote certificate chain in json format using a custom directory of |
| 113 |
root certificates to verify the server: |
| 114 |
|
| 115 |
''' |
| 116 |
$ step certificate inspect https://google.com --format json \ |
| 117 |
--roots "./path/to/root/certificates/" --bundle |
| 118 |
''' |
| 119 |
|
| 120 |
Inspect a remote certificate chain in PEM format: |
| 121 |
|
| 122 |
''' |
| 123 |
$ step certificate inspect https://smallstep.com --format pem --bundle |
| 124 |
''' |
| 125 |
|
| 126 |
Inspect a local CSR in text format (default): |
| 127 |
|
| 128 |
''' |
| 129 |
$ step certificate inspect foo.csr |
| 130 |
''' |
| 131 |
|
| 132 |
Inspect a local CSR in json: |
| 133 |
|
| 134 |
''' |
| 135 |
$ step certificate inspect foo.csr --format json |
| 136 |
''' |
| 137 |
`, |
| 138 |
Flags: []cli.Flag{ |
| 139 |
cli.StringFlag{ |
| 140 |
Name: "format", |
| 141 |
Value: "text", |
| 142 |
Usage: `The output format for printing the introspection details. |
| 143 |
|
| 144 |
: <format> is a string and must be one of: |
| 145 |
|
| 146 |
**text** |
| 147 |
: Print output in unstructured text suitable for a human to read. |
| 148 |
|
| 149 |
**json** |
| 150 |
: Print output in JSON format. |
| 151 |
|
| 152 |
**pem** |
| 153 |
: Print output in PEM format.`, |
| 154 |
}, |
| 155 |
cli.StringFlag{ |
| 156 |
Name: "roots", |
| 157 |
Usage: `Root certificate(s) that will be used to verify the |
| 158 |
authenticity of the remote server. |
| 159 |
|
| 160 |
: <roots> is a case-sensitive string and may be one of: |
| 161 |
|
| 162 |
**file** |
| 163 |
: Relative or full path to a file. All certificates in the file will be used for path validation. |
| 164 |
|
| 165 |
**list of files** |
| 166 |
: Comma-separated list of relative or full file paths. Every PEM encoded certificate from each file will be used for path validation. |
| 167 |
|
| 168 |
**directory** |
| 169 |
: Relative or full path to a directory. Every PEM encoded certificate from each file in the directory will be used for path validation.`, |
| 170 |
}, |
| 171 |
cli.BoolFlag{ |
| 172 |
Name: `bundle`, |
| 173 |
Usage: `Print all certificates in the order in which they appear in the bundle. |
| 174 |
If the output format is 'json' then output a list of certificates, even if |
| 175 |
the bundle only contains one certificate. This flag will result in an error |
| 176 |
if the input bundle includes any PEM that does not have type CERTIFICATE.`, |
| 177 |
}, |
| 178 |
cli.BoolFlag{ |
| 179 |
Name: "short", |
| 180 |
Usage: "Print the certificate or CSR details in shorter and more friendly format.", |
| 181 |
}, |
| 182 |
cli.BoolFlag{ |
| 183 |
Name: "insecure", |
| 184 |
Usage: `Use an insecure client to retrieve a remote peer certificate. Useful for |
| 185 |
debugging invalid certificates remotely.`, |
| 186 |
}, |
| 187 |
flags.ServerName, |
| 188 |
}, |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
func inspectAction(ctx *cli.Context) error { |
| 193 |
if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil { |
| 194 |
return err |
| 195 |
} |
| 196 |
|
| 197 |
var ( |
| 198 |
crtFile = ctx.Args().Get(0) |
| 199 |
bundle = ctx.Bool("bundle") |
| 200 |
format = ctx.String("format") |
| 201 |
roots = ctx.String("roots") |
| 202 |
serverName = ctx.String("servername") |
| 203 |
short = ctx.Bool("short") |
| 204 |
insecure = ctx.Bool("insecure") |
| 205 |
) |
| 206 |
|
| 207 |
|
| 208 |
if crtFile == "" { |
| 209 |
crtFile = "-" |
| 210 |
} |
| 211 |
|
| 212 |
if format != "text" && format != "json" && format != "pem" { |
| 213 |
return errs.InvalidFlagValue(ctx, "format", format, "text, json, pem") |
| 214 |
} |
| 215 |
if short && (format == "json" || format == "pem") { |
| 216 |
return errs.IncompatibleFlagWithFlag(ctx, "short", "format json") |
| 217 |
} |
| 218 |
|
| 219 |
var block *pem.Block |
| 220 |
var blocks []*pem.Block |
| 221 |
if addr, isURL, err := trimURL(crtFile); err != nil { |
| 222 |
return err |
| 223 |
} else if isURL { |
| 224 |
peerCertificates, err := getPeerCertificates(addr, serverName, roots, insecure) |
| 225 |
if err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
for _, crt := range peerCertificates { |
| 229 |
blocks = append(blocks, &pem.Block{ |
| 230 |
Type: "CERTIFICATE", |
| 231 |
Bytes: crt.Raw, |
| 232 |
}) |
| 233 |
} |
| 234 |
} else { |
| 235 |
crtBytes, err := utils.ReadFile(crtFile) |
| 236 |
if err != nil { |
| 237 |
return errs.FileError(err, crtFile) |
| 238 |
} |
| 239 |
if bytes.HasPrefix(crtBytes, []byte("-----BEGIN ")) { |
| 240 |
for len(crtBytes) > 0 { |
| 241 |
block, crtBytes = pem.Decode(crtBytes) |
| 242 |
if block == nil { |
| 243 |
break |
| 244 |
} |
| 245 |
if bundle && block.Type != "CERTIFICATE" { |
| 246 |
return errors.Errorf("certificate bundle %s contains an unexpected PEM block of type %s\n\n expected type: CERTIFICATE", |
| 247 |
crtFile, block.Type) |
| 248 |
} |
| 249 |
blocks = append(blocks, block) |
| 250 |
} |
| 251 |
} else { |
| 252 |
if block = derToPemBlock(crtBytes); block == nil { |
| 253 |
return errors.Errorf("%s contains an invalid PEM block", crtFile) |
| 254 |
} |
| 255 |
blocks = append(blocks, block) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if !bundle { |
| 261 |
blocks = []*pem.Block{blocks[0]} |
| 262 |
} |
| 263 |
|
| 264 |
switch blocks[0].Type { |
| 265 |
case "CERTIFICATE": |
| 266 |
return inspectCertificates(ctx, blocks, os.Stdout) |
| 267 |
case "CERTIFICATE REQUEST", "NEW CERTIFICATE REQUEST": |
| 268 |
return inspectCertificateRequest(ctx, blocks[0]) |
| 269 |
default: |
| 270 |
return errors.Errorf("Invalid PEM type in %s. Expected [CERTIFICATE|CERTIFICATE REQUEST] but got %s)", crtFile, block.Type) |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func inspectCertificates(ctx *cli.Context, blocks []*pem.Block, w io.Writer) error { |
| 275 |
format, short := ctx.String("format"), ctx.Bool("short") |
| 276 |
switch format { |
| 277 |
case "text": |
| 278 |
var text string |
| 279 |
for _, block := range blocks { |
| 280 |
crt, err := x509.ParseCertificate(block.Bytes) |
| 281 |
if err != nil { |
| 282 |
return errors.WithStack(err) |
| 283 |
} |
| 284 |
if short { |
| 285 |
if text, err = certinfo.CertificateShortText(crt); err != nil { |
| 286 |
return err |
| 287 |
} |
| 288 |
} else { |
| 289 |
if text, err = certinfo.CertificateText(crt); err != nil { |
| 290 |
return err |
| 291 |
} |
| 292 |
} |
| 293 |
fmt.Fprint(w, text) |
| 294 |
} |
| 295 |
return nil |
| 296 |
case "json": |
| 297 |
var v interface{} |
| 298 |
if len(blocks) == 1 { |
| 299 |
zcrt, err := zx509.ParseCertificate(blocks[0].Bytes) |
| 300 |
if err != nil { |
| 301 |
return errors.WithStack(err) |
| 302 |
} |
| 303 |
v = struct{ *zx509.Certificate }{zcrt} |
| 304 |
} else { |
| 305 |
var zcrts []*zx509.Certificate |
| 306 |
for _, block := range blocks { |
| 307 |
zcrt, err := zx509.ParseCertificate(block.Bytes) |
| 308 |
if err != nil { |
| 309 |
return errors.WithStack(err) |
| 310 |
} |
| 311 |
zcrts = append(zcrts, zcrt) |
| 312 |
} |
| 313 |
v = zcrts |
| 314 |
} |
| 315 |
enc := json.NewEncoder(w) |
| 316 |
enc.SetIndent("", " ") |
| 317 |
err := enc.Encode(v) |
| 318 |
if err != nil { |
| 319 |
return errors.WithStack(err) |
| 320 |
} |
| 321 |
return nil |
| 322 |
case "pem": |
| 323 |
for _, block := range blocks { |
| 324 |
err := pem.Encode(w, block) |
| 325 |
if err != nil { |
| 326 |
return errors.WithStack(err) |
| 327 |
} |
| 328 |
} |
| 329 |
return nil |
| 330 |
default: |
| 331 |
return errs.InvalidFlagValue(ctx, "format", format, "text, json, pem") |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
func inspectCertificateRequest(ctx *cli.Context, block *pem.Block) error { |
| 336 |
format, short := ctx.String("format"), ctx.Bool("short") |
| 337 |
switch format { |
| 338 |
case "text": |
| 339 |
var text string |
| 340 |
csr, err := x509.ParseCertificateRequest(block.Bytes) |
| 341 |
if err != nil { |
| 342 |
return errors.WithStack(err) |
| 343 |
} |
| 344 |
if short { |
| 345 |
text, err = certinfo.CertificateRequestShortText(csr) |
| 346 |
if err != nil { |
| 347 |
return err |
| 348 |
} |
| 349 |
} else { |
| 350 |
text, err = certinfo.CertificateRequestText(csr) |
| 351 |
if err != nil { |
| 352 |
return err |
| 353 |
} |
| 354 |
} |
| 355 |
fmt.Print(text) |
| 356 |
return nil |
| 357 |
case "json": |
| 358 |
zcsr, err := zx509.ParseCertificateRequest(block.Bytes) |
| 359 |
if err != nil { |
| 360 |
return errors.WithStack(err) |
| 361 |
} |
| 362 |
b, err := json.MarshalIndent(struct { |
| 363 |
*zx509.CertificateRequest |
| 364 |
}{zcsr}, "", " ") |
| 365 |
if err != nil { |
| 366 |
return errors.WithStack(err) |
| 367 |
} |
| 368 |
os.Stdout.Write(b) |
| 369 |
return nil |
| 370 |
default: |
| 371 |
return errs.InvalidFlagValue(ctx, "format", format, "text, json") |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
func derToPemBlock(b []byte) *pem.Block { |
| 379 |
if _, err := x509.ParseCertificate(b); err == nil { |
| 380 |
return &pem.Block{Type: "CERTIFICATE", Bytes: b} |
| 381 |
} |
| 382 |
if _, err := x509.ParseCertificateRequest(b); err == nil { |
| 383 |
return &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: b} |
| 384 |
} |
| 385 |
return nil |
| 386 |
} |