Skip to content

Add a CAN bus TLS example using ISO-TP transport#279

Merged
dgarske merged 4 commits intowolfSSL:masterfrom
LinuxJedi:add-canbus
Dec 9, 2021
Merged

Add a CAN bus TLS example using ISO-TP transport#279
dgarske merged 4 commits intowolfSSL:masterfrom
LinuxJedi:add-canbus

Conversation

@LinuxJedi
Copy link
Copy Markdown
Member

This provides a simple echo server / client that uses ISO-TP over CAN
bus and wolfSSL for TLS.

Copy link
Copy Markdown
Member

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Just a few easy cleanups and this will be ready.

Comment thread can-bus/client.c Outdated

#include "common.h"

#define ISOTP_BUFSIZE 65535
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the max TLS record size 16KB (16384)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was arbitrary whilst I was getting things running, but a good point. I'll clean up this and the rest.

Comment thread can-bus/client.c Outdated
ctx = wolfSSL_CTX_new(method);
if (!ctx) {
printf("Could not init wolfSSL context\n");
return -1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leak method.

Comment thread can-bus/client.c Outdated
if (wolfSSL_CTX_load_verify_locations(ctx, "client.pem", NULL)
!= SSL_SUCCESS) {
fprintf(stderr, "ERROR: failed to load cert, please check the file.\n");
return -1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will leak ctx. Perhaps a goto with cleanup would be a good option for this example? I realize this may have been a copy paste from another example, but the example should not leak memory in error cases.

Comment thread can-bus/client.c Outdated
}

/* Setup ISO-TP to ID 0x7 and set the buffers */
isotp_init_link(&g_link, 0x7, g_isotpSendBuf, sizeof(g_isotpSendBuf), g_isotpRecvBuf, sizeof(g_isotpRecvBuf));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please limit line length to 80 chars.

Comment thread can-bus/client.c Outdated
printf("Message sent\n");
free(line);
}
can_close();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any cleanup. wolfSSL_free(ssl) or wolfSSL_CTX_free(ctx).

Comment thread can-bus/common.c Outdated
* buffer and just return what wolfSSL is asking for */
int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx)
{
uint8_t data[8];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make the magic 8 data size a macro or enum? I suspect this is the max CAN payload?

Comment thread can-bus/common.h Outdated
int send_ssl(WOLFSSL *ssl, char *buf, int sz, void *ctx);
int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx);

#endif // __CANCOMMON_H__
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use C style /* */ comment.

Comment thread can-bus/server.c Outdated
}
}

can_close();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add proper cleanup.

Comment thread can-bus/common.c
Comment thread can-bus/common.c Outdated
return 1;
}
setsockopt(sock, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));
struct ifreq ifr;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables must all be declared at top of function or in brace section.

@dgarske dgarske assigned LinuxJedi and unassigned dgarske Dec 7, 2021
This provides a simple echo server / client that uses ISO-TP over CAN
bus and wolfSSL for TLS.
@LinuxJedi
Copy link
Copy Markdown
Member Author

New commit does the following:

  • Fixes everything mentioned
  • Moves a lot of common code into common.c
  • Uses a signal handler for clean shutdown

@LinuxJedi LinuxJedi requested a review from dgarske December 8, 2021 11:53
Leak after Ctrl-C in CAN bus client.
Copy link
Copy Markdown
Member

@dgarske dgarske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work here. A few review items have not been addressed.

Also with -Wall -Wextra get these warnings:

$ make clean && make
isotp-c/isotp.c: In function ‘isotp_receive_flow_control_frame’:
isotp-c/isotp.c:222:56: warning: unused parameter ‘link’ [-Wunused-parameter]
  222 | static int isotp_receive_flow_control_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len) {
      |                                             ~~~~~~~~~~~^~~~
isotp-c/isotp.c:222:79: warning: unused parameter ‘message’ [-Wunused-parameter]
  222 | static int isotp_receive_flow_control_frame(IsoTpLink *link, IsoTpCanMessage *message, uint8_t len) {
      |                                                              ~~~~~~~~~~~~~~~~~^~~~~~~
common.c: In function ‘send_ssl’:
common.c:162:34: warning: pointer targets in passing argument 2 of ‘isotp_send’ differ in signedness [-Wpointer-sign]
  162 |     int ret = isotp_send(g_link, buf, sz);
      |                                  ^~~
      |                                  |
      |                                  char *
In file included from common.h:40,
                 from common.c:22:
isotp-c/isotp.h:106:47: note: expected ‘const uint8_t *’ {aka ‘const unsigned char *’} but argument is of type ‘char *’
  106 | int isotp_send(IsoTpLink *link, const uint8_t payload[], uint16_t size);
      |                                 ~~~~~~~~~~~~~~^~~~~~~~~
common.c:157:23: warning: unused parameter ‘ssl’ [-Wunused-parameter]
  157 | int send_ssl(WOLFSSL *ssl, char *buf, int sz, void *ctx)
      |              ~~~~~~~~~^~~
common.c: In function ‘recv_ssl’:
common.c:216:22: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
  216 |     if (copy_buf_len >= sz) {
      |                      ^~
common.c:192:9: warning: unused variable ‘ret’ [-Wunused-variable]
  192 |     int ret;
      |         ^~~
common.c:187:23: warning: unused parameter ‘ssl’ [-Wunused-parameter]
  187 | int recv_ssl(WOLFSSL* ssl, char* buf, int sz, void* ctx)
      |              ~~~~~~~~~^~~
common.c: In function ‘sig_handle’:
common.c:252:21: warning: unused parameter ‘dummy’ [-Wunused-parameter]
  252 | void sig_handle(int dummy)
      |                 ~~~~^~~~~
client.c: In function ‘main’:
client.c:32:9: warning: unused variable ‘length’ [-Wunused-variable]
   32 |     int length;
      |         ^~~~~~
client.c:31:13: warning: unused variable ‘data’ [-Wunused-variable]
   31 |     uint8_t data[CAN_MSG_LEN];
      |             ^~~~

Comment thread can-bus/common.c
Comment thread can-bus/common.c Outdated
WOLFSSL* ssl = NULL;

if (type == SERVICE_TYPE_CLIENT) {
method = wolfTLSv1_2_client_method();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use TLS v1.3? I think it is better suited for this example.

Comment thread can-bus/common.c Outdated
SSL_FILETYPE_PEM);
}

if (ret != SSL_SUCCESS) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use our WOLFSSL_SUCCESS. The shorter is the openssl compat version.

Comment thread can-bus/server.c Outdated
}

while(keep_running) {
char reply[64];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding memset on the reply to make sure it is null terminated. Not all printf's support the %.*s version.

Comment thread can-bus/common.c Outdated
struct can_frame frame;
struct pollfd p[1];

p[0].fd = sock;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can indent here.

Comment thread can-bus/common.c Outdated
p[0].events = POLLIN;

/* Poll for new data */
int retval = poll(p, 1, 10);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All variable decorations need to be at top of function or within brace.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding -Wdeclaration-after-statement in the next commit to detect these in future.

@dgarske dgarske removed their assignment Dec 8, 2021
@LinuxJedi LinuxJedi requested a review from dgarske December 9, 2021 10:58
@LinuxJedi
Copy link
Copy Markdown
Member Author

New commit does the following:

  • 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()

* 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()
* Init `sock` with -1 and make static
* Brace style fix in `isotp_user_debug`
* Use wolfCrypt `MEMORY_E` for memory allocation error
@dgarske dgarske merged commit 7be7d7f into wolfSSL:master Dec 9, 2021
yota22721 pushed a commit to yota22721/wolfssl-examples that referenced this pull request Jan 25, 2025
Add a CAN bus TLS example using ISO-TP transport
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants