1 /* 2 * coap_dtls.c -- (D)TLS functions for libcoap 3 * 4 * Copyright (C) 2023 Olaf Bergmann <bergmann@tzi.org> 5 * Copyright (C) 2023 Jon Shallow <supjps-libcoap@jpshallow.com> 6 * 7 * SPDX-License-Identifier: BSD-2-Clause 8 * 9 * This file is part of the CoAP library libcoap. Please see README for terms 10 * of use. 11 */ 12 13 /** 14 * @file coap_dtls.c 15 * @brief CoAP (D)TLS handling functions 16 */ 17 18 #include "coap3/coap_internal.h" 19 20 void coap_dtls_establish(coap_session_t * session)21coap_dtls_establish(coap_session_t *session) { 22 session->state = COAP_SESSION_STATE_HANDSHAKE; 23 #if COAP_CLIENT_SUPPORT 24 if (session->type == COAP_SESSION_TYPE_CLIENT) 25 session->tls = coap_dtls_new_client_session(session); 26 #endif /* COAP_CLIENT_SUPPORT */ 27 #if COAP_SERVER_SUPPORT 28 if (session->type != COAP_SESSION_TYPE_CLIENT) 29 session->tls = coap_dtls_new_server_session(session); 30 #endif /* COAP_SERVER_SUPPORT */ 31 32 if (!session->tls) { 33 coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED); 34 return; 35 } 36 coap_ticks(&session->last_rx_tx); 37 } 38 39 void coap_dtls_close(coap_session_t * session)40coap_dtls_close(coap_session_t *session) { 41 if (session->tls) { 42 coap_dtls_free_session(session); 43 session->tls = NULL; 44 } 45 session->sock.lfunc[COAP_LAYER_TLS].l_close(session); 46 } 47 48 #if !COAP_DISABLE_TCP 49 void coap_tls_establish(coap_session_t * session)50coap_tls_establish(coap_session_t *session) { 51 session->state = COAP_SESSION_STATE_HANDSHAKE; 52 #if COAP_CLIENT_SUPPORT 53 if (session->type == COAP_SESSION_TYPE_CLIENT) 54 session->tls = coap_tls_new_client_session(session); 55 #endif /* COAP_CLIENT_SUPPORT */ 56 #if COAP_SERVER_SUPPORT 57 if (session->type != COAP_SESSION_TYPE_CLIENT) 58 session->tls = coap_tls_new_server_session(session); 59 #endif /* COAP_SERVER_SUPPORT */ 60 61 if (!session->tls) { 62 coap_session_disconnected(session, COAP_NACK_TLS_LAYER_FAILED); 63 return; 64 } 65 coap_ticks(&session->last_rx_tx); 66 } 67 68 void coap_tls_close(coap_session_t * session)69coap_tls_close(coap_session_t *session) { 70 if (session->tls) { 71 coap_tls_free_session(session); 72 session->tls = NULL; 73 } 74 session->sock.lfunc[COAP_LAYER_TLS].l_close(session); 75 } 76 #endif /* !COAP_DISABLE_TCP */ 77