getpubkeys 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # Read a manifest.yaml file, and for each listed node, execute it with
  3. # the --gen option to generate (or load if they already exist) a
  4. # private/public key pair. It will output the public key to stdout,
  5. # which we capture. At the end, output all of the public keys to a
  6. # pubkeys.yaml file.
  7. # Usage: getpubkeys [-m manifest.yaml] [-p pubkeys.yaml]
  8. import argparse
  9. import re
  10. import shlex
  11. import subprocess
  12. import sys
  13. import yaml
  14. # The default input file
  15. MANIFEST = "manifest.yaml"
  16. # The default output file
  17. PUBKEYS = "pubkeys.yaml"
  18. # The TEEMS binary
  19. TEEMS = "./teems"
  20. def getkey(node, manifestdata):
  21. """Get the public key for a particular node (as a 128-character hex
  22. string). The passed manifestdata is the dictionary corresponding to
  23. that node in the manifest."""
  24. cmdline = ''
  25. if 'launchprefix' in manifestdata:
  26. cmdline = manifestdata['launchprefix'] + ' '
  27. cmdline += TEEMS + " --gen %(sprvfile)s %(pubfile)s" % manifestdata
  28. pubkey = None
  29. try:
  30. print("Fetching pubkey for", node)
  31. out = subprocess.run(shlex.split(cmdline), stdout=subprocess.PIPE) \
  32. .stdout.decode('utf-8')
  33. res = re.search('^Pubkey: ([0-9a-f]{128})', out, re.MULTILINE)
  34. if res:
  35. pubkey = res.group(1)
  36. else:
  37. print(res)
  38. except:
  39. # Couldn't find the output
  40. pass
  41. return pubkey
  42. if __name__ == "__main__":
  43. aparse = argparse.ArgumentParser(
  44. description='Create a TEEMS pubkeys.yaml file from a manifest.yaml file'
  45. )
  46. aparse.add_argument('-m', default=MANIFEST,
  47. help='manifest.yaml input file')
  48. aparse.add_argument('-p', default=PUBKEYS,
  49. help='pubkeys.yaml output file')
  50. args = aparse.parse_args()
  51. # A dictionary to store the output pubkeys
  52. pubkeys = {}
  53. # Read the manifest
  54. with open(args.m) as mf:
  55. manifest = yaml.safe_load(mf)
  56. for (node, data) in manifest.items():
  57. if node != "params":
  58. key = getkey(node, data)
  59. if key is not None:
  60. pubkeys[node] = key
  61. print('')
  62. print(yaml.dump(pubkeys))
  63. with open(args.p, 'w') as pf:
  64. yaml.dump(pubkeys, pf)