-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtruststore_nss.go
160 lines (149 loc) · 4.13 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cert
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
)
var nssDBs = []string{
filepath.Join(os.Getenv("HOME"), ".pki/nssdb"),
filepath.Join(os.Getenv("HOME"), "snap/chromium/current/.pki/nssdb"), // Snapcraft
"/etc/pki/nssdb", // CentOS 7
}
var firefoxPaths = []string{
"/usr/bin/firefox",
"/usr/bin/firefox-nightly",
"/usr/bin/firefox-developer-edition",
"/snap/firefox",
"/Applications/Firefox.app",
"/Applications/FirefoxDeveloperEdition.app",
"/Applications/Firefox Developer Edition.app",
"/Applications/Firefox Nightly.app",
"C:\\Program Files\\Mozilla Firefox",
}
func hasNSS() bool {
allPaths := append(append([]string{}, nssDBs...), firefoxPaths...)
for _, path := range allPaths {
if pathExists(path) {
return true
}
}
return false
}
func certutilPath() string {
// certutil does not exist on Windows
if runtime.GOOS == "windows" {
return ""
}
if path, err := exec.LookPath("certutil"); err == nil {
return path
}
if runtime.GOOS == "darwin" {
// Check the default Homebrew path, to save executing Ruby
if path, err := exec.LookPath("/usr/local/opt/nss/bin/certutil"); err == nil {
return path
}
if out, err := exec.Command("brew", "--prefix", "nss").Output(); err == nil {
path := filepath.Join(strings.TrimSpace(string(out)), "bin", "certutil")
if _, err = os.Stat(path); err == nil {
return path
}
}
}
return ""
}
func (ca *CA) checkNSS() bool {
certutilPath := certutilPath()
if certutilPath == "" {
return false
}
success := true
found, err := ca.forEachNSSProfile(func(profile string) error {
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", ca.caUniqueName()).Run()
if err != nil {
success = false
}
return nil
})
if err != nil || found == 0 {
success = false
}
return success
}
func (ca *CA) installNSS() error {
certutilPath := certutilPath()
found, err := ca.forEachNSSProfile(func(profile string) error {
cmd := exec.Command(certutilPath, "-A", "-d", profile, "-t", "C,,", "-n", ca.caUniqueName(), "-i", ca.rootpath)
if out, err := execCertutil(cmd); err != nil {
return wrapCmdErr(err, "certutil -A -d"+profile, out)
}
return nil
})
if err != nil {
return err
}
if found == 0 {
return errors.Errorf("no %s security databases found", NSSBrowsers)
}
if !ca.checkNSS() {
return errors.Errorf("Installing in %s failed (note that if you never started %s, you need to do that at least once)", NSSBrowsers, NSSBrowsers)
}
return nil
}
func (ca *CA) uninstallNSS() {
certutilPath := certutilPath()
ca.forEachNSSProfile(func(profile string) error {
err := exec.Command(certutilPath, "-V", "-d", profile, "-u", "L", "-n", ca.caUniqueName()).Run()
if err != nil {
return nil
}
cmd := exec.Command(certutilPath, "-D", "-d", profile, "-n", ca.caUniqueName())
if out, err := execCertutil(cmd); err != nil {
return wrapCmdErr(err, "certutil -D -d"+profile, out)
}
return nil
})
}
func (ca *CA) forEachNSSProfile(f func(profile string) error) (int, error) {
found := 0
var profiles []string
profiles = append(profiles, nssDBs...)
for _, ff := range FirefoxProfiles {
pp, _ := filepath.Glob(ff)
profiles = append(profiles, pp...)
}
for _, profile := range profiles {
if stat, err := os.Stat(profile); err != nil || !stat.IsDir() {
continue
}
if _, err := os.Stat(filepath.Join(profile, "cert9.db")); err == nil {
if err := f("sql:" + profile); err != nil {
return 0, err
}
found++
continue
}
if _, err := os.Stat(filepath.Join(profile, "cert8.db")); err == nil {
if err := f("dbm:" + profile); err != nil {
return 0, err
}
found++
}
}
return found, nil
}
// execCertutil will execute a "certutil" command and if needed re-execute
// the command with commandWithSudo to work around file permissions.
func execCertutil(cmd *exec.Cmd) ([]byte, error) {
out, err := cmd.CombinedOutput()
if err != nil && bytes.Contains(out, []byte("SEC_ERROR_READ_ONLY")) && runtime.GOOS != "windows" {
origArgs := cmd.Args[1:]
cmd = commandWithSudo(cmd.Path)
cmd.Args = append(cmd.Args, origArgs...)
out, err = cmd.CombinedOutput()
}
return out, err
}