Files
libsignal/swift/Sources/LibSignalClient/DeviceTransfer.swift
Jordan Rose f3e0f61667 bridge: Remove bridge_fn_buffer
At this point, the only special behavior of bridge_fn_buffer is to
support multiple return values for the C bridge (a pointer/length
pair), and that doesn't pull its weight. Remove it in favor of a plain
bridge_fn.

This did reveal that Username_Hash was using bridge_fn_buffer and now
produces a fixed-size array, imported into Swift as a tuple, so this
commit also factors out a new helper invokeFnReturningFixedLengthArray.
2023-02-21 16:41:03 -08:00

45 lines
1.3 KiB
Swift

//
// Copyright 2021-2022 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import SignalFfi
import Foundation
public enum KeyFormat: UInt8, CaseIterable {
// PKCS#8 is the default for backward compatibility
case pkcs8 = 0
case keySpecific = 1
}
public struct DeviceTransferKey {
public let privateKey: [UInt8]
public static func generate(formattedAs keyFormat: KeyFormat = .pkcs8) -> Self {
let privateKey = failOnError {
try invokeFnReturningArray {
signal_device_transfer_generate_private_key_with_format($0, keyFormat.rawValue)
}
}
return Self(privateKey: privateKey)
}
public func privateKeyMaterial() -> [UInt8] {
return self.privateKey
}
public func generateCertificate(_ name: String, _ daysTilExpire: Int) -> [UInt8] {
return privateKey.withUnsafeBorrowedBuffer { privateKeyBuffer in
failOnError {
try invokeFnReturningArray {
signal_device_transfer_generate_certificate($0,
privateKeyBuffer,
name,
UInt32(daysTilExpire))
}
}
}
}
}