| 1 |
package key |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/rsa" |
| 8 |
"fmt" |
| 9 |
"math/big" |
| 10 |
|
| 11 |
"github.com/pkg/errors" |
| 12 |
"github.com/smallstep/cli/command" |
| 13 |
"github.com/smallstep/cli/crypto/pemutil" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/utils" |
| 16 |
"github.com/urfave/cli" |
| 17 |
) |
| 18 |
|
| 19 |
func inspectCommand() cli.Command { |
| 20 |
return cli.Command{ |
| 21 |
Name: "inspect", |
| 22 |
Action: command.ActionFunc(inspectAction), |
| 23 |
Usage: `print key details in human readable format`, |
| 24 |
UsageText: `**step crypto key inspect** <key-file>`, |
| 25 |
Description: `**step crypto key inspect** prints details of a public or a private key in a |
| 26 |
human readable format the public key corresponding to the given <key-file>. |
| 27 |
|
| 28 |
## POSITIONAL ARGUMENTS |
| 29 |
|
| 30 |
<key-file> |
| 31 |
: Path to a public or private key. |
| 32 |
|
| 33 |
## EXAMPLES |
| 34 |
|
| 35 |
Print details of the given key: |
| 36 |
''' |
| 37 |
$ step crypto key inspect priv.pem |
| 38 |
''' |
| 39 |
|
| 40 |
## NOTES |
| 41 |
|
| 42 |
This command shows the raw parameters of the keys, it does not include headers |
| 43 |
that the marshaled version of the keys might have. For example, a marshaled |
| 44 |
version an EC public key will have 0x04 in the first byte to indicate the |
| 45 |
uncompressed form specified in section 4.3.6 of ANSI X9.62.`, |
| 46 |
Flags: []cli.Flag{ |
| 47 |
cli.StringFlag{ |
| 48 |
Name: "password-file", |
| 49 |
Usage: "The path to the <file> containing passphrase to decrypt private key.", |
| 50 |
}, |
| 51 |
}, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func inspectAction(ctx *cli.Context) error { |
| 56 |
if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
|
| 60 |
var name string |
| 61 |
switch ctx.NArg() { |
| 62 |
case 0: |
| 63 |
name = "-" |
| 64 |
case 1: |
| 65 |
name = ctx.Args().First() |
| 66 |
default: |
| 67 |
return errs.TooManyArguments(ctx) |
| 68 |
} |
| 69 |
|
| 70 |
b, err := utils.ReadFile(name) |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
var key interface{} |
| 76 |
switch { |
| 77 |
case bytes.HasPrefix(b, []byte("-----BEGIN ")): |
| 78 |
opts := []pemutil.Options{ |
| 79 |
pemutil.WithFilename(name), |
| 80 |
pemutil.WithFirstBlock(), |
| 81 |
} |
| 82 |
if passFile := ctx.String("password-file"); passFile != "" { |
| 83 |
opts = append(opts, pemutil.WithPasswordFile(passFile)) |
| 84 |
} |
| 85 |
if key, err = pemutil.ParseKey(b, opts...); err != nil { |
| 86 |
return err |
| 87 |
} |
| 88 |
case isSSHPublicKey(b): |
| 89 |
if key, err = pemutil.ParseSSH(b); err != nil { |
| 90 |
return err |
| 91 |
} |
| 92 |
case isJWK(b): |
| 93 |
if key, err = parseJWK(ctx, b); err != nil { |
| 94 |
return err |
| 95 |
} |
| 96 |
default: |
| 97 |
if key, err = pemutil.ParseDER(b); err != nil { |
| 98 |
return err |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
switch k := key.(type) { |
| 103 |
case *rsa.PublicKey: |
| 104 |
fmt.Printf("RSA Public-Key: (%d bit)\n", k.Size()*8) |
| 105 |
bigIntPaddedPrinter("Modulus", k.N, k.Size()) |
| 106 |
fmt.Printf("Exponent: %d (0x%x)\n", k.E, k.E) |
| 107 |
case *rsa.PrivateKey: |
| 108 |
fmt.Printf("RSA Private-Key: (%d bit)\n", k.Size()*8) |
| 109 |
bigIntPaddedPrinter("Modulus", k.N, k.Size()) |
| 110 |
fmt.Printf("Public Exponent: %d (0x%x)\n", k.E, k.E) |
| 111 |
bigIntPaddedPrinter("Private Exponent", k.D, k.Size()) |
| 112 |
for i := range k.Primes { |
| 113 |
bigIntPrinter(fmt.Sprintf("Prime #%d", i+1), k.Primes[i]) |
| 114 |
} |
| 115 |
bigIntPrinter("Exponent #1", k.Precomputed.Dp) |
| 116 |
bigIntPrinter("Exponent #2", k.Precomputed.Dq) |
| 117 |
bigIntPrinter("Coefficient", k.Precomputed.Qinv) |
| 118 |
case *ecdsa.PublicKey: |
| 119 |
byteLen := (k.Params().BitSize + 7) >> 3 |
| 120 |
fmt.Printf("EC Public-Key: (%d bit)\n", k.Params().BitSize) |
| 121 |
bigIntPaddedPrinter("X", k.X, byteLen) |
| 122 |
bigIntPaddedPrinter("Y", k.Y, byteLen) |
| 123 |
fmt.Printf("Curve: %s\n", k.Params().Name) |
| 124 |
case *ecdsa.PrivateKey: |
| 125 |
byteLen := (k.Params().BitSize + 7) >> 3 |
| 126 |
fmt.Printf("EC PrivateKey-Key: (%d bit)\n", k.Params().BitSize) |
| 127 |
bigIntPaddedPrinter("X", k.X, byteLen) |
| 128 |
bigIntPaddedPrinter("Y", k.Y, byteLen) |
| 129 |
bigIntPaddedPrinter("D", k.D, byteLen) |
| 130 |
fmt.Printf("Curve: %s\n", k.Params().Name) |
| 131 |
case ed25519.PublicKey: |
| 132 |
fmt.Printf("Ed25519 Public-Key: (%d bit)\n", 8*len(k)) |
| 133 |
bytesPrinter("Public", k) |
| 134 |
case ed25519.PrivateKey: |
| 135 |
fmt.Printf("Ed25519 Private-Key: (%d bit)\n", 8*len(k)) |
| 136 |
bytesPrinter("Public", k[32:]) |
| 137 |
bytesPrinter("Private", k[:32]) |
| 138 |
default: |
| 139 |
return errors.Errorf("unsupported key type '%T'", k) |
| 140 |
} |
| 141 |
|
| 142 |
return nil |
| 143 |
} |
| 144 |
|
| 145 |
func bigIntPrinter(name string, val *big.Int) { |
| 146 |
bytesPrinter(name, val.Bytes()) |
| 147 |
} |
| 148 |
|
| 149 |
func bigIntPaddedPrinter(name string, val *big.Int, size int) { |
| 150 |
bytesPrinter(name, paddedBytes(val.Bytes(), size)) |
| 151 |
} |
| 152 |
|
| 153 |
func bytesPrinter(name string, bytes []byte) { |
| 154 |
fmt.Print(name + ":") |
| 155 |
for i, b := range bytes { |
| 156 |
if (i % 16) == 0 { |
| 157 |
fmt.Print("\n ") |
| 158 |
} |
| 159 |
fmt.Printf("%02x", b) |
| 160 |
if i != len(bytes)-1 { |
| 161 |
fmt.Print(":") |
| 162 |
} |
| 163 |
} |
| 164 |
fmt.Println() |
| 165 |
} |
| 166 |
|
| 167 |
func paddedBytes(b []byte, size int) []byte { |
| 168 |
ret := make([]byte, size) |
| 169 |
copy(ret[len(ret)-len(b):], b) |
| 170 |
return ret |
| 171 |
} |