gpt4 book ai didi

swift - 如何从 SecCertificate 中提取或比较 kSecPropertyKeyValue

转载 作者:行者123 更新时间:2023-11-30 11:38:20 28 4
gpt4 key购买 nike

需要解析和比较证书值,但其中一些以 Data 形式返回,而不会解析为字符串。大多数属性以数字或字符串形式返回,但 kSecPropertyKeyValueCFData 数组。需要此功能来显示与钥匙串(keychain)访问类似的证书信息。

import Foundation
import Security

let query: [CFString: Any] = [
kSecClass: kSecClassCertificate,
kSecReturnAttributes: kCFBooleanTrue,
kSecReturnRef: kCFBooleanTrue,
kSecReturnData: kCFBooleanTrue,
kSecMatchLimit: kSecMatchLimitAll
]

var result: AnyObject?
SecItemCopyMatching(query as CFDictionary, &result)
let certs: [SecCertificate] = (result as? [[CFString: Any]])?.map({ $0[kSecValueRef] as! SecCertificate }) ?? []

for cert in certs {
if let values: [CFString:Any] = (SecCertificateCopyValues(cert, [kSecOIDExtendedKeyUsage] as CFArray, nil) as? [CFString:Any])?[kSecOIDExtendedKeyUsage] as? [CFString:Any] {

// Expect to find `kSecOIDExtendedUseCodeSigning` value or something else
// meaningful. How do I do that?

print(
"type:", values[kSecPropertyKeyType]!,
"||| data values:", values[kSecPropertyKeyValue] as! [Data],
"||| cString:", (values[kSecPropertyKeyValue] as! [CFData]).map({ String(cString: CFDataGetBytePtr($0)) }),
"||| string:", (values[kSecPropertyKeyValue] as! [Data]).map({ String(data: $0, encoding: .utf8) })
)
}
}

粘贴到 Playground ,打印:

type: array ||| data values: [8 bytes] ||| cString: ["+\u{06}\u{01}\u{05}\u{05}\u{07}\u{03}\u{04}"] ||| string: [Optional("+\u{06}\u{01}\u{05}\u{05}\u{07}\u{03}\u{04}")]
type: array ||| data values: [9 bytes] ||| cString: ["*�H��cd\u{04}\t"] ||| string: [nil]

有些看起来像 unicode 转义,有些看起来像无效字符串。任何帮助表示赞赏。

最佳答案

显然 OID 值没有可读的字符串表示形式,而是常量,可以在 google/der-ascii 中找到。 .

例如,上面的输出 +\u{06}\u{01}\u{05}\u{05}\u{07}\u{03}\u{04} 匹配 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x04,即 email protection (+0x2b,以字节为单位)。

关于swift - 如何从 SecCertificate 中提取或比较 kSecPropertyKeyValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com