blob: b390f37d8e66b4665bb65e66e64dcd7ef1a1cc06 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005from oeqa.selftest.case import OESelftestTestCase
6from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_bb_vars
7import os
Brad Bishopf86d0552018-12-04 14:18:15 -08008import oe
Brad Bishopd7bf8c12018-02-25 22:55:05 -05009import glob
10import re
11import shutil
12import tempfile
Brad Bishopf86d0552018-12-04 14:18:15 -080013from contextlib import contextmanager
Brad Bishopd7bf8c12018-02-25 22:55:05 -050014from oeqa.utils.ftools import write_file
15
16
17class Signing(OESelftestTestCase):
18
19 gpg_dir = ""
20 pub_key_path = ""
21 secret_key_path = ""
22
Brad Bishopf86d0552018-12-04 14:18:15 -080023 def setup_gpg(self):
24 bitbake('gnupg-native -c addto_recipe_sysroot')
Brad Bishopd7bf8c12018-02-25 22:55:05 -050025
Brad Bishopf86d0552018-12-04 14:18:15 -080026 self.gpg_dir = tempfile.mkdtemp(prefix="oeqa-signing-")
27 self.track_for_cleanup(self.gpg_dir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028
Brad Bishopf86d0552018-12-04 14:18:15 -080029 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 Bishopd7bf8c12018-02-25 22:55:05 -050031
Brad Bishopf86d0552018-12-04 14:18:15 -080032 nsysroot = get_bb_var("RECIPE_SYSROOT_NATIVE", "gnupg-native")
Brad Bishop15ae2502019-06-18 21:44:24 -040033
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 Bishopf86d0552018-12-04 14:18:15 -080035 return nsysroot + get_bb_var("bindir_native")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050036
Brad Bishopf86d0552018-12-04 14:18:15 -080037
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:
47 if builddir in os.environ[e]:
48 os.environ[e] = os.environ[e].replace(builddir, newbuilddir)
49
50 os.chdir(newbuilddir)
51 try:
52 yield
53 finally:
54 for e in origenv:
55 os.environ[e] = origenv[e]
56 os.chdir(builddir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 def test_signing_packages(self):
59 """
60 Summary: Test that packages can be signed in the package feed
61 Expected: Package should be signed with the correct key
62 Expected: Images can be created from signed packages
63 Product: oe-core
64 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080065 Author: Alexander Kanavin <alex.kanavin@gmail.com>
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
67 """
68 import oe.packagedata
69
Brad Bishopf86d0552018-12-04 14:18:15 -080070 self.setup_gpg()
71
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072 package_classes = get_bb_var('PACKAGE_CLASSES')
73 if 'package_rpm' not in package_classes:
74 self.skipTest('This test requires RPM Packaging.')
75
76 test_recipe = 'ed'
77
78 feature = 'INHERIT += "sign_rpm"\n'
79 feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
80 feature += 'RPM_GPG_NAME = "testuser"\n'
81 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
82
83 self.write_config(feature)
84
85 bitbake('-c clean %s' % test_recipe)
86 bitbake('-f -c package_write_rpm %s' % test_recipe)
87
88 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
89
90 needed_vars = ['PKGDATA_DIR', 'DEPLOY_DIR_RPM', 'PACKAGE_ARCH', 'STAGING_BINDIR_NATIVE']
91 bb_vars = get_bb_vars(needed_vars, test_recipe)
92 pkgdatadir = bb_vars['PKGDATA_DIR']
93 pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
94 if 'PKGE' in pkgdata:
95 pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
96 else:
97 pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
98 deploy_dir_rpm = bb_vars['DEPLOY_DIR_RPM']
99 package_arch = bb_vars['PACKAGE_ARCH'].replace('-', '_')
100 staging_bindir_native = bb_vars['STAGING_BINDIR_NATIVE']
101
102 pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
103
104 # Use a temporary rpmdb
105 rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
106
107 runCmd('%s/rpmkeys --define "_dbpath %s" --import %s' %
108 (staging_bindir_native, rpmdb, self.pub_key_path))
109
110 ret = runCmd('%s/rpmkeys --define "_dbpath %s" --checksig %s' %
111 (staging_bindir_native, rpmdb, pkg_deploy))
112 # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
Brad Bishop316dfdd2018-06-25 12:45:53 -0400113 self.assertIn('digests signatures OK', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114 shutil.rmtree(rpmdb)
115
116 #Check that an image can be built from signed packages
117 self.add_command_to_tearDown('bitbake -c clean core-image-minimal')
118 bitbake('-c clean core-image-minimal')
119 bitbake('core-image-minimal')
120
121
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 def test_signing_sstate_archive(self):
123 """
124 Summary: Test that sstate archives can be signed
125 Expected: Package should be signed with the correct key
126 Product: oe-core
127 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
128 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
129 """
130
131 test_recipe = 'ed'
132
Brad Bishopf86d0552018-12-04 14:18:15 -0800133 # Since we need gpg but we can't use gpg-native for sstate signatures, we
134 # build gpg-native in our original builddir then run the tests in a second one.
135 builddir = os.environ.get('BUILDDIR') + "-testsign"
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136 sstatedir = os.path.join(builddir, 'test-sstate')
137
Brad Bishopf86d0552018-12-04 14:18:15 -0800138 nsysroot = self.setup_gpg()
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139
140 feature = 'SSTATE_SIG_KEY ?= "testuser"\n'
141 feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
142 feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
143 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
144 feature += 'SSTATE_DIR = "%s"\n' % sstatedir
145 # Any mirror might have partial sstate without .sig files, triggering failures
146 feature += 'SSTATE_MIRRORS_forcevariable = ""\n'
147
148 self.write_config(feature)
149
Brad Bishopf86d0552018-12-04 14:18:15 -0800150 with self.create_new_builddir(os.environ['BUILDDIR'], builddir):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500151
Brad Bishopf86d0552018-12-04 14:18:15 -0800152 os.environ["PATH"] = nsysroot + ":" + os.environ["PATH"]
153 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
154 self.add_command_to_tearDown('rm -rf %s' % sstatedir)
155 self.add_command_to_tearDown('rm -rf %s' % builddir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156
Brad Bishopf86d0552018-12-04 14:18:15 -0800157 bitbake('-c clean %s' % test_recipe)
158 bitbake('-c populate_lic %s' % test_recipe)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500159
Brad Bishopf86d0552018-12-04 14:18:15 -0800160 recipe_sig = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz.sig')
161 recipe_tgz = glob.glob(sstatedir + '/*/*:ed:*_populate_lic.tgz')
162
163 self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
164 self.assertEqual(len(recipe_tgz), 1, 'Failed to find .tgz file.')
165
166 ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_tgz[0]))
167 # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
168 # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
169 self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500170
171
172class LockedSignatures(OESelftestTestCase):
173
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500174 def test_locked_signatures(self):
175 """
176 Summary: Test locked signature mechanism
177 Expected: Locked signatures will prevent task to run
178 Product: oe-core
179 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
180 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
181 """
182
183 test_recipe = 'ed'
184 locked_sigs_file = 'locked-sigs.inc'
185
186 self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
187
188 bitbake(test_recipe)
189 # Generate locked sigs include file
190 bitbake('-S none %s' % test_recipe)
191
192 feature = 'require %s\n' % locked_sigs_file
193 feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
194 self.write_config(feature)
195
196 # Build a locked recipe
197 bitbake(test_recipe)
198
199 # Make a change that should cause the locked task signature to change
200 recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
201 recipe_append_path = os.path.join(self.testlayer_path, 'recipes-test', test_recipe, recipe_append_file)
202 feature = 'SUMMARY += "test locked signature"\n'
203
204 os.mkdir(os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
205 write_file(recipe_append_path, feature)
206
207 self.add_command_to_tearDown('rm -rf %s' % os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
208
209 # Build the recipe again
210 ret = bitbake(test_recipe)
211
212 # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
213 patt = r'WARNING: The %s:do_package sig is computed to be \S+, but the sig is locked to \S+ in SIGGEN_LOCKEDSIGS\S+' % test_recipe
214 found_warn = re.search(patt, ret.output)
215
216 self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)