Skip to content

Commit 3c3fe84

Browse files
committed
Add HelloServerSecure example (#2)
1 parent f0ea119 commit 3c3fe84

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

.github/workflows/build-examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
example:
1515
- HelloServer
1616
- FormServer
17+
- HelloServerSecure
1718
board:
1819
- wrover
1920
- wroom
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* This example shows how you use the server with a secure TLS connection.
3+
*
4+
* It is basically the same code as the HelloServer example, and changes
5+
* are marked with an "TLS" prefix in the comment.
6+
*
7+
* To run the example, you need a certificate and a private key. If you're
8+
* not familiar with these terms, you may need to look up how Transport
9+
* Layer Security (TLS) works. Basically you have two keys, a public and
10+
* a private key. Simplified, you can encrypt data with one of the keys and
11+
* only decrypt it with the _other_ key. So while the public key can be
12+
* shared with everybody (therefore public), and it is part of the certificate,
13+
* it is crucial that the private keys remains only on the server.
14+
* For this very reason you MUST NOT USE THE KEYS PROVIDED WITH THIS EXAMPLE
15+
* for any of your projects. No, not even private ones in your own network.
16+
* Using this example key gives you the same level of security that you would
17+
* have without encryption. You have been warned.
18+
*
19+
* While we include the keys from two additional files "cert.h" and
20+
* "private_key.h", you are free to store the key whereever you want, e.g. in
21+
* the SPIFFS file system. You only need to be able to load it into a byte
22+
* array and pass it to the web server.
23+
*
24+
* All keys have to be in the DER format, and only RSA keys are supported.
25+
*/
26+
27+
// Header file for WiFi
28+
#include <WiFi.h>
29+
30+
// TLS: Header file for the server. Note that we use the ...Secure.hpp version
31+
#include <ESPWebServerSecure.hpp>
32+
33+
// TLS: We need the private key and certificate from the external files
34+
// WARNING: NEVER USE THE FILES FROM THE EXAMPLE FOR MORE THAN TESTING.
35+
// AS THE PRIVATE KEY IS PART OF THE REPOSITORY, THIS MEANS YOU
36+
// HAVE BASICALLY THE SAME LEVEL OF SECURITY AS WITH PLAIN
37+
// HTTP.
38+
// See the repository of the underlying esp32_https_server for
39+
// details on how to generate your own:
40+
// https://github.com/fhessel/esp32_https_server/tree/master/extras#create_certsh
41+
#include "cert.h"
42+
#include "private_key.h"
43+
44+
// TODO: Configure your WiFi here
45+
const char* ssid = ".............";
46+
const char* password = "......................";
47+
48+
ESPWebServerSecure server(443);
49+
50+
const int led = 13;
51+
52+
void handleRoot() {
53+
digitalWrite(led, 1);
54+
server.send(200, "text/plain", "hello from esp32! See /form and /inline too!");
55+
digitalWrite(led, 0);
56+
}
57+
58+
void handleForm() {
59+
String line = server.arg("line");
60+
Serial.print("line: ");
61+
Serial.println(line);
62+
String multi = server.arg("multi");
63+
Serial.print("multi: ");
64+
Serial.println(multi);
65+
line.toLowerCase();
66+
multi.toUpperCase();
67+
String rv;
68+
rv = "<html><head><title>Test Form</title></head><body>";
69+
rv += "<p>Single line will be converted to lowercase, multi line to uppercase. You can submit the form with three different methods.</p>";
70+
rv += "<h2>Form using GET</h2>";
71+
rv += "<form method='GET'>";
72+
rv += "Single line:<br><input name='line' value='" + line + "'><br>";
73+
rv += "Multi line:<br><textarea name='multi' rows='8' cols='40'>" + multi + "</textarea><br>";
74+
rv += "<input type='submit' value='upper+lower case'>";
75+
rv += "</form>";
76+
rv += "<h2>Form using POST urlencoded</h2>";
77+
rv += "<form method='POST' action='/form'>";
78+
rv += "Single line:<br><input name='line' value='" + line + "'><br>";
79+
rv += "Multi line:<br><textarea name='multi' rows='8' cols='40'>" + multi + "</textarea><br>";
80+
rv += "<input type='submit' value='upper+lower case'>";
81+
rv += "</form>";
82+
rv += "<h2>Form using POST with multipart</h2>";
83+
rv += "<form method='POST' enctype=\"multipart/form-data\">";
84+
rv += "Single line:<br><input name='line' value='" + line + "'><br>";
85+
rv += "Multi line:<br><textarea name='multi' rows='8' cols='40'>" + multi + "</textarea><br>";
86+
rv += "<input type='submit' value='upper+lower case'>";
87+
rv += "</form></body></html>";
88+
server.send(200, "text/html", rv);
89+
}
90+
91+
92+
void handleNotFound() {
93+
digitalWrite(led, 1);
94+
String message = "File Not Found\n\n";
95+
message += "URI: ";
96+
message += server.uri();
97+
message += "\nMethod: ";
98+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
99+
message += "\nArguments: ";
100+
message += server.args();
101+
message += "\n";
102+
for (uint8_t i = 0; i < server.args(); i++) {
103+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
104+
}
105+
server.send(404, "text/plain", message);
106+
digitalWrite(led, 0);
107+
}
108+
109+
void setup(void) {
110+
pinMode(led, OUTPUT);
111+
digitalWrite(led, 0);
112+
Serial.begin(115200);
113+
WiFi.mode(WIFI_STA);
114+
WiFi.begin(ssid, password);
115+
Serial.println("");
116+
117+
// Wait for connection
118+
while (WiFi.status() != WL_CONNECTED) {
119+
delay(500);
120+
Serial.print(".");
121+
}
122+
Serial.println("");
123+
Serial.print("Connected to ");
124+
Serial.println(ssid);
125+
Serial.print("IP address: ");
126+
Serial.println(WiFi.localIP());
127+
128+
// TLS: Before configuring the server, you need to set the certificate
129+
server.setServerKeyAndCert(
130+
example_key_der, // Raw DER key data as byte array
131+
example_key_der_len, // Length of the key array
132+
example_der, // Raw DER certificate (no certificate chain!) as byte array
133+
example_der_len // Length of the certificate array
134+
);
135+
136+
server.on("/", handleRoot);
137+
server.on("/form", handleForm);
138+
server.on("/form", HTTP_POST, handleForm);
139+
server.on("/inline", []() {
140+
server.send(200, "text/plain", "this works as well");
141+
});
142+
143+
server.onNotFound(handleNotFound);
144+
145+
server.begin();
146+
Serial.println("HTTPS server started");
147+
}
148+
149+
void loop(void) {
150+
server.handleClient();
151+
}

examples/HelloServerSecure/cert.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#ifndef CERT_H
2+
#define CERT_H
3+
4+
// TEST CERT. DO NOT USE FOR PRODUCTION
5+
6+
unsigned char example_der[] = {
7+
0x30, 0x82, 0x02, 0x18, 0x30, 0x82, 0x01, 0x81, 0x02, 0x01, 0x02, 0x30,
8+
0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
9+
0x05, 0x00, 0x30, 0x54, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
10+
0x06, 0x13, 0x02, 0x44, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
11+
0x04, 0x08, 0x0c, 0x02, 0x42, 0x45, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
12+
0x55, 0x04, 0x07, 0x0c, 0x06, 0x42, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x31,
13+
0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x4d, 0x79,
14+
0x43, 0x6f, 0x6d, 0x70, 0x61, 0x6e, 0x79, 0x31, 0x13, 0x30, 0x11, 0x06,
15+
0x03, 0x55, 0x04, 0x03, 0x0c, 0x0a, 0x6d, 0x79, 0x63, 0x61, 0x2e, 0x6c,
16+
0x6f, 0x63, 0x61, 0x6c, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x36,
17+
0x30, 0x36, 0x32, 0x31, 0x34, 0x33, 0x35, 0x33, 0x5a, 0x17, 0x0d, 0x33,
18+
0x30, 0x30, 0x36, 0x30, 0x34, 0x32, 0x31, 0x34, 0x33, 0x35, 0x33, 0x5a,
19+
0x30, 0x55, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
20+
0x02, 0x44, 0x45, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x08,
21+
0x0c, 0x02, 0x42, 0x45, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04,
22+
0x07, 0x0c, 0x06, 0x42, 0x65, 0x72, 0x6c, 0x69, 0x6e, 0x31, 0x12, 0x30,
23+
0x10, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x09, 0x4d, 0x79, 0x43, 0x6f,
24+
0x6d, 0x70, 0x61, 0x6e, 0x79, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
25+
0x04, 0x03, 0x0c, 0x0b, 0x65, 0x73, 0x70, 0x33, 0x32, 0x2e, 0x6c, 0x6f,
26+
0x63, 0x61, 0x6c, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
27+
0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d,
28+
0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xce, 0xe6, 0x25, 0x6f,
29+
0x3c, 0xfb, 0xda, 0x38, 0x2b, 0xd7, 0x62, 0xcc, 0x04, 0x9c, 0x58, 0xc8,
30+
0xc8, 0x91, 0x14, 0x93, 0xb4, 0x0e, 0xa4, 0x09, 0x78, 0xd8, 0xcb, 0xfe,
31+
0x13, 0x30, 0x6d, 0xd8, 0x22, 0x21, 0xf6, 0xdf, 0x7e, 0xc3, 0xc6, 0x92,
32+
0x2f, 0xc6, 0x84, 0x43, 0xff, 0xb4, 0xa1, 0x6b, 0x13, 0x5f, 0x36, 0xa7,
33+
0xc3, 0x99, 0x32, 0x03, 0x89, 0x50, 0x30, 0x1e, 0xad, 0xc6, 0x36, 0xe7,
34+
0x73, 0x48, 0x09, 0x28, 0x4f, 0xb9, 0x46, 0xbf, 0xdd, 0x76, 0x10, 0xc3,
35+
0x07, 0x33, 0x9b, 0xd0, 0x8d, 0xb2, 0x24, 0xd4, 0xcb, 0x2e, 0x90, 0x06,
36+
0xbf, 0xf1, 0xfa, 0xae, 0x06, 0x5c, 0xec, 0x5d, 0xe8, 0x61, 0x06, 0x4a,
37+
0x3a, 0x2e, 0x2b, 0x1e, 0x60, 0xf2, 0xc4, 0x09, 0xca, 0xe6, 0x27, 0x64,
38+
0x31, 0x9c, 0xbd, 0x2d, 0x3a, 0x56, 0x27, 0x6d, 0x23, 0x67, 0x21, 0x11,
39+
0x6f, 0x50, 0xca, 0x11, 0x02, 0x03, 0x01, 0x00, 0x01, 0x30, 0x0d, 0x06,
40+
0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
41+
0x03, 0x81, 0x81, 0x00, 0x82, 0x74, 0x62, 0xfb, 0xce, 0xca, 0xee, 0xfb,
42+
0x01, 0x16, 0x91, 0xf8, 0x5d, 0x56, 0xb5, 0x70, 0x6e, 0xc9, 0x12, 0x78,
43+
0x5f, 0x9f, 0xbd, 0x16, 0x13, 0xe3, 0x23, 0xa0, 0x2d, 0x52, 0x02, 0xad,
44+
0x08, 0xd5, 0x7b, 0xcb, 0xc3, 0x5a, 0x13, 0x4f, 0x3d, 0x1c, 0x93, 0x95,
45+
0x2b, 0x61, 0xf9, 0xe6, 0xa2, 0x62, 0xb7, 0x54, 0x1f, 0x06, 0x65, 0xe2,
46+
0x30, 0x54, 0xf6, 0x72, 0x4b, 0x87, 0xe8, 0xb7, 0x34, 0xee, 0xad, 0x12,
47+
0x90, 0x30, 0xf7, 0x13, 0x36, 0x4e, 0x32, 0xb1, 0x06, 0xf3, 0xfa, 0x37,
48+
0x8b, 0x8c, 0xb7, 0x30, 0x2a, 0x04, 0x3a, 0x47, 0xd5, 0x99, 0x67, 0x06,
49+
0x42, 0x40, 0x41, 0xbe, 0xbb, 0x59, 0x48, 0xcb, 0xe7, 0xef, 0x1c, 0xed,
50+
0x22, 0x1a, 0xe8, 0x25, 0x83, 0x7f, 0x3d, 0x40, 0x05, 0x8d, 0x5b, 0x0b,
51+
0x6a, 0x69, 0x2b, 0xea, 0x4b, 0xf4, 0xd4, 0x88, 0xdb, 0xd2, 0xcf, 0x7e
52+
};
53+
unsigned int example_der_len = 540;
54+
55+
#endif //CERT_H
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef PRIVATE_KEY_H
2+
#define PRIVATE_KEY_H
3+
4+
// TEST KEY. DO NOT USE FOR PRODUCTION
5+
6+
unsigned char example_key_der[] = {
7+
0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xce,
8+
0xe6, 0x25, 0x6f, 0x3c, 0xfb, 0xda, 0x38, 0x2b, 0xd7, 0x62, 0xcc, 0x04,
9+
0x9c, 0x58, 0xc8, 0xc8, 0x91, 0x14, 0x93, 0xb4, 0x0e, 0xa4, 0x09, 0x78,
10+
0xd8, 0xcb, 0xfe, 0x13, 0x30, 0x6d, 0xd8, 0x22, 0x21, 0xf6, 0xdf, 0x7e,
11+
0xc3, 0xc6, 0x92, 0x2f, 0xc6, 0x84, 0x43, 0xff, 0xb4, 0xa1, 0x6b, 0x13,
12+
0x5f, 0x36, 0xa7, 0xc3, 0x99, 0x32, 0x03, 0x89, 0x50, 0x30, 0x1e, 0xad,
13+
0xc6, 0x36, 0xe7, 0x73, 0x48, 0x09, 0x28, 0x4f, 0xb9, 0x46, 0xbf, 0xdd,
14+
0x76, 0x10, 0xc3, 0x07, 0x33, 0x9b, 0xd0, 0x8d, 0xb2, 0x24, 0xd4, 0xcb,
15+
0x2e, 0x90, 0x06, 0xbf, 0xf1, 0xfa, 0xae, 0x06, 0x5c, 0xec, 0x5d, 0xe8,
16+
0x61, 0x06, 0x4a, 0x3a, 0x2e, 0x2b, 0x1e, 0x60, 0xf2, 0xc4, 0x09, 0xca,
17+
0xe6, 0x27, 0x64, 0x31, 0x9c, 0xbd, 0x2d, 0x3a, 0x56, 0x27, 0x6d, 0x23,
18+
0x67, 0x21, 0x11, 0x6f, 0x50, 0xca, 0x11, 0x02, 0x03, 0x01, 0x00, 0x01,
19+
0x02, 0x81, 0x80, 0x7b, 0x28, 0x2e, 0x12, 0x58, 0x27, 0xc6, 0xce, 0xf6,
20+
0xf1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0x64, 0x9a, 0xdc, 0xe0, 0xb8, 0x83, 0xc0,
50+
0x2e, 0xf6, 0x5f, 0x84, 0x83, 0xf4, 0x1b, 0xfa, 0x9c, 0xc2, 0xcb, 0x1c,
51+
0xb5, 0x49, 0x12, 0xc6, 0x0a, 0x94, 0x18, 0xe3, 0x19, 0x0e, 0xc7, 0x59,
52+
0x48, 0x21, 0x02, 0x41, 0x00, 0xaa, 0x5d, 0x55, 0xc3, 0xee, 0xf7, 0x45,
53+
0xbd, 0xa5, 0x00, 0x32, 0xb9, 0xa1, 0x71, 0x49, 0xd5, 0x8c, 0x32, 0xe0,
54+
0xc7, 0xd5, 0xf0, 0x64, 0xa9, 0xb5, 0xaf, 0x1b, 0x25, 0xdf, 0x34, 0xed,
55+
0xd4, 0xa6, 0xe1, 0x77, 0xfe, 0x9b, 0xc3, 0xed, 0x9b, 0x74, 0xca, 0xbf,
56+
0x6d, 0xa4, 0x85, 0x5a, 0x37, 0xd8, 0xf3, 0xad, 0xae, 0x91, 0x4f, 0xa1,
57+
0x30, 0x24, 0xef, 0x3c, 0x4f, 0x49, 0xec, 0x34, 0xa5
58+
};
59+
unsigned int example_key_der_len = 609;
60+
61+
#endif //PRIVATE_KEY_H

0 commit comments

Comments
 (0)