|
| 1 | +package certificate |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/pem" |
| 6 | + |
| 7 | + "github.com/smallstep/cli/command" |
| 8 | + "github.com/smallstep/cli/crypto/pemutil" |
| 9 | + "github.com/smallstep/cli/errs" |
| 10 | + "github.com/smallstep/cli/flags" |
| 11 | + "github.com/smallstep/cli/ui" |
| 12 | + "github.com/smallstep/cli/utils" |
| 13 | + "github.com/urfave/cli" |
| 14 | + |
| 15 | + "software.sslmate.com/src/go-pkcs12" |
| 16 | +) |
| 17 | + |
| 18 | +func extractCommand() cli.Command { |
| 19 | + return cli.Command{ |
| 20 | + Name: "extract", |
| 21 | + Action: command.ActionFunc(extractAction), |
| 22 | + Usage: `extract a .p12 file`, |
| 23 | + UsageText: `step certificate extract <p12-path> [<crt-path>] [<key-path>] |
| 24 | +[**--ca**=<file>] [**--password-file**=<file>]`, |
| 25 | + Description: `**step certificate extract** extracts a certificate and private key |
| 26 | +from a .p12 (PFX / PKCS12) file. |
| 27 | +
|
| 28 | +## EXIT CODES |
| 29 | +
|
| 30 | +This command returns 0 on success and \>0 if any error occurs. |
| 31 | +
|
| 32 | +## EXAMPLES |
| 33 | +
|
| 34 | +Extract a certificate and a private key from a .p12 file: |
| 35 | +
|
| 36 | +''' |
| 37 | +$ step certificate extract foo.p12 foo.crt foo.key |
| 38 | +''' |
| 39 | +
|
| 40 | +Extract a certificate, private key and intermediate certidicates from a .p12 file: |
| 41 | +
|
| 42 | +''' |
| 43 | +$ step certificate extract foo.p12 foo.crt foo.key --ca intermediate.crt |
| 44 | +''' |
| 45 | +
|
| 46 | +Extract certificates from "trust store" for Java applications: |
| 47 | +
|
| 48 | +''' |
| 49 | +$ step certificate extract trust.p12 --ca ca.crt |
| 50 | +'''`, |
| 51 | + Flags: []cli.Flag{ |
| 52 | + cli.StringFlag{ |
| 53 | + Name: "ca", |
| 54 | + Usage: `The path to the <file> containing a CA or intermediate certificate to |
| 55 | +add to the .p12 file. Use the '--ca' flag multiple times to add |
| 56 | +multiple CAs or intermediates.`, |
| 57 | + }, |
| 58 | + cli.StringFlag{ |
| 59 | + Name: "password-file", |
| 60 | + Usage: `The path to the <file> containing the password to decrypt the .p12 file.`, |
| 61 | + }, |
| 62 | + flags.NoPassword, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func extractAction(ctx *cli.Context) error { |
| 68 | + if err := errs.MinMaxNumberOfArguments(ctx, 1, 3); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + p12File := ctx.Args().Get(0) |
| 73 | + crtFile := ctx.Args().Get(1) |
| 74 | + keyFile := ctx.Args().Get(2) |
| 75 | + caFile := ctx.String("ca") |
| 76 | + |
| 77 | + var err error |
| 78 | + var password string |
| 79 | + if passwordFile := ctx.String("password-file"); passwordFile != "" { |
| 80 | + password, err = utils.ReadStringPasswordFromFile(passwordFile) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if password == "" && !ctx.Bool("no-password") { |
| 87 | + pass, err := ui.PromptPassword("Please enter a password to decrypt the .p12 file") |
| 88 | + if err != nil { |
| 89 | + return errs.Wrap(err, "error reading password") |
| 90 | + } |
| 91 | + password = string(pass) |
| 92 | + } |
| 93 | + |
| 94 | + p12Data, err := utils.ReadFile(p12File) |
| 95 | + if err != nil { |
| 96 | + return errs.Wrap(err, "error reading file %s", p12File) |
| 97 | + } |
| 98 | + |
| 99 | + if crtFile != "" && keyFile != "" { |
| 100 | + // If we have a destination crt path and a key path, |
| 101 | + // we are extracting a .p12 file |
| 102 | + key, crt, CAs, err := pkcs12.DecodeChain(p12Data, password) |
| 103 | + if err != nil { |
| 104 | + return errs.Wrap(err, "failed to decode PKCS12 data") |
| 105 | + } |
| 106 | + |
| 107 | + _, err = pemutil.Serialize(key, pemutil.ToFile(keyFile, 0600)) |
| 108 | + if err != nil { |
| 109 | + return errs.Wrap(err, "failed to serialize private key") |
| 110 | + } |
| 111 | + |
| 112 | + _, err = pemutil.Serialize(crt, pemutil.ToFile(crtFile, 0600)) |
| 113 | + if err != nil { |
| 114 | + return errs.Wrap(err, "failed to serialize certificate") |
| 115 | + } |
| 116 | + |
| 117 | + if caFile != "" { |
| 118 | + if err := extractCerts(CAs, caFile); err != nil { |
| 119 | + return errs.Wrap(err, "failed to serialize CA certificates") |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + } else { |
| 124 | + // If we have only --ca flags, |
| 125 | + // we are extracting from trust store |
| 126 | + certs, err := pkcs12.DecodeTrustStore(p12Data, password) |
| 127 | + if err != nil { |
| 128 | + return errs.Wrap(err, "failed to decode trust store") |
| 129 | + } |
| 130 | + if err := extractCerts(certs, caFile); err != nil { |
| 131 | + return errs.Wrap(err, "failed to serialize CA certificates") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if crtFile != "" { |
| 136 | + ui.Printf("Your certificate has been saved in %s.\n", crtFile) |
| 137 | + } |
| 138 | + if keyFile != "" { |
| 139 | + ui.Printf("Your private key has been saved in %s.\n", keyFile) |
| 140 | + } |
| 141 | + if caFile != "" { |
| 142 | + ui.Printf("Your CA certificate has been saved in %s.\n", caFile) |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func extractCerts(certs []*x509.Certificate, filename string) error { |
| 149 | + var data []byte |
| 150 | + for _, cert := range certs { |
| 151 | + pemblk, err := pemutil.Serialize(cert) |
| 152 | + if err != nil { |
| 153 | + return err |
| 154 | + } |
| 155 | + data = append(data, pem.EncodeToMemory(pemblk)...) |
| 156 | + } |
| 157 | + if err := utils.WriteFile(filename, data, 0600); err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + return nil |
| 161 | +} |
0 commit comments