gmessages/libgm/payload/registerPhoneRelay.go

59 lines
1.6 KiB
Go
Raw Normal View History

2023-06-30 09:54:08 +00:00
package payload
import (
"go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/util"
)
func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContainer, error) {
2023-06-30 09:54:08 +00:00
id := util.RandomUUIDv4()
encryptedKeys, encryptErr := uncompressKey(jwk)
if encryptErr != nil {
return nil, nil, encryptErr
}
payloadData := &binary.AuthenticationContainer{
AuthMessage: &binary.AuthenticationMessage{
RequestID: id,
Network: Network,
ConfigVersion: ConfigMessage,
},
BrowserDetails: BrowserDetailsMessage,
Data: &binary.AuthenticationContainer_KeyData{
KeyData: &binary.KeyData{
EcdsaKeys: &binary.ECDSAKeys{
Field1: 2,
EncryptedKeys: encryptedKeys,
},
},
},
}
encoded, err4 := binary.EncodeProtoMessage(payloadData)
if err4 != nil {
return nil, payloadData, err4
}
return encoded, payloadData, nil
}
func uncompressKey(jwk *crypto.JWK) ([]byte, error) {
2023-07-15 13:45:50 +00:00
uncompressedPublicKey, err3 := jwk.MarshalPubKey()
2023-06-30 09:54:08 +00:00
if err3 != nil {
return nil, err3
2023-06-30 09:54:08 +00:00
}
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
2023-06-30 09:54:08 +00:00
}