Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
beba2ab
net: zstream: API to abstract byte stream communication protocols
pfalcon Feb 28, 2018
76b3243
tls_conf: Convenience API for TLS configuration settings
pfalcon Feb 28, 2018
ba62845
mbedtls: config-tls1_2.h: Extended TLS 1.2 config
pfalcon Feb 28, 2018
fa162e9
net: zstream: Add network stream wrapper implementation for mbedTLS
pfalcon Feb 28, 2018
f739a93
samples: net: zstream: Add conversion of sockets/big_http_download
pfalcon Mar 12, 2018
dcdcf88
samples: sockets: http_get: Example of conversion to zstream API
pfalcon Feb 28, 2018
3ed4149
samples: sockets: http_get: Example of conversion to zstream API
pfalcon Mar 9, 2018
ad1c7f5
samples: sockets: http_get: Further conversion to use use TLS stream.
pfalcon Feb 28, 2018
c511840
samples: sockets: big_http_download: Convert to zstream_sock
pfalcon Feb 28, 2018
8b68df0
samples: sockets: big_http_download: Convert to zstream_tls
pfalcon Feb 28, 2018
cb3f0d5
samples: sockets: echo: Convert to zstream_tls
pfalcon Feb 28, 2018
a4a80bb
samples: sockets: echo_async: Convert to zstream_tls.
pfalcon Feb 28, 2018
77ee6a4
net: async_socket: Asynchronous socket library
Jan 26, 2018
9be5a0a
net: mqtt: Port MQTT library to BSD sockets
Jan 26, 2018
decc7aa
net: samples: mqtt_publisher: remove dependence on net_context.h
Jan 26, 2018
6c1a864
async_socket: Convert to zstream API.
pfalcon Mar 1, 2018
d38f803
net: mqtt: Convert to zstream_sock.
pfalcon Mar 1, 2018
4fd6a2c
net: mqtt: Add support for zstream_tls.
pfalcon Mar 2, 2018
57c0716
samples: sockets: http_get: Severely bump hw requirements for TLS.
pfalcon Mar 9, 2018
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
31 changes: 31 additions & 0 deletions ext/lib/crypto/mbedtls/configs/config-tls1_2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018 Linaro Limited.
*
* SPDX-License-Identifier: Apache-2.0
*
* More complete mbedTLS configuration for TLS 1.2 (RFC 5246) for Zephyr,
* extending config-mini-tls1_2.h.
*/

#ifndef MBEDTLS_CONFIG_TLS1_2_H
#define MBEDTLS_CONFIG_TLS1_2_H

#if 1

/* DHE config - slow but moderate code size impact (~5K x86) */
#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
#define MBEDTLS_DHM_C

#else

/* ECDHE config - faster but higher code size impact (~15K x86) */
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
#define MBEDTLS_ECDH_C
#define MBEDTLS_ECP_C

#endif

#include <config-mini-tls1_2.h>

#endif /* MBEDTLS_CONFIG_TLS1_2_H */
75 changes: 75 additions & 0 deletions include/net/async_socket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @file
* @brief Asynchronous sockets API definitions
*
* An API for adapting synchronous BSD socket APIs to applications
* requiring asynchronous callbacks.
*
* Created to ease adaptation of asynchronous IP protocols from
* net_app/net_context to sockets.
*/

/*
* Copyright (c) 2018, Texas Instruments Incorporated
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NET_ASYNC_SOCKET_H
#define __NET_ASYNC_SOCKET_H

#include <net/socket.h>
#include <net/zstream.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Callbacks, similar in semantics to those of net_context.h */
typedef void (*async_connect_cb_t)(int sock,
int status,
void *cb_data);

typedef void (*async_send_cb_t)(int sock,
int bytes_sent,
void *cb_data);

typedef void (*async_recv_cb_t)(int sock,
void *data,
size_t bytes_received,
void *cb_data);

/*
* Errors are the same as the corresponding POSIX socket functions: i.e.,
* a return value of -1 implicitly sets errno.
*/

/* For now, same semantics as socket() call: */
static inline int async_socket(int family, int type, int proto)
{
return socket(family, type, proto);
}

int async_close(int sock, struct zstream *stream);

int async_bind(int sock, const struct sockaddr *addr, socklen_t addrlen);

int async_connect(int sock, const struct sockaddr *addr, socklen_t addrlen,
async_connect_cb_t cb, void *cb_data);

ssize_t async_send(struct zstream *sock, const void *buf, size_t len,
async_send_cb_t cb, void *cb_data, int flags);

/* buf must be unique per sock */
ssize_t async_recv(int sock, struct zstream *stream, void *buf, size_t max_len,
async_recv_cb_t cb, void *cb_data);

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* __NET_ASYNC_SOCKET_H */
47 changes: 34 additions & 13 deletions include/net/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#ifndef _MQTT_H_
#define _MQTT_H_

#include <net/zstream.h>
#include <net/zstream_tls.h>
#include <net/mqtt_types.h>
#if defined(CONFIG_MQTT_LIB_TLS)
#include <net/net_context.h>
#include <net/net_app.h>
#endif

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -63,8 +67,13 @@ enum mqtt_app {
* the state of the received and sent messages.</b>
*/
struct mqtt_ctx {
/** Net app context structure */
struct net_app_ctx net_app_ctx;
int sock;
/* Points to either stream_sock or stream_tls below */
struct zstream *stream;
struct zstream_sock stream_sock;
#if defined(CONFIG_MBEDTLS)
struct zstream_tls stream_tls;
#endif
s32_t net_init_timeout;
s32_t net_timeout;

Expand Down Expand Up @@ -180,7 +189,10 @@ struct mqtt_ctx {
void (*malformed)(struct mqtt_ctx *ctx, u16_t pkt_type);

/* Internal use only */
int (*rcv)(struct mqtt_ctx *ctx, struct net_pkt *);
int (*rcv)(struct mqtt_ctx *ctx, void *buf, size_t len);

/* Receive buffer for async receive callbacks */
void *rcv_buf;

/** Application type, see: enum mqtt_app */
u8_t app_type;
Expand All @@ -200,7 +212,7 @@ struct mqtt_ctx {
*
* @param ctx MQTT context structure
* @param app_type See enum mqtt_app
* @retval 0 always
* @retval 0 on success, and <0 if error
*/
int mqtt_init(struct mqtt_ctx *ctx, enum mqtt_app app_type);

Expand Down Expand Up @@ -366,102 +378,111 @@ int mqtt_tx_unsubscribe(struct mqtt_ctx *ctx, u16_t pkt_id, u8_t items,
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
* @param [in] clean_session MQTT clean session parameter
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_connack(struct mqtt_ctx *ctx, struct net_buf *rx,
int mqtt_rx_connack(struct mqtt_ctx *ctx, void *rx, size_t len,
int clean_session);

/**
* Parses and validates the MQTT PUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_puback(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_puback(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses and validates the MQTT PUBCOMP message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubcomp(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_pubcomp(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses and validates the MQTT PUBREC message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubrec(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_pubrec(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses and validates the MQTT PUBREL message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pubrel(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_pubrel(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses the MQTT PINGRESP message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_pingresp(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_pingresp(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses the MQTT SUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_suback(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_suback(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses the MQTT UNSUBACK message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
*/
int mqtt_rx_unsuback(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_unsuback(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* Parses the MQTT PUBLISH message
*
* @param [in] ctx MQTT context structure
* @param [in] rx Data buffer
* @param [in] len Length of data
*
* @retval 0 on success
* @retval -EINVAL
* @retval -ENOMEM
*/
int mqtt_rx_publish(struct mqtt_ctx *ctx, struct net_buf *rx);
int mqtt_rx_publish(struct mqtt_ctx *ctx, void *rx, size_t len);

/**
* @}
Expand Down
1 change: 1 addition & 0 deletions include/net/mqtt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#ifndef _MQTT_TYPES_H_
#define _MQTT_TYPES_H_

#include <stddef.h>
#include <zephyr/types.h>

#ifdef __cplusplus
Expand Down
32 changes: 32 additions & 0 deletions include/net/tls_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @file
* @brief TLS configuration API definitions
*
* Convenience configuration API for mbedTLS.
*/

/*
* Copyright (c) 2018 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <mbedtls/ssl.h>

struct ztls_cert_key_pair {
mbedtls_x509_crt cert;
mbedtls_pk_context priv_key;
};


int ztls_get_tls_client_conf(mbedtls_ssl_config **out_conf);
int ztls_get_tls_server_conf(mbedtls_ssl_config **out_conf);

int ztls_conf_add_own_cert_key_pair(mbedtls_ssl_config *conf,
struct ztls_cert_key_pair *pair);

int ztls_parse_cert_key_pair(struct ztls_cert_key_pair *pair,
const unsigned char *cert,
size_t cert_len,
const unsigned char *priv_key,
size_t priv_key_len);
65 changes: 65 additions & 0 deletions include/net/zstream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file
* @brief Network stream API definitions
*
* An API to abstract different transport protocols for SOCK_STREAMs, etc.
*/

/*
* Copyright (c) 2018 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __NET_ZSTREAM_H
#define __NET_ZSTREAM_H

#include <sys/types.h>

struct zstream_api;

struct zstream {
const struct zstream_api *api;
};

struct zstream_api {
ssize_t (*read)(struct zstream *stream, void *buf, size_t size);
ssize_t (*write)(struct zstream *stream, const void *buf, size_t size);
int (*flush)(struct zstream *stream);
int (*close)(struct zstream *stream);
};

static inline ssize_t zstream_read(struct zstream *stream, void *buf,
size_t size)
{
return stream->api->read(stream, buf, size);
}

static inline ssize_t zstream_write(struct zstream *stream, const void *buf,
size_t size)
{
return stream->api->write(stream, buf, size);
}

ssize_t zstream_writeall(struct zstream *stream, const void *buf, size_t size,
size_t *written);

static inline ssize_t zstream_flush(struct zstream *stream)
{
return stream->api->flush(stream);
}

static inline ssize_t zstream_close(struct zstream *stream)
{
return stream->api->close(stream);
}

/* Stream object implementation for socket. */
struct zstream_sock {
const struct zstream_api *api;
int fd;
};

int zstream_sock_init(struct zstream_sock *self, int fd);

#endif /* __NET_ZSTREAM_H */
Loading