Skip to content

Commit

Permalink
verificationhelper: fix JSON serialization of keys
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <[email protected]>
  • Loading branch information
sumnerevans committed Nov 22, 2024
1 parent b4551fc commit d3df25e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 28 deletions.
48 changes: 48 additions & 0 deletions crypto/verificationhelper/ecdhkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package verificationhelper

import (
"crypto/ecdh"
"encoding/json"
)

type ECDHPrivateKey struct {
*ecdh.PrivateKey
}

func (e *ECDHPrivateKey) UnmarshalJSON(data []byte) (err error) {
if len(data) == 0 {
return nil
}
var raw []byte
err = json.Unmarshal(data, &raw)
if err != nil {
return
}
e.PrivateKey, err = ecdh.P256().NewPrivateKey(raw)
return err
}

func (e ECDHPrivateKey) MarshalJSON() ([]byte, error) {
if e.PrivateKey == nil {
return json.Marshal(nil)
}
return json.Marshal(e.Bytes())
}

type ECDHPublicKey struct {
*ecdh.PublicKey
}

func (e *ECDHPublicKey) UnmarshalJSON(data []byte) (err error) {
var raw []byte
err = json.Unmarshal(data, &raw)
if err != nil {
return
}
e.PublicKey, err = ecdh.P256().NewPublicKey(raw)
return
}

func (e ECDHPublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Bytes())
}
48 changes: 48 additions & 0 deletions crypto/verificationhelper/ecdhkeys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package verificationhelper_test

import (
"crypto/ecdh"
"crypto/rand"
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"maunium.net/go/mautrix/crypto/verificationhelper"
)

func TestECDHPrivateKey(t *testing.T) {
pk, err := ecdh.P256().GenerateKey(rand.Reader)
require.NoError(t, err)
private := verificationhelper.ECDHPrivateKey{pk}
marshalled, err := json.Marshal(private)
require.NoError(t, err)

assert.Len(t, marshalled, 46)

var unmarshalled verificationhelper.ECDHPrivateKey
err = json.Unmarshal(marshalled, &unmarshalled)
require.NoError(t, err)

assert.True(t, private.Equal(unmarshalled.PrivateKey))
}

func TestECDHPublicKey(t *testing.T) {
private, err := ecdh.P256().GenerateKey(rand.Reader)
require.NoError(t, err)

public := private.PublicKey()

pub := verificationhelper.ECDHPublicKey{public}
marshalled, err := json.Marshal(pub)
require.NoError(t, err)

assert.Len(t, marshalled, 90)

var unmarshalled verificationhelper.ECDHPublicKey
err = json.Unmarshal(marshalled, &unmarshalled)
require.NoError(t, err)

assert.True(t, public.Equal(unmarshalled.PublicKey))
}
28 changes: 0 additions & 28 deletions crypto/verificationhelper/verificationstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package verificationhelper

import (
"context"
"crypto/ecdh"
"encoding/json"
"errors"
"fmt"

Expand Down Expand Up @@ -53,32 +51,6 @@ func (step VerificationState) String() string {
}
}

type ECDHPrivateKey struct {
*ecdh.PrivateKey
}

func (e *ECDHPrivateKey) UnmarshalJSON(data []byte) (err error) {
e.PrivateKey, err = ecdh.P256().NewPrivateKey(data)
return
}

func (e *ECDHPrivateKey) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Bytes())
}

type ECDHPublicKey struct {
*ecdh.PublicKey
}

func (e *ECDHPublicKey) UnmarshalJSON(data []byte) (err error) {
e.PublicKey, err = ecdh.P256().NewPublicKey(data)
return
}

func (e *ECDHPublicKey) MarshalJSON() ([]byte, error) {
return json.Marshal(e.Bytes())
}

type VerificationTransaction struct {
ExpirationTime jsontime.UnixMilli `json:"expiration_time"`

Expand Down

0 comments on commit d3df25e

Please sign in to comment.