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 (
|
|
|
|
|
"runtime"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SignedPreKeyRecord struct {
|
2024-01-02 23:47:18 +02:00
|
|
|
nc noCopy
|
2023-03-22 10:31:22 -06:00
|
|
|
ptr *C.SignalSignedPreKeyRecord
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wrapSignedPreKeyRecord(ptr *C.SignalSignedPreKeyRecord) *SignedPreKeyRecord {
|
|
|
|
|
spkr := &SignedPreKeyRecord{ptr: ptr}
|
|
|
|
|
runtime.SetFinalizer(spkr, (*SignedPreKeyRecord).Destroy)
|
|
|
|
|
return spkr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSignedPreKeyRecord(id uint32, timestamp time.Time, publicKey *PublicKey, privateKey *PrivateKey, signature []byte) (*SignedPreKeyRecord, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var spkr C.SignalMutPointerSignedPreKeyRecord
|
|
|
|
|
signalFfiError := C.signal_signed_pre_key_record_new(
|
|
|
|
|
&spkr,
|
|
|
|
|
C.uint32_t(id),
|
|
|
|
|
C.uint64_t(timestamp.UnixMilli()),
|
|
|
|
|
publicKey.constPtr(),
|
|
|
|
|
privateKey.constPtr(),
|
|
|
|
|
BytesToBuffer(signature),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(publicKey)
|
|
|
|
|
runtime.KeepAlive(privateKey)
|
|
|
|
|
runtime.KeepAlive(signature)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapSignedPreKeyRecord(spkr.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewSignedPreKeyRecordFromPrivateKey(id uint32, timestamp time.Time, privateKey *PrivateKey, signature []byte) (*SignedPreKeyRecord, error) {
|
|
|
|
|
pub, err := privateKey.GetPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return NewSignedPreKeyRecord(id, timestamp, pub, privateKey, signature)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeserializeSignedPreKeyRecord(serialized []byte) (*SignedPreKeyRecord, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var spkr C.SignalMutPointerSignedPreKeyRecord
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_signed_pre_key_record_deserialize(&spkr, 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 wrapSignedPreKeyRecord(spkr.raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) mutPtr() C.SignalMutPointerSignedPreKeyRecord {
|
|
|
|
|
return C.SignalMutPointerSignedPreKeyRecord{spkr.ptr}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) constPtr() C.SignalConstPointerSignedPreKeyRecord {
|
|
|
|
|
return C.SignalConstPointerSignedPreKeyRecord{spkr.ptr}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) Clone() (*SignedPreKeyRecord, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var cloned C.SignalMutPointerSignedPreKeyRecord
|
|
|
|
|
signalFfiError := C.signal_signed_pre_key_record_clone(&cloned, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapSignedPreKeyRecord(cloned.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) Destroy() error {
|
2024-01-02 23:49:44 +02:00
|
|
|
spkr.CancelFinalizer()
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_signed_pre_key_record_destroy(spkr.mutPtr()))
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 23:49:44 +02:00
|
|
|
func (spkr *SignedPreKeyRecord) CancelFinalizer() {
|
|
|
|
|
runtime.SetFinalizer(spkr, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
func (spkr *SignedPreKeyRecord) 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_signed_pre_key_record_serialize(&serialized, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
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 (spkr *SignedPreKeyRecord) GetSignature() ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var signature C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_signed_pre_key_record_get_signature(&signature, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
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(signature), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2024-03-19 19:15:30 +02:00
|
|
|
func (spkr *SignedPreKeyRecord) GetID() (uint32, error) {
|
2023-03-22 10:31:22 -06:00
|
|
|
var id C.uint32_t
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_signed_pre_key_record_get_id(&id, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return 0, wrapError(signalFfiError)
|
|
|
|
|
}
|
2024-03-19 19:15:30 +02:00
|
|
|
return uint32(id), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) GetTimestamp() (time.Time, error) {
|
|
|
|
|
var ts C.uint64_t
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_signed_pre_key_record_get_timestamp(&ts, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return time.Time{}, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return time.UnixMilli(int64(ts)), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) GetPublicKey() (*PublicKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var pub C.SignalMutPointerPublicKey
|
|
|
|
|
signalFfiError := C.signal_signed_pre_key_record_get_public_key(
|
|
|
|
|
&pub,
|
|
|
|
|
spkr.constPtr(),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapPublicKey(pub.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (spkr *SignedPreKeyRecord) GetPrivateKey() (*PrivateKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var priv C.SignalMutPointerPrivateKey
|
|
|
|
|
signalFfiError := C.signal_signed_pre_key_record_get_private_key(&priv, spkr.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(spkr)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapPrivateKey(priv.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|