swupdate-common.bbclass 6.7 KB

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