blob: 7fa9816e16904237b1b1e723c2fe69e561329e0f [file] [log] [blame]
Mate Toth-Pal51b61982022-03-17 14:19:30 +01001#!/usr/bin/env python3
2#-------------------------------------------------------------------------------
3# Copyright (c) 2019-2022, Arm Limited. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7#-------------------------------------------------------------------------------
8
Mate Toth-Palb9057ff2022-04-29 16:03:21 +02009"""CLI tool for compiling token from a yaml file"""
10
Mate Toth-Pal51b61982022-03-17 14:19:30 +010011import argparse
12import logging
13import os
14import sys
15
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020016from iatverifier.util import read_token_map, convert_map_to_token, read_keyfile
17from iatverifier.util import get_cose_alg_from_key
Mate Toth-Pal51b61982022-03-17 14:19:30 +010018from iatverifier.psa_iot_profile1_token_verifier import PSAIoTProfile1TokenVerifier
Mate Toth-Palbb187d02022-04-26 16:01:51 +020019from iatverifier.attest_token_verifier import AttestationTokenVerifier
Mate Toth-Pal51b61982022-03-17 14:19:30 +010020
21
22if __name__ == '__main__':
23 logging.basicConfig(level=logging.INFO)
24
Mate Toth-Pal6978f7c2022-03-30 14:38:55 +020025 token_verifiers = {
26 "PSA-IoT-Profile1-token": PSAIoTProfile1TokenVerifier,
27 }
28
Mate Toth-Pal51b61982022-03-17 14:19:30 +010029 parser = argparse.ArgumentParser()
30 parser.add_argument('source', help='Token source in YAML format')
31 parser.add_argument('-o', '--outfile',
32 help='''Output file for the compiled token. If this is not
33 specified, the token will be written to standard output.''')
34 parser.add_argument('-k', '--keyfile',
35 help='''Path to the key in PEM format that should be used to
36 sign the token. If this is not specified, the token will be
37 unsigned.''')
38 group = parser.add_mutually_exclusive_group()
Mate Toth-Palbdb475e2022-04-24 12:11:22 +020039 parser.add_argument('-a', '--add-protected-header', action='store_true',
40 help='''
41 Add protected header to the COSE wrapper.
42 ''')
Mate Toth-Pal51b61982022-03-17 14:19:30 +010043 group.add_argument('-r', '--raw', action='store_true',
44 help='''Generate raw CBOR and do not create a signature
45 or COSE wrapper.''')
46 group.add_argument('-m', '--hmac', action='store_true',
47 help='''Generate a token wrapped in a Mac0 rather than
48 Sign1 COSE structure.''')
Mate Toth-Pal6978f7c2022-03-30 14:38:55 +020049 parser.add_argument('-t', '--token-type',
50 help='''The type of the Token.''',
51 choices=token_verifiers.keys(),
52 required=True)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010053
54 args = parser.parse_args()
Mate Toth-Pal51b61982022-03-17 14:19:30 +010055
Mate Toth-Pal51b61982022-03-17 14:19:30 +010056 if args.hmac:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020057 METHOD = AttestationTokenVerifier.SIGN_METHOD_MAC0
Mate Toth-Pal51b61982022-03-17 14:19:30 +010058 elif args.raw:
59 if args.keyfile:
60 raise ValueError('A keyfile cannot be specified with --raw.')
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020061 METHOD = AttestationTokenVerifier.SIGN_METHOD_RAW
Mate Toth-Pal51b61982022-03-17 14:19:30 +010062 else:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020063 METHOD = AttestationTokenVerifier.SIGN_METHOD_SIGN1
Mate Toth-Pal51b61982022-03-17 14:19:30 +010064
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020065 key = read_keyfile(args.keyfile, METHOD)
66
67 COSE_ALG = None
68 if args.hmac:
69 COSE_ALG = AttestationTokenVerifier.COSE_ALG_HS256
70 elif not args.raw:
71 COSE_ALG = get_cose_alg_from_key(key)
72
73 verifier_class = token_verifiers[args.token_type]
74 if verifier_class == PSAIoTProfile1TokenVerifier:
75 verifier = PSAIoTProfile1TokenVerifier(
76 method=METHOD,
77 cose_alg=COSE_ALG,
78 signing_key=key,
79 configuration=None)
80 else:
81 logging.error(f'Invalid token type:{verifier_class}\n\t')
82 sys.exit(1)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010083 token_map = read_token_map(args.source)
84
85 if args.outfile:
86 with open(args.outfile, 'wb') as wfh:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020087 convert_map_to_token(
88 token_map,
89 verifier,
90 wfh,
91 add_p_header=args.add_protected_header,
92 name_as_key=True,
93 parse_raw_value=True)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010094 else:
95 with os.fdopen(sys.stdout.fileno(), 'wb') as wfh:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020096 convert_map_to_token(
97 token_map,
98 verifier,
99 wfh,
100 add_p_header=args.add_protected_header,
101 name_as_key=True,
102 parse_raw_value=True)