88
99#include < WiFiClientSecure.h>
1010
11- char ssid[] = " your_network_name " ; // your network SSID (name of wifi network)
12- char pass[] = " your_password " ; // your network password
11+ const char * ssid = " your-ssid " ; // your network SSID (name of wifi network)
12+ const char * password = " your-password " ; // your network password
1313
14- char server[] = " www.howsmyssl.com" ; // Server URL
15- // You can use x.509 certificates if you want
16- // unsigned char test_ca_cert[] = ""; //For the usage of verifying server
17- // unsigned char test_client_key[] = ""; //For the usage of verifying client
18- // unsigned char test_client_cert[] = ""; //For the usage of verifying client
14+ const char * server = " www.howsmyssl.com" ; // Server URL
15+
16+ // www.howsmyssl.com CA certificate, to verify the server
17+ // change it to your server CA certificate
18+ // SHA1 fingerprint is broken now!
19+
20+ const char * test_ca_cert = \
21+ " -----BEGIN CERTIFICATE-----\n " \
22+ " MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n " \
23+ " MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n " \
24+ " DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n " \
25+ " SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n " \
26+ " GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n " \
27+ " AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n " \
28+ " q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n " \
29+ " SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n " \
30+ " Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n " \
31+ " a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n " \
32+ " /PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n " \
33+ " AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n " \
34+ " CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n " \
35+ " bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n " \
36+ " c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n " \
37+ " VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n " \
38+ " ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n " \
39+ " MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n " \
40+ " Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n " \
41+ " AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n " \
42+ " uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n " \
43+ " wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n " \
44+ " X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n " \
45+ " PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n " \
46+ " KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n " \
47+ " -----END CERTIFICATE-----\n " ;
48+
49+ // You can use x.509 client certificates if you want
50+ // const char* test_client_key = ""; //to verify the client
51+ // const char* test_client_cert = ""; //to verify the client
1952
2053
2154WiFiClientSecure client;
@@ -27,7 +60,7 @@ void setup() {
2760
2861 Serial.print (" Attempting to connect to SSID: " );
2962 Serial.println (ssid);
30- WiFi.begin (ssid, pass );
63+ WiFi.begin (ssid, password );
3164
3265 // attempt to connect to Wifi network:
3366 while (WiFi.status () != WL_CONNECTED) {
@@ -38,39 +71,43 @@ void setup() {
3871
3972 Serial.print (" Connected to " );
4073 Serial.println (ssid);
74+
75+ client.setCACert (test_ca_cert);
76+ // client.setCertificate(certificateBuff); // for client verification
77+ // client.setPrivateKey(privateKeyBuff); // for client verification
4178
4279 Serial.println (" \n Starting connection to server..." );
43- if (client.connect (server, 443 )) { // client.connect(server, 443, test_ca_cert, test_client_cert, test_client_key)
80+ if (!client.connect (server, 443 ))
81+ Serial.println (" Connection failed!" );
82+ else {
4483 Serial.println (" Connected to server!" );
4584 // Make a HTTP request:
4685 client.println (" GET https://www.howsmyssl.com/a/check HTTP/1.0" );
4786 client.println (" Host: www.howsmyssl.com" );
4887 client.println (" Connection: close" );
4988 client.println ();
50- }
51- else
52- Serial.println (" Connection failed!" );
5389
5490Serial.print (" Waiting for response " ); // WiFiClientSecure uses a non blocking implementation
5591while (!client.available ()){
5692delay (50 ); //
5793Serial.print (" ." );
5894}
59- // if there are incoming bytes available
60- // from the server, read them and print them:
61- while (client.available ()) {
62- char c = client.read ();
63- Serial.write (c);
64- }
95+ // if there are incoming bytes available
96+ // from the server, read them and print them:
97+ while (client.available ()) {
98+ char c = client.read ();
99+ Serial.write (c);
100+ }
65101
66- // if the server's disconnected, stop the client:
67- if (!client.connected ()) {
68- Serial.println ();
69- Serial.println (" disconnecting from server." );
70- client.stop ();
102+ // if the server's disconnected, stop the client:
103+ if (!client.connected ()) {
104+ Serial.println ();
105+ Serial.println (" disconnecting from server." );
106+ client.stop ();
107+ }
71108 }
72109}
73110
74111void loop () {
75112 // do nothing
76- }
113+ }
0 commit comments