Remove useless utility functions

This commit is contained in:
Tulir Asokan 2023-07-15 20:43:28 +03:00
parent aa89c98353
commit 45d5a556ba
14 changed files with 52 additions and 118 deletions

View file

@ -1,27 +1,5 @@
package binary package binary
import (
"fmt"
"google.golang.org/protobuf/proto"
)
func EncodeProtoMessage(message proto.Message) ([]byte, error) {
data, err := proto.Marshal(message)
if err != nil {
return nil, fmt.Errorf("failed to encode proto message: %v", err)
}
return data, nil
}
func DecodeProtoMessage(data []byte, message proto.Message) error {
err := proto.Unmarshal(data, message)
if err != nil {
return fmt.Errorf("failed to decode proto message: %v", err)
}
return nil
}
func (et EmojiType) Unicode() string { func (et EmojiType) Unicode() string {
switch et { switch et {
case EmojiType_LIKE: case EmojiType_LIKE:

View file

@ -11,6 +11,7 @@ import (
"time" "time"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto" "go.mau.fi/mautrix-gmessages/libgm/crypto"
@ -208,7 +209,7 @@ func (c *Client) setApiMethods() {
func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) { func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
reqId := util.RandomUUIDv4() reqId := util.RandomUUIDv4()
download_metadata := &binary.UploadImagePayload{ downloadMetadata := &binary.UploadImagePayload{
MetaData: &binary.ImageMetaData{ MetaData: &binary.ImageMetaData{
ImageID: mediaID, ImageID: mediaID,
Encrypted: true, Encrypted: true,
@ -219,16 +220,16 @@ func (c *Client) DownloadMedia(mediaID string, key []byte) ([]byte, error) {
ConfigVersion: payload.ConfigMessage, ConfigVersion: payload.ConfigMessage,
}, },
} }
download_metadata_bytes, err2 := binary.EncodeProtoMessage(download_metadata) downloadMetadataBytes, err2 := proto.Marshal(downloadMetadata)
if err2 != nil { if err2 != nil {
return nil, err2 return nil, err2
} }
download_metadata_b64 := base64.StdEncoding.EncodeToString(download_metadata_bytes) downloadMetadataEncoded := base64.StdEncoding.EncodeToString(downloadMetadataBytes)
req, err := http.NewRequest("GET", util.UPLOAD_MEDIA, nil) req, err := http.NewRequest("GET", util.UPLOAD_MEDIA, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
util.BuildUploadHeaders(req, download_metadata_b64) util.BuildUploadHeaders(req, downloadMetadataEncoded)
res, reqErr := c.http.Do(req) res, reqErr := c.http.Do(req)
if reqErr != nil { if reqErr != nil {
return nil, reqErr return nil, reqErr

View file

@ -8,10 +8,6 @@ import (
"crypto/sha256" "crypto/sha256"
"errors" "errors"
"io" "io"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary"
) )
type Cryptor struct { type Cryptor struct {
@ -87,17 +83,3 @@ func (c *Cryptor) Decrypt(encryptedData []byte) ([]byte, error) {
return encryptedDataWithoutHMAC, nil return encryptedDataWithoutHMAC, nil
} }
func (c *Cryptor) EncodeAndEncryptData(message proto.Message) ([]byte, error) {
encodedData, encodeErr := binary.EncodeProtoMessage(message)
if encodeErr != nil {
return nil, encodeErr
}
encryptedData, encryptErr := c.Encrypt(encodedData)
if encryptErr != nil {
return nil, encryptErr
}
return encryptedData, nil
}

View file

@ -1,21 +0,0 @@
package crypto
import (
"encoding/base64"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
func DecodeAndEncodeB64(data string, msg proto.Message) error {
decodedBytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return err
}
err = binary.DecodeProtoMessage(decodedBytes, msg)
if err != nil {
return err
}
return nil
}

View file

@ -4,7 +4,9 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand" "crypto/rand"
"crypto/sha256"
"encoding/base64" "encoding/base64"
"fmt"
"math/big" "math/big"
) )
@ -70,6 +72,17 @@ func (t *JWK) MarshalPubKey() ([]byte, error) {
return elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y), nil return elliptic.Marshal(pubKey.Curve, pubKey.X, pubKey.Y), nil
} }
func (t *JWK) SignRequest(requestID string, timestamp int64) ([]byte, error) {
signBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%d", requestID, timestamp)))
privKey, privErr := t.GetPrivateKey()
if privErr != nil {
return nil, privErr
}
return ecdsa.SignASN1(rand.Reader, privKey, signBytes[:])
}
// GenerateECDSAKey generates a new ECDSA private key with P-256 curve // GenerateECDSAKey generates a new ECDSA private key with P-256 curve
func GenerateECDSAKey() (*JWK, error) { func GenerateECDSAKey() (*JWK, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)

View file

@ -1,13 +1,5 @@
package crypto package crypto
import (
"encoding/base64"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary"
)
var SequenceOne = []int{1, 2, 840, 10045, 2, 1} var SequenceOne = []int{1, 2, 840, 10045, 2, 1}
var SequenceTwo = []int{1, 2, 840, 10045, 3, 1, 7} var SequenceTwo = []int{1, 2, 840, 10045, 3, 1, 7}
@ -51,12 +43,3 @@ func AppendByteSequence(byteArr1 []byte, byteArr2 []byte, uncompressedPublicKey
copiedByteArray = HelperAppendBytes(copiedByteArray, 0) copiedByteArray = HelperAppendBytes(copiedByteArray, 0)
return copiedByteArray return copiedByteArray
} }
func EncodeProtoB64(message proto.Message) (string, error) {
protoBytes, protoErr := binary.EncodeProtoMessage(message)
if protoErr != nil {
return "", protoErr
}
encodedStr := base64.StdEncoding.EncodeToString(protoBytes)
return encodedStr, nil
}

View file

@ -1,19 +0,0 @@
package crypto
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/sha256"
"fmt"
)
func (t *JWK) SignRequest(requestID string, timestamp int64) ([]byte, error) {
signBytes := sha256.Sum256([]byte(fmt.Sprintf("%s:%d", requestID, timestamp)))
privKey, privErr := t.GetPrivateKey()
if privErr != nil {
return nil, privErr
}
return ecdsa.SignASN1(rand.Reader, privKey, signBytes[:])
}

View file

@ -2,13 +2,15 @@ package libgm
import ( import (
"bytes" "bytes"
"encoding/base64"
"errors" "errors"
"io" "io"
"net/http" "net/http"
"strconv" "strconv"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/payload" "go.mau.fi/mautrix-gmessages/libgm/payload"
"go.mau.fi/mautrix-gmessages/libgm/util" "go.mau.fi/mautrix-gmessages/libgm/util"
) )
@ -58,7 +60,7 @@ func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, e
defer res.Body.Close() defer res.Body.Close()
rHeaders := res.Header rHeaders := res.Header
googleResponse, err3 := io.ReadAll(res.Body) googleResponse, err3 := io.ReadAll(base64.NewDecoder(base64.StdEncoding, res.Body))
if err3 != nil { if err3 != nil {
return nil, err3 return nil, err3
} }
@ -67,7 +69,7 @@ func (c *Client) FinalizeUploadMedia(upload *StartGoogleUpload) (*MediaUpload, e
c.Logger.Debug().Str("upload_status", uploadStatus).Msg("Upload complete") c.Logger.Debug().Str("upload_status", uploadStatus).Msg("Upload complete")
mediaIDs := &binary.UploadMediaResponse{} mediaIDs := &binary.UploadMediaResponse{}
err3 = crypto.DecodeAndEncodeB64(string(googleResponse), mediaIDs) err3 = proto.Unmarshal(googleResponse, mediaIDs)
if err3 != nil { if err3 != nil {
return nil, err3 return nil, err3
} }
@ -135,10 +137,11 @@ func (c *Client) buildStartUploadPayload() (string, error) {
Mobile: c.authData.DevicePair.Mobile, Mobile: c.authData.DevicePair.Mobile,
} }
protoDataEncoded, protoEncodeErr := crypto.EncodeProtoB64(protoData) protoDataBytes, err := proto.Marshal(protoData)
if protoEncodeErr != nil { if err != nil {
return "", protoEncodeErr return "", err
} }
protoDataEncoded := base64.StdEncoding.EncodeToString(protoDataBytes)
return protoDataEncoded, nil return protoDataEncoded, nil
} }

View file

@ -4,6 +4,8 @@ import (
"io" "io"
"time" "time"
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto" "go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/events" "go.mau.fi/mautrix-gmessages/libgm/events"
@ -59,7 +61,7 @@ func (p *Pairer) RegisterPhoneRelay() (*binary.RegisterPhoneRelayResponse, error
} }
relayResponse.Body.Close() relayResponse.Body.Close()
res := &binary.RegisterPhoneRelayResponse{} res := &binary.RegisterPhoneRelayResponse{}
err3 := binary.DecodeProtoMessage(responseBody, res) err3 := proto.Unmarshal(responseBody, res)
if err3 != nil { if err3 != nil {
return nil, err3 return nil, err3
} }
@ -105,7 +107,7 @@ func (p *Pairer) RefreshPhoneRelay() {
} }
p.client.Logger.Debug().Any("responseLength", len(responseBody)).Msg("Response Body Length") p.client.Logger.Debug().Any("responseLength", len(responseBody)).Msg("Response Body Length")
res := &binary.RefreshPhoneRelayResponse{} res := &binary.RefreshPhoneRelayResponse{}
err3 := binary.DecodeProtoMessage(responseBody, res) err3 := proto.Unmarshal(responseBody, res)
if err3 != nil { if err3 != nil {
p.client.Logger.Err(err3) p.client.Logger.Err(err3)
} }
@ -138,7 +140,7 @@ func (c *Client) GetWebEncryptionKey() (*binary.WebEncryptionKeyResponse, error)
} }
//p.client.Logger.Debug().Any("responseLength", len(responseBody)).Any("raw", responseBody).Msg("Response Body Length") //p.client.Logger.Debug().Any("responseLength", len(responseBody)).Any("raw", responseBody).Msg("Response Body Length")
parsedResponse := &binary.WebEncryptionKeyResponse{} parsedResponse := &binary.WebEncryptionKeyResponse{}
err2 = binary.DecodeProtoMessage(responseBody, parsedResponse) err2 = proto.Unmarshal(responseBody, parsedResponse)
if err2 != nil { if err2 != nil {
c.Logger.Err(err2).Msg("Parse webkeyresponse into proto struct error") c.Logger.Err(err2).Msg("Parse webkeyresponse into proto struct error")
return nil, err2 return nil, err2

View file

@ -1,6 +1,8 @@
package payload package payload
import ( import (
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/util" "go.mau.fi/mautrix-gmessages/libgm/util"
) )
@ -14,7 +16,7 @@ func GetWebEncryptionKey(WebPairKey []byte) ([]byte, *binary.AuthenticationConta
ConfigVersion: ConfigMessage, ConfigVersion: ConfigMessage,
}, },
} }
encodedPayload, err2 := binary.EncodeProtoMessage(payload) encodedPayload, err2 := proto.Marshal(payload)
if err2 != nil { if err2 != nil {
return nil, payload, err2 return nil, payload, err2
} }

View file

@ -1,6 +1,8 @@
package payload package payload
import ( import (
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/util" "go.mau.fi/mautrix-gmessages/libgm/util"
) )
@ -14,7 +16,7 @@ func RefreshPhoneRelay(rpcKey []byte) ([]byte, *binary.AuthenticationContainer,
ConfigVersion: ConfigMessage, ConfigVersion: ConfigMessage,
}, },
} }
encodedPayload, err2 := binary.EncodeProtoMessage(payload) encodedPayload, err2 := proto.Marshal(payload)
if err2 != nil { if err2 != nil {
return nil, payload, err2 return nil, payload, err2
} }

View file

@ -1,6 +1,8 @@
package payload package payload
import ( import (
"google.golang.org/protobuf/proto"
"go.mau.fi/mautrix-gmessages/libgm/binary" "go.mau.fi/mautrix-gmessages/libgm/binary"
"go.mau.fi/mautrix-gmessages/libgm/crypto" "go.mau.fi/mautrix-gmessages/libgm/crypto"
"go.mau.fi/mautrix-gmessages/libgm/util" "go.mau.fi/mautrix-gmessages/libgm/util"
@ -30,7 +32,7 @@ func RegisterPhoneRelay(jwk *crypto.JWK) ([]byte, *binary.AuthenticationContaine
}, },
}, },
} }
encoded, err4 := binary.EncodeProtoMessage(payloadData) encoded, err4 := proto.Marshal(payloadData)
if err4 != nil { if err4 != nil {
return nil, payloadData, err4 return nil, payloadData, err4
} }

View file

@ -92,9 +92,15 @@ func (sm *SendMessageBuilder) SetTTL(ttl int64) *SendMessageBuilder {
} }
func (sm *SendMessageBuilder) SetEncryptedProtoMessage(message proto.Message, cryptor *crypto.Cryptor) *SendMessageBuilder { func (sm *SendMessageBuilder) SetEncryptedProtoMessage(message proto.Message, cryptor *crypto.Cryptor) *SendMessageBuilder {
encryptedBytes, encryptErr := cryptor.EncodeAndEncryptData(message) plaintextBytes, err := proto.Marshal(message)
if encryptErr != nil { if err != nil {
sm.err = encryptErr sm.err = err
return sm
}
encryptedBytes, err := cryptor.Encrypt(plaintextBytes)
if err != nil {
sm.err = err
return sm return sm
} }

View file

@ -44,14 +44,14 @@ func DecryptInternalMessage(internalMessage *binary.InternalMessage, cryptor *cr
switch internalMessage.Data.BugleRoute { switch internalMessage.Data.BugleRoute {
case binary.BugleRoute_PairEvent: case binary.BugleRoute_PairEvent:
decodedData := &binary.PairEvents{} decodedData := &binary.PairEvents{}
decodeErr := binary.DecodeProtoMessage(internalMessage.Data.ProtobufData, decodedData) decodeErr := proto.Unmarshal(internalMessage.Data.ProtobufData, decodedData)
if decodeErr != nil { if decodeErr != nil {
return nil, decodeErr return nil, decodeErr
} }
resp = newResponseFromPairEvent(internalMessage.GetData(), decodedData) resp = newResponseFromPairEvent(internalMessage.GetData(), decodedData)
case binary.BugleRoute_DataEvent: case binary.BugleRoute_DataEvent:
internalRequestData := &binary.InternalRequestData{} internalRequestData := &binary.InternalRequestData{}
decodeErr := binary.DecodeProtoMessage(internalMessage.Data.ProtobufData, internalRequestData) decodeErr := proto.Unmarshal(internalMessage.Data.ProtobufData, internalRequestData)
if decodeErr != nil { if decodeErr != nil {
return nil, decodeErr return nil, decodeErr
} }