指定 Arduino WiFiClientSecure 证书

Specifying Arduino WiFiClientSecure Certificates

您应该以什么格式在 WifiClientSecure 模块中提供证书(和密钥)? 不存在 示例,或有关其用法的文档。

我正在关注 Arduino (ESP32) WiFiClientSecure 示例代码 - 并在指定 CA 证书时尝试连接,例如:

client.connect(server, 443, test_ca_cert, test_client_cert, test_client_key)

test_client_certtest_client_key 是 NULL 指针)。如果 test_ca_cert 是 NULL 指针,则 SSL 连接正常。

如果我尝试指定我自己的 test_ca_cert,我总是得到:

CA cert: mbedtls_x509_crt_parse returned -0x2180(这是 "invalid format" 的错误代码)

我已经为 test_ca_cert 尝试了很多东西,例如带有 PEM 格式(明文)base64 编码证书的字符串,以及 DER 格式证书的字节数组。似乎没有任何效果。

此证书应指定的格式是什么?

我通过 brute-force 的组合和 some mbedtls code online 的梳理弄明白了。证书必须以 完全 格式指定,如下所示 - 即通过在数组中嵌入您自己的换行符:

unsigned char test_ca_cert[] = 
"-----BEGIN CERTIFICATE-----\n"
"MIIDpDCCAowCCQC7mCk5Iu3YmDANBgkqhkiG9w0BAQUFADCBkzELMAkGA1UEBhMC\n"
"VVMxFjAUBgNVBAgMDU5ldyBIYW1wc2hpcmUxDzANBgNVBAcMBk5hc2h1YTEYMBYG\n"
"A1UECgwPYnJhZGdvb2RtYW4uY29tMR0wGwYDVQQDDBRCcmFkIEdvb2RtYW4gUm9v\n"
"dCBDQTEiMCAGCSqGSIb3DQEJARYTYnJhZEBicmFkZ29kbWFuLmNvbTAeFw0xNDEy\n"
"MDgwMTM2NDJaFw0yNDEyMDUwMTM2NDJaMIGTMQswCQYDVQQGEwJVUzEWMBQGA1UE\n"
"CAwNTmV3IEhhbXBzaGlyZTEPMA0GA1UEBwwGTmFzaHVhMRgwFgYDVQQKDA9icmFk\n"
"Z29vZG1hbi5jb20xHTAbBgNVBAMMFEJyYWQgR29vZG1hbiBSb290IENBMSIwIAYJ\n"
"KoZIhvcNAQkBFhNicmFkQGJyYWRnb2RtYW4uY29tMIIBIjAN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