Skip to content

Commit f487964

Browse files
qmuntaldagood
andauthored
Implement newOpenSSLError (#28)
* implement newOpenSSLError * Apply suggestions from code review Co-authored-by: Davis Goodin <[email protected]> * use newOpenSSLError in sha operations Co-authored-by: Davis Goodin <[email protected]>
1 parent 39aa43a commit f487964

File tree

4 files changed

+41
-9
lines changed

4 files changed

+41
-9
lines changed

openssl/openssl.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import "C"
1010
import (
1111
"errors"
1212
"strconv"
13+
"strings"
1314
"sync"
1415
"unsafe"
1516
)
@@ -114,7 +115,7 @@ func SetFIPS(enabled bool) error {
114115
mode = C.int(0)
115116
}
116117
if C.go_openssl_FIPS_mode_set(mode) != 1 {
117-
return fail("FIPS_mode_set")
118+
return newOpenSSLError("FIPS_mode_set")
118119
}
119120
return nil
120121
case 3:
@@ -130,15 +131,15 @@ func SetFIPS(enabled bool) error {
130131
if !providerAvailable(props) {
131132
// If not, fallback to provName provider.
132133
if C.go_openssl_OSSL_PROVIDER_load(nil, provName) == nil {
133-
return fail("OSSL_PROVIDER_try_load")
134+
return newOpenSSLError("OSSL_PROVIDER_try_load")
134135
}
135136
// Make sure we now have a provider available.
136137
if !providerAvailable(props) {
137138
return fail("SetFIPS(" + strconv.FormatBool(enabled) + ") not supported")
138139
}
139140
}
140141
if C.go_openssl_EVP_set_default_properties(nil, props) != 1 {
141-
return fail("EVP_set_default_properties")
142+
return newOpenSSLError("EVP_set_default_properties")
142143
}
143144
return nil
144145
default:
@@ -180,3 +181,32 @@ func base(b []byte) *C.uchar {
180181
}
181182
return (*C.uchar)(unsafe.Pointer(&b[0]))
182183
}
184+
185+
func newOpenSSLError(msg string) error {
186+
var b strings.Builder
187+
b.WriteString(msg)
188+
b.WriteString("\nopenssl error(s):")
189+
for {
190+
var (
191+
e C.ulong
192+
file *C.char
193+
line C.int
194+
)
195+
switch vMajor {
196+
case 1:
197+
e = C.go_openssl_ERR_get_error_line(&file, &line)
198+
case 3:
199+
e = C.go_openssl_ERR_get_error_all(&file, &line, nil, nil, nil)
200+
default:
201+
panic(errUnsupportedVersion())
202+
}
203+
if e == 0 {
204+
break
205+
}
206+
b.WriteByte('\n')
207+
var buf [256]byte
208+
C.go_openssl_ERR_error_string_n(e, (*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
209+
b.WriteString(string(buf[:]) + "\n\t" + C.GoString(file) + ":" + strconv.Itoa(int(line)))
210+
}
211+
return errors.New(b.String())
212+
}

openssl/rand.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ func (randReader) Read(b []byte) (int, error) {
1313
// Note: RAND_bytes should never fail; the return value exists only for historical reasons.
1414
// We check it even so.
1515
if len(b) > 0 && C.go_openssl_RAND_bytes((*C.uchar)(unsafe.Pointer(&b[0])), C.int(len(b))) == 0 {
16-
// TODO: use NewOpenSSLError once implemented.
17-
return 0, fail("RAND_bytes")
16+
return 0, newOpenSSLError("RAND_bytes")
1817
}
1918
return len(b), nil
2019
}

openssl/sha.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ func (h *evpHash) Reset() {
104104
// There is no need to reset h.ctx2 because it is always reset after
105105
// use in evpHash.sum.
106106
if C.go_openssl_EVP_DigestInit(h.ctx, h.md) != 1 {
107-
panic("openssl: EVP_DigestInit failed")
107+
panic(newOpenSSLError("EVP_DigestInit"))
108108
}
109109
runtime.KeepAlive(h)
110110
}
111111

112112
func (h *evpHash) Write(p []byte) (int, error) {
113113
if len(p) > 0 && C.go_openssl_EVP_DigestUpdate(h.ctx, unsafe.Pointer(&*addr(p)), C.size_t(len(p))) != 1 {
114-
panic("openssl: EVP_DigestUpdate failed")
114+
panic(newOpenSSLError("EVP_DigestUpdate"))
115115
}
116116
runtime.KeepAlive(h)
117117
return len(p), nil
@@ -131,10 +131,10 @@ func (h *evpHash) sum(out []byte) {
131131
// In particular it is OK to Sum, then Write more, then Sum again,
132132
// and the second Sum acts as if the first didn't happen.
133133
if C.go_openssl_EVP_MD_CTX_copy(h.ctx2, h.ctx) != 1 {
134-
panic("openssl: EVP_MD_CTX_copy failed")
134+
panic(newOpenSSLError("EVP_MD_CTX_copy"))
135135
}
136136
if C.go_openssl_EVP_DigestFinal(h.ctx2, (*C.uchar)(noescape(unsafe.Pointer(base(out)))), nil) != 1 {
137-
panic("openssl: EVP_DigestFinal failed")
137+
panic(newOpenSSLError("EVP_DigestFinal"))
138138
}
139139
runtime.KeepAlive(h)
140140
}

openssl/shims.h

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ typedef void* GO_HMAC_CTX_PTR;
7070
#define FOR_ALL_OPENSSL_FUNCTIONS \
7171
DEFINEFUNC(int, ERR_set_mark, (void), ()) \
7272
DEFINEFUNC(int, ERR_pop_to_mark, (void), ()) \
73+
DEFINEFUNC(void, ERR_error_string_n, (unsigned long e, char *buf, size_t len), (e, buf, len)) \
74+
DEFINEFUNC_LEGACY_1(unsigned long, ERR_get_error_line, (const char **file, int *line), (file, line)) \
75+
DEFINEFUNC_3_0(unsigned long, ERR_get_error_all, (const char **file, int *line, const char **func, const char **data, int *flags), (file, line, func, data, flags)) \
7376
DEFINEFUNC_RENAMED_1_1(const char *, OpenSSL_version, SSLeay_version, (int type), (type)) \
7477
DEFINEFUNC(void, OPENSSL_init, (void), ()) \
7578
DEFINEFUNC_LEGACY_1_0(void, ERR_load_crypto_strings, (void), ()) \

0 commit comments

Comments
 (0)