scep_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file scep_client_transport.c
3  * @brief HTTP transport mechanism
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCRYPTO 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.2
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SCEP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "scep/scep_client.h"
37 #include "debug.h"
38 
39 //Check crypto library configuration
40 #if (SCEP_CLIENT_SUPPORT == ENABLED)
41 
42 
43 /**
44  * @brief Send HTTP request
45  * @param[in] context Pointer to the SCEP client context
46  * @return Error code
47  **/
48 
50 {
51  error_t error;
52  size_t n;
53 
54  //Initialize status code
55  error = NO_ERROR;
56 
57  //Check HTTP request state
58  if(context->requestState == SCEP_REQ_STATE_SEND_HEADER)
59  {
60  //Send HTTP request header
61  error = httpClientWriteHeader(&context->httpClientContext);
62 
63  //Check status code
64  if(!error)
65  {
66  //Check whether the HTTP request contains a body
67  if(context->bufferLen > 0)
68  {
69  //Point to the first byte of the body
70  context->bufferPos = 0;
71 
72  //Send HTTP request body
73  context->requestState = SCEP_REQ_STATE_SEND_BODY;
74  }
75  else
76  {
77  //Receive HTTP response header
78  context->requestState = SCEP_REQ_STATE_RECEIVE_HEADER;
79  }
80  }
81  }
82  else if(context->requestState == SCEP_REQ_STATE_SEND_BODY)
83  {
84  //Send HTTP request body
85  if(context->bufferPos < context->bufferLen)
86  {
87  //Send more data
88  error = httpClientWriteBody(&context->httpClientContext,
89  context->buffer + context->bufferPos,
90  context->bufferLen - context->bufferPos, &n, 0);
91 
92  //Check status code
93  if(!error)
94  {
95  //Advance data pointer
96  context->bufferPos += n;
97  }
98  }
99  else
100  {
101  //Update HTTP request state
102  context->requestState = SCEP_REQ_STATE_RECEIVE_HEADER;
103  }
104  }
105  else if(context->requestState == SCEP_REQ_STATE_RECEIVE_HEADER)
106  {
107  //Receive HTTP response header
108  error = httpClientReadHeader(&context->httpClientContext);
109 
110  //Check status code
111  if(!error)
112  {
113  //Update HTTP request state
114  context->requestState = SCEP_REQ_STATE_PARSE_HEADER;
115  }
116  }
117  else if(context->requestState == SCEP_REQ_STATE_PARSE_HEADER)
118  {
119  //Parse HTTP response header
120  error = scepClientParseResponseHeader(context);
121 
122  //Check status code
123  if(!error)
124  {
125  //Flush the receive buffer
126  context->bufferLen = 0;
127  context->bufferPos = 0;
128 
129  //Update HTTP request state
130  context->requestState = SCEP_REQ_STATE_RECEIVE_BODY;
131  }
132  }
133  else if(context->requestState == SCEP_REQ_STATE_RECEIVE_BODY)
134  {
135  //Receive HTTP response body
136  if(context->bufferLen < SCEP_CLIENT_BUFFER_SIZE)
137  {
138  //Receive more data
139  error = httpClientReadBody(&context->httpClientContext,
140  context->buffer + context->bufferLen,
141  SCEP_CLIENT_BUFFER_SIZE - context->bufferLen, &n, 0);
142 
143  //Check status code
144  if(error == NO_ERROR)
145  {
146  //Advance data pointer
147  context->bufferLen += n;
148  }
149  else if(error == ERROR_END_OF_STREAM)
150  {
151  //The end of the response body has been reached
152  error = NO_ERROR;
153 
154  //Update HTTP request state
155  context->requestState = SCEP_REQ_STATE_CLOSE_BODY;
156  }
157  else
158  {
159  //Just for sanity
160  }
161  }
162  else
163  {
164  //Update HTTP request state
165  context->requestState = SCEP_REQ_STATE_CLOSE_BODY;
166  }
167  }
168  else if(context->requestState == SCEP_REQ_STATE_CLOSE_BODY)
169  {
170  //Close HTTP response body
171  error = httpClientCloseBody(&context->httpClientContext);
172 
173  //Check status code
174  if(!error)
175  {
176  //Update HTTP request state
177  context->requestState = SCEP_REQ_STATE_COMPLETE;
178  }
179  }
180  else
181  {
182  //Invalid state
183  error = ERROR_WRONG_STATE;
184  }
185 
186  //Return status code
187  return error;
188 }
189 
190 
191 /**
192  * @brief Format HTTP request header
193  * @param[in] context Pointer to the SCEP client context
194  * @param[in] method NULL-terminating string containing the HTTP method
195  * @param[in] operation NULL-terminating string containing the SCEP operation
196  * @return Error code
197  **/
198 
200  const char_t *method, const char_t *operation)
201 {
202  error_t error;
203  bool_t defaultPort;
204  HttpClientContext *httpClientContext;
205 
206  //Point to the HTTP client context
207  httpClientContext = &context->httpClientContext;
208 
209  //Create a new HTTP request
210  error = httpClientCreateRequest(httpClientContext);
211  //Any error to report?
212  if(error)
213  return error;
214 
215  //SCEP uses the HTTP POST and GET methods to exchange information with the
216  //CA (refer to RFC 8894, section 4.1)
217  error = httpClientSetMethod(httpClientContext, method);
218  //Any error to report?
219  if(error)
220  return error;
221 
222  //SCEPPATH is the HTTP URL path for accessing the CA
223  error = httpClientSetUri(httpClientContext, context->uri);
224  //Any error to report?
225  if(error)
226  return error;
227 
228  //OPERATION depends on the SCEP transaction
229  error = httpClientAddQueryParam(httpClientContext, "operation", operation);
230  //Any error to report?
231  if(error)
232  return error;
233 
234 #if (SCEP_CLIENT_TLS_SUPPORT == ENABLED)
235  //"https" URI scheme?
236  if(context->tlsInitCallback != NULL)
237  {
238  //The default port number is 443 for "https" URI scheme
239  defaultPort = (context->serverPort == HTTPS_PORT) ? TRUE : FALSE;
240  }
241  else
242 #endif
243  //"http" URI scheme?
244  {
245  //The default port number is 80 for "http" URI scheme
246  defaultPort = (context->serverPort == HTTP_PORT) ? TRUE : FALSE;
247  }
248 
249  //A client must send a Host header field in all HTTP/1.1 requests (refer
250  //to RFC 7230, section 5.4)
251  if(defaultPort)
252  {
253  //A host without any trailing port information implies the default port
254  //for the service requested
255  error = httpClientAddHeaderField(httpClientContext, "Host",
256  context->serverName);
257  }
258  else
259  {
260  //Append the port number information to the host
261  error = httpClientFormatHeaderField(httpClientContext,
262  "Host", "%s:%" PRIu16, context->serverName, context->serverPort);
263  }
264 
265  //Any error to report?
266  if(error)
267  return error;
268 
269  //Set User-Agent header field
270  error = httpClientAddHeaderField(httpClientContext, "User-Agent",
271  "Mozilla/5.0");
272  //Any error to report?
273  if(error)
274  return error;
275 
276  //POST request?
277  if(osStrcmp(method, "POST") == 0)
278  {
279  //When implemented using HTTP POST, the message is sent with a
280  //Content-Type of "application/x-pki-message" (refer to RFC 8894,
281  //section 4.3)
282  error = httpClientAddHeaderField(httpClientContext, "Content-Type",
283  "application/x-pki-message");
284  //Any error to report?
285  if(error)
286  return error;
287 
288  //Specify the length of the request body
289  error = httpClientSetContentLength(httpClientContext, context->bufferLen);
290  //Any error to report?
291  if(error)
292  return error;
293  }
294  else
295  {
296  //The HTTP request body is empty
297  context->bufferLen = 0;
298  }
299 
300  //Successful processing
301  return NO_ERROR;
302 }
303 
304 
305 /**
306  * @brief Parse HTTP response header
307  * @param[in] context Pointer to the SCEP client context
308  * @return Error code
309  **/
310 
312 {
313  size_t n;
314  char_t *p;
315  const char_t *contentType;
316 
317  //Get HTTP response status code
318  context->statusCode = httpClientGetStatus(&context->httpClientContext);
319 
320  //Get the Content-Type header field
321  contentType = httpClientGetHeaderField(&context->httpClientContext,
322  "Content-Type");
323 
324  //Content-Type header field found?
325  if(contentType != NULL)
326  {
327  //Retrieve the header field value
328  n = osStrlen(contentType);
329  //Limit the length of the string
331 
332  //Save the media type
333  osStrncpy(context->contentType, contentType, n);
334  //Properly terminate the string with a NULL character
335  context->contentType[n] = '\0';
336 
337  //Discard the parameters that may follow the type/subtype
338  osStrtok_r(context->contentType, "; \t", &p);
339  }
340  else
341  {
342  //The Content-Type header field is not present in the response
343  context->contentType[0] = '\0';
344  }
345 
346  //Successful processing
347  return NO_ERROR;
348 }
349 
350 #endif
error_t httpClientCloseBody(HttpClientContext *context)
Close HTTP request or response body.
Definition: http_client.c:2014
int bool_t
Definition: compiler_port.h:61
error_t httpClientSetUri(HttpClientContext *context, const char_t *uri)
Set request URI.
Definition: http_client.c:462
#define ScepClientContext
Definition: scep_client.h:165
error_t httpClientReadBody(HttpClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Read HTTP response body.
Definition: http_client.c:1648
@ SCEP_REQ_STATE_SEND_BODY
Definition: scep_client.h:204
uint8_t p
Definition: ndp.h:300
#define TRUE
Definition: os_port.h:50
#define osStrcmp(s1, s2)
Definition: os_port.h:174
#define osStrlen(s)
Definition: os_port.h:168
@ ERROR_END_OF_STREAM
Definition: error.h:211
error_t httpClientSetContentLength(HttpClientContext *context, size_t length)
Set the length of the HTTP request body.
Definition: http_client.c:987
error_t httpClientCreateRequest(HttpClientContext *context)
Create a new HTTP request.
Definition: http_client.c:365
#define SCEP_CLIENT_MAX_CONTENT_TYPE_LEN
Definition: scep_client.h:125
@ SCEP_REQ_STATE_RECEIVE_HEADER
Definition: scep_client.h:205
@ ERROR_WRONG_STATE
Definition: error.h:210
@ SCEP_REQ_STATE_COMPLETE
Definition: scep_client.h:209
#define FALSE
Definition: os_port.h:46
@ SCEP_REQ_STATE_RECEIVE_BODY
Definition: scep_client.h:207
#define HttpClientContext
Definition: http_client.h:198
error_t httpClientWriteHeader(HttpClientContext *context)
Write HTTP request header.
Definition: http_client.c:1014
#define SCEP_CLIENT_BUFFER_SIZE
Definition: scep_client.h:104
error_t
Error codes.
Definition: error.h:43
error_t httpClientFormatHeaderField(HttpClientContext *context, const char_t *name, const char_t *format,...)
Format an HTTP header field.
Definition: http_client.c:890
@ SCEP_REQ_STATE_PARSE_HEADER
Definition: scep_client.h:206
#define osStrtok_r(s, delim, last)
Definition: os_port.h:228
@ SCEP_REQ_STATE_SEND_HEADER
Definition: scep_client.h:202
const char_t * httpClientGetHeaderField(HttpClientContext *context, const char_t *name)
Retrieve the value of the specified header field name.
Definition: http_client.c:1541
error_t scepClientParseResponseHeader(ScepClientContext *context)
Parse HTTP response header.
error_t scepClientSendRequest(ScepClientContext *context)
Send HTTP request.
uint_t httpClientGetStatus(HttpClientContext *context)
Retrieve the HTTP status code of the response.
Definition: http_client.c:1514
#define MIN(a, b)
Definition: os_port.h:63
error_t httpClientReadHeader(HttpClientContext *context)
Read HTTP response header.
Definition: http_client.c:1374
#define HTTPS_PORT
Definition: http_common.h:40
SCEP client.
char char_t
Definition: compiler_port.h:55
error_t httpClientAddHeaderField(HttpClientContext *context, const char_t *name, const char_t *value)
Add a header field to the HTTP request.
Definition: http_client.c:808
#define HTTP_PORT
Definition: http_common.h:38
uint8_t n
error_t httpClientWriteBody(HttpClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Write HTTP request body.
Definition: http_client.c:1137
#define osStrncpy(s1, s2, length)
Definition: os_port.h:216
@ SCEP_REQ_STATE_CLOSE_BODY
Definition: scep_client.h:208
error_t scepClientFormatRequestHeader(ScepClientContext *context, const char_t *method, const char_t *operation)
Format HTTP request header.
error_t httpClientAddQueryParam(HttpClientContext *context, const char_t *name, const char_t *value)
Add a key/value pair to the query string.
Definition: http_client.c:691
error_t httpClientSetMethod(HttpClientContext *context, const char_t *method)
Set HTTP request method.
Definition: http_client.c:402
HTTP transport mechanism.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.