The Implementation of COAP Security
The Implementation of COAP Security
1. CoAP Communication
CoAP uses the Client-Server communication model where nodes send requests and receive
responses from other nodes.
2. CoAP Messages
These define the type of CoAP packets and deal with UDP. The CoAP standard defines four different
types of Messages:
- CoAP standard defines four Request methods - GET, PUT, POST, and DELETE.
- CoAP standard defines Response code 2.xx for success, 4.xx for client error, and 5.xx for
server error.
- Unique 0-8 byte Tokens are used for identifying, mapping request and response much the
same way Message-IDs are used to identify messages and map ACKs.
4. CoAP Security
The CoAP standard does not specify any authentication mechanism. It relies on DTLS to provide
protocol-level security. The standard defines four security modes for devices.
4.2. Certificate
Digital certificates, also known as identity certificates or public key certificates are digital files
that are used to certify the ownership of a public key. DTLS certificates are a type of digital
certificate, issued by a Certificate Authority (CA). The CA signs the certificate, certifying that they
have verified that it belongs to the owners of the domain name which is the subject of the
certificate.
1) Install Dependencies: The first step, before we can compile the OpenSSL library from
source, is to install some package dependencies including the 'build-essential' package on
Ubuntu.
Download OpenSSL: Go to the '/usr/local/src' directory and download the OpenSSL source
code using wget. If you use openssl 1.0 version then do the next command else change
openssl-1.0.2o to openssl-1.1.1s.
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz
export LD_LIBRARY_PATH=/opt/openssl/lib
cd /opt/openssl/openssl-1.1.1b
sudo make
sudo vi /etc/profile.d/openssl.sh
#!/bin/sh
export PATH=/opt/openssl/bin:${PATH}
export LD_LIBRARY_PATH=/opt/openssl/lib:${LD_LIBRARY_PATH}
Save and close the file. Make it executable using the following command.
Then, set the environment variables permanently by running the following command:
source /etc/profile.d/openssl.sh
Generate an RSA private key, of size 2048, and output it to a file named key.pem:
Extract the public key from the key pair, which can be used in a certificate:
Generate an EC private key, of size 256, and output it to a file named key.pem:
Extract the public key from the key pair, which can be used in a certificate:
Prepare the key to generate the certificate, Send the CSR to the trusted CA authority. We must
now create a ca-certificate with the keys and update it.
sudo openssl req -new -sha256 -key key.pem -nodes -out host.csr
sudo openssl req -x509 -in host.csr -key key.pem -sha256 -days 1096 -nodes -out cert.pem
sudo update-ca-certificates
.key files are generally the private key, used by the server to encrypt and package data for
verification by clients.
.pem files are generally the public key, used by the client to verify and decrypt data sent by
servers. PEM files could also be encoded private keys, so check the content if you're not sure.
.cert or .crt files are the signed certificates -- basically the "magic" that allows certain sites to
be marked as trustworthy by a third party.
.csr is a certificate-signing request, a challenge used by a trusted third party to verify the
ownership of a keypair without having direct access to the private key.
Finally, Copy the CA-certificates in the same place of the certificate and key and convert it from
ca-certificates.crt to ca-certificates.pem.
4.4. Encryption
When the CoAP library was built, it will have been compiled using a specific underlying TLS
implementation type (e.g. OpenSSL, GnuTLS, TinyDTLS or noTLS). When the CoAP library is linked into
an application, it is possible that the application needs to dynamically determine whether DTLS or TLS
is supported.
The DTLS is allowed to be use with the port 5684. Therefore, we can change it manually in
Coap_MainJob or automatically with the PDU.h.
If DTLS is going to be used for encrypting the network traffic, then the DTLS 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 DTLS Context level and the CoAP
Context level as the Server is listening for new incoming traffic based on the Endpoint definition. The
DTLS and CoAP session will not be built until the new traffic starts.
coap_new_context()
coap_new_endpoint ()
To activate the Openssl library, there is a flag named HAVE-OPENSSL in coap-config.h that is forced
to zero to disable the security. Turn it to one.
For Clients, all the encryption information can be held at the TLS Context and CoAP Context levels, or
usually 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.
coap_new_context()
coap_new_client_session(), coap_new_client_session_pki() or
coap_new_client_session_psk()
Due to the nature of TLS, there are Callbacks that are invoked as the TLS session negotiates
encryption algorithms, encryption keys etc. Where possible, the CoAP layer handles all this
automatically based on different configuration options passed in by the coap_*_pki() functions.
For PSK setup, the required information needs to be provided in the setup calls with no application
Callbacks required. Both the Client and Server have to provide a PSK. The Server must have a Hint
defined and the Client must have an Identity defined.
For PKI setup, if the CoAP library PKI configuration options do not handle a specific requirement as
defined by the available options, then an application defined Callback can called to do the additional
specific checks.
CoAP library will add in the defined Certificate, Private Key and CA Certificate into the TLS
environment. The CA Certificate is also added in to the list of valid CAs for Certificate checking.
The internal Callbacks (and optionally the Application Callback) will then check the required
information as defined in the coap_dtls_pki_t described below.
uint8_t version;
coap_dtls_cn_callback_t validate_cn_call_back;
coap_dtls_sni_callback_t validate_sni_call_back;
coap_dtls_security_setup_t additional_tls_setup_call_back;
Char* client_sni;
} coap_dtls_pki_t;
} coap_pki_key_t;
Key type defines the format that the certificates / keys are provided in. This can be
COAP_PKI_KEY_PEM or COAP_PKI_KEY_ASN1.
} coap_pki_key_pem_t;
Key.pem.ca_file points to the CA File location on disk, which will be in PEM format, or NULL. This
file should only contain one CA (who signed the Public Certificate) as this is passed from the
server to the client when requesting the client’s certificate. This certificate is also added into the
valid root CAs list if not already present.
Key.pem.public_cert points to the Public Certificate location on disk, which will be in PEM
format.
Key.pem.private_key points to the Private Key location on disk, which will be in PEM format. This
file cannot be password protected.
To activate the security, the server must have the key and the certificate and sometime the ca-
certificates but the client must have only the ca-certificates. Those files will be integrated to
the server as local files in /files/temps and they will be added as path in the makefile of the
project.
The last step that the software development kit SDK must implement openssl 1.1 version or later,
because the DTLS security is added to 1.1.1 version.