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"
|
2023-12-10 17:32:33 -05:00
|
|
|
import (
|
|
|
|
|
"runtime"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
2023-03-22 10:31:22 -06:00
|
|
|
|
|
|
|
|
type SessionRecord struct {
|
2024-01-02 23:47:18 +02:00
|
|
|
nc noCopy
|
2023-03-22 10:31:22 -06:00
|
|
|
ptr *C.SignalSessionRecord
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wrapSessionRecord(ptr *C.SignalSessionRecord) *SessionRecord {
|
|
|
|
|
sessionRecord := &SessionRecord{ptr: ptr}
|
|
|
|
|
runtime.SetFinalizer(sessionRecord, (*SessionRecord).Destroy)
|
|
|
|
|
return sessionRecord
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeserializeSessionRecord(serialized []byte) (*SessionRecord, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var ptr C.SignalMutPointerSessionRecord
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_session_record_deserialize(&ptr, 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 wrapSessionRecord(ptr.raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) mutPtr() C.SignalMutPointerSessionRecord {
|
|
|
|
|
return C.SignalMutPointerSessionRecord{sr.ptr}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) constPtr() C.SignalConstPointerSessionRecord {
|
|
|
|
|
return C.SignalConstPointerSessionRecord{sr.ptr}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) Clone() (*SessionRecord, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var clone C.SignalMutPointerSessionRecord
|
|
|
|
|
signalFfiError := C.signal_session_record_clone(
|
|
|
|
|
&clone,
|
|
|
|
|
sr.constPtr(),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapSessionRecord(clone.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) Destroy() error {
|
2024-01-02 23:49:44 +02:00
|
|
|
sr.CancelFinalizer()
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_session_record_destroy(sr.mutPtr()))
|
2024-01-02 23:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) CancelFinalizer() {
|
|
|
|
|
runtime.SetFinalizer(sr, nil)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) ArchiveCurrentState() error {
|
2024-01-03 20:31:27 +02:00
|
|
|
defer runtime.KeepAlive(sr)
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_session_record_archive_current_state(sr.mutPtr()))
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) CurrentRatchetKeyMatches(key *PublicKey) (bool, error) {
|
2025-12-08 23:11:45 +02:00
|
|
|
if sr == nil || key == nil {
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2023-03-22 10:31:22 -06:00
|
|
|
var result C.bool
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_session_record_current_ratchet_key_matches(
|
|
|
|
|
&result,
|
|
|
|
|
sr.constPtr(),
|
|
|
|
|
key.constPtr(),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
|
|
|
|
runtime.KeepAlive(key)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return false, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return bool(result), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) HasCurrentState() (bool, error) {
|
|
|
|
|
var result C.bool
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_session_record_has_usable_sender_chain(
|
|
|
|
|
&result,
|
|
|
|
|
sr.constPtr(),
|
|
|
|
|
C.uint64_t(time.Now().Unix()),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return false, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return bool(result), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) 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_session_record_serialize(&serialized, sr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) GetLocalRegistrationID() (uint32, error) {
|
|
|
|
|
var result C.uint32_t
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_session_record_get_local_registration_id(&result, sr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return 0, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return uint32(result), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (sr *SessionRecord) GetRemoteRegistrationID() (uint32, error) {
|
|
|
|
|
var result C.uint32_t
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_session_record_get_remote_registration_id(&result, sr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(sr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return 0, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return uint32(result), nil
|
|
|
|
|
}
|