Skip to content

Commit 099e152

Browse files
committed
Reorganize security status and error code types in windows_cert_store_signer.go
1 parent 59cfe8e commit 099e152

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

aws_signing_helper/windows_cert_store_signer.go

+40-34
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,50 @@ var (
9898
ErrRequiresUI = errors.New("provider requries UI to operate")
9999
)
100100

101-
// Error codes for Windows APIs - implements the error interface
101+
// Error codes for Windows APIs - implements the error interface.
102+
// Error codes are maintained on a per-thread basis. In order to
103+
// get the last error code, C.GetLastError needs to be called (called
104+
// within the checkError() function). Some Windows APIs only return
105+
// BOOLs indicating success or failure, and for more detailed error
106+
// information, error codes are used.
102107
type errCode uint64
103108

104-
// Security status for Windows APIs - implements the error interface
105-
// Go representation of the C SECURITY_STATUS
109+
// Implements the error interface for errCode and returns a string
110+
// version of the errCode
111+
func (c errCode) Error() string {
112+
var cMsg C.LPSTR
113+
ret := C.FormatMessage(
114+
C.FORMAT_MESSAGE_ALLOCATE_BUFFER|
115+
C.FORMAT_MESSAGE_FROM_SYSTEM|
116+
C.FORMAT_MESSAGE_IGNORE_INSERTS,
117+
nil,
118+
C.DWORD(c),
119+
C.ulong(C.MAKE_LANG_ID(C.LANG_NEUTRAL, C.SUBLANG_DEFAULT)),
120+
cMsg,
121+
0, nil)
122+
if ret == 0 {
123+
return fmt.Sprintf("Error %X", int(c))
124+
}
125+
126+
if cMsg == nil {
127+
return fmt.Sprintf("Error %X", int(c))
128+
}
129+
130+
goMsg := C.GoString(cMsg)
131+
132+
return fmt.Sprintf("Error: %X %s", int(c), goMsg)
133+
}
134+
135+
// Security status for Windows APIs - implements the error interface.
136+
// Some Windows API calls return this type directly.
137+
// Go representation of the C SECURITY_STATUS.
106138
type securityStatus uint64
107139

140+
// Implements the error interface
141+
func (secStatus securityStatus) Error() string {
142+
return fmt.Sprintf("SECURITY_STATUS %d", int(secStatus))
143+
}
144+
108145
// Gets the certificates that match the given CertIdentifier within the user's "MY" certificate store.
109146
// If there is only a single matching certificate, then its chain will be returned too
110147
func GetMatchingCertsAndChain(certIdentifier CertIdentifier) (store windows.Handle, certCtx *windows.CertContext, certChain []*x509.Certificate, certContainers []CertificateContainer, err error) {
@@ -596,32 +633,6 @@ func checkError(msg string) error {
596633
return nil
597634
}
598635

599-
// Implements the error interface for errCode and returns a string
600-
// version of the errCode
601-
func (c errCode) Error() string {
602-
var cMsg C.LPSTR
603-
ret := C.FormatMessage(
604-
C.FORMAT_MESSAGE_ALLOCATE_BUFFER|
605-
C.FORMAT_MESSAGE_FROM_SYSTEM|
606-
C.FORMAT_MESSAGE_IGNORE_INSERTS,
607-
nil,
608-
C.DWORD(c),
609-
C.ulong(C.MAKE_LANG_ID(C.LANG_NEUTRAL, C.SUBLANG_DEFAULT)),
610-
cMsg,
611-
0, nil)
612-
if ret == 0 {
613-
return fmt.Sprintf("Error %X", int(c))
614-
}
615-
616-
if cMsg == nil {
617-
return fmt.Sprintf("Error %X", int(c))
618-
}
619-
620-
goMsg := C.GoString(cMsg)
621-
622-
return fmt.Sprintf("Error: %X %s", int(c), goMsg)
623-
}
624-
625636
// Converts a SECURITY_STATUS into a securityStatus
626637
func checkStatus(s C.SECURITY_STATUS) error {
627638
secStatus := securityStatus(s)
@@ -640,8 +651,3 @@ func checkStatus(s C.SECURITY_STATUS) error {
640651

641652
return secStatus
642653
}
643-
644-
// Implements the error interface
645-
func (secStatus securityStatus) Error() string {
646-
return fmt.Sprintf("SECURITY_STATUS %d", int(secStatus))
647-
}

0 commit comments

Comments
 (0)