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/>.
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
package libsignalgo
|
|
|
|
|
|
|
|
|
|
/*
|
2023-03-22 10:55:34 -06:00
|
|
|
#include "./libsignal-ffi.h"
|
2023-03-22 10:31:22 -06:00
|
|
|
|
2026-03-24 21:44:23 +02:00
|
|
|
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);
|
2023-03-22 10:31:22 -06:00
|
|
|
*/
|
|
|
|
|
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)
|
2024-03-21 16:24:03 +02:00
|
|
|
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)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_get_identity_key_pair_callback
|
2026-03-24 21:44:23 +02:00
|
|
|
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 {
|
2023-03-22 10:31:22 -06:00
|
|
|
key, err := store.GetIdentityKeyPair(ctx)
|
2023-07-09 09:39:45 -04:00
|
|
|
if err != nil {
|
2023-07-26 10:03:28 -04:00
|
|
|
return err
|
2023-07-09 09:39:45 -04:00
|
|
|
}
|
|
|
|
|
if key == nil {
|
2026-03-24 21:44:23 +02:00
|
|
|
keyp.raw = nil
|
|
|
|
|
} else {
|
|
|
|
|
clone, err := key.privateKey.Clone()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
clone.CancelFinalizer()
|
|
|
|
|
keyp.raw = clone.ptr
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-03-22 10:31:22 -06:00
|
|
|
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
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
2026-02-09 13:54:02 +02:00
|
|
|
addr := &Address{ptr: address.raw}
|
2024-03-21 16:24:03 +02:00
|
|
|
theirServiceID, err := addr.NameServiceID()
|
|
|
|
|
if err != nil {
|
2026-02-09 13:54:02 +02:00
|
|
|
return err
|
2024-03-21 16:24:03 +02:00
|
|
|
}
|
2024-01-04 01:06:45 +02:00
|
|
|
replaced, err := store.SaveIdentityKey(
|
|
|
|
|
ctx,
|
2024-03-21 16:24:03 +02:00
|
|
|
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
|
|
|
})
|
2023-03-22 10:31:22 -06: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}
|
2024-03-21 16:24:03 +02:00
|
|
|
theirServiceID, err := addr.NameServiceID()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
key, err := store.GetIdentityKey(ctx, theirServiceID)
|
2023-03-22 10:31:22 -06:00
|
|
|
if err == nil && key != nil {
|
2024-01-02 23:49:44 +02:00
|
|
|
key.publicKey.CancelFinalizer()
|
2026-02-09 13:54:02 +02:00
|
|
|
public_keyp.raw = key.publicKey.ptr
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
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}
|
2024-03-21 16:24:03 +02:00
|
|
|
theirServiceID, err := addr.NameServiceID()
|
|
|
|
|
if err != nil {
|
2026-02-09 13:54:02 +02:00
|
|
|
return err
|
2024-03-21 16:24:03 +02:00
|
|
|
}
|
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
|
|
|
})
|
2023-03-22 10:31:22 -06: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{
|
2026-03-24 21:44:23 +02:00
|
|
|
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
|
|
|
}}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|