Skip to content

Commit

Permalink
net: sockets: pfl-example: Add TLS support using Zstream API.
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
  • Loading branch information
pfalcon committed Jun 18, 2018
1 parent 8ed2ebf commit a354713
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion samples/net/sockets/pfl-example/prj.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# General config
CONFIG_NEWLIB_LIBC=y
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_MAIN_STACK_SIZE=6000
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_CFG_FILE="config-mini-tls1_2.h"
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384
Expand Down
33 changes: 31 additions & 2 deletions samples/net/sockets/pfl-example/src/example.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <stdio.h>

#ifndef __ZEPHYR__

#include <unistd.h>
Expand All @@ -13,6 +15,10 @@

#endif

#include <net/tls_conf.h>
#include <net/zstream.h>
#include <net/zstream_tls.h>

static char msg[] = "Lorem ipsum dolor sit amet";
static char response[sizeof(msg)] = {};

Expand All @@ -22,6 +28,11 @@ int main(void)
struct addrinfo *a;
int r, s;

struct zstream_sock stream_sock;
struct zstream_tls stream_tls;
struct zstream *stream;
mbedtls_ssl_config *tls_conf;

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;

Expand All @@ -40,17 +51,35 @@ int main(void)
return r;
}

r = send(s, msg, sizeof(msg), 0);
zstream_sock_init(&stream_sock, s);
stream = (struct zstream *)&stream_sock;

if (ztls_get_tls_client_conf(&tls_conf) < 0) {
printf("Unable to initialize TLS config\n");
return 1;
}
mbedtls_ssl_conf_authmode(tls_conf, MBEDTLS_SSL_VERIFY_NONE);

r = zstream_tls_init(&stream_tls, stream, tls_conf, CONFIG_NET_APP_PEER_IPV4_ADDR);
if (r < 0) {
printf("Unable to initialize TLS\n");
return 1;
}
stream = (struct zstream *)&stream_tls;

r = zstream_write(stream, msg, sizeof(msg));
if (r != sizeof(msg)) {
return r;
}

r = recv(s, response, sizeof(response), 0);
r = zstream_read(stream, response, sizeof(response));
if (r != sizeof(msg)) {
return r;
}

close(s);

printf("success\n");

return 0;
}

0 comments on commit a354713

Please sign in to comment.