在Swift中,我通过对一些原始的SecKeyRef证书数据调用SecTrustCopyPublicKey来创建X509对象。这就是这个SecKeyRef对象的样子。
Optional(<SecKeyRef algorithm id: 1,
key type: RSAPublicKey,
version: 3, block size: 2048 bits,
exponent: {hex: 10001, decimal: 65537},
modulus: <omitted a bunch of hex data>,
addr: 0xsomeaddresshere>)基本上,这个SecKeyRef对象包含大量关于公钥的信息,但似乎没有办法实际将此SecKeyRef转换为字符串、NSData或其他任何东西(这是我的目标,只是获取一个base64公钥)。
但是,我有一个函数,它可以给出一个modulus和一个exponent,它只计算出公钥是什么。我通过传递从上面的SecKeyRef记录的数据来测试它。
但是不知何故,我无法从SecKeyRef 对象访问这些属性(我只能在控制台中看到整个对象;例如,我不能执行 SecKeyRef.modulus 或其他类似的操作)。
我的问题:我如何才能访问SecKeyRef.modulus**,,或者将这个** SecKeyRef 转换成 NSData 或类似的东西?
编辑
(欲了解更多信息)
我正在动态地创建我的SecKeyRef,通过这个函数我拥有:
func bytesToPublicKey(certData: NSData) -> SecKeyRef? {
guard let certRef = SecCertificateCreateWithData(nil, certData) else { return nil }
var secTrust: SecTrustRef?
let secTrustStatus = SecTrustCreateWithCertificates(certRef, nil, &secTrust)
if secTrustStatus != errSecSuccess { return nil }
var resultType: SecTrustResultType = UInt32(0) // result will be ignored.
let evaluateStatus = SecTrustEvaluate(secTrust!, &resultType)
if evaluateStatus != errSecSuccess { return nil }
let publicKeyRef = SecTrustCopyPublicKey(secTrust!)
return publicKeyRef
}这样做是从证书中获取原始字节流(可以从使用PKI的硬件广播),然后将其转换为SecKeyRef。
编辑2
(截至2015年1月7日对现有答复的评论)
这样做是行不通的:
let mirror = Mirror(reflecting: mySecKeyObject)
for case let (label?, value) in mirror.children {
print (label, value)
}这将导致控制台中的输出:
Some <Raw SecKeyRef object>不知道字符串“一些”是什么意思。
此外,mirror.descendant("exponent") (或“模”)会导致nil,即使在控制台中打印原始对象时,我也能清楚地看到这些属性是存在的,并且它们实际上是填充的。
此外,如果可能的话,我希望避免将NSData**,保存到密钥链,读取为,然后从密钥链**中删除。如赏金描述所述,如果这是唯一可能的办法,请引用权威的参考资料。感谢您至今所提供的所有答案。
发布于 2017-04-05 08:20:42
确实可以使用键链或私有API提取模数和指数。
有(公开但无证件)函数SecKeyCopyAttributes,它从SecKey中提取CFDictionary。属性键的一个有用来源是SecItemConstants.c。
检查这本字典的内容,我们发现一个条目"v_Data" : <binary>。其内容为编码ASN
SEQUENCE {
modulus INTEGER,
publicExponent INTEGER
}请注意,如果整数是正的,并且有一个前导1位(以免将它们与两个补码负数混淆),则整数被填充为零字节,因此您可能会发现一个字节比您预期的多一个字节。如果那样的话,就把它剪掉。
您可以为这种格式实现解析器,或者,知道您的键大小,对提取进行硬编码.对于2048位键(和3字节指数),格式原来是:
30|82010(a|0) # Sequence of length 0x010(a|0)
02|82010(1|0) # Integer of length 0x010(1|0)
(00)?<modulus>
02|03 # Integer of length 0x03
<exponent>总共10 + 1?+ 256 +3= 269或270字节。
import Foundation
extension String: Error {}
func parsePublicSecKey(publicKey: SecKey) -> (mod: Data, exp: Data) {
let pubAttributes = SecKeyCopyAttributes(publicKey) as! [String: Any]
// Check that this is really an RSA key
guard Int(pubAttributes[kSecAttrKeyType as String] as! String)
== Int(kSecAttrKeyTypeRSA as String) else {
throw "Tried to parse non-RSA key as RSA key"
}
// Check that this is really a public key
guard Int(pubAttributes[kSecAttrKeyClass as String] as! String)
== Int(kSecAttrKeyClassPublic as String)
else {
throw "Tried to parse non-public key as public key"
}
let keySize = pubAttributes[kSecAttrKeySizeInBits as String] as! Int
// Extract values
let pubData = pubAttributes[kSecValueData as String] as! Data
var modulus = pubData.subdata(in: 8..<(pubData.count - 5))
let exponent = pubData.subdata(in: (pubData.count - 3)..<pubData.count)
if modulus.count > keySize / 8 { // --> 257 bytes
modulus.removeFirst(1)
}
return (mod: modulus, exp: exponent)
}(最后我编写了一个完整的ASN解析器,所以这段代码没有经过测试,请注意!)
请注意,您可以以非常相同的方式提取私钥的详细信息。使用DER术语,这是v_Data的格式
PrivateKey ::= SEQUENCE {
version INTEGER,
modulus INTEGER, -- n
publicExponent INTEGER, -- e
privateExponent INTEGER, -- d
prime1 INTEGER, -- p
prime2 INTEGER, -- q
exponent1 INTEGER, -- d mod (p-1) (dmp1)
exponent2 INTEGER, -- d mod (q-1) (dmq1)
coefficient INTEGER, -- (inverse of q) mod p (coeff)
otherPrimeInfos OtherPrimeInfos OPTIONAL
}手工解析这一点可能是不明智的,因为任何整数都可能是填充的。
Nota bene:如果密钥是在macOS上生成的,那么公钥的格式是不同的;上面给出的结构是这样包装的:
SEQUENCE {
id OBJECTID,
PublicKey BITSTRING
}位串是上面表格的编码的ASN .
发布于 2016-01-14 07:43:45
更新下面的答案可能会导致您的应用程序因为使用非公共API而被拒绝。
答案在于苹果开源网站的SecRSAKey.h文件(安全是苹果开源代码的一部分)。该文件并不大,它声明了以下两个重要功能:
CFDataRef SecKeyCopyModulus(SecKeyRef rsaPublicKey);
CFDataRef SecKeyCopyExponent(SecKeyRef rsaPublicKey);您可以将这些函数添加到桥接头中,以便能够从Swift调用它们,同时还可以从CFDataRef切换到NSData*,因为这两种类型的免费桥接:
NSData* SecKeyCopyModulus(SecKeyRef rsaPublicKey);
NSData* SecKeyCopyExponent(SecKeyRef rsaPublicKey);演示快速使用:
let key = bytesToPublicKey(keyData)
let modulus = SecKeyCopyModulus(key)
let exponent = SecKeyCopyExponent(key)
print(modulus, exponent)不过,这是一个私有API,而且可能在某一时刻不再可用,但是我查看了Security made public (http://www.opensource.apple.com/source/Security)的版本,看起来这两个函数都存在于所有这些版本中。更重要的是,由于Security是操作系统的关键组成部分,苹果不太可能对其做出重大改变。
在iOS 8.1、iOS 9.2和OSX10.10.5上进行了测试,代码可以在所有三个平台上运行。
发布于 2016-01-01 23:03:25
我一直在沿着相同的路径尝试SSL公钥钉扎。API几乎不存在,我找到的解决方案是将其放入密钥链中,然后您可以将其检索为NSData (然后可以对其进行Base64编码)。这很可怕,但经过一天左右的研究,我唯一能找到的东西(不用把OpenSSL和我的应用捆绑在一起)。
我将我的一些代码移植到Swift中,但是我还没有对它进行太多的测试,所以我不能100%确定它是否工作:https://gist.github.com/chedabob/64a4cdc4a1194d815814
它基于这个Obj代码(我相信它在一个生产应用程序中工作):https://gist.github.com/chedabob/49eed109a3dfcad4bd41
https://stackoverflow.com/questions/34408825
复制相似问题