diff mbox series

[meta-swupdate,5/6] swupdate-common: swupdate-lib: Support asymmetric encryption

Message ID 20261104170443.3265957-6-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
Adds the ability to asymmetrically encrypt sw-description for a list of
recipient certificates. This lets a single SWU be decrypted by any
device holding one of the recipient private keys.

SWUPDATE_ENCRYPT_SWDESC is reworked from a boolean into a mechanism
selector holding "" (disabled), "AES", or "CMS". The previous "1" and
"0" values are still accepted with a deprecation warning and mapped to
"AES" and "", respectively. The configuration is validated at parse time
so that an unknown mechanism or a missing SWUPDATE_ENCRYPT_CMS_CERTS
fails early rather than at do_swuimage.

Signed-off-by: Colin Pinnell McAllister <colinmca242@gmail.com>
---
 README                                 | 26 +++++++++++++++--
 classes-recipe/swupdate-common.bbclass | 39 ++++++++++++++++++++++----
 classes-recipe/swupdate-lib.bbclass    | 20 +++++++++++++
 3 files changed, 76 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/README b/README
index 1f4a26a..ddcadcb 100644
--- a/README
+++ b/README
@@ -108,9 +108,29 @@  is set for them. By default the key and IV are taken from `SWUPDATE_AES_FILE`.
 
   * An IV can be pinned for a given file with `SWUPDATE_IV[<file>] = "<hex>"`.
 
-sw-description itself is encrypted when `SWUPDATE_ENCRYPT_SWDESC = "1"` is
-set. It always uses the key and IV from `SWUPDATE_AES_FILE`, since the device
-decrypts it with that key and IV before it can read any per-artifact IV.
+There are 2 mechanisms to encrypt sw-description itself:
+
+1. Symmetric (AES) encryption:
+
+  * Set variable: `SWUPDATE_ENCRYPT_SWDESC = "AES"`
+
+  * The key and IV are always taken from `SWUPDATE_AES_FILE`, since the
+    device decrypts sw-description with that key and IV before it can read
+    any per-artifact IV.
+
+  * `SWUPDATE_ENCRYPT_SWDESC = "1"` is deprecated and treated as `"AES"`.
+
+2. Asymmetric (CMS) encryption:
+
+  * Set variable: `SWUPDATE_ENCRYPT_SWDESC = "CMS"`
+
+  * Set `SWUPDATE_ENCRYPT_CMS_CERTS` to a space delimited list of recipient
+    certificate files. sw-description is encrypted with `openssl cms -encrypt`
+    so that any listed recipient can decrypt it. See SWUpdate's
+    "Asymmetrically Encrypted Update Images" documentation.
+
+When sw-description is encrypted, signing is performed on the plaintext
+sw-description.
 
 Maintainer
 ----------
diff --git a/classes-recipe/swupdate-common.bbclass b/classes-recipe/swupdate-common.bbclass
index 3107aaa..5f0d934 100644
--- a/classes-recipe/swupdate-common.bbclass
+++ b/classes-recipe/swupdate-common.bbclass
@@ -22,13 +22,30 @@  do_swuimage[sstate-inputdirs] = "${SWUDEPLOYDIR}"
 do_swuimage[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
 do_swuimage[stamp-extra-info] = "${IMAGE_MACHINE_SUFFIX}"
 
+SWUPDATE_ENCRYPT_SWDESC ?= ""
 SWUPDATE_USE_UNIQUE_IVS ?= "0"
+SWUPDATE_ENCRYPT_CMS_CERTS ?= ""
+
+def swupdate_check_encryption_config(d):
+    encrypt = d.getVar('SWUPDATE_ENCRYPT_SWDESC')
+    if encrypt == "1":
+        bb.warn('SWUPDATE_ENCRYPT_SWDESC = "1" is deprecated, use "AES".')
+        d.setVar('SWUPDATE_ENCRYPT_SWDESC', "AES")
+    elif encrypt == "0":
+        bb.warn('SWUPDATE_ENCRYPT_SWDESC = "0" is treated as disabled; leave it unset instead.')
+        d.setVar('SWUPDATE_ENCRYPT_SWDESC', "")
+    elif encrypt and encrypt not in ("AES", "CMS"):
+        bb.fatal("Unrecognized SWUPDATE_ENCRYPT_SWDESC '%s' (expected AES or CMS)" % encrypt)
+
+    if encrypt == "CMS" and not d.getVar('SWUPDATE_ENCRYPT_CMS_CERTS'):
+        bb.fatal("SWUPDATE_ENCRYPT_SWDESC = \"CMS\" requires SWUPDATE_ENCRYPT_CMS_CERTS")
 
 python () {
     deps = " " + swupdate_getdepends(d)
     d.appendVarFlag('do_swuimage', 'depends', deps)
     d.delVarFlag('do_fetch', 'noexec')
     d.delVarFlag('do_unpack', 'noexec')
+    swupdate_check_encryption_config(d)
 }
 
 def get_pwd_file_args(d, passfile):
@@ -181,12 +198,22 @@  def prepare_sw_description(d):
 
     encrypt = d.getVar('SWUPDATE_ENCRYPT_SWDESC')
     if encrypt:
-        bb.note("Encryption of sw-description")
-        shutil.copyfile(os.path.join(workdir, 'sw-description'), os.path.join(workdir, 'sw-description.plain'))
-        # sw-description must use the static IV from SWUPDATE_AES_FILE: the
-        # device decrypts it with that key/IV, so a unique IV cannot be used here
-        key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
-        swupdate_encrypt_file(os.path.join(workdir, 'sw-description.plain'), os.path.join(workdir, 'sw-description'), key, iv)
+        sw_desc_plain = os.path.join(workdir, 'sw-description.plain')
+        sw_desc_out = os.path.join(workdir, 'sw-description')
+        shutil.copyfile(sw_desc_out, sw_desc_plain)
+        if encrypt == "AES":
+            bb.note("Symmetric (AES) encryption of sw-description")
+            # sw-description must use the static IV from SWUPDATE_AES_FILE: the
+            # device decrypts it with that key/IV, so a unique IV cannot be used here
+            key, iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE'))
+            swupdate_encrypt_file(sw_desc_plain, sw_desc_out, key, iv)
+        elif encrypt == "CMS":
+            bb.note("Asymmetric (CMS) encryption of sw-description")
+            certs = (d.getVar('SWUPDATE_ENCRYPT_CMS_CERTS') or "").split()
+            for cert in certs:
+                if not os.path.exists(cert):
+                    bb.fatal("SWUPDATE_ENCRYPT_CMS_CERTS path %s doesn't exist" % cert)
+            swupdate_cms_encrypt_file(sw_desc_plain, sw_desc_out, certs)
 
     signing = d.getVar('SWUPDATE_SIGNING')
     if signing == "1":
diff --git a/classes-recipe/swupdate-lib.bbclass b/classes-recipe/swupdate-lib.bbclass
index 87a922a..a3dce8d 100644
--- a/classes-recipe/swupdate-lib.bbclass
+++ b/classes-recipe/swupdate-lib.bbclass
@@ -10,6 +10,26 @@  def swupdate_encrypt_file(f, out, key, ivt):
     encargs += ["-K", key, "-iv", ivt, "-nosalt"]
     subprocess.run(encargs, check=True)
 
+def swupdate_cms_encrypt_file(f, out, certs):
+    import subprocess
+    from itertools import chain
+
+    encargs = [
+        "openssl",
+        "cms",
+        "-encrypt",
+        "-binary",
+        "-aes-256-cbc",
+        "-outform",
+        "DER",
+        "-in",
+        f,
+        "-out",
+        out,
+    ]
+    encargs += list(chain.from_iterable(("-recip", v) for v in certs))
+    subprocess.run(encargs, check=True)
+
 def swupdate_extract_keys(keyfile_path):
     try:
         with open(keyfile_path, 'r') as f: