Skip to content

Commit

Permalink
net: sockets: tls: Add missing symbols for a few options
Browse files Browse the repository at this point in the history
`TLS_PEER_VERIFY` and `TLS_DTLS_ROLE` options accept specific values,
yet no symbols were defined for them. In result, magic numbers were used
in several places, making the code less readable.

Fix this issue, by adding the missing symbols to the `socket.h` header,
and using them in places where related socket options are set.

Signed-off-by: Robert Lubos <[email protected]>
  • Loading branch information
rlubos authored and jukkar committed Jan 20, 2020
1 parent 549fdb9 commit 665d195
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doc/reference/networking/mqtt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ additional configuration information:
struct mqtt_sec_config *tls_config = &client_ctx.transport.tls.config;
tls_config->peer_verify = 2;
tls_config->peer_verify = TLS_PEER_VERIFY_REQUIRED;
tls_config->cipher_list = NULL;
tls_config->sec_tag_list = m_sec_tags;
tls_config->sec_tag_count = ARRAY_SIZE(m_sec_tags);
Expand Down
9 changes: 9 additions & 0 deletions include/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ struct zsock_pollfd {

/** @} */

/* Valid values for TLS_PEER_VERIFY option */
#define TLS_PEER_VERIFY_NONE 0 /**< Peer verification disabled. */
#define TLS_PEER_VERIFY_OPTIONAL 1 /**< Peer verification optional. */
#define TLS_PEER_VERIFY_REQUIRED 2 /**< Peer verification required. */

/* Valid values for TLS_DTLS_ROLE option */
#define TLS_DTLS_ROLE_CLIENT 0 /**< Client role in a DTLS session. */
#define TLS_DTLS_ROLE_SERVER 1 /**< Server role in a DTLS session. */

struct zsock_addrinfo {
struct zsock_addrinfo *ai_next;
int ai_flags;
Expand Down
2 changes: 1 addition & 1 deletion lib/updatehub/updatehub.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static bool start_coap_client(void)
}

#if defined(CONFIG_UPDATEHUB_DTLS)
int verify = 0;
int verify = TLS_PEER_VERIFY_NONE;
sec_tag_t sec_list[] = { CA_CERTIFICATE_TAG };
int protocol = IPPROTO_DTLS_1_2;
char port[] = "5684";
Expand Down
2 changes: 1 addition & 1 deletion samples/net/cloud/google_iot_mqtt/src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void mqtt_startup(char *hostname, int port)
struct mqtt_sec_config *tls_config =
&client->transport.tls.config;

tls_config->peer_verify = 2;
tls_config->peer_verify = TLS_PEER_VERIFY_REQUIRED;
tls_config->cipher_list = NULL;
tls_config->sec_tag_list = m_sec_tags;
tls_config->sec_tag_count = ARRAY_SIZE(m_sec_tags);
Expand Down
2 changes: 1 addition & 1 deletion samples/net/cloud/mqtt_azure/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static void client_init(struct mqtt_client *client)

tls_config = &client->transport.tls.config;

tls_config->peer_verify = 2;
tls_config->peer_verify = TLS_PEER_VERIFY_REQUIRED;
tls_config->cipher_list = NULL;
tls_config->sec_tag_list = m_sec_tags;
tls_config->sec_tag_count = ARRAY_SIZE(m_sec_tags);
Expand Down
2 changes: 1 addition & 1 deletion samples/net/mqtt_publisher/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ static void client_init(struct mqtt_client *client)

struct mqtt_sec_config *tls_config = &client->transport.tls.config;

tls_config->peer_verify = 2;
tls_config->peer_verify = TLS_PEER_VERIFY_REQUIRED;
tls_config->cipher_list = NULL;
tls_config->sec_tag_list = m_sec_tags;
tls_config->sec_tag_count = ARRAY_SIZE(m_sec_tags);
Expand Down
2 changes: 1 addition & 1 deletion samples/net/sockets/echo_server/src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static int start_udp_proto(struct data *data, struct sockaddr *bind_addr,
PSK_TAG,
#endif
};
int role = 1;
int role = TLS_DTLS_ROLE_SERVER;

ret = setsockopt(data->udp.sock, SOL_TLS, TLS_SEC_TAG_LIST,
sec_tag_list, sizeof(sec_tag_list));
Expand Down

0 comments on commit 665d195

Please sign in to comment.