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