2023-12-17 15:54:35 +02:00
|
|
|
// mautrix-signal - A Matrix-signal puppeting bridge.
|
|
|
|
|
// Copyright (C) 2023 Scott Weber
|
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-07-28 14:21:14 -04:00
|
|
|
package libsignalgo
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
#include "./libsignal-ffi.h"
|
|
|
|
|
|
2026-01-16 15:01:48 +02:00
|
|
|
extern int signal_load_kyber_pre_key_callback(void *store_ctx, SignalMutPointerKyberPreKeyRecord *recordp, uint32_t id);
|
|
|
|
|
extern int signal_store_kyber_pre_key_callback(void *store_ctx, uint32_t id, SignalMutPointerKyberPreKeyRecord record);
|
|
|
|
|
extern int signal_mark_kyber_pre_key_used_callback(void *store_ctx, uint32_t id, uint32_t ec_prekey_id, SignalMutPointerPublicKey base_key);
|
|
|
|
|
extern void signal_destroy_kyber_pre_key_store_callback(void *store_ctx);
|
2023-07-28 14:21:14 -04:00
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type KyberPreKeyStore interface {
|
2024-01-04 01:06:45 +02:00
|
|
|
LoadKyberPreKey(ctx context.Context, id uint32) (*KyberPreKeyRecord, error)
|
|
|
|
|
StoreKyberPreKey(ctx context.Context, id uint32, kyberPreKeyRecord *KyberPreKeyRecord) error
|
|
|
|
|
MarkKyberPreKeyUsed(ctx context.Context, id uint32) error
|
2023-07-28 14:21:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_load_kyber_pre_key_callback
|
2026-01-16 15:01:48 +02:00
|
|
|
func signal_load_kyber_pre_key_callback(storeCtx unsafe.Pointer, keyp *C.SignalMutPointerKyberPreKeyRecord, id C.uint32_t) C.int {
|
2024-01-04 01:06:45 +02:00
|
|
|
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
|
|
|
|
key, err := store.LoadKyberPreKey(ctx, uint32(id))
|
2023-07-28 14:21:14 -04:00
|
|
|
if err == nil && key != nil {
|
2024-01-02 23:49:44 +02:00
|
|
|
key.CancelFinalizer()
|
2026-01-16 15:01:48 +02:00
|
|
|
keyp.raw = key.ptr
|
2023-07-28 14:21:14 -04:00
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_store_kyber_pre_key_callback
|
2026-01-16 15:01:48 +02:00
|
|
|
func signal_store_kyber_pre_key_callback(storeCtx unsafe.Pointer, id C.uint32_t, preKeyRecord C.SignalMutPointerKyberPreKeyRecord) C.int {
|
2024-01-04 01:06:45 +02:00
|
|
|
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
2026-01-16 15:01:48 +02:00
|
|
|
record := KyberPreKeyRecord{ptr: preKeyRecord.raw}
|
2023-07-28 14:21:14 -04:00
|
|
|
cloned, err := record.Clone()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-01-04 01:06:45 +02:00
|
|
|
return store.StoreKyberPreKey(ctx, uint32(id), cloned)
|
2023-07-28 14:21:14 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//export signal_mark_kyber_pre_key_used_callback
|
2026-01-16 15:01:48 +02:00
|
|
|
func signal_mark_kyber_pre_key_used_callback(storeCtx unsafe.Pointer, id C.uint32_t, ecPrekeyID C.uint32_t, baseKey C.SignalMutPointerPublicKey) C.int {
|
2024-01-04 01:06:45 +02:00
|
|
|
return wrapStoreCallback(storeCtx, func(store KyberPreKeyStore, ctx context.Context) error {
|
2026-01-16 15:01:48 +02:00
|
|
|
// TODO use ecPrekeyID and baseKey?
|
|
|
|
|
return store.MarkKyberPreKeyUsed(ctx, uint32(id))
|
2023-07-28 14:21:14 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-16 15:01:48 +02:00
|
|
|
//export signal_destroy_kyber_pre_key_store_callback
|
|
|
|
|
func signal_destroy_kyber_pre_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) wrapKyberPreKeyStore(store KyberPreKeyStore) C.SignalConstPointerFfiKyberPreKeyStoreStruct {
|
|
|
|
|
return C.SignalConstPointerFfiKyberPreKeyStoreStruct{&C.SignalKyberPreKeyStore{
|
2024-01-04 01:06:45 +02:00
|
|
|
ctx: wrapStore(ctx, store),
|
2026-01-16 15:01:48 +02:00
|
|
|
load_kyber_pre_key: C.SignalFfiBridgeKyberPreKeyStoreLoadKyberPreKey(C.signal_load_kyber_pre_key_callback),
|
|
|
|
|
store_kyber_pre_key: C.SignalFfiBridgeKyberPreKeyStoreStoreKyberPreKey(C.signal_store_kyber_pre_key_callback),
|
|
|
|
|
mark_kyber_pre_key_used: C.SignalFfiBridgeKyberPreKeyStoreMarkKyberPreKeyUsed(C.signal_mark_kyber_pre_key_used_callback),
|
|
|
|
|
destroy: C.SignalFfiBridgeKyberPreKeyStoreDestroy(C.signal_destroy_kyber_pre_key_store_callback),
|
2025-01-17 17:49:37 +02:00
|
|
|
}}
|
2023-07-28 14:21:14 -04:00
|
|
|
}
|