Cleanups to CAN bus

* Add warnings to CFLAGS (except for isotp)
* Fix items found by warnings
* Remove %.*s usage
* Fix indentation issue
* Use WOLFSSL_* instead of SSL_* constants
* Switch to TLS v1.3
* Add missing wolfSSL_Cleanup()
pull/279/head
Andrew Hutchings 2021-12-09 10:51:38 +00:00
parent 0242c880ed
commit 809acb3e3c
6 changed files with 54 additions and 54 deletions

View File

@ -1,6 +1,6 @@
CC=gcc CC=gcc
LIBS=-lwolfssl LIBS=-lwolfssl
CFLAGS=-ggdb3 -Iisotp-c -Wno-cpp CFLAGS=-Iisotp-c -Wno-cpp -Wall -Wextra -Wdeclaration-after-statement
COMMON_OBJS=isotp-c/isotp.o common.o COMMON_OBJS=isotp-c/isotp.o common.o
CLIENT_OBJS=client.o CLIENT_OBJS=client.o
@ -8,6 +8,9 @@ SERVER_OBJS=server.o
all: client server all: client server
isotp-c/isotp.o: isotp-c/isotp.c
@$(CC) -c $< -o $@
%.o: %.c %.o: %.c
@$(CC) -c $< -o $@ $(CFLAGS) @$(CC) -c $< -o $@ $(CFLAGS)

View File

@ -35,32 +35,26 @@ server vcan0
On the client you will see (byte numbers will vary): On the client you will see (byte numbers will vary):
``` ```
Sending 164 bytes Sending 242 bytes
Receiving 95 bytes Receiving 128 bytes
Receiving 954 bytes Receiving 28 bytes
Receiving 338 bytes Receiving 974 bytes
Receiving 9 bytes Receiving 286 bytes
Sending 75 bytes Receiving 58 bytes
Sending 6 bytes Sending 58 bytes
Sending 45 bytes
Receiving 6 bytes
Receiving 45 bytes
SSL handshake done! SSL handshake done!
``` ```
And on the server: And on the server:
``` ```
Receiving 164 bytes Receiving 242 bytes
Sending 95 bytes Sending 128 bytes
Sending 954 bytes Sending 28 bytes
Sending 338 bytes Sending 974 bytes
Sending 9 bytes Sending 286 bytes
Receiving 75 bytes Sending 58 bytes
Receiving 6 bytes Receiving 58 bytes
Receiving 45 bytes
Sending 6 bytes
Sending 45 bytes
SSL handshake done! SSL handshake done!
``` ```
@ -69,22 +63,18 @@ Once you see the message "SSL handshake done!" on both consoles you can enter te
For example, on the client if we type "Hello world, this is a TLS test!": For example, on the client if we type "Hello world, this is a TLS test!":
``` ```
Hello world, this is a TLS test! Hello world! This is a CAN bus test!
Sending: Hello world! This is a CAN bus test!
Sending: Hello world, this is a TLS test! Sending 59 bytes
Sending 62 bytes
Message sent Message sent
``` ```
The server will echo: The server will echo:
``` ```
Receiving 62 bytes Receiving 59 bytes
Got message: Hello world! This is a CAN bus test!
Got message: Hello world, this is a TLS test!
``` ```
## Cleaning Up ## Cleaning Up

View File

@ -28,8 +28,6 @@ int main(int argc, char *argv[])
WOLFSSL_CTX *ctx = NULL; WOLFSSL_CTX *ctx = NULL;
WOLFSSL_METHOD* method = NULL; WOLFSSL_METHOD* method = NULL;
WOLFSSL* ssl = NULL; WOLFSSL* ssl = NULL;
uint8_t data[CAN_MSG_LEN];
int length;
int ret; int ret;
if (argc != 2) { if (argc != 2) {
@ -53,7 +51,7 @@ int main(int argc, char *argv[])
ssize_t line_size = 0; ssize_t line_size = 0;
line_size = getline(&line, &len, stdin); line_size = getline(&line, &len, stdin);
if (line_size > 0) { if (line_size > 0) {
printf("\nSending: %.*s\n\n", (int)line_size, line); printf("Sending: %s\n", line);
wolfSSL_send(ssl, line, line_size, 0); wolfSSL_send(ssl, line, line_size, 0);
printf("Message sent\n"); printf("Message sent\n");
} }

View File

@ -74,6 +74,7 @@ int isotp_user_send_can(const uint32_t arbitration_id, const uint8_t* data,
/* Our CAN bus receive function */ /* Our CAN bus receive function */
int can_receive(uint8_t data[CAN_MSG_LEN], int *length) { int can_receive(uint8_t data[CAN_MSG_LEN], int *length) {
int nbytes; int nbytes;
int ret;
struct can_frame frame; struct can_frame frame;
struct pollfd p[1]; struct pollfd p[1];
@ -81,13 +82,13 @@ int can_receive(uint8_t data[CAN_MSG_LEN], int *length) {
p[0].events = POLLIN; p[0].events = POLLIN;
/* Poll for new data */ /* Poll for new data */
int retval = poll(p, 1, 10); ret = poll(p, 1, 10);
if (retval < 0) { if (ret < 0) {
perror("Poll error\n"); perror("Poll error\n");
return 1; return 1;
} }
else if (retval == 0) { else if (ret == 0) {
/* No data */ /* No data */
*length = 0; *length = 0;
return EAGAIN; return EAGAIN;
@ -159,7 +160,8 @@ int send_ssl(WOLFSSL *ssl, char *buf, int sz, void *ctx)
uint8_t data[CAN_MSG_LEN]; uint8_t data[CAN_MSG_LEN];
int length; int length;
IsoTpLink *g_link = (struct IsoTpLink*)ctx; IsoTpLink *g_link = (struct IsoTpLink*)ctx;
int ret = isotp_send(g_link, buf, sz); int ret = isotp_send(g_link, (uint8_t*)buf, sz);
(void) ssl;
printf("Sending %d bytes\n", sz); printf("Sending %d bytes\n", sz);
if (ret) { if (ret) {
@ -189,8 +191,8 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx)
uint8_t data[CAN_MSG_LEN]; uint8_t data[CAN_MSG_LEN];
int data_len; int data_len;
uint16_t msg_len = 0; uint16_t msg_len = 0;
int ret;
IsoTpLink *g_link = (struct IsoTpLink*)ctx; IsoTpLink *g_link = (struct IsoTpLink*)ctx;
(void) ssl;
if (!copy_buf_len) { if (!copy_buf_len) {
while (isotp_receive(g_link, copy_buf, ISOTP_BUFSIZE, &msg_len) while (isotp_receive(g_link, copy_buf, ISOTP_BUFSIZE, &msg_len)
@ -213,7 +215,7 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx)
} }
} }
if (copy_buf_len >= sz) { if (copy_buf_len >= (size_t)sz) {
memcpy(buf, copy_buf_ptr, sz); memcpy(buf, copy_buf_ptr, sz);
copy_buf_ptr+= sz; copy_buf_ptr+= sz;
copy_buf_len-= sz; copy_buf_len-= sz;
@ -231,11 +233,11 @@ int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx)
void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl) void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
{ {
if (ssl) { if (ssl) {
int ret = SSL_SHUTDOWN_NOT_DONE; int ret = WOLFSSL_SHUTDOWN_NOT_DONE;
while (ret == SSL_SHUTDOWN_NOT_DONE) { while (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
ret = wolfSSL_shutdown(ssl); ret = wolfSSL_shutdown(ssl);
} }
if (ret != SSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
char buffer[ERR_MSG_LEN]; char buffer[ERR_MSG_LEN];
int err = wolfSSL_get_error(ssl, ret); int err = wolfSSL_get_error(ssl, ret);
fprintf(stderr, "Error shutting down TLS connection: %d, %s", fprintf(stderr, "Error shutting down TLS connection: %d, %s",
@ -247,10 +249,12 @@ void close_ssl(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
wolfSSL_free(ssl); wolfSSL_free(ssl);
wolfSSL_CTX_free(ctx); wolfSSL_CTX_free(ctx);
wolfSSL_Cleanup();
} }
void sig_handle(int dummy) void sig_handle(int dummy)
{ {
(void) dummy;
keep_running = 0; keep_running = 0;
} }
@ -283,9 +287,9 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx,
WOLFSSL* ssl = NULL; WOLFSSL* ssl = NULL;
if (type == SERVICE_TYPE_CLIENT) { if (type == SERVICE_TYPE_CLIENT) {
method = wolfTLSv1_2_client_method(); method = wolfTLSv1_3_client_method();
} else { } else {
method = wolfTLSv1_2_server_method(); method = wolfTLSv1_3_server_method();
} }
if (!method) { if (!method) {
@ -311,10 +315,10 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx,
ret = wolfSSL_CTX_load_verify_locations(ctx, "client.pem", NULL); ret = wolfSSL_CTX_load_verify_locations(ctx, "client.pem", NULL);
} else { } else {
ret = wolfSSL_CTX_use_certificate_file(ctx, "server.pem", ret = wolfSSL_CTX_use_certificate_file(ctx, "server.pem",
SSL_FILETYPE_PEM); WOLFSSL_FILETYPE_PEM);
} }
if (ret != SSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load cert, " fprintf(stderr, "ERROR: failed to load cert, "
"please check the file.\n"); "please check the file.\n");
close_ssl(ctx, NULL); close_ssl(ctx, NULL);
@ -323,7 +327,7 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx,
if (type == SERVICE_TYPE_SERVER) { if (type == SERVICE_TYPE_SERVER) {
if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, "server.key", if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, "server.key",
SSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) { WOLFSSL_FILETYPE_PEM)) != WOLFSSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load key file, " fprintf(stderr, "ERROR: failed to load key file, "
"please check the file.\n"); "please check the file.\n");
close_ssl(ctx, NULL); close_ssl(ctx, NULL);
@ -350,7 +354,7 @@ int setup_ssl(enum service_type type, WOLFSSL_CTX **new_ctx,
wolfSSL_set_using_nonblock(ssl, 1); wolfSSL_set_using_nonblock(ssl, 1);
if (ret != SSL_SUCCESS) { if (ret != WOLFSSL_SUCCESS) {
char buffer[ERR_MSG_LEN]; char buffer[ERR_MSG_LEN];
int err = wolfSSL_get_error(ssl, ret); int err = wolfSSL_get_error(ssl, ret);
fprintf(stderr, "ERROR: failed to connect using wolfSSL: %d, %s\n", fprintf(stderr, "ERROR: failed to connect using wolfSSL: %d, %s\n",

View File

@ -38,6 +38,7 @@
#include <signal.h> #include <signal.h>
#include <isotp.h> #include <isotp.h>
#include <wolfssl/options.h>
#include <wolfssl/ssl.h> #include <wolfssl/ssl.h>
#define ISOTP_BUFSIZE 16384 #define ISOTP_BUFSIZE 16384

View File

@ -21,6 +21,8 @@
#include "common.h" #include "common.h"
#define RECV_MSG_LEN 64
extern volatile int keep_running; extern volatile int keep_running;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -46,10 +48,12 @@ int main(int argc, char *argv[])
} }
while(keep_running) { while(keep_running) {
char reply[64]; int input;
int input = wolfSSL_read(ssl, reply, sizeof(reply)); char reply[RECV_MSG_LEN];
memset(reply, 0, RECV_MSG_LEN);
input = wolfSSL_read(ssl, reply, RECV_MSG_LEN);
if (input > 0) { if (input > 0) {
printf("\n\nGot message: %.*s\n", input, reply); printf("Got message: %s\n", reply);
} }
} }