blob: 9cc88f020c1d56aeae52db1b68b2bfa8351ac035 [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001"""Helper module for GPG signing"""
2import os
3
4import bb
5import oe.utils
6
7class LocalSigner(object):
8 """Class for handling local (on the build host) signing"""
9 def __init__(self, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010 self.gpg_bin = d.getVar('GPG_BIN') or \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050011 bb.utils.which(os.getenv('PATH'), 'gpg')
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012 self.gpg_path = d.getVar('GPG_PATH')
Brad Bishop37a0e4d2017-12-04 01:01:44 -050013 self.gpg_version = self.get_gpg_version()
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014 self.rpm_bin = bb.utils.which(os.getenv('PATH'), "rpmsign")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050015
16 def export_pubkey(self, output_file, keyid, armor=True):
17 """Export GPG public key to a file"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018 cmd = '%s --no-permission-warning --batch --yes --export -o %s ' % \
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050019 (self.gpg_bin, output_file)
20 if self.gpg_path:
21 cmd += "--homedir %s " % self.gpg_path
22 if armor:
23 cmd += "--armor "
24 cmd += keyid
25 status, output = oe.utils.getstatusoutput(cmd)
26 if status:
27 raise bb.build.FuncFailed('Failed to export gpg public key (%s): %s' %
28 (keyid, output))
29
Brad Bishopd7bf8c12018-02-25 22:55:05 -050030 def sign_rpms(self, files, keyid, passphrase, digest, sign_chunk, fsk=None, fsk_password=None):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031 """Sign RPM files"""
32
33 cmd = self.rpm_bin + " --addsign --define '_gpg_name %s' " % keyid
Brad Bishopd7bf8c12018-02-25 22:55:05 -050034 gpg_args = '--no-permission-warning --batch --passphrase=%s' % passphrase
Brad Bishop37a0e4d2017-12-04 01:01:44 -050035 if self.gpg_version > (2,1,):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 gpg_args += ' --pinentry-mode=loopback'
37 cmd += "--define '_gpg_sign_cmd_extra_args %s' " % gpg_args
Brad Bishopd7bf8c12018-02-25 22:55:05 -050038 cmd += "--define '_binary_filedigest_algorithm %s' " % digest
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039 if self.gpg_bin:
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 cmd += "--define '__gpg %s' " % self.gpg_bin
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050041 if self.gpg_path:
42 cmd += "--define '_gpg_path %s' " % self.gpg_path
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 if fsk:
44 cmd += "--signfiles --fskpath %s " % fsk
45 if fsk_password:
46 cmd += "--define '_file_signing_key_password %s' " % fsk_password
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048 # Sign in chunks
49 for i in range(0, len(files), sign_chunk):
50 status, output = oe.utils.getstatusoutput(cmd + ' '.join(files[i:i+sign_chunk]))
Brad Bishop37a0e4d2017-12-04 01:01:44 -050051 if status:
52 raise bb.build.FuncFailed("Failed to sign RPM packages: %s" % output)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053
54 def detach_sign(self, input_file, keyid, passphrase_file, passphrase=None, armor=True):
55 """Create a detached signature of a file"""
56 import subprocess
57
58 if passphrase_file and passphrase:
59 raise Exception("You should use either passphrase_file of passphrase, not both")
60
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 cmd = [self.gpg_bin, '--detach-sign', '--no-permission-warning', '--batch',
62 '--no-tty', '--yes', '--passphrase-fd', '0', '-u', keyid]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050063
64 if self.gpg_path:
65 cmd += ['--homedir', self.gpg_path]
66 if armor:
67 cmd += ['--armor']
68
69 #gpg > 2.1 supports password pipes only through the loopback interface
70 #gpg < 2.1 errors out if given unknown parameters
Brad Bishop37a0e4d2017-12-04 01:01:44 -050071 if self.gpg_version > (2,1,):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050072 cmd += ['--pinentry-mode', 'loopback']
73
74 cmd += [input_file]
75
76 try:
77 if passphrase_file:
78 with open(passphrase_file) as fobj:
79 passphrase = fobj.readline();
80
81 job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060082 (_, stderr) = job.communicate(passphrase.encode("utf-8"))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050083
84 if job.returncode:
85 raise bb.build.FuncFailed("GPG exited with code %d: %s" %
Patrick Williamsc0f7c042017-02-23 20:41:17 -060086 (job.returncode, stderr.decode("utf-8")))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087
88 except IOError as e:
89 bb.error("IO error (%s): %s" % (e.errno, e.strerror))
90 raise Exception("Failed to sign '%s'" % input_file)
91
92 except OSError as e:
93 bb.error("OS error (%s): %s" % (e.errno, e.strerror))
94 raise Exception("Failed to sign '%s" % input_file)
95
96
97 def get_gpg_version(self):
Brad Bishop37a0e4d2017-12-04 01:01:44 -050098 """Return the gpg version as a tuple of ints"""
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050099 import subprocess
100 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 ver_str = subprocess.check_output((self.gpg_bin, "--version", "--no-permission-warning")).split()[2].decode("utf-8")
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500102 return tuple([int(i) for i in ver_str.split('.')])
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500103 except subprocess.CalledProcessError as e:
104 raise bb.build.FuncFailed("Could not get gpg version: %s" % e)
105
106
107 def verify(self, sig_file):
108 """Verify signature"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109 cmd = self.gpg_bin + " --verify --no-permission-warning "
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500110 if self.gpg_path:
111 cmd += "--homedir %s " % self.gpg_path
112 cmd += sig_file
113 status, _ = oe.utils.getstatusoutput(cmd)
114 ret = False if status else True
115 return ret
116
117
118def get_signer(d, backend):
119 """Get signer object for the specified backend"""
120 # Use local signing by default
121 if backend == 'local':
122 return LocalSigner(d)
123 else:
124 bb.fatal("Unsupported signing backend '%s'" % backend)