Skip to content

Commit 5228eee

Browse files
committed
error instead of panicing when marshaling hash objects
1 parent 0a2f211 commit 5228eee

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

hash.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"hash"
1111
"runtime"
1212
"strconv"
13+
"sync"
1314
"unsafe"
1415
)
1516

@@ -110,6 +111,37 @@ func SHA3_512(p []byte) (sum [64]byte) {
110111
return
111112
}
112113

114+
var isMarshallableCache sync.Map
115+
116+
// isHashMarshallable returns true if the memory layout of cb
117+
// is known by this library and can therefore be marshalled.
118+
func isHashMarshallable(ch crypto.Hash) bool {
119+
if vMajor == 1 {
120+
return true
121+
}
122+
if v, ok := isMarshallableCache.Load(ch); ok {
123+
return v.(bool)
124+
}
125+
md := cryptoHashToMD(ch)
126+
if md == nil {
127+
return false
128+
}
129+
prov := C.go_openssl_EVP_MD_get0_provider(md)
130+
if prov == nil {
131+
return false
132+
}
133+
cname := C.go_openssl_OSSL_PROVIDER_get0_name(prov)
134+
if cname == nil {
135+
return false
136+
}
137+
name := C.GoString(cname)
138+
// We only know the memory layout of the built-in providers.
139+
// See evpHash.hashState for more details.
140+
marshallable := name == "default" || name == "fips"
141+
isMarshallableCache.Store(ch, marshallable)
142+
return marshallable
143+
}
144+
113145
// evpHash implements generic hash methods.
114146
type evpHash struct {
115147
ctx C.GO_EVP_MD_CTX_PTR
@@ -119,6 +151,8 @@ type evpHash struct {
119151
ctx2 C.GO_EVP_MD_CTX_PTR
120152
size int
121153
blockSize int
154+
155+
marshallable bool
122156
}
123157

124158
func newEvpHash(ch crypto.Hash, size, blockSize int) *evpHash {
@@ -137,6 +171,8 @@ func newEvpHash(ch crypto.Hash, size, blockSize int) *evpHash {
137171
ctx2: ctx2,
138172
size: size,
139173
blockSize: blockSize,
174+
175+
marshallable: isHashMarshallable(ch),
140176
}
141177
runtime.SetFinalizer(h, (*evpHash).finalize)
142178
return h
@@ -200,6 +236,9 @@ func (h *evpHash) sum(out []byte) {
200236
// The EVP_MD_CTX memory layout has changed in OpenSSL 3
201237
// and the property holding the internal structure is no longer md_data but algctx.
202238
func (h *evpHash) hashState() unsafe.Pointer {
239+
if !h.marshallable {
240+
return nil
241+
}
203242
switch vMajor {
204243
case 1:
205244
// https://github.com/openssl/openssl/blob/0418e993c717a6863f206feaa40673a261de7395/crypto/evp/evp_local.h#L12.

openssl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestMain(m *testing.M) {
5454
// or that there is a bug in the Init code.
5555
panic(err)
5656
}
57-
_ = openssl.SetFIPS(true) // Skip the error as we still want to run the tests on machines without FIPS support.
57+
//_ = openssl.SetFIPS(true) // Skip the error as we still want to run the tests on machines without FIPS support.
5858
fmt.Println("OpenSSL version:", openssl.VersionText())
5959
fmt.Println("FIPS enabled:", openssl.FIPS())
6060
status := m.Run()

shims.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ DEFINEFUNC_3_0(int, EVP_default_properties_is_fips_enabled, (GO_OSSL_LIB_CTX_PTR
190190
DEFINEFUNC_3_0(int, EVP_default_properties_enable_fips, (GO_OSSL_LIB_CTX_PTR libctx, int enable), (libctx, enable)) \
191191
DEFINEFUNC_3_0(int, OSSL_PROVIDER_available, (GO_OSSL_LIB_CTX_PTR libctx, const char *name), (libctx, name)) \
192192
DEFINEFUNC_3_0(GO_OSSL_PROVIDER_PTR, OSSL_PROVIDER_load, (GO_OSSL_LIB_CTX_PTR libctx, const char *name), (libctx, name)) \
193+
DEFINEFUNC_3_0(const char *, OSSL_PROVIDER_get0_name, (const GO_OSSL_PROVIDER_PTR prov), (prov)) \
193194
DEFINEFUNC_3_0(GO_EVP_MD_PTR, EVP_MD_fetch, (GO_OSSL_LIB_CTX_PTR ctx, const char *algorithm, const char *properties), (ctx, algorithm, properties)) \
194195
DEFINEFUNC_3_0(void, EVP_MD_free, (GO_EVP_MD_PTR md), (md)) \
195196
DEFINEFUNC_3_0(const char *, EVP_MD_get0_name, (const GO_EVP_MD_PTR md), (md)) \
197+
DEFINEFUNC_3_0(const GO_OSSL_PROVIDER_PTR, EVP_MD_get0_provider, (const GO_EVP_MD_PTR md), (md)) \
196198
DEFINEFUNC(int, RAND_bytes, (unsigned char *arg0, int arg1), (arg0, arg1)) \
197199
DEFINEFUNC_RENAMED_1_1(GO_EVP_MD_CTX_PTR, EVP_MD_CTX_new, EVP_MD_CTX_create, (void), ()) \
198200
DEFINEFUNC_RENAMED_1_1(void, EVP_MD_CTX_free, EVP_MD_CTX_destroy, (GO_EVP_MD_CTX_PTR ctx), (ctx)) \

0 commit comments

Comments
 (0)