Bladeren bron

Add keyless install only mode

Filippo Valsorda 6 jaren geleden
bovenliggende
commit
7433af8d0b
2 gewijzigde bestanden met toevoegingen van 19 en 42 verwijderingen
  1. 8 4
      README.md
  2. 11 38
      main.go

+ 8 - 4
README.md

@@ -43,13 +43,17 @@ $ cd mkcert && make
 
 Windows will be supported soon.
 
-## Changing the location of the CA files
+## Advanced topics
 
-TODO
+### Changing the location of the CA files
 
-## Installing the CA on other computers
+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 if you need it it's printed in the first line of the mkcert output.
 
-TODO
+If you want to manage separate CAs, you can use the environment variable `CAROOT` to set the folder where mkcert will place and look for the local CA files.
+
+### Installing the CA on other computers
+
+Installing in the trust store does not require the CA key, so you can export just the `rootCA.pem` file, point `CAROOT` to its directory on a different machine and run `-install`. Or, for example, commit just `rootCA.pem` and not its key to version control.
 
 Remember that mkcert is meant for development purposes, not production, so it should not be used on users' machines.
 

+ 11 - 38
main.go

@@ -116,6 +116,10 @@ Change the CA certificate and key storage location by setting $CAROOT.
 }
 
 func (m *mkcert) makeCert(hosts []string) {
+	if m.caKey == nil {
+		log.Fatalln("ERROR: can't create new certificates because the CA key (rootCA-key.pem) is missing")
+	}
+
 	priv, err := rsa.GenerateKey(rand.Reader, 2048)
 	fatalIfErr(err, "failed to generate certificate key")
 
@@ -180,9 +184,6 @@ func (m *mkcert) loadCA() {
 
 	certPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, rootName))
 	fatalIfErr(err, "failed to read the CA certificate")
-	keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, keyName))
-	fatalIfErr(err, "failed to read the CA key")
-
 	certDERBlock, _ := pem.Decode(certPEMBlock)
 	if certDERBlock == nil || certDERBlock.Type != "CERTIFICATE" {
 		log.Fatalln("ERROR: failed to read the CA certificate: unexpected content")
@@ -190,6 +191,12 @@ 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) {
+		return // keyless mode, where only -install works
+	}
+
+	keyPEMBlock, err := ioutil.ReadFile(filepath.Join(m.CAROOT, keyName))
+	fatalIfErr(err, "failed to read the CA key")
 	keyDERBlock, _ := pem.Decode(keyPEMBlock)
 	if keyDERBlock == nil || keyDERBlock.Type != "PRIVATE KEY" {
 		log.Fatalln("ERROR: failed to read the CA key: unexpected content")
@@ -230,7 +237,7 @@ func (m *mkcert) newCA() {
 	fatalIfErr(err, "failed to save CA key")
 
 	err = ioutil.WriteFile(filepath.Join(m.CAROOT, rootName), pem.EncodeToMemory(
-		&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0400)
+		&pem.Block{Type: "CERTIFICATE", Bytes: cert}), 0644)
 	fatalIfErr(err, "failed to save CA key")
 
 	log.Printf("Created a new local CA at \"%s\" 💥\n", m.CAROOT)
@@ -272,16 +279,6 @@ func (m *mkcert) install() {
 	m.installPlatform()
 	m.ignoreCheckFailure = true
 
-	/*
-		switch runtime.GOOS {
-		case "darwin":
-			m.installDarwin()
-		default:
-			log.Println("Installing is not available on your platform 👎")
-			log.Fatalf("If you know how, you can install the certificate at \"%s\" in your system trust store", filepath.Join(m.CAROOT, rootName))
-		}
-	*/
-
 	if m.check() { // useless, see comment on ignoreCheckFailure
 		log.Print("The local CA is now installed in the system trust store! ⚡️\n\n")
 	} else {
@@ -299,30 +296,6 @@ func (m *mkcert) check() bool {
 		return true
 	}
 
-	/*
-		priv, err := rsa.GenerateKey(rand.Reader, 2048)
-		fatalIfErr(err, "failed to generate the test key")
-
-		tpl := &x509.Certificate{
-			SerialNumber: big.NewInt(42),
-			DNSNames:     []string{"test.mkcert.invalid"},
-
-			NotAfter:  time.Now().AddDate(0, 0, 1),
-			NotBefore: time.Now().AddDate(0, 0, -1),
-
-			KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
-			ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
-			BasicConstraintsValid: true,
-		}
-
-		pub := priv.PublicKey
-		cert, err := x509.CreateCertificate(rand.Reader, tpl, m.caCert, &pub, m.caKey)
-		fatalIfErr(err, "failed to generate test certificate")
-
-		c, err := x509.ParseCertificate(cert)
-		fatalIfErr(err, "failed to parse test certificate")
-	*/
-
 	_, err := m.caCert.Verify(x509.VerifyOptions{})
 	return err == nil
 }