Debian Codegraph @

command/crypto/key/fingerprint.go

raw symbols SCIP
1 package key
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/sha1"
7 "crypto/sha256"
8 "crypto/x509"
9 "crypto/x509/pkix"
10 "encoding/asn1"
11 "encoding/base64"
12 "fmt"
13 "os"
14
15 "github.com/pkg/errors"
16 "github.com/smallstep/cli/command"
17 "github.com/smallstep/cli/crypto/pemutil"
18 "github.com/smallstep/cli/errs"
19 "github.com/smallstep/cli/utils"
20 "github.com/urfave/cli"
21 "golang.org/x/crypto/ssh"
22 )
23
24 func fingerprintCommand() cli.Command {
25 return cli.Command{
26 Name: "fingerprint",
27 Action: command.ActionFunc(fingerprintAction),
28 Usage: `print the fingerprint of a public key`,
29 UsageText: `**step crypto key fingerprint** <key-file>`,
30 Description: `**step crypto key fingerprint** prints the fingerprint of a public key. The
31 fingerprint of a private key will be only based on the public part of the
32 key.
33
34 By default the fingerprint calculated is the SHA-256 hash with raw Base64 encoding
35 of the ASN.1 BIT STRING of the subjectPublicKey defined in RFC 5280.
36
37 Using the flag **--ssh** the fingerprint would be based on the SSH encoding of
38 the public key.
39
40 Note that for certificates and certificate request, the fingerprint would be
41 based only on the public key embedded in the certificate. To get the certificate
42 fingerprint use the appropriate commands:
43
44 '''
45 $ step certificate fingerprint <x509-crt|x509-csr>
46 $ step ssh fingerprint <ssh-crt>
47 '''
48
49 ## POSITIONAL ARGUMENTS
50
51 <key-file>
52 : Path to a public, private key, certificate (X.509 and SSH) or
53 certificate request.
54
55 ## EXAMPLES
56
57 Print the fingerprint of a public key:
58 '''
59 $ step crypto key fingerprint pub.pem
60 '''
61
62 Print the fingerprint of the public key using the SSH marshaling:
63 '''
64 $ step crypto key fingerprint --ssh pub.pem
65 '''
66
67 Print the fingerprint of the key embedded in a certificate using the SHA-1 hash:
68 '''
69 $ step crypto key fingerprint --sha1 cert.pem
70 '''
71
72 Print the same fingerprint for a public key, a private key and a
73 certificate all of with the same public key.
74 '''
75 $ step crypto key fingerprint id_ed25519
76 $ step crypto key fingerprint id_ed25519.pub
77 $ step crypto key fingerprint id_ed25519-cert.pub
78 '''
79
80 Print the fingerprint of the key using an external tool:
81 '''
82 $ step crypto key fingerprint --raw pub.pem | md5sum
83 '''
84
85 Print the fingerprint of the public key of an encrypted private key:
86 '''
87 $ step crypto key fingerprint --password-file pass.txt priv.pem
88 '''`,
89 Flags: []cli.Flag{
90 cli.BoolFlag{
91 Name: "sha1",
92 Usage: "Use the SHA-1 hash with hexadecimal format. The result will be equivalent to the Subject Key Identifier in a X.509 certificate.",
93 },
94 cli.BoolFlag{
95 Name: "ssh",
96 Usage: "Use the SSH marshaling format instead of X.509.",
97 },
98 cli.StringFlag{
99 Name: "password-file",
100 Usage: "The path to the <file> containing passphrase to decrypt a private key.",
101 },
102 cli.BoolFlag{
103 Name: "raw",
104 Usage: "Print the raw bytes instead of the fingerprint. These bytes can be piped to a different hash command.",
105 },
106 },
107 }
108 }
109
110 func fingerprintAction(ctx *cli.Context) error {
111 if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
112 return err
113 }
114
115 var name string
116 switch ctx.NArg() {
117 case 0:
118 name = "-"
119 case 1:
120 name = ctx.Args().First()
121 default:
122 return errs.TooManyArguments(ctx)
123 }
124
125 b, err := utils.ReadFile(name)
126 if err != nil {
127 return err
128 }
129
130 var key interface{}
131 switch {
132 case bytes.HasPrefix(b, []byte("-----BEGIN ")): // PEM format:
133 opts := []pemutil.Options{
134 pemutil.WithFilename(name),
135 pemutil.WithFirstBlock(),
136 }
137 if passFile := ctx.String("password-file"); passFile != "" {
138 opts = append(opts, pemutil.WithPasswordFile(passFile))
139 }
140 if key, err = pemutil.ParseKey(b, opts...); err != nil {
141 return err
142 }
143 case isSSHPublicKey(b):
144 if key, err = pemutil.ParseSSH(b); err != nil {
145 return err
146 }
147 case isJWK(b):
148 if key, err = parseJWK(ctx, b); err != nil {
149 return err
150 }
151 default: // assuming DER format
152 if key, err = pemutil.ParseDER(b); err != nil {
153 return err
154 }
155 }
156
157 if k, ok := key.(interface{ Public() crypto.PublicKey }); ok {
158 key = k.Public()
159 }
160
161 if ctx.Bool("ssh") {
162 b, err = sshFingerprintBytes(key)
163 } else {
164 b, err = x509FingerprintBytes(key)
165 }
166 if err != nil {
167 return err
168 }
169
170 switch {
171 case ctx.Bool("raw"):
172 os.Stdout.Write(b)
173 case ctx.Bool("sha1"):
174 sum := sha1.Sum(b)
175 fmt.Printf("SHA1:%x\n", sum[:])
176 default:
177 sum := sha256.Sum256(b)
178 fmt.Println("SHA256:" + base64.RawStdEncoding.EncodeToString(sum[:]))
179 }
180
181 return nil
182 }
183
184 // subjectPublicKeyInfo is a PKIX public key structure defined in RFC 5280.
185 type subjectPublicKeyInfo struct {
186 Algorithm pkix.AlgorithmIdentifier
187 SubjectPublicKey asn1.BitString
188 }
189
190 func x509FingerprintBytes(pub crypto.PublicKey) ([]byte, error) {
191 b, err := x509.MarshalPKIXPublicKey(pub)
192 if err != nil {
193 return nil, errors.Wrap(err, "error marshaling public key")
194 }
195 var info subjectPublicKeyInfo
196 if _, err = asn1.Unmarshal(b, &info); err != nil {
197 return nil, errors.Wrap(err, "error unmarshaling public key")
198 }
199 return info.SubjectPublicKey.Bytes, nil
200 }
201
202 func sshFingerprintBytes(pub crypto.PublicKey) ([]byte, error) {
203 key, err := ssh.NewPublicKey(pub)
204 if err != nil {
205 return nil, errors.Wrap(err, "error creating ssh public key")
206 }
207 return key.Marshal(), nil
208 }