forked from getlantern/keyman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyman_trust_windows.go
77 lines (67 loc) · 2.06 KB
/
keyman_trust_windows.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
package keyman
import (
"fmt"
"io/ioutil"
"os"
"github.com/getlantern/byteexec"
"github.com/getlantern/keyman/certimporter"
)
const (
ROOT_CERT_STORE_NAME = "ROOT"
)
var (
cebe *byteexec.Exec
)
func init() {
exe, err := certimporter.Asset("certimporter.exe")
if err != nil {
panic(fmt.Errorf("Unable to get certimporter.exe: %s", err))
}
cebe, err = byteexec.New(exe, "certimporter")
if err != nil {
panic(fmt.Errorf("Unable to construct executable from memory: %s", err))
}
}
func DeleteTrustedRootByName(commonName string) error {
cmd := cebe.Command("delete", ROOT_CERT_STORE_NAME, commonName)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unable to run certimporter.exe: %s\n%s", err, out)
} else {
return nil
}
}
// AddAsTrustedRoot adds the certificate to the user's trust store as a trusted
// root CA.
func (cert *Certificate) AddAsTrustedRoot() error {
// Create a temp file containing the certificate
tempFile, err := ioutil.TempFile("", "tempCert")
defer os.Remove(tempFile.Name())
if err != nil {
return fmt.Errorf("Unable to create temp file: %s", err)
}
err = cert.WriteToDERFile(tempFile.Name())
if err != nil {
return fmt.Errorf("Unable to save certificate to temp file: %s", err)
}
// Add it as a trusted cert
cmd := cebe.Command("add", ROOT_CERT_STORE_NAME, tempFile.Name())
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Unable to run certimporter.exe: %s\n%s", err, out)
} else {
return nil
}
}
func (cert *Certificate) IsInstalled() (bool, error) {
// TODO: make sure that passing byte strings of various encodings to the
// certimporter program works in different languages/different usernames (
// which end up in the temp path, etc.)
cmd := cebe.Command("find", ROOT_CERT_STORE_NAME, cert.X509().Subject.CommonName)
err := cmd.Run()
// Consider the certificate found if and only if certimporter.exe exited
// with a 0 exit code. Any non-zero code (cert not found, or error looking
// for cert) is treated as the cert not being found.
found := err == nil
return found, nil
}