Debian Codegraph @

command/certificate/p12.go

raw symbols SCIP
1 package certificate
2
3 import (
4 "crypto/rand"
5 "crypto/x509"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/cli/command"
9 "github.com/smallstep/cli/crypto/pemutil"
10 "github.com/smallstep/cli/errs"
11 "github.com/smallstep/cli/flags"
12 "github.com/smallstep/cli/ui"
13 "github.com/smallstep/cli/utils"
14 "github.com/urfave/cli"
15
16 "software.sslmate.com/src/go-pkcs12"
17 )
18
19 func p12Command() cli.Command {
20 return cli.Command{
21 Name: "p12",
22 Action: command.ActionFunc(p12Action),
23 Usage: `package a certificate and keys into a .p12 file`,
24 UsageText: `step certificate p12 <p12-path> [<crt-path>] [<key-path>]
25 [**--ca**=<file>] [**--password-file**=<file>]`,
26 Description: `**step certificate p12** creates a .p12 (PFX / PKCS12)
27 file containing certificates and keys. This can then be used to import
28 into Windows / Firefox / Java applications.
29
30 ## EXIT CODES
31
32 This command returns 0 on success and \>0 if any error occurs.
33
34 ## EXAMPLES
35
36 Package a certificate and private key together:
37
38 '''
39 $ step certificate p12 foo.p12 foo.crt foo.key
40 '''
41
42 Package a certificate and private key together, and include an intermediate certificate:
43
44 '''
45 $ step certificate p12 foo.p12 foo.crt foo.key --ca intermediate.crt
46 '''
47
48 Package a CA certificate into a "trust store" for Java applications:
49
50 '''
51 $ step certificate p12 trust.p12 --ca ca.crt
52 '''
53
54 Package a certificate and private key with an empty password:
55
56 '''
57 $ step certificate p12 --no-password --insecure foo.p12 foo.crt foo.key
58 '''`,
59 Flags: []cli.Flag{
60 cli.StringSliceFlag{
61 Name: "ca",
62 Usage: `The path to the <file> containing a CA or intermediate certificate to
63 add to the .p12 file. Use the '--ca' flag multiple times to add
64 multiple CAs or intermediates.`,
65 },
66 cli.StringFlag{
67 Name: "password-file",
68 Usage: `The path to the <file> containing the password to encrypt the .p12 file.`,
69 },
70 flags.NoPassword,
71 flags.Force,
72 flags.Insecure,
73 },
74 }
75 }
76
77 func p12Action(ctx *cli.Context) error {
78 if err := errs.MinMaxNumberOfArguments(ctx, 1, 3); err != nil {
79 return err
80 }
81
82 p12File := ctx.Args().Get(0)
83 crtFile := ctx.Args().Get(1)
84 keyFile := ctx.Args().Get(2)
85 caFiles := ctx.StringSlice("ca")
86 hasKeyAndCert := crtFile != "" && keyFile != ""
87
88 //If either key or cert are provided, both must be provided
89 if !hasKeyAndCert && (crtFile != "" || keyFile != "") {
90 return errs.MissingArguments(ctx, "key_file")
91 }
92
93 //If no key and cert are provided, ca files must be provided
94 if !hasKeyAndCert && len(caFiles) == 0 {
95 return errors.Errorf("flag '--%s' must be provided when no <crt_path> and <key_path> are present", "ca")
96 }
97
98 // Validate flags
99 switch {
100 case ctx.String("password-file") != "" && ctx.Bool("no-password"):
101 return errs.IncompatibleFlagWithFlag(ctx, "no-password", "password-file")
102 case ctx.Bool("no-password") && !ctx.Bool("insecure"):
103 return errs.RequiredInsecureFlag(ctx, "no-password")
104 }
105
106 x509CAs := []*x509.Certificate{}
107 for _, caFile := range caFiles {
108 x509Bundle, err := pemutil.ReadCertificateBundle(caFile)
109 if err != nil {
110 return errors.Wrap(err, "error reading CA certificate")
111 }
112 x509CAs = append(x509CAs, x509Bundle...)
113 }
114
115 var err error
116 var password string
117 if !ctx.Bool("no-password") {
118 if passwordFile := ctx.String("password-file"); passwordFile != "" {
119 password, err = utils.ReadStringPasswordFromFile(passwordFile)
120 if err != nil {
121 return err
122 }
123 }
124
125 if password == "" {
126 pass, err := ui.PromptPassword("Please enter a password to encrypt the .p12 file")
127 if err != nil {
128 return errors.Wrap(err, "error reading password")
129 }
130 password = string(pass)
131 }
132 }
133
134 var pkcs12Data []byte
135 if hasKeyAndCert {
136 //If we have a key and certificate, we're making an identity store
137 x509CertBundle, err := pemutil.ReadCertificateBundle(crtFile)
138 if err != nil {
139 return errors.Wrap(err, "error reading certificate")
140 }
141
142 key, err := pemutil.Read(keyFile)
143 if err != nil {
144 return errors.Wrap(err, "error reading key")
145 }
146
147 //The first certificate in the bundle will be our server cert
148 x509Cert := x509CertBundle[0]
149 //Any remaning certs will be intermediates for the server
150 x509CAs = append(x509CAs, x509CertBundle[1:]...)
151
152 pkcs12Data, err = pkcs12.Encode(rand.Reader, key, x509Cert, x509CAs, password)
153 if err != nil {
154 return errs.Wrap(err, "failed to encode PKCS12 data")
155 }
156 } else {
157 //If we have only --ca flags, we're making a trust store
158 pkcs12Data, err = pkcs12.EncodeTrustStore(rand.Reader, x509CAs, password)
159 if err != nil {
160 return errs.Wrap(err, "failed to encode PKCS12 data")
161 }
162 }
163
164 if err := utils.WriteFile(p12File, pkcs12Data, 0600); err != nil {
165 return err
166 }
167
168 ui.Printf("Your .p12 bundle has been saved as %s.\n", p12File)
169 return nil
170 }