acme_client.c
Go to the documentation of this file.
1 /**
2  * @file acme_client.c
3  * @brief ACME client (Automatic Certificate Management Environment)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2026 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  * @section Description
28  *
29  * ACME is a protocol that a CA and an applicant can use to automate the
30  * process of verification and certificate issuance. The protocol also
31  * provides facilities for other certificate management functions, such as
32  * certificate revocation. Refer to the following RFCs for complete details:
33  * - RFC 8555: Automatic Certificate Management Environment (ACME)
34  * - RFC 8737: ACME TLS Application-Layer Protocol Negotiation (ALPN) Challenge Extension
35  * - RFC 7515: JSON Web Signature (JWS)
36  * - RFC 7517: JSON Web Key (JWK)
37  * - RFC 7518: JSON Web Algorithms (JWA)
38  * - RFC 7638: JSON Web Key (JWK) Thumbprint
39  *
40  * @author Oryx Embedded SARL (www.oryx-embedded.com)
41  * @version 2.6.8
42  **/
43 
44 //Switch to the appropriate trace level
45 #define TRACE_LEVEL ACME_TRACE_LEVEL
46 
47 //Dependencies
48 #include "acme/acme_client.h"
50 #include "acme/acme_client_nonce.h"
52 #include "acme/acme_client_order.h"
53 #include "acme/acme_client_auth.h"
56 #include "acme/acme_client_misc.h"
57 #include "debug.h"
58 
59 //Check TCP/IP stack configuration
60 #if (ACME_CLIENT_SUPPORT == ENABLED)
61 
62 
63 /**
64  * @brief Initialize ACME client context
65  * @param[in] context Pointer to the ACME client context
66  * @return Error code
67  **/
68 
70 {
71  error_t error;
72 
73  //Make sure the ACME client context is valid
74  if(context == NULL)
75  {
77  }
78 
79  //Clear ACME client context
80  osMemset(context, 0, sizeof(AcmeClientContext));
81 
82  //Attach TCP/IP stack context
83  context->netContext = netGetDefaultContext();
84 
85  //Initialize HTTP client context
86  error = httpClientInit(&context->httpClientContext);
87  //Any error to report?
88  if(error)
89  {
90  return error;
91  }
92 
93  //Initialize ACME client state
94  context->state = ACME_CLIENT_STATE_DISCONNECTED;
95  //Initialize HTTP request state
96  context->requestState = ACME_REQ_STATE_INIT;
97  //Default timeout
98  context->timeout = ACME_CLIENT_DEFAULT_TIMEOUT;
99 
100  //Default directory URI
101  osStrcpy(context->directoryUri, "/directory");
102 
103  //Successful initialization
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief Register TLS initialization callback function
110  * @param[in] context Pointer to the ACME client context
111  * @param[in] callback TLS initialization callback function
112  * @return Error code
113  **/
114 
116  AcmeClientTlsInitCallback callback)
117 {
118  //Make sure the ACME client context is valid
119  if(context == NULL)
120  {
122  }
123 
124  //Save callback function
125  context->tlsInitCallback = callback;
126 
127  //Successful processing
128  return NO_ERROR;
129 }
130 
131 
132 /**
133  * @brief Register CSR generation callback function
134  * @param[in] context Pointer to the ACME client context
135  * @param[in] callback CSR generation callback function
136  * @return Error code
137  **/
138 
140  AcmeClientCsrGenCallback callback)
141 {
142  //Make sure the ACME client context is valid
143  if(context == NULL)
144  {
146  }
147 
148  //Save callback function
149  context->csrGenCallback = callback;
150 
151  //Successful processing
152  return NO_ERROR;
153 }
154 
155 
156 /**
157  * @brief Set the pseudo-random number generator to be used
158  * @param[in] context Pointer to the ACME client context
159  * @param[in] prngAlgo PRNG algorithm
160  * @param[in] prngContext Pointer to the PRNG context
161  * @return Error code
162  **/
163 
165  void *prngContext)
166 {
167  //Check parameters
168  if(context == NULL || prngAlgo == NULL || prngContext == NULL)
169  {
171  }
172 
173  //PRNG algorithm that will be used to generate random numbers
174  context->prngAlgo = prngAlgo;
175  //PRNG context
176  context->prngContext = prngContext;
177 
178  //Successful processing
179  return NO_ERROR;
180 }
181 
182 
183 /**
184  * @brief Set communication timeout
185  * @param[in] context Pointer to the ACME client context
186  * @param[in] timeout Timeout value, in milliseconds
187  * @return Error code
188  **/
189 
191 {
192  //Make sure the ACME client context is valid
193  if(context == NULL)
194  {
196  }
197 
198  //Save timeout value
199  context->timeout = timeout;
200 
201  //Successful processing
202  return NO_ERROR;
203 }
204 
205 
206 /**
207  * @brief Set the domain name of the ACME server
208  * @param[in] context Pointer to the ACME client context
209  * @param[in] host NULL-terminated string containing the host name
210  * @return Error code
211  **/
212 
214 {
215  //Check parameters
216  if(context == NULL || host == NULL)
217  {
219  }
220 
221  //Make sure the length of the host name is acceptable
223  {
224  return ERROR_INVALID_LENGTH;
225  }
226 
227  //Save host name
228  osStrcpy(context->serverName, host);
229 
230  //Successful processing
231  return NO_ERROR;
232 }
233 
234 
235 /**
236  * @brief Set the URI of the directory object
237  * @param[in] context Pointer to the ACME client context
238  * @param[in] directoryUri NULL-terminated string containing the directory URI
239  * @return Error code
240  **/
241 
243  const char_t *directoryUri)
244 {
245  //Check parameters
246  if(context == NULL || directoryUri == NULL)
247  {
249  }
250 
251  //Make sure the length of the URI is acceptable
252  if(osStrlen(directoryUri) > ACME_CLIENT_MAX_URI_LEN)
253  {
254  return ERROR_INVALID_LENGTH;
255  }
256 
257  //Save the URI of the directory object
258  osStrcpy(context->directoryUri, directoryUri);
259 
260  //Successful processing
261  return NO_ERROR;
262 }
263 
264 
265 /**
266  * @brief Bind the ACME client to a particular network interface
267  * @param[in] context Pointer to the ACME client context
268  * @param[in] interface Network interface to be used
269  * @return Error code
270  **/
271 
273  NetInterface *interface)
274 {
275  //Make sure the ACME client context is valid
276  if(context == NULL)
277  {
279  }
280 
281  //Explicitly associate the ACME client with the specified interface
282  context->interface = interface;
283 
284  //Successful processing
285  return NO_ERROR;
286 }
287 
288 
289 /**
290  * @brief Establish a connection with the specified ACME server
291  * @param[in] context Pointer to the ACME client context
292  * @param[in] serverIpAddr IP address of the ACME server to connect to
293  * @param[in] serverPort Port number
294  * @return Error code
295  **/
296 
298  const IpAddr *serverIpAddr, uint16_t serverPort)
299 {
300  error_t error;
301 
302  //Initialize status code
303  error = NO_ERROR;
304 
305  //Make sure the ACME client context is valid
306  if(context == NULL)
307  {
309  }
310 
311  //Establish connection with the HTTP server
312  while(!error)
313  {
314  //Check ACME client state
315  if(context->state == ACME_CLIENT_STATE_DISCONNECTED)
316  {
317  //Save the TCP port number to be used
318  context->serverPort = serverPort;
319 
320  //Use of HTTPS is required (refer to RFC 8555, section 6.1)
321  if(context->tlsInitCallback != NULL)
322  {
323  //Register TLS initialization callback
324  error = httpClientRegisterTlsInitCallback(&context->httpClientContext,
325  acmeClientInitTlsContext, context);
326  }
327  else
328  {
329  //Report an error
330  error = ERROR_INVALID_PARAMETER;
331  }
332 
333  //Check status code
334  if(!error)
335  {
336  //Select HTTP protocol version
337  error = httpClientSetVersion(&context->httpClientContext,
339  }
340 
341  //Check status code
342  if(!error)
343  {
344  //Set timeout value for blocking operations
345  error = httpClientSetTimeout(&context->httpClientContext,
346  context->timeout);
347  }
348 
349  //Check status code
350  if(!error)
351  {
352  //Bind the HTTP client to the relevant network interface
353  error = httpClientBindToInterface(&context->httpClientContext,
354  context->interface);
355  }
356 
357  //Check status code
358  if(!error)
359  {
360  //Establish HTTPS connection
361  context->state = ACME_CLIENT_STATE_CONNECTING;
362  }
363  }
364  else if(context->state == ACME_CLIENT_STATE_CONNECTING)
365  {
366  //Establish HTTPS connection
367  error = httpClientConnect(&context->httpClientContext, serverIpAddr,
368  serverPort);
369 
370  //Check status code
371  if(error == NO_ERROR)
372  {
373  //The HTTPS connection is established
374  context->state = ACME_CLIENT_STATE_CONNECTED;
375  }
376  }
377  else if(context->state == ACME_CLIENT_STATE_CONNECTED)
378  {
379  //Initialize HTTP request state
380  context->requestState = ACME_REQ_STATE_INIT;
381 
382  //Initialize the directory object
383  osMemset(&context->directory, 0, sizeof(AcmeDirectory));
384  //Invalidate the nonce
385  osMemset(context->nonce, 0, ACME_CLIENT_MAX_NONCE_LEN);
386 
387  //Reset error counter
388  context->badNonceErrors = 0;
389 
390  //The client is connected to the ACME server
391  break;
392  }
393  else
394  {
395  //Invalid state
396  error = ERROR_WRONG_STATE;
397  }
398  }
399 
400  //Failed to establish connection with the ACME server?
401  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
402  {
403  //Clean up side effects
404  httpClientClose(&context->httpClientContext);
405  //Update ACME client state
406  context->state = ACME_CLIENT_STATE_DISCONNECTED;
407  }
408 
409  //Return status code
410  return error;
411 }
412 
413 
414 /**
415  * @brief Load account key pair
416  * @param[in] context Pointer to the ACME client context
417  * @param[in] publicKey Public key (PEM format)
418  * @param[in] publicKeyLen Length of the public key
419  * @param[in] privateKey Private key (PEM format)
420  * @param[in] privateKeyLen Length of the private key
421  * @param[in] password NULL-terminated string containing the password. This
422  * parameter is required if the private key is encrypted
423  * @return Error code
424  **/
425 
427  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
428  size_t privateKeyLen, const char_t *password)
429 {
430  //Check parameters
431  if(context == NULL || publicKey == NULL || publicKeyLen == 0 ||
432  privateKey == NULL || privateKeyLen == 0)
433  {
435  }
436 
437  //Release the current key pair, if any
438  acmeClientUnloadKeyPair(&context->accountKey);
439 
440  //The public and private keys are encoded in PEM format
441  return acmeClientLoadKeyPair(&context->accountKey, publicKey, publicKeyLen,
442  privateKey, privateKeyLen, password);
443 }
444 
445 
446 /**
447  * @brief Account creation
448  * @param[in] context Pointer to the ACME client context
449  * @param[in] params Account information
450  * @return Error code
451  **/
452 
454  const AcmeAccountParams *params)
455 {
456  error_t error;
457 
458  //Check parameters
459  if(context == NULL || params == NULL)
460  {
462  }
463 
464  //Initialize variables
465  error = NO_ERROR;
466 
467  //Execute the sequence of HTTP requests
468  while(!error)
469  {
470  //Check ACME client state
471  if(context->state == ACME_CLIENT_STATE_CONNECTED)
472  {
473  //Check account information
474  error = acmeClientCheckAccountParams(params);
475 
476  //Check status code
477  if(!error)
478  {
479  //Initialize account object
480  osMemset(&context->account, 0, sizeof(AcmeAccount));
481 
482  //Release the current key pair, if any
483  acmeClientUnloadKeyPair(&context->accountKey);
484 
485  //The public and private keys are encoded in PEM format
486  error = acmeClientLoadKeyPair(&context->accountKey,
487  params->publicKey, params->publicKeyLen, params->privateKey,
488  params->privateKeyLen, params->password);
489  }
490 
491  //Check status code
492  if(!error)
493  {
494  //In order to help clients configure themselves with the right URLs
495  //for each ACME operation, ACME servers provide a directory object
496  //(refer to RFC 8555, section 7.1.1)
497  context->state = ACME_CLIENT_STATE_DIRECTORY;
498  }
499  }
500  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
501  {
502  //If the directory object is no longer valid, the client must access
503  //the directory again by sending a GET request to the directory URL
504  error = acmeClientSendDirectoryRequest(context);
505 
506  //Check status code
507  if(!error)
508  {
509  //Update ACME client state
510  context->state = ACME_CLIENT_STATE_NEW_NONCE;
511  }
512  }
513  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
514  {
515  //Before sending a POST request to the server, an ACME client needs to
516  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
517  error = acmeClientSendNewNonceRequest(context);
518 
519  //Check status code
520  if(!error)
521  {
522  //Update ACME client state
523  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
524  }
525  }
526  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
527  {
528  //A client creates a new account by sending a POST request to the
529  //server's newAccount URL (refer to RFC 8555, section 7.3)
530  error = acmeClientSendNewAccountRequest(context, params, FALSE);
531 
532  //Check status code
533  if(!error)
534  {
535  //Update ACME client state
536  context->state = ACME_CLIENT_STATE_CONNECTED;
537  break;
538  }
539  }
540  else
541  {
542  //Invalid state
543  error = ERROR_WRONG_STATE;
544  }
545  }
546 
547  //Unexpected HTTP response?
548  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
549  error == ERROR_RESPONSE_TOO_LARGE)
550  {
551  //Revert to default state
552  context->state = ACME_CLIENT_STATE_CONNECTED;
553  }
554 
555  //Return status code
556  return error;
557 }
558 
559 
560 /**
561  * @brief Account information update
562  * @param[in] context Pointer to the ACME client context
563  * @param[in] params Updated account information
564  * @return Error code
565  **/
566 
568  const AcmeAccountParams *params)
569 {
570  error_t error;
571 
572  //Check parameters
573  if(context == NULL || params == NULL)
574  {
576  }
577 
578  //Initialize variables
579  error = NO_ERROR;
580 
581  //Execute the sequence of HTTP requests
582  while(!error)
583  {
584  //Check ACME client state
585  if(context->state == ACME_CLIENT_STATE_CONNECTED)
586  {
587  //Check account information
588  error = acmeClientCheckAccountParams(params);
589 
590  //Check status code
591  if(!error)
592  {
593  //In order to help clients configure themselves with the right URLs
594  //for each ACME operation, ACME servers provide a directory object
595  //(refer to RFC 8555, section 7.1.1)
596  context->state = ACME_CLIENT_STATE_DIRECTORY;
597  }
598  }
599  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
600  {
601  //If the directory object is no longer valid, the client must access
602  //the directory again by sending a GET request to the directory URL
603  error = acmeClientSendDirectoryRequest(context);
604 
605  //Check status code
606  if(!error)
607  {
608  //Update ACME client state
609  context->state = ACME_CLIENT_STATE_NEW_NONCE;
610  }
611  }
612  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
613  {
614  //Before sending a POST request to the server, an ACME client needs to
615  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
616  error = acmeClientSendNewNonceRequest(context);
617 
618  //Check status code
619  if(!error)
620  {
621  //Update ACME client state
622  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
623  }
624  }
625  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
626  {
627  //If a client wishes to find the URL for an existing account, then
628  //it should do so by sending a POST request to the newAccount URL
629  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
630  //section 7.3.1)
631  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
632 
633  //Check status code
634  if(!error)
635  {
636  //Update ACME client state
637  context->state = ACME_CLIENT_STATE_UPDATE_ACCOUNT;
638  }
639  }
640  else if(context->state == ACME_CLIENT_STATE_UPDATE_ACCOUNT)
641  {
642  //If the client wishes to update the account information, it sends a
643  //POST request with updated information to the account URL (refer to
644  //RFC 8555, section 7.3.1)
645  error = acmeClientSendUpdateAccountRequest(context, params);
646 
647  //Check status code
648  if(!error)
649  {
650  //Update ACME client state
651  context->state = ACME_CLIENT_STATE_CONNECTED;
652  break;
653  }
654  }
655  else
656  {
657  //Invalid state
658  error = ERROR_WRONG_STATE;
659  }
660  }
661 
662  //Unexpected HTTP response?
663  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
664  error == ERROR_RESPONSE_TOO_LARGE)
665  {
666  //Revert to default state
667  context->state = ACME_CLIENT_STATE_CONNECTED;
668  }
669 
670  //Return status code
671  return error;
672 }
673 
674 
675 /**
676  * @brief Account key rollover
677  * @param[in] context Pointer to the ACME client context
678  * @param[in] publicKey New public key (PEM format)
679  * @param[in] publicKeyLen Length of the new public key
680  * @param[in] privateKey New private key (PEM format)
681  * @param[in] privateKeyLen Length of the new private key
682  * @param[in] password NULL-terminated string containing the password. This
683  * parameter is required if the private key is encrypted
684  * @return Error code
685  **/
686 
688  const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey,
689  size_t privateKeyLen, const char_t *password)
690 {
691  error_t error;
692 
693  //Check parameters
694  if(context == NULL || publicKey == NULL || publicKeyLen == 0 ||
695  privateKey == NULL || privateKeyLen == 0)
696  {
698  }
699 
700  //Initialize variables
701  error = NO_ERROR;
702 
703  //Execute the sequence of HTTP requests
704  while(!error)
705  {
706  //Check ACME client state
707  if(context->state == ACME_CLIENT_STATE_CONNECTED)
708  {
709  //In order to help clients configure themselves with the right URLs for
710  //each ACME operation, ACME servers provide a directory object (refer
711  //to RFC 8555, section 7.1.1)
712  context->state = ACME_CLIENT_STATE_DIRECTORY;
713  }
714  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
715  {
716  //If the directory object is no longer valid, the client must access
717  //the directory again by sending a GET request to the directory URL
718  error = acmeClientSendDirectoryRequest(context);
719 
720  //Check status code
721  if(!error)
722  {
723  //Update ACME client state
724  context->state = ACME_CLIENT_STATE_NEW_NONCE;
725  }
726  }
727  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
728  {
729  //Before sending a POST request to the server, an ACME client needs to
730  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
731  error = acmeClientSendNewNonceRequest(context);
732 
733  //Check status code
734  if(!error)
735  {
736  //Update ACME client state
737  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
738  }
739  }
740  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
741  {
742  //If a client wishes to find the URL for an existing account, then
743  //it should do so by sending a POST request to the newAccount URL
744  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
745  //section 7.3.1)
746  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
747 
748  //Check status code
749  if(!error)
750  {
751  //Update ACME client state
752  context->state = ACME_CLIENT_STATE_CHANGE_KEY;
753  }
754  }
755  else if(context->state == ACME_CLIENT_STATE_CHANGE_KEY)
756  {
757  //A client can change the public key that is associated with an
758  //account, by sending a POST request to the server's keyChange
759  //URL (refer to RFC 8555, section 7.3.5)
760  error = acmeClientSendKeyChangeRequest(context, publicKey,
761  publicKeyLen, privateKey, privateKeyLen, password);
762 
763  //Check status code
764  if(!error)
765  {
766  //Unload the old account key
767  acmeClientUnloadKeyPair(&context->accountKey);
768 
769  //Load the new account key
770  error = acmeClientLoadKeyPair(&context->accountKey, publicKey,
771  publicKeyLen, privateKey, privateKeyLen, password);
772 
773  //Update ACME client state
774  context->state = ACME_CLIENT_STATE_CONNECTED;
775  break;
776  }
777  }
778  else
779  {
780  //Invalid state
781  error = ERROR_WRONG_STATE;
782  }
783  }
784 
785  //Unexpected HTTP response?
786  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
787  error == ERROR_RESPONSE_TOO_LARGE)
788  {
789  //Revert to default state
790  context->state = ACME_CLIENT_STATE_CONNECTED;
791  }
792 
793  //Return status code
794  return error;
795 }
796 
797 
798 /**
799  * @brief ACME account deactivation
800  * @param[in] context Pointer to the ACME client context
801  * @return Error code
802  **/
803 
805 {
806  error_t error;
807  AcmeAccountParams params;
808 
809  //Make sure the ACME client context is valid
810  if(context == NULL)
811  {
813  }
814 
815  //Initialize variables
816  error = NO_ERROR;
817 
818  //Execute the sequence of HTTP requests
819  while(!error)
820  {
821  //Check ACME client state
822  if(context->state == ACME_CLIENT_STATE_CONNECTED)
823  {
824  //In order to help clients configure themselves with the right URLs for
825  //each ACME operation, ACME servers provide a directory object (refer
826  //to RFC 8555, section 7.1.1)
827  context->state = ACME_CLIENT_STATE_DIRECTORY;
828  }
829  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
830  {
831  //If the directory object is no longer valid, the client must access
832  //the directory again by sending a GET request to the directory URL
833  error = acmeClientSendDirectoryRequest(context);
834 
835  //Check status code
836  if(!error)
837  {
838  //Update ACME client state
839  context->state = ACME_CLIENT_STATE_NEW_NONCE;
840  }
841  }
842  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
843  {
844  //Before sending a POST request to the server, an ACME client needs to
845  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
846  error = acmeClientSendNewNonceRequest(context);
847 
848  //Check status code
849  if(!error)
850  {
851  //Update ACME client state
852  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
853  }
854  }
855  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
856  {
857  //If a client wishes to find the URL for an existing account, then
858  //it should do so by sending a POST request to the newAccount URL
859  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
860  //section 7.3.1)
861  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
862 
863  //Check status code
864  if(!error)
865  {
866  //Update ACME client state
867  context->state = ACME_CLIENT_STATE_DEACTIVATE_ACCOUNT;
868  }
869  }
870  else if(context->state == ACME_CLIENT_STATE_DEACTIVATE_ACCOUNT)
871  {
872  //Initialize account parameters
873  osMemset(&params, 0, sizeof(AcmeAccountParams));
874 
875  //A client can deactivate an account by posting a signed update to the
876  //account URL with a status field of "deactivated" (refer to RFC 8555,
877  //section 7.3.1)
878  params.status = "deactivated";
879 
880  //Send the POST request to the account URL
881  error = acmeClientSendUpdateAccountRequest(context, &params);
882 
883  //Check status code
884  if(!error)
885  {
886  //Update ACME client state
887  context->state = ACME_CLIENT_STATE_CONNECTED;
888  break;
889  }
890  }
891  else
892  {
893  //Invalid state
894  error = ERROR_WRONG_STATE;
895  }
896  }
897 
898  //Unexpected HTTP response?
899  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
900  error == ERROR_RESPONSE_TOO_LARGE)
901  {
902  //Revert to default state
903  context->state = ACME_CLIENT_STATE_CONNECTED;
904  }
905 
906  //Return status code
907  return error;
908 }
909 
910 
911 /**
912  * @brief Begin the certificate issuance process
913  * @param[in] context Pointer to the ACME client context
914  * @param[in] params Certificate order information
915  * @return Error code
916  **/
917 
919  const AcmeOrderParams *params)
920 {
921  error_t error;
922 
923  //Check parameters
924  if(context == NULL || params == NULL)
925  {
927  }
928 
929  //Initialize variables
930  error = NO_ERROR;
931 
932  //Execute the sequence of HTTP requests
933  while(!error)
934  {
935  //Check ACME client state
936  if(context->state == ACME_CLIENT_STATE_CONNECTED)
937  {
938  //Check certificate order information
939  error = acmeClientCheckOrderParams(params);
940 
941  //Check status code
942  if(!error)
943  {
944  //Initialize order object
945  error = acmeClientInitOrder(context, params);
946  }
947 
948  //Check status code
949  if(!error)
950  {
951  //In order to help clients configure themselves with the right URLs
952  //for each ACME operation, ACME servers provide a directory object
953  //(refer to RFC 8555, section 7.1.1)
954  context->state = ACME_CLIENT_STATE_DIRECTORY;
955  }
956  }
957  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
958  {
959  //If the directory object is no longer valid, the client must access
960  //the directory again by sending a GET request to the directory URL
961  error = acmeClientSendDirectoryRequest(context);
962 
963  //Check status code
964  if(!error)
965  {
966  //Update ACME client state
967  context->state = ACME_CLIENT_STATE_NEW_NONCE;
968  }
969  }
970  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
971  {
972  //Before sending a POST request to the server, an ACME client needs to
973  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
974  error = acmeClientSendNewNonceRequest(context);
975 
976  //Check status code
977  if(!error)
978  {
979  //Update ACME client state
980  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
981  }
982  }
983  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
984  {
985  //If a client wishes to find the URL for an existing account, then
986  //it should do so by sending a POST request to the newAccount URL
987  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
988  //section 7.3.1)
989  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
990 
991  //Check status code
992  if(!error)
993  {
994  //Update ACME client state
995  context->state = ACME_CLIENT_STATE_NEW_ORDER;
996  }
997  }
998  else if(context->state == ACME_CLIENT_STATE_NEW_ORDER)
999  {
1000  //The client begins the certificate issuance process by sending a
1001  //POST request to the server's newOrder resource (refer to RFC 8555,
1002  //section 7.4)
1003  error = acmeClientSendNewOrderRequest(context, params);
1004 
1005  //Check status code
1006  if(!error)
1007  {
1008  //Point to the first authorization
1009  context->index = 0;
1010  //Update ACME client state
1011  context->state = ACME_CLIENT_STATE_AUTHORIZATION;
1012  }
1013  }
1014  else if(context->state == ACME_CLIENT_STATE_AUTHORIZATION)
1015  {
1016  //Loop through the authorizations
1017  if(context->index < context->numAuthorizations)
1018  {
1019  AcmeAuthorization *authorization;
1020 
1021  //Point to the current authorization
1022  authorization = &context->authorizations[context->index];
1023 
1024  //When a client receives an order from the server in reply to a
1025  //newOrder request, it downloads the authorization resources by
1026  //sending POST-as-GET requests to the indicated URLs (refer to
1027  //RFC 8555, section 7.5)
1028  error = acmeClientSendAuthorizationRequest(context, authorization);
1029 
1030  //Check status code
1031  if(!error)
1032  {
1033  //Point the next authorization
1034  context->index++;
1035  }
1036  }
1037  else
1038  {
1039  //All the authorizations have been downloaded
1040  context->state = ACME_CLIENT_STATE_CONNECTED;
1041  break;
1042  }
1043  }
1044  else
1045  {
1046  //Invalid state
1047  error = ERROR_WRONG_STATE;
1048  }
1049  }
1050 
1051  //Unexpected HTTP response?
1052  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
1053  error == ERROR_RESPONSE_TOO_LARGE)
1054  {
1055  //Revert to default state
1056  context->state = ACME_CLIENT_STATE_CONNECTED;
1057  }
1058 
1059  //Return status code
1060  return error;
1061 }
1062 
1063 
1064 /**
1065  * @brief Get the key authorization that matches a given token (HTTP challenge)
1066  * @param[in] context Pointer to the ACME client context
1067  * @param[in] token NULL-terminated string that contains the token
1068  * @return The function returns a NULL-terminated string that contains the key
1069  * authorization if the token is valid. Else, the NULL pointer is returned
1070  **/
1071 
1073  const char_t *token)
1074 {
1075  const char_t *keyAuth;
1076 
1077  //Default value
1078  keyAuth = NULL;
1079 
1080 #if (ACME_CLIENT_HTTP_CHALLENGE_SUPPORT == ENABLED)
1081  //Check parameters
1082  if(context != NULL && token != NULL)
1083  {
1084  uint_t i;
1085 
1086  //Loop through the challenges
1087  for(i = 0; i < context->numChallenges; i++)
1088  {
1089  //Check the status of the challenge
1090  if(context->challenges[i].status == ACME_CHALLENGE_STATUS_PENDING ||
1091  context->challenges[i].status == ACME_CHALLENGE_STATUS_PROCESSING)
1092  {
1093  //HTTP validation method?
1094  if(context->challenges[i].type == ACME_CHALLENGE_TYPE_HTTP_01)
1095  {
1096  //Compare token values
1097  if(osStrcmp(context->challenges[i].token, token) == 0)
1098  {
1099  //Point to the key authorization
1100  keyAuth = context->challenges[i].keyAuth;
1101  break;
1102  }
1103  }
1104  }
1105  }
1106  }
1107 #endif
1108 
1109  //Return the ASCII representation of the key authorization
1110  return keyAuth;
1111 }
1112 
1113 
1114 /**
1115  * @brief Get the key authorization digest that matches a given identifier (DNS challenge)
1116  * @param[in] context Pointer to the ACME client context
1117  * @param[in] identifier NULL-terminated string that contains the domain name
1118  * @return The function returns a NULL-terminated string that contains the
1119  * Base64url-encoded digest of the key authorization if the identifier is
1120  * valid. Else, the NULL pointer is returned
1121  **/
1122 
1124  const char_t *identifier)
1125 {
1126  const char_t *keyAuth;
1127 
1128  //Default value
1129  keyAuth = NULL;
1130 
1131 #if (ACME_CLIENT_DNS_CHALLENGE_SUPPORT == ENABLED)
1132  //Check parameters
1133  if(context != NULL && identifier != NULL)
1134  {
1135  uint_t i;
1136 
1137  //Loop through the challenges
1138  for(i = 0; i < context->numChallenges; i++)
1139  {
1140  //Check the status of the challenge
1141  if(context->challenges[i].status == ACME_CHALLENGE_STATUS_PENDING ||
1142  context->challenges[i].status == ACME_CHALLENGE_STATUS_PROCESSING)
1143  {
1144  //DNS validation method?
1145  if(context->challenges[i].type == ACME_CHALLENGE_TYPE_DNS_01)
1146  {
1147  //Any identifier of type "dns" may have a wildcard domain name as
1148  //its value
1149  if(context->challenges[i].wildcard)
1150  {
1151  //A wildcard domain name consists of a single asterisk character
1152  //followed by a single full stop character ("*.") followed by a
1153  //domain name
1154  if(osStrncmp(identifier, "*.", 2) == 0 &&
1155  osStrcmp(context->challenges[i].identifier, identifier + 2) == 0)
1156  {
1157  //Point to the key authorization digest
1158  keyAuth = context->challenges[i].keyAuth;
1159  break;
1160  }
1161  }
1162  else
1163  {
1164  //Compare identifier values
1165  if(osStrcmp(context->challenges[i].identifier, identifier) == 0)
1166  {
1167  //Point to the key authorization digest
1168  keyAuth = context->challenges[i].keyAuth;
1169  break;
1170  }
1171  }
1172  }
1173  }
1174  }
1175  }
1176 #endif
1177 
1178  //Return the Base64url representation of the key authorization digest
1179  return keyAuth;
1180 }
1181 
1182 
1183 /**
1184  * @brief Get the self-certificate that matches a given identifier (TLS-ALPN challenge)
1185  * @param[in] context Pointer to the ACME client context
1186  * @param[in] identifier NULL-terminated string that contains the domain name
1187  * @return The function returns a NULL-terminated string that contains the
1188  * TLS-ALPN certificate if the identifier is valid. Else, the NULL pointer
1189  * is returned
1190  **/
1191 
1193  const char_t *identifier)
1194 {
1195  const char_t *cert;
1196 
1197  //Default value
1198  cert = NULL;
1199 
1200 #if (ACME_CLIENT_TLS_ALPN_CHALLENGE_SUPPORT == ENABLED)
1201  //Check parameters
1202  if(context != NULL && identifier != NULL)
1203  {
1204  uint_t i;
1205 
1206  //Loop through the challenges
1207  for(i = 0; i < context->numChallenges; i++)
1208  {
1209  //Check the status of the challenge
1210  if(context->challenges[i].status == ACME_CHALLENGE_STATUS_PENDING ||
1211  context->challenges[i].status == ACME_CHALLENGE_STATUS_PROCESSING)
1212  {
1213  //TLS with ALPN validation method?
1214  if(context->challenges[i].type == ACME_CHALLENGE_TYPE_TLS_ALPN_01)
1215  {
1216  //Compare identifier values
1217  if(osStrcmp(context->challenges[i].identifier, identifier) == 0)
1218  {
1219  //Point to the self-signed certificate
1220  cert = context->challenges[i].cert;
1221  break;
1222  }
1223  }
1224  }
1225  }
1226  }
1227 #endif
1228 
1229  //Return the TLS-ALPN certificate
1230  return cert;
1231 }
1232 
1233 
1234 /**
1235  * @brief Poll for order status
1236  * @param[in] context Pointer to the ACME client context
1237  * @param[out] orderStatus Order status
1238  * @return Error code
1239  **/
1240 
1242  AcmeOrderStatus *orderStatus)
1243 {
1244  error_t error;
1245 
1246  //Check parameters
1247  if(context == NULL || orderStatus == NULL)
1248  {
1249  return ERROR_INVALID_PARAMETER;
1250  }
1251 
1252  //Initialize variables
1253  error = NO_ERROR;
1254 
1255  //Execute the sequence of HTTP requests
1256  while(!error)
1257  {
1258  //Check ACME client state
1259  if(context->state == ACME_CLIENT_STATE_CONNECTED)
1260  {
1261  //Check the order of the order
1262  if(context->order.status == ACME_ORDER_STATUS_PENDING ||
1263  context->order.status == ACME_ORDER_STATUS_READY ||
1264  context->order.status == ACME_ORDER_STATUS_PROCESSING)
1265  {
1266  //In order to help clients configure themselves with the right URLs
1267  //for each ACME operation, ACME servers provide a directory object
1268  //(refer to RFC 8555, section 7.1.1)
1269  context->state = ACME_CLIENT_STATE_DIRECTORY;
1270  }
1271  else if(context->order.status == ACME_ORDER_STATUS_VALID ||
1272  context->order.status == ACME_ORDER_STATUS_INVALID)
1273  {
1274  //Exit immediately
1275  break;
1276  }
1277  else
1278  {
1279  //Report an error
1280  error = ERROR_WRONG_STATE;
1281  }
1282  }
1283  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
1284  {
1285  //If the directory object is no longer valid, the client must access
1286  //the directory again by sending a GET request to the directory URL
1287  error = acmeClientSendDirectoryRequest(context);
1288 
1289  //Check status code
1290  if(!error)
1291  {
1292  //Update ACME client state
1293  context->state = ACME_CLIENT_STATE_NEW_NONCE;
1294  }
1295  }
1296  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
1297  {
1298  //Before sending a POST request to the server, an ACME client needs to
1299  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
1300  error = acmeClientSendNewNonceRequest(context);
1301 
1302  //Check status code
1303  if(!error)
1304  {
1305  //Update ACME client state
1306  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
1307  }
1308  }
1309  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
1310  {
1311  //If a client wishes to find the URL for an existing account, then
1312  //it should do so by sending a POST request to the newAccount URL
1313  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
1314  //section 7.3.1)
1315  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
1316 
1317  //Check status code
1318  if(!error)
1319  {
1320  //Clients should check the status of the order to determine whether
1321  //they need to take any action (refer to RFC 8555, section 7.1.3)
1322  if(context->order.status == ACME_ORDER_STATUS_PENDING)
1323  {
1324  //Point to the first challenge
1325  context->index = 0;
1326 
1327  //The client indicate to the server that it is ready for the
1328  //challenge validation
1329  context->state = ACME_CLIENT_STATE_CHALLENGE_READY;
1330  }
1331  else if(context->order.status == ACME_ORDER_STATUS_READY)
1332  {
1333  //All the authorizations listed in the order object are in the
1334  //"valid" state
1335  context->state = ACME_CLIENT_STATE_FINALIZE;
1336  }
1337  else if(context->order.status == ACME_ORDER_STATUS_PROCESSING)
1338  {
1339  //The client has already submitted a request to the order's
1340  //"finalize" URL
1341  context->state = ACME_CLIENT_STATE_POLL_STATUS_2;
1342  }
1343  else
1344  {
1345  //Report an error
1346  error = ERROR_WRONG_STATE;
1347  }
1348  }
1349  }
1350  else if(context->state == ACME_CLIENT_STATE_CHALLENGE_READY)
1351  {
1352  //Loop through the authorizations
1353  if(context->index < context->numChallenges)
1354  {
1356 
1357  //Point to the current challenge
1358  challenge = &context->challenges[context->index];
1359 
1360  //Check the status of the challenge
1362  {
1363  //The client indicates to the server that it is ready for the
1364  //challenge validation by sending an empty JSON body carried in
1365  //a POST request to the challenge URL (refer to RFC 8555,
1366  //section 7.5.1)
1368 
1369  //Check status code
1370  if(!error)
1371  {
1372  //The challenge transitions to the "processing" state when
1373  //the client responds to the challenge
1375  }
1376  }
1377 
1378  //Check status code
1379  if(!error)
1380  {
1381  //Point the next challenge
1382  context->index++;
1383  }
1384  }
1385  else
1386  {
1387  //Update ACME client state
1388  context->state = ACME_CLIENT_STATE_POLL_STATUS_1;
1389  }
1390  }
1391  else if(context->state == ACME_CLIENT_STATE_POLL_STATUS_1)
1392  {
1393  //The client should then send a POST-as-GET request to the order
1394  //resource to obtain its current state refer to RFC 8555, section 7.4)
1395  error = acmeClientSendOrderStatusRequest(context);
1396 
1397  //Check status code
1398  if(!error)
1399  {
1400  //Check the status of the order
1401  if(context->order.status == ACME_ORDER_STATUS_PENDING ||
1402  context->order.status == ACME_ORDER_STATUS_INVALID)
1403  {
1404  //Update ACME client state
1405  context->state = ACME_CLIENT_STATE_CONNECTED;
1406  break;
1407  }
1408  else if(context->order.status == ACME_ORDER_STATUS_READY)
1409  {
1410  //Once all of the authorizations listed in the order object are
1411  //in the "valid" state, the order transitions to the "ready" state
1412  context->state = ACME_CLIENT_STATE_FINALIZE;
1413  }
1414  else
1415  {
1416  //Report an error
1417  error = ERROR_WRONG_STATE;
1418  break;
1419  }
1420  }
1421  }
1422  else if(context->state == ACME_CLIENT_STATE_FINALIZE)
1423  {
1424  //Once the client believes it has fulfilled the server's requirements,
1425  //it should send a POST request to the order resource's finalize URL.
1426  //The POST body MUST include a CSR (refer to RFC 8555, section 7.4)
1427  error = acmeClientSendFinalizeOrderRequest(context);
1428 
1429  //Check status code
1430  if(!error)
1431  {
1432  //The order moves to the "processing" state after the client submits
1433  //a request to the order's finalize URL
1434  context->order.status = ACME_ORDER_STATUS_PROCESSING;
1435 
1436  //Update ACME client state
1437  context->state = ACME_CLIENT_STATE_POLL_STATUS_2;
1438  }
1439  }
1440  else if(context->state == ACME_CLIENT_STATE_POLL_STATUS_2)
1441  {
1442  //The client should then send a POST-as-GET request to the order
1443  //resource to obtain its current state refer to RFC 8555, section 7.4)
1444  error = acmeClientSendOrderStatusRequest(context);
1445 
1446  //Check status code
1447  if(!error)
1448  {
1449  //Check the status of the order
1450  if(context->order.status != ACME_ORDER_STATUS_PROCESSING &&
1451  context->order.status != ACME_ORDER_STATUS_VALID &&
1452  context->order.status != ACME_ORDER_STATUS_INVALID)
1453  {
1454  //Report an error
1455  error = ERROR_WRONG_STATE;
1456  }
1457 
1458  //Update ACME client state
1459  context->state = ACME_CLIENT_STATE_CONNECTED;
1460  break;
1461  }
1462  }
1463  else
1464  {
1465  //Invalid state
1466  error = ERROR_WRONG_STATE;
1467  }
1468  }
1469 
1470  //Always return the actual status of the order
1471  *orderStatus = context->order.status;
1472 
1473  //Unexpected HTTP response?
1474  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
1475  error == ERROR_RESPONSE_TOO_LARGE)
1476  {
1477  //Revert to default state
1478  context->state = ACME_CLIENT_STATE_CONNECTED;
1479  }
1480 
1481  //Return status code
1482  return error;
1483 }
1484 
1485 
1486 /**
1487  * @brief Download the certificate
1488  * @param[in] context Pointer to the ACME client context
1489  * @param[out] buffer Pointer to the buffer where to store the certificate
1490  * chain (optional parameter)
1491  * @param[in] size Size of the buffer, in bytes
1492  * @param[out] length Actual length of the certificate chain, in bytes
1493  * @return Error code
1494  **/
1495 
1497  char_t *buffer, size_t size, size_t *length)
1498 {
1499  error_t error;
1500 
1501  //Check parameters
1502  if(context == NULL || length == NULL)
1503  {
1504  return ERROR_INVALID_PARAMETER;
1505  }
1506 
1507  //Initialize variables
1508  error = NO_ERROR;
1509 
1510  //Execute the sequence of HTTP requests
1511  while(!error)
1512  {
1513  //Check ACME client state
1514  if(context->state == ACME_CLIENT_STATE_CONNECTED)
1515  {
1516  //Make sure the certificate has been issued by the ACME server
1517  if(context->order.status == ACME_ORDER_STATUS_VALID)
1518  {
1519  //In order to help clients configure themselves with the right URLs
1520  //for each ACME operation, ACME servers provide a directory object
1521  //(refer to RFC 8555, section 7.1.1)
1522  context->state = ACME_CLIENT_STATE_DIRECTORY;
1523  }
1524  else
1525  {
1526  //Report an error
1527  error = ERROR_WRONG_STATE;
1528  }
1529  }
1530  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
1531  {
1532  //If the directory object is no longer valid, the client must access
1533  //the directory again by sending a GET request to the directory URL
1534  error = acmeClientSendDirectoryRequest(context);
1535 
1536  //Check status code
1537  if(!error)
1538  {
1539  //Update ACME client state
1540  context->state = ACME_CLIENT_STATE_NEW_NONCE;
1541  }
1542  }
1543  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
1544  {
1545  //Before sending a POST request to the server, an ACME client needs to
1546  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
1547  error = acmeClientSendNewNonceRequest(context);
1548 
1549  //Check status code
1550  if(!error)
1551  {
1552  //Update ACME client state
1553  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
1554  }
1555  }
1556  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
1557  {
1558  //If a client wishes to find the URL for an existing account, then
1559  //it should do so by sending a POST request to the newAccount URL
1560  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
1561  //section 7.3.1)
1562  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
1563 
1564  //Check status code
1565  if(!error)
1566  {
1567  //Update ACME client state
1568  context->state = ACME_CLIENT_STATE_DOWNLOAD_CERT;
1569  }
1570  }
1571  else if(context->state == ACME_CLIENT_STATE_DOWNLOAD_CERT)
1572  {
1573  //To download the issued certificate, the client simply sends a
1574  //POST-as-GET request to the certificate URL (refer to RFC 8555,
1575  //section 7.4.2)
1576  error = acmeClientSendDownloadCertRequest(context, buffer, size,
1577  length);
1578 
1579  //Check status code
1580  if(!error)
1581  {
1582  //Update ACME client state
1583  context->state = ACME_CLIENT_STATE_CONNECTED;
1584  break;
1585  }
1586  }
1587  else
1588  {
1589  //Invalid state
1590  error = ERROR_WRONG_STATE;
1591  }
1592  }
1593 
1594  //Unexpected HTTP response?
1595  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
1596  error == ERROR_RESPONSE_TOO_LARGE)
1597  {
1598  //Revert to default state
1599  context->state = ACME_CLIENT_STATE_CONNECTED;
1600  }
1601 
1602  //Return status code
1603  return error;
1604 }
1605 
1606 
1607 /**
1608  * @brief Certificate revocation
1609  * @param[in] context Pointer to the ACME client context
1610  * @param[in] cert Certificate to be revoked (PEM format)
1611  * @param[in] certLen Length of the certificate, in bytes
1612  * @param[in] privateKey Private key associated with the certificate (PEM
1613  * format). This parameter is required if the certificate key, rather than
1614  * the account key, is to be used to sign the revocation request
1615  * @param[in] privateKeyLen Length of the private key
1616  * @param[in] password NULL-terminated string containing the password. This
1617  * parameter is required if the private key is encrypted
1618  * @param[in] reason Revocation reason code
1619  * @return Error code
1620  **/
1621 
1623  const char_t *cert, size_t certLen, const char_t *privateKey,
1624  size_t privateKeyLen, const char_t *password, AcmeReasonCode reason)
1625 {
1626  error_t error;
1627 
1628  //Check parameters
1629  if(context == NULL || cert == NULL || certLen == 0)
1630  {
1631  return ERROR_INVALID_PARAMETER;
1632  }
1633 
1634  //Initialize variables
1635  error = NO_ERROR;
1636 
1637  //Execute the sequence of HTTP requests
1638  while(!error)
1639  {
1640  //Check ACME client state
1641  if(context->state == ACME_CLIENT_STATE_CONNECTED)
1642  {
1643  //In order to help clients configure themselves with the right URLs for
1644  //each ACME operation, ACME servers provide a directory object (refer
1645  //to RFC 8555, section 7.1.1)
1646  context->state = ACME_CLIENT_STATE_DIRECTORY;
1647  }
1648  else if(context->state == ACME_CLIENT_STATE_DIRECTORY)
1649  {
1650  //If the directory object is no longer valid, the client must access
1651  //the directory again by sending a GET request to the directory URL
1652  error = acmeClientSendDirectoryRequest(context);
1653 
1654  //Check status code
1655  if(!error)
1656  {
1657  //Update ACME client state
1658  context->state = ACME_CLIENT_STATE_NEW_NONCE;
1659  }
1660  }
1661  else if(context->state == ACME_CLIENT_STATE_NEW_NONCE)
1662  {
1663  //Before sending a POST request to the server, an ACME client needs to
1664  //have a fresh anti-replay nonce (refer to RFC 8555, section 7.2)
1665  error = acmeClientSendNewNonceRequest(context);
1666 
1667  //Check status code
1668  if(!error)
1669  {
1670  //Revocation requests are different from other ACME requests in
1671  //that they can be signed with either an account key pair or the
1672  //key pair in the certificate (refer to RFC 8555, section 7.6)
1673  if(privateKey != NULL && privateKeyLen > 0)
1674  {
1675  //Use the certificate key pair
1676  context->state = ACME_CLIENT_STATE_REVOKE_CERT;
1677  }
1678  else if(context->accountKey.type != X509_KEY_TYPE_UNKNOWN)
1679  {
1680  //Use the account key pair
1681  context->state = ACME_CLIENT_STATE_NEW_ACCOUNT;
1682  }
1683  else
1684  {
1685  //Report an error
1686  error = ERROR_INVALID_KEY;
1687  }
1688  }
1689  }
1690  else if(context->state == ACME_CLIENT_STATE_NEW_ACCOUNT)
1691  {
1692  //If a client wishes to find the URL for an existing account, then
1693  //it should do so by sending a POST request to the newAccount URL
1694  //with an "onlyReturnExisting" field set to "true" (refer to RFC 8555,
1695  //section 7.3.1)
1696  error = acmeClientSendNewAccountRequest(context, NULL, TRUE);
1697 
1698  //Check status code
1699  if(!error)
1700  {
1701  //Update ACME client state
1702  context->state = ACME_CLIENT_STATE_REVOKE_CERT;
1703  }
1704  }
1705  else if(context->state == ACME_CLIENT_STATE_REVOKE_CERT)
1706  {
1707  //To request that a certificate be revoked, the client sends a POST
1708  //request to the ACME server's revokeCert URL (refer to RFC 8555,
1709  //section 7.6)
1710  error = acmeClientSendRevokeCertRequest(context, cert, certLen,
1711  privateKey, privateKeyLen, password, reason);
1712 
1713  //Check status code
1714  if(!error)
1715  {
1716  //Update ACME client state
1717  context->state = ACME_CLIENT_STATE_CONNECTED;
1718  break;
1719  }
1720  }
1721  else
1722  {
1723  //Invalid state
1724  error = ERROR_WRONG_STATE;
1725  }
1726  }
1727 
1728  //Unexpected HTTP response?
1729  if(error == ERROR_UNEXPECTED_STATUS || error == ERROR_INVALID_RESPONSE ||
1730  error == ERROR_RESPONSE_TOO_LARGE)
1731  {
1732  //Revert to default state
1733  context->state = ACME_CLIENT_STATE_CONNECTED;
1734  }
1735 
1736  //Return status code
1737  return error;
1738 }
1739 
1740 
1741 /**
1742  * @brief Gracefully disconnect from the ACME server
1743  * @param[in] context Pointer to the ACME client context
1744  * @return Error code
1745  **/
1746 
1748 {
1749  error_t error;
1750 
1751  //Make sure the ACME client context is valid
1752  if(context == NULL)
1753  {
1754  return ERROR_INVALID_PARAMETER;
1755  }
1756 
1757  //Initialize status code
1758  error = NO_ERROR;
1759 
1760  //Gracefully disconnect from the ACME server
1761  while(!error)
1762  {
1763  //Check ACME client state
1764  if(context->state == ACME_CLIENT_STATE_CONNECTED)
1765  {
1766  //Gracefully shutdown HTTPS connection
1767  context->state = ACME_CLIENT_STATE_DISCONNECTING;
1768  }
1769  else if(context->state == ACME_CLIENT_STATE_DISCONNECTING)
1770  {
1771  //Gracefully shutdown HTTPS connection
1772  error = httpClientDisconnect(&context->httpClientContext);
1773 
1774  //Check status code
1775  if(error == NO_ERROR)
1776  {
1777  //Close HTTPS connection
1778  httpClientClose(&context->httpClientContext);
1779  //Update ACME client state
1780  context->state = ACME_CLIENT_STATE_DISCONNECTED;
1781  }
1782  }
1783  else if(context->state == ACME_CLIENT_STATE_DISCONNECTED)
1784  {
1785  //The client is disconnected from the ACME server
1786  break;
1787  }
1788  else
1789  {
1790  //Invalid state
1791  error = ERROR_WRONG_STATE;
1792  }
1793  }
1794 
1795  //Failed to gracefully disconnect from the ACME server?
1796  if(error != NO_ERROR && error != ERROR_WOULD_BLOCK)
1797  {
1798  //Close HTTPS connection
1799  httpClientClose(&context->httpClientContext);
1800  //Update ACME client state
1801  context->state = ACME_CLIENT_STATE_DISCONNECTED;
1802  }
1803 
1804  //Return status code
1805  return error;
1806 }
1807 
1808 
1809 /**
1810  * @brief Close the connection with the ACME server
1811  * @param[in] context Pointer to the ACME client context
1812  * @return Error code
1813  **/
1814 
1816 {
1817  //Make sure the ACME client context is valid
1818  if(context == NULL)
1819  {
1820  return ERROR_INVALID_PARAMETER;
1821  }
1822 
1823  //Close HTTPS connection
1824  httpClientClose(&context->httpClientContext);
1825  //Update ACME client state
1826  context->state = ACME_CLIENT_STATE_DISCONNECTED;
1827 
1828  //Successful processing
1829  return NO_ERROR;
1830 }
1831 
1832 
1833 /**
1834  * @brief Release ACME client context
1835  * @param[in] context Pointer to the ACME client context
1836  **/
1837 
1839 {
1840  //Make sure the ACME client context is valid
1841  if(context != NULL)
1842  {
1843  //Release HTTP client context
1844  httpClientDeinit(&context->httpClientContext);
1845 
1846  //Release keys
1847  acmeClientUnloadKeyPair(&context->accountKey);
1848  acmeClientUnloadKeyPair(&context->certKey);
1849 
1850  //Clear ACME client context
1851  osMemset(context, 0, sizeof(AcmeClientContext));
1852  }
1853 }
1854 
1855 #endif
error_t acmeClientSendNewOrderRequest(AcmeClientContext *context, const AcmeOrderParams *params)
Send HTTP request (newOrder URL)
error_t acmeClientSendDownloadCertRequest(AcmeClientContext *context, char_t *buffer, size_t size, size_t *length)
Send HTTP request (certificate URL)
error_t acmeClientSetHost(AcmeClientContext *context, const char_t *host)
Set the domain name of the ACME server.
Definition: acme_client.c:213
error_t httpClientDisconnect(HttpClientContext *context)
Gracefully disconnect from the HTTP server.
Definition: http_client.c:2348
@ ACME_ORDER_STATUS_VALID
Definition: acme_client.h:339
error_t acmeClientInitOrder(AcmeClientContext *context, const AcmeOrderParams *params)
Initialize order object.
error_t httpClientBindToInterface(HttpClientContext *context, NetInterface *interface)
Bind the HTTP client to a particular network interface.
Definition: http_client.c:322
@ ERROR_WOULD_BLOCK
Definition: error.h:96
error_t acmeClientRegisterTlsInitCallback(AcmeClientContext *context, AcmeClientTlsInitCallback callback)
Register TLS initialization callback function.
Definition: acme_client.c:115
@ ACME_CHALLENGE_STATUS_PENDING
Definition: acme_client.h:367
IP network address.
Definition: ip.h:97
error_t acmeClientLoadKeyPair(AcmeKeyPair *keyPair, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load public/private key pair.
#define PrngAlgo
Definition: crypto.h:1161
void acmeClientDeinit(AcmeClientContext *context)
Release ACME client context.
Definition: acme_client.c:1838
#define TRUE
Definition: os_port.h:50
Helper functions for ACME client.
const char_t * password
Password (required if the private key is encrypted)
Definition: acme_client.h:461
error_t acmeClientSendNewNonceRequest(AcmeClientContext *context)
Send HTTP request (newNonce URL)
AcmeReasonCode
Revocation reason codes.
Definition: acme_client.h:392
error_t acmeClientBindToInterface(AcmeClientContext *context, NetInterface *interface)
Bind the ACME client to a particular network interface.
Definition: acme_client.c:272
@ ACME_CLIENT_STATE_CONNECTING
Definition: acme_client.h:277
error_t acmeClientSetAccountKey(AcmeClientContext *context, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Load account key pair.
Definition: acme_client.c:426
Challenge object.
Definition: acme_client.h:561
const char_t * acmeClientGetHttpKeyAuthorization(AcmeClientContext *context, const char_t *token)
Get the key authorization that matches a given token (HTTP challenge)
Definition: acme_client.c:1072
const char_t * publicKey
Account public key.
Definition: acme_client.h:457
Anti-replay nonce management.
Certificate order parameters.
Definition: acme_client.h:482
Account object management.
error_t acmeClientSendDirectoryRequest(AcmeClientContext *context)
Send HTTP request (directory URL)
@ ACME_CHALLENGE_TYPE_DNS_01
Definition: acme_client.h:382
#define osStrcmp(s1, s2)
Definition: os_port.h:177
uint8_t challenge[16]
error_t acmeClientSetDirectoryUri(AcmeClientContext *context, const char_t *directoryUri)
Set the URI of the directory object.
Definition: acme_client.c:242
#define osStrlen(s)
Definition: os_port.h:171
Directory object.
Definition: acme_client.h:500
error_t acmeClientInit(AcmeClientContext *context)
Initialize ACME client context.
Definition: acme_client.c:69
#define ACME_CLIENT_MAX_NAME_LEN
Definition: acme_client.h:173
@ ACME_CLIENT_STATE_AUTHORIZATION
Definition: acme_client.h:286
Challenge object management.
@ ACME_CLIENT_STATE_CONNECTED
Definition: acme_client.h:278
Account object.
Definition: acme_client.h:514
error_t httpClientSetVersion(HttpClientContext *context, HttpVersion version)
Set the HTTP protocol version to be used.
Definition: http_client.c:182
#define ACME_CLIENT_MAX_URI_LEN
Definition: acme_client.h:180
error_t acmeClientCreateAccount(AcmeClientContext *context, const AcmeAccountParams *params)
Account creation.
Definition: acme_client.c:453
ACME account creation parameters.
Definition: acme_client.h:453
@ ERROR_WRONG_STATE
Definition: error.h:211
@ ACME_CLIENT_STATE_NEW_ACCOUNT
Definition: acme_client.h:281
void httpClientDeinit(HttpClientContext *context)
Release HTTP client context.
Definition: http_client.c:2448
#define ACME_CLIENT_DEFAULT_TIMEOUT
Definition: acme_client.h:145
error_t acmeClientSendKeyChangeRequest(AcmeClientContext *context, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Send HTTP request (keyChange URL)
error_t(* AcmeClientCsrGenCallback)(AcmeClientContext *context, uint8_t *buffer, size_t size, size_t *length)
CSR generation callback function.
Definition: acme_client.h:418
@ ACME_CHALLENGE_STATUS_PROCESSING
Definition: acme_client.h:368
@ ACME_CLIENT_STATE_NEW_ORDER
Definition: acme_client.h:285
#define FALSE
Definition: os_port.h:46
@ ACME_CLIENT_STATE_FINALIZE
Definition: acme_client.h:289
#define ACME_CLIENT_MAX_NONCE_LEN
Definition: acme_client.h:201
Authorization object management.
@ ERROR_INVALID_PARAMETER
Invalid parameter.
Definition: error.h:47
@ ERROR_UNEXPECTED_STATUS
Definition: error.h:285
AcmeOrderStatus
Order status.
Definition: acme_client.h:334
error_t
Error codes.
Definition: error.h:43
Certificate management.
error_t acmeClientSendFinalizeOrderRequest(AcmeClientContext *context)
Send HTTP request (order's finalize URL)
Order object management.
error_t acmeClientRegisterCsrGenCallback(AcmeClientContext *context, AcmeClientCsrGenCallback callback)
Register CSR generation callback function.
Definition: acme_client.c:139
error_t httpClientRegisterTlsInitCallback(HttpClientContext *context, HttpClientTlsInitCallback callback, void *param)
Register TLS initialization callback function.
Definition: http_client.c:125
const char_t * acmeClientGetTlsAlpnCertificate(AcmeClientContext *context, const char_t *identifier)
Get the self-certificate that matches a given identifier (TLS-ALPN challenge)
Definition: acme_client.c:1192
@ ERROR_RESPONSE_TOO_LARGE
Definition: error.h:286
@ ACME_CHALLENGE_TYPE_HTTP_01
Definition: acme_client.h:381
error_t acmeClientSetTimeout(AcmeClientContext *context, systime_t timeout)
Set communication timeout.
Definition: acme_client.c:190
error_t acmeClientUpdateAccount(AcmeClientContext *context, const AcmeAccountParams *params)
Account information update.
Definition: acme_client.c:567
error_t acmeClientClose(AcmeClientContext *context)
Close the connection with the ACME server.
Definition: acme_client.c:1815
#define NetInterface
Definition: net.h:40
Directory object management.
error_t httpClientSetTimeout(HttpClientContext *context, systime_t timeout)
Set communication timeout.
Definition: http_client.c:211
error_t httpClientClose(HttpClientContext *context)
Close the connection with the HTTP server.
Definition: http_client.c:2425
@ ERROR_INVALID_LENGTH
Definition: error.h:111
NetContext * netGetDefaultContext(void)
Get default TCP/IP stack context.
Definition: net.c:546
error_t acmeClientSendAuthorizationRequest(AcmeClientContext *context, AcmeAuthorization *authorization)
Send HTTP request (authorization URL)
@ ACME_REQ_STATE_INIT
Definition: acme_client.h:303
@ ACME_CLIENT_STATE_CHALLENGE_READY
Definition: acme_client.h:287
uint8_t length
Definition: tcp.h:375
error_t httpClientInit(HttpClientContext *context)
Initialize HTTP client context.
Definition: http_client.c:66
error_t acmeClientSetPrng(AcmeClientContext *context, const PrngAlgo *prngAlgo, void *prngContext)
Set the pseudo-random number generator to be used.
Definition: acme_client.c:164
@ ACME_CLIENT_STATE_DISCONNECTED
Definition: acme_client.h:276
error_t acmeClientConnect(AcmeClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified ACME server.
Definition: acme_client.c:297
@ ACME_CLIENT_STATE_UPDATE_ACCOUNT
Definition: acme_client.h:282
Authorization object.
Definition: acme_client.h:549
const char_t * status
Status of the account.
Definition: acme_client.h:462
@ ACME_CLIENT_STATE_CHANGE_KEY
Definition: acme_client.h:283
@ ACME_CLIENT_STATE_REVOKE_CERT
Definition: acme_client.h:292
@ ACME_ORDER_STATUS_PROCESSING
Definition: acme_client.h:338
uint32_t systime_t
System time.
char char_t
Definition: compiler_port.h:55
#define AcmeClientContext
Definition: acme_client.h:262
error_t httpClientConnect(HttpClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish a connection with the specified HTTP server.
Definition: http_client.c:347
error_t acmeClientInitTlsContext(HttpClientContext *httpClientContext, TlsContext *tlsContext, void *param)
TLS initialization.
error_t acmeClientSendUpdateAccountRequest(AcmeClientContext *context, const AcmeAccountParams *params)
Send HTTP request (account URL)
size_t privateKeyLen
Length of the account private key, in bytes.
Definition: acme_client.h:460
@ HTTP_VERSION_1_1
Definition: http_common.h:63
@ ACME_CHALLENGE_TYPE_TLS_ALPN_01
Definition: acme_client.h:383
size_t publicKeyLen
Length of the account public key, in bytes.
Definition: acme_client.h:458
error_t acmeClientSendNewAccountRequest(AcmeClientContext *context, const AcmeAccountParams *params, bool_t onlyReturnExisting)
Send HTTP request (newAccount URL)
@ ACME_CLIENT_STATE_DISCONNECTING
Definition: acme_client.h:293
@ ACME_ORDER_STATUS_READY
Definition: acme_client.h:337
error_t acmeClientSendOrderStatusRequest(AcmeClientContext *context)
Send HTTP request (order URL)
error_t(* AcmeClientTlsInitCallback)(AcmeClientContext *context, TlsContext *tlsContext)
TLS initialization callback function.
Definition: acme_client.h:410
error_t acmeClientChangeAccountKey(AcmeClientContext *context, const char_t *publicKey, size_t publicKeyLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password)
Account key rollover.
Definition: acme_client.c:687
error_t acmeClientDownloadCertificate(AcmeClientContext *context, char_t *buffer, size_t size, size_t *length)
Download the certificate.
Definition: acme_client.c:1496
uint8_t identifier[]
const char_t * privateKey
Account private key.
Definition: acme_client.h:459
error_t acmeClientCreateOrder(AcmeClientContext *context, const AcmeOrderParams *params)
Begin the certificate issuance process.
Definition: acme_client.c:918
#define osStrncmp(s1, s2, length)
Definition: os_port.h:183
@ ACME_CLIENT_STATE_POLL_STATUS_1
Definition: acme_client.h:288
@ ACME_ORDER_STATUS_PENDING
Definition: acme_client.h:336
@ X509_KEY_TYPE_UNKNOWN
Definition: x509_common.h:711
@ ACME_CLIENT_STATE_DIRECTORY
Definition: acme_client.h:279
@ ACME_ORDER_STATUS_INVALID
Definition: acme_client.h:340
@ ACME_CLIENT_STATE_NEW_NONCE
Definition: acme_client.h:280
void acmeClientUnloadKeyPair(AcmeKeyPair *keyPair)
Unload public/private key pair.
unsigned int uint_t
Definition: compiler_port.h:57
#define osMemset(p, value, length)
Definition: os_port.h:141
error_t acmeClientCheckOrderParams(const AcmeOrderParams *params)
Check certificate order information.
error_t acmeClientPollOrderStatus(AcmeClientContext *context, AcmeOrderStatus *orderStatus)
Poll for order status.
Definition: acme_client.c:1241
#define osStrcpy(s1, s2)
Definition: os_port.h:213
const char_t * acmeClientGetDnsKeyAuthorization(AcmeClientContext *context, const char_t *identifier)
Get the key authorization digest that matches a given identifier (DNS challenge)
Definition: acme_client.c:1123
@ ERROR_INVALID_RESPONSE
Definition: error.h:71
ACME client (Automatic Certificate Management Environment)
error_t acmeClientSendChallengeReadyRequest(AcmeClientContext *context, AcmeChallenge *challenge)
Send HTTP request (challenge URL)
error_t acmeClientDeactivateAccount(AcmeClientContext *context)
ACME account deactivation.
Definition: acme_client.c:804
error_t acmeClientCheckAccountParams(const AcmeAccountParams *params)
Check account information.
@ ACME_CLIENT_STATE_DOWNLOAD_CERT
Definition: acme_client.h:291
@ ACME_CLIENT_STATE_POLL_STATUS_2
Definition: acme_client.h:290
error_t acmeClientDisconnect(AcmeClientContext *context)
Gracefully disconnect from the ACME server.
Definition: acme_client.c:1747
error_t acmeClientRevokeCertificate(AcmeClientContext *context, const char_t *cert, size_t certLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password, AcmeReasonCode reason)
Certificate revocation.
Definition: acme_client.c:1622
@ ERROR_INVALID_KEY
Definition: error.h:106
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint8_t token[]
Definition: coap_common.h:181
@ ACME_CLIENT_STATE_DEACTIVATE_ACCOUNT
Definition: acme_client.h:284
error_t acmeClientSendRevokeCertRequest(AcmeClientContext *context, const char_t *cert, size_t certLen, const char_t *privateKey, size_t privateKeyLen, const char_t *password, AcmeReasonCode reason)
Send HTTP request (revokeCert URL)