est_client.h
Go to the documentation of this file.
1 /**
2  * @file est_client.h
3  * @brief EST client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2024-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneEST 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  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.5.4
29  **/
30 
31 #ifndef _EST_CLIENT_H
32 #define _EST_CLIENT_H
33 
34 //Dependencies
35 #include "est_config.h"
36 #include "core/net.h"
37 #include "http/http_client.h"
38 
39 
40 /*
41  * CycloneEST Open is licensed under GPL version 2. In particular:
42  *
43  * - If you link your program to CycloneEST Open, the result is a derivative
44  * work that can only be distributed under the same GPL license terms.
45  *
46  * - If additions or changes to CycloneEST Open are made, the result is a
47  * derivative work that can only be distributed under the same license terms.
48  *
49  * - The GPL license requires that you make the source code available to
50  * whoever you make the binary available to.
51  *
52  * - If you sell or distribute a hardware product that runs CycloneEST Open,
53  * the GPL license requires you to provide public and full access to all
54  * source code on a nondiscriminatory basis.
55  *
56  * If you fully understand and accept the terms of the GPL license, then edit
57  * the os_port_config.h header and add the following directive:
58  *
59  * #define GPL_LICENSE_TERMS_ACCEPTED
60  */
61 
62 #ifndef GPL_LICENSE_TERMS_ACCEPTED
63  #error Before compiling CycloneEST Open, you must accept the terms of the GPL license
64 #endif
65 
66 //Version string
67 #define CYCLONE_EST_VERSION_STRING "2.5.4"
68 //Major version
69 #define CYCLONE_EST_MAJOR_VERSION 2
70 //Minor version
71 #define CYCLONE_EST_MINOR_VERSION 5
72 //Revision number
73 #define CYCLONE_EST_REV_NUMBER 4
74 
75 //EST client support
76 #ifndef EST_CLIENT_SUPPORT
77  #define EST_CLIENT_SUPPORT DISABLED
78 #elif (EST_CLIENT_SUPPORT != ENABLED && EST_CLIENT_SUPPORT != DISABLED)
79  #error EST_CLIENT_SUPPORT parameter is not valid
80 #endif
81 
82 //RSA key support
83 #ifndef EST_CLIENT_RSA_SUPPORT
84  #define EST_CLIENT_RSA_SUPPORT ENABLED
85 #elif (EST_CLIENT_RSA_SUPPORT != ENABLED && EST_CLIENT_RSA_SUPPORT != DISABLED)
86  #error EST_CLIENT_RSA_SUPPORT parameter is not valid
87 #endif
88 
89 //ECDSA key support
90 #ifndef EST_CLIENT_ECDSA_SUPPORT
91  #define EST_CLIENT_ECDSA_SUPPORT ENABLED
92 #elif (EST_CLIENT_ECDSA_SUPPORT != ENABLED && EST_CLIENT_ECDSA_SUPPORT != DISABLED)
93  #error EST_CLIENT_ECDSA_SUPPORT parameter is not valid
94 #endif
95 
96 //Default timeout
97 #ifndef EST_CLIENT_DEFAULT_TIMEOUT
98  #define EST_CLIENT_DEFAULT_TIMEOUT 20000
99 #elif (EST_CLIENT_DEFAULT_TIMEOUT < 1000)
100  #error EST_CLIENT_DEFAULT_TIMEOUT parameter is not valid
101 #endif
102 
103 //Size of the buffer for input/output operations
104 #ifndef EST_CLIENT_BUFFER_SIZE
105  #define EST_CLIENT_BUFFER_SIZE 4096
106 #elif (EST_CLIENT_BUFFER_SIZE < 512)
107  #error EST_CLIENT_BUFFER_SIZE parameter is not valid
108 #endif
109 
110 //Maximum length of host names
111 #ifndef EST_CLIENT_MAX_HOST_LEN
112  #define EST_CLIENT_MAX_HOST_LEN 64
113 #elif (EST_CLIENT_MAX_HOST_LEN < 1)
114  #error EST_CLIENT_MAX_HOST_LEN parameter is not valid
115 #endif
116 
117 //Maximum length of URIs
118 #ifndef EST_CLIENT_MAX_URI_LEN
119  #define EST_CLIENT_MAX_URI_LEN 64
120 #elif (EST_CLIENT_MAX_URI_LEN < 1)
121  #error EST_CLIENT_MAX_URI_LEN parameter is not valid
122 #endif
123 
124 //Maximum length of media types
125 #ifndef EST_CLIENT_MAX_CONTENT_TYPE_LEN
126  #define EST_CLIENT_MAX_CONTENT_TYPE_LEN 40
127 #elif (EST_CLIENT_MAX_CONTENT_TYPE_LEN < 1)
128  #error EST_CLIENT_MAX_CONTENT_TYPE_LEN parameter is not valid
129 #endif
130 
131 //Maximum length of CSR
132 #ifndef EST_CLIENT_MAX_CSR_LEN
133  #define EST_CLIENT_MAX_CSR_LEN 1024
134 #elif (EST_CLIENT_MAX_CSR_LEN < 1)
135  #error EST_CLIENT_MAX_CSR_LEN parameter is not valid
136 #endif
137 
138 //Maximum length of certificate
139 #ifndef EST_CLIENT_MAX_CERT_LEN
140  #define EST_CLIENT_MAX_CERT_LEN 2048
141 #elif (EST_CLIENT_MAX_CERT_LEN < 1)
142  #error EST_CLIENT_MAX_CERT_LEN parameter is not valid
143 #endif
144 
145 //Maximum length of CA certificates
146 #ifndef EST_CLIENT_MAX_CA_CERTS_LEN
147  #define EST_CLIENT_MAX_CA_CERTS_LEN 4096
148 #elif (EST_CLIENT_MAX_CA_CERTS_LEN < 1)
149  #error EST_CLIENT_MAX_CA_CERTS_LEN parameter is not valid
150 #endif
151 
152 //Application specific context
153 #ifndef EST_CLIENT_PRIVATE_CONTEXT
154  #define EST_CLIENT_PRIVATE_CONTEXT
155 #endif
156 
157 //Forward declaration of EstClientContext structure
158 struct _EstClientContext;
159 #define EstClientContext struct _EstClientContext
160 
161 //C++ guard
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165 
166 
167 /**
168  * @brief EST client states
169  **/
170 
171 typedef enum
172 {
182 
183 
184 /**
185  * @brief HTTP request states
186  */
187 
188 typedef enum
189 {
201 
202 
203 /**
204  * @brief TLS initialization callback function
205  **/
206 
208  TlsContext *tlsContext);
209 
210 
211 /**
212  * @brief CSR generation callback function
213  **/
214 
216  const char_t *challengePwd, uint8_t *buffer, size_t size, size_t *length);
217 
218 
219 /**
220  * @brief EST client context
221  **/
222 
224 {
225  EstClientState state; ///<EST client state
226  EstRequestState requestState; ///<HTTP request state
227  NetInterface *interface; ///<Underlying network interface
228  systime_t timeout; ///<Timeout value
229  const PrngAlgo *prngAlgo; ///<Pseudo-random number generator to be used
230  void *prngContext; ///<Pseudo-random number generator context
231  HttpClientContext httpClientContext; ///<HTTP client context
232  EstClientTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
233  EstClientCsrGenCallback csrGenCallback; ///<CSR generation callback function
234  char_t serverName[EST_CLIENT_MAX_HOST_LEN + 1]; ///<Host name of the EST server
235  uint16_t serverPort; ///<TCP port number
237  X509KeyType keyType; ///<Public key type
238 #if (EST_CLIENT_RSA_SUPPORT == ENABLED)
239  RsaPublicKey rsaPublicKey; ///<RSA public key
240  RsaPrivateKey rsaPrivateKey; ///<RSA private key
241 #endif
242 #if (EST_CLIENT_ECDSA_SUPPORT == ENABLED)
243  EcPublicKey ecPublicKey; ///<EC public key
244  EcPrivateKey ecPrivateKey; ///<EC private key
245 #endif
246  uint8_t csr[EST_CLIENT_MAX_CSR_LEN]; ///<CSR
247  size_t csrLen; ///<Length of the CSR, in bytes
248  uint8_t cert[EST_CLIENT_MAX_CERT_LEN]; ///<Client's certificate
249  size_t certLen; ///<Length of the client's certificate, in bytes
251  size_t caCertsLen; ///<Length of the CA certificates, in bytes
252  uint8_t buffer[EST_CLIENT_BUFFER_SIZE]; ///<Memory buffer for input/output operations
253  size_t bufferLen; ///<Length of the buffer, in bytes
254  size_t bufferPos; ///<Current position in the buffer
255  bool_t useExplicitTa; ///<Use of explicit TA database
256  uint_t allowedAuthModes; ///<Allowed HTTP authentication modes
257  HttpAuthMode selectedAuthMode; ///<Selected HTTP authentication mode
258  uint_t statusCode; ///<HTTP status code
259  char_t contentType[EST_CLIENT_MAX_CONTENT_TYPE_LEN + 1]; ///<Content type of the response
260  EST_CLIENT_PRIVATE_CONTEXT ///<Application specific context
261 };
262 
263 
264 //EST client related functions
266 
268  EstClientTlsInitCallback callback);
269 
271  EstClientCsrGenCallback callback);
272 
273 error_t estClientSetPrng(EstClientContext *context, const PrngAlgo *prngAlgo,
274  void *prngContext);
275 
277 
278 error_t estClientSetHost(EstClientContext *context, const char_t *host);
279 
281  const char_t *pathPrefix);
282 
284  uint_t allowedAuthModes);
285 
286 error_t estClientSetAuthInfo(EstClientContext *context, const char_t *username,
287  const char_t *password);
288 
290  NetInterface *interface);
291 
293  const IpAddr *serverIpAddr, uint16_t serverPort);
294 
296  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
297  size_t privateKeyLen, const char_t *password);
298 
300 
302  const char_t *input, size_t length);
303 
305  char_t *output, size_t *written);
306 
308  const char_t *input, size_t length);
309 
311  char_t *output, size_t *written);
312 
316 
319 
320 void estClientDeinit(EstClientContext *context);
321 
322 //C++ guard
323 #ifdef __cplusplus
324 }
325 #endif
326 
327 #endif
#define EST_CLIENT_MAX_CA_CERTS_LEN
Definition: est_client.h:147
const PrngAlgo * prngAlgo
Pseudo-random number generator to be used.
Definition: est_client.h:229
#define EST_CLIENT_MAX_URI_LEN
Definition: est_client.h:119
EstClientState state
EST client state.
Definition: est_client.h:225
X509KeyType keyType
Public key type.
Definition: est_client.h:237
error_t estClientReEnroll(EstClientContext *context)
Certificate re-enrollment.
Definition: est_client.c:910
EstClientTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition: est_client.h:232
int bool_t
Definition: compiler_port.h:61
char_t caCerts[EST_CLIENT_MAX_CA_CERTS_LEN]
CA certificates.
Definition: est_client.h:250
uint_t allowedAuthModes
Allowed HTTP authentication modes.
Definition: est_client.h:256
uint8_t cert[EST_CLIENT_MAX_CERT_LEN]
Client's certificate.
Definition: est_client.h:248
IP network address.
Definition: ip.h:90
error_t estClientSetAuthInfo(EstClientContext *context, const char_t *username, const char_t *password)
Set authentication information.
Definition: est_client.c:273
#define PrngAlgo
Definition: crypto.h:1008
@ EST_CLIENT_STATE_CONNECTED
Definition: est_client.h:175
@ EST_CLIENT_STATE_DISCONNECTED
Definition: est_client.h:173
error_t estClientSetHost(EstClientContext *context, const char_t *host)
Set the domain name of the EST server.
Definition: est_client.c:199
@ EST_REQ_STATE_FORMAT_BODY
Definition: est_client.h:193
@ EST_CLIENT_STATE_REENROLL
Definition: est_client.h:179
error_t estClientBindToInterface(EstClientContext *context, NetInterface *interface)
Bind the EST client to a particular network interface.
Definition: est_client.c:290
@ EST_REQ_STATE_COMPLETE
Definition: est_client.h:199
size_t bufferLen
Length of the buffer, in bytes.
Definition: est_client.h:253
NetInterface * interface
Underlying network interface.
Definition: est_client.h:227
@ EST_CLIENT_STATE_CSR_GEN
Definition: est_client.h:177
char_t contentType[EST_CLIENT_MAX_CONTENT_TYPE_LEN+1]
Content type of the response.
Definition: est_client.h:259
#define EST_CLIENT_BUFFER_SIZE
Definition: est_client.h:105
#define EST_CLIENT_MAX_CERT_LEN
Definition: est_client.h:140
error_t estClientEnroll(EstClientContext *context)
Certificate enrollment.
Definition: est_client.c:821
HttpClientContext httpClientContext
HTTP client context.
Definition: est_client.h:231
void * prngContext
Pseudo-random number generator context.
Definition: est_client.h:230
EstRequestState
HTTP request states.
Definition: est_client.h:189
error_t(* EstClientTlsInitCallback)(EstClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition: est_client.h:207
error_t estClientClose(EstClientContext *context)
Close the connection with the EST server.
Definition: est_client.c:1069
#define HttpClientContext
Definition: http_client.h:198
uint8_t csr[EST_CLIENT_MAX_CSR_LEN]
CSR.
Definition: est_client.h:246
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
@ EST_REQ_STATE_PARSE_HEADER
Definition: est_client.h:196
error_t estClientSetTimeout(EstClientContext *context, systime_t timeout)
Set communication timeout.
Definition: est_client.c:178
HttpAuthMode selectedAuthMode
Selected HTTP authentication mode.
Definition: est_client.h:257
error_t estClientSetPrng(EstClientContext *context, const PrngAlgo *prngAlgo, void *prngContext)
Set the pseudo-random number generator to be used.
Definition: est_client.c:154
EstClientCsrGenCallback csrGenCallback
CSR generation callback function.
Definition: est_client.h:233
uint_t statusCode
HTTP status code.
Definition: est_client.h:258
RsaPublicKey rsaPublicKey
RSA public key.
Definition: est_client.h:239
error_t estClientLoadCert(EstClientContext *context, const char_t *input, size_t length)
Load client's certificate.
Definition: est_client.c:563
EstRequestState requestState
HTTP request state.
Definition: est_client.h:226
RSA public key.
Definition: rsa.h:57
#define NetInterface
Definition: net.h:36
#define EST_CLIENT_MAX_HOST_LEN
Definition: est_client.h:112
bool_t useExplicitTa
Use of explicit TA database.
Definition: est_client.h:255
error_t estClientGetCaCerts(EstClientContext *context)
Get CA certificates.
Definition: est_client.c:762
@ EST_REQ_STATE_FORMAT_HEADER
Definition: est_client.h:191
#define EST_CLIENT_MAX_CSR_LEN
Definition: est_client.h:133
RsaPrivateKey rsaPrivateKey
RSA private key.
Definition: est_client.h:240
EC private key.
Definition: ec.h:432
error_t estClientDisconnect(EstClientContext *context)
Gracefully disconnect from the EST server.
Definition: est_client.c:1003
@ EST_REQ_STATE_RECEIVE_HEADER
Definition: est_client.h:195
uint8_t length
Definition: tcp.h:375
EstClientState
EST client states.
Definition: est_client.h:172
error_t(* EstClientCsrGenCallback)(EstClientContext *context, const char_t *challengePwd, uint8_t *buffer, size_t size, size_t *length)
CSR generation callback function.
Definition: est_client.h:215
HTTP client (HyperText Transfer Protocol)
char_t pathPrefix[EST_CLIENT_MAX_URI_LEN+1]
Path prefix.
Definition: est_client.h:236
error_t estClientLoadCaCerts(EstClientContext *context, const char_t *input, size_t length)
Load implicit TA database.
Definition: est_client.c:659
@ EST_CLIENT_STATE_CONNECTING
Definition: est_client.h:174
uint32_t systime_t
System time.
EST client context.
Definition: est_client.h:224
EC public key.
Definition: ec.h:421
#define EST_CLIENT_PRIVATE_CONTEXT
Definition: est_client.h:154
char char_t
Definition: compiler_port.h:55
size_t bufferPos
Current position in the buffer.
Definition: est_client.h:254
@ EST_REQ_STATE_CLOSE_BODY
Definition: est_client.h:198
void estClientDeinit(EstClientContext *context)
Release EST client context.
Definition: est_client.c:1090
@ EST_REQ_STATE_RECEIVE_BODY
Definition: est_client.h:197
error_t estClientStoreCaCerts(EstClientContext *context, char_t *output, size_t *written)
Store CA certificates.
Definition: est_client.c:717
uint8_t buffer[EST_CLIENT_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: est_client.h:252
@ EST_REQ_STATE_SEND_BODY
Definition: est_client.h:194
systime_t timeout
Timeout value.
Definition: est_client.h:228
error_t estClientRegisterCsrGenCallback(EstClientContext *context, EstClientCsrGenCallback callback)
Register CSR generation callback function.
Definition: est_client.c:131
size_t certLen
Length of the client's certificate, in bytes.
Definition: est_client.h:249
RSA private key.
Definition: rsa.h:68
error_t estClientLoadKeyPair(EstClientContext *context, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load public/private key pair.
Definition: est_client.c:429
uint16_t serverPort
TCP port number.
Definition: est_client.h:235
error_t estClientStoreCert(EstClientContext *context, char_t *output, size_t *written)
Store client's certificate.
Definition: est_client.c:624
@ EST_REQ_STATE_INIT
Definition: est_client.h:190
EcPublicKey ecPublicKey
EC public key.
Definition: est_client.h:243
error_t estClientInit(EstClientContext *context)
EST client initialization.
Definition: est_client.c:62
#define EST_CLIENT_MAX_CONTENT_TYPE_LEN
Definition: est_client.h:126
@ EST_CLIENT_STATE_GET_CA
Definition: est_client.h:176
@ EST_REQ_STATE_SEND_HEADER
Definition: est_client.h:192
size_t caCertsLen
Length of the CA certificates, in bytes.
Definition: est_client.h:251
void estClientUnloadKeyPair(EstClientContext *context)
Unload public/private key pair.
Definition: est_client.c:522
error_t estClientSetPathPrefix(EstClientContext *context, const char_t *pathPrefix)
Set path prefix.
Definition: est_client.c:224
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
X509KeyType
Public Key types.
Definition: x509_common.h:635
HttpAuthMode
HTTP authentication schemes.
Definition: http_common.h:72
@ EST_CLIENT_STATE_DISCONNECTING
Definition: est_client.h:180
#define EstClientContext
Definition: est_client.h:159
EcPrivateKey ecPrivateKey
EC private key.
Definition: est_client.h:244
error_t estClientSetAllowedAuthModes(EstClientContext *context, uint_t allowedAuthModes)
Set allowed HTTP authentication modes.
Definition: est_client.c:250
@ EST_CLIENT_STATE_ENROLL
Definition: est_client.h:178
error_t estClientConnect(EstClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Specify the address of the EST server.
Definition: est_client.c:313
char_t serverName[EST_CLIENT_MAX_HOST_LEN+1]
Host name of the EST server.
Definition: est_client.h:234
size_t csrLen
Length of the CSR, in bytes.
Definition: est_client.h:247
error_t estClientRegisterTlsInitCallback(EstClientContext *context, EstClientTlsInitCallback callback)
Register TLS initialization callback function.
Definition: est_client.c:109