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 怎么支持低版本项目使用高版本库?

相关推荐
alloc10 小时前
Foundation Models Framework
ios
ajassi200013 小时前
开源 Objective-C IOS 应用开发(二十三).a静态库的封装和使用
ios·开源·objective-c
明远湖之鱼15 小时前
浅入理解流式SSR的性能收益与工作原理
前端·ios
白玉cfc15 小时前
【iOS】多线程基础
macos·ios
2501_9159090616 小时前
iOS APP 抓包全流程解析,HTTPS 调试、网络协议分析与多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063217 小时前
游戏上架 App Store 的技术流程解析 从构建到审核的全流程指南
游戏·macos·ios·小程序·uni-app·cocoa·iphone
非专业程序员17 小时前
精读 GitHub - servo 浏览器(一)
前端·ios·rust
2501_916007471 天前
iOS 压力测试的工程化体系,构建高强度、多维度、跨工具协同的真实负载测试流程
android·ios·小程序·uni-app·cocoa·压力测试·iphone
2501_916008891 天前
API接口调试全攻略 Fiddler抓包工具、HTTPS配置与代理设置实战指南
前端·ios·小程序·https·fiddler·uni-app·webview
2501_915921431 天前
iOS 开发者工具推荐,构建从调试到性能优化的多维度生产力工具链(2025 深度工程向)
android·ios·性能优化·小程序·uni-app·iphone·webview