-
Notifications
You must be signed in to change notification settings - Fork 8k
net: tls: Implement TLS socket options #9007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2688383
da37810
4e2125c
641d20b
cf3e60b
207b12d
52911df
37c799f
f8f261c
0919178
b49691d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright (c) 2018 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** @file | ||
* @brief TLS credentials management | ||
* | ||
* An API for applications to configure TLS credentials. | ||
*/ | ||
|
||
#ifndef __TLS_CREDENTIAL_H | ||
#define __TLS_CREDENTIAL_H | ||
|
||
/** | ||
* @brief TLS credentials management | ||
* @defgroup tls_credentials TLS credentials management | ||
* @ingroup networking | ||
* @{ | ||
*/ | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** TLS credential types */ | ||
enum tls_credential_type { | ||
TLS_CREDENTIAL_NONE, | ||
TLS_CREDENTIAL_CA_CERTIFICATE, | ||
TLS_CREDENTIAL_SERVER_CERTIFICATE, | ||
TLS_CREDENTIAL_PRIVATE_KEY, | ||
TLS_CREDENTIAL_PSK, | ||
TLS_CREDENTIAL_PSK_ID | ||
}; | ||
|
||
/** Secure tag, a reference to TLS credential | ||
* | ||
* Secure tag can be used to reference credential after it was registered | ||
* in the system. | ||
* | ||
* @note Some TLS credentials come in pairs: | ||
* - TLS_CREDENTIAL_SERVER_CERTIFICATE with TLS_CREDENTIAL_PRIVATE_KEY, | ||
* - TLS_CREDENTIAL_PSK with TLS_CREDENTIAL_PSK_ID. | ||
* Such pairs of credentials must be assigned the same secure tag to be | ||
* correctly handled in the system. | ||
*/ | ||
typedef int sec_tag_t; | ||
|
||
/** | ||
* @brief Add a TLS credential. | ||
* | ||
* @details This function adds a TLS credential, that can be used | ||
* by TLS/DTLS for authentication. | ||
* | ||
* @param tag A security tag that credential will be referenced with. | ||
* @param type A TLS/DTLS credential type. | ||
* @param cred A TLS/DTLS credential. | ||
* @param credlen A TLS/DTLS credential length. | ||
* | ||
* @retval 0 TLS credential successfully added. | ||
* @retval -EACCES Access to the TLS credential subsystem was denied. | ||
* @retval -ENOMEM Not enough memory to add new TLS credential. | ||
* @retval -EEXIST TLS credential of specific tag and type already exists. | ||
*/ | ||
int tls_credential_add(sec_tag_t tag, enum tls_credential_type type, | ||
const void *cred, size_t credlen); | ||
|
||
/** | ||
* @brief Get a TLS credential. | ||
* | ||
* @details This function gets an already registered TLS credential, | ||
* referenced by @p tag secure tag of @p type. | ||
* | ||
* @param tag A security tag of requested credential. | ||
* @param type A TLS/DTLS credential type of requested credential. | ||
* @param cred A buffer for TLS/DTLS credential. | ||
* @param credlen A buffer size on input. TLS/DTLS credential length on output. | ||
* | ||
* @retval 0 TLS credential successfully obtained. | ||
* @retval -EACCES Access to the TLS credential subsystem was denied. | ||
* @retval -ENOENT Requested TLS credential was not found. | ||
* @retval -EFBIG Requested TLS credential does not fit in the buffer provided. | ||
*/ | ||
int tls_credential_get(sec_tag_t tag, enum tls_credential_type type, | ||
void *cred, size_t *credlen); | ||
|
||
/** | ||
* @brief Delete a TLS credential. | ||
* | ||
* @details This function removes a TLS credential, referenced by @p tag | ||
* secure tag of @p type. | ||
* | ||
* @param tag A security tag corresponding to removed credential. | ||
* @param type A TLS/DTLS credential type of removed credential. | ||
* | ||
* @retval 0 TLS credential successfully deleted. | ||
* @retval -EACCES Access to the TLS credential subsystem was denied. | ||
* @retval -ENOENT Requested TLS credential was not found. | ||
*/ | ||
int tls_credential_delete(sec_tag_t tag, enum tls_credential_type type); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
/** | ||
* @} | ||
*/ | ||
|
||
#endif /* __TLS_CREDENTIAL_H */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,11 @@ | |
#include <kernel.h> | ||
#include <net/net_app.h> | ||
|
||
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) | ||
#include <net/tls_credentials.h> | ||
#include "ca_certificate.h" | ||
#endif | ||
|
||
#define sleep(x) k_sleep(x * 1000) | ||
|
||
#endif | ||
|
@@ -144,6 +149,19 @@ void download(struct addrinfo *ai, bool is_tls) | |
CHECK(sock); | ||
printf("sock = %d\n", sock); | ||
|
||
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) | ||
if (is_tls) { | ||
sec_tag_t sec_tag_opt[] = { | ||
CA_CERTIFICATE_TAG, | ||
}; | ||
CHECK(setsockopt(sock, SOL_TLS, TLS_SEC_TAG_LIST, | ||
sec_tag_opt, sizeof(sec_tag_opt))); | ||
|
||
CHECK(setsockopt(sock, SOL_TLS, TLS_HOSTNAME, | ||
host, strlen(host) + 1)); | ||
|
||
} | ||
#endif | ||
|
||
CHECK(connect(sock, ai->ai_addr, ai->ai_addrlen)); | ||
sendall(sock, "GET /", SSTRLEN("GET /")); | ||
sendall(sock, uri_path, strlen(uri_path)); | ||
|
@@ -207,6 +225,11 @@ int main(void) | |
int resolve_attempts = 10; | ||
bool is_tls = false; | ||
|
||
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) | ||
tls_credential_add(CA_CERTIFICATE_TAG, TLS_CREDENTIAL_CA_CERTIFICATE, | ||
ca_certificate, sizeof(ca_certificate)); | ||
#endif | ||
|
||
setbuf(stdout, NULL); | ||
|
||
if (strncmp(download_url, "http://", SSTRLEN("http://")) == 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2018 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef __CA_CERTIFICATE_H__ | ||
#define __CA_CERTIFICATE_H__ | ||
|
||
#define CA_CERTIFICATE_TAG 1 | ||
|
||
/* By default only certificates in DER format are supported. If you want to use | ||
* certificate in PEM format, you can enable support for it in Kconfig. | ||
*/ | ||
|
||
/* Let's Encrypt Authority X3 for https://www.7-zip.org */ | ||
static const unsigned char ca_certificate[] = { | ||
#include "lets_encrypt_x3.der.inc" | ||
}; | ||
|
||
#endif /* __CA_CERTIFICATE_H__ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CONFIG_NET_UDP=n | ||
CONFIG_NET_TCP=y | ||
CONFIG_NET_IPV6=y | ||
CONFIG_NET_IPV4=y | ||
|
||
CONFIG_MAIN_STACK_SIZE=4096 | ||
|
||
# TLS configuration | ||
CONFIG_MBEDTLS=y | ||
CONFIG_MBEDTLS_BUILTIN=y | ||
CONFIG_MBEDTLS_ENABLE_HEAP=y | ||
CONFIG_MBEDTLS_HEAP_SIZE=60000 | ||
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=4096 | ||
|
||
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y | ||
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=2 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (c) 2018 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef __CA_CERTIFICATE_H__ | ||
#define __CA_CERTIFICATE_H__ | ||
|
||
#define CA_CERTIFICATE_TAG 1 | ||
|
||
#define TLS_PEER_HOSTNAME "localhost" | ||
|
||
/* This is the same cert as what is found in net-tools/echo-apps-cert.pem file | ||
*/ | ||
static const unsigned char ca_certificate[] = { | ||
|
||
#include "echo-apps-cert.der.inc" | ||
}; | ||
|
||
#endif /* __CA_CERTIFICATE_H__ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rlubos , so this would be the commit whose message can be updated with the description of 2-stage credential management as suggested in #9007 (comment)