-
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
verificationhelper: fix JSON serialization of keys
Signed-off-by: Sumner Evans <[email protected]>
- Loading branch information
1 parent
b4551fc
commit d3df25e
Showing
3 changed files
with
96 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters