| 1 |
package certificate |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/x509" |
| 6 |
"encoding/pem" |
| 7 |
"os" |
| 8 |
|
| 9 |
"github.com/pkg/errors" |
| 10 |
"github.com/smallstep/cli/command" |
| 11 |
"github.com/smallstep/cli/errs" |
| 12 |
"github.com/smallstep/cli/flags" |
| 13 |
"github.com/smallstep/cli/ui" |
| 14 |
"github.com/smallstep/cli/utils" |
| 15 |
"github.com/urfave/cli" |
| 16 |
) |
| 17 |
|
| 18 |
func formatCommand() cli.Command { |
| 19 |
return cli.Command{ |
| 20 |
Name: "format", |
| 21 |
Action: command.ActionFunc(formatAction), |
| 22 |
Usage: `reformat certificate`, |
| 23 |
UsageText: `**step certificate format** <crt_file> [**--out**=<path>]`, |
| 24 |
Description: `**step certificate format** prints the certificate in |
| 25 |
a different format. |
| 26 |
|
| 27 |
Only 2 formats are currently supported; PEM and ASN.1 DER. This tool will convert |
| 28 |
a certificate in one format to the other. |
| 29 |
|
| 30 |
## POSITIONAL ARGUMENTS |
| 31 |
|
| 32 |
<crt_file> |
| 33 |
: Path to a certificate file. |
| 34 |
|
| 35 |
## EXIT CODES |
| 36 |
|
| 37 |
This command returns 0 on success and \>0 if any error occurs. |
| 38 |
|
| 39 |
## EXAMPLES |
| 40 |
|
| 41 |
Convert PEM format to DER. |
| 42 |
''' |
| 43 |
$ step certificate format foo.pem |
| 44 |
''' |
| 45 |
|
| 46 |
Convert DER format to PEM. |
| 47 |
''' |
| 48 |
$ step certificate format foo.der |
| 49 |
''' |
| 50 |
|
| 51 |
Convert PEM format to DER and write to disk. |
| 52 |
''' |
| 53 |
$ step certificate format foo.pem --out foo.der |
| 54 |
''' |
| 55 |
`, |
| 56 |
Flags: []cli.Flag{ |
| 57 |
cli.StringFlag{ |
| 58 |
Name: "out", |
| 59 |
Usage: `Path to write the reformatted result.`, |
| 60 |
}, |
| 61 |
flags.Force, |
| 62 |
}, |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func formatAction(ctx *cli.Context) error { |
| 67 |
if err := errs.NumberOfArguments(ctx, 1); err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
|
| 71 |
var ( |
| 72 |
crtFile = ctx.Args().Get(0) |
| 73 |
out = ctx.String("out") |
| 74 |
ob []byte |
| 75 |
) |
| 76 |
|
| 77 |
crtBytes, err := utils.ReadFile(crtFile) |
| 78 |
if err != nil { |
| 79 |
return errs.FileError(err, crtFile) |
| 80 |
} |
| 81 |
|
| 82 |
switch { |
| 83 |
case bytes.HasPrefix(crtBytes, []byte("-----BEGIN ")): |
| 84 |
var ( |
| 85 |
blocks []*pem.Block |
| 86 |
block *pem.Block |
| 87 |
) |
| 88 |
for len(crtBytes) > 0 { |
| 89 |
block, crtBytes = pem.Decode(crtBytes) |
| 90 |
if block == nil { |
| 91 |
return errors.Errorf("%s contains an invalid PEM block", crtFile) |
| 92 |
} |
| 93 |
if block.Type != "CERTIFICATE" { |
| 94 |
return errors.Errorf("certificate bundle %s contains an "+ |
| 95 |
"unexpected PEM block of type %s\n\n expected type: "+ |
| 96 |
"CERTIFICATE", crtFile, block.Type) |
| 97 |
} |
| 98 |
blocks = append(blocks, block) |
| 99 |
} |
| 100 |
|
| 101 |
crt, err := x509.ParseCertificate(blocks[0].Bytes) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
ob = crt.Raw |
| 106 |
default: |
| 107 |
p := &pem.Block{ |
| 108 |
Type: "CERTIFICATE", |
| 109 |
Bytes: crtBytes, |
| 110 |
} |
| 111 |
ob = pem.EncodeToMemory(p) |
| 112 |
} |
| 113 |
|
| 114 |
if out == "" { |
| 115 |
os.Stdout.Write(ob) |
| 116 |
} else { |
| 117 |
info, err := os.Stat(crtFile) |
| 118 |
if err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
if err := utils.WriteFile(out, ob, info.Mode()); err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
ui.Printf("Your certificate has been saved in %s.\n", out) |
| 125 |
} |
| 126 |
|
| 127 |
return nil |
| 128 |
} |