poky/meta/lib/oe/gpg_sign.py
Ioan-Adrian Ratiu 0b088e0829 gpg_sign: detach_sign: fix gpg > 2.1 STDIN file descriptor
Starting from v2.1 passing passwords directly to gpg does not work
anymore [1], instead a loopback interface must be used otherwise
gpg >2.1 will error out with:
"gpg: signing failed: Inappropriate ioctl for device"

gpg <2.1 does not work with the new --pinentry-mode arg and gives an
invalid option error, so we detect what is the running version of gpg
and pass it accordingly.

[1] https://wiki.archlinux.org/index.php/GnuPG#Unattended_passphrase

(From OE-Core rev: 0413bd8e294ca8ac972ac68662b43a981952f5ae)

Signed-off-by: Ioan-Adrian Ratiu <adrian.ratiu@ni.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
2016-03-11 16:50:45 +00:00

127 lines
4.5 KiB
Python

"""Helper module for GPG signing"""
import os
import bb
import oe.utils
class LocalSigner(object):
"""Class for handling local (on the build host) signing"""
def __init__(self, d):
self.gpg_bin = d.getVar('GPG_BIN', True) or \
bb.utils.which(os.getenv('PATH'), 'gpg')
self.gpg_path = d.getVar('GPG_PATH', True)
self.rpm_bin = bb.utils.which(os.getenv('PATH'), "rpm")
def export_pubkey(self, output_file, keyid):
"""Export GPG public key to a file"""
cmd = '%s --batch --yes --export --armor -o %s ' % \
(self.gpg_bin, output_file)
if self.gpg_path:
cmd += "--homedir %s " % self.gpg_path
cmd += keyid
status, output = oe.utils.getstatusoutput(cmd)
if status:
raise bb.build.FuncFailed('Failed to export gpg public key (%s): %s' %
(keyid, output))
def sign_rpms(self, files, keyid, passphrase_file):
"""Sign RPM files"""
import pexpect
cmd = self.rpm_bin + " --addsign --define '_gpg_name %s' " % keyid
if self.gpg_bin:
cmd += "--define '%%__gpg %s' " % self.gpg_bin
if self.gpg_path:
cmd += "--define '_gpg_path %s' " % self.gpg_path
cmd += ' '.join(files)
# Need to use pexpect for feeding the passphrase
proc = pexpect.spawn(cmd)
try:
proc.expect_exact('Enter pass phrase:', timeout=15)
with open(passphrase_file) as fobj:
proc.sendline(fobj.readline().rstrip('\n'))
proc.expect(pexpect.EOF, timeout=900)
proc.close()
except pexpect.TIMEOUT as err:
bb.error('rpmsign timeout: %s' % err)
proc.terminate()
if os.WEXITSTATUS(proc.status) or not os.WIFEXITED(proc.status):
bb.error('rpmsign failed: %s' % proc.before.strip())
raise bb.build.FuncFailed("Failed to sign RPM packages")
def detach_sign(self, input_file, keyid, passphrase_file, passphrase=None, armor=True):
"""Create a detached signature of a file"""
import subprocess
if passphrase_file and passphrase:
raise Exception("You should use either passphrase_file of passphrase, not both")
cmd = [self.gpg_bin, '--detach-sign', '--batch', '--no-tty', '--yes',
'--passphrase-fd', '0', '-u', keyid]
if self.gpg_path:
cmd += ['--homedir', self.gpg_path]
if armor:
cmd += ['--armor']
#gpg > 2.1 supports password pipes only through the loopback interface
#gpg < 2.1 errors out if given unknown parameters
dots = self.get_gpg_version().split('.')
assert len(dots) >= 2
if int(dots[0]) >= 2 and int(dots[1]) >= 1:
cmd += ['--pinentry-mode', 'loopback']
cmd += [input_file]
try:
if passphrase_file:
with open(passphrase_file) as fobj:
passphrase = fobj.readline();
job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
(_, stderr) = job.communicate(passphrase)
if job.returncode:
raise bb.build.FuncFailed("GPG exited with code %d: %s" %
(job.returncode, stderr))
except IOError as e:
bb.error("IO error (%s): %s" % (e.errno, e.strerror))
raise Exception("Failed to sign '%s'" % input_file)
except OSError as e:
bb.error("OS error (%s): %s" % (e.errno, e.strerror))
raise Exception("Failed to sign '%s" % input_file)
def get_gpg_version(self):
"""Return the gpg version"""
import subprocess
try:
return subprocess.check_output((self.gpg_bin, "--version")).split()[2]
except subprocess.CalledProcessError as e:
raise bb.build.FuncFailed("Could not get gpg version: %s" % e)
def verify(self, sig_file):
"""Verify signature"""
cmd = self.gpg_bin + " --verify "
if self.gpg_path:
cmd += "--homedir %s " % self.gpg_path
cmd += sig_file
status, _ = oe.utils.getstatusoutput(cmd)
ret = False if status else True
return ret
def get_signer(d, backend):
"""Get signer object for the specified backend"""
# Use local signing by default
if backend == 'local':
return LocalSigner(d)
else:
bb.fatal("Unsupported signing backend '%s'" % backend)