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
|
|
|
*/
|
|
|
|
|
import "C"
|
|
|
|
|
import (
|
2024-01-04 01:06:45 +02:00
|
|
|
"context"
|
2023-03-22 10:31:22 -06:00
|
|
|
"runtime"
|
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
2024-01-04 01:06:45 +02:00
|
|
|
func ProcessSenderKeyDistributionMessage(ctx context.Context, message *SenderKeyDistributionMessage, fromSender *Address, store SenderKeyStore) error {
|
|
|
|
|
callbackCtx := NewCallbackContext(ctx)
|
|
|
|
|
defer callbackCtx.Unref()
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_process_sender_key_distribution_message(
|
2025-01-17 17:49:37 +02:00
|
|
|
fromSender.constPtr(),
|
|
|
|
|
message.constPtr(),
|
2024-01-04 01:06:45 +02:00
|
|
|
callbackCtx.wrapSenderKeyStore(store),
|
2023-03-22 10:31:22 -06:00
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(message)
|
|
|
|
|
runtime.KeepAlive(fromSender)
|
2024-01-04 01:06:45 +02:00
|
|
|
return callbackCtx.wrapError(signalFfiError)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SenderKeyDistributionMessage struct {
|
2024-01-02 23:47:18 +02:00
|
|
|
nc noCopy
|
2023-03-22 10:31:22 -06:00
|
|
|
ptr *C.SignalSenderKeyDistributionMessage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wrapSenderKeyDistributionMessage(ptr *C.SignalSenderKeyDistributionMessage) *SenderKeyDistributionMessage {
|
|
|
|
|
sc := &SenderKeyDistributionMessage{ptr: ptr}
|
|
|
|
|
runtime.SetFinalizer(sc, (*SenderKeyDistributionMessage).Destroy)
|
|
|
|
|
return sc
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 01:06:45 +02:00
|
|
|
func NewSenderKeyDistributionMessage(ctx context.Context, sender *Address, distributionID uuid.UUID, store SenderKeyStore) (*SenderKeyDistributionMessage, error) {
|
|
|
|
|
callbackCtx := NewCallbackContext(ctx)
|
|
|
|
|
defer callbackCtx.Unref()
|
2025-01-17 17:49:37 +02:00
|
|
|
var skdm C.SignalMutPointerSenderKeyDistributionMessage
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_sender_key_distribution_message_create(
|
|
|
|
|
&skdm,
|
2025-01-17 17:49:37 +02:00
|
|
|
sender.constPtr(),
|
2026-01-16 15:01:48 +02:00
|
|
|
*(*C.SignalUuid)(unsafe.Pointer(&distributionID)),
|
2024-01-04 01:06:45 +02:00
|
|
|
callbackCtx.wrapSenderKeyStore(store),
|
2023-12-10 17:32:33 -05:00
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sender)
|
|
|
|
|
runtime.KeepAlive(distributionID)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
2024-01-04 01:06:45 +02:00
|
|
|
return nil, callbackCtx.wrapError(signalFfiError)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapSenderKeyDistributionMessage(skdm.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeserializeSenderKeyDistributionMessage(serialized []byte) (*SenderKeyDistributionMessage, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var skdm C.SignalMutPointerSenderKeyDistributionMessage
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_sender_key_distribution_message_deserialize(&skdm, BytesToBuffer(serialized))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(serialized)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapSenderKeyDistributionMessage(skdm.raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sc *SenderKeyDistributionMessage) mutPtr() C.SignalMutPointerSenderKeyDistributionMessage {
|
|
|
|
|
return C.SignalMutPointerSenderKeyDistributionMessage{sc.ptr}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sc *SenderKeyDistributionMessage) constPtr() C.SignalConstPointerSenderKeyDistributionMessage {
|
|
|
|
|
return C.SignalConstPointerSenderKeyDistributionMessage{sc.ptr}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sc *SenderKeyDistributionMessage) Destroy() error {
|
2024-01-02 23:49:44 +02:00
|
|
|
sc.CancelFinalizer()
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_sender_key_distribution_message_destroy(sc.mutPtr()))
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 23:49:44 +02:00
|
|
|
func (sc *SenderKeyDistributionMessage) CancelFinalizer() {
|
|
|
|
|
runtime.SetFinalizer(sc, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
func (sc *SenderKeyDistributionMessage) Serialize() ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var serialized C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_sender_key_distribution_message_serialize(&serialized, sc.constPtr())
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2023-06-20 15:55:54 -04:00
|
|
|
return CopySignalOwnedBufferToBytes(serialized), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-04 01:06:45 +02:00
|
|
|
func (sc *SenderKeyDistributionMessage) Process(ctx context.Context, sender *Address, store SenderKeyStore) error {
|
|
|
|
|
callbackCtx := NewCallbackContext(ctx)
|
|
|
|
|
defer callbackCtx.Unref()
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_process_sender_key_distribution_message(
|
2025-01-17 17:49:37 +02:00
|
|
|
sender.constPtr(),
|
|
|
|
|
sc.constPtr(),
|
2024-01-04 01:06:45 +02:00
|
|
|
callbackCtx.wrapSenderKeyStore(store),
|
2023-12-10 17:32:33 -05:00
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sender)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
2024-01-04 01:06:45 +02:00
|
|
|
return callbackCtx.wrapError(signalFfiError)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|