ios - Swift Keychain 不加载外部生成的 key 。 SecKey 加载 key 后无错误返回 Nil 值

标签 ios swift keychain

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

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

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

在使用这段GIT代码之前,我多次尝试使用secItemAdd。该代码的工作方式和显示方式与获取 key 类似,但在 SecItemCopyMatching 中返回 Nil 作为 SecKey 值。

我还尝试在 Swift 中添加 Objective-C,方法是在 http://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/ 中创建 .h header 、.m 文件和 .m 代码的桥接器。 。同样的问题。上面的 .m 完成并返回一个值后,我得到一个 Nil。

我正在处理的私钥有页眉和页脚 -----BEGIN RSA PRIVATE KEY----- 和 -----END RSA PRIVATE KEY-----。我可以在 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)")
    }

}

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

感谢任何帮助。 谢谢

最佳答案

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

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

关于ios - Swift Keychain 不加载外部生成的 key 。 SecKey 加载 key 后无错误返回 Nil 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59469572/

相关文章:

ios - 在常量初始化中使用 IBoutlet

ios - 如何从 iOS 上的 Facebook SDK 获取用户数据

objective-c - 区分 Mac OS X 钥匙串(keychain)中的 session 和系统项目

ios - UILabel 不会在 UIView 中居中

iphone - 按住按钮时如何更改具有选定颜色的单元格

ios - 在应用程序级别删除粘贴板的内容

ios - 如何使用 Locksmith 保存和加载多个凭据?

ios - 防止 SwiftUI NavigationLink 继承重写的 preferredColorScheme

ios - 如何将带有 subview 的 UIImageView 保存到相机胶卷?

ios - 我应该使用哪个 key 将密码存储在 iOS 钥匙串(keychain)中?