Remove manual X.509 public key encoding

This commit is contained in:
Tulir Asokan 2023-07-16 02:35:35 +03:00
parent 2d69a3c42f
commit 9f189d913b
3 changed files with 7 additions and 71 deletions

View file

@ -5,6 +5,7 @@ import (
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"crypto/x509"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"math/big" "math/big"
@ -64,12 +65,12 @@ func (t *JWK) GetPublicKey() (*ecdsa.PublicKey, error) {
return pubKey, nil return pubKey, nil
} }
func (t *JWK) MarshalPubKey() ([]byte, error) { func (t *JWK) MarshalX509PublicKey() ([]byte, error) {
pubKey, err := t.GetPublicKey() pubKey, err := t.GetPublicKey()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y), nil return x509.MarshalPKIXPublicKey(pubKey)
} }
func (t *JWK) SignRequest(requestID string, timestamp int64) ([]byte, error) { func (t *JWK) SignRequest(requestID string, timestamp int64) ([]byte, error) {

View file

@ -1,45 +0,0 @@
package crypto
var SequenceOne = []int{1, 2, 840, 10045, 2, 1}
var SequenceTwo = []int{1, 2, 840, 10045, 3, 1, 7}
func EncodeValues(a *[]byte, b []int) {
*a = append(*a, 6)
idx := len(*a)
*a = append(*a, 0)
*a = append(*a, byte(40*b[0]+b[1]))
for i := 2; i < len(b); i++ {
d := b[i]
e := make([]byte, 0)
if d > 128 {
e = append(e, byte(d/128)+128)
}
e = append(e, byte(d%128))
*a = append(*a, e...)
}
(*a)[idx] = byte(len(*a) - idx - 1)
}
func AppendBytes(a []byte, b []byte) []byte {
newA := make([]byte, len(a))
copy(newA, a)
newA = HelperAppendBytes(newA, 48)
newA = HelperAppendBytes(newA, byte(len(b)))
for _, value := range b {
newA = HelperAppendBytes(newA, value)
}
return newA
}
func HelperAppendBytes(a []byte, b byte) []byte {
return append(a, b)
}
func AppendByteSequence(byteArr1 []byte, byteArr2 []byte, uncompressedPublicKey []byte) []byte {
copiedByteArray := AppendBytes(byteArr1, byteArr2)
copiedByteArray = HelperAppendBytes(copiedByteArray, 3)
copiedByteArray = HelperAppendBytes(copiedByteArray, uint8(len(uncompressedPublicKey)+1))
copiedByteArray = HelperAppendBytes(copiedByteArray, 0)
return copiedByteArray
}

View file

@ -11,9 +11,9 @@ import (
func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContainer, error) { func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContainer, error) {
id := util.RandomUUIDv4() id := util.RandomUUIDv4()
encryptedKeys, encryptErr := uncompressKey(jwk) key, err := jwk.MarshalX509PublicKey()
if encryptErr != nil { if err != nil {
return nil, nil, encryptErr return nil, nil, err
} }
payloadData := &binary.AuthenticationContainer{ payloadData := &binary.AuthenticationContainer{
@ -27,7 +27,7 @@ func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContaine
KeyData: &binary.KeyData{ KeyData: &binary.KeyData{
EcdsaKeys: &binary.ECDSAKeys{ EcdsaKeys: &binary.ECDSAKeys{
Field1: 2, Field1: 2,
EncryptedKeys: encryptedKeys, EncryptedKeys: key,
}, },
}, },
}, },
@ -38,23 +38,3 @@ func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContaine
} }
return encoded, payloadData, nil return encoded, payloadData, nil
} }
func uncompressKey(jwk *crypto.JWK) ([]byte, error) {
uncompressedPublicKey, err3 := jwk.MarshalPubKey()
if err3 != nil {
return nil, err3
}
var emptyByteArray []byte
crypto.EncodeValues(&emptyByteArray, crypto.SequenceOne)
crypto.EncodeValues(&emptyByteArray, crypto.SequenceTwo)
var copiedByteArray []byte
copiedByteArray = crypto.AppendByteSequence(copiedByteArray, emptyByteArray, uncompressedPublicKey)
for _, value := range uncompressedPublicKey {
copiedByteArray = crypto.HelperAppendBytes(copiedByteArray, value)
}
var encryptedKeys []byte
encryptedKeys = crypto.AppendBytes(encryptedKeys, copiedByteArray[0:])
return encryptedKeys, nil
}