|
| 1 | +// WiFiClientShowPeerCredentials |
| 2 | +// |
| 3 | +// Example of a establishing a secure connection and then |
| 4 | +// showing the fingerprint of the certificate. This can |
| 5 | +// be useful in an IoT setting to know for sure that you |
| 6 | +// are connecting to the right server. Especally in |
| 7 | +// situations where you cannot hardcode a trusted root |
| 8 | +// certificate for long periods of time (as they tend to |
| 9 | +// get replaced more often than the lifecycle of IoT |
| 10 | +// hardware). |
| 11 | +// |
| 12 | + |
| 13 | +#include <WiFi.h> |
| 14 | +#include <HTTPClient.h> |
| 15 | +#include <WiFiClientSecure.h> |
| 16 | + |
| 17 | +#ifndef WIFI_NETWORK |
| 18 | +#define WIFI_NETWORK "MyWifiNetwork" |
| 19 | +#endif |
| 20 | + |
| 21 | +#ifndef WIFI_PASSWD |
| 22 | +#define WIFI_PASSWD "MySecretWifiPassword" |
| 23 | +#endif |
| 24 | + |
| 25 | +#define URL "https://arduino.cc" |
| 26 | + |
| 27 | +void demo() { |
| 28 | + WiFiClientSecure *client = new WiFiClientSecure; |
| 29 | + client->setInsecure(); // |
| 30 | + |
| 31 | + HTTPClient https; |
| 32 | + if (!https.begin(*client, URL )) { |
| 33 | + Serial.println("HTTPS setup failed"); |
| 34 | + return; |
| 35 | + }; |
| 36 | + |
| 37 | + https.setTimeout(5000); |
| 38 | + |
| 39 | + int httpCode = https.GET(); |
| 40 | + if (httpCode != 200) { |
| 41 | + Serial.print("Connect failed: "); |
| 42 | + Serial.println(https.errorToString(httpCode)); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + const mbedtls_x509_crt* peer = client->getPeerCertificate(); |
| 47 | + |
| 48 | + // Show general output / certificate information |
| 49 | + // |
| 50 | + char buf[1024]; |
| 51 | + int l = mbedtls_x509_crt_info (buf, sizeof(buf), "", peer); |
| 52 | + if (l <= 0) { |
| 53 | + Serial.println("Peer conversion to printable buffer failed"); |
| 54 | + return; |
| 55 | + }; |
| 56 | + Serial.println(); |
| 57 | + Serial.println(buf); |
| 58 | + |
| 59 | + uint8_t fingerprint_remote[32]; |
| 60 | + if (!client->getFingerprintSHA256(fingerprint_remote)) { |
| 61 | + Serial.println("Failed to get the fingerprint"); |
| 62 | + return; |
| 63 | + } |
| 64 | + // Fingerprint late 2021 |
| 65 | + Serial.println("Expecting Fingerprint (SHA256): 70 CF A4 B7 5D 09 E9 2A 52 A8 B6 85 B5 0B D6 BE 83 47 83 5B 3A 4D 3C 3E 32 30 EC 1D 61 98 D7 0F"); |
| 66 | + Serial.print( " Received Fingerprint (SHA256): "); |
| 67 | + |
| 68 | + for (int i = 0; i < 32; i++) { |
| 69 | + Serial.print(fingerprint_remote[i], HEX); |
| 70 | + Serial.print(" "); |
| 71 | + }; |
| 72 | + Serial.println(""); |
| 73 | +}; |
| 74 | + |
| 75 | +void setup() { |
| 76 | + Serial.begin(115200); |
| 77 | + Serial.println("Started " __FILE__ " build " __DATE__ " " __TIME__); |
| 78 | + |
| 79 | + WiFi.mode(WIFI_STA); |
| 80 | + WiFi.begin(WIFI_NETWORK, WIFI_PASSWD); |
| 81 | + |
| 82 | + while (WiFi.waitForConnectResult() != WL_CONNECTED) { |
| 83 | + Serial.println("Wifi fail - rebooting"); |
| 84 | + delay(5000); |
| 85 | + ESP.restart(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void loop() { |
| 90 | + bool already_tried = false; |
| 91 | + if ((millis() < 1000) || already_tried) |
| 92 | + return; |
| 93 | + already_tried = true; |
| 94 | + |
| 95 | + // Run the test just once. |
| 96 | + demo(); |
| 97 | +} |
0 commit comments