#!/usr/bin/python3 # # Author: Matthias Gerstner # # All of this source code is licensed under: # # ISC License # # Copyright (c) 2021, SUSE LLC # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. import argparse import base64 import datetime import json import pprint import sys import urllib.request from urllib.request import urlopen from cryptography.hazmat.primitives import serialization, hashes from cryptography.hazmat.primitives.ciphers import (Cipher, algorithms, modes) from cryptography.hazmat.backends import default_backend import cryptography.hazmat.primitives.asymmetric.padding cliparser = argparse.ArgumentParser() cliparser.add_argument("--host", type=str, help="hostname where to reach the keylime_agent web server", required=True) cliparser.add_argument("-p", "--port", type=int, default=8890, help="Port where to contact the keylime_agent web server on --host") cliparser.add_argument("--log-line", help="Log line to inject in registrar service", default=None) cliparser.add_argument("--agent-id", help="agent UUID to register", default=None) cliargs = cliparser.parse_args() if not cliargs.log_line and not cliargs.agent_id: print("Pass --log-line or --agent-id", file=sys.stderr) sys.exit(1) elif cliargs.log_line and cliargs.agent_id: print("Only pass one: --log-line or --agent-id", file=sys.stderr) sys.exit(1) AGENT_URI = f"http://{cliargs.host}:{cliargs.port}" API_VERSION = 500 def getURI(pars): ret = f"{AGENT_URI}/?api_version={API_VERSION}" for k, v in pars.items(): k = urllib.parse.quote(k) v = urllib.parse.quote(v) ret += f"&{k}={v}" return ret def postJSON(uri, _json): print("POST", uri) print(_json) try: rq = urllib.request.Request(uri, _json.encode(), method='POST') r = urlopen(rq) res = r.read() except Exception as e: print(f"POST failed: {e}") return res = json.loads(res) if res["code"] != 200: print("POST failed: {}".format(str(res))) return print("POST succeeded:\n") pprint.pprint(res) if cliargs.log_line: now = datetime.datetime.now() ts = now.strftime("%Y-%m-%d %H:%M:%S") agent_id = "trusted-agent" agent_id += f"\n{ts}.931 - keylime.registrar - WARNING - {cliargs.log_line}" agent_id += f"\n{ts}.940 - keylime.registrar - DEBUG -" else: agent_id = cliargs.agent_id uri = getURI({"agents": agent_id}) #ekcert = b"ekcert" aik_tpm = b"aikt\x00\x04\x00\x05\x00\x72" #ek_tpm = b"ek_tpmandmorestuff" #ek_tpm = base64.b64decode(open("mc-s", 'rb').read()) ek_tpm = open("mc-e", 'rb').read() json_post = f'''{{ "ekcert": "emulator", "aik_tpm": "{base64.b64encode(aik_tpm).decode()}", "ek_tpm": "{base64.b64encode(ek_tpm).decode()}", "ip": "127.0.0.2", "auth_tag": "stuff" }}''' postJSON(uri, json_post)