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"
|
|
|
|
|
|
|
|
|
|
type PrivateKey struct {
|
2024-01-02 23:47:18 +02:00
|
|
|
nc noCopy
|
2023-03-22 10:31:22 -06:00
|
|
|
ptr *C.SignalPrivateKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wrapPrivateKey(ptr *C.SignalPrivateKey) *PrivateKey {
|
|
|
|
|
privateKey := &PrivateKey{ptr: ptr}
|
|
|
|
|
runtime.SetFinalizer(privateKey, (*PrivateKey).Destroy)
|
|
|
|
|
return privateKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GeneratePrivateKey() (*PrivateKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var pk C.SignalMutPointerPrivateKey
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_privatekey_generate(&pk)
|
|
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapPrivateKey(pk.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeserializePrivateKey(keyData []byte) (*PrivateKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var pk C.SignalMutPointerPrivateKey
|
2023-03-22 10:31:22 -06:00
|
|
|
signalFfiError := C.signal_privatekey_deserialize(&pk, BytesToBuffer(keyData))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(keyData)
|
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(pk.raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pk *PrivateKey) mutPtr() C.SignalMutPointerPrivateKey {
|
|
|
|
|
return C.SignalMutPointerPrivateKey{pk.ptr}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pk *PrivateKey) constPtr() C.SignalConstPointerPrivateKey {
|
|
|
|
|
return C.SignalConstPointerPrivateKey{pk.ptr}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pk *PrivateKey) Clone() (*PrivateKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var cloned C.SignalMutPointerPrivateKey
|
|
|
|
|
signalFfiError := C.signal_privatekey_clone(&cloned, pk.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(pk)
|
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(cloned.raw), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pk *PrivateKey) Destroy() error {
|
2024-01-02 23:49:44 +02:00
|
|
|
pk.CancelFinalizer()
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_privatekey_destroy(pk.mutPtr()))
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2024-01-02 23:49:44 +02:00
|
|
|
func (pk *PrivateKey) CancelFinalizer() {
|
|
|
|
|
runtime.SetFinalizer(pk, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
func (pk *PrivateKey) GetPublicKey() (*PublicKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var pub C.SignalMutPointerPublicKey
|
|
|
|
|
signalFfiError := C.signal_privatekey_get_public_key(&pub, pk.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(pk)
|
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 (pk *PrivateKey) 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_privatekey_serialize(&serialized, pk.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(pk)
|
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 (pk *PrivateKey) Sign(message []byte) ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var signed C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_privatekey_sign(&signed, pk.constPtr(), BytesToBuffer(message))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(pk)
|
|
|
|
|
runtime.KeepAlive(message)
|
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(signed), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (pk *PrivateKey) Agree(publicKey *PublicKey) ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var agreed C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_privatekey_agree(&agreed, pk.constPtr(), publicKey.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(pk)
|
|
|
|
|
runtime.KeepAlive(publicKey)
|
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(agreed), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|