acme_dns_client_misc.c
Go to the documentation of this file.
1 /**
2  * @file acme_dns_client_misc.c
3  * @brief Helper functions for ACME-DNS client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneACME 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.4.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL ACME_DNS_TRACE_LEVEL
33 
34 //Dependencies
37 #include "jansson.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (ACME_DNS_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Format HTTP request body (register endpoint)
46  * @param[in] context Pointer to the ACME-DNS client context
47  * @return Error code
48  **/
49 
51 {
52  bool_t defaultPort;
53 
54  //Create an HTTP request
55  httpClientCreateRequest(&context->httpClientContext);
56  httpClientSetMethod(&context->httpClientContext, "POST");
57  httpClientSetUri(&context->httpClientContext, "/register");
58 
59 #if (ACME_DNS_CLIENT_TLS_SUPPORT == ENABLED)
60  //"https" URI scheme?
61  if(context->tlsInitCallback != NULL)
62  {
63  //The default port number is 443 for "https" URI scheme
64  defaultPort = (context->serverPort == HTTPS_PORT) ? TRUE : FALSE;
65  }
66  else
67 #endif
68  //"http" URI scheme?
69  {
70  //The default port number is 80 for "http" URI scheme
71  defaultPort = (context->serverPort == HTTP_PORT) ? TRUE : FALSE;
72  }
73 
74  //A client must send a Host header field in all HTTP/1.1 requests (refer
75  //to RFC 7230, section 5.4)
76  if(defaultPort)
77  {
78  //A host without any trailing port information implies the default port
79  //for the service requested
80  httpClientAddHeaderField(&context->httpClientContext, "Host",
81  context->serverName);
82  }
83  else
84  {
85  //Append the port number information to the host
86  httpClientFormatHeaderField(&context->httpClientContext,
87  "Host", "%s:%" PRIu16, context->serverName, context->serverPort);
88  }
89 
90  //Add HTTP header fields
91  httpClientAddHeaderField(&context->httpClientContext, "User-Agent",
92  "Mozilla/5.0");
93 
94  httpClientAddHeaderField(&context->httpClientContext, "Content-Type",
95  "application/json");
96 
97  //The body of the request is empty
98  httpClientSetContentLength(&context->httpClientContext, 0);
99 
100  //Successful processing
101  return NO_ERROR;
102 }
103 
104 
105 /**
106  * @brief Parse HTTP response (register endpoint)
107  * @param[in] context Pointer to the ACME-DNS client context
108  * @return Error code
109  **/
110 
112 {
113  error_t error;
114  const char_t *username;
115  const char_t *password;
116  const char_t *subDomain;
117  const char_t *fullDomain;
118  json_t *rootObj;
119  json_t *usernameObj;
120  json_t *passwordObj;
121  json_t *subDomainObj;
122  json_t *fullDomainObj;
123 
124  //Check HTTP status code
125  if(!HTTP_STATUS_CODE_2YZ(context->statusCode))
127 
128  //Check whether the body of the response is truncated
129  if(context->bufferLen >= ACME_DNS_CLIENT_BUFFER_SIZE)
131 
132  //Initialize status code
133  error = ERROR_INVALID_RESPONSE;
134 
135  //Clear credentials
136  context->username[0] = '\0';
137  context->password[0] = '\0';
138  context->subDomain[0] = '\0';
139  context->fullDomain[0] = '\0';
140 
141  //Decode JSON string
142  rootObj = json_loads(context->buffer, 0, NULL);
143 
144  //Successful parsing?
145  if(json_is_object(rootObj))
146  {
147  //The method returns a new unique subdomain and credentials needed to
148  //update the record
149  usernameObj = json_object_get(rootObj, "username");
150  passwordObj = json_object_get(rootObj, "password");
151  subDomainObj = json_object_get(rootObj, "subdomain");
152  fullDomainObj = json_object_get(rootObj, "fulldomain");
153 
154  //Valid credentials?
155  if(json_is_string(usernameObj) &&
156  json_is_string(passwordObj) &&
157  json_is_string(subDomainObj) &&
158  json_is_string(fullDomainObj))
159  {
160  //The strings are NULL-terminated
161  username = json_string_value(usernameObj);
162  password = json_string_value(passwordObj);
163  subDomain = json_string_value(subDomainObj);
164  fullDomain = json_string_value(fullDomainObj);
165 
166  //Check the length of the URLs
167  if(osStrlen(username) <= ACME_DNS_CLIENT_MAX_USERNAME_LEN &&
171  {
172  //Save the credentials
173  osStrcpy(context->username, username);
174  osStrcpy(context->password, password);
175  osStrcpy(context->subDomain, subDomain);
176  osStrcpy(context->fullDomain, fullDomain);
177 
178  //The response was successfully parsed
179  error = NO_ERROR;
180  }
181  }
182  }
183 
184  //Release JSON object
185  json_decref(rootObj);
186 
187  //Return status code
188  return error;
189 }
190 
191 
192 /**
193  * @brief Format HTTP request body (update endpoint)
194  * @param[in] context Pointer to the ACME-DNS client context
195  * @param[in] txt NULL-terminated string that contains the value of the TXT record
196  * @return Error code
197  **/
198 
200  const char_t *txt)
201 {
202  bool_t defaultPort;
203 
204  //Check the length of the TXT record
206  return ERROR_INVALID_LENGTH;
207 
208  //Create an HTTP request
209  httpClientCreateRequest(&context->httpClientContext);
210  httpClientSetMethod(&context->httpClientContext, "POST");
211  httpClientSetUri(&context->httpClientContext, "/update");
212 
213 #if (ACME_DNS_CLIENT_TLS_SUPPORT == ENABLED)
214  //"https" URI scheme?
215  if(context->tlsInitCallback != NULL)
216  {
217  //The default port number is 443 for "https" URI scheme
218  defaultPort = (context->serverPort == HTTPS_PORT) ? TRUE : FALSE;
219  }
220  else
221 #endif
222  //"http" URI scheme?
223  {
224  //The default port number is 80 for "http" URI scheme
225  defaultPort = (context->serverPort == HTTP_PORT) ? TRUE : FALSE;
226  }
227 
228  //A client must send a Host header field in all HTTP/1.1 requests (refer
229  //to RFC 7230, section 5.4)
230  if(defaultPort)
231  {
232  //A host without any trailing port information implies the default port
233  //for the service requested
234  httpClientAddHeaderField(&context->httpClientContext, "Host",
235  context->serverName);
236  }
237  else
238  {
239  //Append the port number information to the host
240  httpClientFormatHeaderField(&context->httpClientContext,
241  "Host", "%s:%" PRIu16, context->serverName, context->serverPort);
242  }
243 
244  //Add HTTP header fields
245  httpClientAddHeaderField(&context->httpClientContext, "User-Agent",
246  "Mozilla/5.0");
247 
248  httpClientAddHeaderField(&context->httpClientContext, "X-Api-User",
249  context->username);
250 
251  httpClientAddHeaderField(&context->httpClientContext, "X-Api-Key",
252  context->password);
253 
254  httpClientAddHeaderField(&context->httpClientContext, "Content-Type",
255  "application/json");
256 
257  //Format the body of the POST request
258  context->bufferLen = osSprintf(context->buffer,
259  "{\"subdomain\":\"%s\",\"txt\":\"%s\"}", context->subDomain, txt);
260 
261  //Specify the length of the request body
262  httpClientSetContentLength(&context->httpClientContext, context->bufferLen);
263 
264  //Successful processing
265  return NO_ERROR;
266 }
267 
268 
269 /**
270  * @brief Parse HTTP response (update endpoint)
271  * @param[in] context Pointer to the ACME-DNS client context
272  * @return Error code
273  **/
274 
276 {
277  //Check HTTP status code
278  if(!HTTP_STATUS_CODE_2YZ(context->statusCode))
280 
281  //Check whether the body of the response is truncated
282  if(context->bufferLen >= ACME_DNS_CLIENT_BUFFER_SIZE)
284 
285  //Successful processing
286  return NO_ERROR;
287 }
288 
289 #endif
ACME-DNS client.
#define ACME_DNS_TXT_RECORD_LEN
#define ACME_DNS_CLIENT_MAX_USERNAME_LEN
#define AcmeDnsClientContext
#define ACME_DNS_CLIENT_MAX_SUB_DOMAIN_LEN
#define ACME_DNS_CLIENT_MAX_FULL_DOMAIN_LEN
#define ACME_DNS_CLIENT_MAX_PASSWORD_LEN
#define ACME_DNS_CLIENT_BUFFER_SIZE
error_t acmeDnsClientFormatRegisterRequest(AcmeDnsClientContext *context)
Format HTTP request body (register endpoint)
error_t acmeDnsClientParseUpdateResponse(AcmeDnsClientContext *context)
Parse HTTP response (update endpoint)
error_t acmeDnsClientFormatUpdateRequest(AcmeDnsClientContext *context, const char_t *txt)
Format HTTP request body (update endpoint)
error_t acmeDnsClientParseRegisterResponse(AcmeDnsClientContext *context)
Parse HTTP response (register endpoint)
Helper functions for ACME-DNS client.
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_RESPONSE_TOO_LARGE
Definition: error.h:283
@ ERROR_UNEXPECTED_STATUS
Definition: error.h:282
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_INVALID_RESPONSE
Definition: error.h:71
error_t httpClientFormatHeaderField(HttpClientContext *context, const char_t *name, const char_t *format,...)
Format an HTTP header field.
Definition: http_client.c:890
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
error_t httpClientSetMethod(HttpClientContext *context, const char_t *method)
Set HTTP request method.
Definition: http_client.c:402
error_t httpClientSetUri(HttpClientContext *context, const char_t *uri)
Set request URI.
Definition: http_client.c:462
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
#define HTTP_STATUS_CODE_2YZ(code)
Definition: http_common.h:44
#define HTTPS_PORT
Definition: http_common.h:40
#define osStrlen(s)
Definition: os_port.h:165
#define osSprintf(dest,...)
Definition: os_port.h:231
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
#define osStrcpy(s1, s2)
Definition: os_port.h:207