AUTHWITHPUBKEY!!!!

i had to extract the middle base64 bit of the key
added computed properties for it
added more error logging
This commit is contained in:
neon443
2025-07-02 09:23:03 +01:00
parent 8b2cdf910b
commit 421444b2f8
2 changed files with 41 additions and 22 deletions

View File

@@ -42,6 +42,19 @@ struct Keypair: KeypairProtocol {
} }
} }
var base64Pubkey: String {
String(openSshPubkey.split(separator: " ")[1])
}
var base64Privkey: String {
var opensshprivkey = openSshPrivkey
let header = "-----BEGIN OPENSSH PRIVATE KEY-----\n"
let footer = "\n-----END OPENSSH PRIVATE KEY-----\n"
opensshprivkey = opensshprivkey.replacingOccurrences(of: header, with: "")
opensshprivkey = opensshprivkey.replacingOccurrences(of: footer, with: "")
return opensshprivkey
}
var openSshPubkey: String { var openSshPubkey: String {
if privateKey.isEmpty { if privateKey.isEmpty {
return "" return ""

View File

@@ -83,25 +83,25 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
try? authWithPubkey2() try? authWithPubkey2()
fatalError() // fatalError()
if state != .authorized { // if state != .authorized {
if !host.password.isEmpty { // if !host.password.isEmpty {
do { try authWithPw() } catch { // do { try authWithPw() } catch {
print("pw auth error") // print("pw auth error")
print(error.localizedDescription) // print(error.localizedDescription)
} // }
} else { // } else {
do { // do {
if let publicKey = host.publicKey, // if let publicKey = host.publicKey,
let privateKey = host.privateKey { // let privateKey = host.privateKey {
try authWithPubkey(pub: publicKey, priv: privateKey, pass: host.passphrase) // try authWithPubkey()
} // }
} catch { // } catch {
print("error with pubkey auth") // print("error with pubkey auth")
print(error.localizedDescription) // print(error.localizedDescription)
} // }
} // }
} // }
ssh_channel_request_env(channel, "TERM", "xterm-256color") ssh_channel_request_env(channel, "TERM", "xterm-256color")
ssh_channel_request_env(channel, "LANG", "en_US.UTF-8") ssh_channel_request_env(channel, "LANG", "en_US.UTF-8")
@@ -281,13 +281,19 @@ class SSHHandler: @unchecked Sendable, ObservableObject {
} }
var pubkey: ssh_key? var pubkey: ssh_key?
ssh_pki_import_pubkey_base64(keypair.publicKey.base64EncodedString(), SSH_KEYTYPE_ECDSA, &pubkey) if ssh_pki_import_pubkey_base64(keypair.base64Pubkey, SSH_KEYTYPE_ED25519, &pubkey) != 0 {
throw .importPubkeyError
}
ssh_userauth_try_publickey(session, nil, pubkey) ssh_userauth_try_publickey(session, nil, pubkey)
var privkey: ssh_key? var privkey: ssh_key?
ssh_pki_import_privkey_base64(keypair.privateKey.base64EncodedString(), keypair.passphrase, nil, nil, &privkey) if ssh_pki_import_privkey_base64(keypair.openSshPrivkey, keypair.passphrase, nil, nil, &privkey) != 0 {
throw .privkeyRejected
}
ssh_userauth_publickey(session, nil, privkey) if ssh_userauth_publickey(session, nil, privkey) != 0 {
throw .pubkeyRejected
}
state = .authorized state = .authorized
} }