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后程序员张10 小时前
如何提高 IPA 安全性 多工具组合打造可复用的 iOS 加固与反编译防护体系(IPA 安全 iOS 加固 无源码混淆 Ipa Guard 实战)
android·安全·ios·小程序·uni-app·iphone·webview
洞窝技术13 小时前
前端开发APP之跨平台开发(ReactNative0.74.5)
android·react native·ios
EricStone14 小时前
iOS语音转换SDK相关记录
ios
2501_9160074716 小时前
Fastlane 结合 开心上架 命令行版本实现跨平台上传发布 iOS App
android·ios·小程序·https·uni-app·iphone·webview
00后程序员张17 小时前
iOS 26 内存占用监控 多工具协同下的性能稳定性分析实战
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9159090618 小时前
iOS 26 性能监控工具有哪些?多工具协同打造全方位性能分析体系
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_9387739918 小时前
Objective-C 类的归档与解档:NSCoding 协议实现对象持久化存储
开发语言·ios·objective-c
美狐美颜SDK开放平台19 小时前
美颜SDK跨平台适配实战解析:让AI美颜功能在iOS与Android都丝滑运行
android·人工智能·ios·美颜sdk·直播美颜sdk·第三方美颜sdk·美颜api
2501_9159184120 小时前
uni-app 上架 iOS 应用全流程 从云打包到开心上架(Appuploader)免 Mac 上传发布指南
android·macos·ios·小程序·uni-app·iphone·webview
2501_9159214321 小时前
iOS 抓包工具有哪些,开发者的选型与实战指南
android·ios·小程序·https·uni-app·iphone·webview