swupdate-common.bbclass 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. def swupdate_is_hash_needed(s, filename):
  2. with open(os.path.join(s, "sw-description"), 'r') as f:
  3. for line in f:
  4. if line.find("@%s" % (filename)) != -1:
  5. return True
  6. return False
  7. def swupdate_get_sha256(s, filename):
  8. import hashlib
  9. m = hashlib.sha256()
  10. with open(os.path.join(s, filename), 'rb') as f:
  11. while True:
  12. data = f.read(1024)
  13. if not data:
  14. break
  15. m.update(data)
  16. return m.hexdigest()
  17. def swupdate_extract_keys(keyfile_path):
  18. try:
  19. with open(keyfile_path, 'r') as f:
  20. lines = f.readlines()
  21. except IOError:
  22. bb.fatal("Failed to open file with keys %s" % (keyfile))
  23. data = {}
  24. for _ in lines:
  25. k,v = _.split('=',maxsplit=1)
  26. data[k.rstrip()] = v
  27. key = data['key'].rstrip('\n')
  28. iv = data['iv'].rstrip('\n')
  29. return key,iv
  30. def swupdate_encrypt_file(f, out, key, ivt):
  31. import subprocess
  32. encargs = ["openssl", "enc", "-aes-256-cbc", "-in", f, "-out", out]
  33. encargs += ["-K", key, "-iv", ivt, "-nosalt"]
  34. subprocess.run(encargs, check=True)
  35. def swupdate_write_sha256(s, filename, hash):
  36. write_lines = []
  37. with open(os.path.join(s, "sw-description"), 'r') as f:
  38. for line in f:
  39. write_lines.append(line.replace("@%s" % (filename), hash))
  40. with open(os.path.join(s, "sw-description"), 'w+') as f:
  41. for line in write_lines:
  42. f.write(line)
  43. def swupdate_expand_bitbake_variables(d, s):
  44. write_lines = []
  45. with open(os.path.join(s, "sw-description"), 'r') as f:
  46. import re
  47. for line in f:
  48. found = False
  49. while True:
  50. m = re.match(r"^(?P<before_placeholder>.+)@@(?P<bitbake_variable_name>\w+)@@(?P<after_placeholder>.+)$", line)
  51. if m:
  52. bitbake_variable_value = d.getVar(m.group('bitbake_variable_name'), True)
  53. if bitbake_variable_value is None:
  54. bitbake_variable_value = ""
  55. bb.warn("BitBake variable %s not set" % (m.group('bitbake_variable_name')))
  56. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  57. found = True
  58. continue
  59. else:
  60. m = re.match(r"^(?P<before_placeholder>.+)@@(?P<bitbake_variable_name>.+)\[(?P<flag_var_name>.+)\]@@(?P<after_placeholder>.+)$", line)
  61. if m:
  62. bitbake_variable_value = (d.getVarFlag(m.group('bitbake_variable_name'), m.group('flag_var_name'), True) or "")
  63. if bitbake_variable_value is None:
  64. bitbake_variable_value = ""
  65. line = m.group('before_placeholder') + bitbake_variable_value + m.group('after_placeholder')
  66. continue
  67. if found:
  68. line = line + "\n"
  69. break
  70. write_lines.append(line)
  71. with open(os.path.join(s, "sw-description"), 'w+') as f:
  72. for line in write_lines:
  73. f.write(line)
  74. def prepare_sw_description(d, s, list_for_cpio):
  75. import shutil
  76. swupdate_expand_bitbake_variables(d, s)
  77. for file in list_for_cpio:
  78. if file != 'sw-description' and swupdate_is_hash_needed(s, file):
  79. hash = swupdate_get_sha256(s, file)
  80. swupdate_write_sha256(s, file, hash)
  81. encrypt = d.getVar('SWUPDATE_ENCRYPT_SWDESC', True)
  82. if encrypt:
  83. bb.note("Encryption of sw-description")
  84. shutil.copyfile(os.path.join(s, 'sw-description'), os.path.join(s, 'sw-description.plain'))
  85. key,iv = swupdate_extract_keys(d.getVar('SWUPDATE_AES_FILE', True))
  86. swupdate_encrypt_file(os.path.join(s, 'sw-description.plain'), os.path.join(s, 'sw-description'), key, iv)
  87. signing = d.getVar('SWUPDATE_SIGNING', True)
  88. if signing == "1":
  89. bb.warn('SWUPDATE_SIGNING = "1" is deprecated, falling back to "RSA". It is advised to set it to "RSA" if using RSA signing.')
  90. signing = "RSA"
  91. if signing:
  92. if signing == "CUSTOM":
  93. sign_tool = d.getVar('SWUPDATE_SIGN_TOOL', True)
  94. if sign_tool:
  95. ret = os.system(sign_tool)
  96. if ret != 0:
  97. bb.fatal("Failed to sign with %s" % (sign_tool))
  98. else:
  99. bb.fatal("Custom SWUPDATE_SIGN_TOOL is not given")
  100. elif signing == "RSA":
  101. privkey = d.getVar('SWUPDATE_PRIVATE_KEY', True)
  102. if not privkey:
  103. bb.fatal("SWUPDATE_PRIVATE_KEY isn't set")
  104. if not os.path.exists(privkey):
  105. bb.fatal("SWUPDATE_PRIVATE_KEY %s doesn't exist" % (privkey))
  106. passout = d.getVar('SWUPDATE_PASSWORD_FILE', True)
  107. if passout:
  108. passout = "-passin file:'%s' " % (passout)
  109. else:
  110. passout = ""
  111. signcmd = "openssl dgst -sha256 -sign '%s' %s -out '%s' '%s'" % (
  112. privkey,
  113. passout,
  114. os.path.join(s, 'sw-description.sig'),
  115. os.path.join(s, 'sw-description.plain' if encrypt else 'sw-description'))
  116. if os.system(signcmd) != 0:
  117. bb.fatal("Failed to sign sw-description with %s" % (privkey))
  118. elif signing == "CMS":
  119. cms_cert = d.getVar('SWUPDATE_CMS_CERT', True)
  120. if not cms_cert:
  121. bb.fatal("SWUPDATE_CMS_CERT is not set")
  122. if not os.path.exists(cms_cert):
  123. bb.fatal("SWUPDATE_CMS_CERT %s doesn't exist" % (cms_cert))
  124. cms_key = d.getVar('SWUPDATE_CMS_KEY', True)
  125. if not cms_key:
  126. bb.fatal("SWUPDATE_CMS_KEY isn't set")
  127. if not os.path.exists(cms_key):
  128. bb.fatal("SWUPDATE_CMS_KEY %s doesn't exist" % (cms_key))
  129. passout = d.getVar('SWUPDATE_PASSWORD_FILE', True)
  130. if passout:
  131. passout = "-passin file:'%s' " % (passout)
  132. else:
  133. passout = ""
  134. signcmd = "openssl cms -sign -in '%s' -out '%s' -signer '%s' -inkey '%s' %s -outform DER -nosmimecap -binary" % (
  135. os.path.join(s, 'sw-description.plain' if encrypt else 'sw-description'),
  136. os.path.join(s, 'sw-description.sig'),
  137. cms_cert,
  138. cms_key,
  139. passout)
  140. if os.system(signcmd) != 0:
  141. bb.fatal("Failed to sign sw-description with %s" % (privkey))
  142. else:
  143. bb.fatal("Unrecognized SWUPDATE_SIGNING mechanism.");