blob: 58bc9cf701a4fa4bd0d6a4ff5801e85e327c1bbc [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 script for decompiling a cbor formatted IAT file"""
10
Mate Toth-Pal51b61982022-03-17 14:19:30 +010011import argparse
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020012import logging
Mate Toth-Pal51b61982022-03-17 14:19:30 +010013import sys
14
15import yaml
Mate Toth-Pal51b61982022-03-17 14:19:30 +010016from iatverifier.psa_iot_profile1_token_verifier import PSAIoTProfile1TokenVerifier
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020017from iatverifier.attest_token_verifier import AttestationTokenVerifier
Mate Toth-Pal5ebca512022-03-24 16:45:51 +010018from iatverifier.cca_token_verifier import CCATokenVerifier, CCAPlatformTokenVerifier
Mate Toth-Pal51b61982022-03-17 14:19:30 +010019
20
21if __name__ == '__main__':
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020022 logging.basicConfig(level=logging.INFO)
Mate Toth-Pal6978f7c2022-03-30 14:38:55 +020023
24 token_verifiers = {
25 "PSA-IoT-Profile1-token": PSAIoTProfile1TokenVerifier,
Mate Toth-Pal5ebca512022-03-24 16:45:51 +010026 "CCA-token": CCATokenVerifier,
27 "CCA-plat-token": CCAPlatformTokenVerifier,
Mate Toth-Pal6978f7c2022-03-30 14:38:55 +020028 }
29
Mate Toth-Pal51b61982022-03-17 14:19:30 +010030 parser = argparse.ArgumentParser()
31 parser.add_argument('source', help='A compiled COSE IAT token.')
32 parser.add_argument('-o', '--outfile',
33 help='''Output file for the depompiled claims. If this is not
34 specified, the claims will be written to standard output.''')
Mate Toth-Pal6978f7c2022-03-30 14:38:55 +020035 parser.add_argument('-t', '--token-type',
36 help='''The type of the Token.''',
37 choices=token_verifiers.keys(),
38 required=True)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010039 args = parser.parse_args()
40
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020041 verifier_class = token_verifiers[args.token_type]
42 if verifier_class == PSAIoTProfile1TokenVerifier:
43 verifier = PSAIoTProfile1TokenVerifier(
44 method=AttestationTokenVerifier.SIGN_METHOD_SIGN1,
45 cose_alg=AttestationTokenVerifier.COSE_ALG_ES256,
Mate Toth-Pal5ebca512022-03-24 16:45:51 +010046 signing_key=None,
47 configuration=None)
48 elif verifier_class == CCATokenVerifier:
49 realm_token_method = AttestationTokenVerifier.SIGN_METHOD_SIGN1
50 platform_token_method = AttestationTokenVerifier.SIGN_METHOD_SIGN1
51 realm_token_cose_alg = AttestationTokenVerifier.COSE_ALG_ES384
52 platform_token_cose_alg = AttestationTokenVerifier.COSE_ALG_ES384
53 verifier = CCATokenVerifier(
54 realm_token_method=realm_token_method,
55 realm_token_cose_alg=realm_token_cose_alg,
56 realm_token_key=None,
57 platform_token_method=platform_token_method,
58 platform_token_cose_alg=platform_token_cose_alg,
59 platform_token_key=None,
60 configuration=None)
61 elif verifier_class == CCAPlatformTokenVerifier:
62 cose_alg = AttestationTokenVerifier.COSE_ALG_ES384
63 verifier = CCAPlatformTokenVerifier(
64 method=AttestationTokenVerifier.SIGN_METHOD_SIGN1,
65 cose_alg=cose_alg,
66 signing_key=None,
67 configuration=None,
68 necessity=None)
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020069 else:
70 logging.error(f'Invalid token type:{verifier_class}\n\t')
71 sys.exit(1)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010072 with open(args.source, 'rb') as fh:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020073 token_map = verifier.parse_token(
74 token=fh.read(),
75 verify=False,
76 check_p_header=False,
77 lower_case_key=True)
Mate Toth-Pal51b61982022-03-17 14:19:30 +010078
79 if args.outfile:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020080 with open(args.outfile, 'w', encoding="UTF-8") as wfh:
Mate Toth-Pal51b61982022-03-17 14:19:30 +010081 yaml.dump(token_map, wfh)
82 else:
83 yaml.dump(token_map, sys.stdout)