Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1-1 #16

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
88 changes: 39 additions & 49 deletions certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"log"
"os"
"strings"
"syscall"
Expand All @@ -22,30 +21,33 @@ func getCAFingerprint(caFileBinary string) (string, error) {
// open certificate of authority binary file
caFile, err := os.Open(caFileBinary)
if err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotOpenCAFile"), walk.MsgBoxOK)
log.Fatal("Failed opening CA file: ", err)
addNewLinesToDebug(T("cannotOpenCAFile") + err.Error())
viewErrorAndExit(T("cannotOpenCAFile"))
defer caFile.Close()
return "", err
}
// close file
defer caFile.Close()
} else {
// close file
defer caFile.Close()

// create new hash
hashSha1 := sha1.New()
// copy hash to the file
if _, err := io.Copy(hashSha1, caFile); err != nil {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotCopyCAFile"), walk.MsgBoxOK)
log.Fatal("Failed copying CA file: ", err)
return "", err
}
// returns sha1 checksum of the data
caFingerprintBytes := hashSha1.Sum(nil)
// convert sha1 to hex (base16) to string
caFingerprint = strings.ToLower(hex.EncodeToString(caFingerprintBytes))
// add spaces every two characters
for i := 2; i < len(caFingerprint); i += 3 {
caFingerprint = caFingerprint[:i] + " " + caFingerprint[i:]
// create new hash
hashSha1 := sha1.New()
// copy hash to the file
if _, err := io.Copy(hashSha1, caFile); err != nil {
addNewLinesToDebug(T("cannotCopyCAFile") + err.Error())
viewErrorAndExit(T("cannotCopyCAFile"))
return "", err
} else {
// returns sha1 checksum of the data
caFingerprintBytes := hashSha1.Sum(nil)
// convert sha1 to hex (base16) to string
caFingerprint = strings.ToLower(hex.EncodeToString(caFingerprintBytes))
// add spaces every two characters
for i := 2; i < len(caFingerprint); i += 3 {
caFingerprint = caFingerprint[:i] + " " + caFingerprint[i:]
}
return caFingerprint, nil
}
}
return caFingerprint, nil
}

// Add cert to windows
Expand Down Expand Up @@ -90,33 +92,27 @@ func addCertToMachine(userCertDecode string, CERTUTIL_PROGRAM_PATH string) error
if exitErr, ok := err.(*exec.ExitError); ok {
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok {
exitStatus := status.ExitStatus()
log.Print("Exit Status: ", exitStatus)
addNewLinesToDebug("Exit Status: " + string(exitStatus))
switch exitStatus {
case int(ERROR_INVALID_PASSWORD):
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("wrongPassword"), walk.MsgBoxOK)
addNewLinesToDebug(T("wrongPassword"))
viewErrorAndExit(T("wrongPassword"))
badCertificatePassword = true
mw.Close()
case int(ERROR_INVALID_DATA):
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("invalidCertificate"), walk.MsgBoxOK)
os.Remove(userCertDecode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this massive copy/paste needs to be removed but I don't see it in the defer so that it still happens when this is done

This would mean the files are left there after it exits unless I'm missing where this has been added

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files are now removed on exit.
In utils.go, func cleanTmpFiles

os.Remove("profile.xml")
log.Fatal("Invalid certificate: ", exitStatus)
addNewLinesToDebug(T("invalidCertificate"))
JeGoi marked this conversation as resolved.
Show resolved Hide resolved
viewErrorAndExit(T("invalidCertificate"))
case int(ERROR_FILE_NOT_FOUND):
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotFindCertificateFile"), walk.MsgBoxOK)
os.Remove(userCertDecode)
os.Remove("profile.xml")
log.Fatal("Certificate not found: ", exitStatus)
addNewLinesToDebug(T("cannotFindCertificateFile") + string(exitStatus))
viewErrorAndExit(T("cannotFindCertificateFile"))
default:
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotInstallCertificate"), walk.MsgBoxOK)
os.Remove(userCertDecode)
os.Remove("profile.xml")
log.Fatal("Cannot install certificate: ", exitStatus)
addNewLinesToDebug(T("cannotInstallCertificate") + string(exitStatus))
viewErrorAndExit(T("cannotInstallCertificate"))
}
}
}
} else {
log.Println(T("successWindowTitle"), T("certificateInstallationSuccess"))
os.Remove(userCertDecode)
addNewLinesToDebug(T("certificateInstallationSuccess"))
}
mw.Close()
},
Expand Down Expand Up @@ -147,25 +143,19 @@ func addCAToMachine(caFileBinary string, CERTUTIL_PROGRAM_PATH string) error {
// reprompt user to add certificate to windows
retryOrCancel := walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("caErrorCanceled"), walk.MsgBoxRetryCancel)
if retryOrCancel == 4 {
log.Print("Failed installing certificate: ", err)
os.Remove(caFileBinary)
os.Remove("profile.xml")
addNewLinesToDebug("Failed installing certificate: " + err.Error())
runCommand = true
} else {
log.Fatal("Failed installing certificate: ", err)
os.Remove(caFileBinary)
os.Remove("profile.xml")
addNewLinesToDebug("Failed installing certificate: " + err.Error())
}
} else {
walk.MsgBox(windowMsgBox, T("errorWindowTitle"), T("cannotInstallCA"), walk.MsgBoxOK)
os.Remove(caFileBinary)
os.Remove("profile.xml")
log.Fatal("Failed installing certificate: ", err)
addNewLinesToDebug(T("cannotInstallCA") + err.Error())
viewErrorAndExit(T("cannotInstallCA"))
}
}
}
} else {
log.Println(T("successWindowTitle"), T("caInstallationSuccess"))
addNewLinesToDebug(T("caInstallationSuccess") + err.Error())
}
}
return err
Expand Down
Loading