mirror of
https://github.com/obgm/libcoap.git
synced 2026-11-27 01:07:10 +03:00
The use of the verify_peer_cert and require_peer_cert variables in the coap_dtls_pki_t structure was giving inconsistent results across all the TLS libraries. This primarily was down to the large numbers of options available to control the TLS handshakes in OpenSSL compared to the limited control available to mbedTLS port which followed later. require_peer_cert is not easy to control in mbedTLS as it is an implicit configuration based on how other, not always related, items were configured. require_peer_cert was used by the server to control whether the client could use anonymous certificates or not. This is now controlled by verify_peer_cert. require_peer_cert variable has been replaced with check_common_ca, so that the OpenSSL functionality can continue, but enable GnuTLS / mbedTLS to produce the same results. This allows peers to mutually authenticate (because the peer certs are signed by the same common CA) or not which was in effect controlled by verify_peer_cert previously. If check_common_ca is set, then allow_self_signed is ignored. If is_rpk_not_cert is set, then all certificate validation is ignored. In the examples, use of the -R option unsets check_common_ca, so disables mutual authentication support by having a common CA. This was needed as mbedTLS and GnuTLS only have a single trust store for CAs. configure.ac: Increase the number of mbed libraries to use when checking for mbedTLS. examples/client.c: examples/coap-rd.c: examples/coap-server.c: Add in -n (unset verify_peer_cert) option. In the case of coap-server and coap-rd, make -n refer to verify_peer_cert. Add in TLS library capabilites in usage(). Update usage() documentation as appropriate, with some changes to fit everything into a 80 column output. include/coap2/coap_dtls.h: include/coap2/net.h: Update with variable changes, and make the coap_dtls_pki_t parameter const for the *_context_set_pki() functions. man/coap-client.txt.in: man/coap-rd.txt.in: man/coap-server.txt.in: Update documentation to reflect the examples option usage. man/coap_context.txt.in: man/coap_encryption.txt.in: man/coap_session.txt.in: Update with the new variable name and document as appropriate. src/coap_gnutls.c src/coap_mbedtls.c src/coap_notls.c src/coap_openssl.c coap_tinydtls.c Update to make variable usage consistent. Update logging from LOG_WARNING to LOG_INFO where there is an override of a PKI check failure by one of the coap_dtls_pki_t variables. Timing window closed for TLS where the peer does not like a certificate, sends fatal alert and closes connection. local then fails on writing the next handshake - but now also reads in alert and reports on it. src/coap_io.c: Update logging from LOG_WARNING to LOG_INFO for EPIPE or ECONNRESET errors in coap_socket_write(). src/net.c: Handle the const coap_dtls_pki_t parameter in coap_context_set_pki() function.
454 lines
15 KiB
Plaintext
454 lines
15 KiB
Plaintext
// -*- mode:doc; -*-
|
|
// vim: set syntax=asciidoc,tw=0:
|
|
|
|
coap_session(3)
|
|
=================
|
|
:doctype: manpage
|
|
:man source: coap_session
|
|
:man version: @PACKAGE_VERSION@
|
|
:man manual: libcoap Manual
|
|
|
|
NAME
|
|
----
|
|
coap_session,
|
|
coap_new_client_session,
|
|
coap_new_client_session_psk2,
|
|
coap_new_client_session_pki,
|
|
coap_session_reference,
|
|
coap_session_release,
|
|
coap_session_set_mtu,
|
|
coap_session_max_pdu_size,
|
|
coap_session_set_app_data,
|
|
coap_session_get_app_data,
|
|
coap_tcp_is_supported
|
|
- Work with CoAP sessions
|
|
|
|
SYNOPSIS
|
|
--------
|
|
*#include <coap@LIBCOAP_API_VERSION@/coap.h>*
|
|
|
|
*coap_session_t *coap_new_client_session(coap_context_t *_context_,
|
|
const coap_address_t *_local_if_, const coap_address_t *_server_,
|
|
coap_proto_t _proto_);*
|
|
|
|
*coap_session_t *coap_new_client_session_psk2(coap_context_t *_context_,
|
|
const coap_address_t *_local_if_, const coap_address_t *_server_, coap_proto_t
|
|
_proto_, coap_dtls_cpsk_t *_setup_data_);*
|
|
|
|
*coap_session_t *coap_new_client_session_pki(coap_context_t *_context_,
|
|
const coap_address_t *_local_if_, const coap_address_t *_server_, coap_proto_t
|
|
_proto_, coap_dtls_pki_t *_setup_data_);*
|
|
|
|
*coap_session_t *coap_session_reference(coap_session_t *_session_);*
|
|
|
|
*void coap_session_release(coap_session_t *_session_);*
|
|
|
|
*void coap_session_set_mtu(coap_session_t *_session_, unsigned _mtu_);*
|
|
|
|
*size_t coap_session_max_pdu_size(const coap_session_t *_session_);*
|
|
|
|
*void coap_session_set_app_data(coap_session_t *_session_, void *_data_);*
|
|
|
|
*void *coap_session_get_app_data(const coap_session_t *_session_);*
|
|
|
|
*int coap_tcp_is_supported(void);*
|
|
|
|
Link with *-lcoap-@LIBCOAP_API_VERSION@*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
|
|
*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
|
|
or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls* depending on your (D)TLS library
|
|
type.
|
|
|
|
DESCRIPTION
|
|
-----------
|
|
This man page focuses on the CoAP Session.
|
|
|
|
The CoAP stack's global state is stored in a coap_context_t Context object.
|
|
Resources, Endpoints and Sessions are associated with this context object.
|
|
There can be more than one coap_context_t object per application, it is up to
|
|
the application to manage each one accordingly.
|
|
|
|
A CoAP Session maintains the state of an ongoing connection between a Client
|
|
and Server which is stored in a coap_session_t Session object.
|
|
|
|
The Session network traffic can be encrypted or un-encrypted if there is an
|
|
underlying TLS library.
|
|
|
|
If TLS is going to be used for encrypting the network traffic, then the TLS
|
|
information for Pre-Shared Keys (PSK) or Public Key Infrastructure (PKI) needs
|
|
to be configured before any network traffic starts to flow. For Servers, this
|
|
has to be done before the Endpoint is created, for Clients, this is done
|
|
during the Client Session set up.
|
|
|
|
For Servers, all the encryption information is held internally by the TLS
|
|
Context level and the CoAP Context level as the Server is listening for new
|
|
incoming traffic based on the Endpoint definition. The TLS and CoAP session
|
|
will not get built until the new traffic starts, which is done by the libcoap
|
|
library, with the session having a reference count of 1.
|
|
|
|
For Clients, all the encryption information can be held at the TLS Context and
|
|
CoAP Context levels, or at the TLS Session and CoAP Session levels. If
|
|
defined at the Context level, then when Sessions are created, they will
|
|
inherit the Context definitions, unless they have separately been defined for
|
|
the Session level, in which case the Session version will get used.
|
|
Typically the information will be configured at the Session level for Clients.
|
|
|
|
In principle the set-up sequence for CoAP Servers looks like
|
|
(see coap_context(3) for further information on the functions)
|
|
----
|
|
coap_new_context()
|
|
coap_context_set_pki_root_cas() - if the root CAs need to be updated and PKI
|
|
coap_context_set_pki() and/or coap_context_set_psk2() - if encryption is required
|
|
coap_new_endpoint()
|
|
----
|
|
|
|
Multiple endpoints can be set up per Context, each listening for a new traffic
|
|
flow with different TCP/UDP protocols, TLS protocols, port numbers etc. When a
|
|
new traffic flow is started, then the CoAP library will create and start a new
|
|
server session.
|
|
|
|
In principle the set-up sequence for CoAP Clients looks like
|
|
----
|
|
coap_new_context()
|
|
coap_context_set_pki_root_cas() if the root CAs need to be updated and PKI
|
|
coap_new_client_session(), coap_new_client_session_pki() or coap_new_client_session_psk2()
|
|
----
|
|
|
|
Multiple client sessions are supported per Context.
|
|
|
|
Different CoAP protocols can be defined for _proto_ - the current supported
|
|
list is:
|
|
|
|
[source, c]
|
|
----
|
|
COAP_PROTO_UDP
|
|
COAP_PROTO_DTLS
|
|
COAP_PROTO_TCP
|
|
COAP_PROTO_TLS
|
|
----
|
|
|
|
The *coap_tcp_is_supported*() function returns 1 if support for TCP is
|
|
enabled, otherwise 0. Likewise *coap_dtls_is_supported*() and
|
|
*coap_tls_is_supported*() can be used for checking whether the underlying
|
|
(D)TLS protocol support is available. See *coap_tls_library(3)* for further
|
|
information.
|
|
|
|
The *coap_new_client_session*() function initiates a new client session
|
|
associated with _context_ to the specified _server_ using the CoAP protocol
|
|
_proto_. If the port is not specified in _server_, then the default CoAP
|
|
port is used. Normally _local_if_ would be set to NULL, but by specifying
|
|
_local_if_ the source of the network session can be bound to a specific IP
|
|
address or port. The session will initially have a reference count of 1.
|
|
|
|
The *coap_new_client_session_pki*() function, for a specific _context_, is
|
|
used to configure the TLS context using the _setup_data_ variables as defined
|
|
in the coap_dtls_pki_t structure in the newly created session -
|
|
see *coap_encryption*(3). The connection is to the specified _server_ using
|
|
the CoAP protocol _proto_. If the port is not specified in _server_, then the
|
|
default CoAP port is used. Normally _local_if_ would be set to NULL, but by
|
|
specifying _local_if_ the source of the network session can be bound to a
|
|
specific IP address or port. The session will initially have a reference count
|
|
of 1.
|
|
|
|
The *coap_new_client_session_psk2*() function, for a specific _context_, is
|
|
used to configure the TLS context using the _setup_data_ variables as defined
|
|
in the coap_dtls_cpsk_t structure in the newly created session -
|
|
see *coap_encryption*(3). The connection is to the specified _server_ using
|
|
the CoAP protocol _proto_. If the port is not specified in _server_, then the
|
|
default CoAP port is used. Normally _local_if_ would be set to NULL, but by
|
|
specifying _local_if_ the source of the network session can be bound to a
|
|
specific IP address or port. The session will initially have a reference count
|
|
of 1.
|
|
|
|
The *coap_session_reference*() is used to increment the reference count of
|
|
the _session_. Incrementing the reference count by an application means that
|
|
the library will not inadvertently remove the session when it has finished
|
|
processing the session.
|
|
|
|
The *coap_session_release*() function must be used to decrement the _session_
|
|
reference count, which when it gets to 0, will free off the session if this is
|
|
a Client, which then clears all entries from the receive queue and send
|
|
queue. If the reference count goes to 0 for a Server, then the _session_ is
|
|
added to a free pool ready for subsequent re-use. If the Server _session_ is
|
|
not used for 5 minutes, then it will get completely freed off.
|
|
|
|
The *coap_sesson_set_default_mtu*() function is used to set the MTU size
|
|
(the maximum message size) of the data in a packet, excluding any IP or
|
|
TCP/UDP overhead to _mtu_ for the _session_.
|
|
|
|
The *coap_session_max_pdu_size*() funcition is used to get the maximum MTU
|
|
size of the data for the _session_.
|
|
|
|
The *coap_session_set_app_data*() funstion is used to define a _data_ pointer
|
|
for the _session_ which can then be retrieved at a later date.
|
|
|
|
The *coap_session_get_app_data*() function is used to retrieve the data
|
|
pointer previously defined by *coap_session_set_app_data*().
|
|
|
|
RETURN VALUES
|
|
-------------
|
|
*coap_new_client_session*(), *coap_new_client_session_psk2*(),
|
|
*coap_new_client_session_pki*() functions returns a newly created client
|
|
session or NULL if there is a creation failure.
|
|
|
|
*coap_session_reference*() function returns a pointer to the session.
|
|
|
|
*coap_session_get_app_data*() function return a previously defined pointer.
|
|
|
|
*coap_session_max_pdu_size*() function returns the MTU size.
|
|
|
|
The *coap_tcp_is_supported*() function returns 1 if support for TCP is
|
|
enabled, otherwise 0.
|
|
|
|
EXAMPLES
|
|
--------
|
|
*CoAP Client Non-Encrypted Setup*
|
|
[source, c]
|
|
----
|
|
#include <coap@LIBCOAP_API_VERSION@/coap.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
static coap_session_t *
|
|
setup_client_session (struct in_addr ip_address) {
|
|
coap_session_t *session;
|
|
coap_address_t server;
|
|
/* See coap_context(3) */
|
|
coap_context_t *context = coap_new_context(NULL);
|
|
|
|
if (!context)
|
|
return NULL;
|
|
|
|
coap_address_init(&server);
|
|
server.addr.sa.sa_family = AF_INET;
|
|
server.addr.sin.sin_addr = ip_address;
|
|
server.addr.sin.sin_port = htons (5683);
|
|
|
|
session = coap_new_client_session(context, NULL, &server, COAP_PROTO_UDP);
|
|
if (!session) {
|
|
coap_free_context(context);
|
|
return NULL;
|
|
}
|
|
/* The context is in session->context */
|
|
return session;
|
|
}
|
|
----
|
|
|
|
*CoAP Client PKI Setup*
|
|
[source, c]
|
|
----
|
|
#include <coap@LIBCOAP_API_VERSION@/coap.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
static int
|
|
verify_cn_callback(const char *cn,
|
|
const uint8_t *asn1_public_cert,
|
|
size_t asn1_length,
|
|
coap_session_t *c_session,
|
|
unsigned int depth,
|
|
int validated,
|
|
void *arg
|
|
) {
|
|
/* Remove (void) definition if variable is used */
|
|
(void)cn;
|
|
(void)asn1_public_cert;
|
|
(void)asn1_length;
|
|
(void)c_session;
|
|
(void)depth;
|
|
(void)validated;
|
|
(void)arg;
|
|
|
|
/* Check that the CN is valid */
|
|
|
|
/* ... */
|
|
|
|
return 1;
|
|
}
|
|
|
|
static coap_session_t *
|
|
setup_client_session_pki (struct in_addr ip_address,
|
|
const char *public_cert_file,
|
|
const char *private_key_file,
|
|
const char *ca_file
|
|
) {
|
|
coap_session_t *session;
|
|
coap_address_t server;
|
|
coap_dtls_pki_t dtls_pki;
|
|
/* See coap_context(3) */
|
|
coap_context_t *context = coap_new_context(NULL);
|
|
|
|
if (!context)
|
|
return NULL;
|
|
|
|
coap_address_init(&server);
|
|
server.addr.sa.sa_family = AF_INET;
|
|
server.addr.sin.sin_addr = ip_address;
|
|
server.addr.sin.sin_port = htons (5684);
|
|
|
|
memset (&dtls_pki, 0, sizeof (dtls_pki));
|
|
|
|
/* See coap_encryption(3) */
|
|
dtls_pki.version = COAP_DTLS_PKI_SETUP_VERSION;
|
|
dtls_pki.verify_peer_cert = 1;
|
|
dtls_pki.check_common_ca = 1;
|
|
dtls_pki.allow_self_signed = 1;
|
|
dtls_pki.allow_expired_certs = 1;
|
|
dtls_pki.cert_chain_validation = 1;
|
|
dtls_pki.cert_chain_verify_depth = 1;
|
|
dtls_pki.check_cert_revocation = 1;
|
|
dtls_pki.allow_no_crl = 1;
|
|
dtls_pki.allow_expired_crl = 1;
|
|
dtls_pki.allow_bad_md_hash = 0;
|
|
dtls_pki.allow_short_rsa_length = 0;
|
|
dtls_pki.is_rpk_not_cert = 0; /* Set to 1 if RPK */
|
|
dtls_pki.validate_cn_call_back = verify_cn_callback;
|
|
dtls_pki.cn_call_back_arg = NULL;
|
|
dtls_pki.validate_sni_call_back = NULL;
|
|
dtls_pki.sni_call_back_arg = NULL;
|
|
dtls_pki.additional_tls_setup_call_back = NULL;
|
|
dtls_pki.client_sni = NULL;
|
|
dtls_pki.pki_key.key_type = COAP_PKI_KEY_PEM;
|
|
dtls_pki.pki_key.key.pem.ca_file = ca_file;
|
|
dtls_pki.pki_key.key.pem.public_cert = public_cert_file;
|
|
dtls_pki.pki_key.key.pem.private_key = private_key_file;
|
|
|
|
session = coap_new_client_session_pki(context, NULL, &server,
|
|
COAP_PROTO_DTLS, &dtls_pki);
|
|
if (!session) {
|
|
coap_free_context(context);
|
|
return NULL;
|
|
}
|
|
/* The context is in session->context */
|
|
return session;
|
|
}
|
|
----
|
|
|
|
*CoAP Client PSK Setup*
|
|
[source, c]
|
|
----
|
|
#include <coap@LIBCOAP_API_VERSION@/coap.h>
|
|
|
|
#include <stdio.h>
|
|
#include <netinet/in.h>
|
|
|
|
#ifndef min
|
|
#define min(a,b) ((a) < (b) ? (a) : (b))
|
|
#endif
|
|
|
|
static const coap_dtls_cpsk_info_t *
|
|
verify_ih_callback(coap_str_const_t *hint,
|
|
coap_session_t *c_session,
|
|
void *arg
|
|
) {
|
|
coap_dtls_cpsk_info_t *psk_info = (coap_dtls_cpsk_info_t *)arg;
|
|
/* Remove (void) definition if variable is used */
|
|
(void)c_session;
|
|
|
|
coap_log(LOG_INFO, "Identity Hint '%.*s' provided\n", (int)hint->length, hint->s);
|
|
|
|
/* Just use the defined information for now as passed in by arg */
|
|
return psk_info;
|
|
}
|
|
|
|
static coap_dtls_cpsk_t dtls_psk;
|
|
static char client_sni[256];
|
|
|
|
static coap_session_t *
|
|
setup_client_session_psk (const char *uri,
|
|
struct in_addr ip_address,
|
|
const uint8_t *identity,
|
|
unsigned int identity_len,
|
|
const uint8_t *key,
|
|
unsigned int key_len
|
|
) {
|
|
coap_session_t *session;
|
|
coap_address_t server;
|
|
/* See coap_context(3) */
|
|
coap_context_t *context = coap_new_context(NULL);
|
|
|
|
if (!context)
|
|
return NULL;
|
|
|
|
coap_address_init(&server);
|
|
server.addr.sa.sa_family = AF_INET;
|
|
server.addr.sin.sin_addr = ip_address;
|
|
server.addr.sin.sin_port = htons (5684);
|
|
|
|
/* See coap_encryption(3) */
|
|
memset (&dtls_psk, 0, sizeof(dtls_psk));
|
|
dtls_psk.version = COAP_DTLS_CPSK_SETUP_VERSION;
|
|
dtls_psk.validate_ih_call_back = verify_ih_callback;
|
|
dtls_psk.ih_call_back_arg = &dtls_psk.psk_info;
|
|
if (uri)
|
|
memcpy(client_sni, uri, min(strlen(uri), sizeof(client_sni)-1));
|
|
else
|
|
memcpy(client_sni, "localhost", 9);
|
|
dtls_psk.client_sni = client_sni;
|
|
dtls_psk.psk_info.identity.s = identity;
|
|
dtls_psk.psk_info.identity.length = identity_len;
|
|
dtls_psk.psk_info.key.s = key;
|
|
dtls_psk.psk_info.key.length = key_len;
|
|
session = coap_new_client_session_psk2(context, NULL, &server,
|
|
COAP_PROTO_DTLS, &dtls_psk);
|
|
if (!session) {
|
|
coap_free_context(context);
|
|
return NULL;
|
|
}
|
|
/* The context is in session->context */
|
|
return session;
|
|
}
|
|
----
|
|
|
|
*CoAP Client Anonymous PKI Setup*
|
|
[source, c]
|
|
----
|
|
#include <coap@LIBCOAP_API_VERSION@/coap.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
static coap_session_t *
|
|
setup_client_session_dtls (struct in_addr ip_address) {
|
|
coap_session_t *session;
|
|
coap_address_t server;
|
|
/* See coap_context(3) */
|
|
coap_context_t *context = coap_new_context(NULL);
|
|
|
|
if (!context)
|
|
return NULL;
|
|
|
|
coap_address_init(&server);
|
|
server.addr.sa.sa_family = AF_INET;
|
|
server.addr.sin.sin_addr = ip_address;
|
|
server.addr.sin.sin_port = htons (5683);
|
|
|
|
session = coap_new_client_session(context, NULL, &server,
|
|
COAP_PROTO_DTLS);
|
|
if (!session) {
|
|
coap_free_context(context);
|
|
return NULL;
|
|
}
|
|
/* The context is in session->context */
|
|
return session;
|
|
}
|
|
----
|
|
|
|
SEE ALSO
|
|
--------
|
|
*coap_context*(3), *coap_resource*(3), *coap_encryption*(3) and
|
|
*coap_tls_library*(3)
|
|
|
|
FURTHER INFORMATION
|
|
-------------------
|
|
See "RFC7252: The Constrained Application Protocol (CoAP)" for further
|
|
information.
|
|
|
|
BUGS
|
|
----
|
|
Please report bugs on the mailing list for libcoap:
|
|
libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at
|
|
https://github.com/obgm/libcoap/issues
|
|
|
|
AUTHORS
|
|
-------
|
|
The libcoap project <libcoap-developers@lists.sourceforge.net>
|