forked from getlantern/keyman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyman_trust_darwin.go
61 lines (53 loc) · 1.76 KB
/
keyman_trust_darwin.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
package keyman
import (
"fmt"
"os"
"os/exec"
)
const (
// TODO: Make sure to handle case where library is on a different path
OSX_SYSTEM_KEYCHAIN_PATH = "/Library/Keychains/System.keychain"
)
// DeleteTrustedRootByName deletes a certificate from the user's trust store as a trusted
// root CA by name.
func DeleteTrustedRootByName(commonName string) error {
cmd := exec.Command("security", "delete-certificate", "-c", commonName, OSX_SYSTEM_KEYCHAIN_PATH)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unable to run security command: %s\n%s", err, out)
} else {
return nil
}
return err
}
// AddAsTrustedRoot adds the certificate to the user's trust store as a trusted
// root CA.
func (cert *Certificate) AddAsTrustedRoot() error {
tempFileName, err := cert.WriteToTempFile()
defer func() {
if err := os.Remove(tempFileName); err != nil {
log.Debugf("Unable to remove file: %v", err)
}
}()
if err != nil {
return fmt.Errorf("Unable to create temp file: %s", err)
}
// Add it as a trusted cert
cmd := exec.Command("security", "add-trusted-cert", "-d", "-k", OSX_SYSTEM_KEYCHAIN_PATH, tempFileName)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unable to run security command: %s\n%s", err, out)
} else {
return nil
}
}
// Checks whether this certificate is install based purely on looking for a cert
// in the system keychain that has the same common name. This function returns
// true if there are one or more certs in the system keychain whose common name
// matches this cert.
func (cert *Certificate) IsInstalled() (bool, error) {
cmd := exec.Command("security", "find-certificate", "-c", cert.X509().Subject.CommonName, OSX_SYSTEM_KEYCHAIN_PATH)
err := cmd.Run()
found := err == nil
return found, nil
}