blob: 7e616054965808e7924a219b177e956069f55a32 [file] [log] [blame]
Mate Toth-Pal51b61982022-03-17 14:19:30 +01001#!/usr/bin/env python3
2# -----------------------------------------------------------------------------
Mate Toth-Pal916a3de2024-05-03 09:34:41 +02003# Copyright (c) 2019-2024, Arm Limited. All rights reserved.
Mate Toth-Pal51b61982022-03-17 14:19:30 +01004#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7# -----------------------------------------------------------------------------
8
Mate Toth-Palb9057ff2022-04-29 16:03:21 +02009"""
10Generate a sample token, signing it with the specified key, and writing
11the output to the specified file.
12
13This script is deprecated - use ``compile_token`` (see above) instead.
14"""
Mate Toth-Pal51b61982022-03-17 14:19:30 +010015import struct
16
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020017from iatverifier.util import convert_map_to_token, read_keyfile
18from iatverifier.attest_token_verifier import AttestationTokenVerifier
19from iatverifier.psa_iot_profile1_token_claims import InstanceIdClaim, ImplementationIdClaim
20from iatverifier.psa_iot_profile1_token_claims import ChallengeClaim, ClientIdClaim
21from iatverifier.psa_iot_profile1_token_claims import SecurityLifecycleClaim, ProfileIdClaim
22from iatverifier.psa_iot_profile1_token_claims import BootSeedClaim, SWComponentsClaim
23from iatverifier.psa_iot_profile1_token_claims import SWComponentTypeClaim, SignerIdClaim
24from iatverifier.psa_iot_profile1_token_claims import SwComponentVersionClaim
25from iatverifier.psa_iot_profile1_token_claims import MeasurementValueClaim
26from iatverifier.psa_iot_profile1_token_claims import MeasurementDescriptionClaim
Mate Toth-Pala7a97172022-03-24 16:43:22 +010027from iatverifier.psa_iot_profile1_token_verifier import PSAIoTProfile1TokenVerifier
Thomas Fossatif4e1ca32024-08-16 16:01:31 +000028from pycose.algorithms import Es256, Es384, Es512
Mate Toth-Pal51b61982022-03-17 14:19:30 +010029
30# First byte indicates "GUID"
31GUID = b'\x01' + struct.pack('QQQQ', 0x0001020304050607, 0x08090A0B0C0D0E0F,
32 0x1011121314151617, 0x18191A1B1C1D1E1F)
33NONCE = struct.pack('QQQQ', 0X0001020304050607, 0X08090A0B0C0D0E0F,
34 0X1011121314151617, 0X18191A1B1C1D1E1F)
35ORIGIN = struct.pack('QQQQ', 0X0001020304050607, 0X08090A0B0C0D0E0F,
36 0X1011121314151617, 0X18191A1B1C1D1E1F)
37BOOT_SEED = struct.pack('QQQQ', 0X0001020304050607, 0X08090A0B0C0D0E0F,
38 0X1011121314151617, 0X18191A1B1C1D1E1F)
39SIGNER_ID = struct.pack('QQQQ', 0X0001020304050607, 0X08090A0B0C0D0E0F,
40 0X1011121314151617, 0X18191A1B1C1D1E1F)
41MEASUREMENT = struct.pack('QQQQ', 0X0001020304050607, 0X08090A0B0C0D0E0F,
42 0X1011121314151617, 0X18191A1B1C1D1E1F)
43
44token_map = {
45 InstanceIdClaim.get_claim_key(): GUID,
46 ImplementationIdClaim.get_claim_key(): ORIGIN,
47 ChallengeClaim.get_claim_key(): NONCE,
48 ClientIdClaim.get_claim_key(): 2,
Mate Toth-Pal916a3de2024-05-03 09:34:41 +020049 SecurityLifecycleClaim.get_claim_key(): 0x1000,
Mate Toth-Pal51b61982022-03-17 14:19:30 +010050 ProfileIdClaim.get_claim_key(): 'http://example.com',
51 BootSeedClaim.get_claim_key(): BOOT_SEED,
52 SWComponentsClaim.get_claim_key(): [
53 {
54 # bootloader
55 SWComponentTypeClaim.get_claim_key(): 'BL',
56 SignerIdClaim.get_claim_key(): SIGNER_ID,
57 SwComponentVersionClaim.get_claim_key(): '3.4.2',
58 MeasurementValueClaim.get_claim_key(): MEASUREMENT,
59 MeasurementDescriptionClaim.get_claim_key(): 'TF-M_SHA256MemPreXIP',
60 },
61 {
62 # mod1
63 SWComponentTypeClaim.get_claim_key(): 'M1',
64 SignerIdClaim.get_claim_key(): SIGNER_ID,
65 SwComponentVersionClaim.get_claim_key(): '3.4.2',
66 MeasurementValueClaim.get_claim_key(): MEASUREMENT,
67 },
68 {
69 # mod2
70 SWComponentTypeClaim.get_claim_key(): 'M2',
71 SignerIdClaim.get_claim_key(): SIGNER_ID,
72 SwComponentVersionClaim.get_claim_key(): '3.4.2',
73 MeasurementValueClaim.get_claim_key(): MEASUREMENT,
74 },
75 {
76 # mod3
77 SWComponentTypeClaim.get_claim_key(): 'M3',
78 SignerIdClaim.get_claim_key(): SIGNER_ID,
79 SwComponentVersionClaim.get_claim_key(): '3.4.2',
80 MeasurementValueClaim.get_claim_key(): MEASUREMENT,
81 },
82 ],
83}
84
85
86if __name__ == '__main__':
87 import sys
88 if len(sys.argv) != 3:
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020089 print(f'Usage: {sys.argv[0]} KEYFILE OUTFILE')
Mate Toth-Pal51b61982022-03-17 14:19:30 +010090 sys.exit(1)
91 keyfile = sys.argv[1]
92 outfile = sys.argv[2]
93
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020094 key = read_keyfile(keyfile,
95 method=AttestationTokenVerifier.SIGN_METHOD_SIGN1)
96 verifier = PSAIoTProfile1TokenVerifier(signing_key=key,
97 method=AttestationTokenVerifier.SIGN_METHOD_SIGN1,
Thomas Fossatif4e1ca32024-08-16 16:01:31 +000098 cose_alg=Es256,
Mate Toth-Palb9057ff2022-04-29 16:03:21 +020099 configuration=None)
Mate Toth-Pal51b61982022-03-17 14:19:30 +0100100 with open(outfile, 'wb') as wfh:
Mate Toth-Pale305e552022-10-07 14:04:53 +0200101 convert_map_to_token(token_map, verifier, wfh,
Mate Toth-Palb9057ff2022-04-29 16:03:21 +0200102 name_as_key=False, parse_raw_value=False)