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"
|
2024-01-03 20:31:27 +02:00
|
|
|
import (
|
|
|
|
|
"runtime"
|
|
|
|
|
)
|
2023-03-22 10:31:22 -06:00
|
|
|
|
|
|
|
|
type IdentityKey struct {
|
|
|
|
|
publicKey *PublicKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewIdentityKeyFromPublicKey(publicKey *PublicKey) (*IdentityKey, error) {
|
|
|
|
|
return &IdentityKey{publicKey: publicKey}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewIdentityKeyFromBytes(bytes []byte) (*IdentityKey, error) {
|
|
|
|
|
publicKey, err := DeserializePublicKey(bytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &IdentityKey{publicKey: publicKey}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 17:36:18 +02:00
|
|
|
func (i *IdentityKey) TrySerialize() []byte {
|
|
|
|
|
if i == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
serialized, err := i.Serialize()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return serialized
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
func (i *IdentityKey) Serialize() ([]byte, error) {
|
|
|
|
|
return i.publicKey.Serialize()
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 16:39:09 -04:00
|
|
|
func DeserializeIdentityKey(bytes []byte) (*IdentityKey, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var publicKey C.SignalMutPointerPublicKey
|
2023-05-31 16:39:09 -04:00
|
|
|
signalFfiError := C.signal_publickey_deserialize(&publicKey, BytesToBuffer(bytes))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(bytes)
|
2023-05-31 16:39:09 -04:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return &IdentityKey{publicKey: wrapPublicKey(publicKey.raw)}, nil
|
2023-05-31 16:39:09 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-22 10:31:22 -06:00
|
|
|
func (i *IdentityKey) VerifyAlternateIdentity(other *IdentityKey, signature []byte) (bool, error) {
|
|
|
|
|
var verify C.bool
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_identitykey_verify_alternate_identity(
|
|
|
|
|
&verify,
|
|
|
|
|
i.publicKey.constPtr(),
|
|
|
|
|
other.publicKey.constPtr(),
|
|
|
|
|
BytesToBuffer(signature),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(i)
|
|
|
|
|
runtime.KeepAlive(other)
|
|
|
|
|
runtime.KeepAlive(signature)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return false, wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
return bool(verify), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *IdentityKey) Equal(other *IdentityKey) (bool, error) {
|
2026-02-09 13:54:02 +02:00
|
|
|
return i.publicKey.Equal(other.publicKey)
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type IdentityKeyPair struct {
|
|
|
|
|
publicKey *PublicKey
|
|
|
|
|
privateKey *PrivateKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *IdentityKeyPair) GetPublicKey() *PublicKey {
|
|
|
|
|
return i.publicKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *IdentityKeyPair) GetPrivateKey() *PrivateKey {
|
|
|
|
|
return i.privateKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GenerateIdentityKeyPair() (*IdentityKeyPair, error) {
|
|
|
|
|
privateKey, err := GeneratePrivateKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
publicKey, err := privateKey.GetPublicKey()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &IdentityKeyPair{publicKey: publicKey, privateKey: privateKey}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeserializeIdentityKeyPair(bytes []byte) (*IdentityKeyPair, error) {
|
2025-10-10 18:41:37 +03:00
|
|
|
var keys C.SignalPairOfMutPointerPublicKeyMutPointerPrivateKey
|
|
|
|
|
signalFfiError := C.signal_identitykeypair_deserialize(&keys, BytesToBuffer(bytes))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(bytes)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-10-10 18:41:37 +03:00
|
|
|
return &IdentityKeyPair{publicKey: wrapPublicKey(keys.first.raw), privateKey: wrapPrivateKey(keys.second.raw)}, nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewIdentityKeyPair(publicKey *PublicKey, privateKey *PrivateKey) (*IdentityKeyPair, error) {
|
|
|
|
|
return &IdentityKeyPair{publicKey: publicKey, privateKey: privateKey}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *IdentityKeyPair) 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_identitykeypair_serialize(
|
|
|
|
|
&serialized,
|
|
|
|
|
i.publicKey.constPtr(),
|
|
|
|
|
i.privateKey.constPtr(),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(i)
|
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 (i *IdentityKeyPair) GetIdentityKey() *IdentityKey {
|
|
|
|
|
return &IdentityKey{publicKey: i.publicKey}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *IdentityKeyPair) SignAlternateIdentity(other *IdentityKey) ([]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_identitykeypair_sign_alternate_identity(
|
|
|
|
|
&signature,
|
|
|
|
|
i.publicKey.constPtr(),
|
|
|
|
|
i.privateKey.constPtr(),
|
|
|
|
|
other.publicKey.constPtr(),
|
|
|
|
|
)
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(i)
|
|
|
|
|
runtime.KeepAlive(other)
|
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
|
|
|
}
|