From 139a35c219cf1b76ca906e11cd8203402f5641ec Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 24 Oct 2022 13:00:45 -0700 Subject: [PATCH] Expose token.T in json + fix issue with new auth protocol --- primitives/privacypass/token.go | 14 ++++++++++++++ utils/crypto.go | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/primitives/privacypass/token.go b/primitives/privacypass/token.go index a91db76..ee5abf4 100644 --- a/primitives/privacypass/token.go +++ b/primitives/privacypass/token.go @@ -3,6 +3,7 @@ package privacypass import ( "crypto/hmac" "crypto/rand" + "encoding/json" "fmt" "git.openprivacy.ca/cwtch.im/tapir/primitives/core" "git.openprivacy.ca/openprivacy/log" @@ -122,3 +123,16 @@ func UnblindSignedTokenBatch(tokens []*Token, blindedTokens []BlindedToken, sign } return true } + +// MarshalJSON - in order to store tokens in a serialized form we need to expose the private, unexported value +// `t`. Note that `r` is not needed to spend the token, and as such we effectively destroy it when we serialize. +// Ideally, go would let us do this with an annotation, alas. +func (t Token) MarshalJSON() ([]byte, error) { + return json.Marshal(struct { + T []byte `json:"t"` + W *ristretto.Element + }{ + T: t.t, + W: t.W, + }) +} diff --git a/utils/crypto.go b/utils/crypto.go index fd5f0d6..e3cdb6f 100644 --- a/utils/crypto.go +++ b/utils/crypto.go @@ -16,16 +16,16 @@ func EDH(privateKey ed25519.PrivateKey, remotePublicKey ed25519.PublicKey) ([]by var curve25519priv [32]byte PrivateKeyToCurve25519(&curve25519priv, &privKeyBytes) - curve25519pub, err := ed25519PublicKeyToCurve25519(remotePublicKey) - if err == nil { - return nil, err + remoteCurve25519pub, err := ed25519PublicKeyToCurve25519New(remotePublicKey) + if err != nil { + return []byte{}, err } - secret, err := curve25519.X25519(curve25519priv[:], curve25519pub[:]) + secret, err := curve25519.X25519(curve25519priv[:], remoteCurve25519pub[:]) return secret, err } // reproduced from https://github.com/FiloSottile/age/blob/main/agessh/agessh.go#L190 -func ed25519PublicKeyToCurve25519(pk ed25519.PublicKey) ([]byte, error) { +func ed25519PublicKeyToCurve25519New(pk ed25519.PublicKey) ([]byte, error) { // See https://blog.filippo.io/using-ed25519-keys-for-encryption and // https://pkg.go.dev/filippo.io/edwards25519#Point.BytesMontgomery. p, err := new(edwards25519.Point).SetBytes(pk)