ESP32 HTTPS Server
SSLCert.hpp
1 #ifndef SRC_SSLCERT_HPP_
2 #define SRC_SSLCERT_HPP_
3 
4 #include <Arduino.h>
5 
6 #ifndef HTTPS_DISABLE_SELFSIGNING
7 #include <string>
8 #include <mbedtls/rsa.h>
9 #include <mbedtls/entropy.h>
10 #include <mbedtls/ctr_drbg.h>
11 #include <mbedtls/pk.h>
12 #include <mbedtls/x509.h>
13 #include <mbedtls/x509_crt.h>
14 #include <mbedtls/x509_csr.h>
15 
16 #define HTTPS_SERVER_ERROR_KEYGEN 0x0F
17 #define HTTPS_SERVER_ERROR_KEYGEN_RNG 0x02
18 #define HTTPS_SERVER_ERROR_KEYGEN_SETUP_PK 0x03
19 #define HTTPS_SERVER_ERROR_KEYGEN_GEN_PK 0x04
20 #define HTTPS_SERVER_ERROR_KEY_WRITE_PK 0x05
21 #define HTTPS_SERVER_ERROR_KEY_OUT_OF_MEM 0x06
22 #define HTTPS_SERVER_ERROR_CERTGEN 0x1F
23 #define HTTPS_SERVER_ERROR_CERTGEN_RNG 0x12
24 #define HTTPS_SERVER_ERROR_CERTGEN_READKEY 0x13
25 #define HTTPS_SERVER_ERROR_CERTGEN_WRITE 0x15
26 #define HTTPS_SERVER_ERROR_CERTGEN_OUT_OF_MEM 0x16
27 #define HTTPS_SERVER_ERROR_CERTGEN_NAME 0x17
28 #define HTTPS_SERVER_ERROR_CERTGEN_SERIAL 0x18
29 #define HTTPS_SERVER_ERROR_CERTGEN_VALIDITY 0x19
30 
31 #endif // !HTTPS_DISABLE_SELFSIGNING
32 
33 namespace httpsserver {
34 
59 class SSLCert {
60 public:
75  SSLCert(
76  unsigned char * certData = NULL,
77  uint16_t certLength = 0,
78  unsigned char * pkData = NULL,
79  uint16_t pkLength = 0
80  );
81  virtual ~SSLCert();
82 
86  uint16_t getCertLength();
87 
91  uint16_t getPKLength();
92 
96  unsigned char * getCertData();
97 
101  unsigned char * getPKData();
102 
114  void setPK(unsigned char * _pkData, uint16_t length);
115 
127  void setCert(unsigned char * _certData, uint16_t length);
128 
132  void clear();
133 
134 private:
135  uint16_t _certLength;
136  unsigned char * _certData;
137  uint16_t _pkLength;
138  unsigned char * _pkData;
139 
140 };
141 
142 #ifndef HTTPS_DISABLE_SELFSIGNING
143 
151  KEYSIZE_1024 = 1024,
153  KEYSIZE_2048 = 2048,
156 };
157 
176 int createSelfSignedCert(SSLCert &certCtx, SSLKeySize keySize, std::string dn, std::string validFrom = "20190101000000", std::string validUntil = "20300101000000");
177 
178 #endif // !HTTPS_DISABLE_SELFSIGNING
179 
180 } /* namespace httpsserver */
181 
182 #endif /* SRC_SSLCERT_HPP_ */
uint16_t getPKLength()
Returns the length of the private key in byte.
Definition: SSLCert.cpp:22
void clear()
Clears the key buffers and deletes them.
Definition: SSLCert.cpp:44
RSA key with 2048 bit.
Definition: SSLCert.hpp:153
RSA key with 1024 bit.
Definition: SSLCert.hpp:151
unsigned char * getCertData()
Returns the certificate data.
Definition: SSLCert.cpp:26
Certificate and private key that can be passed to the HTTPSServer.
Definition: SSLCert.hpp:59
int createSelfSignedCert(SSLCert &certCtx, SSLKeySize keySize, std::string dn, std::string validFrom, std::string validUntil)
Creates a self-signed certificate on the ESP32.
Definition: SSLCert.cpp:287
SSLCert(unsigned char *certData=NULL, uint16_t certLength=0, unsigned char *pkData=NULL, uint16_t pkLength=0)
Creates a new SSLCert.
Definition: SSLCert.cpp:5
void setCert(unsigned char *_certData, uint16_t length)
Sets the certificate data in DER format.
Definition: SSLCert.cpp:39
unsigned char * getPKData()
Returns the private key data.
Definition: SSLCert.cpp:30
RSA key with 4096 bit.
Definition: SSLCert.hpp:155
uint16_t getCertLength()
Returns the length of the certificate in byte.
Definition: SSLCert.cpp:18
Definition: ConnectionContext.cpp:3
void setPK(unsigned char *_pkData, uint16_t length)
Sets the private key in DER format.
Definition: SSLCert.cpp:34
SSLKeySize
Defines the key size for key generation.
Definition: SSLCert.hpp:149