From 3a2ad2c4efefb2a3d3572bc4eca8c4efaf80622e Mon Sep 17 00:00:00 2001 From: Michael Zillgith Date: Mon, 29 Jan 2018 21:07:29 +0100 Subject: [PATCH] - refactored TLS API (moved TLSSocket in a separate private header file) --- src/mms/inc_private/acse.h | 2 +- src/mms/inc_private/cotp.h | 2 +- src/tls/tls_api.h | 60 +------------------------- src/tls/tls_socket.h | 88 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 61 deletions(-) create mode 100644 src/tls/tls_socket.h diff --git a/src/mms/inc_private/acse.h b/src/mms/inc_private/acse.h index 3f966d00..6ad56342 100644 --- a/src/mms/inc_private/acse.h +++ b/src/mms/inc_private/acse.h @@ -26,7 +26,7 @@ #include "byte_buffer.h" #include "buffer_chain.h" #include "iso_connection_parameters.h" -#include "tls_api.h" +#include "tls_socket.h" #ifndef ACSE_H_ #define ACSE_H_ diff --git a/src/mms/inc_private/cotp.h b/src/mms/inc_private/cotp.h index 4b6346b7..188d5282 100644 --- a/src/mms/inc_private/cotp.h +++ b/src/mms/inc_private/cotp.h @@ -29,7 +29,7 @@ #include "buffer_chain.h" #include "hal_socket.h" #include "iso_connection_parameters.h" -#include "tls_api.h" +#include "tls_socket.h" typedef struct { TSelector tSelSrc; diff --git a/src/tls/tls_api.h b/src/tls/tls_api.h index 4b0cde9c..28bc7304 100644 --- a/src/tls/tls_api.h +++ b/src/tls/tls_api.h @@ -15,7 +15,7 @@ #ifndef SRC_TLS_TLS_API_H_ #define SRC_TLS_TLS_API_H_ -#include "hal_socket.h" +#include #ifdef __cplusplus extern "C" { @@ -23,8 +23,6 @@ extern "C" { typedef struct sTLSConfiguration* TLSConfiguration; -typedef struct sTLSSocket* TLSSocket; - /** * \brief Create a new \ref TLSConfiguration object to represent TLS configuration and certificates * @@ -108,62 +106,6 @@ TLSConfiguration_setRenegotiationTime(TLSConfiguration self, int timeInMs); void TLSConfiguration_destroy(TLSConfiguration self); -TLSSocket -TLSSocket_create(Socket socket, TLSConfiguration configuration, bool storeClientCert); - -/** - * \brief Perform a new TLS handshake/session renegotiation - */ -bool -TLSSocket_performHandshake(TLSSocket self); - -/** - * \brief Access the certificate used by the peer - * - * \param[out] certSize the size of the certificate in bytes - * - * \return the certificate byte buffer - */ -uint8_t* -TLSSocket_getPeerCertificate(TLSSocket self, int* certSize); - -/** - * \brief read from socket to local buffer (non-blocking) - * - * The function shall return immediately if no data is available. In this case - * the function returns 0. If an error happens the function shall return -1. - * - * Implementation of this function is MANDATORY - * - * NOTE: The behaviour of this function changed with version 0.8! - * - * \param self the client, connection or server socket instance - * \param buf the buffer where the read bytes are copied to - * \param size the maximum number of bytes to read (size of the provided buffer) - * - * \return the number of bytes read or -1 if an error occurred - */ -int -TLSSocket_read(TLSSocket self, uint8_t* buf, int size); - -/** - * \brief send a message through the socket - * - * Implementation of this function is MANDATORY - * - * \param self client, connection or server socket instance - * - * \return number of bytes transmitted of -1 in case of an error - */ -int -TLSSocket_write(TLSSocket self, uint8_t* buf, int size); - -/** - * \brief Close the TLS socket and release all resources - */ -void -TLSSocket_close(TLSSocket self); - #ifdef __cplusplus } #endif diff --git a/src/tls/tls_socket.h b/src/tls/tls_socket.h new file mode 100644 index 00000000..009a9906 --- /dev/null +++ b/src/tls/tls_socket.h @@ -0,0 +1,88 @@ +/* + * tls_socket.h + * + * TLS API for TCP/IP protocol stacks + * + * Copyright 2017 MZ Automation GmbH + * + * Abstraction layer for different TLS implementations + * + * Implementation connects the TLS API layer with the socket API layer + * and performs all TLS tasks like handshake, encryption/decryption. + * + */ + +#ifndef SRC_TLS_TLS_SOCKET_H_ +#define SRC_TLS_TLS_SOCKET_H_ + +#include "tls_api.h" + +#include "hal_socket.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct sTLSSocket* TLSSocket; + +TLSSocket +TLSSocket_create(Socket socket, TLSConfiguration configuration, bool storeClientCert); + +/** + * \brief Perform a new TLS handshake/session renegotiation + */ +bool +TLSSocket_performHandshake(TLSSocket self); + +/** + * \brief Access the certificate used by the peer + * + * \param[out] certSize the size of the certificate in bytes + * + * \return the certificate byte buffer + */ +uint8_t* +TLSSocket_getPeerCertificate(TLSSocket self, int* certSize); + +/** + * \brief read from socket to local buffer (non-blocking) + * + * The function shall return immediately if no data is available. In this case + * the function returns 0. If an error happens the function shall return -1. + * + * Implementation of this function is MANDATORY + * + * NOTE: The behaviour of this function changed with version 0.8! + * + * \param self the client, connection or server socket instance + * \param buf the buffer where the read bytes are copied to + * \param size the maximum number of bytes to read (size of the provided buffer) + * + * \return the number of bytes read or -1 if an error occurred + */ +int +TLSSocket_read(TLSSocket self, uint8_t* buf, int size); + +/** + * \brief send a message through the socket + * + * Implementation of this function is MANDATORY + * + * \param self client, connection or server socket instance + * + * \return number of bytes transmitted of -1 in case of an error + */ +int +TLSSocket_write(TLSSocket self, uint8_t* buf, int size); + +/** + * \brief Close the TLS socket and release all resources + */ +void +TLSSocket_close(TLSSocket self); + +#ifdef __cplusplus +} +#endif + +#endif /* SRC_TLS_TLS_SOCKET_H_ */