Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add Linux CANbus example #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/Linux-CANbus/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CC=gcc
LIBS=-lwolfssl -lwolfsentry
CFLAGS=-g -Wno-cpp -Wall -Wextra -Wdeclaration-after-statement

COMMON_OBJS=common.o
CLIENT_OBJS=client.o
SERVER_OBJS=server.o

all: client server

%.o: %.c
@$(CC) -c $< -o $@ $(CFLAGS)

client: $(COMMON_OBJS) $(CLIENT_OBJS)
@$(CC) -o $@ $(COMMON_OBJS) $(CLIENT_OBJS) $(CFLAGS) $(LIBS)

server: $(COMMON_OBJS) $(SERVER_OBJS)
@$(CC) -o $@ $(COMMON_OBJS) $(SERVER_OBJS) $(CFLAGS) $(LIBS)

clean:
@rm -f *.o
@rm -f client
@rm -f server
79 changes: 79 additions & 0 deletions examples/Linux-CANbus/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# wolfSentry CAN Bus Example

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.

All packets received are filtered through wolfSentry and if the CAN bus addresses do not match the packet is filtered out.

## Building

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.

You will also need wolfSentry installed on your computer.

To generate the required SSL certificates use `./generate_ssl.sh`.

## Setting Up

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:

```sh
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set vcan0 up
```

## Running

Both the client and server require three parameters:

1. The can bus address
2. The local address
3. The remote address

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.

On one console run the server, this should be executed first or the handshake will fail. This is executed using:

```sh
./server vcan0 11 22
```

Then in another terminal run the client:

```sh
./client vcan0 22 11
```

On both ends you will see:

```
SSL handshake done!
```

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.

For example, on the client if we type "Hello world, this is a TLS test!":

```
Hello world! This is a CAN bus test!
Sending: Hello world! This is a CAN bus test!

Message sent
```

The server will echo:

```
Got message: Hello world! This is a CAN bus test!
```

If you very the addresses you will find that wolfSentry will block the messages before the application processes them.

## Cleaning Up

If you wish to disable the virtual CAN bus you can turn it off by doing:

```sh
sudo ip link set vcan0 down
```

70 changes: 70 additions & 0 deletions examples/Linux-CANbus/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* client.c
*
* Copyright (C) 2022 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include "common.h"

extern volatile int keep_running;

int main(int argc, char *argv[])
{
WOLFSSL_CTX *ctx = NULL;
WOLFSSL_METHOD* method = NULL;
WOLFSSL* ssl = NULL;
int ret;
uint8_t local;
uint8_t remote;

if (argc != 4) {
printf("Usage: ./client <CAN interface> <local ID> <remote ID>\n");
return -1;
}

local = strtoul(argv[2], NULL, 16);
remote = strtoul(argv[3], NULL, 16);

sentry_init(local, remote);
ret = setup_connection(argv[1], local, remote);
if (ret) {
return ret;
}

ret = setup_ssl(SERVICE_TYPE_CLIENT, &ctx, &method, &ssl);
if (ret) {
return ret;
}

while(keep_running) {
char *line = NULL;
size_t len = 0;
ssize_t line_size = 0;
line_size = getline(&line, &len, stdin);
if (line_size > 0) {
printf("Sending: %s\n", line);
wolfSSL_send(ssl, line, line_size, 0);
printf("Message sent\n");
}
free(line);
}

close_ssl(ctx, ssl);

return 0;
}
Loading