Skip to content

Commit

Permalink
Using new ByteExec api
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Nov 6, 2014
1 parent 2d90820 commit 7da54bf
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions keyman_trust_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ 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.exe")
if err != nil {
panic(fmt.Errorf("Unable to construct executable from memory: %s", err))
}
}

// AddAsTrustedRoot adds the certificate to the user's trust store as a trusted
// root CA.
func (cert *Certificate) AddAsTrustedRoot() error {
Expand All @@ -28,13 +43,7 @@ func (cert *Certificate) AddAsTrustedRoot() error {
}

// Add it as a trusted cert
be, err := certImporter()
if err != nil {
return err
}
defer be.Close()

cmd := be.Command("add", ROOT_CERT_STORE_NAME, tempFile.Name())
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)
Expand All @@ -44,33 +53,15 @@ func (cert *Certificate) AddAsTrustedRoot() error {
}

func (cert *Certificate) IsInstalled() (bool, error) {
be, err := certImporter()
if err != nil {
return false, err
}
defer be.Close()

// 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 := be.Command("find", ROOT_CERT_STORE_NAME, cert.X509().Subject.CommonName)
err = cmd.Run()
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
}

func certImporter() (be *byteexec.ByteExec, err error) {
exe, err := certimporter.Asset("certimporter.exe")
if err != nil {
return nil, fmt.Errorf("Unable to get certimporter.exe: %s", err)
}
be, err = byteexec.NewByteExec(exe)
if err != nil {
return nil, fmt.Errorf("Unable to construct executable from memory: %s", err)
}
return be, nil
}

0 comments on commit 7da54bf

Please sign in to comment.