Debian Codegraph @

command/certificate/fingerprint.go

raw symbols SCIP
1 package certificate
2
3 import (
4 "crypto/x509"
5 "fmt"
6 "strings"
7
8 "github.com/pkg/errors"
9 "github.com/smallstep/cli/crypto/pemutil"
10 "github.com/smallstep/cli/crypto/x509util"
11 "github.com/smallstep/cli/errs"
12 "github.com/smallstep/cli/flags"
13 "github.com/urfave/cli"
14 )
15
16 func fingerprintCommand() cli.Command {
17 return cli.Command{
18 Name: "fingerprint",
19 Action: cli.ActionFunc(fingerprintAction),
20 Usage: "print the fingerprint of a certificate",
21 UsageText: `**step certificate fingerprint** <crt-file>
22 [**--bundle**] [**--roots**=<root-bundle>] [**--servername**=<servername>] [**--format**=<format>]`,
23 Description: `**step certificate fingerprint** reads a certificate and prints to STDOUT the
24 certificate SHA256 of the raw certificate.
25
26 If <crt-file> contains multiple certificates (i.e., it is a certificate
27 "bundle") the fingerprint of the first certificate in the bundle will be
28 printed. Pass the --bundle option to print all fingerprints in the order in
29 which they appear in the bundle.
30
31 ## POSITIONAL ARGUMENTS
32
33 <crt-file>
34 : A certificate PEM file, usually the root certificate.
35
36 ## EXAMPLES
37
38 Get the fingerprint for a root certificate:
39 '''
40 $ step certificate fingerprint /path/to/root_ca.crt
41 0d7d3834cf187726cf331c40a31aa7ef6b29ba4df601416c9788f6ee01058cf3
42 '''
43
44 Get the fingerprint for a remote certificate:
45 '''
46 $ step certificate fingerprint https://smallstep.com
47 e2c4f12edfc1816cc610755d32e6f45d5678ba21ecda1693bb5b246e3c48c03d
48 '''
49
50 Get the fingerprints for a remote certificate with its intemediate:
51 '''
52 $ step certificate fingerprint --bundle https://smallstep.com
53 e2c4f12edfc1816cc610755d32e6f45d5678ba21ecda1693bb5b246e3c48c03d
54 25847d668eb4f04fdd40b12b6b0740c567da7d024308eb6c2c96fe41d9de218d
55 '''`,
56 Flags: []cli.Flag{
57 cli.StringFlag{
58 Name: "roots",
59 Usage: `Root certificate(s) that will be used to verify the
60 authenticity of the remote server.
61
62 : <roots> is a case-sensitive string and may be one of:
63
64 **file**
65 : Relative or full path to a file. All certificates in the file will be used for path validation.
66
67 **list of files**
68 : Comma-separated list of relative or full file paths. Every PEM encoded certificate from each file will be used for path validation.
69
70 **directory**
71 : Relative or full path to a directory. Every PEM encoded certificate from each file in the directory will be used for path validation.`,
72 },
73 cli.BoolFlag{
74 Name: `bundle`,
75 Usage: `Print all fingerprints in the order in which they appear in the bundle.`,
76 },
77 cli.BoolFlag{
78 Name: "insecure",
79 Usage: `Use an insecure client to retrieve a remote peer certificate. Useful for
80 debugging invalid certificates remotely.`,
81 },
82 flags.ServerName,
83 cli.StringFlag{
84 Name: "format",
85 Usage: `The <format> of the fingerprint, it must be "hex", "base64" or "base64-url".`,
86 },
87 },
88 }
89 }
90
91 func fingerprintAction(ctx *cli.Context) error {
92 if err := errs.NumberOfArguments(ctx, 1); err != nil {
93 return err
94 }
95
96 var (
97 certs []*x509.Certificate
98 serverName = ctx.String("servername")
99 roots = ctx.String("roots")
100 bundle = ctx.Bool("bundle")
101 insecure = ctx.Bool("insecure")
102 crtFile = ctx.Args().First()
103 format = ctx.String("format")
104 )
105
106 encoding, err := getFingerprintFormat(format)
107 if err != nil {
108 return err
109 }
110
111 if addr, isURL, err := trimURL(crtFile); err != nil {
112 return err
113 } else if isURL {
114 certs, err = getPeerCertificates(addr, serverName, roots, insecure)
115 if err != nil {
116 return err
117 }
118 } else {
119 certs, err = pemutil.ReadCertificateBundle(crtFile)
120 if err != nil {
121 return err
122 }
123 }
124
125 if !bundle {
126 certs = certs[:1]
127 }
128
129 for i, crt := range certs {
130 if bundle {
131 fmt.Printf("%d: %s\n", i, x509util.EncodedFingerprint(crt, encoding))
132 } else {
133 fmt.Println(x509util.EncodedFingerprint(crt, encoding))
134 }
135 }
136 return nil
137 }
138
139 func getFingerprintFormat(format string) (x509util.FingerprintEncoding, error) {
140 switch strings.ToLower(strings.TrimSpace(format)) {
141 case "hex", "":
142 return x509util.HexFingerprint, nil
143 case "base64":
144 return x509util.Base64Fingerprint, nil
145 case "base64url", "base64-url":
146 return x509util.Base64UrlFingerprint, nil
147 }
148 return x509util.HexFingerprint, errors.Errorf("error parsing fingerprint format: '%s' is not a valid certificate fingerprint format", format)
149 }