Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
| 2 | # SPDX-License-Identifier: MIT |
| 3 | # |
| 4 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 5 | from oeqa.selftest.case import OESelftestTestCase |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 6 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, create_temp_layer |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | import os |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 8 | import oe |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 9 | import glob |
| 10 | import re |
| 11 | import shutil |
| 12 | import tempfile |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 13 | from contextlib import contextmanager |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 14 | from oeqa.utils.ftools import write_file |
| 15 | |
| 16 | |
| 17 | class Signing(OESelftestTestCase): |
| 18 | |
| 19 | gpg_dir = "" |
| 20 | pub_key_path = "" |
| 21 | secret_key_path = "" |
| 22 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 23 | def setup_gpg(self): |
| 24 | bitbake('gnupg-native -c addto_recipe_sysroot') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 25 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 26 | self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-") |
| 27 | self.track_for_cleanup(self.gpg_dir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 28 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 29 | self.pub_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.pub") |
| 30 | self.secret_key_path = os.path.join(self.testlayer_path, 'files', 'signing', "key.secret") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 31 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 32 | nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native") |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 33 | |
| 34 | 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) |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 35 | return nsysroot + get_bb_var("bindir_native") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 36 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 37 | |
| 38 | @contextmanager |
| 39 | def create_new_builddir(self, builddir, newbuilddir): |
| 40 | bb.utils.mkdirhier(newbuilddir) |
| 41 | oe.path.copytree(builddir + "/conf", newbuilddir + "/conf") |
| 42 | oe.path.copytree(builddir + "/cache", newbuilddir + "/cache") |
| 43 | |
| 44 | origenv = os.environ.copy() |
| 45 | |
| 46 | for e in os.environ: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 47 | if builddir + "/" in os.environ[e]: |
| 48 | os.environ[e] = os.environ[e].replace(builddir + "/", newbuilddir + "/") |
| 49 | if os.environ[e].endswith(builddir): |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 50 | os.environ[e] = os.environ[e].replace(builddir, newbuilddir) |
| 51 | |
| 52 | os.chdir(newbuilddir) |
| 53 | try: |
| 54 | yield |
| 55 | finally: |
| 56 | for e in origenv: |
| 57 | os.environ[e] = origenv[e] |
| 58 | os.chdir(builddir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 59 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 60 | def test_signing_packages(self): |
| 61 | """ |
| 62 | Summary: Test that packages can be signed in the package feed |
| 63 | Expected: Package should be signed with the correct key |
| 64 | Expected: Images can be created from signed packages |
| 65 | Product: oe-core |
| 66 | Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 67 | Author: Alexander Kanavin <alex.kanavin@gmail.com> |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 69 | """ |
| 70 | import oe.packagedata |
| 71 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 72 | self.setup_gpg() |
| 73 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 74 | package_classes = get_bb_var('PACKAGE_CLASSES') |
| 75 | if 'package_rpm' not in package_classes: |
| 76 | self.skipTest('This test requires RPM Packaging.') |
| 77 | |
| 78 | test_recipe = 'ed' |
| 79 | |
| 80 | feature = 'INHERIT += "sign_rpm"\n' |
| 81 | feature += 'RPM_GPG_PASSPHRASE = "test123"\n' |
| 82 | feature += 'RPM_GPG_NAME = "testuser"\n' |
| 83 | feature += 'GPG_PATH = "%s"\n' % self.gpg_dir |
| 84 | |
| 85 | self.write_config(feature) |
| 86 | |
| 87 | bitbake('-c clean %s' % test_recipe) |
| 88 | bitbake('-f -c package_write_rpm %s' % test_recipe) |
| 89 | |
| 90 | self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe) |
| 91 | |
| 92 | needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE'] |
| 93 | bb_vars = get_bb_vars(needed_vars, test_recipe) |
| 94 | pkgdatadir = bb_vars['PKGDATA_DIR'] |
| 95 | pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed") |
| 96 | if 'PKGE' in pkgdata: |
| 97 | pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR'] |
| 98 | else: |
| 99 | pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR'] |
| 100 | deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM'] |
| 101 | package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_') |
| 102 | staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE'] |
| 103 | |
| 104 | pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm'))) |
| 105 | |
| 106 | # Use a temporary rpmdb |
| 107 | rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb') |
| 108 | |
| 109 | runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' % |
| 110 | (staging_bindir_native, rpmdb, self.pub_key_path)) |
| 111 | |
| 112 | ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' % |
| 113 | (staging_bindir_native, rpmdb, pkg_deploy)) |
| 114 | # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 115 | self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 116 | shutil.rmtree(rpmdb) |
| 117 | |
| 118 | #Check that an image can be built from signed packages |
| 119 | self.add_command_to_tearDown('bitbake -c clean core-image-minimal') |
| 120 | bitbake('-c clean core-image-minimal') |
| 121 | bitbake('core-image-minimal') |
| 122 | |
| 123 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 124 | def test_signing_sstate_archive(self): |
| 125 | """ |
| 126 | Summary: Test that sstate archives can be signed |
| 127 | Expected: Package should be signed with the correct key |
| 128 | Product: oe-core |
| 129 | Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 130 | AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 131 | """ |
| 132 | |
| 133 | test_recipe = 'ed' |
| 134 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 135 | # Since we need gpg but we can't use gpg-native for sstate signatures, we |
| 136 | # build gpg-native in our original builddir then run the tests in a second one. |
| 137 | builddir = os.environ.get('BUILDDIR') + "-testsign" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 138 | sstatedir = os.path.join(builddir, 'test-sstate') |
| 139 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 140 | nsysroot = self.setup_gpg() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 141 | |
| 142 | feature = 'SSTATE_SIG_KEY ?= "testuser"\n' |
| 143 | feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n' |
| 144 | feature += 'SSTATE_VERIFY_SIG ?= "1"\n' |
| 145 | feature += 'GPG_PATH = "%s"\n' % self.gpg_dir |
| 146 | feature += 'SSTATE_DIR = "%s"\n' % sstatedir |
| 147 | # Any mirror might have partial sstate without .sig files, triggering failures |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 148 | feature += 'SSTATE_MIRRORS:forcevariable = ""\n' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 149 | |
| 150 | self.write_config(feature) |
| 151 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 152 | with self.create_new_builddir(os.environ['BUILDDIR'], builddir): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 153 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 154 | os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"] |
| 155 | self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe) |
| 156 | self.add_command_to_tearDown('rm -rf %s' % sstatedir) |
| 157 | self.add_command_to_tearDown('rm -rf %s' % builddir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 158 | |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 159 | bitbake('-c clean %s' % test_recipe) |
| 160 | bitbake('-c populate_lic %s' % test_recipe) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 161 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 162 | recipe_sig = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tgz.sig') |
| 163 | recipe_tgz = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tgz') |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 164 | |
| 165 | self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.') |
| 166 | self.assertEqual(len(recipe_tgz), 1, 'Failed to find .tgz file.') |
| 167 | |
| 168 | ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_tgz[0])) |
| 169 | # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30 |
| 170 | # gpg: Good signature from "testuser (nocomment) <testuser@email.com>" |
| 171 | self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.') |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 172 | |
| 173 | |
| 174 | class LockedSignatures(OESelftestTestCase): |
| 175 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 176 | def test_locked_signatures(self): |
| 177 | """ |
| 178 | Summary: Test locked signature mechanism |
| 179 | Expected: Locked signatures will prevent task to run |
| 180 | Product: oe-core |
| 181 | Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 182 | AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com> |
| 183 | """ |
| 184 | |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 185 | import uuid |
| 186 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 187 | test_recipe = 'ed' |
| 188 | locked_sigs_file = 'locked-sigs.inc' |
| 189 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 190 | bitbake(test_recipe) |
| 191 | # Generate locked sigs include file |
| 192 | bitbake('-S none %s' % test_recipe) |
| 193 | |
| 194 | feature = 'require %s\n' % locked_sigs_file |
| 195 | feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n' |
| 196 | self.write_config(feature) |
| 197 | |
| 198 | # Build a locked recipe |
| 199 | bitbake(test_recipe) |
| 200 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 201 | templayerdir = tempfile.mkdtemp(prefix='signingqa') |
| 202 | create_temp_layer(templayerdir, 'selftestsigning') |
| 203 | runCmd('bitbake-layers add-layer %s' % templayerdir) |
| 204 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | # Make a change that should cause the locked task signature to change |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 206 | # Use uuid so hash equivalance server isn't triggered |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 207 | recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend' |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 208 | recipe_append_path = os.path.join(templayerdir, 'recipes-test', test_recipe, recipe_append_file) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 209 | feature = 'SUMMARY:${PN} = "test locked signature%s"\n' % uuid.uuid4() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 210 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 211 | os.mkdir(os.path.join(templayerdir, 'recipes-test')) |
| 212 | os.mkdir(os.path.join(templayerdir, 'recipes-test', test_recipe)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 213 | write_file(recipe_append_path, feature) |
| 214 | |
Brad Bishop | 1d80a2e | 2019-11-15 16:35:03 -0500 | [diff] [blame] | 215 | self.add_command_to_tearDown('bitbake-layers remove-layer %s' % templayerdir) |
| 216 | self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file)) |
| 217 | self.add_command_to_tearDown('rm -rf %s' % templayerdir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 218 | |
| 219 | # Build the recipe again |
| 220 | ret = bitbake(test_recipe) |
| 221 | |
| 222 | # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked) |
Brad Bishop | 00e122a | 2019-10-05 11:10:57 -0400 | [diff] [blame] | 223 | 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 |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 224 | found_warn = re.search(patt, ret.output) |
| 225 | |
| 226 | self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output) |