Mar*_*rek 5 certificate pkcs#12 ios swift swift3
此函数将Base64编码的PKCS#12证书字符串作为参数,然后将其解码并传递给SecPKCS12Import函数.在更新到iOS 11后,SecPKCS12Import会产生不同的结果.两个OS版本上的securityError都返回0.
let securityError: OSStatus = SecPKCS12Import(decodedData!, options, &items)
Run Code Online (Sandbox Code Playgroud)
返回0项的列表.而在iOS 10上,我在阵列中得到1个项目.
func certificateFromCertificate(certP12: String, psswd: String) -> SecCertificate {
let decodedData = NSData(base64Encoded: certP12, options:NSData.Base64DecodingOptions(rawValue: 0))
let keytmp : NSString = kSecImportExportPassphrase as NSString
let options : NSDictionary = [keytmp : psswd]
var certificateRef: SecCertificate? = nil
var items : CFArray?
let securityError: OSStatus = SecPKCS12Import(decodedData!, options, &items)
let theArray: CFArray = items!
if securityError == noErr && CFArrayGetCount(theArray) > 0 {
let newArray = theArray as [AnyObject] as NSArray
let dictionary = newArray.object(at: 0)
let secIdentity = (dictionary as AnyObject)[kSecImportItemIdentity as String] as! SecIdentity
let securityError = SecIdentityCopyCertificate(secIdentity , &certificateRef)
if securityError != noErr {
certificateRef = nil
}
}
certificate = certificateRef
return certificateRef!
}
Run Code Online (Sandbox Code Playgroud)
这是Apple Developer论坛上的帖子,说SecPKCS12Import实现了从Base64的自动转换.这意味着我应该在将普通证书传递给函数之前对其进行解码.这可能是问题吗?
开发环境:
编程语言:Swift 3
调试设备:Apple iPad mini Retina Wi-Fi 32GB ME280SL/A.
开发设备:iMAC mini Xcode版本9.0 9A235
Apple 开发人员支持已调查此问题并得出结论:特定 .p12 文件的 DER 编码不符合 iOS 11 的安全要求。
\n\n下面贴出 Quinn 来自 Apple DTS 的电子邮件:
\n\n\n\n\n要了解\xe2\x80\x99 发生了什么,您必须在\n 函数上设置断点
\n\nSecKeyCreateRSAPrivateKey。这是安全框架内的私有函数,因此您需要使用符号断点(在断点导航器中,单击 + 按钮,然后从菜单中选择符号断点。一旦您\xe2\x80\x99 完成后,点击 \xe2\x80\x9cTest 1\xe2\x80\x9d 按钮,\xe2\x80\x99 将在断点处停止。Run Code Online (Sandbox Code Playgroud)\n\nThis function takes 4 arguments, but the two relevant ones are the second and third arguments, which are a pointer to the RSA private key\n要解码的数据以及该数据的长度。一旦到达\n断点,您就可以转储数据,如下所示:
\n
(lldb) # Confirm the length in the third argument.\n(lldb) p $arg3\n(unsigned long) $0 = 1192\n(lldb) # Dump the data pointed to be the second argument.\n(lldb) memory read -f x -s 1 -c 1192 --force $arg2\n\xe2\x80\xa6 elided \xe2\x80\xa6\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n结果太长,无法包含在此处,因此我取消了十六进制编码并将结果附加为文件
\n\nRSAPrivateKey.asn1.如果你用\xe2\x80\x99转储它,
\ndumpasn1就会看到问题:
$ dumpasn1 -p -a RSAPrivateKey.asn1 \nSEQUENCE {\n INTEGER 0\n INTEGER\n \xe2\x80\xa6\n INTEGER 65537\n INTEGER\n 00 0B 67 67 29 0A 38 38 7A 7E 17 39 E7 84 FB 7D\n 02 D1 AB AD 21 27 4D CD 2C 09 C1 1F CB 73 5C 18\n 37 D4 CE E2 98 61 19 3B 70 6C 1A 1B 33 E8 69 8C\n 65 5B 77 B8 24 9C 90 8A 79 A7 4A 77 26 38 7C 3E\n 70 3C 80 24 BD 73 DA 97 5A F3 90 0F 79 2D 45 F8\n 2A 5A 37 03 4D 0C 80 DB 8F 99 67 55 D3 F3 70 CA\n 2D F0 B6 48 6A D8 9A 95 CE AA C8 F1 96 24 04 61\n 38 A1 7C CD 56 70 5E F9 13 D4 1B E2 F2 5D 28 67\n 9A 30 E0 ED 71 84 4C EC 86 44 18 5F 64 70 46 D8\n 6B 3A 52 2C D7 DE AF E5 E2 35 41 0D 1E FF E7 DE\n AC 43 83 5C A6 F2 2E C8 79 1A 30 87 7A 78 9B 42\n 3E D9 2D AA C0 9D A6 7C E9 5C E3 6C 1E 8D 87 DF\n EB 05 DF CA F5 B9 2D BA B6 01 71 18 22 4D 25 4E\n D5 77 CB B8 9B 95 F9 C6 39 1C 0D D2 46 E4 4A 45\n D8 26 6F B4 25 03 E7 BE 91 02 43 7D DC B0 1E C8\n 67 E8 2E 5F EA 3A 8D 1C 69 43 80 F9 60 69 BF BA\n 01\n Error: Integer has non-DER encoding.\n INTEGER\n \xe2\x80\xa6\n INTEGER\n \xe2\x80\xa6\n INTEGER\n \xe2\x80\xa6\n INTEGER\n \xe2\x80\xa6\n INTEGER\n \xe2\x80\xa6\n }\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n请注意,\xe2\x80\x9cInteger 在第四个 INTEGER 元素中标记了非 DER 编码\xe2\x80\x9d 错误。出现这种情况是因为 INTEGER 数据不规范。在 ASN.1 DER 中,INTEGER 是有符号的,这会引入歧义。像 80 00 这样的值可以解释为 -32768 或\n 32768。要解决此 ASN.1 DER 要求将正数形式编码为 00 80 00(\xe2\x80\x99s 类似的情况需要前导\不适用)。此外,DER 特别要求仅在必要时添加该前导字节。在上面所示的情况下,第二个字节 0b 没有设置最高位,因此必须以 00 开头。
\n\n早期版本的 iOS 并未强制执行此要求。iOS 11 包含许多安全强化更改,拒绝\n 无效编码的 ASN.1 DER INTEGER 就是其中之一。
\n\n我\xe2\x80\x99见过其他开发人员受到此安全更改的影响,但这些情况更容易调试,因为密钥\xe2\x80\x99t嵌入\n在PKCS#12中。例如,考虑以下 DevForums\n 线程。\n https://forums.developer.apple.com/message/262593#262593 \n 此外,这个问题很难调试,因为当您解压
\n.p12with时openssl,它会忽略输入和输出时的此错误会发出固定版本的私钥!
Quin 建议我通过修复生成此 PKCS#12 数据的任何代码来解决此问题。如果\xe2\x80\x99s 不可能,你\xe2\x80\x99 将需要编写(或获取)自己的代码来解析 PKCS#12。iOS 11 正确地拒绝了格式错误的私钥,并且\xe2\x80\x99s 不太可能改变。
\n| 归档时间: |
|
| 查看次数: |
523 次 |
| 最近记录: |