Skip to content

Commit a8e8ecb

Browse files
committed
WiFiClientSecure: add example of setCACert/verifyCertChain
Ref #1851
1 parent 79cfad5 commit a8e8ecb

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
const unsigned char caCert[] = {
3+
0x30, 0x82, 0x03, 0xc5, 0x30, 0x82, 0x02, 0xad, 0xa0, 0x03, 0x02, 0x01,
4+
0x02, 0x02, 0x10, 0x02, 0xac, 0x5c, 0x26, 0x6a, 0x0b, 0x40, 0x9b, 0x8f,
5+
0x0b, 0x79, 0xf2, 0xae, 0x46, 0x25, 0x77, 0x30, 0x0d, 0x06, 0x09, 0x2a,
6+
0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x6c,
7+
0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
8+
0x53, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0c,
9+
0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x49, 0x6e, 0x63,
10+
0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x10, 0x77,
11+
0x77, 0x77, 0x2e, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2e,
12+
0x63, 0x6f, 0x6d, 0x31, 0x2b, 0x30, 0x29, 0x06, 0x03, 0x55, 0x04, 0x03,
13+
0x13, 0x22, 0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x48,
14+
0x69, 0x67, 0x68, 0x20, 0x41, 0x73, 0x73, 0x75, 0x72, 0x61, 0x6e, 0x63,
15+
0x65, 0x20, 0x45, 0x56, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41,
16+
0x30, 0x1e, 0x17, 0x0d, 0x30, 0x36, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30,
17+
0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x31, 0x31, 0x31, 0x31,
18+
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x6c, 0x31, 0x0b,
19+
0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
20+
0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0c, 0x44, 0x69,
21+
0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x49, 0x6e, 0x63, 0x31, 0x19,
22+
0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x10, 0x77, 0x77, 0x77,
23+
0x2e, 0x64, 0x69, 0x67, 0x69, 0x63, 0x65, 0x72, 0x74, 0x2e, 0x63, 0x6f,
24+
0x6d, 0x31, 0x2b, 0x30, 0x29, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x22,
25+
0x44, 0x69, 0x67, 0x69, 0x43, 0x65, 0x72, 0x74, 0x20, 0x48, 0x69, 0x67,
26+
0x68, 0x20, 0x41, 0x73, 0x73, 0x75, 0x72, 0x61, 0x6e, 0x63, 0x65, 0x20,
27+
0x45, 0x56, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x30, 0x82,
28+
0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
29+
0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
30+
0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc6, 0xcc, 0xe5, 0x73, 0xe6,
31+
0xfb, 0xd4, 0xbb, 0xe5, 0x2d, 0x2d, 0x32, 0xa6, 0xdf, 0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0x55, 0xdb, 0x5e, 0x64, 0xe1, 0x37,
47+
0x87, 0x30, 0x56, 0x04, 0xe5, 0x11, 0xb4, 0x29, 0x80, 0x12, 0xf1, 0x79,
48+
0x39, 0x88, 0xa2, 0x02, 0x11, 0x7c, 0x27, 0x66, 0xb7, 0x88, 0xb7, 0x78,
49+
0xf2, 0xca, 0x0a, 0xa8, 0x38, 0xab, 0x0a, 0x64, 0xc2, 0xbf, 0x66, 0x5d,
50+
0x95, 0x84, 0xc1, 0xa1, 0x25, 0x1e, 0x87, 0x5d, 0x1a, 0x50, 0x0b, 0x20,
51+
0x12, 0xcc, 0x41, 0xbb, 0x6e, 0x0b, 0x51, 0x38, 0xb8, 0x4b, 0xcb, 0x02,
52+
0x03, 0x01, 0x00, 0x01, 0xa3, 0x63, 0x30, 0x61, 0x30, 0x0e, 0x06, 0x03,
53+
0x55, 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x01, 0x86,
54+
0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05,
55+
0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
56+
0x04, 0x16, 0x04, 0x14, 0xb1, 0x3e, 0xc3, 0x69, 0x03, 0xf8, 0xbf, 0x47,
57+
0x01, 0xd4, 0x98, 0x26, 0x1a, 0x08, 0x02, 0xef, 0x63, 0x64, 0x2b, 0xc3,
58+
0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
59+
0x14, 0xb1, 0x3e, 0xc3, 0x69, 0x03, 0xf8, 0xbf, 0x47, 0x01, 0xd4, 0x98,
60+
0x26, 0x1a, 0x08, 0x02, 0xef, 0x63, 0x64, 0x2b, 0xc3, 0x30, 0x0d, 0x06,
61+
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
62+
0x03, 0x82, 0x01, 0x01, 0x00, 0x1c, 0x1a, 0x06, 0x97, 0xdc, 0xd7, 0x9c,
63+
0x9f, 0x3c, 0x88, 0x66, 0x06, 0x08, 0x57, 0x21, 0xdb, 0x21, 0x47, 0xf8,
64+
0x2a, 0x67, 0xaa, 0xbf, 0x18, 0x32, 0x76, 0x40, 0x10, 0x57, 0xc1, 0x8a,
65+
0xf3, 0x7a, 0xd9, 0x11, 0x65, 0x8e, 0x35, 0xfa, 0x9e, 0xfc, 0x45, 0xb5,
66+
0x9e, 0xd9, 0x4c, 0x31, 0x4b, 0xb8, 0x91, 0xe8, 0x43, 0x2c, 0x8e, 0xb3,
67+
0x78, 0xce, 0xdb, 0xe3, 0x53, 0x79, 0x71, 0xd6, 0xe5, 0x21, 0x94, 0x01,
68+
0xda, 0x55, 0x87, 0x9a, 0x24, 0x64, 0xf6, 0x8a, 0x66, 0xcc, 0xde, 0x9c,
69+
0x37, 0xcd, 0xa8, 0x34, 0xb1, 0x69, 0x9b, 0x23, 0xc8, 0x9e, 0x78, 0x22,
70+
0x2b, 0x70, 0x43, 0xe3, 0x55, 0x47, 0x31, 0x61, 0x19, 0xef, 0x58, 0xc5,
71+
0x85, 0x2f, 0x4e, 0x30, 0xf6, 0xa0, 0x31, 0x16, 0x23, 0xc8, 0xe7, 0xe2,
72+
0x65, 0x16, 0x33, 0xcb, 0xbf, 0x1a, 0x1b, 0xa0, 0x3d, 0xf8, 0xca, 0x5e,
73+
0x8b, 0x31, 0x8b, 0x60, 0x08, 0x89, 0x2d, 0x0c, 0x06, 0x5c, 0x52, 0xb7,
74+
0xc4, 0xf9, 0x0a, 0x98, 0xd1, 0x15, 0x5f, 0x9f, 0x12, 0xbe, 0x7c, 0x36,
75+
0x63, 0x38, 0xbd, 0x44, 0xa4, 0x7f, 0xe4, 0x26, 0x2b, 0x0a, 0xc4, 0x97,
76+
0x69, 0x0d, 0xe9, 0x8c, 0xe2, 0xc0, 0x10, 0x57, 0xb8, 0xc8, 0x76, 0x12,
77+
0x91, 0x55, 0xf2, 0x48, 0x69, 0xd8, 0xbc, 0x2a, 0x02, 0x5b, 0x0f, 0x44,
78+
0xd4, 0x20, 0x31, 0xdb, 0xf4, 0xba, 0x70, 0x26, 0x5d, 0x90, 0x60, 0x9e,
79+
0xbc, 0x4b, 0x17, 0x09, 0x2f, 0xb4, 0xcb, 0x1e, 0x43, 0x68, 0xc9, 0x07,
80+
0x27, 0xc1, 0xd2, 0x5c, 0xf7, 0xea, 0x21, 0xb9, 0x68, 0x12, 0x9c, 0x3c,
81+
0x9c, 0xbf, 0x9e, 0xfc, 0x80, 0x5c, 0x9b, 0x63, 0xcd, 0xec, 0x47, 0xaa,
82+
0x25, 0x27, 0x67, 0xa0, 0x37, 0xf3, 0x00, 0x82, 0x7d, 0x54, 0xd7, 0xa9,
83+
0xf8, 0xe9, 0x2e, 0x13, 0xa3, 0x77, 0xe8, 0x1f, 0x4a
84+
};
85+
const unsigned int caCertLen = 969;
86+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
HTTP over TLS (HTTPS) example sketch
3+
4+
This example demonstrates how to use
5+
WiFiClientSecure class to connect to a TLS server.
6+
7+
This example verifies server certificate using the
8+
root CA certificate.
9+
10+
We fetch and display the status of
11+
esp8266/Arduino project continuous integration
12+
build.
13+
14+
Created by Ivan Grokhotkov, 2017.
15+
This example is in public domain.
16+
*/
17+
18+
#include <time.h>
19+
#include <ESP8266WiFi.h>
20+
#include <WiFiClientSecure.h>
21+
22+
const char* ssid = "........";
23+
const char* password = "........";
24+
25+
const char* host = "api.github.com";
26+
const int httpsPort = 443;
27+
28+
// Root certificate used by api.github.com.
29+
// Defined in "CACert" tab.
30+
extern const unsigned char caCert[];
31+
extern const unsigned int caCertLen;
32+
33+
WiFiClientSecure client;
34+
35+
void setup() {
36+
Serial.begin(115200);
37+
Serial.println();
38+
Serial.print("connecting to ");
39+
Serial.println(ssid);
40+
WiFi.begin(ssid, password);
41+
while (WiFi.status() != WL_CONNECTED) {
42+
delay(500);
43+
Serial.print(".");
44+
}
45+
Serial.println("");
46+
Serial.println("WiFi connected");
47+
Serial.println("IP address: ");
48+
Serial.println(WiFi.localIP());
49+
50+
// Synchronize time useing SNTP. This is necessary to verify that
51+
// the TLS certificates offered by the server are currently valid.
52+
Serial.print("Setting time using SNTP");
53+
configTime(8 * 3600, 0, "pool.ntp.org", "time.nist.gov");
54+
time_t now = time(nullptr);
55+
while (now < 1000) {
56+
delay(500);
57+
Serial.print(".");
58+
now = time(nullptr);
59+
}
60+
Serial.println("");
61+
struct tm timeinfo;
62+
gmtime_r(&now, &timeinfo);
63+
Serial.print("Current time: ");
64+
Serial.print(asctime(&timeinfo));
65+
66+
// Load root certificate in DER format into WiFiClientSecure object
67+
bool res = client.setCACert(caCert, caCertLen);
68+
if (!res) {
69+
Serial.println("Failed to load root CA certificate!");
70+
while (true) {
71+
yield();
72+
}
73+
}
74+
}
75+
76+
void loop() {
77+
// Connect to remote server
78+
Serial.print("connecting to ");
79+
Serial.println(host);
80+
if (!client.connect(host, httpsPort)) {
81+
Serial.println("connection failed");
82+
return;
83+
}
84+
85+
// Verify validity of server's certificate
86+
if (client.verifyCertChain(host)) {
87+
Serial.println("Server certificate verified");
88+
} else {
89+
Serial.println("ERROR: certificate verification failed!");
90+
return;
91+
}
92+
93+
String url = "/repos/esp8266/Arduino/commits/master/status";
94+
Serial.print("requesting URL: ");
95+
Serial.println(url);
96+
97+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
98+
"Host: " + host + "\r\n" +
99+
"User-Agent: BuildFailureDetectorESP8266\r\n" +
100+
"Connection: close\r\n\r\n");
101+
102+
Serial.println("request sent");
103+
while (client.connected()) {
104+
String line = client.readStringUntil('\n');
105+
if (line == "\r") {
106+
Serial.println("headers received");
107+
break;
108+
}
109+
}
110+
String line = client.readStringUntil('\n');
111+
if (line.startsWith("{\"state\":\"success\"")) {
112+
Serial.println("esp8266/Arduino CI successfull!");
113+
} else {
114+
Serial.println("esp8266/Arduino CI has failed");
115+
}
116+
Serial.println("reply was:");
117+
Serial.println("==========");
118+
Serial.println(line);
119+
Serial.println("==========");
120+
Serial.println();
121+
122+
static int repeat = 0;
123+
if (++repeat == 3) {
124+
Serial.println("Done");
125+
while (true) {
126+
delay(1000);
127+
}
128+
}
129+
delay(10000);
130+
}
131+
132+

0 commit comments

Comments
 (0)