From e156d63cf682dc0eb64cdc618668fff04f88d9df Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Sun, 22 Jun 2025 12:35:08 +0100 Subject: [PATCH] WORKS!!!!! PUBKEY ON IOS!!!!!! no file import rn --- ShhShell/SSH/SSHHandler.swift | 34 ++++++++++++------- ShhShell/Views/ConnectionView.swift | 18 ++++++++-- .../Views/Terminal/TerminalDelegate.swift | 1 + 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/ShhShell/SSH/SSHHandler.swift b/ShhShell/SSH/SSHHandler.swift index 40d23ad..6d31e6c 100644 --- a/ShhShell/SSH/SSHHandler.swift +++ b/ShhShell/SSH/SSHHandler.swift @@ -171,29 +171,32 @@ class SSHHandler: ObservableObject { withAnimation { authorized = false } return } + var status: Int32 + let fileManager = FileManager.default + let tempDir = fileManager.temporaryDirectory + let tempPubkey = tempDir.appendingPathComponent("key.pub") + let tempKey = tempDir.appendingPathComponent("key") - print(pubInp) - var pubInpCChar: [Int8] = [] - for byte in pubInp { - pubInpCChar.append(Int8(byte)) - } + fileManager.createFile(atPath: tempPubkey.path(), contents: nil) + fileManager.createFile(atPath: tempKey.path(), contents: nil) - var privInpCChar: [Int8] = [] - for byte in privInp { - privInpCChar.append(Int8(byte)) - } + let attributes: [FileAttributeKey: Any] = [.posixPermissions: 0o600] + try? fileManager.setAttributes(attributes, ofItemAtPath: tempPubkey.path()) + try? fileManager.setAttributes(attributes, ofItemAtPath: tempKey.path()) + + try? pubInp.write(to: tempPubkey) + try? privInp.write(to: tempKey) var pubkey: ssh_key? - - ssh_pki_import_pubkey_base64(&pubInpCChar, SSH_KEYTYPE_SK_ED25519, &pubkey) + ssh_pki_import_pubkey_file(tempPubkey.path(), &pubkey) status = ssh_userauth_try_publickey(session, nil, pubkey) print(status) var privkey: ssh_key? - if ssh_pki_import_privkey_base64(&privInpCChar, pass, nil, nil, &privkey) != 0 { + if ssh_pki_import_privkey_file(tempKey.path(), pass, nil, nil, &privkey) != 0 { print("help?!?") - print("likeley passphrase is incorrect") + print("likeley password is incorrect") } status = ssh_userauth_publickey(session, nil, privkey) @@ -206,6 +209,11 @@ class SSHHandler: ObservableObject { withAnimation { authorized = true } return //if u got this far, youre authed! + //cleanpu here: + //ssh_key_free(pubkey) + //ssh_key_free(privkey) + //try? fileManager.removeItem(at: tempPubkey) + //try? fileManager.removeItem(at: tempKey) } func authWithPw() -> Bool { diff --git a/ShhShell/Views/ConnectionView.swift b/ShhShell/Views/ConnectionView.swift index 6ad0e71..74964a5 100644 --- a/ShhShell/Views/ConnectionView.swift +++ b/ShhShell/Views/ConnectionView.swift @@ -64,7 +64,8 @@ struct ConnectionView: View { HStack { TextField("", text: $pubkeyStr, prompt: Text("Public Key")) .onSubmit { - pubkey = Data(pubkeyStr.utf8) + let newStr = pubkeyStr.replacingOccurrences(of: "\r\n", with: "") + pubkey = Data(newStr.utf8) } Button() { pubPickerPresented.toggle() @@ -75,6 +76,11 @@ struct ConnectionView: View { .fileImporter(isPresented: $pubPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in do { let fileURL = try Result.get() + guard fileURL.startAccessingSecurityScopedResource() else { + print("cant acces file") + return + } + defer { fileURL.stopAccessingSecurityScopedResource() } pubkey = try? Data(contentsOf: fileURL) print(fileURL) } catch { @@ -86,7 +92,8 @@ struct ConnectionView: View { HStack { TextField("", text: $privkeyStr, prompt: Text("Private Key")) .onSubmit { - privkey = Data(privkeyStr.utf8) + let newStr = privkeyStr.replacingOccurrences(of: "\r\n", with: "") + privkey = Data(newStr.utf8) } Button() { privPickerPresented.toggle() @@ -97,6 +104,11 @@ struct ConnectionView: View { .fileImporter(isPresented: $privPickerPresented, allowedContentTypes: [.item, .content, .data]) { (Result) in do { let fileURL = try Result.get() + guard fileURL.startAccessingSecurityScopedResource() else { + print("cant access file") + return + } + defer { fileURL.stopAccessingSecurityScopedResource() } privkey = try? Data(contentsOf: fileURL) print(privkey) print(fileURL) @@ -150,7 +162,7 @@ struct ConnectionView: View { handler.host.key = hostsManager.getHostMatching(handler.host)?.key } } message: { - Text("Expected \(hostsManager.getHostMatching(handler.host)!.key?.base64EncodedString() ?? "null")\nbut recieved \(handler.host.key?.base64EncodedString() ?? "null" ) from the server") + Text("Expected \(hostsManager.getHostMatching(handler.host)?.key?.base64EncodedString() ?? "null")\nbut recieved \(handler.host.key?.base64EncodedString() ?? "null" ) from the server") } .transition(.opacity) .toolbar { diff --git a/ShhShell/Views/Terminal/TerminalDelegate.swift b/ShhShell/Views/Terminal/TerminalDelegate.swift index 3f377fd..d501481 100644 --- a/ShhShell/Views/Terminal/TerminalDelegate.swift +++ b/ShhShell/Views/Terminal/TerminalDelegate.swift @@ -23,6 +23,7 @@ class ShhTerminalViewDelegate: TerminalViewDelegate { func send(source: TerminalView, data: ArraySlice) { print(data) + print("send") } func scrolled(source: TerminalView, position: Double) {