diff mbox series

[meta-swupdate,3/6] swupdate-common: swupdate-lib: Harden encryption error handling

Message ID 20261104170443.3265957-4-colinmca242@gmail.com
State New
Headers show
Series Improve signing/encryption and add asymmetric encryption support | expand

Commit Message

Colin Pinnell McAllister Nov. 4, 2026, 5:04 p.m. UTC
Ensures that encryption key and iv variables are always initialized
before they're referenced and adds checks to ensure that
SWUPDATE_AES_FILE is set before trying to extract the key and iv.

Signed-off-by: Colin Pinnell McAllister <colinmca242@gmail.com>
---
 classes-recipe/swupdate-common.bbclass | 15 +++++++++++----
 classes-recipe/swupdate-lib.bbclass    |  5 ++++-
 2 files changed, 15 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/classes-recipe/swupdate-common.bbclass b/classes-recipe/swupdate-common.bbclass
index 6462257..19ee40f 100644
--- a/classes-recipe/swupdate-common.bbclass
+++ b/classes-recipe/swupdate-common.bbclass
@@ -261,15 +261,16 @@  def swupdate_add_src_uri(d, list_for_cpio):
         if filename in exclude:
             continue
         aes_file = d.getVar('SWUPDATE_AES_FILE')
+        key = iv = None
         if aes_file:
-            key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
+            key,iv = swupdate_extract_keys(aes_file)
         if (filename != 'sw-description') and (os.path.isfile(local)):
             encrypted = (d.getVarFlag("SWUPDATE_IMAGES_ENCRYPTED", filename) or "")
             dst = os.path.join(workdir, "%s" % filename )
             if encrypted == '1':
                 bb.note("Encryption requested for %s" %(filename))
                 if not key or not iv:
-                    bb.fatal("Encryption required, but no key found")
+                    bb.fatal("Encryption required for %s, but no key or IV found" % filename)
                 iv = swupdate_get_IV(d, workdir, filename)
                 swupdate_encrypt_file(local, dst, key, iv)
             else:
@@ -285,9 +286,15 @@  def add_image_to_swu(d, deploydir, imagename, s, encrypt, list_for_cpio):
     target_imagename = os.path.basename(imagename)  # allow images in subfolders of DEPLOY_DIR_IMAGE
     dst = os.path.join(s, target_imagename)
     if encrypt == '1':
-        key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
+        aes_file = d.getVar('SWUPDATE_AES_FILE')
+        key = iv = None
+        if aes_file:
+            key,iv = swupdate_extract_keys(aes_file)
         bb.note("Encryption requested for %s" %(imagename))
-        iv = swupdate_get_IV(d, s, imagename)
+        # Key by the name used inside the SWU so sw-description lookups match
+        iv = swupdate_get_IV(d, s, target_imagename)
+        if not key or not iv:
+            bb.fatal("Encryption required for %s, but no key or IV found" % target_imagename)
         swupdate_encrypt_file(src, dst, key, iv)
     else:
         shutil.copyfile(src, dst)
diff --git a/classes-recipe/swupdate-lib.bbclass b/classes-recipe/swupdate-lib.bbclass
index ae8c093..37910ba 100644
--- a/classes-recipe/swupdate-lib.bbclass
+++ b/classes-recipe/swupdate-lib.bbclass
@@ -42,7 +42,10 @@  def swupdate_get_sha256(d, s, filename):
 
 def swupdate_get_IV(d, s, filename):
     # By default preserve original behavior: use IV from SWUPDATE_AES_FILE.
-    key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE', True))
+    aes_file = d.getVar('SWUPDATE_AES_FILE')
+    if not aes_file:
+        bb.fatal("SWUPDATE_AES_FILE must be set to encrypt %s" % filename)
+    key,iv = swupdate_extract_keys(aes_file)
     return iv
 
 def swupdate_get_unique_IV(d, s, filename):