swupdate-lib.bbclass 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. DEPENDS += "python3-magic-native zstd-native"
  2. def swupdate_encrypt_file(f, out, key, ivt):
  3. import subprocess
  4. encargs = ["openssl", "enc", "-aes-256-cbc", "-in", f, "-out", out]
  5. encargs += ["-K", key, "-iv", ivt, "-nosalt"]
  6. subprocess.run(encargs, check=True)
  7. def swupdate_extract_keys(keyfile_path):
  8. try:
  9. with open(keyfile_path, 'r') as f:
  10. lines = f.readlines()
  11. except IOError:
  12. bb.fatal("Failed to open file with keys %s" % (keyfile))
  13. data = {}
  14. for _ in lines:
  15. k,v = _.split('=',maxsplit=1)
  16. data[k.rstrip()] = v
  17. key = data['key'].rstrip('\n')
  18. iv = data['iv'].rstrip('\n')
  19. return key,iv
  20. def swupdate_get_sha256(d, s, filename):
  21. import hashlib
  22. m = hashlib.sha256()
  23. with open(os.path.join(s, filename), 'rb') as f:
  24. while True:
  25. data = f.read(1024)
  26. if not data:
  27. break
  28. m.update(data)
  29. return m.hexdigest()
  30. def swupdate_sign_file(d, s, filename):
  31. import subprocess
  32. import magic
  33. import base64
  34. fname = os.path.join(s, filename)
  35. mime = magic.Magic(mime=True)
  36. ftype = mime.from_file(fname)
  37. if ftype == 'application/zstd':
  38. zcmd = 'zstdcat'
  39. elif ftype == 'application/gzip':
  40. zcmd = 'zcat'
  41. else:
  42. zcmd = 'cat'
  43. privkey = d.getVar('SWUPDATE_SIGN_PRIVATE_KEY')
  44. dump = subprocess.run([ zcmd, fname ], check=True, capture_output=True)
  45. signature = subprocess.run([ "openssl", "dgst", "-keyform", "PEM", "-sha256", "-sign", privkey ] + \
  46. get_pwd_file_args(d, 'SWUPDATE_SIGN_PASSWORD_FILE'), check=True, capture_output=True, input=dump.stdout)
  47. hash = base64.b64encode(signature.stdout).decode()
  48. # SWUpdate accepts attribute with a maximum size of 255. If the hash
  49. # exceeds this value, returns sha256 of the generated hash
  50. #
  51. if len(hash) > 255:
  52. m = hashlib.sha256()
  53. m.update(hash)
  54. hash = m.hexdigest()
  55. return hash