iOS从Matter的设备认证证书中获取VID和PID

设备认证证书也叫 DAC, 相当于每个已经认证的设备的标识。包含了 VID 和 PID.

根据 Matter 对于设备证书的规定,DAC证书subject应该包含VID 和 PID. 可通过解析 X509 证书读取subject 来获得信息。

1 通过 SPM 添加X509

git地址:https://github.com/apple/swift-certificates.git

苹果开源库。

注意最低支持 iOS13系统。

2 项目实现了 MatterExtension

这个通过 Matter Support 配 Matter 必须实现的。

并从协议中获取设备相关证书。

swift 复制代码
class RequestHandler: MatterAddDeviceExtensionRequestHandler {
    let dataShare = MatterSupportShare() // 数据共享, 扩展和主 APP 共享数据,通过 Group 来共享。
    override func validateDeviceCredential(_ deviceCredential: MatterAddDeviceExtensionRequestHandler.DeviceCredential) async throws {
        // Use this function to perform additional attestation checks if that is useful for your ecosystem.
        dataShare.matterDeviceCredentialDAC = deviceCredential.deviceAttestationCertificate  // 保存DAC 信息, 
    }
    // 其他...
}

3 解析 X509

swift 复制代码
import X509

/// 从dacData中获取 vid 和 pid
    class func getX509Info(dacData: Data) -> (vid: String?, pid: String?) {
        let der = Array(dacData)
        guard let cer = try? Certificate(derEncoded: der) else {
            print("不存在 rdns")
            return (nil,nil)
        }
        let subject = cer.subject
        let VIDType = "1.3.6.1.4.1.37244.2.1"
        let PIDType = "1.3.6.1.4.1.37244.2.2"
        var vid: String?
        var pid: String?
        for inex1  in subject.startIndex..<subject.endIndex {
            let rdn = subject[inex1]
            for inex2 in rdn.startIndex..<rdn.endIndex {
                let attribute = rdn[inex2]
                print("rdn:\(attribute)")
                let type = attribute.type.oidComponents.map { String($0) }.joined(separator: ".")
                let value = attribute.value.description
                if type==VIDType {
                    vid = value
                } else if type == PIDType {
                    pid = value
                }
            }
        }
        // 16进制
        return (vid,pid)
    }

这样就可以在 Matter Support 阶段就能读取设备的 VID 和 PID.

待解决问题

Swift Package Manager 怎么支持低版本项目使用高版本库?

相关推荐
00后程序员张9 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
00后程序员张13 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
TheNextByte114 小时前
如何从锁定的 iPhone 中恢复照片?
ios·iphone
Maynor99615 小时前
Clawdbot手机访问完整教程:像用App一样方便
ios·智能手机·iphone
游戏开发爱好者817 小时前
在 Windows、Linux 与 CI 环境下命令行上传 IPA 到 App Store
linux·windows·ios·ci/cd·小程序·uni-app·iphone
符哥200817 小时前
对比ArkTsUI和Flutter和 SwiftUI 和Jetpack Compose四个框架语法及使用场景。
flutter·ios·swiftui
TheNextByte117 小时前
如何在有或没有备份的 iPhone 上检索已删除的短信
ios·iphone
TheNextByte119 小时前
如何在恢复模式下从 iPhone 恢复照片?
ios·cocoa·iphone
TheNextByte119 小时前
【已修复】由于软件版本过旧,无法将备份恢复到此 iPhone
ios·cocoa·iphone
2501_916007472 天前
跨平台 App 安全,Flutter、RN、Unity、H5 混合应用加固
android·ios·小程序·https·uni-app·iphone·webview