Skip to content

Commit 5f97b10

Browse files
Add self-signed cert generation script example
1 parent ea24c88 commit 5f97b10

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

libraries/ESP8266WiFi/examples/WiFiHTTPSServer/WiFiHTTPSServer.ino

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
* This sketch demonstrates how to set up a simple HTTP-like server using HTTPS encryption.
33
* A sample self-signed certificate is included. For your own application, please be sure
44
* to GENERATE YOUR OWN AND REPLACE.
5+
*
56
* The server will set a GPIO pin depending on the request
6-
* http://server_ip/gpio/0 will set the GPIO2 low,
7-
* http://server_ip/gpio/1 will set the GPIO2 high
7+
* https://server_ip/gpio/0 will set the GPIO2 low,
8+
* https://server_ip/gpio/1 will set the GPIO2 high
89
* server_ip is the IP address of the ESP8266 module, will be
910
* printed to Serial when the module is connected.
1011
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
# This script generates a self-signed certificate for use by the ESP8266
4+
# Replace your-name-here with somethine appropriate before running and use
5+
# the generated .H files in your code as follows:
6+
#
7+
# static const uint8_t rsakey[] ICACHE_RODATA_ATTR = {
8+
# #include "key.h"
9+
# };
10+
#
11+
# static const uint8_t x509[] ICACHE_RODATA_ATTR = {
12+
# #include "x509.h"
13+
# };
14+
#
15+
# ....
16+
# WiFiServerSecure server(443);
17+
# server.setServerKeyAndCert_P(rsakey, sizeof(rsakey), x509, sizeof(x509));
18+
# ....
19+
20+
# 1024 or 512. 512 saves memory...
21+
BITS=512
22+
C=$PWD
23+
pushd /tmp
24+
25+
openssl genrsa -out tls.ca_key.pem $BITS
26+
openssl genrsa -out tls.key_$BITS.pem $BITS
27+
openssl rsa -in tls.key_$BITS.pem -out tls.key_$BITS -outform DER
28+
cat > certs.conf <<EOF
29+
[ req ]
30+
distinguished_name = req_distinguished_name
31+
prompt = no
32+
33+
[ req_distinguished_name ]
34+
O = your-name-here
35+
CN = 127.0.0.1
36+
EOF
37+
openssl req -out tls.ca_x509.req -key tls.ca_key.pem -new -config certs.conf
38+
openssl req -out tls.x509_$BITS.req -key tls.key_$BITS.pem -new -config certs.conf
39+
openssl x509 -req -in tls.ca_x509.req -out tls.ca_x509.pem -sha256 -days 5000 -signkey tls.ca_key.pem
40+
openssl x509 -req -in tls.x509_$BITS.req -out tls.x509_$BITS.pem -sha256 -CAcreateserial -days 5000 -CA tls.ca_x509.pem -CAkey tls.ca_key.pem
41+
openssl x509 -in tls.ca_x509.pem -outform DER -out tls.ca_x509.cer
42+
openssl x509 -in tls.x509_$BITS.pem -outform DER -out tls.x509_$BITS.cer
43+
44+
xxd -i tls.key_$BITS | sed 's/.*{//' | sed 's/\};//' | sed 's/unsigned.*//' > "$C/key.h"
45+
xxd -i tls.x509_$BITS.cer | sed 's/.*{//' | sed 's/\};//' | sed 's/unsigned.*//' > "$C/x509.h"
46+
47+
rm -f tls.ca_key.pem tls.key_$BITS.pem tls.key_$BITS certs.conf tls.ca_x509.req tls.x509_$BITS.req tls.ca_x509.pem tls.x509_$BITS.pem tls.srl tls.x509_$BITS.cer tls.ca_x509.cer
48+
49+
popd

0 commit comments

Comments
 (0)