首页 > 解决方案 > Swift Keychain 不会加载外部生成的密钥。SecKey 在没有错误的情况下加载密钥后返回 Nil 值

问题描述

我是 SWIFT、iOS as 和 Cryptography 的新手。请多多包涵。我无法在 Swift 5 中的 iOS 钥匙串中加载外部钥匙。我在 Stackoverflow 和其他网站上遇到了几篇关于将钥匙加载到 iOS 钥匙串中的困难的帖子。

我的目标是使用端口 22、公钥和用户名凭据与远程 SFTP 服务器建立 SSH 连接。截至目前,防火墙规则允许使用 NMSSH(来自 GIT)连接到服务器,但如果没有在 i-phone 和 Windows 服务器之间交换公钥,我将无法进行身份验证。我多次从我的 Swift 项目中生成密钥对,并将公钥发送给 Windows 服务器管理员,以将其添加到我在 SSH 设置中的用户名中。管理员的所有尝试都因服务器锁定而失败。所以最后他使用 puTTYGen 在服务器中生成了一个密钥对,将公钥添加到我的用户名中,并将密钥对发送给我以添加到 iOS 钥匙串中。

我将私钥导出到 puTTYgen 中的 OpenSSL 并将其转换为 base64 编码字符串,然后在 GIT 的 RSAUtils 中使用它(https://github.com/btnguyen2k/swiftutils)。该过程有效,但将 SecKey 返回为 Nil。看起来钥匙从未进入钥匙串。

在使用这个 GIT 代码之前,我曾多次尝试使用 secItemAdd。该代码工作并显示为获取密钥,但在 SecItemCopyMatching 中将 Nil 作为 SecKey 值返回。

我还通过在http://blog.flirble.org/2011/01/05/rsa-public-中为 .m 代码创建一个 .h 标头、一个 .m 文件和一个桥来冒险在 Swift 中添加 Objective-C键-openssl-ios/。同样的问题。在上述 .m 完成并返回一个值后,我得到一个 Nil 。

我正在处理的私钥有页眉和页脚 -----BEGIN RSA PRIVATE KEY----- 和 -----END RSA PRIVATE KEY-----。我可以在键中看到 +、/ 等字符,表明它不是 base64,所以我使用了字符串扩展名来转换它。

func toBase64() -> String {
    return Data(self.utf8).base64EncodedString()

编码:


// added to use GIT code
// https://github.com/btnguyen2k/swiftutils.git

// variable declared at class level

var pKeyContents: String = ""

func addKeys2() {

    let tag: String = "com.mail.private"

    // if let path = Bundle.main.path(forResource: "PKeyWOHeader", ofType: "txt") // I tried this first after manually removing header and footer ("BEGIN ....KEY AND "END ... KEY" etc). It didn't work

       if let path = Bundle.main.path(forResource: "ExportAsOpenSSL", ofType: "") // This one is a dump of puTTYgen export to openssl of private key

    {
            do {
                let contents = try String(contentsOfFile: path)
                pKeyContents = contents
                }
            catch {
                print("Contents could not be loaded")
                return
            }
    } else {
        print("File not found")
        return
    }

    let myData = pKeyContents.data(using: String.Encoding.utf8)

    let encoded = pKeyContents.toBase64()

    //let instanceOfObjCKeyFix = ObjCKeyFix()
    //privateKeyAsData = instanceOfObjCKeyFix.stripPublicKeyHeader(myData)
    // stringFromData = String(decoding: privateKeyAsData!, as: UTF8.self)

    // Above lines were an attempt to use Objective C code from
    // http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/.
    // This was also returning nil value

    do {
        try privateKey = RSAUtils.addRSAPrivateKey(encoded, tagName: tag)
        print(privateKey as Any) // returns nil
    } catch {
        print("error)")
    }

}

我的目标是将密钥从外部加载到 iOS 钥匙串。我还尝试发送使用https://digitalleaves.com/blog/2015/10/sharing-public-keys-between-ios-and-the-rest-of-the-world/中建议的 CryptoExportImportManager() 创建的公钥. 就像我之前说的,我的 Windows 服务器管理员在尝试加载此类密钥时会报告服务器锁。

任何帮助表示赞赏。谢谢

标签: iosswiftkeychain

解决方案


我正在用我自己的答案更新这个。在尝试了上述链接(Digital Leaves 和 Flirble.org)中提到的两个解决方案几周后,我可以使用来自 I-phone 7 的 NMSSH 连接并验证到 Windows 服务器。我无法使用 I-phone 钥匙串来存储外部生成的密钥。这太乏味了。我能做到的最好的办法是在钥匙串中添加一个私钥,但是当我试图检索它时它总是返回 Nil。

我遵循 NMSSH 中推荐的连接步骤,使用 cocopod,添加到框架并添加库。在使用 NMSSH 会话类时,我使用了带有 in memory keys 选项的方法。重要的一点是将公钥参数留空,并为私钥提供私钥和密码。


推荐阅读