Skip to content

Commit 6209eac

Browse files
committed
Merge remote-tracking branch 'origin/master' into axtls-8266
2 parents c5c9c73 + 139914f commit 6209eac

40 files changed

+1398
-318
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.o
2+
bin/
3+
Makefile.local
4+
.DS_Store
5+

.travis.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
sudo: false
2+
language: bash
3+
os:
4+
- linux
5+
6+
script:
7+
# Download Arduino IDE
8+
- wget -O arduino.tar.xz https://www.arduino.cc/download.php?f=/arduino-nightly-linux64.tar.xz
9+
- tar xf arduino.tar.xz
10+
- mv arduino-nightly $HOME/arduino_ide
11+
# Download ESP8266 Arduino core
12+
- cd $HOME/arduino_ide/hardware
13+
- mkdir esp8266com
14+
- cd esp8266com
15+
- git clone https://github.com/esp8266/Arduino.git esp8266
16+
- cd esp8266
17+
- export ESP8266_ARDUINO_DIR="$PWD"
18+
# Download toolchain and esptool
19+
- cd tools
20+
- python get.py
21+
- export PATH="$PATH:$PWD/xtensa-lx106-elf/bin"
22+
# Build axTLS
23+
- cd $TRAVIS_BUILD_DIR
24+
- make
25+
# Copy the library into Arduino core
26+
- cp bin/libaxtls.a $ESP8266_ARDUINO_DIR/tools/sdk/lib/libaxtls.a
27+
# Try building examples in ESP8266WiFi library from the ESP8266 Arduino core
28+
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
29+
- sleep 3
30+
- export DISPLAY=:1.0
31+
- export PATH="$HOME/arduino_ide:$PATH"
32+
- which arduino
33+
- cd $ESP8266_ARDUINO_DIR
34+
- source tests/common.sh
35+
- arduino --board esp8266com:esp8266:generic --save-prefs
36+
- arduino --get-pref sketchbook.path
37+
- build_sketches $HOME/arduino_ide $ESP8266_ARDUINO_DIR/libraries/ESP8266WiFi/examples/HTTPSRequest
38+
# Feel free to add more test cases (for other environments) here
39+
40+
notifications:
41+
email:
42+
on_success: change
43+
on_failure: change

Makefile

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
TOOLCHAIN_PREFIX := xtensa-lx106-elf-
2+
CC := $(TOOLCHAIN_PREFIX)gcc
3+
AR := $(TOOLCHAIN_PREFIX)ar
4+
LD := $(TOOLCHAIN_PREFIX)gcc
5+
OBJCOPY := $(TOOLCHAIN_PREFIX)objcopy
6+
7+
8+
XTENSA_LIBS ?= $(shell $(CC) -print-sysroot)
9+
10+
11+
OBJ_FILES := \
12+
crypto/aes.o \
13+
crypto/bigint.o \
14+
crypto/hmac.o \
15+
crypto/md2.o \
16+
crypto/md5.o \
17+
crypto/rc4.o \
18+
crypto/rsa.o \
19+
crypto/sha1.o \
20+
ssl/asn1.o \
21+
ssl/gen_cert.o \
22+
ssl/loader.o \
23+
ssl/os_port.o \
24+
ssl/p12.o \
25+
ssl/tls1.o \
26+
ssl/tls1_clnt.o \
27+
ssl/tls1_svr.o \
28+
ssl/x509.o \
29+
crypto/crypto_misc.o \
30+
31+
32+
CPPFLAGS += -I$(XTENSA_LIBS)/include \
33+
-Icrypto \
34+
-Issl \
35+
-I.
36+
37+
LDFLAGS += -L$(XTENSA_LIBS)/lib \
38+
-L$(XTENSA_LIBS)/arch/lib \
39+
40+
41+
CFLAGS+=-std=c99 -DESP8266
42+
43+
CFLAGS += -Wall -Os -g -O2 -Wpointer-arith -Wno-implicit-function-declaration -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mno-text-section-literals -D__ets__ -DICACHE_FLASH
44+
BIN_DIR := bin
45+
AXTLS_AR := $(BIN_DIR)/libaxtls.a
46+
47+
all: $(AXTLS_AR)
48+
49+
$(AXTLS_AR): | $(BIN_DIR)
50+
51+
$(AXTLS_AR): $(OBJ_FILES)
52+
for file in $(OBJ_FILES); do \
53+
$(OBJCOPY) \
54+
--rename-section .text=.irom0.text \
55+
--rename-section .literal=.irom0.literal \
56+
$$file; \
57+
done
58+
$(AR) cru $@ $^
59+
60+
$(BIN_DIR):
61+
mkdir -p $(BIN_DIR)
62+
63+
clean:
64+
rm -rf $(OBJ_FILES) $(AXTLS_AR)
65+
66+
67+
.PHONY: all clean

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Replacement for Espressif's libssl, kept as close as possible to [axTLS](http://axtls.sourceforge.net/) source.
2+
Currently based on axTLS 1.4.9, will be upgraded to 1.5.3.
3+
4+
[![Build status](https://travis-ci.org/igrr/axtls-8266.svg)](https://travis-ci.org/igrr/axtls-8266)
5+
6+
This is not a self-sufficient library. Application has to provide the following symbols (list not complete yet):
7+
```
8+
ax_port_malloc
9+
ax_port_calloc
10+
ax_port_realloc
11+
ax_port_free
12+
ax_port_read
13+
ax_port_write
14+
ax_port_open
15+
ax_port_close
16+
ax_get_file
17+
phy_get_rand (provided by the IoT SDK)
18+
ets_printf (in ESP8266 ROM)
19+
ets_putc (in ESP8266 ROM)
20+
gettimeofday
21+
time
22+
ctime
23+
```
24+
25+
For use with LwIP raw TCP API, see [compat/README.md](compat/README.md)
26+
27+
To build, add xtensa toolchain to your path, and run `make`.
28+
29+
See [LICENSE](LICENSE) file for axTLS license.

compat/README.md

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
If you are using [LWIP raw tcp mode](http://lwip.wikia.com/wiki/Raw/TCP) and want to add SSL support below are the steps that can help you to achieve this with the help of [axTLS]( http://axtls.sourceforge.net/ ).
2+
3+
First you have to include the `lwipr_compat.h` header.
4+
5+
```C
6+
#include "compat/lwipr_compat.h"
7+
```
8+
9+
Then in the code block where you initialize the tcp raw connection you should call `axl_init`.
10+
Take a look at the example below:
11+
12+
```C
13+
lwip_init();
14+
15+
/*
16+
* The line below should be added AFTER the lwip_init code
17+
* AND BEFORE the call to tcp_new()
18+
* The parameter value 10 specifies how many SSL connections are expected
19+
*/
20+
axl_init(10);
21+
22+
// .. some more code
23+
tcp = tcp_new();
24+
tcp_sent(tcp, staticOnSent);
25+
tcp_recv(tcp, staticOnReceive);
26+
tcp_err(tcp, staticOnError);
27+
tcp_poll(tcp, staticOnPoll, 4);
28+
// ... and even more code
29+
res = tcp_connect(tcp, &addr, port, staticOnConnected);
30+
31+
32+
```
33+
34+
Now we should add in our `staticOnConnected` funciton code to create new ssl context and ssl object.
35+
In the example below the `sslObj` and `sslContext` are defined as global
36+
37+
```C
38+
// global definitions
39+
SSL *sslObj = NULL;
40+
SSLCTX* sslContext = NULL;
41+
42+
// and some more code...
43+
44+
err_t staticOnConnected(void *arg, struct tcp_pcb *tcp, err_t err)
45+
{
46+
int clientfd = -1;
47+
uint32_t options = 0;
48+
49+
if (tcp == NULL) {
50+
/* @TODO: Take care to handle error conditions */
51+
return -1;
52+
}
53+
54+
clientfd = axl_append(tcp);
55+
if(clientfd == -1) {
56+
printf("Unable to add LWIP tcp -> clientfd mapping\n");
57+
return ERR_OK;
58+
}
59+
60+
printf("Connected: ClientId: %d\n", clientfd);
61+
#ifdef SSL_DEBUG
62+
options |= SSL_DISPLAY_STATES | SSL_DISPLAY_BYTES;
63+
#endif
64+
65+
// if you want to verify the server certificate later you can also add the following option
66+
options |= SSL_SERVER_VERIFY_LATER
67+
68+
sslContext = ssl_ctx_new(SSL_CONNECT_IN_PARTS | options, 1); // !!! SSL_CONNECT_IN_PARTS must be in the flags !!!
69+
sslObj = ssl_client_new(sslContext, clientfd, NULL, 0);
70+
71+
return ERR_OK;
72+
}
73+
```
74+
75+
76+
Once we are connected we can send and receive information. For the receiving part we can do the following
77+
78+
```C
79+
err_t staticOnReceive(void *arg, struct tcp_pcb *tcp, struct pbuf *p, err_t err)
80+
{
81+
uint8_t *read_buf = NULL;
82+
int read_bytes = 0;
83+
84+
printf("Err: %d\n", err);
85+
86+
if(tcp == NULL || p == NULL) {
87+
/* @TODO: Take care to handle error conditions */
88+
return -1;
89+
}
90+
91+
read_bytes = axl_ssl_read(sslObj, &read_buf, tcp, p);
92+
if(read_bytes > 0) {
93+
printf("Got data: %s", read_buf);
94+
// @TODO: Do something useful with the read_buf
95+
}
96+
97+
return ERR_OK;
98+
}
99+
```
100+
101+
In the receiving part you can also add debug code to display more information about the SSL handshake, once it was successul.
102+
103+
104+
```C
105+
err_t staticOnReceive(void *arg, struct tcp_pcb *tcp, struct pbuf *p, err_t err)
106+
{
107+
static int show_info = 0;
108+
const char *common_name = NULL;
109+
110+
// ..
111+
read_bytes = axl_ssl_read(sslObj, &read_buf, tcp, p);
112+
if(read_bytes > 0) {
113+
printf("Got data: %s", read_buf);
114+
// @TODO: Do something useful with the read_buf
115+
}
116+
117+
if(!show_info && ssl_handshake_status(sslObj) == SSL_OK) {
118+
common_name = ssl_get_cert_dn(sslObj, SSL_X509_CERT_COMMON_NAME);
119+
if (common_name) {
120+
printf("Common Name:\t\t\t%s\n", common_name);
121+
}
122+
123+
// These two funcitons below can be found in the axtls examples
124+
display_session_id(sslObj);
125+
display_cipher(sslObj);
126+
show_info = 1;
127+
}
128+
129+
return ERR_OK;
130+
}
131+
132+
```
133+
134+
135+
And for the sending part we can use the following code sample as a start
136+
137+
```C
138+
void someSendingfunction() {
139+
uint8_t *out_buf;
140+
int out_bytes = 0;
141+
142+
// ... take care to store something in the out_buf
143+
144+
axl_ssl_write(sslObj, out_buf, out_bytes);
145+
}
146+
147+
```
148+
149+
Good luck and send your success stories at [email protected].

0 commit comments

Comments
 (0)