Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTPClient fails with https but WiFiClientSecure works #2783

Closed
torntrousers opened this issue Dec 21, 2016 · 3 comments
Closed

HTTPClient fails with https but WiFiClientSecure works #2783

torntrousers opened this issue Dec 21, 2016 · 3 comments
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.

Comments

@torntrousers
Copy link
Contributor

Trying to use HTTPClient to do an HTTP POST with TLS 1.2 fails but it works ok when using WiFiClientSecure.

This is with the latest github code as at 21st Dec 2016.

This sketch demostrates:

`
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

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

String urlHost = "quickstart.messaging.internetofthings.ibmcloud.com";
String urlPath = "/api/v0002/device/types/typeId/devices/myDevice1/events/eventId";
int urlPort = 8883; // or 1883 for non-secure

void setup() {
Serial.begin(115200); Serial.println();
initWifi();
}

void loop() {
doPost1();
doPost2();
delay(10000);
}

void doPost1() {
Serial.println("*** HTTPClient ***");
HTTPClient http;
String url = (urlPort == 8883 ? "https://" : "http://") + urlHost + ":" + urlPort + urlPath;
Serial.println(url);
String payload = String("{ "d": {"aMessage": "") + millis()/1000 + ""} }";
Serial.print("POST payload: "); Serial.println(payload);
http.begin(url, payload);
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(payload);
Serial.print("HTTP POST Response: "); Serial.println(httpCode);
}

void doPost2() {
Serial.println("*** WiFiClientSecure ***");
WiFiClientSecure client;

Serial.print("connect: "); Serial.println(urlHost);
while (!client.connect(urlHost.c_str(), 8883)) {
Serial.print(".");
}
Serial.println("Connected");

String postData = String("{ "d": {"aMessage": "") + millis()/1000 + ""} }";

String msg = "POST " + urlPath + " HTTP/1.1\r\n"
"Host: " + urlHost + "\r\n"
"Content-Type: application/json\r\n"
"Content-Length: " + postData.length() + "\r\n"
"\r\n" + postData;

client.print(msg);
Serial.print(msg);

Serial.print("\n*** Request sent, receiving response...");
while (!!!client.available()) {
delay(50);
Serial.print(".");
}
Serial.println();
Serial.println("Got response");

// Read all the lines of the reply from server and print them to Serial
while(client.available()){
Serial.write(client.read());
}

Serial.println();
Serial.println("closing connection");
client.stop();
}

void initWifi() {
Serial.print("Connecting to: "); Serial.print(WiFi.SSID());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected, IP address: "); Serial.println(WiFi.localIP());
}
`

@devyte
Copy link
Collaborator

devyte commented Oct 5, 2017

@torntrousers is this issue still valid with latest git?

@devyte devyte added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Oct 5, 2017
@igrr
Copy link
Member

igrr commented Oct 7, 2017

I think the line

http.begin(url, payload);

is the problem here.

Please have a look at the overloads of begin method:

    bool begin(String url);
    bool begin(String url, String httpsFingerprint);
    bool begin(String host, uint16_t port, String uri = "/");
    bool begin(String host, uint16_t port, String uri, String httpsFingerprint);

Note that the overload which takes two strings has 'httpsFingerprint' as the second argument, not 'payload'.

The connection fails when HTTPClient is trying to verify certificate fingerprint. It compares the real fingerprint to the contents of your 'payload' and obviously finds no match, hence terminates the connection.

@devyte
Copy link
Collaborator

devyte commented May 29, 2018

Per previous comment, closing as user error.
Also, BearSSL is merged in #4273 , with alternate BearSSL::WiFi* classes. Although axtls-based classes are still available and even the default, they are planned for deprecation and then retirement, hence won't be fixed. Any issues with BearSSL-based classes should be reported in new issues.

@devyte devyte closed this as completed May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

No branches or pull requests

3 participants