forked from FiloSottile/mkcert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
truststore_nss.go
113 lines (102 loc) · 3.03 KB
/
truststore_nss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
var (
hasNSS bool
hasCertutil bool
certutilPath string
nssDB = filepath.Join(os.Getenv("HOME"), ".pki/nssdb")
)
func init() {
_, err := os.Stat(FirefoxPath)
hasNSS = !os.IsNotExist(err)
switch runtime.GOOS {
case "darwin":
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)
hasCertutil = !os.IsNotExist(err)
case "linux":
_, err := os.Stat(nssDB)
hasNSS = hasNSS && !os.IsNotExist(err)
certutilPath, err = exec.LookPath("certutil")
hasCertutil = err == nil
}
}
func (m *mkcert) checkNSS() bool {
if !hasCertutil {
return false
}
success := true
if m.forEachNSSProfile(func(profile string) {
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
if err != nil {
success = false
}
}) == 0 {
success = false
}
return success
}
func (m *mkcert) installNSS() {
if m.forEachNSSProfile(func(profile string) {
cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", m.caUniqueName(), "-i", filepath.Join(m.CAROOT, rootName))
out, err := cmd.CombinedOutput()
if err != nil {
log.Printf("!!! You've hit a known issue. Please report the entire command output at https://github.com/FiloSottile/mkcert/issues/12\nProfile path: %s\nOS: %s/%s\ncertutil: %s\n", profile, runtime.GOOS, runtime.GOARCH, certutilPath)
cmd := exec.Command("ls", "-l", profile[4:])
cmd.Stdout, cmd.Stderr = os.Stderr, os.Stderr
cmd.Run()
}
fatalIfCmdErr(err, "certutil -A", out)
}) == 0 {
log.Printf("ERROR: no %s security databases found", NSSBrowsers)
}
if !m.checkNSS() {
log.Printf("Installing in %s failed. Please report the issue with details about your environment at https://github.com/FiloSottile/mkcert/issues/new 👎", NSSBrowsers)
log.Printf("Note that if you never started %s, you need to do that at least once.", NSSBrowsers)
}
}
func (m *mkcert) uninstallNSS() {
m.forEachNSSProfile(func(profile string) {
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", m.caUniqueName()).Run()
if err != nil {
return
}
cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", m.caUniqueName())
out, err := cmd.CombinedOutput()
fatalIfCmdErr(err, "certutil -D", out)
})
}
func (m *mkcert) forEachNSSProfile(f func(profile string)) (found int) {
profiles, _ := filepath.Glob(FirefoxProfile)
if _, err := os.Stat(nssDB); !os.IsNotExist(err) {
profiles = append(profiles, nssDB)
}
if len(profiles) == 0 {
return
}
for _, profile := range profiles {
if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
continue
}
if _, err := os.Stat(filepath.Join(profile, "cert8.db")); !os.IsNotExist(err) {
f("dbm:" + profile)
found++
}
if _, err := os.Stat(filepath.Join(profile, "cert9.db")); !os.IsNotExist(err) {
f("sql:" + profile)
found++
}
}
return
}