Skip to content

Commit

Permalink
Add a -days flag to specify the validity period.
Browse files Browse the repository at this point in the history
The hard-coded default of 2 years, 3 months works for most applications.
However, some applications enforce that the certificate is only valid
for a short period and this default is too long.

For example, WebRTC fingerprinting enforces a max duration of 30 days.
WebTransport is even more extreme and rejects certs valid for more than
14 days. These certificates are meant to be ephemeral.

Fixes FiloSottile#339 FiloSottile#343
  • Loading branch information
kixelated committed Mar 25, 2023
1 parent 2a46726 commit 6d3982e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (m *mkcert) makeCert(hosts []string) {
// Certificates last for 2 years and 3 months, which is always less than
// 825 days, the limit that macOS/iOS apply to all certificates,
// including custom roots. See https://support.apple.com/en-us/HT210176.
expiration := time.Now().AddDate(2, 3, 0)
expiration := time.Now().AddDate(0, 0, m.days)

tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Expand Down Expand Up @@ -225,7 +225,7 @@ func (m *mkcert) makeCertFromCSR() {
fatalIfErr(err, "failed to parse the CSR")
fatalIfErr(csr.CheckSignature(), "invalid CSR signature")

expiration := time.Now().AddDate(2, 3, 0)
expiration := time.Now().AddDate(0, 0, m.days)
tpl := &x509.Certificate{
SerialNumber: randomSerialNumber(),
Subject: csr.Subject,
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ const advancedUsage = `Advanced options:
-CAROOT
Print the CA certificate and key storage location.
-days INT
Generate a certificate valid for the specified number of days.
The default is 810, which is just under the macOS/iOS limit of 825.
$CAROOT (environment variable)
Set the CA certificate and key storage location. (This allows
maintaining multiple local CAs in parallel.)
Expand Down Expand Up @@ -103,6 +107,7 @@ func main() {
keyFileFlag = flag.String("key-file", "", "")
p12FileFlag = flag.String("p12-file", "", "")
versionFlag = flag.Bool("version", false, "")
daysFlag = flag.Int("days", 810, "")
)
flag.Usage = func() {
fmt.Fprint(flag.CommandLine.Output(), shortUsage)
Expand Down Expand Up @@ -146,6 +151,7 @@ func main() {
installMode: *installFlag, uninstallMode: *uninstallFlag, csrPath: *csrFlag,
pkcs12: *pkcs12Flag, ecdsa: *ecdsaFlag, client: *clientFlag,
certFile: *certFileFlag, keyFile: *keyFileFlag, p12File: *p12FileFlag,
days: *daysFlag,
}).Run(flag.Args())
}

Expand All @@ -157,6 +163,7 @@ type mkcert struct {
pkcs12, ecdsa, client bool
keyFile, certFile, p12File string
csrPath string
days int

CAROOT string
caCert *x509.Certificate
Expand Down

0 comments on commit 6d3982e

Please sign in to comment.