// Example of the different modes of the X.509 validation options // in the WiFiClientBearSSL object // // Mar 2018 by Earle F. Philhower, III // Released to the public domain #include #include const char *ssid = "...."; const char *pass = "...."; const char * host = "api.github.com"; const uint16_t port = 443; const char * path = "/"; // Set time via NTP, as required for x.509 validation void setClock() { configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); Serial.print("Waiting for NTP time sync: "); time_t now = time(nullptr); while (now < 8 * 3600 * 2) { delay(500); Serial.print("."); now = time(nullptr); } Serial.println(""); struct tm timeinfo; gmtime_r(&now, &timeinfo); Serial.print("Current time: "); Serial.print(asctime(&timeinfo)); } // Try and connect using a WiFiClientBearSSL to specified host:port and dump HTTP response void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) { if (!path) { path = "/"; } Serial.printf("Trying: %s:443...", host); client->connect(host, port); if (!client->connected()) { Serial.printf("*** Can't connect. ***\n-------\n"); return; } Serial.printf("Connected!\n-------\n"); client->write("GET "); client->write(path); client->write(" HTTP/1.0\r\nHost: "); client->write(host); client->write("\r\nUser-Agent: ESP8266\r\n"); client->write("\r\n"); uint32_t to = millis() + 5000; if (client->connected()) { do { char tmp[32]; memset(tmp, 0, 32); int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1); yield(); if (rlen < 0) { break; } // Only print out first line up to \r, then abort connection char *nl = strchr(tmp, '\r'); if (nl) { *nl = 0; Serial.print(tmp); break; } Serial.print(tmp); } while (millis() < to); } client->stop(); Serial.printf("\n-------\n\n"); } void fetchNoConfig() { Serial.printf(R"EOF( If there are no CAs or insecure options specified, BearSSL will not connect. Expect the following call to fail as none have been configured. )EOF"); BearSSL::WiFiClientSecure client; fetchURL(&client, host, port, path); } void fetchInsecure() { Serial.printf(R"EOF( This is absolutely *insecure*, but you can tell BearSSL not to check the certificate of the server. In this mode it will accept ANY certificate, which is subject to man-in-the-middle (MITM) attacks. )EOF"); BearSSL::WiFiClientSecure client; client.setInsecure(); fetchURL(&client, host, port, path); } void fetchFingerprint() { Serial.printf(R"EOF( The SHA-1 fingerprint of an X.509 certificate can be used to validate it instead of the while certificate. This is not nearly as secure as real X.509 validation, but is better than nothing. )EOF"); BearSSL::WiFiClientSecure client; const uint8_t fp[20] = {0x5F, 0xF1, 0x60, 0x31, 0x09, 0x04, 0x3E, 0xF2, 0x90, 0xD2, 0xB0, 0x8A, 0x50, 0x38, 0x04, 0xE8, 0x37, 0x9F, 0xBC, 0x76}; client.setFingerprint(fp); fetchURL(&client, host, port, path); } void fetchSelfSigned() { Serial.printf(R"EOF( It is also possible to accept *any* self-signed certificate. This is absolutely insecure as anyone can make a self-signed certificate. )EOF"); BearSSL::WiFiClientSecure client; Serial.printf("First, try and connect to a badssl.com self-signed website (will fail):\n"); fetchURL(&client, "self-signed.badssl.com", 443, "/"); Serial.printf("Now we'll enable self-signed certs (will pass)\n"); client.allowSelfSignedCerts(); fetchURL(&client, "self-signed.badssl.com", 443, "/"); } void fetchKnownKey() { Serial.printf(R"EOF( The server certificate can be completely ignored and its public key hardcoded in your application. This should be secure as the public key needs to be paired with the private key of the site, which is obviously private and not shared. A MITM without the private key would not be able to establish communications. )EOF"); // Extracted by: openssl x509 -pubkey -noout -in servercert.pem static const char pubkey[] PROGMEM = R"KEY( -----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqcCPMEktuxLoDAxdHQgI 95FweH4Fa6+LslU2qmmUBF+pu4ZOUvpIQxVU5wqdWaxZauxG1nYUTrAWdPb1n0um gLsGE7WYXJnQPJewIK4Qhua0LsrirIdHkcwHQ83NEYj+lswhg0fUQURt06Uta5ak LovDdJPLqTuTS/nshOa76hR0ouWnrqucLL1szcvX/obB+Nsbmr58Mrg8prQfRoK6 ibzlZysV88qPcCpc57lq6QBKQ2F9WgQMssQigXfTNm8lAAQ+L6gCZngd4KfHYPSJ YA07oFWmuSOalgh00Wh8PUjuRGrcNxWpmgfALQHHFYgoDcD+a8+GoJk+GdJd3ong ZQIDAQAB -----END PUBLIC KEY----- )KEY"; BearSSL::WiFiClientSecure client; BearSSLPublicKey key(pubkey); client.setKnownKey(&key); fetchURL(&client, host, port, path); } void fetchCertAuthority() { static const char digicert[] PROGMEM = R"EOF( -----BEGIN CERTIFICATE----- MIIDxTCCAq2gAwIBAgIQAqxcJmoLQJuPC3nyrkYldzANBgkqhkiG9w0BAQUFADBs MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3 d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5j ZSBFViBSb290IENBMB4XDTA2MTExMDAwMDAwMFoXDTMxMTExMDAwMDAwMFowbDEL MAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UECxMQd3d3 LmRpZ2ljZXJ0LmNvbTErMCkGA1UEAxMiRGlnaUNlcnQgSGlnaCBBc3N1cmFuY2Ug RVYgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbM5XPm +9S75S0tMqbf5YE/yc0lSbZxKsPVlDRnogocsF9ppkCxxLeyj9CYpKlBWTrT3JTW PNt0OKRKzE0lgvdKpVMSOO7zSW1xkX5jtqumX8OkhPhPYlG++MXs2ziS4wblCJEM xChBVfvLWokVfnHoNb9Ncgk9vjo4UFt3MRuNs8ckRZqnrG0AFFoEt7oT61EKmEFB Ik5lYYeBQVCmeVyJ3hlKV9Uu5l0cUyx+mM0aBhakaHPQNAQTXKFx01p8VdteZOE3 hzBWBOURtCmAEvF5OYiiAhF8J2a3iLd48soKqDirCmTCv2ZdlYTBoSUeh10aUAsg EsxBu24LUTi4S8sCAwEAAaNjMGEwDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQF MAMBAf8wHQYDVR0OBBYEFLE+w2kD+L9HAdSYJhoIAu9jZCvDMB8GA1UdIwQYMBaA FLE+w2kD+L9HAdSYJhoIAu9jZCvDMA0GCSqGSIb3DQEBBQUAA4IBAQAcGgaX3Nec nzyIZgYIVyHbIUf4KmeqvxgydkAQV8GK83rZEWWONfqe/EW1ntlMMUu4kehDLI6z eM7b41N5cdblIZQB2lWHmiRk9opmzN6cN82oNLFpmyPInngiK3BD41VHMWEZ71jF hS9OMPagMRYjyOfiZRYzy78aG6A9+MpeizGLYAiJLQwGXFK3xPkKmNEVX58Svnw2 Yzi9RKR/5CYrCsSXaQ3pjOLAEFe4yHYSkVXySGnYvCoCWw9E1CAx2/S6cCZdkGCe vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep +OkuE6N36B9K -----END CERTIFICATE----- )EOF"; Serial.printf(R"EOF( A specific certification authority can be passed in and used to validate a chain of certificates from a given server. These will be validated using BearSSL's rules, which do NOT include certificate revocation lists. A specific server's certificate, or your own self-signed root certificate can also be used. ESP8266 time needs to be valid for checks to pass as BearSSL does verify the notValidBefore/After fields. )EOF"); BearSSL::WiFiClientSecure client; BearSSLX509List cert(digicert); client.setTrustAnchors(&cert); Serial.printf("Try validating without setting the time (should fail)\n"); fetchURL(&client, host, port, path); Serial.printf("Try again after setting NTP time (should pass)\n"); setClock(); fetchURL(&client, host, port, path); } void setup() { Serial.begin(115200); Serial.println(); Serial.println(); // We start by connecting to a WiFi network Serial.print("Connecting to "); Serial.println(ssid); WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); fetchNoConfig(); fetchInsecure(); fetchFingerprint(); fetchSelfSigned(); fetchKnownKey(); fetchCertAuthority(); } void loop() { // Nothing to do here }