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-02-09 13:54:02 +02:00
|
|
|
extern int signal_load_sender_key_callback(void *store_ctx, SignalMutPointerSenderKeyRecord *out, SignalMutPointerProtocolAddress sender, SignalUuid distribution_id);
|
|
|
|
|
extern int signal_store_sender_key_callback(void *store_ctx, SignalMutPointerProtocolAddress sender, SignalUuid distribution_id, SignalMutPointerSenderKeyRecord record);
|
|
|
|
|
extern void signal_destroy_sender_key_store_callback(void *store_ctx);
|
2023-03-22 10:31:22 -06:00
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SenderKeyStore interface {
|
2024-01-04 01:06:45 +02:00
|
|
|
LoadSenderKey(ctx context.Context, sender *Address, distributionID uuid.UUID) (*SenderKeyRecord, error)
|
|
|
|
|
StoreSenderKey(ctx context.Context, sender *Address, distributionID uuid.UUID, record *SenderKeyRecord) error
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_load_sender_key_callback
|
2026-02-09 13:54:02 +02:00
|
|
|
func signal_load_sender_key_callback(storeCtx unsafe.Pointer, recordp *C.SignalMutPointerSenderKeyRecord, address C.SignalMutPointerProtocolAddress, distributionID C.SignalUuid) C.int {
|
2024-01-04 01:06:45 +02:00
|
|
|
return wrapStoreCallback(storeCtx, func(store SenderKeyStore, ctx context.Context) error {
|
2026-02-09 13:54:02 +02:00
|
|
|
record, err := store.LoadSenderKey(ctx, &Address{ptr: address.raw}, *(*uuid.UUID)(unsafe.Pointer(&distributionID)))
|
2023-03-22 10:31:22 -06:00
|
|
|
if err == nil && record != nil {
|
2024-01-02 23:49:44 +02:00
|
|
|
record.CancelFinalizer()
|
2026-02-09 13:54:02 +02:00
|
|
|
recordp.raw = record.ptr
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_store_sender_key_callback
|
2026-02-09 13:54:02 +02:00
|
|
|
func signal_store_sender_key_callback(storeCtx unsafe.Pointer, address C.SignalMutPointerProtocolAddress, distributionID C.SignalUuid, senderKeyRecord C.SignalMutPointerSenderKeyRecord) C.int {
|
2024-01-04 01:06:45 +02:00
|
|
|
return wrapStoreCallback(storeCtx, func(store SenderKeyStore, ctx context.Context) error {
|
2026-02-09 13:54:02 +02:00
|
|
|
record := SenderKeyRecord{ptr: senderKeyRecord.raw}
|
2023-03-22 10:31:22 -06:00
|
|
|
cloned, err := record.Clone()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-09 13:54:02 +02:00
|
|
|
return store.StoreSenderKey(ctx, &Address{ptr: address.raw}, *(*uuid.UUID)(unsafe.Pointer(&distributionID)), cloned)
|
2023-03-22 10:31:22 -06:00
|
|
|
})
|
|
|
|
|
}
|
2024-01-04 01:06:45 +02:00
|
|
|
|
2026-02-09 13:54:02 +02:00
|
|
|
//export signal_destroy_sender_key_store_callback
|
|
|
|
|
func signal_destroy_sender_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) wrapSenderKeyStore(store SenderKeyStore) C.SignalConstPointerFfiSenderKeyStoreStruct {
|
|
|
|
|
return C.SignalConstPointerFfiSenderKeyStoreStruct{&C.SignalSenderKeyStore{
|
2024-01-04 01:06:45 +02:00
|
|
|
ctx: wrapStore(ctx, store),
|
2026-02-09 13:54:02 +02:00
|
|
|
load_sender_key: C.SignalFfiBridgeSenderKeyStoreLoadSenderKey(C.signal_load_sender_key_callback),
|
|
|
|
|
store_sender_key: C.SignalFfiBridgeSenderKeyStoreStoreSenderKey(C.signal_store_sender_key_callback),
|
|
|
|
|
destroy: C.SignalFfiBridgeSenderKeyStoreDestroy(C.signal_destroy_sender_key_store_callback),
|
2025-01-17 17:49:37 +02:00
|
|
|
}}
|
2024-01-04 01:06:45 +02:00
|
|
|
}
|