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"
|
|
|
|
)
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContainer, error) {
|
2023-06-30 09:54:08 +00:00
|
|
|
id := util.RandomUUIDv4()
|
2023-07-09 11:16:52 +00:00
|
|
|
|
|
|
|
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-06-30 09:54:08 +00:00
|
|
|
decodedPrivateKey, err2 := jwk.PrivKeyB64Bytes()
|
|
|
|
if err2 != nil {
|
2023-07-09 11:16:52 +00:00
|
|
|
return nil, err2
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|
|
|
|
jwk.PrivateBytes = decodedPrivateKey
|
|
|
|
uncompressedPublicKey, err3 := jwk.UncompressPubKey()
|
|
|
|
if err3 != nil {
|
2023-07-09 11:16:52 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-07-09 11:16:52 +00:00
|
|
|
var encryptedKeys []byte
|
|
|
|
encryptedKeys = crypto.AppendBytes(encryptedKeys, copiedByteArray[0:])
|
|
|
|
return encryptedKeys, nil
|
2023-06-30 09:54:08 +00:00
|
|
|
}
|