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 HSMEnclaveClient struct {
|
2024-01-02 23:47:18 +02:00
|
|
|
nc noCopy
|
2023-03-22 10:31:22 -06:00
|
|
|
ptr *C.SignalHsmEnclaveClient
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wrapHSMEnclaveClient(ptr *C.SignalHsmEnclaveClient) *HSMEnclaveClient {
|
|
|
|
|
hsmEnclaveClient := &HSMEnclaveClient{ptr: ptr}
|
|
|
|
|
runtime.SetFinalizer(hsmEnclaveClient, (*HSMEnclaveClient).Destroy)
|
|
|
|
|
return hsmEnclaveClient
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewHSMEnclaveClient(trustedPublicKey, trustedCodeHashes []byte) (*HSMEnclaveClient, error) {
|
2025-01-17 17:49:37 +02:00
|
|
|
var cds C.SignalMutPointerHsmEnclaveClient
|
|
|
|
|
signalFfiError := C.signal_hsm_enclave_client_new(
|
|
|
|
|
&cds,
|
|
|
|
|
BytesToBuffer(trustedPublicKey),
|
|
|
|
|
BytesToBuffer(trustedCodeHashes),
|
|
|
|
|
)
|
2023-03-22 10:31:22 -06:00
|
|
|
if signalFfiError != nil {
|
|
|
|
|
return nil, wrapError(signalFfiError)
|
|
|
|
|
}
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapHSMEnclaveClient(cds.raw), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) mutPtr() C.SignalMutPointerHsmEnclaveClient {
|
|
|
|
|
return C.SignalMutPointerHsmEnclaveClient{hsm.ptr}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) constPtr() C.SignalConstPointerHsmEnclaveClient {
|
|
|
|
|
return C.SignalConstPointerHsmEnclaveClient{hsm.ptr}
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) Destroy() error {
|
|
|
|
|
runtime.SetFinalizer(hsm, nil)
|
2025-01-17 17:49:37 +02:00
|
|
|
return wrapError(C.signal_hsm_enclave_client_destroy(hsm.mutPtr()))
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) InitialRequest() ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var resp C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_hsm_enclave_client_initial_request(&resp, hsm.constPtr())
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(hsm)
|
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(resp), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) CompleteHandshake(handshakeReceived []byte) error {
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_hsm_enclave_client_complete_handshake(hsm.mutPtr(), BytesToBuffer(handshakeReceived))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(hsm)
|
|
|
|
|
runtime.KeepAlive(handshakeReceived)
|
2023-03-22 10:31:22 -06:00
|
|
|
return wrapError(signalFfiError)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (hsm *HSMEnclaveClient) EstablishedSend(plaintext []byte) ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var resp C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_hsm_enclave_client_established_send(&resp, hsm.mutPtr(), BytesToBuffer(plaintext))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(hsm)
|
|
|
|
|
runtime.KeepAlive(plaintext)
|
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(resp), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|
|
|
|
|
|
2023-12-22 14:56:31 +02:00
|
|
|
func (hsm *HSMEnclaveClient) EstablishedReceive(ciphertext []byte) ([]byte, error) {
|
2023-06-20 15:55:54 -04:00
|
|
|
var resp C.SignalOwnedBuffer = C.SignalOwnedBuffer{}
|
2025-01-17 17:49:37 +02:00
|
|
|
signalFfiError := C.signal_hsm_enclave_client_established_recv(&resp, hsm.mutPtr(), BytesToBuffer(ciphertext))
|
2024-01-03 20:31:27 +02:00
|
|
|
runtime.KeepAlive(hsm)
|
|
|
|
|
runtime.KeepAlive(ciphertext)
|
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(resp), nil
|
2023-03-22 10:31:22 -06:00
|
|
|
}
|