Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame^] | 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | """Helper module for GPG signing""" |
| 8 | import os |
| 9 | |
| 10 | import bb |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 11 | import subprocess |
| 12 | import shlex |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 13 | |
| 14 | class LocalSigner(object): |
| 15 | """Class for handling local (on the build host) signing""" |
| 16 | def __init__(self, d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 17 | self.gpg_bin = d.getVar('GPG_BIN') or \ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | bb.utils.which(os.getenv('PATH'), 'gpg') |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 19 | self.gpg_cmd = [self.gpg_bin] |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 20 | self.gpg_agent_bin = bb.utils.which(os.getenv('PATH'), "gpg-agent") |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 21 | # Without this we see "Cannot allocate memory" errors when running processes in parallel |
| 22 | # It needs to be set for any gpg command since any agent launched can stick around in memory |
| 23 | # and this parameter must be set. |
| 24 | if self.gpg_agent_bin: |
| 25 | self.gpg_cmd += ["--agent-program=%s|--auto-expand-secmem" % (self.gpg_agent_bin)] |
| 26 | self.gpg_path = d.getVar('GPG_PATH') |
| 27 | self.rpm_bin = bb.utils.which(os.getenv('PATH'), "rpmsign") |
| 28 | self.gpg_version = self.get_gpg_version() |
| 29 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 30 | |
| 31 | def export_pubkey(self, output_file, keyid, armor=True): |
| 32 | """Export GPG public key to a file""" |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 33 | cmd = self.gpg_cmd + ["--no-permission-warning", "--batch", "--yes", "--export", "-o", output_file] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | if self.gpg_path: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 35 | cmd += ["--homedir", self.gpg_path] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 36 | if armor: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 37 | cmd += ["--armor"] |
| 38 | cmd += [keyid] |
| 39 | subprocess.check_output(cmd, stderr=subprocess.STDOUT) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 40 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 41 | def sign_rpms(self, files, keyid, passphrase, digest, sign_chunk, fsk=None, fsk_password=None): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 42 | """Sign RPM files""" |
| 43 | |
| 44 | cmd = self.rpm_bin + " --addsign --define '_gpg_name %s' " % keyid |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 45 | gpg_args = '--no-permission-warning --batch --passphrase=%s --agent-program=%s|--auto-expand-secmem' % (passphrase, self.gpg_agent_bin) |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 46 | if self.gpg_version > (2,1,): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 47 | gpg_args += ' --pinentry-mode=loopback' |
| 48 | cmd += "--define '_gpg_sign_cmd_extra_args %s' " % gpg_args |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 49 | cmd += "--define '_binary_filedigest_algorithm %s' " % digest |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 50 | if self.gpg_bin: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 51 | cmd += "--define '__gpg %s' " % self.gpg_bin |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 52 | if self.gpg_path: |
| 53 | cmd += "--define '_gpg_path %s' " % self.gpg_path |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | if fsk: |
| 55 | cmd += "--signfiles --fskpath %s " % fsk |
| 56 | if fsk_password: |
| 57 | cmd += "--define '_file_signing_key_password %s' " % fsk_password |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 58 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 59 | # Sign in chunks |
| 60 | for i in range(0, len(files), sign_chunk): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 61 | subprocess.check_output(shlex.split(cmd + ' '.join(files[i:i+sign_chunk])), stderr=subprocess.STDOUT) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 62 | |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 63 | def detach_sign(self, input_file, keyid, passphrase_file, passphrase=None, armor=True, output_suffix=None, use_sha256=False): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 64 | """Create a detached signature of a file""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 65 | |
| 66 | if passphrase_file and passphrase: |
| 67 | raise Exception("You should use either passphrase_file of passphrase, not both") |
| 68 | |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 69 | cmd = self.gpg_cmd + ['--detach-sign', '--no-permission-warning', '--batch', |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 70 | '--no-tty', '--yes', '--passphrase-fd', '0', '-u', keyid] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 71 | |
| 72 | if self.gpg_path: |
| 73 | cmd += ['--homedir', self.gpg_path] |
| 74 | if armor: |
| 75 | cmd += ['--armor'] |
Patrick Williams | de0582f | 2022-04-08 10:23:27 -0500 | [diff] [blame] | 76 | if output_suffix: |
| 77 | cmd += ['-o', input_file + "." + output_suffix] |
| 78 | if use_sha256: |
| 79 | cmd += ['--digest-algo', "SHA256"] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 80 | |
| 81 | #gpg > 2.1 supports password pipes only through the loopback interface |
| 82 | #gpg < 2.1 errors out if given unknown parameters |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 83 | if self.gpg_version > (2,1,): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 84 | cmd += ['--pinentry-mode', 'loopback'] |
| 85 | |
| 86 | cmd += [input_file] |
| 87 | |
| 88 | try: |
| 89 | if passphrase_file: |
| 90 | with open(passphrase_file) as fobj: |
| 91 | passphrase = fobj.readline(); |
| 92 | |
| 93 | job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 94 | (_, stderr) = job.communicate(passphrase.encode("utf-8")) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 95 | |
| 96 | if job.returncode: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 97 | bb.fatal("GPG exited with code %d: %s" % (job.returncode, stderr.decode("utf-8"))) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 98 | |
| 99 | except IOError as e: |
| 100 | bb.error("IO error (%s): %s" % (e.errno, e.strerror)) |
| 101 | raise Exception("Failed to sign '%s'" % input_file) |
| 102 | |
| 103 | except OSError as e: |
| 104 | bb.error("OS error (%s): %s" % (e.errno, e.strerror)) |
| 105 | raise Exception("Failed to sign '%s" % input_file) |
| 106 | |
| 107 | |
| 108 | def get_gpg_version(self): |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 109 | """Return the gpg version as a tuple of ints""" |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 110 | try: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 111 | cmd = self.gpg_cmd + ["--version", "--no-permission-warning"] |
| 112 | ver_str = subprocess.check_output(cmd).split()[2].decode("utf-8") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 113 | return tuple([int(i) for i in ver_str.split("-")[0].split('.')]) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 114 | except subprocess.CalledProcessError as e: |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 115 | bb.fatal("Could not get gpg version: %s" % e) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 116 | |
| 117 | |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 118 | def verify(self, sig_file, valid_sigs = ''): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 | """Verify signature""" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 120 | cmd = self.gpg_cmd + ["--verify", "--no-permission-warning", "--status-fd", "1"] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 121 | if self.gpg_path: |
Brad Bishop | 15ae250 | 2019-06-18 21:44:24 -0400 | [diff] [blame] | 122 | cmd += ["--homedir", self.gpg_path] |
| 123 | |
| 124 | cmd += [sig_file] |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 125 | status = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 126 | # Valid if any key matches if unspecified |
| 127 | if not valid_sigs: |
| 128 | ret = False if status.returncode else True |
| 129 | return ret |
| 130 | |
| 131 | import re |
| 132 | goodsigs = [] |
| 133 | sigre = re.compile(r'^\[GNUPG:\] GOODSIG (\S+)\s(.*)$') |
| 134 | for l in status.stdout.decode("utf-8").splitlines(): |
| 135 | s = sigre.match(l) |
| 136 | if s: |
| 137 | goodsigs += [s.group(1)] |
| 138 | |
| 139 | for sig in valid_sigs.split(): |
| 140 | if sig in goodsigs: |
| 141 | return True |
| 142 | if len(goodsigs): |
| 143 | bb.warn('No accepted signatures found. Good signatures found: %s.' % ' '.join(goodsigs)) |
| 144 | return False |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 145 | |
| 146 | |
| 147 | def get_signer(d, backend): |
| 148 | """Get signer object for the specified backend""" |
| 149 | # Use local signing by default |
| 150 | if backend == 'local': |
| 151 | return LocalSigner(d) |
| 152 | else: |
| 153 | bb.fatal("Unsupported signing backend '%s'" % backend) |