【问题标题】:SSL certificate verification on ESP8266 Wemos D1 Mini with MQTT broker使用 MQTT 代理在 ESP8266 Wemos D1 Mini 上验证 SSL 证书
【发布时间】:2017-10-13 18:38:04
【问题描述】:

我有一个树莓派 3,它的操作系统是 raspbian stretch。按照本教程,我已经在树莓派上安装并完全配置了一个 MQTT 代理:https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-the-mosquitto-mqtt-messaging-broker-on-ubuntu-16-04 经纪人方面一切正常。证书在 60 天后更新,您只能通过 localhost 连接到端口 1883,其他端口(8883 和 8083)是开放的,但只能使用 TLS 版本 1.2 访问,后者也使用 websockets。下面你可以找到我配置mosquitto的代码(/etc/mosquitto/conf.d/default.conf)。

allow_anonymous false
password_file /etc/mosquitto/passwd

listener 1883 localhost

listener 8883
certfile /etc/letsencrypt/live/home.kamidesigns.be/cert.pem
cafile /etc/letsencrypt/live/home.kamidesigns.be/chain.pem
keyfile /etc/letsencrypt/live/home.kamidesigns.be/privkey.pem
tls_version tlsv1.2

listener 8083
protocol websockets
certfile /etc/letsencrypt/live/home.kamidesigns.be/cert.pem
cafile /etc/letsencrypt/live/home.kamidesigns.be/chain.pem
keyfile /etc/letsencrypt/live/home.kamidesigns.be/privkey.pem
tls_version tlsv1.2

我还购买了 ESP8266 Wemos D1 Mini 以安全地连接到该代理。我使用此链接中的 pubsubclient 库:https://github.com/knolleary/pubsubclient 用于我的 MQTT 客户端。 我使用此链接的主分支:https://github.com/esp8266/Arduino 进行安全 SSL 连接。下面是我用于编写 Wemos D1 Mini 的代码

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <time.h>

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

}

const char* ssid = "ssid";
const char* password = "wifipassword";

const char* host = "home.kamidesigns.be";
const int port = 8883;

WiFiClientSecure espClient;
PubSubClient client(host, port, callback, espClient);

long lastMsg = 0;
char msg[50];
int value = 0;

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.print("connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  // Synchronize time useing SNTP. This is necessary to verify that
  // the TLS certificates offered by the server are currently valid.
  Serial.print("Setting time using SNTP");
  configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
  time_t now = time(nullptr);
  while (now < 1000) {
    delay(500);
    Serial.print(".");
    now = time(nullptr);
  }
  Serial.println("");
  struct tm timeinfo;
  gmtime_r(&now, &timeinfo);
  Serial.print("Current time: ");
  Serial.print(asctime(&timeinfo));
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266LightController","username","password")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

当我启动 Wemos D1 时,串行监视器显示: 连接到 ssid .. 已连接 WiFi IP地址: 192.168.0.213 使用 SNTP 设置时间。 当前时间:2017年10月14日星期六02:26:25 正在尝试 MQTT 连接...已连接

这很好,这正是我想要的,但我对我的 Wemos D1 如何在不验证服务器证书链的情况下连接到端口 8883 感到困惑?请记住,我从未将证书上传到 Wemos D1 或在代码中实施证书,但它仍然可以连接。

【问题讨论】:

    标签: ssl mqtt esp8266


    【解决方案1】:

    两个选项之一

    1. WiFiClientSecure 包含一个公共 CA 证书列表,并正在根据此列表验证您的证书
    2. WiFiClientSecure 默认不验证远程证书。

    看看这个issue,看起来选项 2 最有可能,因为这意味着您必须在连接后自己验证证书。

    【讨论】:

    • 你是对的,我确实需要手动验证链。我使用了 HTTPSRequestCACert.ino 的示例代码,我首先使用 espClient.setCACert(caCert, caCertLen); 设置了根 CA 证书。然后使用 espClient.verifyCertChain(host);现在我尝试了不同的证书,由letsencrypt创建的chain.pem,fullchain.pem文件,根证书和letsencrypt网站上的中间件(letsencrypt.org/certificates)。但没有一个奏效。我使用此命令更改证书:xxd -i chain.pem ca_cert.h.
    猜你喜欢
    • 2021-03-13
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2020-10-12
    • 2010-11-08
    相关资源
    最近更新 更多