mirror of
https://github.com/FiloSottile/mkcert.git
synced 2025-10-14 08:51:41 +08:00
Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
da4da8a4bc | ||
![]() |
5ea72c377d | ||
![]() |
0d0636e824 | ||
![]() |
6f4ab38307 | ||
![]() |
7fbb1febce | ||
![]() |
6060e206a4 | ||
![]() |
ea716f38b3 | ||
![]() |
438ae98b1c | ||
![]() |
afbc420fa0 | ||
![]() |
48e7d200ae | ||
![]() |
fcebdc9845 | ||
![]() |
5f8e78d012 | ||
![]() |
4f4405fc0c | ||
![]() |
f42b073e94 |
14
AUTHORS
14
AUTHORS
@@ -1,3 +1,11 @@
|
||||
# This source code refers to The Go Authors for copyright purposes.
|
||||
# The master list of authors is in the main Go distribution,
|
||||
# visible at https://tip.golang.org/AUTHORS.
|
||||
# This is the list of mkcert authors for copyright purposes.
|
||||
#
|
||||
# This does not necessarily list everyone who has contributed code, since in
|
||||
# some cases, their employer may be the copyright holder. To see the full list
|
||||
# of contributors, see the revision history in source control.
|
||||
|
||||
Google LLC
|
||||
Adam Shannon
|
||||
Chad Retz
|
||||
Travis Campbell
|
||||
Carl Henrik Lunde
|
||||
|
@@ -1,28 +0,0 @@
|
||||
# How to Contribute
|
||||
|
||||
We'd love to accept your patches and contributions to this project. There are
|
||||
just a few small guidelines you need to follow.
|
||||
|
||||
## Contributor License Agreement
|
||||
|
||||
Contributions to this project must be accompanied by a Contributor License
|
||||
Agreement. You (or your employer) retain the copyright to your contribution;
|
||||
this simply gives us permission to use and redistribute your contributions as
|
||||
part of the project. Head over to <https://cla.developers.google.com/> to see
|
||||
your current agreements on file or to sign a new one.
|
||||
|
||||
You generally only need to submit a CLA once, so if you've already submitted one
|
||||
(even if it was for a different project), you probably don't need to do it
|
||||
again.
|
||||
|
||||
## Code reviews
|
||||
|
||||
All submissions, including submissions by project members, require review. We
|
||||
use GitHub pull requests for this purpose. Consult
|
||||
[GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
|
||||
information on using pull requests.
|
||||
|
||||
## Community Guidelines
|
||||
|
||||
This project follows [Google's Open Source Community
|
||||
Guidelines](https://opensource.google.com/conduct/).
|
@@ -1,3 +0,0 @@
|
||||
# This source code was written by the Go contributors.
|
||||
# The master list of contributors is in the main Go distribution,
|
||||
# visible at https://tip.golang.org/CONTRIBUTORS.
|
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2018 The Go Authors. All rights reserved.
|
||||
Copyright (c) 2018 The mkcert Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
|
22
PATENTS
22
PATENTS
@@ -1,22 +0,0 @@
|
||||
Additional IP Rights Grant (Patents)
|
||||
|
||||
"This implementation" means the copyrightable works distributed by
|
||||
Google as part of the Go project.
|
||||
|
||||
Google hereby grants to You a perpetual, worldwide, non-exclusive,
|
||||
no-charge, royalty-free, irrevocable (except as stated in this section)
|
||||
patent license to make, have made, use, offer to sell, sell, import,
|
||||
transfer and otherwise run, modify and propagate the contents of this
|
||||
implementation of Go, where such license applies only to those patent
|
||||
claims, both currently owned or controlled by Google and acquired in
|
||||
the future, licensable by Google that are necessarily infringed by this
|
||||
implementation of Go. This grant does not include claims that would be
|
||||
infringed only as a consequence of further modification of this
|
||||
implementation. If you or your agent or exclusive licensee institute or
|
||||
order or agree to the institution of patent litigation against any
|
||||
entity (including a cross-claim or counterclaim in a lawsuit) alleging
|
||||
that this implementation of Go or any code incorporated within this
|
||||
implementation of Go constitutes direct or contributory patent
|
||||
infringement, or inducement of patent infringement, then any patent
|
||||
rights granted to you under this License for this implementation of Go
|
||||
shall terminate as of the date such litigation is filed.
|
37
README.md
37
README.md
@@ -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 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" ✨
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
@@ -41,11 +41,12 @@ brew install mkcert
|
||||
brew install nss # if you use Firefox
|
||||
```
|
||||
|
||||
or MacPorts.
|
||||
or [MacPorts](https://www.macports.org/).
|
||||
|
||||
```
|
||||
sudo port sync
|
||||
sudo port selfupdate
|
||||
sudo port install mkcert
|
||||
sudo port install nss # if you use Firefox
|
||||
```
|
||||
|
||||
### Linux
|
||||
@@ -75,10 +76,12 @@ $(go env GOPATH)/bin/mkcert
|
||||
|
||||
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/).
|
||||
|
||||
```
|
||||
yaourt -S mkcert
|
||||
```bash
|
||||
git clone https://aur.archlinux.org/mkcert.git
|
||||
cd mkcert
|
||||
makepkg -si
|
||||
```
|
||||
|
||||
### Windows
|
||||
@@ -89,8 +92,16 @@ On Windows, use Chocolatey
|
||||
choco install mkcert
|
||||
```
|
||||
|
||||
or use Scoop
|
||||
|
||||
```
|
||||
scoop install mkcert
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
mkcert supports the following root stores:
|
||||
@@ -115,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).
|
||||
|
||||
### 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
|
||||
|
||||
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`.
|
||||
@@ -131,7 +150,3 @@ Installing in the trust store does not require the CA key, so you can export the
|
||||
* run `mkcert -install`
|
||||
|
||||
Remember that mkcert is meant for development purposes, not production, so it should not be used on end users' machines, and that you should *not* export or share `rootCA-key.pem`.
|
||||
|
||||
---
|
||||
|
||||
This is not an official Google project, just some code that happens to be owned by Google.
|
||||
|
61
cert.go
61
cert.go
@@ -77,27 +77,23 @@ func (m *mkcert) makeCert(hosts []string) {
|
||||
cert, err := x509.CreateCertificate(rand.Reader, tpl, m.caCert, &pub, m.caKey)
|
||||
fatalIfErr(err, "failed to generate certificate")
|
||||
|
||||
filename := strings.Replace(hosts[0], ":", "_", -1)
|
||||
filename = strings.Replace(filename, "*", "_wildcard", -1)
|
||||
if len(hosts) > 1 {
|
||||
filename += "+" + strconv.Itoa(len(hosts)-1)
|
||||
}
|
||||
certFile, keyFile, p12File := m.fileNames(hosts)
|
||||
|
||||
if !m.pkcs12 {
|
||||
privDER, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
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)
|
||||
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)
|
||||
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(filename+".p12", pfxData, 0644)
|
||||
err = ioutil.WriteFile(p12File, pfxData, 0644)
|
||||
fatalIfErr(err, "failed to save PKCS#12")
|
||||
}
|
||||
|
||||
@@ -110,11 +106,42 @@ func (m *mkcert) makeCert(hosts []string) {
|
||||
}
|
||||
}
|
||||
|
||||
if !m.pkcs12 {
|
||||
log.Printf("\nThe certificate is at \"./%s.pem\" and the key at \"./%s-key.pem\" ✅\n\n", filename, filename)
|
||||
} else {
|
||||
log.Printf("\nThe PKCS#12 bundle is at \"./%s.p12\" ✅\n\n", filename)
|
||||
for _, h := range hosts {
|
||||
if strings.HasPrefix(h, "*.") {
|
||||
log.Printf("\nReminder: X.509 wildcards only go one level deep, so this won't match a.b.%s ℹ️", h[2:])
|
||||
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.
|
||||
@@ -134,11 +161,11 @@ func (m *mkcert) loadCA() {
|
||||
m.caCert, err = x509.ParseCertificate(certDERBlock.Bytes)
|
||||
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
|
||||
}
|
||||
|
||||
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")
|
||||
keyDERBlock, _ := pem.Decode(keyPEMBlock)
|
||||
if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
|
||||
@@ -188,8 +215,8 @@ func (m *mkcert) newCA() {
|
||||
KeyUsage: x509.KeyUsageCertSign,
|
||||
|
||||
BasicConstraintsValid: true,
|
||||
IsCA: true,
|
||||
MaxPathLenZero: true,
|
||||
IsCA: true,
|
||||
MaxPathLenZero: true,
|
||||
}
|
||||
|
||||
cert, err := x509.CreateCertificate(rand.Reader, tpl, tpl, &pub, priv)
|
||||
@@ -197,7 +224,7 @@ func (m *mkcert) newCA() {
|
||||
|
||||
privDER, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||
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)
|
||||
fatalIfErr(err, "failed to save CA key")
|
||||
|
||||
|
11
main.go
11
main.go
@@ -31,7 +31,7 @@ const usage = `Usage of mkcert:
|
||||
$ mkcert example.com myapp.dev localhost 127.0.0.1 ::1
|
||||
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".
|
||||
|
||||
$ mkcert -pkcs12 example.com
|
||||
@@ -40,6 +40,8 @@ const usage = `Usage of mkcert:
|
||||
$ mkcert -uninstall
|
||||
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,
|
||||
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 pkcs12Flag = flag.Bool("pkcs12", false, "generate PKCS#12 instead of PEM")
|
||||
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.Parse()
|
||||
if *carootFlag {
|
||||
@@ -64,15 +69,17 @@ func main() {
|
||||
}
|
||||
(&mkcert{
|
||||
installMode: *installFlag, uninstallMode: *uninstallFlag, pkcs12: *pkcs12Flag,
|
||||
certFile: *certFileFlag, keyFile: *keyFileFlag, p12File: *p12FileFlag,
|
||||
}).Run(flag.Args())
|
||||
}
|
||||
|
||||
const rootName = "rootCA.pem"
|
||||
const keyName = "rootCA-key.pem"
|
||||
const rootKeyName = "rootCA-key.pem"
|
||||
|
||||
type mkcert struct {
|
||||
installMode, uninstallMode bool
|
||||
pkcs12 bool
|
||||
keyFile, certFile, p12File string
|
||||
|
||||
CAROOT string
|
||||
caCert *x509.Certificate
|
||||
|
@@ -58,6 +58,10 @@ func init() {
|
||||
}
|
||||
|
||||
func (m *mkcert) checkJava() bool {
|
||||
if !hasKeytool {
|
||||
return false
|
||||
}
|
||||
|
||||
// exists returns true if the given x509.Certificate's fingerprint
|
||||
// is in the keytool -list output
|
||||
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.
|
||||
func (m *mkcert) execKeytool(cmd *exec.Cmd) ([]byte, error) {
|
||||
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:]
|
||||
cmd = exec.Command("sudo", keytoolPath)
|
||||
cmd.Args = append(cmd.Args, origArgs...)
|
||||
|
@@ -28,13 +28,17 @@ func init() {
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "darwin":
|
||||
out, err := exec.Command("brew", "--prefix", "nss").Output()
|
||||
var err error
|
||||
certutilPath, err = exec.LookPath("certutil")
|
||||
if err != nil {
|
||||
return
|
||||
var out []byte
|
||||
out, err = exec.Command("brew", "--prefix", "nss").Output()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
|
||||
_, err = os.Stat(certutilPath)
|
||||
}
|
||||
certutilPath = filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
|
||||
|
||||
_, err = os.Stat(certutilPath)
|
||||
hasCertutil = err == nil
|
||||
|
||||
case "linux":
|
||||
|
Reference in New Issue
Block a user