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

go/signature: apply options on registered contexts #4591

Merged
merged 3 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .changelog/4591.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/signature: Apply options on registered contexts
24 changes: 12 additions & 12 deletions go/common/crypto/signature/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,20 +313,20 @@ type UnsafeSigner interface {

// PrepareSignerContext prepares a context for use during signing by a Signer.
func PrepareSignerContext(context Context) ([]byte, error) {
// The remote signer implementation uses the raw context, and
// registration is dealt with client side. Just check that the
// length is sensible, even though the client should be sending
// something sane.
if allowUnregisteredContexts {
if cLen := len(context); cLen == 0 || cLen > ed25519.ContextMaxSize {
return nil, errMalformedContext
}
return []byte(context), nil
}

// Ensure that the context is registered for use.
rawOpts, isRegistered := registeredContexts.Load(context)
if !isRegistered {
// The remote signer implementation uses the raw context, and
// registration is dealt with client side. Just check that the
// length is sensible, even though the client should be sending
// something sane.
if allowUnregisteredContexts {
if cLen := len(context); cLen == 0 || cLen > ed25519.ContextMaxSize {
return nil, errMalformedContext
}
return []byte(context), nil
}

// Ensure that the context is registered for use.
return nil, errUnregisteredContext
}
opts := rawOpts.(*contextOptions)
Expand Down
13 changes: 13 additions & 0 deletions go/common/crypto/signature/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"testing"

"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -126,6 +127,10 @@ func TestContext(t *testing.T) {
require.NoError(err, "PrepareSignerMessage should work")
require.NotEqual(msg3, msg4, "messages for different contexts should be different")

ctx5 := NewContext("test: dummy context 5", WithChainSeparation())
msg5, err := PrepareSignerMessage(ctx5, []byte("message"))
require.NoError(err, "PrepareSignerMessage before UnsafeResetChainContext")

// The remote signer requires bypassing the context registration checks.
UnsafeAllowUnregisteredContexts()
defer func() {
Expand All @@ -135,6 +140,14 @@ func TestContext(t *testing.T) {

_, err = PrepareSignerMessage(unregCtx, []byte("message"))
require.NoError(err, "PrepareSignerMessage should work with unregisered context (bypassed)")

overlongUnregCtx := Context("test: l" + strings.Repeat("o", ed25519.ContextMaxSize) + "ng")
_, err = PrepareSignerMessage(overlongUnregCtx, []byte("message"))
require.Error(err, "PrepareSignerMessage should fail with overlong unregistered context")

msg5UAUC, err := PrepareSignerMessage(ctx5, []byte("message"))
require.NoError(err, "PrepareSignerMessage after UnsafeAllowUnregisteredContexts")
require.Equal(msg5, msg5UAUC, "message for same chain context should be same")
}

func TestSignerRoles(t *testing.T) {
Expand Down