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

相关推荐
iOS日常2 天前
Xcode 垃圾清理
ios·xcode
开心就好20252 天前
不越狱能抓到 HTTPS 吗?在未越狱 iPhone 上抓取 HTTPS
后端·ios
傅里叶2 天前
iOS相机权限获取
flutter·ios
zhangkai3 天前
flutter存储知识点总结
flutter·ios
齐生13 天前
网络知识点 - TCP/IP 四层模型知识大扫盲
笔记·ios
IT技术分享社区3 天前
数码资讯:iPhone 18 Pro,十大升级细节浮出水面
ios·手机·iphone
嵌入式学习菌3 天前
https不校验证书实现及https接口实现
ios·iphone