Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions certstore_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *winStore) Identities() ([]Identity, error) {
idents = append(idents, newWinIdentity(chain))
}

if err = lastError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
if err = checkError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
goto fail
}

Expand Down Expand Up @@ -184,7 +184,7 @@ func (s *winStore) Import(data []byte, password string) error {
for {
// iterate through certs in temporary store
if ctx = C.CertFindCertificateInStore(store, encoding, 0, C.CERT_FIND_ANY, nil, ctx); ctx == nil {
if err := lastError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
if err := checkError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
return err
}

Expand Down Expand Up @@ -612,8 +612,19 @@ func exportCertCtx(ctx C.PCCERT_CONTEXT) (*x509.Certificate, error) {

type errCode uint64

// lastError gets the last error from the current thread.
// lastError gets the last error from the current thread. If there isn't one, it
// returns a new error.
func lastError(msg string) error {
if err := checkError(msg); err != nil {
return err
}

return errors.New(msg)
}

// checkError tries to get the last error from the current thread. If there
// isn't one, it returns nil.
func checkError(msg string) error {
if code := errCode(C.GetLastError()); code != 0 {
return errors.Wrap(code, msg)
}
Expand Down