繁体   English   中英

如何将 SecKey 从应用程序钥匙串移动到共享钥匙串组

[英]How to move SecKey , from app keychain to shared keychain group

我正在我的 iOS 应用程序中生成私钥,用于服务器和应用程序之间的安全通信。 私钥存储在钥匙串中。 在新版本的应用程序中,由于通知扩展,我想使用共享钥匙串组。 如何将存储在应用程序钥匙串中的私钥传输到共享组钥匙串。 下面是我用来生成私钥的代码

func createPrivateKey(withLabel label: String) throws -> SecKey {
    let privateKeyAttrs: [String: Any] = [
        kSecAttrIsPermanent as String: true,
        kSecAttrApplicationLabel as String: label,
        kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
    ]

    let attributes: [String: Any] = [
        kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
        kSecAttrKeySizeInBits as String: 2048,
        kSecPrivateKeyAttrs as String: privateKeyAttrs,
    ]

    var error: Unmanaged<CFError>?
    guard let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
        // swiftlint:disable:next force_unwrapping
        throw error!.takeRetainedValue() as Error
    }
    return privateKey
}

您必须在创建时指定访问组,或使用新访问组更新现有密钥。 完成后,您应该正确设置权利,以便您创建的应用程序和扩展程序可以访问正确的钥匙串访问组。 仔细阅读,因为应用程序组和钥匙串共享组之间只有一条细线。 确保您设置了正确的( 此处为文档)。

至于你的查询

let accessGroup = "<# Your Team ID #>.com.example.SharedItems"
let attributes: [String: Any] = [
    kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
    kSecAttrKeySizeInBits as String: 2048,
    kSecAttrAccessGroup as String: accessGroup,
    kSecPrivateKeyAttrs as String: privateKeyAttrs
]

这应该会创建用于为您指定的访问组创建密钥的查询。 我想您可以自己找出更新查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM