dongyiyu3953 2019-01-08 22:17
浏览 56

结合使用加密/ ssh和代理身份验证,同时回退到加密密钥身份验证

I am implementing an SSH client which will use agent authentication (if available) and fallback to public key authentication if that fails. I have found that I can do this using multiple signers like the following:

sshConfig := &ssh.ClientConfig{
    User:            getUsername(username, currentUser),
    HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    Auth: []ssh.AuthMethod{
        ssh.PublicKeysCallback(getSigners),
    },
}

func getSigners() ([]ssh.Signer, error) {
    signers := make([]ssh.Signer, 0)
    currentUser, _ := user.Current()
    if os.Getenv("SSH_AUTH_SOCK") != "" {
        sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
        if err == nil {
            agentSigners, _ := agent.NewClient(sshAgent).Signers()
            signers = append(signers, agentSigners...)
        }
    }

    // default to id_rsa
    keyPath := path.Join(path.Join(currentUser.HomeDir, ".ssh/id_rsa"))

    buffer, errI := ioutil.ReadFile(keyPath)
    if errI != nil {
        fmt.Println(errI)
        return signers, errI
    }

    block, _ := pem.Decode(buffer)

    var key ssh.Signer
    if strings.Contains(block.Headers["Proc-Type"], "ENCRYPTED") {
        fmt.Print("SSH Passphrase: ")
        bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
        key, _ = ssh.ParsePrivateKeyWithPassphrase(buffer, bytePassword)
    } else {
        key, _ = ssh.ParsePrivateKey(buffer)
    }

    signers = append(signers, key)
    return signers, nil
}

The issue with this method is that it will always prompt for a passphrase, since the passphrase prompt occurs before any authentication attempts have been made. Is it possible to delay when the public key gets decrypted so that agent authentication can be attempted before the user is asked to enter a passphrase?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 名为“Product”的列已属于此 DataTable
    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题