1
0
Fork 0
mirror of https://github.com/mautrix/signal.git synced 2026-05-15 05:36:53 -04:00
mautrix-signal/pkg/libsignalgo/identitykeystore.go

162 lines
6.2 KiB
Go
Raw Permalink Normal View History

2023-12-17 15:54:35 +02:00
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Sumner Evans
2025-01-17 17:49:37 +02:00
// Copyright (C) 2025 Tulir Asokan
2023-12-17 15:54:35 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package libsignalgo
/*
#include "./libsignal-ffi.h"
extern int signal_get_identity_key_pair_callback(void *store_ctx, SignalMutPointerPrivateKey *keyp);
2024-01-04 01:06:45 +02:00
extern int signal_get_local_registration_id_callback(void *store_ctx, uint32_t *idp);
2026-02-09 13:54:02 +02:00
extern int signal_save_identity_key_callback(void *store_ctx, uint8_t *out, SignalMutPointerProtocolAddress address, SignalMutPointerPublicKey public_key);
extern int signal_get_identity_key_callback(void *store_ctx, SignalMutPointerPublicKey *public_keyp, SignalMutPointerProtocolAddress address);
extern int signal_is_trusted_identity_callback(void *store_ctx, bool *out, SignalMutPointerProtocolAddress address, SignalMutPointerPublicKey public_key, uint32_t direction);
extern void signal_destroy_identity_key_store_callback(void *store_ctx);
*/
import "C"
import (
"context"
"unsafe"
)
type SignalDirection uint
const (
SignalDirectionSending SignalDirection = 0
SignalDirectionReceiving SignalDirection = 1
)
type IdentityKeyStore interface {
GetIdentityKeyPair(ctx context.Context) (*IdentityKeyPair, error)
GetLocalRegistrationID(ctx context.Context) (uint32, error)
SaveIdentityKey(ctx context.Context, theirServiceID ServiceID, identityKey *IdentityKey) (bool, error)
GetIdentityKey(ctx context.Context, theirServiceID ServiceID) (*IdentityKey, error)
IsTrustedIdentity(ctx context.Context, theirServiceID ServiceID, identityKey *IdentityKey, direction SignalDirection) (bool, error)
}
//export signal_get_identity_key_pair_callback
func signal_get_identity_key_pair_callback(storeCtx unsafe.Pointer, keyp *C.SignalMutPointerPrivateKey) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
key, err := store.GetIdentityKeyPair(ctx)
if err != nil {
return err
}
if key == nil {
keyp.raw = nil
} else {
clone, err := key.privateKey.Clone()
if err != nil {
return err
}
clone.CancelFinalizer()
keyp.raw = clone.ptr
}
return err
})
}
//export signal_get_local_registration_id_callback
2024-01-04 01:06:45 +02:00
func signal_get_local_registration_id_callback(storeCtx unsafe.Pointer, idp *C.uint32_t) C.int {
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
registrationID, err := store.GetLocalRegistrationID(ctx)
if err == nil {
*idp = C.uint32_t(registrationID)
}
return err
})
}
//export signal_save_identity_key_callback
2026-02-09 13:54:02 +02:00
func signal_save_identity_key_callback(storeCtx unsafe.Pointer, out *C.uint8_t, address C.SignalMutPointerProtocolAddress, publicKey C.SignalMutPointerPublicKey) C.int {
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
publicKeyStruct := PublicKey{ptr: publicKey.raw}
2024-01-04 01:06:45 +02:00
cloned, err := publicKeyStruct.Clone()
if err != nil {
2026-02-09 13:54:02 +02:00
return err
}
2026-02-09 13:54:02 +02:00
addr := &Address{ptr: address.raw}
theirServiceID, err := addr.NameServiceID()
if err != nil {
2026-02-09 13:54:02 +02:00
return err
}
2024-01-04 01:06:45 +02:00
replaced, err := store.SaveIdentityKey(
ctx,
theirServiceID,
2024-01-04 01:06:45 +02:00
&IdentityKey{cloned},
)
if err != nil {
2026-02-09 13:54:02 +02:00
return err
2024-01-04 01:06:45 +02:00
}
if replaced {
2026-02-09 13:54:02 +02:00
*out = 1
2024-01-04 01:06:45 +02:00
} else {
2026-02-09 13:54:02 +02:00
*out = 0
2024-01-04 01:06:45 +02:00
}
2026-02-09 13:54:02 +02:00
return nil
2024-01-04 01:06:45 +02:00
})
}
//export signal_get_identity_key_callback
2026-02-09 13:54:02 +02:00
func signal_get_identity_key_callback(storeCtx unsafe.Pointer, public_keyp *C.SignalMutPointerPublicKey, address C.SignalMutPointerProtocolAddress) C.int {
2024-01-04 01:06:45 +02:00
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
2026-02-09 13:54:02 +02:00
addr := &Address{ptr: address.raw}
theirServiceID, err := addr.NameServiceID()
if err != nil {
return err
}
key, err := store.GetIdentityKey(ctx, theirServiceID)
if err == nil && key != nil {
key.publicKey.CancelFinalizer()
2026-02-09 13:54:02 +02:00
public_keyp.raw = key.publicKey.ptr
}
return err
})
}
//export signal_is_trusted_identity_callback
2026-02-09 13:54:02 +02:00
func signal_is_trusted_identity_callback(storeCtx unsafe.Pointer, out *C.bool, address C.SignalMutPointerProtocolAddress, public_key C.SignalMutPointerPublicKey, direction C.uint32_t) C.int {
return wrapStoreCallback(storeCtx, func(store IdentityKeyStore, ctx context.Context) error {
addr := &Address{ptr: address.raw}
theirServiceID, err := addr.NameServiceID()
if err != nil {
2026-02-09 13:54:02 +02:00
return err
}
2026-02-09 13:54:02 +02:00
trusted, err := store.IsTrustedIdentity(ctx, theirServiceID, &IdentityKey{&PublicKey{ptr: public_key.raw}}, SignalDirection(direction))
2024-01-04 01:06:45 +02:00
if err != nil {
2026-02-09 13:54:02 +02:00
return err
2024-01-04 01:06:45 +02:00
}
2026-02-09 13:54:02 +02:00
*out = C.bool(trusted)
return nil
2024-01-04 01:06:45 +02:00
})
}
2026-02-09 13:54:02 +02:00
//export signal_destroy_identity_key_store_callback
func signal_destroy_identity_key_store_callback(storeCtx unsafe.Pointer) {
// No-op: Go's garbage collector handles cleanup
}
2025-01-17 17:49:37 +02:00
func (ctx *CallbackContext) wrapIdentityKeyStore(store IdentityKeyStore) C.SignalConstPointerFfiIdentityKeyStoreStruct {
return C.SignalConstPointerFfiIdentityKeyStoreStruct{&C.SignalIdentityKeyStore{
ctx: wrapStore(ctx, store),
get_local_identity_private_key: C.SignalFfiBridgeIdentityKeyStoreGetLocalIdentityPrivateKey(C.signal_get_identity_key_pair_callback),
get_local_registration_id: C.SignalFfiBridgeIdentityKeyStoreGetLocalRegistrationId(C.signal_get_local_registration_id_callback),
get_identity_key: C.SignalFfiBridgeIdentityKeyStoreGetIdentityKey(C.signal_get_identity_key_callback),
save_identity_key: C.SignalFfiBridgeIdentityKeyStoreSaveIdentityKey(C.signal_save_identity_key_callback),
is_trusted_identity: C.SignalFfiBridgeIdentityKeyStoreIsTrustedIdentity(C.signal_is_trusted_identity_callback),
destroy: C.SignalFfiBridgeIdentityKeyStoreDestroy(C.signal_destroy_identity_key_store_callback),
2025-01-17 17:49:37 +02:00
}}
}