Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: allow setting max pathlen constraint #487

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 27 additions & 16 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"io/ioutil"
"log"
"math/big"
"net"
Expand Down Expand Up @@ -71,6 +70,9 @@ func (m *mkcert) makeCert(hosts []string) {
NotBefore: time.Now(), NotAfter: expiration,

KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,

BasicConstraintsValid: m.isCA,
IsCA: m.isCA,
}

for _, h := range hosts {
Expand Down Expand Up @@ -113,19 +115,19 @@ func (m *mkcert) makeCert(hosts []string) {
privPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privDER})

if certFile == keyFile {
err = ioutil.WriteFile(keyFile, append(certPEM, privPEM...), 0600)
err = os.WriteFile(keyFile, append(certPEM, privPEM...), 0600)
fatalIfErr(err, "failed to save certificate and key")
} else {
err = ioutil.WriteFile(certFile, certPEM, 0644)
err = os.WriteFile(certFile, certPEM, 0644)
fatalIfErr(err, "failed to save certificate")
err = ioutil.WriteFile(keyFile, privPEM, 0600)
err = os.WriteFile(keyFile, privPEM, 0600)
fatalIfErr(err, "failed to save certificate key")
}
} else {
domainCert, _ := x509.ParseCertificate(cert)
pfxData, err := pkcs12.Encode(rand.Reader, priv, domainCert, []*x509.Certificate{m.caCert}, "changeit")
fatalIfErr(err, "failed to generate PKCS#12")
err = ioutil.WriteFile(p12File, pfxData, 0644)
err = os.WriteFile(p12File, pfxData, 0644)
fatalIfErr(err, "failed to save PKCS#12")
}

Expand Down Expand Up @@ -211,7 +213,7 @@ func (m *mkcert) makeCertFromCSR() {
log.Fatalln("ERROR: can't create new certificates because the CA key (rootCA-key.pem) is missing")
}

csrPEMBytes, err := ioutil.ReadFile(m.csrPath)
csrPEMBytes, err := os.ReadFile(m.csrPath)
fatalIfErr(err, "failed to read the CSR")
csrPEM, _ := pem.Decode(csrPEMBytes)
if csrPEM == nil {
Expand All @@ -226,12 +228,17 @@ func (m *mkcert) makeCertFromCSR() {
fatalIfErr(csr.CheckSignature(), "invalid CSR signature")

expiration := time.Now().AddDate(2, 3, 0)

keyUsage := x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
if m.isCA {
keyUsage = x509.KeyUsageCertSign | x509.KeyUsageDigitalSignature
}

tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: csr.Subject,
ExtraExtensions: csr.Extensions, // includes requested SANs, KUs and EKUs

NotBefore: time.Now(), NotAfter: expiration,
NotBefore: time.Now(), NotAfter: expiration,

// If the CSR does not request a SAN extension, fix it up for them as
// the Common Name field does not work in modern browsers. Otherwise,
Expand All @@ -240,8 +247,11 @@ func (m *mkcert) makeCertFromCSR() {

// Likewise, if the CSR does not set KUs and EKUs, fix it up as Apple
// platforms require serverAuth for TLS.
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
KeyUsage: keyUsage,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},

BasicConstraintsValid: m.isCA,
IsCA: m.isCA,
}

if m.client {
Expand All @@ -267,7 +277,7 @@ func (m *mkcert) makeCertFromCSR() {
}
certFile, _, _ := m.fileNames(hosts)

err = ioutil.WriteFile(certFile, pem.EncodeToMemory(
err = os.WriteFile(certFile, pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
fatalIfErr(err, "failed to save certificate")

Expand All @@ -284,7 +294,7 @@ func (m *mkcert) loadCA() {
m.newCA()
}

certPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
certPEMBlock, err := os.ReadFile(filepath.Join(m.CAROOT, rootName))
fatalIfErr(err, "failed to read the CA certificate")
certDERBlock, _ := pem.Decode(certPEMBlock)
if certDERBlock == nil || certDERBlock.Type != "CERTIFICATE" {
Expand All @@ -297,7 +307,7 @@ func (m *mkcert) loadCA() {
return // keyless mode, where only -install works
}

keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootKeyName))
keyPEMBlock, err := os.ReadFile(filepath.Join(m.CAROOT, rootKeyName))
fatalIfErr(err, "failed to read the CA key")
keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
Expand Down Expand Up @@ -344,19 +354,20 @@ func (m *mkcert) newCA() {

BasicConstraintsValid: true,
IsCA: true,
MaxPathLenZero: true,
MaxPathLenZero: m.maxPathLen == 0,
MaxPathLen: m.maxPathLen,
}

cert, err := x509.CreateCertificate(rand.Reader, tpl, tpl, pub, priv)
fatalIfErr(err, "failed to generate CA certificate")

privDER, err := x509.MarshalPKCS8PrivateKey(priv)
fatalIfErr(err, "failed to encode CA key")
err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootKeyName), pem.EncodeToMemory(
err = os.WriteFile(filepath.Join(m.CAROOT, rootKeyName), pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0400)
fatalIfErr(err, "failed to save CA key")

err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootName), pem.EncodeToMemory(
err = os.WriteFile(filepath.Join(m.CAROOT, rootName), pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
fatalIfErr(err, "failed to save CA certificate")

Expand Down
39 changes: 24 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ const advancedUsage = `Advanced options:
Generate a certificate based on the supplied CSR. Conflicts with
all other flags and arguments except -install and -cert-file.

-ca
Generate a certificate with the CA constraint enabled.

-max-path-len LEN
Generate the CA certificate with path length constraint set to LEN.

-CAROOT
Print the CA certificate and key storage location.

Expand All @@ -91,18 +97,20 @@ func main() {
}
log.SetFlags(0)
var (
installFlag = flag.Bool("install", false, "")
uninstallFlag = flag.Bool("uninstall", false, "")
pkcs12Flag = flag.Bool("pkcs12", false, "")
ecdsaFlag = flag.Bool("ecdsa", false, "")
clientFlag = flag.Bool("client", false, "")
helpFlag = flag.Bool("help", false, "")
carootFlag = flag.Bool("CAROOT", false, "")
csrFlag = flag.String("csr", "", "")
certFileFlag = flag.String("cert-file", "", "")
keyFileFlag = flag.String("key-file", "", "")
p12FileFlag = flag.String("p12-file", "", "")
versionFlag = flag.Bool("version", false, "")
installFlag = flag.Bool("install", false, "")
uninstallFlag = flag.Bool("uninstall", false, "")
pkcs12Flag = flag.Bool("pkcs12", false, "")
ecdsaFlag = flag.Bool("ecdsa", false, "")
clientFlag = flag.Bool("client", false, "")
helpFlag = flag.Bool("help", false, "")
carootFlag = flag.Bool("CAROOT", false, "")
csrFlag = flag.String("csr", "", "")
certFileFlag = flag.String("cert-file", "", "")
keyFileFlag = flag.String("key-file", "", "")
p12FileFlag = flag.String("p12-file", "", "")
caFlag = flag.Bool("ca", false, "")
maxPathLenFlag = flag.Int("max-path-len", 0, "")
versionFlag = flag.Bool("version", false, "")
)
flag.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), shortUsage)
Expand Down Expand Up @@ -139,13 +147,12 @@ func main() {
if *csrFlag != "" && (*pkcs12Flag || *ecdsaFlag || *clientFlag) {
log.Fatalln("ERROR: can only combine -csr with -install and -cert-file")
}
if *csrFlag != "" && flag.NArg() != 0 {
log.Fatalln("ERROR: can't specify extra arguments when using -csr")
}
(&mkcert{
installMode: *installFlag, uninstallMode: *uninstallFlag, csrPath: *csrFlag,
pkcs12: *pkcs12Flag, ecdsa: *ecdsaFlag, client: *clientFlag,
certFile: *certFileFlag, keyFile: *keyFileFlag, p12File: *p12FileFlag,
maxPathLen: *maxPathLenFlag,
isCA: *caFlag,
}).Run(flag.Args())
}

Expand All @@ -157,6 +164,8 @@ type mkcert struct {
pkcs12, ecdsa, client bool
keyFile, certFile, p12File string
csrPath string
maxPathLen int
isCA bool

CAROOT string
caCert *x509.Certificate
Expand Down
3 changes: 1 addition & 2 deletions truststore_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -59,7 +58,7 @@ func (m *mkcert) installPlatform() bool {
return false
}

cert, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
cert, err := os.ReadFile(filepath.Join(m.CAROOT, rootName))
fatalIfErr(err, "failed to read root certificate")

cmd := commandWithSudo("tee", m.systemTrustFilename())
Expand Down