blob: 322e753ed3b9d7a6098caff8c2b7cecd2101ed71 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Brad Bishopd7bf8c12018-02-25 22:55:05 -05007from oeqa.selftest.case import OESelftestTestCase
Brad Bishop1d80a2e2019-11-15 16:35:03 -05008from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars, create_temp_layer
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009import os
Brad Bishopf86d0552018-12-04 14:18:15 -080010import oe
Brad Bishopd7bf8c12018-02-25 22:55:05 -050011import glob
12import re
13import shutil
14import tempfile
Brad Bishopf86d0552018-12-04 14:18:15 -080015from contextlib import contextmanager
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016from oeqa.utils.ftools import write_file
17
18
19class Signing(OESelftestTestCase):
20
21 gpg_dir = ""
22 pub_key_path = ""
23 secret_key_path = ""
24
Brad Bishopf86d0552018-12-04 14:18:15 -080025 def setup_gpg(self):
26 bitbake('gnupg-native -c addto_recipe_sysroot')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050027
Brad Bishopf86d0552018-12-04 14:18:15 -080028 self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-")
29 self.track_for_cleanup(self.gpg_dir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030
Brad Bishopf86d0552018-12-04 14:18:15 -080031 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")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050033
Brad Bishopf86d0552018-12-04 14:18:15 -080034 nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native")
Brad Bishop15ae2502019-06-18 21:44:24 -040035
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)
Brad Bishopf86d0552018-12-04 14:18:15 -080037 return nsysroot + get_bb_var("bindir_native")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050038
Brad Bishopf86d0552018-12-04 14:18:15 -080039
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:
Andrew Geisslerc9f78652020-09-18 14:11:35 -050049 if builddir + "/" in os.environ[e]:
50 os.environ[e] = os.environ[e].replace(builddir + "/", newbuilddir + "/")
51 if os.environ[e].endswith(builddir):
Brad Bishopf86d0552018-12-04 14:18:15 -080052 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)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 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>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080069 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
71 """
72 import oe.packagedata
73
Brad Bishopf86d0552018-12-04 14:18:15 -080074 self.setup_gpg()
75
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 package_classes = get_bb_var('PACKAGE_CLASSES')
77 if 'package_rpm' not in package_classes:
78 self.skipTest('This test requires RPM Packaging.')
79
80 test_recipe = 'ed'
81
82 feature = 'INHERIT += "sign_rpm"\n'
83 feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
84 feature += 'RPM_GPG_NAME = "testuser"\n'
85 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
86
87 self.write_config(feature)
88
89 bitbake('-c clean %s' % test_recipe)
90 bitbake('-f -c package_write_rpm %s' % test_recipe)
91
92 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
93
94 needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE']
95 bb_vars = get_bb_vars(needed_vars, test_recipe)
96 pkgdatadir = bb_vars['PKGDATA_DIR']
97 pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
98 if 'PKGE' in pkgdata:
99 pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
100 else:
101 pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
102 deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM']
103 package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_')
104 staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE']
105
106 pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
107
108 # Use a temporary rpmdb
109 rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
110
111 runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' %
112 (staging_bindir_native, rpmdb, self.pub_key_path))
113
114 ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' %
115 (staging_bindir_native, rpmdb, pkg_deploy))
116 # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
Brad Bishop316dfdd2018-06-25 12:45:53 -0400117 self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 shutil.rmtree(rpmdb)
119
120 #Check that an image can be built from signed packages
121 self.add_command_to_tearDown('bitbake -c clean core-image-minimal')
122 bitbake('-c clean core-image-minimal')
123 bitbake('core-image-minimal')
124
125
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 def test_signing_sstate_archive(self):
127 """
128 Summary: Test that sstate archives can be signed
129 Expected: Package should be signed with the correct key
130 Product: oe-core
131 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
132 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
133 """
134
135 test_recipe = 'ed'
136
Brad Bishopf86d0552018-12-04 14:18:15 -0800137 # Since we need gpg but we can't use gpg-native for sstate signatures, we
138 # build gpg-native in our original builddir then run the tests in a second one.
139 builddir = os.environ.get('BUILDDIR') + "-testsign"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 sstatedir = os.path.join(builddir, 'test-sstate')
141
Brad Bishopf86d0552018-12-04 14:18:15 -0800142 nsysroot = self.setup_gpg()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143
144 feature = 'SSTATE_SIG_KEY ?= "testuser"\n'
145 feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
146 feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
147 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
148 feature += 'SSTATE_DIR = "%s"\n' % sstatedir
149 # Any mirror might have partial sstate without .sig files, triggering failures
Patrick Williams213cb262021-08-07 19:21:33 -0500150 feature += 'SSTATE_MIRRORS:forcevariable = ""\n'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151
152 self.write_config(feature)
153
Brad Bishopf86d0552018-12-04 14:18:15 -0800154 with self.create_new_builddir(os.environ['BUILDDIR'], builddir):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500155
Brad Bishopf86d0552018-12-04 14:18:15 -0800156 os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"]
157 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
158 self.add_command_to_tearDown('rm -rf %s' % sstatedir)
159 self.add_command_to_tearDown('rm -rf %s' % builddir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160
Brad Bishopf86d0552018-12-04 14:18:15 -0800161 bitbake('-c clean %s' % test_recipe)
162 bitbake('-c populate_lic %s' % test_recipe)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163
Andrew Geisslereff27472021-10-29 15:35:00 -0500164 recipe_sig = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tar.zst.sig')
165 recipe_archive = glob.glob(sstatedir + '/*/*/*:ed:*_populate_lic.tar.zst')
Brad Bishopf86d0552018-12-04 14:18:15 -0800166
167 self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
Andrew Geisslereff27472021-10-29 15:35:00 -0500168 self.assertEqual(len(recipe_archive), 1, 'Failed to find .tar.zst file.')
Brad Bishopf86d0552018-12-04 14:18:15 -0800169
Andrew Geisslereff27472021-10-29 15:35:00 -0500170 ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_archive[0]))
Brad Bishopf86d0552018-12-04 14:18:15 -0800171 # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
172 # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
173 self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174
175
176class LockedSignatures(OESelftestTestCase):
177
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500178 def test_locked_signatures(self):
179 """
180 Summary: Test locked signature mechanism
181 Expected: Locked signatures will prevent task to run
182 Product: oe-core
183 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
184 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
185 """
186
Brad Bishop00e122a2019-10-05 11:10:57 -0400187 import uuid
188
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500189 test_recipe = 'ed'
190 locked_sigs_file = 'locked-sigs.inc'
191
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500192 bitbake(test_recipe)
193 # Generate locked sigs include file
194 bitbake('-S none %s' % test_recipe)
195
196 feature = 'require %s\n' % locked_sigs_file
197 feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
198 self.write_config(feature)
199
200 # Build a locked recipe
201 bitbake(test_recipe)
202
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500203 templayerdir = tempfile.mkdtemp(prefix='signingqa')
204 create_temp_layer(templayerdir, 'selftestsigning')
205 runCmd('bitbake-layers add-layer %s' % templayerdir)
206
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500207 # Make a change that should cause the locked task signature to change
Brad Bishop00e122a2019-10-05 11:10:57 -0400208 # Use uuid so hash equivalance server isn't triggered
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500209 recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500210 recipe_append_path = os.path.join(templayerdir, 'recipes-test', test_recipe, recipe_append_file)
Patrick Williams213cb262021-08-07 19:21:33 -0500211 feature = 'SUMMARY:${PN} = "test locked signature%s"\n' % uuid.uuid4()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500212
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500213 os.mkdir(os.path.join(templayerdir, 'recipes-test'))
214 os.mkdir(os.path.join(templayerdir, 'recipes-test', test_recipe))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500215 write_file(recipe_append_path, feature)
216
Brad Bishop1d80a2e2019-11-15 16:35:03 -0500217 self.add_command_to_tearDown('bitbake-layers remove-layer %s' % templayerdir)
218 self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
219 self.add_command_to_tearDown('rm -rf %s' % templayerdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500220
221 # Build the recipe again
222 ret = bitbake(test_recipe)
223
224 # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
Brad Bishop00e122a2019-10-05 11:10:57 -0400225 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 Bishopd7bf8c12018-02-25 22:55:05 -0500226 found_warn = re.search(patt, ret.output)
227
228 self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)