0

我正在构建一个连接到 AWS IOT 平台的硬件设备。根据文档,aws iot 平台的身份验证是使用 TLS 完成的。我在授权访问的设备上有根 CA、客户端密钥和客户端证书文件。有没有办法在发出 POST 请求时在 HTTP 标头中使用这些文件?如果是这样,怎么做?到目前为止,这里是 Energia IDE(基于 Arduino IDE)和使用 WiFiClient 方法的代码。

if (client.sslConnect(aws_endpoint, 443))
{
  Serial.println("\nConnected to AWS endpoint");

  String PostData = "{\"value1\" : \"testValue\", \"value2\" : \"Hello\", \"value3\" : \"World!\" }";

  request = "POST /things/";
  request += thingname;
  request += "/shadow";
  request += " HTTP/1.1";
  Serial.print("Request:\t"); Serial.println(request);
  Serial.print("Post data:\t"); Serial.println(PostData);

  client.println(request);
  client.println("Host: ");
  client.println(aws_endpoint);
  client.println(":443");
  client.println("User-Agent: Energia/1.1");
  client.println("Connection: close");
  client.println("Content-Type: application/json");
  client.print("Content-Length: "); client.println(PostData.length());
  client.println();
  client.println(PostData);
  client.println();
}
else
{
  Serial.println("Connection failed");
}

Serial.println(); 
Serial.println("Server response:"); 
Serial.println(); 

// Capture response from the server. (10 second timeout)
long timeOut = 5000;
long lastTime = millis();

while((millis()-lastTime) < timeOut)
{ // Wait for incoming response from server
  while (client.available()) 
  { // Characters incoming from the server
    char c = client.read();            // Read characters
    Serial.write(c);
  }
}

但是,这会产生身份验证错误:

HTTP/1.1 403 Forbidden
content-type: application/json
content-length: 91
date: Tue, 26 Jul 2016 11:46:59 GMT
x-amzn-RequestId: 4d5388a9-e3c4-460a-b674-c3f971f3330d
connection: Keep-Alive
x-amzn-ErrorType: ForbiddenException:

{"message":"Missing Authentication Token","traceId":"4d5388a9-e3c4-460a-b674-c3f971f3330d"}
4

1 回答 1

2

TLS 客户端证书将作为您client.sslConnect()调用的一部分发送/使用,而不是作为 HTTP 请求的一部分。TLS 握手(以及客户端和服务器证书的交换/验证)发生在发送任何 HTTP 消息之前。

此 AWS 论坛帖子建议您可能需要为影子 API 使用端口 8443(而不是端口 443)。看起来 TLS 相互身份验证(通过证书)的使用/要求AWS SIGv4 标头的使用相比,是由 AWS IOT 根据使用的端口确定的。

希望这可以帮助!

于 2016-07-26T19:29:40.633 回答