gpg_sign.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """Helper module for GPG signing"""
  2. import os
  3. import bb
  4. import oe.utils
  5. import subprocess
  6. import shlex
  7. class LocalSigner(object):
  8. """Class for handling local (on the build host) signing"""
  9. def __init__(self, d):
  10. self.gpg_bin = d.getVar('GPG_BIN') or \
  11. bb.utils.which(os.getenv('PATH'), 'gpg')
  12. self.gpg_path = d.getVar('GPG_PATH')
  13. self.gpg_version = self.get_gpg_version()
  14. self.rpm_bin = bb.utils.which(os.getenv('PATH'), "rpmsign")
  15. self.gpg_agent_bin = bb.utils.which(os.getenv('PATH'), "gpg-agent")
  16. def export_pubkey(self, output_file, keyid, armor=True):
  17. """Export GPG public key to a file"""
  18. cmd = '%s --no-permission-warning --batch --yes --export -o %s ' % \
  19. (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. subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT)
  26. def sign_rpms(self, files, keyid, passphrase, digest, sign_chunk, fsk=None, fsk_password=None):
  27. """Sign RPM files"""
  28. cmd = self.rpm_bin + " --addsign --define '_gpg_name %s' " % keyid
  29. gpg_args = '--no-permission-warning --batch --passphrase=%s --agent-program=%s|--auto-expand-secmem' % (passphrase, self.gpg_agent_bin)
  30. if self.gpg_version > (2,1,):
  31. gpg_args += ' --pinentry-mode=loopback'
  32. cmd += "--define '_gpg_sign_cmd_extra_args %s' " % gpg_args
  33. cmd += "--define '_binary_filedigest_algorithm %s' " % digest
  34. if self.gpg_bin:
  35. cmd += "--define '__gpg %s' " % self.gpg_bin
  36. if self.gpg_path:
  37. cmd += "--define '_gpg_path %s' " % self.gpg_path
  38. if fsk:
  39. cmd += "--signfiles --fskpath %s " % fsk
  40. if fsk_password:
  41. cmd += "--define '_file_signing_key_password %s' " % fsk_password
  42. # Sign in chunks
  43. for i in range(0, len(files), sign_chunk):
  44. subprocess.check_output(shlex.split(cmd + ' '.join(files[i:i+sign_chunk])), stderr=subprocess.STDOUT)
  45. def detach_sign(self, input_file, keyid, passphrase_file, passphrase=None, armor=True):
  46. """Create a detached signature of a file"""
  47. if passphrase_file and passphrase:
  48. raise Exception("You should use either passphrase_file of passphrase, not both")
  49. cmd = [self.gpg_bin, '--detach-sign', '--no-permission-warning', '--batch',
  50. '--no-tty', '--yes', '--passphrase-fd', '0', '-u', keyid]
  51. if self.gpg_path:
  52. cmd += ['--homedir', self.gpg_path]
  53. if armor:
  54. cmd += ['--armor']
  55. #gpg > 2.1 supports password pipes only through the loopback interface
  56. #gpg < 2.1 errors out if given unknown parameters
  57. if self.gpg_version > (2,1,):
  58. cmd += ['--pinentry-mode', 'loopback']
  59. if self.gpg_agent_bin:
  60. cmd += ["--agent-program=%s|--auto-expand-secmem" % (self.gpg_agent_bin)]
  61. cmd += [input_file]
  62. try:
  63. if passphrase_file:
  64. with open(passphrase_file) as fobj:
  65. passphrase = fobj.readline();
  66. job = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
  67. (_, stderr) = job.communicate(passphrase.encode("utf-8"))
  68. if job.returncode:
  69. raise bb.build.FuncFailed("GPG exited with code %d: %s" %
  70. (job.returncode, stderr.decode("utf-8")))
  71. except IOError as e:
  72. bb.error("IO error (%s): %s" % (e.errno, e.strerror))
  73. raise Exception("Failed to sign '%s'" % input_file)
  74. except OSError as e:
  75. bb.error("OS error (%s): %s" % (e.errno, e.strerror))
  76. raise Exception("Failed to sign '%s" % input_file)
  77. def get_gpg_version(self):
  78. """Return the gpg version as a tuple of ints"""
  79. try:
  80. ver_str = subprocess.check_output((self.gpg_bin, "--version", "--no-permission-warning")).split()[2].decode("utf-8")
  81. return tuple([int(i) for i in ver_str.split("-")[0].split('.')])
  82. except subprocess.CalledProcessError as e:
  83. raise bb.build.FuncFailed("Could not get gpg version: %s" % e)
  84. def verify(self, sig_file):
  85. """Verify signature"""
  86. cmd = self.gpg_bin + " --verify --no-permission-warning "
  87. if self.gpg_path:
  88. cmd += "--homedir %s " % self.gpg_path
  89. cmd += sig_file
  90. status = subprocess.call(shlex.split(cmd))
  91. ret = False if status else True
  92. return ret
  93. def get_signer(d, backend):
  94. """Get signer object for the specified backend"""
  95. # Use local signing by default
  96. if backend == 'local':
  97. return LocalSigner(d)
  98. else:
  99. bb.fatal("Unsupported signing backend '%s'" % backend)