forked from Blendman974/faceclaw
[verified] merge upstream 25cb255b into all-features
This commit is contained in:
commit
ffcdae660d
169 files changed
+16201
-597
No files matched your search
@@ -292,6 +292,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
|
||||
/** See [GlassesSessionCore.getRingStatus]. */
|
||||
fun getRingStatus(): String = core.getRingStatus()
|
||||
|
||||
/** See [GlassesSessionCore.setHeadUpTrigger]. */
|
||||
fun setHeadUpTrigger(degrees: Int, originalSwitch: Int, originalAngle: Int) =
|
||||
core.setHeadUpTrigger(degrees, originalSwitch, originalAngle)
|
||||
|
||||
/** See [GlassesSessionCore.getHeadUpStatus]. */
|
||||
fun getHeadUpStatus(): String = core.getHeadUpStatus()
|
||||
|
||||
/** See [GlassesSessionCore.setGlassesRingLink]. */
|
||||
fun setGlassesRingLink(action: String, fallbackAddress: String?, fallbackName: String?): Boolean =
|
||||
core.setGlassesRingLink(action, fallbackAddress, fallbackName)
|
||||
@@ -324,6 +331,13 @@ class FaceclawBleCommunicator(context: Context, rightAddress: String?, leftAddre
|
||||
fun sendFaceclawMicControl(record: ByteArray?, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
|
||||
core.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
|
||||
|
||||
fun addKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.addKeyboardEventListener(listener)
|
||||
|
||||
fun removeKeyboardEventListener(listener: FaceclawKeyboardEventListener?) = core.removeKeyboardEventListener(listener)
|
||||
|
||||
fun sendFaceclawKeyboardControl(record: ByteArray?, label: String): Boolean =
|
||||
core.sendFaceclawKeyboardControl(record, label)
|
||||
|
||||
fun startG2AudioForwarding(listener: FaceclawAudioPacketListener?): Boolean = core.startG2AudioForwarding(listener)
|
||||
|
||||
fun stopG2AudioForwarding() = core.stopG2AudioForwarding()
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.security.keystore.KeyGenParameterSpec
|
||||
import android.security.keystore.KeyProperties
|
||||
import android.security.keystore.StrongBoxUnavailableException
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import com.faceclaw.ssh.faceclawssh.Channel
|
||||
import com.faceclaw.ssh.faceclawssh.Faceclawssh
|
||||
import com.faceclaw.ssh.faceclawssh.Listener
|
||||
import com.faceclaw.ssh.faceclawssh.Signer
|
||||
import java.security.KeyPairGenerator
|
||||
import java.security.KeyStore
|
||||
import java.security.PrivateKey
|
||||
import java.security.Signature
|
||||
import java.security.spec.ECGenParameterSpec
|
||||
import javax.crypto.Cipher
|
||||
import javax.crypto.KeyGenerator
|
||||
import javax.crypto.SecretKey
|
||||
import javax.crypto.spec.GCMParameterSpec
|
||||
|
||||
/**
|
||||
* One SSH channel for the TypeScript side (app/native/ssh.ts), backed by the
|
||||
* gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec JSON.
|
||||
* Events arrive from Go goroutines and are posted to the Looper of the
|
||||
* thread that constructed this object (a JS isolate's own thread), like
|
||||
* FaceclawWebSocket.
|
||||
*/
|
||||
class FaceclawSshChannel(spec: String, useKey: Boolean, listener: FaceclawSshListener) {
|
||||
private val handler = Handler(Looper.myLooper() ?: Looper.getMainLooper())
|
||||
private val channel: Channel
|
||||
|
||||
init {
|
||||
val signer = if (useKey) FaceclawSshKeys.signer() else null
|
||||
channel = Faceclawssh.open(spec, signer, object : Listener {
|
||||
override fun onEvent(kind: String?, payload: String?) {
|
||||
handler.post {
|
||||
try {
|
||||
listener.onEvent(kind, payload)
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "listener onEvent($kind) failed", t)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun send(text: String): Boolean = channel.send(text)
|
||||
|
||||
fun close() = channel.close()
|
||||
|
||||
private companion object {
|
||||
const val TAG = "FaceclawSsh"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The device's SSH client key and the Terminal app's saved SSH passwords.
|
||||
*
|
||||
* The client key is an ECDSA P-256 key generated inside Android Keystore
|
||||
* (StrongBox when the device has one) and never exportable: only its public
|
||||
* half leaves the keystore, as an authorized_keys line. Passwords are
|
||||
* encrypted with an AES-GCM key that also lives in Keystore, and stored in a
|
||||
* private SharedPreferences file kept apart from the settings store (which is
|
||||
* exported by the settings port).
|
||||
*/
|
||||
object FaceclawSshKeys {
|
||||
private const val TAG = "FaceclawSsh"
|
||||
private const val CLIENT_KEY_ALIAS = "faceclaw-ssh-client"
|
||||
private const val SECRET_KEY_ALIAS = "faceclaw-ssh-secrets"
|
||||
private const val SECRETS_PREFS = "faceclaw_ssh_secrets"
|
||||
private const val GCM_TAG_BITS = 128
|
||||
|
||||
private fun keyStore(): KeyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
|
||||
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun hasClientKey(): Boolean = keyStore().containsAlias(CLIENT_KEY_ALIAS)
|
||||
|
||||
/** Generate the client key if it doesn't exist yet. */
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun ensureClientKey() {
|
||||
if (keyStore().containsAlias(CLIENT_KEY_ALIAS)) return
|
||||
fun spec(strongBox: Boolean): KeyGenParameterSpec {
|
||||
val builder = KeyGenParameterSpec.Builder(CLIENT_KEY_ALIAS, KeyProperties.PURPOSE_SIGN)
|
||||
.setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
|
||||
.setDigests(KeyProperties.DIGEST_SHA256)
|
||||
if (strongBox && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) builder.setIsStrongBoxBacked(true)
|
||||
return builder.build()
|
||||
}
|
||||
val generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore")
|
||||
try {
|
||||
generator.initialize(spec(strongBox = true))
|
||||
generator.generateKeyPair()
|
||||
} catch (e: Exception) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && e is StrongBoxUnavailableException) {
|
||||
Log.i(TAG, "StrongBox unavailable; generating the SSH key in the TEE")
|
||||
} else {
|
||||
Log.w(TAG, "StrongBox key generation failed; retrying in the TEE", e)
|
||||
}
|
||||
generator.initialize(spec(strongBox = false))
|
||||
generator.generateKeyPair()
|
||||
}
|
||||
Log.i(TAG, "generated SSH client key")
|
||||
}
|
||||
|
||||
/** X.509 SubjectPublicKeyInfo DER of the client key, or null if none. */
|
||||
@JvmStatic
|
||||
fun publicKey(): ByteArray? = keyStore().getCertificate(CLIENT_KEY_ALIAS)?.publicKey?.encoded
|
||||
|
||||
/** The client key's authorized_keys line (generating the key if needed). */
|
||||
@JvmStatic
|
||||
fun authorizedKey(comment: String): String {
|
||||
ensureClientKey()
|
||||
return Faceclawssh.authorizedKey(publicKey(), comment)
|
||||
}
|
||||
|
||||
/** The client key's SHA256 fingerprint (generating the key if needed). */
|
||||
@JvmStatic
|
||||
fun fingerprint(): String {
|
||||
ensureClientKey()
|
||||
return Faceclawssh.fingerprint(publicKey())
|
||||
}
|
||||
|
||||
/** A Go Signer over the client key, generating the key if needed. */
|
||||
@JvmStatic
|
||||
fun signer(): Signer? = try {
|
||||
ensureClientKey()
|
||||
val publicKey = publicKey() ?: throw IllegalStateException("client key has no certificate")
|
||||
object : Signer {
|
||||
override fun publicKey(): ByteArray = publicKey
|
||||
override fun sign(data: ByteArray?): ByteArray {
|
||||
val privateKey = keyStore().getKey(CLIENT_KEY_ALIAS, null) as PrivateKey
|
||||
return Signature.getInstance("SHA256withECDSA").run {
|
||||
initSign(privateKey)
|
||||
update(data ?: ByteArray(0))
|
||||
sign()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "SSH client key unavailable", t)
|
||||
null
|
||||
}
|
||||
|
||||
private fun secretKey(): SecretKey {
|
||||
val store = keyStore()
|
||||
(store.getKey(SECRET_KEY_ALIAS, null) as? SecretKey)?.let { return it }
|
||||
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
|
||||
generator.init(
|
||||
KeyGenParameterSpec.Builder(SECRET_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
|
||||
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
|
||||
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
|
||||
.setKeySize(256)
|
||||
.build()
|
||||
)
|
||||
return generator.generateKey()
|
||||
}
|
||||
|
||||
private fun prefs(context: Context) =
|
||||
context.applicationContext.getSharedPreferences(SECRETS_PREFS, Context.MODE_PRIVATE)
|
||||
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun setSecret(context: Context, id: String, value: String) {
|
||||
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey())
|
||||
val sealed = cipher.iv + cipher.doFinal(value.toByteArray(Charsets.UTF_8))
|
||||
prefs(context).edit().putString(id, Base64.encodeToString(sealed, Base64.NO_WRAP)).apply()
|
||||
}
|
||||
|
||||
/** The saved secret, or "" if none (or it can no longer be decrypted). */
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun getSecret(context: Context, id: String): String {
|
||||
val encoded = prefs(context).getString(id, null) ?: return ""
|
||||
return try {
|
||||
val sealed = Base64.decode(encoded, Base64.NO_WRAP)
|
||||
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
|
||||
val ivLength = 12
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey(), GCMParameterSpec(GCM_TAG_BITS, sealed, 0, ivLength))
|
||||
String(cipher.doFinal(sealed, ivLength, sealed.size - ivLength), Charsets.UTF_8)
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "saved SSH secret unreadable; dropping it", t)
|
||||
prefs(context).edit().remove(id).apply()
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun removeSecret(context: Context, id: String) {
|
||||
prefs(context).edit().remove(id).apply()
|
||||
}
|
||||
|
||||
/** Put text on the phone's clipboard (from any thread). */
|
||||
@JvmStatic
|
||||
fun copyToClipboard(context: Context, label: String, text: String) {
|
||||
val appContext = context.applicationContext
|
||||
Handler(Looper.getMainLooper()).post {
|
||||
val clipboard = appContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.setPrimaryClip(ClipData.newPlainText(label, text))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,21 @@ class FaceclawVoiceController(context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
override fun micArrayLink(): MicArrayLink? {
|
||||
val currentCommunicator = communicator ?: return null
|
||||
return object : MicArrayLink {
|
||||
override fun startForwarding(listener: FaceclawAudioPacketListener): Boolean =
|
||||
currentCommunicator.startG2AudioForwarding(listener)
|
||||
|
||||
override fun stopForwarding() = currentCommunicator.stopG2AudioForwarding()
|
||||
|
||||
override fun isForwarding(): Boolean = currentCommunicator.isAudioCaptureActive()
|
||||
|
||||
override fun sendControl(record: ByteArray, label: String, rightTemple: Boolean, leftTemple: Boolean): Boolean =
|
||||
currentCommunicator.sendFaceclawMicControl(record, label, rightTemple, leftTemple)
|
||||
}
|
||||
}
|
||||
|
||||
override fun createPacketDecoder(): AudioPacketDecoder =
|
||||
if (communicator?.isRayNeoIo() == true) AndroidSpeechEngines.OpusDecoderAdapter()
|
||||
else AndroidSpeechEngines.Lc3DecoderAdapter()
|
||||
@@ -131,6 +146,11 @@ class FaceclawVoiceController(context: Context) {
|
||||
|
||||
private val session = VoiceCaptureSession(host)
|
||||
|
||||
// The recording of this controller's live captures (see startRecording). It outlives any one
|
||||
// capture, and is only handed to the session while recording is enabled.
|
||||
private var recording: MicRecordingWriter? = null
|
||||
private var recordingEnabled = true
|
||||
|
||||
fun setListener(listener: FaceclawVoiceControllerListener?) {
|
||||
session.setListener(listener)
|
||||
}
|
||||
@@ -203,6 +223,88 @@ class FaceclawVoiceController(context: Context) {
|
||||
session.setBeamFilter(enabled, centerDeg, halfWidthDeg)
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture glasses sessions from the temples' own microphones (the custom firmware's
|
||||
* mic_control stream) instead of the stock mixed stream. [configureRecord] is the CONFIGURE
|
||||
* record to arm both temples with; null goes back to the mixed stream. Must be set before
|
||||
* {@link #start}.
|
||||
*/
|
||||
fun setMicArray(configureRecord: ByteArray?) {
|
||||
session.setMicArray(configureRecord)
|
||||
}
|
||||
|
||||
/**
|
||||
* Have the next {@link #start} play the recording at [path] (a .mka file) from [startMs] in
|
||||
* place of any microphone; null goes back to the microphones. Progress arrives through the
|
||||
* listener's onReplay.
|
||||
*/
|
||||
fun setReplay(path: String?, startMs: Long) {
|
||||
session.setReplay(path, startMs)
|
||||
}
|
||||
|
||||
/** Continue the running replay from [positionMs] into its recording. */
|
||||
fun seekReplay(positionMs: Long) {
|
||||
session.seekReplay(positionMs)
|
||||
}
|
||||
|
||||
fun setReplayPaused(paused: Boolean) {
|
||||
session.setReplayPaused(paused)
|
||||
}
|
||||
|
||||
/**
|
||||
* Record live captures to a new Matroska audio file at [path], from now until
|
||||
* {@link #stopRecording}, across however many captures run in between: every source's audio
|
||||
* as it arrives, before any processing. [configJson] describes the capture and is stored in
|
||||
* the file. Returns false when the file cannot be created.
|
||||
*/
|
||||
@Synchronized
|
||||
fun startRecording(path: String, configJson: String?): Boolean {
|
||||
stopRecording()
|
||||
return try {
|
||||
val parent = File(path).parentFile
|
||||
if (parent != null && !parent.exists()) parent.mkdirs()
|
||||
recording = MicRecordingWriter(openFileOutput(path), configJson, System.currentTimeMillis())
|
||||
session.setRecorder(if (recordingEnabled) recording else null)
|
||||
true
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "could not create recording $path", t)
|
||||
recording = null
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether captures are written to the recording right now: off while the capture running
|
||||
* isn't one that is to be recorded.
|
||||
*/
|
||||
@Synchronized
|
||||
fun setRecordingEnabled(enabled: Boolean) {
|
||||
recordingEnabled = enabled
|
||||
session.setRecorder(if (enabled) recording else null)
|
||||
}
|
||||
|
||||
/** Whether a recording is open and still being written (false once writing to it failed). */
|
||||
@Synchronized
|
||||
fun isRecording(): Boolean = recording?.failed == false
|
||||
|
||||
/**
|
||||
* Note a change in what is being captured (a JSON object) at the recording's current
|
||||
* position; the audio after it is placed afresh on the recording's timeline.
|
||||
*/
|
||||
@Synchronized
|
||||
fun noteRecordingEvent(json: String) {
|
||||
recording?.event(json)
|
||||
}
|
||||
|
||||
/** Complete the recording's file. Returns its length in ms, or -1 when none was open. */
|
||||
@Synchronized
|
||||
fun stopRecording(): Long {
|
||||
val current = recording ?: return -1
|
||||
recording = null
|
||||
session.setRecorder(null)
|
||||
return current.finish()
|
||||
}
|
||||
|
||||
fun start(requestedMode: String?) {
|
||||
session.start(requestedMode, 0)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* One SSH channel for the TypeScript side (app/native/ssh.ios.ts), backed by
|
||||
* the gomobile-bound Go transport in native/ssh. `spec` is the Go openSpec
|
||||
* JSON. Events arrive on Go threads and queue here; the owning JS isolate
|
||||
* drains them with takeEvents (a JSON array of {kind, payload}).
|
||||
*/
|
||||
@interface FaceclawSshChannel : NSObject
|
||||
- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey;
|
||||
- (NSString *)takeEvents;
|
||||
- (BOOL)send:(NSString *)text;
|
||||
- (void)close;
|
||||
@end
|
||||
|
||||
/**
|
||||
* The device's SSH client key (a non-exportable Secure Enclave P-256 key; a
|
||||
* plain keychain key on the simulator, which has no Secure Enclave) and the
|
||||
* Terminal app's saved SSH passwords (keychain generic passwords).
|
||||
*/
|
||||
@interface FaceclawSshKeys : NSObject
|
||||
/** "ecdsa-sha2-nistp256 AAAA... comment"; generates the key if needed. nil on failure. */
|
||||
+ (NSString *)authorizedKeyWithComment:(NSString *)comment;
|
||||
/** "SHA256:..." fingerprint; generates the key if needed. nil on failure. */
|
||||
+ (NSString *)fingerprint;
|
||||
+ (void)setSecret:(NSString *)value forId:(NSString *)secretId;
|
||||
/** The saved secret, or "" if none. */
|
||||
+ (NSString *)secretForId:(NSString *)secretId;
|
||||
+ (void)removeSecretForId:(NSString *)secretId;
|
||||
+ (void)copyToClipboard:(NSString *)text;
|
||||
@end
|
||||
@@ -0,0 +1,234 @@
|
||||
#import "FaceclawSsh.h"
|
||||
#import <Faceclawssh/Faceclawssh.h>
|
||||
#import <Security/Security.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
static NSString *const kClientKeyTag = @"com.faceclaw.ssh.client";
|
||||
static NSString *const kSecretService = @"com.faceclaw.ssh.secret";
|
||||
|
||||
#pragma mark - Client key
|
||||
|
||||
static NSData *ClientKeyTagData(void) {
|
||||
return [kClientKeyTag dataUsingEncoding:NSUTF8StringEncoding];
|
||||
}
|
||||
|
||||
static SecKeyRef CopyClientKey(void) {
|
||||
NSDictionary *query = @{
|
||||
(id)kSecClass: (id)kSecClassKey,
|
||||
(id)kSecAttrApplicationTag: ClientKeyTagData(),
|
||||
(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
|
||||
(id)kSecReturnRef: @YES,
|
||||
};
|
||||
CFTypeRef key = NULL;
|
||||
if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &key) != errSecSuccess) return NULL;
|
||||
return (SecKeyRef)key;
|
||||
}
|
||||
|
||||
static SecKeyRef CreateClientKey(BOOL secureEnclave) {
|
||||
SecAccessControlRef access = SecAccessControlCreateWithFlags(
|
||||
kCFAllocatorDefault, kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
|
||||
secureEnclave ? kSecAccessControlPrivateKeyUsage : 0, NULL);
|
||||
if (!access) return NULL;
|
||||
NSMutableDictionary *attributes = [@{
|
||||
(id)kSecAttrKeyType: (id)kSecAttrKeyTypeECSECPrimeRandom,
|
||||
(id)kSecAttrKeySizeInBits: @256,
|
||||
(id)kSecPrivateKeyAttrs: @{
|
||||
(id)kSecAttrIsPermanent: @YES,
|
||||
(id)kSecAttrApplicationTag: ClientKeyTagData(),
|
||||
(id)kSecAttrAccessControl: (__bridge id)access,
|
||||
},
|
||||
} mutableCopy];
|
||||
if (secureEnclave) attributes[(id)kSecAttrTokenID] = (id)kSecAttrTokenIDSecureEnclave;
|
||||
CFErrorRef error = NULL;
|
||||
SecKeyRef key = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, &error);
|
||||
CFRelease(access);
|
||||
if (!key && error) {
|
||||
NSLog(@"FaceclawSsh: key generation failed (secure enclave %d): %@", secureEnclave, (__bridge NSError *)error);
|
||||
CFRelease(error);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/** The client key, generating it on first use. Caller releases. */
|
||||
static SecKeyRef CopyOrCreateClientKey(void) {
|
||||
@synchronized([FaceclawSshKeys class]) {
|
||||
SecKeyRef key = CopyClientKey();
|
||||
if (key) return key;
|
||||
key = CreateClientKey(YES);
|
||||
#if TARGET_OS_SIMULATOR
|
||||
if (!key) key = CreateClientKey(NO);
|
||||
#endif
|
||||
return key;
|
||||
}
|
||||
}
|
||||
|
||||
/** Uncompressed X9.62 public point (0x04 || X || Y). */
|
||||
static NSData *PublicKeyData(SecKeyRef key) {
|
||||
SecKeyRef publicKey = SecKeyCopyPublicKey(key);
|
||||
if (!publicKey) return nil;
|
||||
NSData *data = (__bridge_transfer NSData *)SecKeyCopyExternalRepresentation(publicKey, NULL);
|
||||
CFRelease(publicKey);
|
||||
return data;
|
||||
}
|
||||
|
||||
@interface FaceclawSshSigner : NSObject <FaceclawsshSigner>
|
||||
@end
|
||||
|
||||
@implementation FaceclawSshSigner {
|
||||
SecKeyRef _key;
|
||||
NSData *_publicKey;
|
||||
}
|
||||
- (instancetype)initWithKey:(SecKeyRef)key {
|
||||
if ((self = [super init])) {
|
||||
_key = (SecKeyRef)CFRetain(key);
|
||||
_publicKey = PublicKeyData(key);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
- (void)dealloc {
|
||||
if (_key) CFRelease(_key);
|
||||
}
|
||||
- (NSData *)publicKey {
|
||||
return _publicKey;
|
||||
}
|
||||
- (NSData *)sign:(NSData *)data error:(NSError **)error {
|
||||
CFErrorRef cfError = NULL;
|
||||
NSData *signature = (__bridge_transfer NSData *)SecKeyCreateSignature(
|
||||
_key, kSecKeyAlgorithmECDSASignatureMessageX962SHA256, (__bridge CFDataRef)(data ?: [NSData data]), &cfError);
|
||||
if (!signature) {
|
||||
if (error) *error = (__bridge_transfer NSError *)cfError;
|
||||
else if (cfError) CFRelease(cfError);
|
||||
}
|
||||
return signature;
|
||||
}
|
||||
@end
|
||||
|
||||
#pragma mark - Channel
|
||||
|
||||
@interface FaceclawSshEventQueue : NSObject <FaceclawsshListener>
|
||||
@property(nonatomic, strong) NSMutableArray *events;
|
||||
@property(nonatomic) BOOL ended;
|
||||
@end
|
||||
|
||||
@implementation FaceclawSshEventQueue
|
||||
- (instancetype)init {
|
||||
if ((self = [super init])) _events = [NSMutableArray array];
|
||||
return self;
|
||||
}
|
||||
- (void)onEvent:(NSString *)kind payload:(NSString *)payload {
|
||||
@synchronized(self) {
|
||||
if (_ended) return;
|
||||
[_events addObject:@{@"kind": kind ?: @"", @"payload": payload ?: @""}];
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation FaceclawSshChannel {
|
||||
FaceclawsshChannel *_channel;
|
||||
FaceclawSshEventQueue *_queue;
|
||||
}
|
||||
|
||||
- (instancetype)initWithSpec:(NSString *)spec useKey:(BOOL)useKey {
|
||||
if ((self = [super init])) {
|
||||
_queue = [FaceclawSshEventQueue new];
|
||||
FaceclawSshSigner *signer = nil;
|
||||
if (useKey) {
|
||||
SecKeyRef key = CopyOrCreateClientKey();
|
||||
if (key) {
|
||||
signer = [[FaceclawSshSigner alloc] initWithKey:key];
|
||||
CFRelease(key);
|
||||
}
|
||||
}
|
||||
_channel = FaceclawsshOpen(spec, signer, _queue);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)takeEvents {
|
||||
NSArray *events;
|
||||
@synchronized(_queue) {
|
||||
if (_queue.events.count == 0) return @"[]";
|
||||
events = [_queue.events copy];
|
||||
[_queue.events removeAllObjects];
|
||||
}
|
||||
NSData *data = [NSJSONSerialization dataWithJSONObject:events options:0 error:nil];
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"[]";
|
||||
}
|
||||
|
||||
- (BOOL)send:(NSString *)text {
|
||||
return [_channel send:text];
|
||||
}
|
||||
|
||||
- (void)close {
|
||||
@synchronized(_queue) {
|
||||
_queue.ended = YES;
|
||||
[_queue.events removeAllObjects];
|
||||
}
|
||||
[_channel close];
|
||||
}
|
||||
@end
|
||||
|
||||
#pragma mark - Keys and secrets
|
||||
|
||||
@implementation FaceclawSshKeys
|
||||
|
||||
+ (NSString *)authorizedKeyWithComment:(NSString *)comment {
|
||||
SecKeyRef key = CopyOrCreateClientKey();
|
||||
if (!key) return nil;
|
||||
NSData *publicKey = PublicKeyData(key);
|
||||
CFRelease(key);
|
||||
NSError *error = nil;
|
||||
NSString *line = FaceclawsshAuthorizedKey(publicKey, comment ?: @"", &error);
|
||||
if (error) NSLog(@"FaceclawSsh: authorizedKey failed: %@", error);
|
||||
return line;
|
||||
}
|
||||
|
||||
+ (NSString *)fingerprint {
|
||||
SecKeyRef key = CopyOrCreateClientKey();
|
||||
if (!key) return nil;
|
||||
NSData *publicKey = PublicKeyData(key);
|
||||
CFRelease(key);
|
||||
NSError *error = nil;
|
||||
NSString *fingerprint = FaceclawsshFingerprint(publicKey, &error);
|
||||
if (error) NSLog(@"FaceclawSsh: fingerprint failed: %@", error);
|
||||
return fingerprint;
|
||||
}
|
||||
|
||||
+ (NSDictionary *)secretQuery:(NSString *)secretId {
|
||||
return @{
|
||||
(id)kSecClass: (id)kSecClassGenericPassword,
|
||||
(id)kSecAttrService: kSecretService,
|
||||
(id)kSecAttrAccount: secretId ?: @"",
|
||||
};
|
||||
}
|
||||
|
||||
+ (void)setSecret:(NSString *)value forId:(NSString *)secretId {
|
||||
NSDictionary *query = [self secretQuery:secretId];
|
||||
SecItemDelete((__bridge CFDictionaryRef)query);
|
||||
NSMutableDictionary *item = [query mutableCopy];
|
||||
item[(id)kSecValueData] = [(value ?: @"") dataUsingEncoding:NSUTF8StringEncoding];
|
||||
item[(id)kSecAttrAccessible] = (id)kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly;
|
||||
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)item, NULL);
|
||||
if (status != errSecSuccess) NSLog(@"FaceclawSsh: saving secret failed: %d", (int)status);
|
||||
}
|
||||
|
||||
+ (NSString *)secretForId:(NSString *)secretId {
|
||||
NSMutableDictionary *query = [[self secretQuery:secretId] mutableCopy];
|
||||
query[(id)kSecReturnData] = @YES;
|
||||
query[(id)kSecMatchLimit] = (id)kSecMatchLimitOne;
|
||||
CFTypeRef result = NULL;
|
||||
if (SecItemCopyMatching((__bridge CFDictionaryRef)query, &result) != errSecSuccess) return @"";
|
||||
NSData *data = (__bridge_transfer NSData *)result;
|
||||
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] ?: @"";
|
||||
}
|
||||
|
||||
+ (void)removeSecretForId:(NSString *)secretId {
|
||||
SecItemDelete((__bridge CFDictionaryRef)[self secretQuery:secretId]);
|
||||
}
|
||||
|
||||
+ (void)copyToClipboard:(NSString *)text {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
UIPasteboard.generalPasteboard.string = text ?: @"";
|
||||
});
|
||||
}
|
||||
@end
|
||||
@@ -2,6 +2,7 @@ module FaceclawGraphics {
|
||||
header "FaceclawGraphics.h"
|
||||
header "FaceclawFontRenderer.h"
|
||||
header "FaceclawSocket.h"
|
||||
header "FaceclawSsh.h"
|
||||
header "FaceclawLc3Decoder.h"
|
||||
header "FaceclawSpeech.h"
|
||||
header "FaceclawLocation.h"
|
||||
|
||||
Reference in new issue
Block a user