-
Notifications
You must be signed in to change notification settings - Fork 202
/
gramine-sgx-sigstruct-view
executable file
·58 lines (49 loc) · 2.25 KB
/
gramine-sgx-sigstruct-view
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2023 Intel Corporation
# Dmitrii Kuvaiskii <dmitrii.kuvaiskii@intel.com>
import hashlib
import io
import json
import click
import tomli_w
from graminelibos import Sigstruct
@click.command()
@click.argument('sigfile', type=click.File('rb'), required=True)
@click.option('--verbose/--quiet', '-v/-q', help='Display detailed information')
@click.option('--output-format', default='text', type=click.Choice(['text', 'toml', 'json']),
help='Output format: plain text (unstable, should not be parsed), toml or json')
@click.argument('output', metavar='[FILE]', type=click.File('wb'), default='-')
def main(sigfile, verbose, output_format, output):
output_txt = io.TextIOWrapper(output)
sig = Sigstruct.from_bytes(sigfile.read())
sig_readable = {
'mr_signer': hashlib.sha256(sig['modulus']).hexdigest(),
'mr_enclave': sig['enclave_hash'].hex(),
'isv_prod_id': sig['isv_prod_id'],
'isv_svn': sig['isv_svn'],
'attribute_flags': hex(sig['attribute_flags']),
'attribute_xfrms': hex(sig['attribute_xfrms']),
'misc_select': hex(sig['misc_select']),
'attribute_flags_mask': hex(sig['attribute_flags_mask']),
'attribute_xfrm_mask': hex(sig['attribute_xfrm_mask']),
'misc_mask': hex(sig['misc_mask']),
'date': f"{sig['date_year']:04}-{sig['date_month']:02}-{sig['date_day']:02}",
'debug_enclave': bool(sig['attribute_flags'] & 0b10),
}
if not verbose:
keys_to_remove = ('attribute_flags', 'attribute_xfrms', 'misc_select',
'attribute_flags_mask', 'attribute_xfrm_mask', 'misc_mask', 'date')
for key in keys_to_remove:
del sig_readable[key]
if output_format == "toml":
tomli_w.dump(sig_readable, output)
elif output_format == "json":
json.dump(sig_readable, output_txt, indent=4)
else:
# plaintext format imitates the legacy output of `gramine-sgx-get-token` tool
print('Attributes:', file=output_txt)
for key, value in sig_readable.items():
print(f' {key}: {value}', file=output_txt)
if __name__ == '__main__':
main() # pylint: disable=no-value-for-parameter