This commit is contained in:
2026-09-24 16:25:22 +08:00
commit 7428184f01
1198 changed files with 314515 additions and 0 deletions
@@ -0,0 +1,122 @@
import Foundation
import Security
// UTS
import DCloudUTSFoundation
func getClientCertFromP12File(resourcePath: String, certPassword: String) -> [String: NSObject]? {
do {
//
let p12Data = try Data(contentsOf: URL(fileURLWithPath: resourcePath))
// p12
let key = kSecImportExportPassphrase as String
let options : NSDictionary = [key: certPassword]
var items : CFArray?
let securityError = SecPKCS12Import(p12Data as NSData, options, &items)
guard securityError == errSecSuccess else {
if securityError == errSecAuthFailed {
console.log("ERROR: SecPKCS12Import returned errSecAuthFailed. Incorrect password?")
} else {
console.log("Failed to open the certificate file", resourcePath)
}
return nil
}
guard let theArray = items, CFArrayGetCount(theArray) > 0 else {
return nil
}
let dictionary = (theArray as NSArray).object(at: 0)
guard let identity = (dictionary as AnyObject).value(forKey: kSecImportItemIdentity as String) as? NSObject else {
return nil
}
return [
"useCertificateChainValidation": NSNumber(value: true),
"useSSLCertificateVerification": NSNumber(value: true),
"clientCertificate": identity
]
} catch {
console.log("读取证书文件失败", resourcePath)
return nil
}
}
// PEMDER
func convertPEMToDER(pemString: String) -> Data? {
//
var cleanedString = pemString.trimmingCharacters(in: .whitespacesAndNewlines)
// PEM
let beginCertPattern = "-----BEGIN CERTIFICATE-----"
let endCertPattern = "-----END CERTIFICATE-----"
// PEM
if !cleanedString.contains(beginCertPattern) || !cleanedString.contains(endCertPattern) {
console.log("警告: PEM证书格式不标准,缺少标准头尾标记")
// Base64
let noWhitespace = cleanedString.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
return Data(base64Encoded: noWhitespace)
}
// BEGINEND
guard let startRange = cleanedString.range(of: beginCertPattern),
let endRange = cleanedString.range(of: endCertPattern) else {
console.log("无法定位PEM证书的头尾标记位置")
return nil
}
// BEGIN
let afterBeginIndex = cleanedString.index(after: startRange.upperBound)
// END
let beforeEndIndex = endRange.lowerBound
// Base64
let base64Content = String(cleanedString[afterBeginIndex..<beforeEndIndex])
//
let noWhitespace = base64Content.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)
// Base64
guard let data = Data(base64Encoded: noWhitespace) else {
console.log("Base64解码失败,证书内容可能损坏")
return nil
}
return data
}
func getClientCertFromCrtFile(resourcePath: String) -> [String: NSObject]? {
do {
//
let pemData = try Data(contentsOf: URL(fileURLWithPath: resourcePath))
//
guard let pemString = String(data: pemData, encoding: .utf8) else {
console.log("将证书数据转换为字符串失败")
return nil
}
// PEMDER
guard let derData = convertPEMToDER(pemString: pemString) else {
console.log("转换PEM到DER格式失败")
return nil
}
// SecCertificate
guard let certificate = SecCertificateCreateWithData(nil, derData as CFData) else {
console.log("创建SecCertificate对象失败")
return nil
}
return [
"useCertificateChainValidation": NSNumber(value: true),
"useSSLCertificateVerification": NSNumber(value: true),
"kCFStreamSSLCertificates": [certificate] as NSArray
]
} catch {
console.log("读取证书文件失败",resourcePath)
return nil
}
}