Remove unnecessary JWK functions

This commit is contained in:
Tulir Asokan 2023-07-15 16:45:50 +03:00
parent ffd84f5039
commit 50ea658fc9
3 changed files with 20 additions and 78 deletions

View file

@ -5,20 +5,15 @@ import (
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"encoding/json"
"fmt"
"math/big" "math/big"
) )
type JWK struct { type JWK struct {
Kty string `json:"kty"` KeyType string `json:"kty"`
Crv string `json:"crv"` Curve string `json:"crv"`
D string `json:"d"` D string `json:"d"`
X string `json:"x"` X string `json:"x"`
Y string `json:"y"` Y string `json:"y"`
Ext bool `json:"ext"`
KeyOps []string `json:"key_ops"`
PrivateBytes []byte `json:"private_bytes,omitempty"`
} }
func (t *JWK) GetPrivateKey() (*ecdsa.PrivateKey, error) { func (t *JWK) GetPrivateKey() (*ecdsa.PrivateKey, error) {
@ -47,34 +42,7 @@ func (t *JWK) GetPrivateKey() (*ecdsa.PrivateKey, error) {
return priv, nil return priv, nil
} }
// Returns a byte slice containing the JWK and an error if the generation or export failed. func (t *JWK) GetPublicKey() (*ecdsa.PublicKey, error) {
func (t *JWK) Marshal() ([]byte, error) {
JWKJSON, err := json.Marshal(t)
if err != nil {
fmt.Printf("Failed to marshal JWK: %v", err)
return nil, err
}
fmt.Printf("%s\n", JWKJSON)
return JWKJSON, err
}
func (t *JWK) PrivKeyB64Bytes() ([]byte, error) {
decodedPrivateKey, err2 := base64.RawURLEncoding.DecodeString(t.D)
return decodedPrivateKey, err2
}
func (t *JWK) ExtractPublicKeyDetails(pubKey []byte) *JWK {
x := base64.RawURLEncoding.EncodeToString(pubKey[1:33])
y := base64.RawURLEncoding.EncodeToString(pubKey[33:])
return &JWK{
Kty: "EC",
Crv: "P-256",
X: x,
Y: y,
}
}
func (t *JWK) DecompressPubkey() (*ecdsa.PublicKey, error) {
xBytes, err := base64.RawURLEncoding.DecodeString(t.X) xBytes, err := base64.RawURLEncoding.DecodeString(t.X)
if err != nil { if err != nil {
return nil, err return nil, err
@ -94,46 +62,25 @@ func (t *JWK) DecompressPubkey() (*ecdsa.PublicKey, error) {
return pubKey, nil return pubKey, nil
} }
func (t *JWK) UncompressPubKey() ([]byte, error) { func (t *JWK) MarshalPubKey() ([]byte, error) {
xBytes, err := base64.RawURLEncoding.DecodeString(t.X) pubKey, err := t.GetPublicKey()
if err != nil { if err != nil {
return nil, err return nil, err
} }
yBytes, err := base64.RawURLEncoding.DecodeString(t.Y) return elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y), nil
if err != nil {
return nil, err
}
x := new(big.Int).SetBytes(xBytes)
y := new(big.Int).SetBytes(yBytes)
pubKey := &ecdsa.PublicKey{
Curve: elliptic.P256(),
X: x,
Y: y,
}
uncompressedPubKey := elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y)
return uncompressedPubKey, nil
} }
// GenerateECDSA_P256_JWK generates a new ECDSA private key with P-256 curve // GenerateECDSAKey generates a new ECDSA private key with P-256 curve
func GenerateECDSA_P256_JWK() (*JWK, error) { func GenerateECDSAKey() (*JWK, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil { if err != nil {
fmt.Printf("Failed to generate private key: %v", err)
return nil, err return nil, err
} }
return &JWK{
JWK := &JWK{ KeyType: "EC",
Kty: "EC", Curve: "P-256",
Crv: "P-256",
D: base64.RawURLEncoding.EncodeToString(privKey.D.Bytes()), D: base64.RawURLEncoding.EncodeToString(privKey.D.Bytes()),
X: base64.RawURLEncoding.EncodeToString(privKey.X.Bytes()), X: base64.RawURLEncoding.EncodeToString(privKey.X.Bytes()),
Y: base64.RawURLEncoding.EncodeToString(privKey.Y.Bytes()), Y: base64.RawURLEncoding.EncodeToString(privKey.Y.Bytes()),
Ext: true, }, nil
KeyOps: []string{"sign"},
}
return JWK, nil
} }

View file

@ -25,7 +25,7 @@ refreshQrCodeTime is the interval to refresh the qr code in seconds, this is usu
func (c *Client) NewPairer(keyData *crypto.JWK, refreshQrCodeTime int) (*Pairer, error) { func (c *Client) NewPairer(keyData *crypto.JWK, refreshQrCodeTime int) (*Pairer, error) {
if keyData == nil { if keyData == nil {
var err error var err error
keyData, err = crypto.GenerateECDSA_P256_JWK() keyData, err = crypto.GenerateECDSAKey()
c.updateJWK(keyData) c.updateJWK(keyData)
if err != nil { if err != nil {
c.Logger.Error().Any("data", keyData).Msg(err.Error()) c.Logger.Error().Any("data", keyData).Msg(err.Error())

View file

@ -38,12 +38,7 @@ func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContaine
} }
func uncompressKey(jwk *crypto.JWK) ([]byte, error) { func uncompressKey(jwk *crypto.JWK) ([]byte, error) {
decodedPrivateKey, err2 := jwk.PrivKeyB64Bytes() uncompressedPublicKey, err3 := jwk.MarshalPubKey()
if err2 != nil {
return nil, err2
}
jwk.PrivateBytes = decodedPrivateKey
uncompressedPublicKey, err3 := jwk.UncompressPubKey()
if err3 != nil { if err3 != nil {
return nil, err3 return nil, err3
} }