Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add option to create CA restricted to some domains #94

Merged
merged 1 commit into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func NewInitCommand() cli.Command {
Name: "stdout",
Usage: "Print certificate to stdout in addition to saving file",
},
cli.StringSliceFlag{
Name: "permit-domain",
Usage: "Create a CA restricted to subdomains of this domain (can be specified multiple times)",
},
},
Action: initAction,
}
Expand Down Expand Up @@ -150,7 +154,7 @@ func initAction(c *cli.Context) {
}
}

crt, err := pkix.CreateCertificateAuthority(key, c.String("organizational-unit"), expiresTime, c.String("organization"), c.String("country"), c.String("province"), c.String("locality"), c.String("common-name"))
crt, err := pkix.CreateCertificateAuthority(key, c.String("organizational-unit"), expiresTime, c.String("organization"), c.String("country"), c.String("province"), c.String("locality"), c.String("common-name"), c.StringSlice("permit-domain"))
if err != nil {
fmt.Fprintln(os.Stderr, "Create certificate error:", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func setupCA(t *testing.T, dt depot.Depot) {
}

// create certificate authority
caCert, err := pkix.CreateCertificateAuthority(key, caName, time.Now().Add(1*time.Minute), "", "", "", "", caName)
caCert, err := pkix.CreateCertificateAuthority(key, caName, time.Now().Add(1*time.Minute), "", "", "", "", caName, nil)
if err != nil {
t.Fatalf("could not create authority cert: %v", err)
}
Expand Down
7 changes: 6 additions & 1 deletion pkix/cert_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// CreateCertificateAuthority creates Certificate Authority using existing key.
// CertificateAuthorityInfo returned is the extra infomation required by Certificate Authority.
func CreateCertificateAuthority(key *Key, organizationalUnit string, expiry time.Time, organization string, country string, province string, locality string, commonName string) (*Certificate, error) {
func CreateCertificateAuthority(key *Key, organizationalUnit string, expiry time.Time, organization string, country string, province string, locality string, commonName string, permitDomains []string) (*Certificate, error) {
authTemplate := newAuthTemplate()

subjectKeyID, err := GenerateSubjectKeyID(key.Public)
Expand Down Expand Up @@ -54,6 +54,11 @@ func CreateCertificateAuthority(key *Key, organizationalUnit string, expiry time
authTemplate.Subject.CommonName = commonName
}

if len(permitDomains) > 0 {
authTemplate.PermittedDNSDomainsCritical = true
authTemplate.PermittedDNSDomains = permitDomains
}

crtBytes, err := x509.CreateCertificate(rand.Reader, &authTemplate, &authTemplate, key.Public, key.Private)
if err != nil {
return nil, err
Expand Down
14 changes: 13 additions & 1 deletion pkix/cert_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCreateCertificateAuthority(t *testing.T) {
t.Fatal("Failed creating rsa key:", err)
}

crt, err := CreateCertificateAuthority(key, "OU", time.Now().AddDate(5, 0, 0), "test", "US", "California", "San Francisco", "CA Name")
crt, err := CreateCertificateAuthority(key, "OU", time.Now().AddDate(5, 0, 0), "test", "US", "California", "San Francisco", "CA Name", []string{".example.com"})
if err != nil {
t.Fatal("Failed creating certificate authority:", err)
}
Expand All @@ -52,4 +52,16 @@ func TestCreateCertificateAuthority(t *testing.T) {
if !time.Now().Before(rawCrt.NotAfter) {
t.Fatal("Failed to be before NotAfter")
}

if crt.crt.PermittedDNSDomainsCritical != true {
t.Fatal("Permitted DNS Domains is not set to critical")
}

if len(crt.crt.PermittedDNSDomains) != 1 {
t.Fatal("More than one entry found in list of permitted DNS domains")
}

if crt.crt.PermittedDNSDomains[0] != ".example.com" {
t.Fatalf("Wrong permitted DNS domain, want %q, got %q", ".example.com", crt.crt.PermittedDNSDomains[0])
}
}
2 changes: 1 addition & 1 deletion pkix/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestCreateCertificateRevocationList(t *testing.T) {
t.Fatal("Failed creating rsa key:", err)
}

crt, err := CreateCertificateAuthority(key, "OU", time.Now().AddDate(5, 0, 0), "test", "US", "California", "San Francisco", "CA Name")
crt, err := CreateCertificateAuthority(key, "OU", time.Now().AddDate(5, 0, 0), "test", "US", "California", "San Francisco", "CA Name", nil)
if err != nil {
t.Fatal("Failed creating certificate authority:", err)
}
Expand Down