10 Commits

Author SHA1 Message Date
Filippo Valsorda
da4da8a4bc Refactor output path flags 2019-01-06 19:07:18 -05:00
单元源
5ea72c377d Add -cert-file, -key-file and -p12-file (#77) 2019-01-06 19:07:18 -05:00
Filippo Valsorda
0d0636e824 Print a warning about wildcard depth in X.509
Fixes #60
2019-01-06 18:23:59 -05:00
Filippo Valsorda
6f4ab38307 Use double quotes in examples for CMD.exe support
See https://github.com/FiloSottile/mkcert/issues/60#issuecomment-415352403
2019-01-06 18:14:39 -05:00
Filippo Valsorda
7fbb1febce Document NODE_EXTRA_CA_CERTS for Node.js
Fixes #78
2019-01-06 18:07:49 -05:00
Filippo Valsorda
6060e206a4 Document the hardcoded PKCS#12 password
PKCS#12 encryption is legacy and we don't want to encourage relying on
it by making the password configurable. Some systems require the default
"changeit", so stick with that.

Fixes #86
Closes #58
Closes #87
2019-01-06 17:38:49 -05:00
Adam Shannon
ea716f38b3 java: don't attempt sudo retry on windows (#85)
* java: don't attempt retry on windows
* docs: mention "Run as Administrator" for windows if needed

Fixes #84
2018-12-22 17:10:28 -05:00
Adam Shannon
438ae98b1c java: don't try to run keytool during check if it's not available (#90)
If JAVA_HOME isn't set then keytoolPath has an invalid path. This means
checkJava() fails and doesn't tell the problem clearly to the user.

Fixes #88
2018-11-26 12:03:40 -05:00
Jurgen Verhasselt
afbc420fa0 README: update the AUR install instructions (#91) 2018-11-26 11:57:50 -05:00
Adam Shannon
48e7d200ae docs: clarify mkcert doens't configure servers (#82) 2018-10-25 23:15:37 +02:00
4 changed files with 75 additions and 25 deletions

View File

@@ -8,7 +8,7 @@ Created a new local CA at "/Users/filippo/Library/Application Support/mkcert"
The local CA is now installed in the system trust store! ⚡️ The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires restart)! 🦊 The local CA is now installed in the Firefox trust store (requires restart)! 🦊
$ mkcert example.com '*.example.org' myapp.dev localhost 127.0.0.1 ::1 $ mkcert example.com "*.example.org" myapp.dev localhost 127.0.0.1 ::1
Using the local CA at "/Users/filippo/Library/Application Support/mkcert" ✨ Using the local CA at "/Users/filippo/Library/Application Support/mkcert" ✨
Created a new certificate valid for the following names 📜 Created a new certificate valid for the following names 📜
@@ -26,7 +26,7 @@ The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.
Using certificates from real certificate authorities (CAs) for development can be dangerous or impossible (for hosts like `localhost` or `127.0.0.1`), but self-signed certificates cause trust errors. Managing your own CA is the best solution, but usually involves arcane commands, specialized knowledge and manual steps. Using certificates from real certificate authorities (CAs) for development can be dangerous or impossible (for hosts like `localhost` or `127.0.0.1`), but self-signed certificates cause trust errors. Managing your own CA is the best solution, but usually involves arcane commands, specialized knowledge and manual steps.
mkcert automatically creates and installs a local CA in the system root store, and generates locally-trusted certificates. mkcert automatically creates and installs a local CA in the system root store, and generates locally-trusted certificates. mkcert does not automatically configure servers to use the certificates, though, that's up to you.
## Installation ## Installation
@@ -76,10 +76,12 @@ $(go env GOPATH)/bin/mkcert
or use [the pre-built binaries](https://github.com/FiloSottile/mkcert/releases). or use [the pre-built binaries](https://github.com/FiloSottile/mkcert/releases).
On Arch Linux you can use your [AUR helper](https://wiki.archlinux.org/index.php/AUR_helpers) to install mkcert from the [PKGBUILD](https://aur.archlinux.org/packages/mkcert/). For Arch Linux users, mkcert is available from AUR as [`mkcert`](https://aur.archlinux.org/packages/mkcert/) or [`mkcert-git`](https://aur.archlinux.org/packages/mkcert-git/).
``` ```bash
yaourt -S mkcert git clone https://aur.archlinux.org/mkcert.git
cd mkcert
makepkg -si
``` ```
### Windows ### Windows
@@ -98,6 +100,8 @@ scoop install mkcert
or build from source (requires Go 1.10+), or use [the pre-built binaries](https://github.com/FiloSottile/mkcert/releases). or build from source (requires Go 1.10+), or use [the pre-built binaries](https://github.com/FiloSottile/mkcert/releases).
If you're running into permission problems try running `mkcert` as an Administrator.
## Supported root stores ## Supported root stores
mkcert supports the following root stores: mkcert supports the following root stores:
@@ -122,6 +126,14 @@ On iOS, you can either use AirDrop, email the CA to yourself, or serve it from a
For Android, you will have to install the CA and then enable user roots in the development build of your app. See [this StackOverflow answer](https://stackoverflow.com/a/22040887/749014). For Android, you will have to install the CA and then enable user roots in the development build of your app. See [this StackOverflow answer](https://stackoverflow.com/a/22040887/749014).
### Using the root with Node.js
Node does not use the system root store, so it won't accept mkcert certificates automatically. Instead, you will have to set the [`NODE_EXTRA_CA_CERTS`](https://nodejs.org/api/cli.html#cli_node_extra_ca_certs_file) environment variable.
```
export NODE_EXTRA_CA_CERTS="$(mkcert -CAROOT)/rootCA.pem"
```
### Changing the location of the CA files ### Changing the location of the CA files
The CA certificate and its key are stored in an application data folder in the user home. You usually don't have to worry about it, as installation is automated, but the location is printed by `mkcert -CAROOT`. The CA certificate and its key are stored in an application data folder in the user home. You usually don't have to worry about it, as installation is automated, but the location is printed by `mkcert -CAROOT`.

57
cert.go
View File

@@ -77,27 +77,23 @@ func (m *mkcert) makeCert(hosts []string) {
cert, err := x509.CreateCertificate(rand.Reader, tpl, m.caCert, &pub, m.caKey) cert, err := x509.CreateCertificate(rand.Reader, tpl, m.caCert, &pub, m.caKey)
fatalIfErr(err, "failed to generate certificate") fatalIfErr(err, "failed to generate certificate")
filename := strings.Replace(hosts[0], ":", "_", -1) certFile, keyFile, p12File := m.fileNames(hosts)
filename = strings.Replace(filename, "*", "_wildcard", -1)
if len(hosts) > 1 {
filename += "+" + strconv.Itoa(len(hosts)-1)
}
if !m.pkcs12 { if !m.pkcs12 {
privDER, err := x509.MarshalPKCS8PrivateKey(priv) privDER, err := x509.MarshalPKCS8PrivateKey(priv)
fatalIfErr(err, "failed to encode certificate key") fatalIfErr(err, "failed to encode certificate key")
err = ioutil.WriteFile(filename+"-key.pem", pem.EncodeToMemory( err = ioutil.WriteFile(keyFile, pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0600) &pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0600)
fatalIfErr(err, "failed to save certificate key") fatalIfErr(err, "failed to save certificate key")
err = ioutil.WriteFile(filename+".pem", pem.EncodeToMemory( err = ioutil.WriteFile(certFile, pem.EncodeToMemory(
&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644) &pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
fatalIfErr(err, "failed to save certificate key") fatalIfErr(err, "failed to save certificate key")
} else { } else {
domainCert, _ := x509.ParseCertificate(cert) domainCert, _ := x509.ParseCertificate(cert)
pfxData, err := pkcs12.Encode(rand.Reader, priv, domainCert, []*x509.Certificate{m.caCert}, "changeit") pfxData, err := pkcs12.Encode(rand.Reader, priv, domainCert, []*x509.Certificate{m.caCert}, "changeit")
fatalIfErr(err, "failed to generate PKCS#12") fatalIfErr(err, "failed to generate PKCS#12")
err = ioutil.WriteFile(filename+".p12", pfxData, 0644) err = ioutil.WriteFile(p12File, pfxData, 0644)
fatalIfErr(err, "failed to save PKCS#12") fatalIfErr(err, "failed to save PKCS#12")
} }
@@ -110,11 +106,42 @@ func (m *mkcert) makeCert(hosts []string) {
} }
} }
if !m.pkcs12 { for _, h := range hosts {
log.Printf("\nThe certificate is at \"./%s.pem\" and the key at \"./%s-key.pem\" ✅\n\n", filename, filename) if strings.HasPrefix(h, "*.") {
} else { log.Printf("\nReminder: X.509 wildcards only go one level deep, so this won't match a.b.%s ", h[2:])
log.Printf("\nThe PKCS#12 bundle is at \"./%s.p12\" ✅\n\n", filename) break
} }
}
if !m.pkcs12 {
log.Printf("\nThe certificate is at \"%s\" and the key at \"%s\" ✅\n\n", certFile, keyFile)
} else {
log.Printf("\nThe PKCS#12 bundle is at \"%s\" ✅\n", p12File)
log.Printf("\nThe legacy PKCS#12 encryption password is the often hardcoded default \"changeit\" \n\n")
}
}
func (m *mkcert) fileNames(hosts []string) (certFile, keyFile, p12File string) {
defaultName := strings.Replace(hosts[0], ":", "_", -1)
defaultName = strings.Replace(defaultName, "*", "_wildcard", -1)
if len(hosts) > 1 {
defaultName += "+" + strconv.Itoa(len(hosts)-1)
}
certFile = "./" + defaultName + ".pem"
if m.certFile != "" {
certFile = m.certFile
}
keyFile = "./" + defaultName + "-key.pem"
if m.keyFile != "" {
keyFile = m.keyFile
}
p12File = "./" + defaultName + ".p12"
if m.p12File != "" {
p12File = m.p12File
}
return
} }
// loadCA will load or create the CA at CAROOT. // loadCA will load or create the CA at CAROOT.
@@ -134,11 +161,11 @@ func (m *mkcert) loadCA() {
m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes) m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes)
fatalIfErr(err, "failed to parse the CA certificate") fatalIfErr(err, "failed to parse the CA certificate")
if _, err := os.Stat(filepath.Join(m.CAROOT, keyName)); os.IsNotExist(err) { if _, err := os.Stat(filepath.Join(m.CAROOT, rootKeyName)); os.IsNotExist(err) {
return // keyless mode, where only -install works return // keyless mode, where only -install works
} }
keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, keyName)) keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootKeyName))
fatalIfErr(err, "failed to read the CA key") fatalIfErr(err, "failed to read the CA key")
keyDERBlock, _ := pem.Decode(keyPEMBlock) keyDERBlock, _ := pem.Decode(keyPEMBlock)
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" { if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
@@ -197,7 +224,7 @@ func (m *mkcert) newCA() {
privDER, err := x509.MarshalPKCS8PrivateKey(priv) privDER, err := x509.MarshalPKCS8PrivateKey(priv)
fatalIfErr(err, "failed to encode CA key") fatalIfErr(err, "failed to encode CA key")
err = ioutil.WriteFile(filepath.Join(m.CAROOT, keyName), pem.EncodeToMemory( err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootKeyName), pem.EncodeToMemory(
&pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0400) &pem.Block{Type: "PRIVATE KEY", Bytes: privDER}), 0400)
fatalIfErr(err, "failed to save CA key") fatalIfErr(err, "failed to save CA key")

11
main.go
View File

@@ -31,7 +31,7 @@ const usage = `Usage of mkcert:
$ mkcert example.com myapp.dev localhost 127.0.0.1 ::1 $ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
Generate "example.com+4.pem" and "example.com+4-key.pem". Generate "example.com+4.pem" and "example.com+4-key.pem".
$ mkcert '*.example.com' $ mkcert "*.example.com"
Generate "_wildcard.example.com.pem" and "_wildcard.example.com-key.pem". Generate "_wildcard.example.com.pem" and "_wildcard.example.com-key.pem".
$ mkcert -pkcs12 example.com $ mkcert -pkcs12 example.com
@@ -40,6 +40,8 @@ const usage = `Usage of mkcert:
$ mkcert -uninstall $ mkcert -uninstall
Uninstall the local CA (but do not delete it). Uninstall the local CA (but do not delete it).
Use -cert-file, -key-file and -p12-file to customize the output paths.
Change the CA certificate and key storage location by setting $CAROOT, Change the CA certificate and key storage location by setting $CAROOT,
print it with "mkcert -CAROOT". print it with "mkcert -CAROOT".
` `
@@ -50,6 +52,9 @@ func main() {
var uninstallFlag = flag.Bool("uninstall", false, "uninstall the local root CA from the system trust store") var uninstallFlag = flag.Bool("uninstall", false, "uninstall the local root CA from the system trust store")
var pkcs12Flag = flag.Bool("pkcs12", false, "generate PKCS#12 instead of PEM") var pkcs12Flag = flag.Bool("pkcs12", false, "generate PKCS#12 instead of PEM")
var carootFlag = flag.Bool("CAROOT", false, "print the CAROOT path") var carootFlag = flag.Bool("CAROOT", false, "print the CAROOT path")
var certFileFlag = flag.String("cert-file", "", "output certificate file path")
var keyFileFlag = flag.String("key-file", "", "output key file path")
var p12FileFlag = flag.String("p12-file", "", "output PKCS#12 file path")
flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), usage) } flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), usage) }
flag.Parse() flag.Parse()
if *carootFlag { if *carootFlag {
@@ -64,15 +69,17 @@ func main() {
} }
(&mkcert{ (&mkcert{
installMode: *installFlag, uninstallMode: *uninstallFlag, pkcs12: *pkcs12Flag, installMode: *installFlag, uninstallMode: *uninstallFlag, pkcs12: *pkcs12Flag,
certFile: *certFileFlag, keyFile: *keyFileFlag, p12File: *p12FileFlag,
}).Run(flag.Args()) }).Run(flag.Args())
} }
const rootName = "rootCA.pem" const rootName = "rootCA.pem"
const keyName = "rootCA-key.pem" const rootKeyName = "rootCA-key.pem"
type mkcert struct { type mkcert struct {
installMode, uninstallMode bool installMode, uninstallMode bool
pkcs12 bool pkcs12 bool
keyFile, certFile, p12File string
CAROOT string CAROOT string
caCert *x509.Certificate caCert *x509.Certificate

View File

@@ -58,6 +58,10 @@ func init() {
} }
func (m *mkcert) checkJava() bool { func (m *mkcert) checkJava() bool {
if !hasKeytool {
return false
}
// exists returns true if the given x509.Certificate's fingerprint // exists returns true if the given x509.Certificate's fingerprint
// is in the keytool -list output // is in the keytool -list output
exists := func(c *x509.Certificate, h hash.Hash, keytoolOutput []byte) bool { exists := func(c *x509.Certificate, h hash.Hash, keytoolOutput []byte) bool {
@@ -108,7 +112,7 @@ func (m *mkcert) uninstallJava() {
// the command wrapped in 'sudo' to work around file permissions. // the command wrapped in 'sudo' to work around file permissions.
func (m *mkcert) execKeytool(cmd *exec.Cmd) ([]byte, error) { func (m *mkcert) execKeytool(cmd *exec.Cmd) ([]byte, error) {
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(out, []byte("java.io.FileNotFoundException")) { if err != nil && bytes.Contains(out, []byte("java.io.FileNotFoundException")) && runtime.GOOS != "windows" {
origArgs := cmd.Args[1:] origArgs := cmd.Args[1:]
cmd = exec.Command("sudo", keytoolPath) cmd = exec.Command("sudo", keytoolPath)
cmd.Args = append(cmd.Args, origArgs...) cmd.Args = append(cmd.Args, origArgs...)