2019-09-13 19:11:46 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
2020-10-20 01:42:03 +00:00
|
|
|
"context"
|
2019-09-13 19:11:46 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
2020-10-23 22:04:09 +00:00
|
|
|
"crypto/x509/pkix"
|
2019-09-13 19:11:46 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2020-08-11 02:05:27 +00:00
|
|
|
"time"
|
2019-09-13 19:11:46 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-08-02 23:07:30 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
admindb "github.com/smallstep/certificates/authority/admin/db/nosql"
|
2021-05-03 19:48:20 +00:00
|
|
|
authconfig "github.com/smallstep/certificates/authority/config"
|
2019-09-13 19:11:46 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
"github.com/smallstep/certificates/ca"
|
2020-10-20 01:42:03 +00:00
|
|
|
"github.com/smallstep/certificates/cas"
|
|
|
|
"github.com/smallstep/certificates/cas/apiv1"
|
2019-09-13 19:11:46 +00:00
|
|
|
"github.com/smallstep/certificates/db"
|
2021-08-02 23:07:30 +00:00
|
|
|
"github.com/smallstep/nosql"
|
2020-10-29 20:10:03 +00:00
|
|
|
"go.step.sm/cli-utils/errs"
|
|
|
|
"go.step.sm/cli-utils/fileutil"
|
2021-08-10 07:38:30 +00:00
|
|
|
"go.step.sm/cli-utils/step"
|
2020-10-29 20:10:03 +00:00
|
|
|
"go.step.sm/cli-utils/ui"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2022-08-09 00:58:18 +00:00
|
|
|
"go.step.sm/crypto/kms"
|
|
|
|
kmsapi "go.step.sm/crypto/kms/apiv1"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2021-08-02 23:07:30 +00:00
|
|
|
"go.step.sm/linkedca"
|
2019-10-11 18:25:48 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2019-09-13 19:11:46 +00:00
|
|
|
)
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
// DeploymentType defines witch type of deployment a user is initializing
|
|
|
|
type DeploymentType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// StandaloneDeployment is a deployment where all the components like keys,
|
|
|
|
// provisioners, admins, certificates and others are managed by the user.
|
|
|
|
StandaloneDeployment DeploymentType = iota
|
|
|
|
// LinkedDeployment is a deployment where the keys are managed by the user,
|
|
|
|
// but provisioners, admins and the record of certificates are managed in
|
|
|
|
// the cloud.
|
|
|
|
LinkedDeployment
|
|
|
|
// HostedDeployment is a deployment where all the components are managed in
|
|
|
|
// the cloud by smallstep.com/certificate-manager.
|
|
|
|
HostedDeployment
|
|
|
|
)
|
|
|
|
|
2021-08-11 00:07:15 +00:00
|
|
|
// String returns the string version of the deployment type.
|
|
|
|
func (d DeploymentType) String() string {
|
|
|
|
switch d {
|
|
|
|
case StandaloneDeployment:
|
|
|
|
return "standalone"
|
|
|
|
case LinkedDeployment:
|
|
|
|
return "linked"
|
|
|
|
case HostedDeployment:
|
|
|
|
return "hosted"
|
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
const (
|
|
|
|
// ConfigPath is the directory name under the step path where the configuration
|
|
|
|
// files will be stored.
|
|
|
|
configPath = "config"
|
|
|
|
// PublicPath is the directory name under the step path where the public keys
|
|
|
|
// will be stored.
|
|
|
|
publicPath = "certs"
|
|
|
|
// PublicPath is the directory name under the step path where the private keys
|
|
|
|
// will be stored.
|
|
|
|
privatePath = "secrets"
|
|
|
|
// DBPath is the directory name under the step path where the private keys
|
|
|
|
// will be stored.
|
|
|
|
dbPath = "db"
|
2019-10-11 18:25:48 +00:00
|
|
|
// templatesPath is the directory to store templates
|
|
|
|
templatesPath = "templates"
|
2019-09-13 19:11:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetDBPath returns the path where the file-system persistence is stored
|
2021-11-12 06:28:25 +00:00
|
|
|
// based on the $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetDBPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), dbPath)
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfigPath returns the directory where the configuration files are stored
|
2021-11-12 06:28:25 +00:00
|
|
|
// based on the $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetConfigPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), configPath)
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 06:28:25 +00:00
|
|
|
// GetProfileConfigPath returns the directory where the profile configuration
|
|
|
|
// files are stored based on the $(step path).
|
|
|
|
func GetProfileConfigPath() string {
|
|
|
|
return filepath.Join(step.ProfilePath(), configPath)
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
// GetPublicPath returns the directory where the public keys are stored based on
|
2021-11-12 06:28:25 +00:00
|
|
|
// the $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetPublicPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), publicPath)
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSecretsPath returns the directory where the private keys are stored based
|
2021-11-12 06:28:25 +00:00
|
|
|
// on the $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetSecretsPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), privatePath)
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRootCAPath returns the path where the root CA is stored based on the
|
2021-11-12 06:28:25 +00:00
|
|
|
// $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetRootCAPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), publicPath, "root_ca.crt")
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetOTTKeyPath returns the path where the one-time token key is stored based
|
2021-11-12 06:28:25 +00:00
|
|
|
// on the $(step path).
|
2019-09-13 19:11:46 +00:00
|
|
|
func GetOTTKeyPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), privatePath, "ott_key")
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 18:25:48 +00:00
|
|
|
// GetTemplatesPath returns the path where the templates are stored.
|
|
|
|
func GetTemplatesPath() string {
|
2021-08-10 07:38:30 +00:00
|
|
|
return filepath.Join(step.Path(), templatesPath)
|
2019-10-11 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
// GetProvisioners returns the map of provisioners on the given CA.
|
|
|
|
func GetProvisioners(caURL, rootFile string) (provisioner.List, error) {
|
2021-10-08 18:59:57 +00:00
|
|
|
if rootFile == "" {
|
2019-09-13 19:11:46 +00:00
|
|
|
rootFile = GetRootCAPath()
|
|
|
|
}
|
|
|
|
client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cursor := ""
|
|
|
|
provisioners := provisioner.List{}
|
|
|
|
for {
|
|
|
|
resp, err := client.Provisioners(ca.WithProvisionerCursor(cursor), ca.WithProvisionerLimit(100))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
provisioners = append(provisioners, resp.Provisioners...)
|
|
|
|
if resp.NextCursor == "" {
|
|
|
|
return provisioners, nil
|
|
|
|
}
|
|
|
|
cursor = resp.NextCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProvisionerKey returns the encrypted provisioner key with the for the
|
|
|
|
// given kid.
|
|
|
|
func GetProvisionerKey(caURL, rootFile, kid string) (string, error) {
|
2021-10-08 18:59:57 +00:00
|
|
|
if rootFile == "" {
|
2019-09-13 19:11:46 +00:00
|
|
|
rootFile = GetRootCAPath()
|
|
|
|
}
|
|
|
|
client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
resp, err := client.ProvisionerKey(kid)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.Key, nil
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
type options struct {
|
2022-10-24 12:51:27 +00:00
|
|
|
provisioner string
|
|
|
|
superAdminSubject string
|
|
|
|
pkiOnly bool
|
|
|
|
enableACME bool
|
|
|
|
enableSSH bool
|
|
|
|
enableAdmin bool
|
|
|
|
noDB bool
|
|
|
|
isHelm bool
|
|
|
|
deploymentType DeploymentType
|
|
|
|
rootKeyURI string
|
|
|
|
intermediateKeyURI string
|
|
|
|
hostKeyURI string
|
|
|
|
userKeyURI string
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 21:58:03 +00:00
|
|
|
// Option is the type of a configuration option on the pki constructor.
|
|
|
|
type Option func(p *PKI)
|
2021-08-02 23:07:30 +00:00
|
|
|
|
|
|
|
// WithAddress sets the listen address of step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithAddress(s string) Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.Address = s
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:58:03 +00:00
|
|
|
// WithCaURL sets the default ca-url of step-ca.
|
|
|
|
func WithCaURL(s string) Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.Defaults.CaUrl = s
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDNSNames sets the SANs of step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithDNSNames(s []string) Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.DnsNames = s
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithProvisioner defines the name of the default provisioner.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithProvisioner(s string) Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.provisioner = s
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-24 12:51:27 +00:00
|
|
|
// WithSuperAdminSubject defines the subject of the first
|
2022-10-14 14:01:18 +00:00
|
|
|
// super admin for use with the Admin API. The admin will belong
|
|
|
|
// to the first JWK provisioner.
|
2022-10-24 12:51:27 +00:00
|
|
|
func WithSuperAdminSubject(s string) Option {
|
2022-10-14 14:01:18 +00:00
|
|
|
return func(p *PKI) {
|
2022-10-24 12:51:27 +00:00
|
|
|
p.options.superAdminSubject = s
|
2022-10-14 14:01:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// WithPKIOnly will only generate the PKI without the step-ca config files.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithPKIOnly() Option {
|
2021-08-05 22:57:13 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.pkiOnly = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
// WithACME enables acme provisioner in step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithACME() Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.enableACME = true
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSSH enables ssh in step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithSSH() Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.enableSSH = true
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAdmin enables the admin api in step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithAdmin() Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.enableAdmin = true
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNoDB disables the db in step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithNoDB() Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.noDB = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithHelm configures the pki to create a helm values.yaml.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithHelm() Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.isHelm = true
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithDeploymentType defines the deployment type of step-ca.
|
2021-08-06 21:58:03 +00:00
|
|
|
func WithDeploymentType(dt DeploymentType) Option {
|
2021-08-05 03:15:26 +00:00
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.deploymentType = dt
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 22:14:01 +00:00
|
|
|
// WithKMS enables the kms with the given name.
|
2021-10-08 00:28:39 +00:00
|
|
|
func WithKMS(name string) Option {
|
|
|
|
return func(p *PKI) {
|
|
|
|
typ := linkedca.KMS_Type_value[strings.ToUpper(name)]
|
|
|
|
p.Configuration.Kms = &linkedca.KMS{
|
|
|
|
Type: linkedca.KMS_Type(typ),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithKeyURIs defines the key uris for X.509 and SSH keys.
|
|
|
|
func WithKeyURIs(rootKey, intermediateKey, hostKey, userKey string) Option {
|
|
|
|
return func(p *PKI) {
|
|
|
|
p.options.rootKeyURI = rootKey
|
|
|
|
p.options.intermediateKeyURI = intermediateKey
|
|
|
|
p.options.hostKeyURI = hostKey
|
|
|
|
p.options.userKeyURI = userKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
// PKI represents the Public Key Infrastructure used by a certificate authority.
|
|
|
|
type PKI struct {
|
2021-08-05 03:15:26 +00:00
|
|
|
linkedca.Configuration
|
2021-10-18 20:56:24 +00:00
|
|
|
Defaults linkedca.Defaults
|
|
|
|
casOptions apiv1.Options
|
|
|
|
caService apiv1.CertificateAuthorityService
|
|
|
|
caCreator apiv1.CertificateAuthorityCreator
|
|
|
|
keyManager kmsapi.KeyManager
|
|
|
|
config string
|
|
|
|
defaults string
|
|
|
|
profileDefaults string
|
|
|
|
ottPublicKey *jose.JSONWebKey
|
|
|
|
ottPrivateKey *jose.JSONWebEncryption
|
|
|
|
options *options
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new PKI configuration.
|
2021-08-06 21:58:03 +00:00
|
|
|
func New(o apiv1.Options, opts ...Option) (*PKI, error) {
|
2022-10-11 15:39:35 +00:00
|
|
|
// TODO(hs): invoking `New` with a context active will use values from
|
|
|
|
// that CA context while generating the context. Thay may or may not
|
|
|
|
// be fully expected and/or what we want. Check that.
|
2021-11-12 06:28:25 +00:00
|
|
|
currentCtx := step.Contexts().GetCurrent()
|
2021-08-02 23:07:30 +00:00
|
|
|
caService, err := cas.New(context.Background(), o)
|
2020-10-23 22:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
var caCreator apiv1.CertificateAuthorityCreator
|
|
|
|
if o.IsCreator {
|
|
|
|
creator, ok := caService.(apiv1.CertificateAuthorityCreator)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("cas type '%s' does not implements CertificateAuthorityCreator", o.Type)
|
|
|
|
}
|
|
|
|
caCreator = creator
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
// get absolute path for dir/name
|
|
|
|
getPath := func(dir string, name string) (string, error) {
|
|
|
|
s, err := filepath.Abs(filepath.Join(dir, name))
|
|
|
|
return s, errors.Wrapf(err, "error getting absolute path for %s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &PKI{
|
2021-08-05 03:15:26 +00:00
|
|
|
Configuration: linkedca.Configuration{
|
2021-08-06 21:58:03 +00:00
|
|
|
Address: "127.0.0.1:9000",
|
|
|
|
DnsNames: []string{"127.0.0.1"},
|
|
|
|
Ssh: &linkedca.SSH{},
|
|
|
|
Authority: &linkedca.Authority{},
|
|
|
|
Files: make(map[string][]byte),
|
2021-08-05 03:15:26 +00:00
|
|
|
},
|
2021-08-02 23:07:30 +00:00
|
|
|
casOptions: o,
|
|
|
|
caService: caService,
|
2021-10-08 00:28:39 +00:00
|
|
|
caCreator: caCreator,
|
|
|
|
keyManager: o.KeyManager,
|
2021-08-02 23:07:30 +00:00
|
|
|
options: &options{
|
|
|
|
provisioner: "step-cli",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, fn := range opts {
|
2021-08-05 03:15:26 +00:00
|
|
|
fn(p)
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:28:39 +00:00
|
|
|
// Use default key manager
|
2021-10-08 21:25:24 +00:00
|
|
|
if p.keyManager == nil {
|
2021-10-08 00:28:39 +00:00
|
|
|
p.keyManager = kms.Default
|
|
|
|
}
|
|
|
|
|
2021-08-05 03:15:26 +00:00
|
|
|
// Use /home/step as the step path in helm configurations.
|
|
|
|
// Use the current step path when creating pki in files.
|
2021-10-08 18:59:57 +00:00
|
|
|
var public, private, cfg string
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.options.isHelm {
|
|
|
|
public = "/home/step/certs"
|
|
|
|
private = "/home/step/secrets"
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg = "/home/step/config"
|
2021-08-05 03:15:26 +00:00
|
|
|
} else {
|
|
|
|
public = GetPublicPath()
|
|
|
|
private = GetSecretsPath()
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg = GetConfigPath()
|
2021-08-05 03:15:26 +00:00
|
|
|
// Create directories
|
2021-10-08 18:59:57 +00:00
|
|
|
dirs := []string{public, private, cfg, GetTemplatesPath()}
|
2021-11-12 06:28:25 +00:00
|
|
|
if currentCtx != nil {
|
|
|
|
dirs = append(dirs, GetProfileConfigPath())
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
for _, name := range dirs {
|
|
|
|
if _, err := os.Stat(name); os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(name, 0700); err != nil {
|
|
|
|
return nil, errs.FileError(err, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Defaults.CaUrl == "" {
|
|
|
|
p.Defaults.CaUrl = p.DnsNames[0]
|
|
|
|
_, port, err := net.SplitHostPort(p.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing %s", p.Address)
|
|
|
|
}
|
2021-09-17 19:49:16 +00:00
|
|
|
// On k8s we usually access through a service, and this is configured on
|
|
|
|
// port 443 by default.
|
|
|
|
if port == "443" || p.options.isHelm {
|
2021-08-05 03:15:26 +00:00
|
|
|
p.Defaults.CaUrl = fmt.Sprintf("https://%s", p.Defaults.CaUrl)
|
|
|
|
} else {
|
2022-08-23 19:43:48 +00:00
|
|
|
p.Defaults.CaUrl = fmt.Sprintf("https://%s", net.JoinHostPort(p.Defaults.CaUrl, port))
|
2021-08-05 03:15:26 +00:00
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
|
2021-08-05 03:15:26 +00:00
|
|
|
root, err := getPath(public, "root_ca.crt")
|
|
|
|
if err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
rootKey, err := getPath(private, "root_ca_key")
|
|
|
|
if err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
p.Root = []string{root}
|
|
|
|
p.RootKey = []string{rootKey}
|
|
|
|
p.Defaults.Root = root
|
|
|
|
|
|
|
|
if p.Intermediate, err = getPath(public, "intermediate_ca.crt"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.IntermediateKey, err = getPath(private, "intermediate_ca_key"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.Ssh.HostPublicKey, err = getPath(public, "ssh_host_ca_key.pub"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.Ssh.UserPublicKey, err = getPath(public, "ssh_user_ca_key.pub"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.Ssh.HostKey, err = getPath(private, "ssh_host_ca_key"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
if p.Ssh.UserKey, err = getPath(private, "ssh_user_ca_key"); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
if p.defaults, err = getPath(cfg, "defaults.json"); err != nil {
|
2021-08-05 03:15:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-12 06:28:25 +00:00
|
|
|
if currentCtx != nil {
|
|
|
|
p.profileDefaults = currentCtx.ProfileDefaultsFile()
|
2021-11-11 21:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
if p.config, err = getPath(cfg, "ca.json"); err != nil {
|
2021-08-05 03:15:26 +00:00
|
|
|
return nil, err
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
p.Defaults.CaConfig = p.config
|
2019-09-13 19:11:46 +00:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCAConfigPath returns the path of the CA configuration file.
|
|
|
|
func (p *PKI) GetCAConfigPath() string {
|
|
|
|
return p.config
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRootFingerprint returns the root fingerprint.
|
|
|
|
func (p *PKI) GetRootFingerprint() string {
|
2021-08-05 03:15:26 +00:00
|
|
|
return p.Defaults.Fingerprint
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateKeyPairs generates the key pairs used by the certificate authority.
|
|
|
|
func (p *PKI) GenerateKeyPairs(pass []byte) error {
|
|
|
|
var err error
|
|
|
|
// Create OTT key pair, the user doesn't need to know about this.
|
|
|
|
p.ottPublicKey, p.ottPrivateKey, err = jose.GenerateDefaultKeyPair(pass)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:20:16 +00:00
|
|
|
var claims *linkedca.Claims
|
|
|
|
if p.options.enableSSH {
|
|
|
|
claims = &linkedca.Claims{
|
|
|
|
Ssh: &linkedca.SSHClaims{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:58:03 +00:00
|
|
|
// Add JWK provisioner to the configuration.
|
|
|
|
publicKey, err := json.Marshal(p.ottPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error marshaling public key")
|
|
|
|
}
|
|
|
|
encryptedKey, err := p.ottPrivateKey.CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error serializing private key")
|
|
|
|
}
|
|
|
|
p.Authority.Provisioners = append(p.Authority.Provisioners, &linkedca.Provisioner{
|
2021-08-26 17:20:16 +00:00
|
|
|
Type: linkedca.Provisioner_JWK,
|
|
|
|
Name: p.options.provisioner,
|
|
|
|
Claims: claims,
|
2021-08-06 21:58:03 +00:00
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_JWK{
|
|
|
|
JWK: &linkedca.JWKProvisioner{
|
|
|
|
PublicKey: publicKey,
|
|
|
|
EncryptedPrivateKey: []byte(encryptedKey),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// GenerateRootCertificate generates a root certificate with the given name
|
|
|
|
// and using the default key type.
|
|
|
|
func (p *PKI) GenerateRootCertificate(name, org, resource string, pass []byte) (*apiv1.CreateCertificateAuthorityResponse, error) {
|
2021-10-08 00:28:39 +00:00
|
|
|
if uri := p.options.rootKeyURI; uri != "" {
|
|
|
|
p.RootKey[0] = uri
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
resp, err := p.caCreator.CreateCertificateAuthority(&apiv1.CreateCertificateAuthorityRequest{
|
2021-10-08 00:28:39 +00:00
|
|
|
Name: resource + "-Root-CA",
|
|
|
|
Type: apiv1.RootCA,
|
|
|
|
Lifetime: 10 * 365 * 24 * time.Hour,
|
|
|
|
CreateKey: &apiv1.CreateKeyRequest{
|
|
|
|
Name: p.RootKey[0],
|
|
|
|
SignatureAlgorithm: kmsapi.UnspecifiedSignAlgorithm,
|
|
|
|
},
|
2020-10-23 22:04:09 +00:00
|
|
|
Template: &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: name + " Root CA",
|
|
|
|
Organization: []string{org},
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
|
|
|
MaxPathLen: 1,
|
|
|
|
MaxPathLenZero: false,
|
|
|
|
},
|
|
|
|
})
|
2019-09-13 19:11:46 +00:00
|
|
|
if err != nil {
|
2020-10-23 22:04:09 +00:00
|
|
|
return nil, err
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 00:28:39 +00:00
|
|
|
// Replace key name with the one from the key manager if available. On
|
|
|
|
// softcas this will be the original filename, on any other kms will be the
|
|
|
|
// uri to the key.
|
|
|
|
if resp.KeyName != "" {
|
|
|
|
p.RootKey[0] = resp.KeyName
|
|
|
|
}
|
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// PrivateKey will only be set if we have access to it (SoftCAS).
|
|
|
|
if err := p.WriteRootCertificate(resp.Certificate, resp.PrivateKey, pass); err != nil {
|
2020-10-23 22:04:09 +00:00
|
|
|
return nil, err
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
return resp, nil
|
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// WriteRootCertificate writes to the buffer the given certificate and key if given.
|
|
|
|
func (p *PKI) WriteRootCertificate(rootCrt *x509.Certificate, rootKey interface{}, pass []byte) error {
|
|
|
|
p.Files[p.Root[0]] = encodeCertificate(rootCrt)
|
|
|
|
if rootKey != nil {
|
|
|
|
var err error
|
|
|
|
p.Files[p.RootKey[0]], err = encodePrivateKey(rootKey, pass)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sum := sha256.Sum256(rootCrt.Raw)
|
|
|
|
p.Defaults.Fingerprint = strings.ToLower(hex.EncodeToString(sum[:]))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// GenerateIntermediateCertificate generates an intermediate certificate with
|
|
|
|
// the given name and using the default key type.
|
|
|
|
func (p *PKI) GenerateIntermediateCertificate(name, org, resource string, parent *apiv1.CreateCertificateAuthorityResponse, pass []byte) error {
|
2021-10-08 00:28:39 +00:00
|
|
|
if uri := p.options.intermediateKeyURI; uri != "" {
|
|
|
|
p.IntermediateKey = uri
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
resp, err := p.caCreator.CreateCertificateAuthority(&apiv1.CreateCertificateAuthorityRequest{
|
2021-10-08 00:28:39 +00:00
|
|
|
Name: resource + "-Intermediate-CA",
|
|
|
|
Type: apiv1.IntermediateCA,
|
|
|
|
Lifetime: 10 * 365 * 24 * time.Hour,
|
|
|
|
CreateKey: &apiv1.CreateKeyRequest{
|
|
|
|
Name: p.IntermediateKey,
|
|
|
|
SignatureAlgorithm: kmsapi.UnspecifiedSignAlgorithm,
|
|
|
|
},
|
2020-10-23 22:04:09 +00:00
|
|
|
Template: &x509.Certificate{
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: name + " Intermediate CA",
|
|
|
|
Organization: []string{org},
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
IsCA: true,
|
|
|
|
MaxPathLen: 0,
|
|
|
|
MaxPathLenZero: true,
|
|
|
|
},
|
|
|
|
Parent: parent,
|
|
|
|
})
|
2020-08-11 02:05:27 +00:00
|
|
|
if err != nil {
|
2020-10-23 22:04:09 +00:00
|
|
|
return err
|
2020-08-11 02:05:27 +00:00
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
p.casOptions.CertificateAuthority = resp.Name
|
2021-08-05 03:15:26 +00:00
|
|
|
p.Files[p.Intermediate] = encodeCertificate(resp.Certificate)
|
2021-10-08 00:28:39 +00:00
|
|
|
|
|
|
|
// Replace the key name with the one from the key manager. On softcas this
|
|
|
|
// will be the original filename, on any other kms will be the uri to the
|
|
|
|
// key.
|
|
|
|
if resp.KeyName != "" {
|
|
|
|
p.IntermediateKey = resp.KeyName
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a kms is used it will not have the private key
|
|
|
|
if resp.PrivateKey != nil {
|
|
|
|
p.Files[p.IntermediateKey], err = encodePrivateKey(resp.PrivateKey, pass)
|
|
|
|
}
|
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
return err
|
2020-10-23 22:04:09 +00:00
|
|
|
}
|
2020-10-20 01:42:03 +00:00
|
|
|
|
2020-10-26 23:43:44 +00:00
|
|
|
// CreateCertificateAuthorityResponse returns a
|
|
|
|
// CreateCertificateAuthorityResponse that can be used as a parent of a
|
|
|
|
// CreateCertificateAuthority request.
|
|
|
|
func (p *PKI) CreateCertificateAuthorityResponse(cert *x509.Certificate, key crypto.PrivateKey) *apiv1.CreateCertificateAuthorityResponse {
|
|
|
|
signer, _ := key.(crypto.Signer)
|
|
|
|
return &apiv1.CreateCertificateAuthorityResponse{
|
|
|
|
Certificate: cert,
|
|
|
|
PrivateKey: key,
|
|
|
|
Signer: signer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:04:09 +00:00
|
|
|
// GetCertificateAuthority attempts to load the certificate authority from the
|
|
|
|
// RA.
|
|
|
|
func (p *PKI) GetCertificateAuthority() error {
|
2021-08-02 23:07:30 +00:00
|
|
|
srv, ok := p.caService.(apiv1.CertificateAuthorityGetter)
|
2020-10-20 01:42:03 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := srv.GetCertificateAuthority(&apiv1.GetCertificateAuthorityRequest{
|
2020-10-23 22:04:09 +00:00
|
|
|
Name: p.casOptions.CertificateAuthority,
|
2020-10-20 01:42:03 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.WriteRootCertificate(resp.RootCertificate, nil, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issuer is in the RA
|
2021-08-05 03:15:26 +00:00
|
|
|
p.Intermediate = ""
|
|
|
|
p.IntermediateKey = ""
|
2020-10-20 01:42:03 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
// GenerateSSHSigningKeys generates and encrypts a private key used for signing
|
|
|
|
// SSH user certificates and a private key used for signing host certificates.
|
|
|
|
func (p *PKI) GenerateSSHSigningKeys(password []byte) error {
|
2021-10-08 00:28:39 +00:00
|
|
|
// Enable SSH
|
2022-10-14 11:52:27 +00:00
|
|
|
p.options.enableSSH = true // TODO(hs): change this function to not mutate configuration state
|
2021-10-08 00:28:39 +00:00
|
|
|
|
|
|
|
// Create SSH key used to sign host certificates. Using
|
|
|
|
// kmsapi.UnspecifiedSignAlgorithm will default to the default algorithm.
|
2021-10-08 21:25:24 +00:00
|
|
|
name := p.Ssh.HostKey
|
2021-10-08 00:28:39 +00:00
|
|
|
if uri := p.options.hostKeyURI; uri != "" {
|
|
|
|
name = uri
|
|
|
|
}
|
|
|
|
resp, err := p.keyManager.CreateKey(&kmsapi.CreateKeyRequest{
|
|
|
|
Name: name,
|
|
|
|
SignatureAlgorithm: kmsapi.UnspecifiedSignAlgorithm,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sshKey, err := ssh.NewPublicKey(resp.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error converting public key")
|
|
|
|
}
|
2021-10-08 21:25:24 +00:00
|
|
|
p.Files[p.Ssh.HostPublicKey] = ssh.MarshalAuthorizedKey(sshKey)
|
2021-10-08 00:28:39 +00:00
|
|
|
|
|
|
|
// On softkms we will have the private key
|
|
|
|
if resp.PrivateKey != nil {
|
|
|
|
p.Files[p.Ssh.HostKey], err = encodePrivateKey(resp.PrivateKey, password)
|
2019-09-13 19:11:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-08 21:25:24 +00:00
|
|
|
} else {
|
|
|
|
p.Ssh.HostKey = resp.Name
|
2021-10-08 00:28:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create SSH key used to sign user certificates. Using
|
|
|
|
// kmsapi.UnspecifiedSignAlgorithm will default to the default algorithm.
|
2021-10-08 21:25:24 +00:00
|
|
|
name = p.Ssh.UserKey
|
2021-10-08 00:28:39 +00:00
|
|
|
if uri := p.options.userKeyURI; uri != "" {
|
|
|
|
name = uri
|
|
|
|
}
|
|
|
|
resp, err = p.keyManager.CreateKey(&kmsapi.CreateKeyRequest{
|
|
|
|
Name: name,
|
|
|
|
SignatureAlgorithm: kmsapi.UnspecifiedSignAlgorithm,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sshKey, err = ssh.NewPublicKey(resp.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error converting public key")
|
|
|
|
}
|
2021-10-08 21:25:24 +00:00
|
|
|
p.Files[p.Ssh.UserPublicKey] = ssh.MarshalAuthorizedKey(sshKey)
|
2021-10-08 00:28:39 +00:00
|
|
|
|
|
|
|
// On softkms we will have the private key
|
|
|
|
if resp.PrivateKey != nil {
|
|
|
|
p.Files[p.Ssh.UserKey], err = encodePrivateKey(resp.PrivateKey, password)
|
2019-09-13 19:11:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-08 21:25:24 +00:00
|
|
|
} else {
|
|
|
|
p.Ssh.UserKey = resp.Name
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
2021-10-08 00:28:39 +00:00
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// WriteFiles writes on disk the previously generated files.
|
|
|
|
func (p *PKI) WriteFiles() error {
|
|
|
|
for fn, b := range p.Files {
|
|
|
|
if err := fileutil.WriteFile(fn, b, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 19:11:46 +00:00
|
|
|
func (p *PKI) askFeedback() {
|
|
|
|
ui.Println()
|
2021-08-10 21:54:31 +00:00
|
|
|
ui.Println("\033[1mFEEDBACK\033[0m 😍 🍻")
|
|
|
|
ui.Println(" The \033[1mstep\033[0m utility is not instrumented for usage statistics. It does not phone")
|
|
|
|
ui.Println(" home. But your feedback is extremely valuable. Any information you can provide")
|
|
|
|
ui.Println(" regarding how you’re using `step` helps. Please send us a sentence or two,")
|
|
|
|
ui.Println(" good or bad at \033[1mfeedback@smallstep.com\033[0m or join GitHub Discussions")
|
|
|
|
ui.Println(" \033[1mhttps://github.com/smallstep/certificates/discussions\033[0m and our Discord ")
|
2021-08-11 00:07:15 +00:00
|
|
|
ui.Println(" \033[1mhttps://u.step.sm/discord\033[0m.")
|
2021-08-10 21:54:31 +00:00
|
|
|
|
|
|
|
if p.options.deploymentType == LinkedDeployment {
|
|
|
|
ui.Println()
|
|
|
|
ui.Println("\033[1mNEXT STEPS\033[0m")
|
|
|
|
ui.Println(" 1. Log in or create a Certificate Manager account at \033[1mhttps://u.step.sm/linked\033[0m")
|
2021-08-13 18:59:12 +00:00
|
|
|
ui.Println(" 2. Add a new authority and select \"Link a step-ca instance\"")
|
2021-08-10 21:54:31 +00:00
|
|
|
ui.Println(" 3. Follow instructions in browser to start `step-ca` using the `--token` flag")
|
|
|
|
ui.Println()
|
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PKI) tellPKI() {
|
|
|
|
ui.Println()
|
2021-10-08 18:59:57 +00:00
|
|
|
switch {
|
|
|
|
case p.casOptions.Is(apiv1.SoftCAS):
|
2021-08-05 03:15:26 +00:00
|
|
|
ui.PrintSelected("Root certificate", p.Root[0])
|
|
|
|
ui.PrintSelected("Root private key", p.RootKey[0])
|
|
|
|
ui.PrintSelected("Root fingerprint", p.Defaults.Fingerprint)
|
|
|
|
ui.PrintSelected("Intermediate certificate", p.Intermediate)
|
|
|
|
ui.PrintSelected("Intermediate private key", p.IntermediateKey)
|
2021-10-08 18:59:57 +00:00
|
|
|
case p.Defaults.Fingerprint != "":
|
2021-08-05 03:15:26 +00:00
|
|
|
ui.PrintSelected("Root certificate", p.Root[0])
|
|
|
|
ui.PrintSelected("Root fingerprint", p.Defaults.Fingerprint)
|
2021-10-08 18:59:57 +00:00
|
|
|
default:
|
2020-10-20 01:42:03 +00:00
|
|
|
ui.Printf(`{{ "%s" | red }} {{ "Root certificate:" | bold }} failed to retrieve it from RA`+"\n", ui.IconBad)
|
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
if p.options.enableSSH {
|
2021-08-05 03:15:26 +00:00
|
|
|
ui.PrintSelected("SSH user public key", p.Ssh.UserPublicKey)
|
|
|
|
ui.PrintSelected("SSH user private key", p.Ssh.UserKey)
|
|
|
|
ui.PrintSelected("SSH host public key", p.Ssh.HostPublicKey)
|
|
|
|
ui.PrintSelected("SSH host private key", p.Ssh.HostKey)
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type caDefaults struct {
|
|
|
|
CAUrl string `json:"ca-url"`
|
|
|
|
CAConfig string `json:"ca-config"`
|
|
|
|
Fingerprint string `json:"fingerprint"`
|
|
|
|
Root string `json:"root"`
|
|
|
|
}
|
|
|
|
|
2021-08-06 21:58:03 +00:00
|
|
|
// ConfigOption is the type for modifiers over the auth config object.
|
|
|
|
type ConfigOption func(c *authconfig.Config) error
|
2019-09-13 19:11:46 +00:00
|
|
|
|
|
|
|
// GenerateConfig returns the step certificates configuration.
|
2021-08-06 21:58:03 +00:00
|
|
|
func (p *PKI) GenerateConfig(opt ...ConfigOption) (*authconfig.Config, error) {
|
2020-10-23 22:04:09 +00:00
|
|
|
var authorityOptions *apiv1.Options
|
|
|
|
if !p.casOptions.Is(apiv1.SoftCAS) {
|
|
|
|
authorityOptions = &p.casOptions
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg := &authconfig.Config{
|
2021-08-05 03:15:26 +00:00
|
|
|
Root: p.Root,
|
|
|
|
FederatedRoots: p.FederatedRoots,
|
|
|
|
IntermediateCert: p.Intermediate,
|
|
|
|
IntermediateKey: p.IntermediateKey,
|
|
|
|
Address: p.Address,
|
|
|
|
DNSNames: p.DnsNames,
|
2019-09-13 19:11:46 +00:00
|
|
|
Logger: []byte(`{"format": "text"}`),
|
|
|
|
DB: &db.Config{
|
2021-08-11 18:19:29 +00:00
|
|
|
Type: "badgerv2",
|
2019-09-13 19:11:46 +00:00
|
|
|
DataSource: GetDBPath(),
|
|
|
|
},
|
2021-05-03 19:48:20 +00:00
|
|
|
AuthorityConfig: &authconfig.AuthConfig{
|
2020-10-23 22:04:09 +00:00
|
|
|
Options: authorityOptions,
|
2019-09-13 19:11:46 +00:00
|
|
|
DisableIssuedAtCheck: false,
|
2021-08-02 23:07:30 +00:00
|
|
|
EnableAdmin: false,
|
2019-09-13 19:11:46 +00:00
|
|
|
},
|
2021-08-02 23:07:30 +00:00
|
|
|
TLS: &authconfig.DefaultTLSOptions,
|
2019-10-11 19:49:09 +00:00
|
|
|
Templates: p.getTemplates(),
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
|
2023-02-24 23:40:48 +00:00
|
|
|
// Disable the database when WithNoDB() option is passed.
|
|
|
|
if p.options.noDB {
|
|
|
|
cfg.DB = nil
|
|
|
|
}
|
|
|
|
|
2021-08-11 00:07:15 +00:00
|
|
|
// Add linked as a deployment type to detect it on start and provide a
|
|
|
|
// message if the token is not given.
|
|
|
|
if p.options.deploymentType == LinkedDeployment {
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg.AuthorityConfig.DeploymentType = LinkedDeployment.String()
|
2021-08-11 00:07:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 00:28:39 +00:00
|
|
|
// Enable KMS if necessary
|
|
|
|
if p.Kms != nil {
|
2022-08-09 00:58:18 +00:00
|
|
|
typ := strings.ToLower(p.Kms.Type.String())
|
2021-10-12 22:28:08 +00:00
|
|
|
cfg.KMS = &kmsapi.Options{
|
2022-08-09 00:58:18 +00:00
|
|
|
Type: kmsapi.Type(typ),
|
2021-10-08 00:28:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
// On standalone deployments add the provisioners to either the ca.json or
|
|
|
|
// the database.
|
|
|
|
var provisioners []provisioner.Interface
|
|
|
|
if p.options.deploymentType == StandaloneDeployment {
|
|
|
|
key, err := p.ottPrivateKey.CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error serializing private key")
|
|
|
|
}
|
|
|
|
|
|
|
|
prov := &provisioner.JWK{
|
|
|
|
Name: p.options.provisioner,
|
|
|
|
Type: "JWK",
|
|
|
|
Key: p.ottPublicKey,
|
|
|
|
EncryptedKey: key,
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
provisioners = append(provisioners, prov)
|
|
|
|
|
|
|
|
// Add default ACME provisioner if enabled
|
|
|
|
if p.options.enableACME {
|
|
|
|
provisioners = append(provisioners, &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
})
|
2019-09-26 22:23:32 +00:00
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
|
|
|
|
if p.options.enableSSH {
|
|
|
|
enableSSHCA := true
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg.SSH = &authconfig.SSHConfig{
|
2021-08-05 03:15:26 +00:00
|
|
|
HostKey: p.Ssh.HostKey,
|
|
|
|
UserKey: p.Ssh.UserKey,
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
// Enable SSH authorization for default JWK provisioner
|
|
|
|
prov.Claims = &provisioner.Claims{
|
2020-07-31 17:32:08 +00:00
|
|
|
EnableSSHCA: &enableSSHCA,
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add default SSHPOP provisioner
|
|
|
|
provisioners = append(provisioners, &provisioner.SSHPOP{
|
|
|
|
Type: "SSHPOP",
|
|
|
|
Name: "sshpop",
|
|
|
|
Claims: &provisioner.Claims{
|
|
|
|
EnableSSHCA: &enableSSHCA,
|
|
|
|
},
|
|
|
|
})
|
2020-07-31 17:32:08 +00:00
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply configuration modifiers
|
|
|
|
for _, o := range opt {
|
2021-10-08 18:59:57 +00:00
|
|
|
if err := o(cfg); err != nil {
|
2019-09-13 19:11:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 23:07:30 +00:00
|
|
|
// Set authority.enableAdmin to true
|
|
|
|
if p.options.enableAdmin {
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg.AuthorityConfig.EnableAdmin = true
|
2021-08-02 23:07:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.options.deploymentType == StandaloneDeployment {
|
2021-10-08 18:59:57 +00:00
|
|
|
if !cfg.AuthorityConfig.EnableAdmin {
|
|
|
|
cfg.AuthorityConfig.Provisioners = provisioners
|
2021-08-02 23:07:30 +00:00
|
|
|
} else {
|
2021-08-23 22:33:16 +00:00
|
|
|
// At this moment this code path is never used because `step ca
|
|
|
|
// init` will always set enableAdmin to false for a standalone
|
|
|
|
// deployment. Once we move `step beta` commands out of the beta we
|
|
|
|
// should probably default to this route.
|
|
|
|
//
|
|
|
|
// Note that we might want to be able to define the database as a
|
|
|
|
// flag in `step ca init` so we can write to the proper place.
|
2022-10-14 14:01:18 +00:00
|
|
|
//
|
|
|
|
// TODO(hs): the logic for creating the provisioners and the super admin
|
|
|
|
// is similar to what's done when automatically migrating the provisioners.
|
|
|
|
// This is related to the existing comment above. Refactor this to exist in
|
2022-10-21 09:11:42 +00:00
|
|
|
// a single place and ensure it happens only once.
|
2021-10-08 18:59:57 +00:00
|
|
|
_db, err := db.New(cfg.DB)
|
2021-08-02 23:07:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
adminDB, err := admindb.New(_db.(nosql.DB), admin.DefaultAuthorityID)
|
2021-08-02 23:07:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Add all the provisioners to the db.
|
|
|
|
var adminID string
|
|
|
|
for i, p := range provisioners {
|
|
|
|
prov, err := authority.ProvisionerToLinkedca(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := adminDB.CreateProvisioner(context.Background(), prov); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if i == 0 {
|
|
|
|
adminID = prov.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Add the first provisioner as an admin.
|
2022-10-24 12:51:27 +00:00
|
|
|
superAdminSubject := "step"
|
|
|
|
if p.options.superAdminSubject != "" {
|
|
|
|
superAdminSubject = p.options.superAdminSubject
|
2022-10-14 14:01:18 +00:00
|
|
|
}
|
2021-08-02 23:07:30 +00:00
|
|
|
if err := adminDB.CreateAdmin(context.Background(), &linkedca.Admin{
|
|
|
|
AuthorityId: admin.DefaultAuthorityID,
|
2022-10-24 12:51:27 +00:00
|
|
|
Subject: superAdminSubject,
|
2021-08-02 23:07:30 +00:00
|
|
|
Type: linkedca.Admin_SUPER_ADMIN,
|
|
|
|
ProvisionerId: adminID,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
return cfg, nil
|
2019-09-13 19:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save stores the pki on a json file that will be used as the certificate
|
|
|
|
// authority configuration.
|
2021-08-06 21:58:03 +00:00
|
|
|
func (p *PKI) Save(opt ...ConfigOption) error {
|
2021-08-05 22:57:13 +00:00
|
|
|
// Write generated files
|
|
|
|
if err := p.WriteFiles(); err != nil {
|
|
|
|
return err
|
2021-08-05 19:58:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-23 22:29:18 +00:00
|
|
|
// Display the files written
|
2019-09-13 19:11:46 +00:00
|
|
|
p.tellPKI()
|
|
|
|
|
2019-10-11 19:49:09 +00:00
|
|
|
// Generate and write ca.json
|
2021-08-05 22:57:13 +00:00
|
|
|
if !p.options.pkiOnly {
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg, err := p.GenerateConfig(opt...)
|
2021-08-05 22:57:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
b, err := json.MarshalIndent(cfg, "", "\t")
|
2021-08-05 22:57:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error marshaling %s", p.config)
|
|
|
|
}
|
|
|
|
if err = fileutil.WriteFile(p.config, b, 0644); err != nil {
|
|
|
|
return errs.FileError(err, p.config)
|
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// Generate and write defaults.json
|
|
|
|
defaults := &caDefaults{
|
|
|
|
Root: p.Defaults.Root,
|
|
|
|
CAConfig: p.Defaults.CaConfig,
|
|
|
|
CAUrl: p.Defaults.CaUrl,
|
|
|
|
Fingerprint: p.Defaults.Fingerprint,
|
|
|
|
}
|
|
|
|
b, err = json.MarshalIndent(defaults, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error marshaling %s", p.defaults)
|
|
|
|
}
|
|
|
|
if err = fileutil.WriteFile(p.defaults, b, 0644); err != nil {
|
|
|
|
return errs.FileError(err, p.defaults)
|
|
|
|
}
|
2021-11-17 19:40:01 +00:00
|
|
|
// If we're using contexts then write a blank object to the default profile
|
2021-11-15 18:20:10 +00:00
|
|
|
// configuration location.
|
2021-11-11 21:49:32 +00:00
|
|
|
if p.profileDefaults != "" {
|
2021-11-12 06:05:47 +00:00
|
|
|
if _, err := os.Stat(p.profileDefaults); os.IsNotExist(err) {
|
2021-11-15 18:20:10 +00:00
|
|
|
// Write with 0600 to be consistent with directories structure.
|
|
|
|
if err = fileutil.WriteFile(p.profileDefaults, []byte("{}"), 0600); err != nil {
|
2021-11-12 06:05:47 +00:00
|
|
|
return errs.FileError(err, p.profileDefaults)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
2021-11-11 21:49:32 +00:00
|
|
|
return errs.FileError(err, p.profileDefaults)
|
|
|
|
}
|
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
// Generate and write templates
|
2021-10-08 18:59:57 +00:00
|
|
|
if err := generateTemplates(cfg.Templates); err != nil {
|
2021-08-05 22:57:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-10-11 19:49:09 +00:00
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
if cfg.DB != nil {
|
2022-01-12 20:35:37 +00:00
|
|
|
os.MkdirAll(cfg.DB.DataSource, 0700)
|
2021-10-08 18:59:57 +00:00
|
|
|
ui.PrintSelected("Database folder", cfg.DB.DataSource)
|
2021-08-05 22:57:13 +00:00
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
if cfg.Templates != nil {
|
2021-08-05 22:57:13 +00:00
|
|
|
ui.PrintSelected("Templates folder", GetTemplatesPath())
|
|
|
|
}
|
2019-10-11 19:49:09 +00:00
|
|
|
|
2021-08-05 22:57:13 +00:00
|
|
|
ui.PrintSelected("Default configuration", p.defaults)
|
2021-08-13 07:16:41 +00:00
|
|
|
if p.profileDefaults != "" {
|
2021-11-11 21:49:32 +00:00
|
|
|
ui.PrintSelected("Default profile configuration", p.profileDefaults)
|
2021-08-13 07:16:41 +00:00
|
|
|
}
|
2021-08-05 22:57:13 +00:00
|
|
|
ui.PrintSelected("Certificate Authority configuration", p.config)
|
2022-10-25 09:47:54 +00:00
|
|
|
if cfg.AuthorityConfig.EnableAdmin && p.options.deploymentType != LinkedDeployment {
|
|
|
|
// TODO(hs): we may want to get this information from the DB, because that's
|
|
|
|
// where the admin and provisioner are stored in this case. Requires some
|
|
|
|
// refactoring.
|
|
|
|
superAdminSubject := "step"
|
|
|
|
if p.options.superAdminSubject != "" {
|
|
|
|
superAdminSubject = p.options.superAdminSubject
|
|
|
|
}
|
|
|
|
ui.PrintSelected("Admin provisioner", fmt.Sprintf("%s (JWK)", p.options.provisioner))
|
|
|
|
ui.PrintSelected("Super admin subject", superAdminSubject)
|
|
|
|
}
|
|
|
|
|
2021-08-10 21:54:31 +00:00
|
|
|
if p.options.deploymentType != LinkedDeployment {
|
|
|
|
ui.Println()
|
|
|
|
if p.casOptions.Is(apiv1.SoftCAS) {
|
|
|
|
ui.Println("Your PKI is ready to go. To generate certificates for individual services see 'step help ca'.")
|
|
|
|
} else {
|
|
|
|
ui.Println("Your registration authority is ready to go. To generate certificates for individual services see 'step help ca'.")
|
|
|
|
}
|
2021-08-05 22:57:13 +00:00
|
|
|
}
|
2020-10-20 01:42:03 +00:00
|
|
|
}
|
2019-09-13 19:11:46 +00:00
|
|
|
|
|
|
|
p.askFeedback()
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-05 03:15:26 +00:00
|
|
|
|
|
|
|
func encodeCertificate(c *x509.Certificate) []byte {
|
|
|
|
return pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: c.Raw,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodePrivateKey(key crypto.PrivateKey, pass []byte) ([]byte, error) {
|
|
|
|
block, err := pemutil.Serialize(key, pemutil.WithPassword(pass))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pem.EncodeToMemory(block), nil
|
|
|
|
}
|