Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 1 | #!/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-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 9 | """CLI script for decompiling a cbor formatted IAT file""" |
| 10 | |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 11 | import argparse |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 12 | import logging |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 13 | import sys |
| 14 | |
| 15 | import yaml |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 16 | from iatverifier.psa_iot_profile1_token_verifier import PSAIoTProfile1TokenVerifier |
Tamas Ban | 1e7944a | 2022-07-04 13:09:03 +0200 | [diff] [blame] | 17 | from iatverifier.psa_2_0_0_token_verifier import PSA_2_0_0_TokenVerifier |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 18 | from iatverifier.attest_token_verifier import AttestationTokenVerifier |
Mate Toth-Pal | 5ebca51 | 2022-03-24 16:45:51 +0100 | [diff] [blame] | 19 | from iatverifier.cca_token_verifier import CCATokenVerifier, CCAPlatformTokenVerifier |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 20 | |
| 21 | |
| 22 | if __name__ == '__main__': |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 23 | logging.basicConfig(level=logging.INFO) |
Mate Toth-Pal | 6978f7c | 2022-03-30 14:38:55 +0200 | [diff] [blame] | 24 | |
| 25 | token_verifiers = { |
| 26 | "PSA-IoT-Profile1-token": PSAIoTProfile1TokenVerifier, |
Mate Toth-Pal | 5ebca51 | 2022-03-24 16:45:51 +0100 | [diff] [blame] | 27 | "CCA-token": CCATokenVerifier, |
| 28 | "CCA-plat-token": CCAPlatformTokenVerifier, |
Tamas Ban | 1e7944a | 2022-07-04 13:09:03 +0200 | [diff] [blame] | 29 | "PSA-2.0.0-token": PSA_2_0_0_TokenVerifier, |
Mate Toth-Pal | 6978f7c | 2022-03-30 14:38:55 +0200 | [diff] [blame] | 30 | } |
| 31 | |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 32 | parser = argparse.ArgumentParser() |
| 33 | parser.add_argument('source', help='A compiled COSE IAT token.') |
| 34 | parser.add_argument('-o', '--outfile', |
| 35 | help='''Output file for the depompiled claims. If this is not |
| 36 | specified, the claims will be written to standard output.''') |
Mate Toth-Pal | 6978f7c | 2022-03-30 14:38:55 +0200 | [diff] [blame] | 37 | parser.add_argument('-t', '--token-type', |
| 38 | help='''The type of the Token.''', |
| 39 | choices=token_verifiers.keys(), |
| 40 | required=True) |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 41 | args = parser.parse_args() |
| 42 | |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 43 | verifier_class = token_verifiers[args.token_type] |
| 44 | if verifier_class == PSAIoTProfile1TokenVerifier: |
| 45 | verifier = PSAIoTProfile1TokenVerifier( |
| 46 | method=AttestationTokenVerifier.SIGN_METHOD_SIGN1, |
| 47 | cose_alg=AttestationTokenVerifier.COSE_ALG_ES256, |
Mate Toth-Pal | 5ebca51 | 2022-03-24 16:45:51 +0100 | [diff] [blame] | 48 | signing_key=None, |
| 49 | configuration=None) |
| 50 | elif verifier_class == CCATokenVerifier: |
| 51 | realm_token_method = AttestationTokenVerifier.SIGN_METHOD_SIGN1 |
| 52 | platform_token_method = AttestationTokenVerifier.SIGN_METHOD_SIGN1 |
| 53 | realm_token_cose_alg = AttestationTokenVerifier.COSE_ALG_ES384 |
| 54 | platform_token_cose_alg = AttestationTokenVerifier.COSE_ALG_ES384 |
| 55 | verifier = CCATokenVerifier( |
| 56 | realm_token_method=realm_token_method, |
| 57 | realm_token_cose_alg=realm_token_cose_alg, |
Mate Toth-Pal | 5ebca51 | 2022-03-24 16:45:51 +0100 | [diff] [blame] | 58 | platform_token_method=platform_token_method, |
| 59 | platform_token_cose_alg=platform_token_cose_alg, |
| 60 | platform_token_key=None, |
| 61 | configuration=None) |
| 62 | elif verifier_class == CCAPlatformTokenVerifier: |
| 63 | cose_alg = AttestationTokenVerifier.COSE_ALG_ES384 |
| 64 | verifier = CCAPlatformTokenVerifier( |
| 65 | method=AttestationTokenVerifier.SIGN_METHOD_SIGN1, |
| 66 | cose_alg=cose_alg, |
| 67 | signing_key=None, |
| 68 | configuration=None, |
| 69 | necessity=None) |
Tamas Ban | 1e7944a | 2022-07-04 13:09:03 +0200 | [diff] [blame] | 70 | elif verifier_class == PSA_2_0_0_TokenVerifier: |
| 71 | verifier = PSA_2_0_0_TokenVerifier( |
| 72 | method=AttestationTokenVerifier.SIGN_METHOD_SIGN1, |
| 73 | cose_alg=AttestationTokenVerifier.COSE_ALG_ES256, |
| 74 | signing_key=None, |
| 75 | configuration=None) |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 76 | else: |
| 77 | logging.error(f'Invalid token type:{verifier_class}\n\t') |
| 78 | sys.exit(1) |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 79 | with open(args.source, 'rb') as fh: |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 80 | token_map = verifier.parse_token( |
| 81 | token=fh.read(), |
Mate Toth-Pal | c7404e9 | 2022-07-15 11:11:13 +0200 | [diff] [blame] | 82 | lower_case_key=True).get_token_map() |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 83 | |
| 84 | if args.outfile: |
Mate Toth-Pal | b9057ff | 2022-04-29 16:03:21 +0200 | [diff] [blame] | 85 | with open(args.outfile, 'w', encoding="UTF-8") as wfh: |
Mate Toth-Pal | 51b6198 | 2022-03-17 14:19:30 +0100 | [diff] [blame] | 86 | yaml.dump(token_map, wfh) |
| 87 | else: |
| 88 | yaml.dump(token_map, sys.stdout) |