【问题标题】:Retrieving list of signing identities via API通过 API 检索签名身份列表
【发布时间】:2018-10-29 20:33:24
【问题描述】:

这与 macOS 有关。

我需要使用本地 Mac 应用程序内部的 API 检索导入并在 Keychain 上可用的签名身份列表(证书 + 私钥)。我可以运行以下命令行并解析结果:

> /usr/bin/security find-identity -v -p codesigning
> 
>  1) 0123456789ABCDEF0123456789ABCDEF01234567 "iPhone Developer: John Doe (GTHESFW12)"
>  2) 0123456789ABCDEF0123456789ABCDEF01234567 "iPhone Distribution: ABC Inc (12356DGEWS)"
> 2 valid identities found

但我正在寻找一种通过本机安全 API 执行此操作的方法。有没有办法做到这一点?

【问题讨论】:

  • 你需要阅读Keychain Services
  • 非常感谢!我正在查看附近的内容,但没有找到正确的链接
  • 祝你好运。这是一个令人生畏的 API,但它应该有你需要的一切。

标签: objective-c swift macos security certificate


【解决方案1】:

@JamesBucanek,比你的链接!这是使用 Keychain Services API 生成的代码。可能对其他人有用:

let query: [String: Any] = [
    kSecClass as String: kSecClassIdentity,
    kSecReturnRef as String: kCFBooleanTrue,
    kSecMatchLimit as String: kSecMatchLimitAll
]
var items: CFTypeRef?

// Get list of all SecIdentity from Keychain without limiting search and without any filtering applied
guard SecItemCopyMatching(query as CFDictionary, &items) == errSecSuccess, let array = items as? NSArray else {
    return
}

for item in array {
    let identity = item as! SecIdentity
    var certificate: SecCertificate?

    // Get SecCertificate out of SecIdentity object (it contains both SecCertificate and SecKey
    if SecIdentityCopyCertificate(identity, &certificate) == errSecSuccess {

        var commonName: CFString?
        // Print name for each certificate
        if SecCertificateCopyCommonName(certificate!, &commonName) == errSecSuccess {
            print(commonName! as String)
        }
    }
}

【讨论】:

  • 节省了我的时间,非常感谢 :) 如果有人想将其转换为数据,下面还有:let certData = SecCertificateCopyData(certificate) as Data print("certData : ((certData.base64EncodedString()) )")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多