28
28
29
29
using namespace httpsserver ;
30
30
31
- static volatile bool isCertReady = 0 ;
31
+ static volatile bool isCertReady = false ;
32
32
33
33
// "20190101000000"
34
34
static std::string toCertDate (time_t theTime) {
@@ -78,11 +78,9 @@ static void createCert(void *param) {
78
78
createDn (),
79
79
fromDate,
80
80
toDate);
81
- log_i (" PK Length %d, Key Length %d" , newCert.getPKLength (), newCert.getCertLength ());
82
-
83
81
if (res != 0 ) {
84
82
// Certificate generation failed. Inform the user.
85
- log_e (" An error occured during certificate generation." );
83
+ log_e (" An error occurred during certificate generation." );
86
84
log_e (" Error code is 0x%04x" , res);
87
85
log_e (" You may have a look at SSLCert.h to find the reason for this error." );
88
86
} else {
@@ -91,17 +89,11 @@ static void createCert(void *param) {
91
89
cert->setPK (newCert.getPKData (), newCert.getPKLength ());
92
90
log_i (" Created new cert." );
93
91
};
94
-
95
92
// Can this be done more elegant?
96
- isCertReady = 1 ;
93
+ isCertReady = true ;
97
94
vTaskDelete (nullptr );
98
95
}
99
96
100
- /* Just ensure there is a cert! */
101
- void Https::ensureCertificate () {
102
- delete getCertificate ();
103
- }
104
-
105
97
bool Https::existsCertificate () {
106
98
return SPIFFS.exists (" /key.der" ) && SPIFFS.exists (" /cert.der" );
107
99
}
@@ -117,61 +109,42 @@ SSLCert *Https::getCertificate(std::function<void()> progress) {
117
109
118
110
// If now, create them
119
111
if (!keyFile || !certFile || keyFile.size ()==0 || certFile.size ()==0 ) {
120
- log_i (" No certificate found in SPIFFS, generating a new one for you." );
121
- log_i (" If you face a Guru Meditation, give the script another try (or two...)." );
112
+ log_i (" No certificate found in SPIFFS, generating a new one." );
122
113
log_i (" This may take up to a minute, so please stand by :)" );
123
114
124
115
SSLCert * newCert = new SSLCert ();
125
-
126
- TaskHandle_t xHandle;
127
116
xTaskCreate (reinterpret_cast <TaskFunction_t>(createCert), " createCert" ,
128
- 16 * 1024 , newCert, 1 , &xHandle );
117
+ 16 * 1024 , newCert, 1 , nullptr );
129
118
130
119
while (!isCertReady) {
131
120
if (progress) {
132
121
progress ();
133
122
}
134
123
delay (100 );
135
- yield ();
136
124
esp_task_wdt_reset ();
137
125
}
138
126
139
- log_i (" PK Length %d, Key Length %d" , newCert->getPKLength (), newCert->getCertLength ());
140
- int res = 0 ;
141
- if (res == 0 ) {
142
- // We now have a certificate. We store it on the SPIFFS to restore it on next boot.
143
-
144
- bool failure = false ;
145
- // Private key
146
- keyFile = SPIFFS.open (" /key.der" , FILE_WRITE);
147
- if (!keyFile || !keyFile.write (newCert->getPKData (), newCert->getPKLength ())) {
148
- log_e (" Could not write /key.der" );
149
- failure = true ;
150
- }
151
- if (keyFile) keyFile.close ();
152
-
153
- // Certificate
154
- certFile = SPIFFS.open (" /cert.der" , FILE_WRITE);
155
- if (!certFile || !certFile.write (newCert->getCertData (), newCert->getCertLength ())) {
156
- log_e (" Could not write /cert.der" );
157
- failure = true ;
158
- }
159
- if (certFile) certFile.close ();
160
-
161
- if (failure) {
162
- log_e (" Certificate could not be stored permanently, generating new certificate on reboot..." );
163
- }
164
-
165
- return newCert;
127
+ bool failure = false ;
128
+ // Private key
129
+ keyFile = SPIFFS.open (" /key.der" , FILE_WRITE);
130
+ if (!keyFile || !keyFile.write (newCert->getPKData (), newCert->getPKLength ())) {
131
+ log_e (" Could not write /key.der" );
132
+ failure = true ;
133
+ }
134
+ if (keyFile) keyFile.close ();
166
135
167
- } else {
168
- // Certificate generation failed. Inform the user.
169
- log_e (" An error occured during certificate generation." );
170
- log_e (" Error code is 0x%04x" , res);
171
- log_e (" You may have a look at SSLCert.h to find the reason for this error." );
172
- return nullptr ;
136
+ // Certificate
137
+ certFile = SPIFFS.open (" /cert.der" , FILE_WRITE);
138
+ if (!certFile || !certFile.write (newCert->getCertData (), newCert->getCertLength ())) {
139
+ log_e (" Could not write /cert.der" );
140
+ failure = true ;
173
141
}
142
+ if (certFile) certFile.close ();
174
143
144
+ if (failure) {
145
+ log_e (" Certificate could not be stored permanently, generating new certificate on reboot..." );
146
+ }
147
+ return newCert;
175
148
} else {
176
149
log_i (" Reading certificate from SPIFFS." );
177
150
@@ -193,7 +166,6 @@ SSLCert *Https::getCertificate(std::function<void()> progress) {
193
166
keyFile.read (keyBuffer, keySize);
194
167
certFile.read (certBuffer, certSize);
195
168
196
- // Close the files
197
169
keyFile.close ();
198
170
certFile.close ();
199
171
log_i (" Read %u bytes of certificate and %u bytes of key from SPIFFS" , certSize, keySize);
0 commit comments