我正在构建连接到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"}

最佳答案

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

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

希望这可以帮助!

07-28 02:58
查看更多