| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | #!/usr/bin/env python3# Read a manifest.yaml file, and for each listed node, execute it with# the --gen option to generate (or load if they already exist) a# private/public key pair.  It will output the public key to stdout,# which we capture.  At the end, output all of the public keys to a# pubkeys.yaml file.# Usage: getpubkeys [-m manifest.yaml] [-p pubkeys.yaml]import argparseimport reimport shleximport subprocessimport sysimport yaml# The default input fileMANIFEST = "manifest.yaml"# The default output filePUBKEYS = "pubkeys.yaml"# The TEEMS binaryTEEMS = "./teems"def getkey(node, manifestdata):    """Get the public key for a particular node (as a 128-character hex    string).  The passed manifestdata is the dictionary corresponding to    that node in the manifest."""    cmdline = ''    if 'launchprefix' in manifestdata:        cmdline = manifestdata['launchprefix'] + ' '    cmdline += TEEMS + " --gen %(sprvfile)s %(pubfile)s" % manifestdata    pubkey = None    try:        print("Fetching pubkey for", node)        out = subprocess.run(shlex.split(cmdline), stdout=subprocess.PIPE) \            .stdout.decode('utf-8')        res = re.search('^Pubkey: ([0-9a-f]{128})', out, re.MULTILINE)        if res:            pubkey = res.group(1)        else:            print(res)    except:        # Couldn't find the output        pass    return pubkeyif __name__ == "__main__":    aparse = argparse.ArgumentParser(        description='Create a TEEMS pubkeys.yaml file from a manifest.yaml file'    )    aparse.add_argument('-m', default=MANIFEST,        help='manifest.yaml input file')    aparse.add_argument('-p', default=PUBKEYS,        help='pubkeys.yaml output file')    args = aparse.parse_args()    # A dictionary to store the output pubkeys    pubkeys = {}    # Read the manifest    with open(args.m) as mf:        manifest = yaml.safe_load(mf)        for (node, data) in manifest.items():            if node != "params":                key = getkey(node, data)                if key is not None:                    pubkeys[node] = key    print('')    print(yaml.dump(pubkeys))    with open(args.p, 'w') as pf:        yaml.dump(pubkeys, pf)
 |