Skip to content

Commit 94a3b0d

Browse files
committed
Add Linux CANbus example
A client/server example that uses wolfSSL's ISO-TP transport implementation and wolfSentry to filter ISO-TP's Normal Fixed Addressing method.
1 parent 24da892 commit 94a3b0d

File tree

8 files changed

+775
-1
lines changed

8 files changed

+775
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ifndef C_WARNFLAGS
6565
endif
6666
endif
6767

68-
CFLAGS := -I$(SRC_TOP) $(OPTIM) $(DEBUG) -MMD $(C_WARNFLAGS) $(EXTRA_CFLAGS)
68+
CFLAGS := -ggdb3 -I$(SRC_TOP) $(OPTIM) $(DEBUG) -MMD $(C_WARNFLAGS) $(EXTRA_CFLAGS)
6969
LDFLAGS := $(EXTRA_LDFLAGS)
7070

7171
VISIBILITY_CFLAGS := -fvisibility=hidden -DHAVE_VISIBILITY=1

examples/Linux-CANbus/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CC=gcc
2+
LIBS=-lwolfssl -lwolfsentry
3+
CFLAGS=-g -Wno-cpp -Wall -Wextra -Wdeclaration-after-statement
4+
5+
COMMON_OBJS=common.o
6+
CLIENT_OBJS=client.o
7+
SERVER_OBJS=server.o
8+
9+
all: client server
10+
11+
%.o: %.c
12+
@$(CC) -c $< -o $@ $(CFLAGS)
13+
14+
client: $(COMMON_OBJS) $(CLIENT_OBJS)
15+
@$(CC) -o $@ $(COMMON_OBJS) $(CLIENT_OBJS) $(CFLAGS) $(LIBS)
16+
17+
server: $(COMMON_OBJS) $(SERVER_OBJS)
18+
@$(CC) -o $@ $(COMMON_OBJS) $(SERVER_OBJS) $(CFLAGS) $(LIBS)
19+
20+
clean:
21+
@rm -f *.o
22+
@rm -f client
23+
@rm -f server

examples/Linux-CANbus/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# wolfSentry CAN Bus Example
2+
3+
This example implements a simple echo client and server that uses TLS over a CAN bus using [ISO-TP](https://en.wikipedia.org/wiki/ISO_15765-2) as a transport protocol. This is because the raw CAN bus protocol can only support payloads of up to 8 bytes. The example requires Linux to run but can modified to work on any setup that uses CAN bus.
4+
5+
All packets received are filtered through wolfSentry and if the CAN bus addresses do not match the packet is filtered out.
6+
7+
## Building
8+
9+
You need to have wolfSSL installed on your computer prior to building, this will need to be built with `WOLFSSL_ISOTP` defined to provide ISO-TP functionality.
10+
11+
You will also need wolfSentry installed on your computer.
12+
13+
To generate the required SSL certificates use `./generate_ssl.sh`.
14+
15+
## Setting Up
16+
17+
If you do not have a physical CAN bus between too machines you can use the virtual CAN bus which is a Linux kernel module. This behaves just like a real CAN bus with a similar bandwidth. To enable this run the following commands:
18+
19+
```sh
20+
sudo modprobe vcan
21+
sudo ip link add dev vcan0 type vcan
22+
sudo ip link set vcan0 up
23+
```
24+
25+
## Running
26+
27+
Both the client and server require three parameters:
28+
29+
1. The can bus address
30+
2. The local address
31+
3. The remote address
32+
33+
These addresses are used for ISP-TP's "Normal Fixed Addressing". For example, with a local of 11 and a remote of 22 the CAN arbitration is 0x18DA1122. wolfSentry is configured to require that both the local and remote addresses are correct.
34+
35+
On one console run the server, this should be executed first or the handshake will fail. This is executed using:
36+
37+
```sh
38+
./server vcan0 11 22
39+
```
40+
41+
Then in another terminal run the client:
42+
43+
```sh
44+
./client vcan0 22 11
45+
```
46+
47+
On both ends you will see:
48+
49+
```
50+
SSL handshake done!
51+
```
52+
53+
Once you see the message "SSL handshake done!" on both consoles you can enter text into the client console. When you hit "enter" this will be sent to the server via the TLS encrypted CAN bus and will echo there.
54+
55+
For example, on the client if we type "Hello world, this is a TLS test!":
56+
57+
```
58+
Hello world! This is a CAN bus test!
59+
Sending: Hello world! This is a CAN bus test!
60+
61+
Message sent
62+
```
63+
64+
The server will echo:
65+
66+
```
67+
Got message: Hello world! This is a CAN bus test!
68+
```
69+
70+
If you very the addresses you will find that wolfSentry will block the messages before the application processes them.
71+
72+
## Cleaning Up
73+
74+
If you wish to disable the virtual CAN bus you can turn it off by doing:
75+
76+
```sh
77+
sudo ip link set vcan0 down
78+
```
79+

examples/Linux-CANbus/client.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* client.c
2+
*
3+
* Copyright (C) 2022 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#include "common.h"
23+
24+
extern volatile int keep_running;
25+
26+
int main(int argc, char *argv[])
27+
{
28+
WOLFSSL_CTX *ctx = NULL;
29+
WOLFSSL_METHOD* method = NULL;
30+
WOLFSSL* ssl = NULL;
31+
int ret;
32+
uint8_t local;
33+
uint8_t remote;
34+
35+
if (argc != 4) {
36+
printf("Usage: ./client <CAN interface> <local ID> <remote ID>\n");
37+
return -1;
38+
}
39+
40+
local = strtoul(argv[2], NULL, 16);
41+
remote = strtoul(argv[3], NULL, 16);
42+
43+
sentry_init(local, remote);
44+
ret = setup_connection(argv[1], local, remote);
45+
if (ret) {
46+
return ret;
47+
}
48+
49+
ret = setup_ssl(SERVICE_TYPE_CLIENT, &ctx, &method, &ssl);
50+
if (ret) {
51+
return ret;
52+
}
53+
54+
while(keep_running) {
55+
char *line = NULL;
56+
size_t len = 0;
57+
ssize_t line_size = 0;
58+
line_size = getline(&line, &len, stdin);
59+
if (line_size > 0) {
60+
printf("Sending: %s\n", line);
61+
wolfSSL_send(ssl, line, line_size, 0);
62+
printf("Message sent\n");
63+
}
64+
free(line);
65+
}
66+
67+
close_ssl(ctx, ssl);
68+
69+
return 0;
70+
}

0 commit comments

Comments
 (0)