| 1 |
package ca |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/rand" |
| 5 |
"crypto/x509" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/smallstep/certificates/cas/apiv1" |
| 11 |
"github.com/smallstep/certificates/pki" |
| 12 |
"github.com/smallstep/cli/command" |
| 13 |
"github.com/smallstep/cli/crypto/pemutil" |
| 14 |
"github.com/smallstep/cli/errs" |
| 15 |
"github.com/smallstep/cli/ui" |
| 16 |
"github.com/smallstep/cli/utils" |
| 17 |
"github.com/urfave/cli" |
| 18 |
) |
| 19 |
|
| 20 |
func initCommand() cli.Command { |
| 21 |
return cli.Command{ |
| 22 |
Name: "init", |
| 23 |
Action: cli.ActionFunc(initAction), |
| 24 |
Usage: "initialize the CA PKI", |
| 25 |
UsageText: `**step ca init** |
| 26 |
[**--root**=<path>] [**--key**=<path>] [**--pki**] [**--ssh**] [**--name**=<name>] |
| 27 |
[**--dns**=<dns>] [**--address**=<address>] [**--provisioner**=<name>] |
| 28 |
[**--provisioner-password-file**=<path>] [**--password-file**=<path>] |
| 29 |
[**--with-ca-url**=<url>] [**--no-db**]`, |
| 30 |
Description: `**step ca init** command initializes a public key infrastructure (PKI) to be |
| 31 |
used by the Certificate Authority.`, |
| 32 |
Flags: []cli.Flag{ |
| 33 |
cli.StringFlag{ |
| 34 |
Name: "root", |
| 35 |
Usage: "The path of an existing PEM <file> to be used as the root certificate authority.", |
| 36 |
EnvVar: command.IgnoreEnvVar, |
| 37 |
}, |
| 38 |
cli.StringFlag{ |
| 39 |
Name: "key", |
| 40 |
Usage: "The path of an existing key <file> of the root certificate authority.", |
| 41 |
EnvVar: command.IgnoreEnvVar, |
| 42 |
}, |
| 43 |
cli.BoolFlag{ |
| 44 |
Name: "pki", |
| 45 |
Usage: "Generate only the PKI without the CA configuration.", |
| 46 |
}, |
| 47 |
cli.BoolFlag{ |
| 48 |
Name: "ssh", |
| 49 |
Usage: `Create keys to sign SSH certificates.`, |
| 50 |
}, |
| 51 |
cli.StringFlag{ |
| 52 |
Name: "name", |
| 53 |
Usage: "The <name> of the new PKI.", |
| 54 |
}, |
| 55 |
cli.StringFlag{ |
| 56 |
Name: "dns", |
| 57 |
Usage: "The comma separated DNS <names> or IP addresses of the new CA.", |
| 58 |
}, |
| 59 |
cli.StringFlag{ |
| 60 |
Name: "address", |
| 61 |
Usage: "The <address> that the new CA will listen at.", |
| 62 |
}, |
| 63 |
cli.StringFlag{ |
| 64 |
Name: "provisioner", |
| 65 |
Usage: "The <name> of the first provisioner.", |
| 66 |
}, |
| 67 |
cli.StringFlag{ |
| 68 |
Name: "password-file", |
| 69 |
Usage: `The path to the <file> containing the password to encrypt the keys.`, |
| 70 |
}, |
| 71 |
cli.StringFlag{ |
| 72 |
Name: "provisioner-password-file", |
| 73 |
Usage: `The path to the <file> containing the password to encrypt the provisioner key.`, |
| 74 |
}, |
| 75 |
cli.StringFlag{ |
| 76 |
Name: "with-ca-url", |
| 77 |
Usage: `<URI> of the Step Certificate Authority to write in defaults.json`, |
| 78 |
}, |
| 79 |
cli.StringFlag{ |
| 80 |
Name: "ra", |
| 81 |
Usage: `The registration authority <name> to use. Currently only "CloudCAS" is supported.`, |
| 82 |
}, |
| 83 |
cli.StringFlag{ |
| 84 |
Name: "issuer", |
| 85 |
Usage: `The registration authority issuer <name> to use. |
| 86 |
|
| 87 |
: If CloudCAS is used, this flag should be the resource name of the |
| 88 |
intermediate certificate to use. This has the format |
| 89 |
'projects/\\*/locations/\\*/certificateAuthorities/\\*'.`, |
| 90 |
}, |
| 91 |
cli.StringFlag{ |
| 92 |
Name: "credentials-file", |
| 93 |
Usage: `The registration authority credentials <file> to use. |
| 94 |
|
| 95 |
: If CloudCAS is used, this flag should be the path to a service account key. |
| 96 |
It can also be set using the 'GOOGLE_APPLICATION_CREDENTIALS=path' |
| 97 |
environment variable or the default service account in an instance in Google |
| 98 |
Cloud.`, |
| 99 |
}, |
| 100 |
cli.BoolFlag{ |
| 101 |
Name: "no-db", |
| 102 |
Usage: `Generate a CA configuration without the DB stanza. No persistence layer.`, |
| 103 |
}, |
| 104 |
}, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func initAction(ctx *cli.Context) (err error) { |
| 109 |
if err = assertCryptoRand(); err != nil { |
| 110 |
return err |
| 111 |
} |
| 112 |
|
| 113 |
var rootCrt *x509.Certificate |
| 114 |
var rootKey interface{} |
| 115 |
|
| 116 |
caURL := ctx.String("with-ca-url") |
| 117 |
root := ctx.String("root") |
| 118 |
key := ctx.String("key") |
| 119 |
ra := strings.ToLower(ctx.String("ra")) |
| 120 |
switch { |
| 121 |
case len(root) > 0 && len(key) == 0: |
| 122 |
return errs.RequiredWithFlag(ctx, "root", "key") |
| 123 |
case len(root) == 0 && len(key) > 0: |
| 124 |
return errs.RequiredWithFlag(ctx, "key", "root") |
| 125 |
case len(root) > 0 && len(key) > 0: |
| 126 |
if rootCrt, err = pemutil.ReadCertificate(root); err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
if rootKey, err = pemutil.Read(key); err != nil { |
| 130 |
return err |
| 131 |
} |
| 132 |
case ra != "" && ra != apiv1.CloudCAS: |
| 133 |
return errs.InvalidFlagValue(ctx, "ra", ctx.String("ra"), "CloudCAS") |
| 134 |
} |
| 135 |
|
| 136 |
configure := !ctx.Bool("pki") |
| 137 |
noDB := ctx.Bool("no-db") |
| 138 |
if !configure && noDB { |
| 139 |
return errs.IncompatibleFlagWithFlag(ctx, "pki", "no-db") |
| 140 |
} |
| 141 |
|
| 142 |
var password string |
| 143 |
if passwordFile := ctx.String("password-file"); passwordFile != "" { |
| 144 |
password, err = utils.ReadStringPasswordFromFile(passwordFile) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
var provisionerPassword []byte |
| 153 |
if passwordFile := ctx.String("provisioner-password-file"); passwordFile != "" { |
| 154 |
provisionerPassword, err = utils.ReadPasswordFromFile(passwordFile) |
| 155 |
if err != nil { |
| 156 |
return err |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
var name, org, resource string |
| 161 |
var casOptions apiv1.Options |
| 162 |
switch ra { |
| 163 |
case apiv1.CloudCAS: |
| 164 |
var create bool |
| 165 |
var project, location string |
| 166 |
|
| 167 |
iss := ctx.String("issuer") |
| 168 |
if iss == "" { |
| 169 |
create, err = ui.PromptYesNo("Would you like to create a new PKI (y) or use an existing one (n)?") |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
if create { |
| 174 |
ui.Println("What would you like to name your new PKI?", ui.WithValue(ctx.String("name"))) |
| 175 |
name, err = ui.Prompt("(e.g. Smallstep)", |
| 176 |
ui.WithValidateNotEmpty(), ui.WithValue(ctx.String("name"))) |
| 177 |
if err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
ui.Println("What is the name of your organization?") |
| 181 |
org, err = ui.Prompt("(e.g. Smallstep)", |
| 182 |
ui.WithValidateNotEmpty()) |
| 183 |
if err != nil { |
| 184 |
return err |
| 185 |
} |
| 186 |
ui.Println("What resource id do you want to use? [we will append -Root-CA or -Intermediate-CA]") |
| 187 |
resource, err = ui.Prompt("(e.g. Smallstep)", |
| 188 |
ui.WithValidateRegexp("^[a-zA-Z0-9-_]+$")) |
| 189 |
if err != nil { |
| 190 |
return err |
| 191 |
} |
| 192 |
ui.Println("What is the id of your project on Google's Cloud Platform?") |
| 193 |
project, err = ui.Prompt("(e.g. smallstep-ca)", |
| 194 |
ui.WithValidateRegexp("^[a-z][a-z0-9-]{4,28}[a-z0-9]$")) |
| 195 |
if err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
ui.Println("What region or location do you want to use?") |
| 199 |
location, err = ui.Prompt("(e.g. us-west1)", |
| 200 |
ui.WithValidateRegexp("^[a-z0-9-]+$")) |
| 201 |
if err != nil { |
| 202 |
return err |
| 203 |
} |
| 204 |
} else { |
| 205 |
ui.Println("What certificate authority would you like to use?") |
| 206 |
iss, err = ui.Prompt("(e.g. projects/smallstep-ca/locations/us-west1/certificateAuthorities/intermediate-ca)", |
| 207 |
ui.WithValidateRegexp("^projects/[a-z][a-z0-9-]{4,28}[a-z0-9]/locations/[a-z0-9-]+/certificateAuthorities/[a-zA-Z0-9-_]+$")) |
| 208 |
if err != nil { |
| 209 |
return err |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
casOptions = apiv1.Options{ |
| 214 |
Type: apiv1.CloudCAS, |
| 215 |
CredentialsFile: ctx.String("credentials-file"), |
| 216 |
CertificateAuthority: iss, |
| 217 |
IsCreator: create, |
| 218 |
Project: project, |
| 219 |
Location: location, |
| 220 |
} |
| 221 |
default: |
| 222 |
ui.Println("What would you like to name your new PKI?", ui.WithValue(ctx.String("name"))) |
| 223 |
name, err = ui.Prompt("(e.g. Smallstep)", |
| 224 |
ui.WithValidateNotEmpty(), ui.WithValue(ctx.String("name"))) |
| 225 |
if err != nil { |
| 226 |
return err |
| 227 |
} |
| 228 |
org = name |
| 229 |
casOptions = apiv1.Options{ |
| 230 |
Type: apiv1.SoftCAS, |
| 231 |
IsCreator: true, |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
p, err := pki.New(casOptions) |
| 236 |
if err != nil { |
| 237 |
return err |
| 238 |
} |
| 239 |
|
| 240 |
if configure { |
| 241 |
var names string |
| 242 |
ui.Println("What DNS names or IP addresses would you like to add to your new CA?", ui.WithValue(ctx.String("dns"))) |
| 243 |
names, err = ui.Prompt("(e.g. ca.smallstep.com[,1.1.1.1,etc.])", |
| 244 |
ui.WithValidateFunc(ui.DNS()), ui.WithValue(ctx.String("dns"))) |
| 245 |
if err != nil { |
| 246 |
return err |
| 247 |
} |
| 248 |
names = strings.Replace(names, " ", ",", -1) |
| 249 |
parts := strings.Split(names, ",") |
| 250 |
var dnsNames []string |
| 251 |
for _, name := range parts { |
| 252 |
if len(name) == 0 { |
| 253 |
continue |
| 254 |
} |
| 255 |
dnsNames = append(dnsNames, strings.TrimSpace(name)) |
| 256 |
} |
| 257 |
|
| 258 |
var address string |
| 259 |
ui.Println("What IP and port will your new CA bind to?", ui.WithValue(ctx.String("address"))) |
| 260 |
address, err = ui.Prompt("(e.g. :443 or 127.0.0.1:4343)", |
| 261 |
ui.WithValidateFunc(ui.Address()), ui.WithValue(ctx.String("address"))) |
| 262 |
if err != nil { |
| 263 |
return err |
| 264 |
} |
| 265 |
|
| 266 |
var provisioner string |
| 267 |
ui.Println("What would you like to name the CA's first provisioner?", ui.WithValue(ctx.String("provisioner"))) |
| 268 |
provisioner, err = ui.Prompt("(e.g. [email protected])", |
| 269 |
ui.WithValidateNotEmpty(), ui.WithValue(ctx.String("provisioner"))) |
| 270 |
if err != nil { |
| 271 |
return err |
| 272 |
} |
| 273 |
|
| 274 |
p.SetProvisioner(provisioner) |
| 275 |
p.SetAddress(address) |
| 276 |
p.SetDNSNames(dnsNames) |
| 277 |
p.SetCAURL(caURL) |
| 278 |
} |
| 279 |
|
| 280 |
ui.Println("Choose a password for your CA keys and first provisioner.", ui.WithValue(password)) |
| 281 |
pass, err := ui.PromptPasswordGenerate("[leave empty and we'll generate one]", |
| 282 |
ui.WithRichPrompt(), ui.WithValue(password)) |
| 283 |
if err != nil { |
| 284 |
return err |
| 285 |
} |
| 286 |
|
| 287 |
if configure { |
| 288 |
|
| 289 |
if len(provisionerPassword) > 0 { |
| 290 |
if err = p.GenerateKeyPairs(provisionerPassword); err != nil { |
| 291 |
return err |
| 292 |
} |
| 293 |
} else { |
| 294 |
if err = p.GenerateKeyPairs(pass); err != nil { |
| 295 |
return err |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if casOptions.IsCreator { |
| 301 |
var root *apiv1.CreateCertificateAuthorityResponse |
| 302 |
|
| 303 |
if rootCrt == nil && rootKey == nil { |
| 304 |
fmt.Println() |
| 305 |
fmt.Print("Generating root certificate... \n") |
| 306 |
|
| 307 |
root, err = p.GenerateRootCertificate(name, org, resource, pass) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
|
| 312 |
fmt.Println("all done!") |
| 313 |
} else { |
| 314 |
fmt.Println() |
| 315 |
fmt.Print("Copying root certificate... \n") |
| 316 |
|
| 317 |
if err = p.WriteRootCertificate(rootCrt, nil, nil); err != nil { |
| 318 |
return err |
| 319 |
} |
| 320 |
root = p.CreateCertificateAuthorityResponse(rootCrt, rootKey) |
| 321 |
fmt.Println("all done!") |
| 322 |
} |
| 323 |
|
| 324 |
fmt.Println() |
| 325 |
fmt.Print("Generating intermediate certificate... \n") |
| 326 |
|
| 327 |
err = p.GenerateIntermediateCertificate(name, org, resource, root, pass) |
| 328 |
if err != nil { |
| 329 |
return err |
| 330 |
} |
| 331 |
} else { |
| 332 |
|
| 333 |
if err := p.GetCertificateAuthority(); err != nil { |
| 334 |
return err |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
if ctx.Bool("ssh") { |
| 339 |
fmt.Println() |
| 340 |
fmt.Print("Generating user and host SSH certificate signing keys... \n") |
| 341 |
if err := p.GenerateSSHSigningKeys(pass); err != nil { |
| 342 |
return err |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
fmt.Println("all done!") |
| 347 |
|
| 348 |
if !configure { |
| 349 |
p.TellPKI() |
| 350 |
return nil |
| 351 |
} |
| 352 |
opts := []pki.Option{} |
| 353 |
if noDB { |
| 354 |
opts = append(opts, pki.WithoutDB()) |
| 355 |
} |
| 356 |
return p.Save(opts...) |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
func assertCryptoRand() error { |
| 362 |
buf := make([]byte, 64) |
| 363 |
_, err := io.ReadFull(rand.Reader, buf) |
| 364 |
if err != nil { |
| 365 |
return errs.NewError("crypto/rand is unavailable: Read() failed with %#v", err) |
| 366 |
} |
| 367 |
return nil |
| 368 |
} |