NetBurner 3.5.6
PDF Version
acmeRFC8555Servlet.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
50#ifndef _NB_ACME_H
51#define _NB_ACME_H
52
53#include <predef.h>
54#include <ip.h>
55#include <nettypes.h>
56
57
58#include <base64.h>
59#include <json_lexer.h>
60#include <nbtime.h>
61#include <nbstring.h>
62#include <diagnostics.h>
63#include <webclient/http_funcs.h>
64
65#include <crypto/wolfssl/wolfcrypt/ecc.h>
66#include <crypto/wolfssl/wolfcrypt/random.h>
67#include <crypto/wolfssl/wolfcrypt/sha256.h>
68#include <crypto/wolfssl/wolfcrypt/hash.h>
69#include <crypto/wolfssl/wolfcrypt/asn_public.h>
70#include <crypto/wolfssl/wolfcrypt/wolfmath.h>
71#include <crypto/certgen.h>
72#include <servlets.h>
73
74const int ACME_ERR_NONE=0;
75const int ACME_ERR_HAL_SAVE_FAIL=-10;
76const int ACME_ERR_CSR_ERR=-11;
77const int ACME_ERR_FAIL_READ_EXPIRY=-12;
78const int ACME_ERR_STORED_CERT_INVALID=-14;
79const int ACME_FAILED_GET_DIR=-15;
80const int ACME_FAILED_GET_NONCE=-16;
81const int ACME_FAILED_GET_NONCE_DIR=-17;
82const int ACME_FAILED_GET_ACCOUNT=-18;
83const int ACME_ERR_GET_CSR_FAIL=-19;
84const int ACME_ERR_ORDER_FAIL=-20;
85const int ACME_ERR_MALLOC_FAIL=-21;
86const int ACME_ERR_SELF_FAIL=-22;
87const int ACME_ERR_DNS_FAIL=-23;
88const int ACME_ERR_TRANSACT_FAIL=-24;
89
90
91
92//Forward declartions.
93class AcmeDataSet;
95
96
97//Class to hanle header processing as ACME needs both headers and body processed.
98class AcmeServletBuffer : public ParsedJsonDataSet
99{
100AcmeDataSet * pAcme;
101 virtual void ProcessHeader(const char * hdr);
102public:
103 AcmeServletBuffer(AcmeDataSet * pOwner) {pAcme=pOwner;}
104
105};
106
107class AcmeAuthItem : public HtmlPageHandler
108{
109 typedef enum {
110 eAuthStart,
111 eAuthCreated,
112 eAuthRetrieved,
113 eAuthActivated,
114 eAuthValid,
115 eAuthPending,
116 eAuthInvalid
117 } eAcmeAuthState_t;
118
119
120public:
121AcmeServletBuffer AuthBuffer;
122NBString AuthItem;//The URL from the Order list
123NBString AuthActivation; //The URL for activation and status
124NBString AuthToken;//The Token to return
125NBString AuthPath;//The Path
126NBString AuthName;//The DNS name were doing all this for
127NBString UseThumb;
128AcmeServletObject & Owner;
129int m_Index;
130int m_Query_Num;
131int m_Retry;
132eAcmeAuthState_t m_State;
133
134//HtmlPageHandler callback used to serve up the response page
135//Possible this might change from is one to has one...
136virtual int ProcessRaw(int sock, HTTP_Request &pd);
137
138AcmeAuthItem(AcmeServletObject & owner,AcmeDataSet * pData);
139void InitNew(const NBString & src, int index);
140~AcmeAuthItem();
141void SendForState();
142bool ProcessForState();
143bool ProcessForTimeout();
144const char * GetStateCC();
145void clear();
146void SetTimeoutSecs(int secs);
147inline bool valid() {return (m_State==eAuthValid); };
148
149};
150
151
152
153
154struct AcmeDataSet
155{
156AcmeServletBuffer DirListing; //Holds the rood directoory listing of services.
157AcmeServletBuffer OrderResult; //Holds the order in process.
158AcmeServletBuffer TransactionResult; //Temp holds the transactions...
159
160
161NBString nonce;
162NBString jwk;
163NBString kid;
164NBString FinalizeUrl;
165NBString RetryAfter;
166NBString Location;
167NBString OrderUrl;
168AcmeAuthItem AuthItem;
169bool bDiag;
170uint8_t CertBigBuffer[16384];
171SimpleBufferObject sbo{CertBigBuffer,16384};
172
173WC_RNG rng;
174
175//Helper function Used to scan incomming headers for things of interest.
176bool ScanHeaderAndSet(const char * pTarget, NBString & setv,const char * hdr);
177
178//Called for every header in incomming transactions.
179void ProcessHeader(const char * hdr);
180
181AcmeDataSet(AcmeServletObject & owner):DirListing(this),OrderResult(this),TransactionResult(this),AuthItem(owner,this)
182{
183}
184~AcmeDataSet();
185};
186
187
191class AcmeServletObject : public DiagItemClass, public WebClientServlet
192{
193
195 typedef enum {
196 eWaitStart,
197 eGetDirs,
198 eFirstNonce,
199 eDoAccount,
200 eDoOrder,
201 eDoAuthItemDo,
202 eDoOrderFinalize,
203 eDoOrderStatus,
204 eDoGetCert,
205 eDoneSleeping,
206 eErrorWaitForRetry,
207 eRestart
208 } eAcmeServletState_t;
209
210const char * m_AcmeServerUrl;
211
212
213ecc_key AccountKey;
214ecc_key ServerKey;
215uint32_t keysize;
216
217//Used by SSL to get keys etc...
218puint8_t m_pServerKey;
219puint8_t m_pServerCert;
220
221
222//State variables
223
224time_t m_issued; //When does cert expire
225time_t m_expiry; //When does cert expire
226time_t m_retrydate; //When shoudl we renew?
227NBString m_CertIssuer; //Name of entity who signed cert.
228NBString m_LastAction;
229
230
231AcmeDataSet * pAcmeSet;
232
233eAcmeServletState_t m_pvt_State;
234int m_Retry_Order;
235int m_Retry_Status;
236int m_Retry_Transaction;
237uint32_t m_StatusFlags;
238
239
240//The erorr state of the world.
241NBString err_str;
242int err_code;
243
244//Active status of the world
245NBString status_str;
246
247
248private:
249
250//Access the things we need to generate responses...
251 NBString GetJwk(); //Java web key
252 NBString GetThumb(); //Thumb print of account key
253 bool GetCSR(NBString & s); //Generate a Certificate signing request
254
255
256 void FillInReq(Cert & req);//Fillin names and details on a cert request
257
258
259//Global pointer so we can get the one and only object for SSL key handeling
260
261
262void RetryOrder();
263
264void SendForState(eAcmeServletState_t state);
265void ProcessForState(eAcmeServletState_t state);
266void ProcessForTimeout(eAcmeServletState_t state);
267
268
269
270PoolPtr PrepTransaction(const NBString & url, const char* payload="", bool bJwk=false);
271void StartTransaction(AcmeServletBuffer & buf,const char * dir_entry, const char* payload="", bool bJwk=false);
272void StartTransactionUrl(AcmeServletBuffer & buf,const NBString & url, const char* payload="", bool bJwk=false);
273
274
275//Save current active keys to storage
276bool SaveKeysToStorage();
277
278//Make and save to storage a selfsigned cert...
279bool MakeSaveSelfSignedCert();
280
281//Read the server cert and set the issue and expiry date
282//Setup cert on SSL.
283void UseCurrentCert();
284
285//DigItemClass virtual for showing state of the world on diag item.
286virtual void ServeContent(int fd);
287
288//WebClientServlet and Root servlet virtual func
289virtual int AddToSelectSet(fd_set &rd_set, fd_set &wr_set, fd_set &er_set);
290
291//WebClientServlet virtual function.
292virtual void ActionComplete(eWebClientAction_t action);
293
294
295bool LoadKeys();
296bool MakeKeys();
297void CheckCert();
298
299protected:
300inline bool FlagIsSet(uint32_t flag){ return ((m_StatusFlags&flag)==flag); };
301inline bool FlagIsClear(uint32_t flag){return ((m_StatusFlags&flag)==0); };
302inline void SetFlag(uint32_t flag) {m_StatusFlags|=flag;};
303inline void ClearFlag(uint32_t flag){m_StatusFlags &=(~flag); };
304
305inline eAcmeServletState_t GetState() {return m_pvt_State;};
306inline void SetState(eAcmeServletState_t s){m_pvt_State=s;}
307
308
309bool bDiag;
310
311
312
313
314public:
315
327
328
332void SetDiag(bool v){bDiag=v; if(pAcmeSet) pAcmeSet->bDiag=v;}
333
334// Access functions:
335
340
346const char * GetStateCC();
347
348//Get the current DNSName the servlet is using.
349//NBString GetNames() {return (NBString)m_DnsName; };
350
351
352
358AcmeServletObject(const char * pUrlDir):
359DiagItemClass("ACMEClient"),
360m_AcmeServerUrl(pUrlDir),
361pAcmeSet(0),
362m_pvt_State(eWaitStart),
363m_Retry_Order(0),
364m_Retry_Status(0),
365m_Retry_Transaction(0),
366m_StatusFlags(0),
367bDiag(false)
368{
369};
370
371
372friend AcmeAuthItem;
374
375
376
377
378
379
380};
381
382
387{
388
389public:
390
396LetsEncryptAcmeServletObject(const bool useStaging=false):
398 (useStaging ? "https://acme-staging-v02.api.letsencrypt.org/directory" : "https://acme-v02.api.letsencrypt.org/directory")
399){};
400
401};
402
403
409{
410
411public:
412
419 "https://api.buypass.com/acme/directory"
420){};
421
422};
423
424#endif
425
Main ACME Client Class.
Definition acmeRFC8555Servlet.h:192
void SetDiag(bool v)
Turn on diganostic printf messages to the console.
Definition acmeRFC8555Servlet.h:332
const char * GetStateCC()
Returns the specific state condition of the acmeservlet.
AcmeServletObject(const char *pUrlDir)
ACME Client Constructor.
Definition acmeRFC8555Servlet.h:358
void Delete_Everything_Restart()
Re-request the ACME certificate.
NBString GetStateString()
Returns the specific state condition of the acmeservlet.
friend CertGenData * GetDataForCertGen()
A function to be overridden to pass certificate details to the certificate generator.
Definition SSL/SslOnboardCertGeneration/CompiledCa/src/main.cpp:17
NBString GetGlobalStateString()
Returns the detailed state of the ACME process.
ACME Client for BuyPass.
Definition acmeRFC8555Servlet.h:409
BuyPassAcmeServletObject()
Enable the BuyPass ACME client.
Definition acmeRFC8555Servlet.h:417
Base class for all diagnostic items.
Definition diagnostics.h:50
Base class for HTTP request handlers that process requests for specific URLs and HTTP methods.
Definition http.h:220
ACME Client for Let's Encrypt.
Definition acmeRFC8555Servlet.h:387
LetsEncryptAcmeServletObject(const bool useStaging=false)
Enable the Let's Encrypt ACME client.
Definition acmeRFC8555Servlet.h:396
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
A class to create, read, and modify a JSON object.
Definition json_lexer.h:535
Class for web client response buffers.
Definition web_buffers.h:32
Used to store the information that is passed in when enabling onboard generated certificates,...
Definition certgen.h:101
HTTP Request Structure.
Definition http.h:87
Main buffer structure for network and serial communication.
Definition buffers.h:90