Skip to content

Commit 3138399

Browse files
committed
feat(example): Add openssl server demo
internal: 9c54c474
1 parent eb91560 commit 3138399

File tree

10 files changed

+860
-0
lines changed

10 files changed

+860
-0
lines changed

examples/openssl_server/Makefile

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#############################################################
2+
# Required variables for each makefile
3+
# Discard this section from all parent makefiles
4+
# Expected variables (with automatic defaults):
5+
# CSRCS (all "C" files in the dir)
6+
# SUBDIRS (all subdirs with a Makefile)
7+
# GEN_LIBS - list of libs to be generated ()
8+
# GEN_IMAGES - list of object file images to be generated ()
9+
# GEN_BINS - list of binaries to be generated ()
10+
# COMPONENTS_xxx - a list of libs/objs in the form
11+
# subdir/lib to be extracted and rolled up into
12+
# a generated lib/image xxx.a ()
13+
#
14+
TARGET = eagle
15+
#FLAVOR = release
16+
FLAVOR = debug
17+
18+
#EXTRA_CCFLAGS += -u
19+
20+
ifndef PDIR # {
21+
GEN_IMAGES= eagle.app.v6.out
22+
GEN_BINS= eagle.app.v6.bin
23+
SPECIAL_MKTARGETS=$(APP_MKTARGETS)
24+
SUBDIRS= \
25+
user
26+
27+
endif # } PDIR
28+
29+
LDDIR = $(SDK_PATH)/ld
30+
31+
CCFLAGS += -Os
32+
33+
TARGET_LDFLAGS = \
34+
-nostdlib \
35+
-Wl,-EL \
36+
--longcalls \
37+
--text-section-literals
38+
39+
ifeq ($(FLAVOR),debug)
40+
TARGET_LDFLAGS += -g -O2
41+
endif
42+
43+
ifeq ($(FLAVOR),release)
44+
TARGET_LDFLAGS += -g -O0
45+
endif
46+
47+
COMPONENTS_eagle.app.v6 = \
48+
user/libuser.a
49+
50+
LINKFLAGS_eagle.app.v6 = \
51+
-L$(SDK_PATH)/lib \
52+
-Wl,--gc-sections \
53+
-nostdlib \
54+
-T$(LD_FILE) \
55+
-Wl,--no-check-sections \
56+
-u call_user_start \
57+
-Wl,-static \
58+
-Wl,--start-group \
59+
-lcirom \
60+
-lgcc \
61+
-lhal \
62+
-lcrypto \
63+
-lfreertos \
64+
-llwip \
65+
-lmain \
66+
-lnet80211 \
67+
-lphy \
68+
-lpp \
69+
-lmbedtls \
70+
-lopenssl \
71+
-lwpa \
72+
$(DEP_LIBS_eagle.app.v6)\
73+
-Wl,--end-group
74+
75+
DEPENDS_eagle.app.v6 = \
76+
$(LD_FILE) \
77+
$(LDDIR)/eagle.rom.addr.v6.ld
78+
79+
#############################################################
80+
# Configuration i.e. compile options etc.
81+
# Target specific stuff (defines etc.) goes in here!
82+
# Generally values applying to a tree are captured in the
83+
# makefile at its root level - these are then overridden
84+
# for a subtree within the makefile rooted therein
85+
#
86+
87+
#UNIVERSAL_TARGET_DEFINES = \
88+
89+
# Other potential configuration flags include:
90+
# -DTXRX_TXBUF_DEBUG
91+
# -DTXRX_RXBUF_DEBUG
92+
# -DWLAN_CONFIG_CCX
93+
CONFIGURATION_DEFINES = -DICACHE_FLASH
94+
95+
DEFINES += \
96+
$(UNIVERSAL_TARGET_DEFINES) \
97+
$(CONFIGURATION_DEFINES)
98+
99+
DDEFINES += \
100+
$(UNIVERSAL_TARGET_DEFINES) \
101+
$(CONFIGURATION_DEFINES)
102+
103+
104+
#############################################################
105+
# Recursion Magic - Don't touch this!!
106+
#
107+
# Each subtree potentially has an include directory
108+
# corresponding to the common APIs applicable to modules
109+
# rooted at that subtree. Accordingly, the INCLUDE PATH
110+
# of a module can only contain the include directories up
111+
# its parent path, and not its siblings
112+
#
113+
# Required for each makefile to inherit from the parent
114+
#
115+
116+
INCLUDES := $(INCLUDES) -I $(PDIR)include -I include -I $(SDK_PATH)/include/openssl
117+
sinclude $(SDK_PATH)/Makefile
118+
119+
.PHONY: FORCE
120+
FORCE:
121+

examples/openssl_server/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
1. Run ./gencrt.sh or if you have your own certificate, move to the openssl_server directory, the name is ca.crt,server.crt, server.key, client.crt and client.key.
2+
3+
The server.crt and client.crt was generate by the same ca.crt in ./gencrt.sh.
4+
5+
Server side needs ca.crt(to verify client.crt), server.crt, server.key
6+
7+
Client side needs ca.crt(to verify server.crt), client.crt, client.key
8+
9+
If you have two ca.crt to generate server.crt and client.crt respectively, client1.crt is generate by ca1.crt and client1.key, server2.crt is generate by ca2.crt and server2.key:
10+
11+
Client side needs ca2.crt, client1.crt, client1.key.
12+
13+
Server side needs ca1.crt, server2.crt, server2.key.
14+
15+
Rename ca1.crt server2.crt server2.key to ca.crt server.crt server.key and run ./genheader.sh.
16+
17+
Use ca2.crt in openssl s_client -CAfile option.
18+
19+
2. Run ./genheader.sh.
20+
21+
3. Modify thease two lines in file user_config.h to your local Wi-Fi SSID and Password.
22+
23+
```#define SSID "HUAWEI001"```
24+
25+
```#define PASSWORD ""```
26+
27+
4. Make sure that the computer and ESP8266 are in the same local area network.
28+
29+
5. Run ./gen_misc.sh.
30+
31+
6. Download bin file to ESP8266.
32+
33+
Find server ip address in ESP8266 UART log: ip:192.168.3.6,mask:255.255.255.0,gw:192.168.3.1.
34+
35+
7. Run openssl s_client -CAfile ca.crt -cert client.crt -key client.key -verify 1 -tls1_1 -host 192.168.3.6 -port 443.
36+
37+
38+
**ATTENTION**
39+
40+
**1. Make sure the free heap size larger than 30K.**
41+
42+
**2. Make sure the private key length larger than 2048.**
43+
44+
**3. Make sure the fragment size range is between 2048 and 8192.**

examples/openssl_server/gen_misc.sh

+191
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#!/bin/bash
2+
3+
:<<!
4+
******NOTICE******
5+
MUST set SDK_PATH & BIN_PATH firstly!!!
6+
example:
7+
export SDK_PATH=~/esp_iot_sdk_freertos
8+
export BIN_PATH=~/esp8266_bin
9+
!
10+
11+
export SDK_PATH=$SDK_PATH
12+
export BIN_PATH=$BIN_PATH
13+
14+
echo "gen_misc.sh version 20150911"
15+
echo ""
16+
17+
if [ $SDK_PATH ]; then
18+
echo "SDK_PATH:"
19+
echo "$SDK_PATH"
20+
echo ""
21+
else
22+
echo "ERROR: Please export SDK_PATH in gen_misc.sh firstly, exit!!!"
23+
exit
24+
fi
25+
26+
if [ $BIN_PATH ]; then
27+
echo "BIN_PATH:"
28+
echo "$BIN_PATH"
29+
echo ""
30+
else
31+
echo "ERROR: Please export BIN_PATH in gen_misc.sh firstly, exit!!!"
32+
exit
33+
fi
34+
35+
echo "Please check SDK_PATH & BIN_PATH, enter (Y/y) to continue:"
36+
read input
37+
38+
if [[ $input != Y ]] && [[ $input != y ]]; then
39+
exit
40+
fi
41+
42+
echo ""
43+
44+
echo "Please follow below steps(1-5) to generate specific bin(s):"
45+
echo "STEP 1: use boot_v1.2+ by default"
46+
boot=new
47+
48+
echo "boot mode: $boot"
49+
echo ""
50+
51+
echo "STEP 2: choose bin generate(0=eagle.flash.bin+eagle.irom0text.bin, 1=user1.bin, 2=user2.bin)"
52+
echo "enter (0/1/2, default 0):"
53+
read input
54+
55+
if [ -z "$input" ]; then
56+
if [ $boot != none ]; then
57+
boot=none
58+
echo "ignore boot"
59+
fi
60+
app=0
61+
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
62+
elif [ $input == 1 ]; then
63+
if [ $boot == none ]; then
64+
app=0
65+
echo "choose no boot before"
66+
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
67+
else
68+
app=1
69+
echo "generate bin: user1.bin"
70+
fi
71+
elif [ $input == 2 ]; then
72+
if [ $boot == none ]; then
73+
app=0
74+
echo "choose no boot before"
75+
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
76+
else
77+
app=2
78+
echo "generate bin: user2.bin"
79+
fi
80+
else
81+
if [ $boot != none ]; then
82+
boot=none
83+
echo "ignore boot"
84+
fi
85+
app=0
86+
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
87+
fi
88+
89+
echo ""
90+
91+
echo "STEP 3: choose spi speed(0=20MHz, 1=26.7MHz, 2=40MHz, 3=80MHz)"
92+
echo "enter (0/1/2/3, default 2):"
93+
read input
94+
95+
if [ -z "$input" ]; then
96+
spi_speed=40
97+
elif [ $input == 0 ]; then
98+
spi_speed=20
99+
elif [ $input == 1 ]; then
100+
spi_speed=26.7
101+
elif [ $input == 3 ]; then
102+
spi_speed=80
103+
else
104+
spi_speed=40
105+
fi
106+
107+
echo "spi speed: $spi_speed MHz"
108+
echo ""
109+
110+
echo "STEP 4: choose spi mode(0=QIO, 1=QOUT, 2=DIO, 3=DOUT)"
111+
echo "enter (0/1/2/3, default 0):"
112+
read input
113+
114+
if [ -z "$input" ]; then
115+
spi_mode=QIO
116+
elif [ $input == 1 ]; then
117+
spi_mode=QOUT
118+
elif [ $input == 2 ]; then
119+
spi_mode=DIO
120+
elif [ $input == 3 ]; then
121+
spi_mode=DOUT
122+
else
123+
spi_mode=QIO
124+
fi
125+
126+
echo "spi mode: $spi_mode"
127+
echo ""
128+
129+
echo "STEP 5: choose spi size and map"
130+
echo " 0= 512KB( 256KB+ 256KB)"
131+
echo " 2=1024KB( 512KB+ 512KB)"
132+
echo " 3=2048KB( 512KB+ 512KB)"
133+
echo " 4=4096KB( 512KB+ 512KB)"
134+
echo " 5=2048KB(1024KB+1024KB)"
135+
echo " 6=4096KB(1024KB+1024KB)"
136+
echo " 7=4096KB(2048KB+2048KB) not support ,just for compatible with nodeMCU board"
137+
echo " 8=8192KB(1024KB+1024KB)"
138+
echo " 9=16384KB(1024KB+1024KB)"
139+
echo "enter (0/2/3/4/5/6/7/8/9, default 0):"
140+
read input
141+
142+
if [ -z "$input" ]; then
143+
spi_size_map=0
144+
echo "spi size: 512KB"
145+
echo "spi ota map: 256KB + 256KB"
146+
elif [ $input == 2 ]; then
147+
spi_size_map=2
148+
echo "spi size: 1024KB"
149+
echo "spi ota map: 512KB + 512KB"
150+
elif [ $input == 3 ]; then
151+
spi_size_map=3
152+
echo "spi size: 2048KB"
153+
echo "spi ota map: 512KB + 512KB"
154+
elif [ $input == 4 ]; then
155+
spi_size_map=4
156+
echo "spi size: 4096KB"
157+
echo "spi ota map: 512KB + 512KB"
158+
elif [ $input == 5 ]; then
159+
spi_size_map=5
160+
echo "spi size: 2048KB"
161+
echo "spi ota map: 1024KB + 1024KB"
162+
elif [ $input == 6 ]; then
163+
spi_size_map=6
164+
echo "spi size: 4096KB"
165+
echo "spi ota map: 1024KB + 1024KB"
166+
elif [ $input == 7 ]; then
167+
spi_size_map=7
168+
echo"not support ,just for compatible with nodeMCU board"
169+
exit
170+
elif [ $input == 8 ]; then
171+
spi_size_map=8
172+
echo "spi size: 8192KB"
173+
echo "spi ota map: 1024KB + 1024KB"
174+
elif [ $input == 9 ]; then
175+
spi_size_map=9
176+
echo "spi size: 16384KB"
177+
echo "spi ota map: 1024KB + 1024KB"
178+
else
179+
spi_size_map=0
180+
echo "spi size: 512KB"
181+
echo "spi ota map: 256KB + 256KB"
182+
fi
183+
184+
echo ""
185+
186+
echo "start..."
187+
echo ""
188+
189+
make clean
190+
191+
make BOOT=$boot APP=$app SPI_SPEED=$spi_speed SPI_MODE=$spi_mode SPI_SIZE_MAP=$spi_size_map

examples/openssl_server/gencrt.sh

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
#
4+
# Generate the certificates and keys for testing.
5+
#
6+
7+
SAVEIFS=$IFS
8+
IFS=$(echo -en "\n\b")
9+
10+
ROOT_SUBJECT="/C=C1/ST=JS1/L=WX1/O=ESP1/OU=ESP1/CN=Server1 CA/emailAddress=ESP1"
11+
LEVEL2_SUBJECT="/C=C2/ST=JS22/L=WX22/O=ESP22/OU=ESP22/CN=Server22 CA/emailAddress=ESP22"
12+
LEVEL3_SUBJECT="/C=C3/ST=JS333/L=WX333/O=ESP333/OU=ESP333/CN=Server333 CA/emailAddress=ESP333"
13+
14+
# private key generation
15+
openssl genrsa -out ca.key 2048
16+
openssl genrsa -out server.key 2048
17+
openssl genrsa -out client.key 2048
18+
19+
# cert requests
20+
openssl req -new -key ca.key -out ca.csr -text -subj $ROOT_SUBJECT
21+
openssl req -new -key server.key -out server.csr -text -subj $LEVEL2_SUBJECT
22+
openssl req -new -key client.key -out client.csr -text -subj $LEVEL3_SUBJECT
23+
24+
# generate the actual certs.
25+
openssl x509 -req -in ca.csr -out ca.crt -sha1 -days 5000 -signkey ca.key -text -extensions v3_ca
26+
openssl x509 -req -in server.csr -out server.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key -text -extensions v3_ca
27+
openssl x509 -req -in client.csr -out client.crt -sha1 -CAcreateserial -days 5000 -CA ca.crt -CAkey ca.key -text -extensions v3_ca

examples/openssl_server/genheader.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# set ca crt for use in the server
4+
xxd -i ca.crt | sed -e "s/ca_crt/ca_crt/" > ssl_server_crt.h
5+
6+
# set server crt for use in the server
7+
xxd -i server.crt | sed -e "s/server_crt/server_crt/" >> ssl_server_crt.h
8+
9+
# set private key for use in the server
10+
xxd -i server.key | sed -e "s/server_key/server_key/" >> ssl_server_crt.h
11+
12+
mv ssl_server_crt.h ./include

0 commit comments

Comments
 (0)