signing.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #
  2. # SPDX-License-Identifier: MIT
  3. #
  4. from oeqa.selftest.case import OESelftestTestCase
  5. from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
  6. import os
  7. import oe
  8. import glob
  9. import re
  10. import shutil
  11. import tempfile
  12. from contextlib import contextmanager
  13. from oeqa.utils.ftools import write_file
  14. class Signing(OESelftestTestCase):
  15. gpg_dir = ""
  16. pub_key_path = ""
  17. secret_key_path = ""
  18. def setup_gpg(self):
  19. bitbake('gnupg-native -c addto_recipe_sysroot')
  20. self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-")
  21. self.track_for_cleanup(self.gpg_dir)
  22. self.pub_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.pub")
  23. self.secret_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.secret")
  24. nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native")
  25. runCmd('gpg --agent-program=`which gpg-agent`\|--auto-expand-secmem --batch --homedir %s --import %s %s' % (self.gpg_dir, self.pub_key_path, self.secret_key_path), native_sysroot=nsysroot)
  26. return nsysroot + get_bb_var("bindir_native")
  27. @contextmanager
  28. def create_new_builddir(self, builddir, newbuilddir):
  29. bb.utils.mkdirhier(newbuilddir)
  30. oe.path.copytree(builddir + "/conf", newbuilddir + "/conf")
  31. oe.path.copytree(builddir + "/cache", newbuilddir + "/cache")
  32. origenv = os.environ.copy()
  33. for e in os.environ:
  34. if builddir + "/" in os.environ[e]:
  35. os.environ[e] = os.environ[e].replace(builddir + "/", newbuilddir + "/")
  36. if os.environ[e].endswith(builddir):
  37. os.environ[e] = os.environ[e].replace(builddir, newbuilddir)
  38. os.chdir(newbuilddir)
  39. try:
  40. yield
  41. finally:
  42. for e in origenv:
  43. os.environ[e] = origenv[e]
  44. os.chdir(builddir)
  45. def test_signing_packages(self):
  46. """
  47. Summary: Test that packages can be signed in the package feed
  48. Expected: Package should be signed with the correct key
  49. Expected: Images can be created from signed packages
  50. Product: oe-core
  51. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  52. Author: Alexander Kanavin <alex.kanavin@gmail.com>
  53. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  54. """
  55. import oe.packagedata
  56. self.setup_gpg()
  57. package_classes = get_bb_var('PACKAGE_CLASSES')
  58. if 'package_rpm' not in package_classes:
  59. self.skipTest('This test requires RPM Packaging.')
  60. test_recipe = 'ed'
  61. feature = 'INHERIT += "sign_rpm"\n'
  62. feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
  63. feature += 'RPM_GPG_NAME = "testuser"\n'
  64. feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
  65. self.write_config(feature)
  66. bitbake('-c clean %s' % test_recipe)
  67. bitbake('-f -c package_write_rpm %s' % test_recipe)
  68. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  69. needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE']
  70. bb_vars = get_bb_vars(needed_vars, test_recipe)
  71. pkgdatadir = bb_vars['PKGDATA_DIR']
  72. pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
  73. if 'PKGE' in pkgdata:
  74. pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
  75. else:
  76. pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
  77. deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM']
  78. package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_')
  79. staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE']
  80. pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
  81. # Use a temporary rpmdb
  82. rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
  83. runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' %
  84. (staging_bindir_native, rpmdb, self.pub_key_path))
  85. ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' %
  86. (staging_bindir_native, rpmdb, pkg_deploy))
  87. # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
  88. self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.')
  89. shutil.rmtree(rpmdb)
  90. #Check that an image can be built from signed packages
  91. self.add_command_to_tearDown('bitbake -c clean core-image-minimal')
  92. bitbake('-c clean core-image-minimal')
  93. bitbake('core-image-minimal')
  94. def test_signing_sstate_archive(self):
  95. """
  96. Summary: Test that sstate archives can be signed
  97. Expected: Package should be signed with the correct key
  98. Product: oe-core
  99. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  100. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  101. """
  102. test_recipe = 'ed'
  103. # Since we need gpg but we can't use gpg-native for sstate signatures, we
  104. # build gpg-native in our original builddir then run the tests in a second one.
  105. builddir = os.environ.get('BUILDDIR') + "-testsign"
  106. sstatedir = os.path.join(builddir, 'test-sstate')
  107. nsysroot = self.setup_gpg()
  108. feature = 'SSTATE_SIG_KEY ?= "testuser"\n'
  109. feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
  110. feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
  111. feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
  112. feature += 'SSTATE_DIR = "%s"\n' % sstatedir
  113. # Any mirror might have partial sstate without .sig files, triggering failures
  114. feature += 'SSTATE_MIRRORS_forcevariable = ""\n'
  115. self.write_config(feature)
  116. with self.create_new_builddir(os.environ['BUILDDIR'], builddir):
  117. os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"]
  118. self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
  119. self.add_command_to_tearDown('rm -rf %s' % sstatedir)
  120. self.add_command_to_tearDown('rm -rf %s' % builddir)
  121. bitbake('-c clean %s' % test_recipe)
  122. bitbake('-c populate_lic %s' % test_recipe)
  123. recipe_sig = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz.sig')
  124. recipe_tgz = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz')
  125. self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
  126. self.assertEqual(len(recipe_tgz), 1, 'Failed to find .tgz file.')
  127. ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_tgz[0]))
  128. # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
  129. # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
  130. self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
  131. class LockedSignatures(OESelftestTestCase):
  132. def test_locked_signatures(self):
  133. """
  134. Summary: Test locked signature mechanism
  135. Expected: Locked signatures will prevent task to run
  136. Product: oe-core
  137. Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  138. AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
  139. """
  140. import uuid
  141. test_recipe = 'ed'
  142. locked_sigs_file = 'locked-sigs.inc'
  143. self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
  144. bitbake(test_recipe)
  145. # Generate locked sigs include file
  146. bitbake('-S none %s' % test_recipe)
  147. feature = 'require %s\n' % locked_sigs_file
  148. feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
  149. self.write_config(feature)
  150. # Build a locked recipe
  151. bitbake(test_recipe)
  152. # Make a change that should cause the locked task signature to change
  153. # Use uuid so hash equivalance server isn't triggered
  154. recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
  155. recipe_append_path = os.path.join(self.testlayer_path, 'recipes-test', test_recipe, recipe_append_file)
  156. feature = 'SUMMARY_${PN} = "test locked signature%s"\n' % uuid.uuid4()
  157. os.mkdir(os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
  158. write_file(recipe_append_path, feature)
  159. self.add_command_to_tearDown('rm -rf %s' % os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
  160. # Build the recipe again
  161. ret = bitbake(test_recipe)
  162. # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
  163. patt = r'The %s:do_package sig is computed to be \S+, but the sig is locked to \S+ in SIGGEN_LOCKEDSIGS\S+' % test_recipe
  164. found_warn = re.search(patt, ret.output)
  165. self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)