27
27
#include " esp_system.h"
28
28
#include " lib/core/CHIPError.h"
29
29
30
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
31
+ #include < esp_encrypted_img.h>
32
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
33
+
30
34
#define TAG " OTAImageProcessor"
31
35
using namespace chip ::System;
32
36
using namespace ::chip::DeviceLayer::Internal;
@@ -54,6 +58,20 @@ void PostOTAStateChangeEvent(DeviceLayer::OtaState newState)
54
58
55
59
} // namespace
56
60
61
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
62
+ void OTAImageProcessorImpl::EndDecryption ()
63
+ {
64
+ VerifyOrReturn (mEncryptedOTAEnabled );
65
+
66
+ esp_err_t err = esp_encrypted_img_decrypt_end (mOTADecryptionHandle );
67
+ if (err != ESP_OK)
68
+ {
69
+ ChipLogError (SoftwareUpdate, " Failed to end pre encrypted OTA esp_err:%d" , err);
70
+ }
71
+ mOTADecryptionHandle = nullptr ;
72
+ }
73
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
74
+
57
75
bool OTAImageProcessorImpl::IsFirstImageRun ()
58
76
{
59
77
OTARequestorInterface * requestor = GetRequestorInstance ();
@@ -145,6 +163,32 @@ void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context)
145
163
imageProcessor->mDownloader ->OnPreparedForDownload (ESP32Utils::MapError (err));
146
164
return ;
147
165
}
166
+
167
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
168
+ if (imageProcessor->mEncryptedOTAEnabled == false )
169
+ {
170
+ ChipLogError (SoftwareUpdate, " Encrypted OTA is not initialized" );
171
+ imageProcessor->mDownloader ->OnPreparedForDownload (ESP32Utils::MapError (err));
172
+ return ;
173
+ }
174
+
175
+ // This struct takes in private key but arguments are named as pub_key
176
+ // This is the issue in the esp_encrypted_img component
177
+ // https://github.com/espressif/idf-extra-components/blob/791d506/esp_encrypted_img/include/esp_encrypted_img.h#L47
178
+ const esp_decrypt_cfg_t decryptionConfig = {
179
+ .rsa_pub_key = imageProcessor->mKey .data (),
180
+ .rsa_pub_key_len = imageProcessor->mKey .size (),
181
+ };
182
+
183
+ imageProcessor->mOTADecryptionHandle = esp_encrypted_img_decrypt_start (&decryptionConfig);
184
+ if (imageProcessor->mOTADecryptionHandle == nullptr )
185
+ {
186
+ ChipLogError (SoftwareUpdate, " Failed to initialize encrypted OTA" );
187
+ imageProcessor->mDownloader ->OnPreparedForDownload (ESP32Utils::MapError (ESP_FAIL));
188
+ return ;
189
+ }
190
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
191
+
148
192
imageProcessor->mHeaderParser .Init ();
149
193
imageProcessor->mDownloader ->OnPreparedForDownload (CHIP_NO_ERROR);
150
194
PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadInProgress );
@@ -158,6 +202,11 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
158
202
ChipLogError (SoftwareUpdate, " ImageProcessor context is null" );
159
203
return ;
160
204
}
205
+
206
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
207
+ imageProcessor->EndDecryption ();
208
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
209
+
161
210
esp_err_t err = esp_ota_end (imageProcessor->mOTAUpdateHandle );
162
211
if (err != ESP_OK)
163
212
{
@@ -185,6 +234,11 @@ void OTAImageProcessorImpl::HandleAbort(intptr_t context)
185
234
ChipLogError (SoftwareUpdate, " ImageProcessor context is null" );
186
235
return ;
187
236
}
237
+
238
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
239
+ imageProcessor->EndDecryption ();
240
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
241
+
188
242
if (esp_ota_abort (imageProcessor->mOTAUpdateHandle ) != ESP_OK)
189
243
{
190
244
ESP_LOGE (TAG, " ESP OTA abort failed" );
@@ -218,15 +272,68 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
218
272
return ;
219
273
}
220
274
221
- esp_err_t err = esp_ota_write (imageProcessor->mOTAUpdateHandle , block.data (), block.size ());
275
+ esp_err_t err;
276
+ ByteSpan blockToWrite = block;
277
+
278
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
279
+ if (imageProcessor->mEncryptedOTAEnabled == false )
280
+ {
281
+ ChipLogError (SoftwareUpdate, " Encrypted OTA is not initialized" );
282
+ imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_INCORRECT_STATE);
283
+ PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadFailed );
284
+ return ;
285
+ }
286
+
287
+ if (imageProcessor->mOTADecryptionHandle == nullptr )
288
+ {
289
+ ChipLogError (SoftwareUpdate, " OTA decryption handle is nullptr" );
290
+ imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_INCORRECT_STATE);
291
+ PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadFailed );
292
+ return ;
293
+ }
294
+
295
+ pre_enc_decrypt_arg_t preEncOtaDecryptArgs = {
296
+ .data_in = reinterpret_cast <const char *>(block.data ()),
297
+ .data_in_len = block.size (),
298
+ .data_out = nullptr ,
299
+ .data_out_len = 0 ,
300
+ };
301
+
302
+ err = esp_encrypted_img_decrypt_data (imageProcessor->mOTADecryptionHandle , &preEncOtaDecryptArgs);
303
+ if (err != ESP_OK && err != ESP_ERR_NOT_FINISHED)
304
+ {
305
+ ChipLogError (SoftwareUpdate, " esp_encrypted_img_decrypt_data failed err:%d" , err);
306
+ imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
307
+ PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadFailed );
308
+ return ;
309
+ }
310
+
311
+ ChipLogDetail (SoftwareUpdate, " data_in_len:%u, data_out_len:%u" , preEncOtaDecryptArgs.data_in_len ,
312
+ preEncOtaDecryptArgs.data_out_len );
313
+
314
+ if (preEncOtaDecryptArgs.data_out == nullptr || preEncOtaDecryptArgs.data_out_len <= 0 )
315
+ {
316
+ ChipLogProgress (SoftwareUpdate, " Decrypted data is null or out len is zero" );
317
+ }
318
+
319
+ blockToWrite = ByteSpan (reinterpret_cast <const uint8_t *>(preEncOtaDecryptArgs.data_out ), preEncOtaDecryptArgs.data_out_len );
320
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
321
+
322
+ err = esp_ota_write (imageProcessor->mOTAUpdateHandle , blockToWrite.data (), blockToWrite.size ());
323
+
324
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
325
+ free (preEncOtaDecryptArgs.data_out );
326
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
327
+
222
328
if (err != ESP_OK)
223
329
{
224
330
ESP_LOGE (TAG, " esp_ota_write failed (%s)" , esp_err_to_name (err));
225
331
imageProcessor->mDownloader ->EndDownload (CHIP_ERROR_WRITE_FAILED);
226
332
PostOTAStateChangeEvent (DeviceLayer::kOtaDownloadFailed );
227
333
return ;
228
334
}
229
- imageProcessor->mParams .downloadedBytes += block.size ();
335
+
336
+ imageProcessor->mParams .downloadedBytes += blockToWrite.size ();
230
337
imageProcessor->mDownloader ->FetchNextData ();
231
338
}
232
339
@@ -310,4 +417,16 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & block)
310
417
return CHIP_NO_ERROR;
311
418
}
312
419
420
+ #if CONFIG_ENABLE_ENCRYPTED_OTA
421
+ CHIP_ERROR OTAImageProcessorImpl::InitEncryptedOTA (const CharSpan & key)
422
+ {
423
+ VerifyOrReturnError (mEncryptedOTAEnabled == false , CHIP_ERROR_INCORRECT_STATE);
424
+ VerifyOrReturnError (IsSpanUsable (key), CHIP_ERROR_INVALID_ARGUMENT);
425
+
426
+ mKey = key;
427
+ mEncryptedOTAEnabled = true ;
428
+ return CHIP_NO_ERROR;
429
+ }
430
+ #endif // CONFIG_ENABLE_ENCRYPTED_OTA
431
+
313
432
} // namespace chip
0 commit comments