Skip to content

Commit

Permalink
Remove custom UUID type
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 22, 2023
1 parent 5efa86b commit 3a3c729
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 115 deletions.
17 changes: 7 additions & 10 deletions pkg/libsignalgo/authcredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ package libsignalgo
import "C"
import (
"unsafe"

"github.com/google/uuid"
)

// type AuthCredential [C.SignalAUTH_CREDENTIAL_LEN]byte
Expand All @@ -38,8 +40,8 @@ func (ac *AuthCredentialWithPni) Slice() []byte {

func ReceiveAuthCredentialWithPni(
serverPublicParams ServerPublicParams,
aci UUID,
pni UUID,
aci uuid.UUID,
pni uuid.UUID,
redemptionTime uint64,
authCredResponse AuthCredentialWithPniResponse,
) (*AuthCredentialWithPni, error) {
Expand All @@ -49,14 +51,9 @@ func ReceiveAuthCredentialWithPni(
if err != nil {
return nil, err
}
var c_pni cPNIType
if len(pni) != 16 {
c_pni = nil
} else {
c_pni, err = SignalPNIServiceIdFromUUID(pni)
if err != nil {
return nil, err
}
c_pni, err := SignalPNIServiceIdFromUUID(pni)
if err != nil {
return nil, err
}
c_authCredResponse := (*[C.SignalAUTH_CREDENTIAL_WITH_PNI_RESPONSE_LEN]C.uchar)(unsafe.Pointer(&authCredResponse[0]))

Expand Down
2 changes: 0 additions & 2 deletions pkg/libsignalgo/groupcipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
gopointer "github.com/mattn/go-pointer"
)

var UUIDLen = sizeMustMatch(C.SignalUUID_LEN, 16)

func GroupEncrypt(ptext []byte, sender *Address, distributionID uuid.UUID, store SenderKeyStore, ctx *CallbackContext) (*CiphertextMessage, error) {
contextPointer := gopointer.Save(ctx)
defer gopointer.Unref(contextPointer)
Expand Down
14 changes: 8 additions & 6 deletions pkg/libsignalgo/groupsecretparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import "C"
import (
"crypto/rand"
"unsafe"

"github.com/google/uuid"
)

type Randomness [C.SignalRANDOMNESS_LEN]byte
Expand Down Expand Up @@ -108,27 +110,27 @@ func (gsp *GroupSecretParams) DecryptBlobWithPadding(blob []byte) ([]byte, error
return CopySignalOwnedBufferToBytes(plaintext), nil
}

func (gsp *GroupSecretParams) DecryptUUID(ciphertextUUID UUIDCiphertext) (*UUID, error) {
uuid := C.SignalServiceIdFixedWidthBinaryBytes{}
func (gsp *GroupSecretParams) DecryptUUID(ciphertextUUID UUIDCiphertext) (*uuid.UUID, error) {
u := C.SignalServiceIdFixedWidthBinaryBytes{}
signalFfiError := C.signal_group_secret_params_decrypt_service_id(
&uuid,
&u,
(*[C.SignalGROUP_SECRET_PARAMS_LEN]C.uint8_t)(unsafe.Pointer(gsp)),
(*[C.SignalUUID_CIPHERTEXT_LEN]C.uint8_t)(unsafe.Pointer(&ciphertextUUID)),
)
if signalFfiError != nil {
return nil, wrapError(signalFfiError)
}

result, err := SignalServiceIdToUUID(&uuid)
result, err := SignalServiceIdToUUID(&u)
if err != nil {
return nil, err
}
return &result, nil
}

func (gsp *GroupSecretParams) DecryptProfileKey(ciphertextProfileKey ProfileKeyCiphertext, uuid UUID) (*ProfileKey, error) {
func (gsp *GroupSecretParams) DecryptProfileKey(ciphertextProfileKey ProfileKeyCiphertext, u uuid.UUID) (*ProfileKey, error) {
profileKey := [C.SignalPROFILE_KEY_LEN]C.uchar{}
serviceId, err := SignalServiceIdFromUUID(uuid)
serviceId, err := SignalServiceIdFromUUID(u)
if err != nil {
return nil, err
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/libsignalgo/profilekey.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"crypto/rand"
"fmt"
"unsafe"

"github.com/google/uuid"
)

type ProfileKey [C.SignalPROFILE_KEY_LEN]byte
Expand All @@ -45,10 +47,10 @@ func (pk *ProfileKey) Slice() []byte {
return (*pk)[:]
}

func (pk *ProfileKey) GetCommitment(uuid UUID) (*ProfileKeyCommitment, error) {
func (pk *ProfileKey) GetCommitment(u uuid.UUID) (*ProfileKeyCommitment, error) {
c_result := [C.SignalPROFILE_KEY_COMMITMENT_LEN]C.uchar{}
c_profileKey := (*[C.SignalPROFILE_KEY_LEN]C.uchar)(unsafe.Pointer(pk))
c_uuid, err := SignalServiceIdFromUUID(uuid)
c_uuid, err := SignalServiceIdFromUUID(u)
if err != nil {
return nil, err
}
Expand All @@ -68,10 +70,10 @@ func (pk *ProfileKey) GetCommitment(uuid UUID) (*ProfileKeyCommitment, error) {
return &result, nil
}

func (pk *ProfileKey) GetProfileKeyVersion(uuid UUID) (*ProfileKeyVersion, error) {
func (pk *ProfileKey) GetProfileKeyVersion(u uuid.UUID) (*ProfileKeyVersion, error) {
c_result := [C.SignalPROFILE_KEY_VERSION_ENCODED_LEN]C.uchar{}
c_profileKey := (*[C.SignalPROFILE_KEY_LEN]C.uchar)(unsafe.Pointer(pk))
c_uuid, err := SignalServiceIdFromUUID(uuid)
c_uuid, err := SignalServiceIdFromUUID(u)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,13 +134,13 @@ type ProfileKeyCredentialResponse []byte
type ProfileKeyCredentialPresentation []byte
type ServerPublicParams [C.SignalSERVER_PUBLIC_PARAMS_LEN]byte

func CreateProfileKeyCredentialRequestContext(serverPublicParams ServerPublicParams, uuid UUID, profileKey ProfileKey) (*ProfileKeyCredentialRequestContext, error) {
func CreateProfileKeyCredentialRequestContext(serverPublicParams ServerPublicParams, u uuid.UUID, profileKey ProfileKey) (*ProfileKeyCredentialRequestContext, error) {
c_result := [C.SignalPROFILE_KEY_CREDENTIAL_REQUEST_CONTEXT_LEN]C.uchar{}
c_serverPublicParams := (*[C.SignalSERVER_PUBLIC_PARAMS_LEN]C.uchar)(unsafe.Pointer(&serverPublicParams[0]))
random := [32]byte(randBytes(32))
c_random := (*[32]C.uchar)(unsafe.Pointer(&random[0]))
c_profileKey := (*[C.SignalPROFILE_KEY_LEN]C.uchar)(unsafe.Pointer(&profileKey[0]))
c_uuid, err := SignalServiceIdFromUUID(uuid)
c_uuid, err := SignalServiceIdFromUUID(u)
if err != nil {
return nil, err
}
Expand Down
26 changes: 17 additions & 9 deletions pkg/libsignalgo/serviceid.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ package libsignalgo
*/
import "C"
import (
"errors"
"unsafe"

"github.com/google/uuid"
)

type UUID [C.SignalUUID_LEN]byte
func init() {
if C.SignalUUID_LEN != 16 {
panic("libsignal-ffi uuid type size mismatch")
}
}

func SignalServiceIdFromUUID(uuid UUID) (cPNIType, error) {
func SignalServiceIdFromUUID(uuid uuid.UUID) (cPNIType, error) {
var result C.SignalServiceIdFixedWidthBinaryBytes
signalFfiError := C.signal_service_id_parse_from_service_id_binary(&result, BytesToBuffer(uuid[:]))
if signalFfiError != nil {
Expand All @@ -37,7 +44,7 @@ func SignalServiceIdFromUUID(uuid UUID) (cPNIType, error) {
return cPNIType(unsafe.Pointer(&result)), nil
}

func SignalPNIServiceIdFromUUID(uuid UUID) (cPNIType, error) {
func SignalPNIServiceIdFromUUID(uuid uuid.UUID) (cPNIType, error) {
var result C.SignalServiceIdFixedWidthBinaryBytes
// Prepend a 0x01 to the UUID to indicate that it is a PNI UUID
pniUUID := append([]byte{0x01}, uuid[:]...)
Expand All @@ -48,15 +55,16 @@ func SignalPNIServiceIdFromUUID(uuid UUID) (cPNIType, error) {
return cPNIType(unsafe.Pointer(&result)), nil
}

func SignalServiceIdToUUID(serviceId *C.SignalServiceIdFixedWidthBinaryBytes) (UUID, error) {
func SignalServiceIdToUUID(serviceId *C.SignalServiceIdFixedWidthBinaryBytes) (uuid.UUID, error) {
result := C.SignalOwnedBuffer{}
serviceIdBytes := cPNIType(unsafe.Pointer(serviceId)) // Hack around gcc bug, not needed for clang
signalFfiError := C.signal_service_id_service_id_binary(&result, serviceIdBytes)
if signalFfiError != nil {
return UUID{}, wrapError(signalFfiError)
return uuid.UUID{}, wrapError(signalFfiError)
}
uuidBytes := CopySignalOwnedBufferToBytes(result)
if len(uuidBytes) != 16 {
return uuid.UUID{}, errors.New("invalid UUID length")
}
UUIDBytes := CopySignalOwnedBufferToBytes(result)
var uuid UUID
copy(uuid[:], UUIDBytes)
return uuid, nil
return uuid.UUID(uuidBytes), nil
}
25 changes: 0 additions & 25 deletions pkg/libsignalgo/util.go

This file was deleted.

11 changes: 6 additions & 5 deletions pkg/signalmeow/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"time"
"unicode"

"github.com/google/uuid"
"google.golang.org/protobuf/proto"

"go.mau.fi/mautrix-signal/pkg/libsignalgo"
Expand Down Expand Up @@ -157,20 +158,20 @@ func GetAuthorizationForToday(ctx context.Context, d *Device, masterKey libsigna
}

// Receive the auth credential
aciUuidBytes, err := convertUUIDToByteUUID(d.Data.AciUuid)
aciUuidBytes, err := uuid.Parse(d.Data.AciUuid)
if err != nil {
zlog.Err(err).Msg("aci convertUUIDToBytes error")
return nil, err
}
pniUuidBytes, err := convertUUIDToByteUUID(d.Data.PniUuid)
pniUuidBytes, err := uuid.Parse(d.Data.PniUuid)
if err != nil {
zlog.Err(err).Msg("pni convertUUIDToBytes error")
return nil, err
}
authCredential, err := libsignalgo.ReceiveAuthCredentialWithPni(
serverPublicParams(),
*aciUuidBytes,
*pniUuidBytes,
aciUuidBytes,
pniUuidBytes,
redemptionTime,
*authCredentialResponse,
)
Expand Down Expand Up @@ -306,7 +307,7 @@ func decryptGroup(encryptedGroup *signalpb.Group, groupMasterKey SerializedGroup
return nil, err
}
decryptedGroup.Members = append(decryptedGroup.Members, &GroupMember{
UserId: convertByteUUIDToUUID(*userID),
UserId: userID.String(),
ProfileKey: *profileKey,
Role: GroupMemberRole(member.Role),
JoinedAtRevision: member.JoinedAtRevision,
Expand Down
21 changes: 0 additions & 21 deletions pkg/signalmeow/message.go

This file was deleted.

27 changes: 0 additions & 27 deletions pkg/signalmeow/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ package signalmeow

import (
"encoding/base64"
"encoding/hex"
"errors"
"strings"

"github.com/rs/zerolog"

Expand Down Expand Up @@ -94,27 +91,3 @@ func serverPublicParams() libsignalgo.ServerPublicParams {
copy(serverPublicParams[:], serverPublicParamsBytes)
return serverPublicParams
}

func convertUUIDToByteUUID(uuid string) (*libsignalgo.UUID, error) {
uuid = strings.Replace(uuid, "-", "", -1)
uuidBytes, err := hex.DecodeString(uuid)
if err != nil {
return nil, err
}
if len(uuidBytes) != 16 {
return nil, errors.New("invalid UUID length")
}
byteUUID := libsignalgo.UUID(uuidBytes)
return &byteUUID, nil
}

func convertByteUUIDToUUID(uuidBytes libsignalgo.UUID) string {
uuid := hex.EncodeToString(uuidBytes[:4]) + "-" +
hex.EncodeToString(uuidBytes[4:6]) + "-" +
hex.EncodeToString(uuidBytes[6:8]) + "-" +
hex.EncodeToString(uuidBytes[8:10]) + "-" +
hex.EncodeToString(uuidBytes[10:])
// ensure uuid is lowercase
uuid = strings.ToLower(uuid)
return uuid
}
10 changes: 6 additions & 4 deletions pkg/signalmeow/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"strings"
"time"

"github.com/google/uuid"

"go.mau.fi/mautrix-signal/pkg/libsignalgo"
"go.mau.fi/mautrix-signal/pkg/signalmeow/web"
)
Expand Down Expand Up @@ -67,12 +69,12 @@ func ProfileKeyCredentialRequest(ctx context.Context, d *Device, signalId string
zlog.Err(err).Msg("ProfileKey error")
return nil, err
}
uuid, err := convertUUIDToByteUUID(signalId)
parsedUUID, err := uuid.Parse(signalId)
serverPublicParams := serverPublicParams()

requestContext, err := libsignalgo.CreateProfileKeyCredentialRequestContext(
serverPublicParams,
*uuid,
parsedUUID,
*profileKey,
)
if err != nil {
Expand Down Expand Up @@ -179,13 +181,13 @@ func fetchProfileByID(ctx context.Context, d *Device, signalID string) (*Profile
zlog.Err(err).Msg("profileKey is nil")
return nil, nil
}
uuid, err := convertUUIDToByteUUID(signalID)
u, err := uuid.Parse(signalID)
if err != nil {
zlog.Err(err).Msg("UUIDFromString error")
return nil, err
}

profileKeyVersion, err := profileKey.GetProfileKeyVersion(*uuid)
profileKeyVersion, err := profileKey.GetProfileKeyVersion(u)
if err != nil {
zlog.Err(err).Msg("profileKey error")
return nil, err
Expand Down

0 comments on commit 3a3c729

Please sign in to comment.