nts_client.h
Go to the documentation of this file.
1 /**
2  * @file nts_client.h
3  * @brief NTS client (Network Time Security)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * The Simple Network Time Protocol is used to synchronize computer clocks
30  * in the Internet. Refer to RFC 4330 for more details
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 2.4.4
34  **/
35 
36 #ifndef _NTS_CLIENT_H
37 #define _NTS_CLIENT_H
38 
39 //Dependencies
40 #include "core/net.h"
41 #include "ntp/ntp_common.h"
42 #include "nts/nts_common.h"
43 #include "core/crypto.h"
44 #include "tls.h"
45 
46 //NTS client support
47 #ifndef NTS_CLIENT_SUPPORT
48  #define NTS_CLIENT_SUPPORT ENABLED
49 #elif (NTS_CLIENT_SUPPORT != ENABLED && NTS_CLIENT_SUPPORT != DISABLED)
50  #error NTS_CLIENT_SUPPORT parameter is not valid
51 #endif
52 
53 //Default timeout
54 #ifndef NTS_CLIENT_DEFAULT_TIMEOUT
55  #define NTS_CLIENT_DEFAULT_TIMEOUT 30000
56 #elif (NTS_CLIENT_DEFAULT_TIMEOUT < 1000)
57  #error NTS_CLIENT_DEFAULT_TIMEOUT parameter is not valid
58 #endif
59 
60 //Initial NTP retransmission timeout
61 #ifndef NTS_CLIENT_INIT_NTP_RETRANSMIT_TIMEOUT
62  #define NTS_CLIENT_INIT_NTP_RETRANSMIT_TIMEOUT 2000
63 #elif (NTS_CLIENT_INIT_NTP_RETRANSMIT_TIMEOUT < 1000)
64  #error NTS_CLIENT_INIT_NTP_RETRANSMIT_TIMEOUT parameter is not valid
65 #endif
66 
67 //Maximum NTP retransmission timeout
68 #ifndef NTS_CLIENT_MAX_NTP_RETRANSMIT_TIMEOUT
69  #define NTS_CLIENT_MAX_NTP_RETRANSMIT_TIMEOUT 15000
70 #elif (NTS_CLIENT_MAX_NTP_RETRANSMIT_TIMEOUT < 1000)
71  #error NTS_CLIENT_MAX_NTP_RETRANSMIT_TIMEOUT parameter is not valid
72 #endif
73 
74 //Size of the buffer for input/output operations
75 #ifndef NTS_CLIENT_BUFFER_SIZE
76  #define NTS_CLIENT_BUFFER_SIZE 512
77 #elif (NTS_CLIENT_BUFFER_SIZE < 128)
78  #error NTS_CLIENT_BUFFER_SIZE parameter is not valid
79 #endif
80 
81 //TX buffer size for TLS connections
82 #ifndef NTS_CLIENT_TLS_TX_BUFFER_SIZE
83  #define NTS_CLIENT_TLS_TX_BUFFER_SIZE 2048
84 #elif (NTS_CLIENT_TLS_TX_BUFFER_SIZE < 512)
85  #error NTS_CLIENT_TLS_TX_BUFFER_SIZE parameter is not valid
86 #endif
87 
88 //RX buffer size for TLS connections
89 #ifndef NTS_CLIENT_TLS_RX_BUFFER_SIZE
90  #define NTS_CLIENT_TLS_RX_BUFFER_SIZE 4096
91 #elif (NTS_CLIENT_TLS_RX_BUFFER_SIZE < 512)
92  #error NTS_CLIENT_TLS_RX_BUFFER_SIZE parameter is not valid
93 #endif
94 
95 //Maximum size for NTS cookies
96 #ifndef NTS_CLIENT_MAX_COOKIE_SIZE
97  #define NTS_CLIENT_MAX_COOKIE_SIZE 128
98 #elif (NTS_CLIENT_MAX_COOKIE_SIZE < 1)
99  #error NTS_CLIENT_MAX_COOKIE_SIZE parameter is not valid
100 #endif
101 
102 //Maximum length of NTP server names
103 #ifndef NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN
104  #define NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN 64
105 #elif (NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN < 1)
106  #error NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN parameter is not valid
107 #endif
108 
109 //Size of the unique identifier
110 #ifndef NTS_CLIENT_UNIQUE_ID_SIZE
111  #define NTS_CLIENT_UNIQUE_ID_SIZE 32
112 #elif (NTS_CLIENT_UNIQUE_ID_SIZE < 32)
113  #error NTS_CLIENT_UNIQUE_ID_SIZE parameter is not valid
114 #endif
115 
116 //Size of the nonce
117 #ifndef NTS_CLIENT_NONCE_SIZE
118  #define NTS_CLIENT_NONCE_SIZE 16
119 #elif (NTS_CLIENT_NONCE_SIZE < 16)
120  #error NTS_CLIENT_NONCE_SIZE parameter is not valid
121 #endif
122 
123 //Application specific context
124 #ifndef NTS_CLIENT_PRIVATE_CONTEXT
125  #define NTS_CLIENT_PRIVATE_CONTEXT
126 #endif
127 
128 //Forward declaration of NtsClientContext structure
129 struct _NtsClientContext;
130 #define NtsClientContext struct _NtsClientContext
131 
132 //C++ guard
133 #ifdef __cplusplus
134 extern "C" {
135 #endif
136 
137 
138 /**
139  * @brief NTS client states
140  **/
141 
142 typedef enum
143 {
156 
157 
158 /**
159  * @brief TLS initialization callback function
160  **/
161 
164 
165 
166 /**
167  * @brief Random data generation callback function
168  **/
169 
170 typedef error_t (*NtsClientRandCallback)(uint8_t *data, size_t length);
171 
172 
173 /**
174  * @brief NTS client context
175  **/
176 
178 {
179  NtsClientState state; ///<NTS client state
180  NetInterface *interface; ///<Underlying network interface
181  IpAddr ntsKeServerIpAddr; ///<NTS-KE server address
182  uint16_t ntsKeServerPort; ///<NTS-KE server port
184  IpAddr ntpServerIpAddr; ///<NTP server address
185  uint16_t ntpServerPort; ///<NTP server port
186  systime_t timeout; ///<Timeout value
187  systime_t timestamp; ///<Timestamp to manage timeout
188  Socket *ntsKeSocket; ///<NTS-KE socket
189  Socket *ntpSocket; ///<NTP socket
190  TlsContext *tlsContext; ///<TLS context
191  TlsSessionState tlsSession; ///<TLS session state
192  NtsClientTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
193  NtsClientRandCallback randCallback; ///<Random data generation callback function
194  systime_t startTime; ///<Request start time
195  systime_t retransmitStartTime; ///<Time at which the last request was sent
196  systime_t retransmitTimeout; ///<Retransmission timeout
197  uint8_t buffer[NTS_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
198  size_t bufferLen; ///<Length of the buffer, in bytes
199  size_t bufferPos; ///<Current position in the buffer
200  bool_t ntsNextProtoNegoRecordReceived; ///<The NTS Next Protocol Negotiation record has been received
201  bool_t aeadAlgoNegoRecordReceived; ///<The AEAD Algorithm Negotiation record has been received
202  uint8_t c2sKey[32]; ///<Client-to-server (C2S) key
203  uint8_t s2cKey[32]; ///<Server-to-client (S2C) key
204  uint8_t cookie[NTS_CLIENT_MAX_COOKIE_SIZE]; ///<NTS cookie
205  size_t cookieLen; ///<Length of the NTS cookie, in bytes
206  uint8_t uniqueId[NTS_CLIENT_UNIQUE_ID_SIZE]; ///<Unique identifier
207  uint8_t nonce[NTS_CLIENT_NONCE_SIZE]; ///<Nonce
208  uint32_t kissCode; ///<Kiss code
209  NTS_CLIENT_PRIVATE_CONTEXT ///<Application specific context
210 };
211 
212 
213 //NTS client related functions
215 
217  NtsClientTlsInitCallback callback);
218 
220  NtsClientRandCallback callback);
221 
223 
225  NetInterface *interface);
226 
228  const IpAddr *serverIpAddr, uint16_t serverPort);
229 
231  NtpTimestamp *timestamp);
232 
233 uint32_t ntsClientGetKissCode(NtsClientContext *context);
234 
235 void ntsClientDeinit(NtsClientContext *context);
236 
237 //C++ guard
238 #ifdef __cplusplus
239 }
240 #endif
241 
242 #endif
@ NTS_CLIENT_STATE_INIT
Definition: nts_client.h:144
int bool_t
Definition: compiler_port.h:53
error_t ntsClientRegisterTlsInitCallback(NtsClientContext *context, NtsClientTlsInitCallback callback)
Register TLS initialization callback function.
Definition: nts_client.c:111
systime_t startTime
Request start time.
Definition: nts_client.h:194
IpAddr ntpServerIpAddr
NTP server address.
Definition: nts_client.h:184
IP network address.
Definition: ip.h:90
systime_t timeout
Timeout value.
Definition: nts_client.h:186
NtpTimestamp
Definition: ntp_common.h:179
#define NTS_CLIENT_UNIQUE_ID_SIZE
Definition: nts_client.h:111
error_t ntsClientGetTimestamp(NtsClientContext *context, NtpTimestamp *timestamp)
Retrieve current time from NTS server.
Definition: nts_client.c:209
TlsSessionState tlsSession
TLS session state.
Definition: nts_client.h:191
uint8_t data[]
Definition: ethernet.h:222
error_t(* NtsClientTlsInitCallback)(NtsClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition: nts_client.h:162
@ NTS_CLIENT_STATE_NTS_KE_DISCONNECTING
Definition: nts_client.h:149
#define NTS_CLIENT_BUFFER_SIZE
Definition: nts_client.h:76
error_t ntsClientBindToInterface(NtsClientContext *context, NetInterface *interface)
Bind the NTS client to a particular network interface.
Definition: nts_client.c:155
uint16_t ntpServerPort
NTP server port.
Definition: nts_client.h:185
@ NTS_CLIENT_STATE_NTS_KE_RECEIVING
Definition: nts_client.h:148
IpAddr ntsKeServerIpAddr
NTS-KE server address.
Definition: nts_client.h:181
systime_t retransmitTimeout
Retransmission timeout.
Definition: nts_client.h:196
error_t ntsClientInit(NtsClientContext *context)
Initialize NTS client context.
Definition: nts_client.c:56
void ntsClientDeinit(NtsClientContext *context)
Release NTS client context.
Definition: nts_client.c:452
Socket * ntsKeSocket
NTS-KE socket.
Definition: nts_client.h:188
NetInterface * interface
Underlying network interface.
Definition: nts_client.h:180
error_t ntsClientRegisterRandCallback(NtsClientContext *context, NtsClientRandCallback callback)
Register random data generation callback function.
Definition: nts_client.c:133
@ NTS_CLIENT_STATE_NTS_KE_INIT
Definition: nts_client.h:145
size_t bufferPos
Current position in the buffer.
Definition: nts_client.h:199
uint16_t ntsKeServerPort
NTS-KE server port.
Definition: nts_client.h:182
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
error_t ntsClientSetTimeout(NtsClientContext *context, systime_t timeout)
Set communication timeout.
Definition: nts_client.c:90
NtsClientState
NTS client states.
Definition: nts_client.h:143
#define NetInterface
Definition: net.h:36
@ NTS_CLIENT_STATE_NTP_INIT
Definition: nts_client.h:151
uint8_t nonce[NTS_CLIENT_NONCE_SIZE]
Nonce.
Definition: nts_client.h:207
General definitions for cryptographic algorithms.
uint8_t uniqueId[NTS_CLIENT_UNIQUE_ID_SIZE]
Unique identifier.
Definition: nts_client.h:206
char_t ntpServerName[NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN+1]
NTP server name.
Definition: nts_client.h:183
#define NTS_CLIENT_PRIVATE_CONTEXT
Definition: nts_client.h:125
uint8_t length
Definition: tcp.h:368
NtsClientState state
NTS client state.
Definition: nts_client.h:179
uint8_t c2sKey[32]
Client-to-server (C2S) key.
Definition: nts_client.h:202
@ NTS_CLIENT_STATE_NTP_RECEIVING
Definition: nts_client.h:153
TlsContext * tlsContext
TLS context.
Definition: nts_client.h:190
#define NtsClientContext
Definition: nts_client.h:130
@ NTS_CLIENT_STATE_NTP_SENDING
Definition: nts_client.h:152
uint32_t ntsClientGetKissCode(NtsClientContext *context)
Retrieve the kiss code from a Kiss-of-Death message.
Definition: nts_client.c:426
@ NTS_CLIENT_STATE_NTP_RESOLVING
Definition: nts_client.h:150
uint32_t systime_t
System time.
NtsClientRandCallback randCallback
Random data generation callback function.
Definition: nts_client.h:193
char char_t
Definition: compiler_port.h:48
@ NTS_CLIENT_STATE_NTS_KE_CONNECTING
Definition: nts_client.h:146
systime_t timestamp
Timestamp to manage timeout.
Definition: nts_client.h:187
systime_t retransmitStartTime
Time at which the last request was sent.
Definition: nts_client.h:195
TLS session state.
Definition: tls.h:2038
size_t bufferLen
Length of the buffer, in bytes.
Definition: nts_client.h:198
#define Socket
Definition: socket.h:36
size_t cookieLen
Length of the NTS cookie, in bytes.
Definition: nts_client.h:205
uint8_t buffer[NTS_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: nts_client.h:197
uint8_t s2cKey[32]
Server-to-client (S2C) key.
Definition: nts_client.h:203
TLS (Transport Layer Security)
@ NTS_CLIENT_STATE_NTS_KE_SENDING
Definition: nts_client.h:147
NtsClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: nts_client.h:192
Definitions common to NTP client and server.
bool_t aeadAlgoNegoRecordReceived
The AEAD Algorithm Negotiation record has been received.
Definition: nts_client.h:201
#define NTS_CLIENT_MAX_COOKIE_SIZE
Definition: nts_client.h:97
@ NTS_CLIENT_STATE_COMPLETE
Definition: nts_client.h:154
Definitions common to NTS client and server.
error_t(* NtsClientRandCallback)(uint8_t *data, size_t length)
Random data generation callback function.
Definition: nts_client.h:170
#define NTS_CLIENT_MAX_NTP_SERVER_NAME_LEN
Definition: nts_client.h:104
NTS client context.
Definition: nts_client.h:178
TCP/IP stack core.
error_t ntsClientSetServerAddr(NtsClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the IP address of the NTS server.
Definition: nts_client.c:178
bool_t ntsNextProtoNegoRecordReceived
The NTS Next Protocol Negotiation record has been received.
Definition: nts_client.h:200
uint32_t kissCode
Kiss code.
Definition: nts_client.h:208
#define NTS_CLIENT_NONCE_SIZE
Definition: nts_client.h:118
uint8_t cookie[NTS_CLIENT_MAX_COOKIE_SIZE]
NTS cookie.
Definition: nts_client.h:204
Socket * ntpSocket
NTP socket.
Definition: nts_client.h:189