Skip to content

Commit 15140aa

Browse files
committed
global_platform: add new command "install_cap"
Installing JAVA-card applets from a CAP file is a multi step process, which is difficult when done manually. Fortunately it is easy to automate the process, so let's add a dedicated command for that. Change-Id: I6cbd37f0fad5579b20e83c27349bd5acc129e6d0 Related: OS#6679
1 parent a0071b3 commit 15140aa

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed

docs/cap-tutorial.rst

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
Guide: Installing JAVA-card applets
3+
===================================
4+
5+
Almost all modern-day UICC cards have some form of JAVA-card / Sim-Toolkit support, which allows the installation
6+
of customer specific JAVA-card applets. The installation of JAVA-card applets is usually done via the standardized
7+
GlobalPlatform (GPC_SPE_034) ISD (Issuer Security Domain) application interface during the card provisioning process.
8+
(it is also possible to load JAVA-card applets in field via OTA-SMS, but that is beyond the scope of this guide). In
9+
this guide we will go through the individual steps that are required to load JAVA-card applet onto an UICC card.
10+
11+
12+
Preparation
13+
~~~~~~~~~~~
14+
15+
In this example we will install the CAP file HelloSTK_09122024.cap [1] on an sysmoISIM-SJA2 card. Since the interface
16+
is standardized, the exact card model does not matter.
17+
18+
The example applet makes use of the STK (Sim-Toolkit), so we must supply STK installation parameters. Those
19+
parameters are supplied in the form of a hexstring and should be provided by the applet manufacturer. The available
20+
parameters and their exact encoding is specified in ETSI TS 102 226, section 8.2.1.3.2.1. The installation of
21+
HelloSTK_09122024.cap [1], will require the following STK installation parameters: "010001001505000000000000000000000000"
22+
23+
During the installation, we also have to set a memory quota for the volatile and for the non volatile card memory.
24+
Those values also should be provided by the applet manufacturer. In this example, we will allow 255 bytes of volatile
25+
memory and 255 bytes of non volatile memory to be consumed by the applet.
26+
27+
To install JAVA-card applets, one must be in the possession of the key material belonging to the card. The keys are
28+
usually provided by the card manufacturer. The following example will use the following keyset:
29+
30+
+---------+----------------------------------+
31+
| Keyname | Keyvalue |
32+
+=========+==================================+
33+
| DEK/KIK | 5524F4BECFE96FB63FC29D6BAAC6058B |
34+
+---------+----------------------------------+
35+
| ENC/KIC | 542C37A6043679F2F9F71116418B1CD5 |
36+
+---------+----------------------------------+
37+
| MAC/KID | 34F11BAC8E5390B57F4E601372339E3C |
38+
+---------+----------------------------------+
39+
40+
[1] https://osmocom.org/projects/cellular-infrastructure/wiki/HelloSTK
41+
42+
43+
Applet Installation
44+
~~~~~~~~~~~~~~~~~~~
45+
46+
To prepare the installation, a secure channel to the ISD must be established first:
47+
48+
::
49+
50+
pySIM-shell (00:MF)> select ADF.ISD
51+
{
52+
"application_id": "a000000003000000",
53+
"proprietary_data": {
54+
"maximum_length_of_data_field_in_command_message": 255
55+
}
56+
}
57+
pySIM-shell (00:MF/ADF.ISD)> establish_scp02 --key-dek 5524F4BECFE96FB63FC29D6BAAC6058B --key-enc 542C37A6043679F2F9F71116418B1CD5 --key-mac 34F11BAC8E5390B57F4E601372339E3C --security-level 1
58+
Successfully established a SCP02[01] secure channel
59+
60+
.. warning:: In case you get an "EXCEPTION of type 'ValueError' occurred with message: card cryptogram doesn't match" error message, it is very likely that there is a problem with the key material. The card may lock the ISD access after a certain amount of failed tries. Carefully check the key material any try again.
61+
62+
63+
When the secure channel is established, we are ready to install the applet. The installation normally is a multi step
64+
procedure, where the loading of an executable load file is announced first, then loaded and then installed in a final
65+
step. The pySim-shell command ``install_cap`` automatically takes care of those three steps.
66+
67+
::
68+
69+
pySIM-shell (SCP02[01]:00:MF/ADF.ISD)> install_cap /home/user/HelloSTK_09122024.cap --install-parameters-non-volatile-memory-quota 255 --install-parameters-volatile-memory-quota 255 --install-parameters-stk 010001001505000000000000000000000000
70+
loading cap file: /home/user/HelloSTK_09122024.cap ...
71+
parameters:
72+
security-domain-aid: a000000003000000
73+
load-file: 569 bytes
74+
load-file-aid: d07002ca44
75+
module-aid: d07002ca44900101
76+
application-aid: d07002ca44900101
77+
install-parameters: c900ef1cc80200ffc70200ffca12010001001505000000000000000000000000
78+
step #1: install for load...
79+
step #2: load...
80+
Loaded a total of 573 bytes in 3 blocks. Don't forget install_for_install (and make selectable) now!
81+
step #3: install_for_install (and make selectable)...
82+
done.
83+
84+
The applet is now installed on the card. We can now quit pySim-shell and remove the card from the reader and test the
85+
applet in a mobile phone. There should be a new STK application with one menu entry shown, that will greet the user
86+
when pressed.
87+
88+
89+
Applet Removal
90+
~~~~~~~~~~~~~~
91+
92+
To remove the applet, we must establish a secure channel to the ISD (see above). Then we can delete the applet using the
93+
``delete_card_content`` command.
94+
95+
::
96+
97+
pySIM-shell (SCP02[01]:00:MF/ADF.ISD)> delete_card_content D07002CA44 --delete-related-objects
98+
99+
The parameter "D07002CA44" is the load-file-AID of the applet. The load-file-AID is encoded in the .cap file and also
100+
displayed during the installation process. It is also important to note that when the applet is installed, it cannot
101+
be installed (under the same AID) again until it is removed.
102+
103+

docs/shell.rst

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Usage Examples
6767
:caption: Tutorials for pySIM-shell:
6868

6969
suci-tutorial
70+
cap-tutorial
7071

7172

7273
Advanced Topics
@@ -1053,6 +1054,12 @@ load
10531054
:module: pySim.global_platform
10541055
:func: ADF_SD.AddlShellCommands.load_parser
10551056

1057+
install_cap
1058+
~~~~~~~~~~~
1059+
.. argparse::
1060+
:module: pySim.global_platform
1061+
:func: ADF_SD.AddlShellCommands.install_cap_parser
1062+
10561063
install_for_personalization
10571064
~~~~~~~~~~~~~~~~~~~~~~~~~~~
10581065
.. argparse::

pySim/global_platform/__init__.py

+53
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pySim.utils import ResTuple
3131
from pySim.card_key_provider import card_key_provider_get_field
3232
from pySim.global_platform.scp import SCP02, SCP03
33+
from pySim.global_platform.install_param import gen_install_parameters
3334
from pySim.filesystem import *
3435
from pySim.profile import CardProfile
3536
from pySim.ota import SimFileAccessAndToolkitAppSpecParams
@@ -858,6 +859,58 @@ def load(self, contents:bytes, chunk_len:int = 240):
858859
_rsp_hex, _sw = self._cmd.lchan.scc.send_apdu_checksw(cmd_hex)
859860
self._cmd.poutput("Loaded a total of %u bytes in %u blocks. Don't forget install_for_install (and make selectable) now!" % (total_size, block_nr))
860861

862+
install_cap_parser = argparse.ArgumentParser()
863+
install_cap_parser.add_argument('cap_file', type=str, metavar='FILE',
864+
help='JAVA-CARD CAP file to install')
865+
install_cap_parser_inst_prm_g = install_cap_parser.add_mutually_exclusive_group()
866+
install_cap_parser_inst_prm_g.add_argument('--install-parameters', type=is_hexstr, default=None,
867+
help='install Parameters (GPC_SPE_034, section 11.5.2.3.7, table 11-49)')
868+
install_cap_parser_inst_prm_g_grp = install_cap_parser_inst_prm_g.add_argument_group()
869+
install_cap_parser_inst_prm_g_grp.add_argument('--install-parameters-volatile-memory-quota',
870+
type=int, default=None,
871+
help='volatile memory quota (GPC_SPE_034, section 11.5.2.3.7, table 11-49)')
872+
install_cap_parser_inst_prm_g_grp.add_argument('--install-parameters-non-volatile-memory-quota',
873+
type=int, default=None,
874+
help='non volatile memory quota (GPC_SPE_034, section 11.5.2.3.7, table 11-49)')
875+
install_cap_parser_inst_prm_g_grp.add_argument('--install-parameters-stk',
876+
type=is_hexstr, default=None,
877+
help='Load Parameters (ETSI TS 102 226, section 8.2.1.3.2.1)')
878+
879+
@cmd2.with_argparser(install_cap_parser)
880+
def do_install_cap(self, opts):
881+
"""Perform a .cap file installation using GlobalPlatform LOAD and INSTALL commands."""
882+
883+
self._cmd.poutput("loading cap file: %s ..." % opts.cap_file)
884+
cap = CapFile(opts.cap_file)
885+
886+
security_domain_aid = self._cmd.lchan.selected_file.aid
887+
load_file = cap.get_loadfile()
888+
load_file_aid = cap.get_loadfile_aid()
889+
module_aid = cap.get_applet_aid()
890+
application_aid = module_aid
891+
if opts.install_parameters:
892+
install_parameters = opts.install_parameters;
893+
else:
894+
install_parameters = gen_install_parameters(opts.install_parameters_non_volatile_memory_quota,
895+
opts.install_parameters_volatile_memory_quota,
896+
opts.install_parameters_stk)
897+
self._cmd.poutput("parameters:")
898+
self._cmd.poutput(" security-domain-aid: %s" % security_domain_aid)
899+
self._cmd.poutput(" load-file: %u bytes" % len(load_file))
900+
self._cmd.poutput(" load-file-aid: %s" % load_file_aid)
901+
self._cmd.poutput(" module-aid: %s" % module_aid)
902+
self._cmd.poutput(" application-aid: %s" % application_aid)
903+
self._cmd.poutput(" install-parameters: %s" % install_parameters)
904+
905+
self._cmd.poutput("step #1: install for load...")
906+
self.do_install_for_load("--load-file-aid %s --security-domain-aid %s" % (load_file_aid, security_domain_aid))
907+
self._cmd.poutput("step #2: load...")
908+
self.load(load_file)
909+
self._cmd.poutput("step #3: install_for_install (and make selectable)...")
910+
self.do_install_for_install("--load-file-aid %s --module-aid %s --application-aid %s --install-parameters %s --make-selectable" %
911+
(load_file_aid, module_aid, application_aid, install_parameters))
912+
self._cmd.poutput("done.")
913+
861914
est_scp02_parser = argparse.ArgumentParser()
862915
est_scp02_parser.add_argument('--key-ver', type=auto_uint8, default=0, help='Key Version Number (KVN)')
863916
est_scp02_parser.add_argument('--host-challenge', type=is_hexstr,
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# GlobalPlatform install parameter generator
2+
#
3+
# (C) 2024 by Sysmocom s.f.m.c. GmbH
4+
# All Rights Reserved
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
20+
from osmocom.construct import *
21+
from osmocom.utils import *
22+
from osmocom.tlv import *
23+
24+
class AppSpecificParams(BER_TLV_IE, tag=0xC9):
25+
# GPD_SPE_013, table 11-49
26+
_construct = HexAdapter(GreedyBytes)
27+
28+
class VolatileMemoryQuota(BER_TLV_IE, tag=0xC7):
29+
# GPD_SPE_013, table 11-49
30+
_construct = StripHeaderAdapter(GreedyBytes, 4, steps = [2,4])
31+
32+
class NonVolatileMemoryQuota(BER_TLV_IE, tag=0xC8):
33+
# GPD_SPE_013, table 11-49
34+
_construct = StripHeaderAdapter(GreedyBytes, 4, steps = [2,4])
35+
36+
class StkParameter(BER_TLV_IE, tag=0xCA):
37+
# GPD_SPE_013, table 11-49
38+
# ETSI TS 102 226, section 8.2.1.3.2.1
39+
_construct = HexAdapter(GreedyBytes)
40+
41+
class SystemSpecificParams(BER_TLV_IE, tag=0xEF, nested=[VolatileMemoryQuota, NonVolatileMemoryQuota, StkParameter]):
42+
# GPD_SPE_013 v1.1 Table 6-5
43+
pass
44+
45+
class InstallParams(TLV_IE_Collection, nested=[AppSpecificParams, SystemSpecificParams]):
46+
# GPD_SPE_013, table 11-49
47+
pass
48+
49+
def gen_install_parameters(non_volatile_memory_quota:int, volatile_memory_quota:int, stk_parameter:str):
50+
51+
# GPD_SPE_013, table 11-49
52+
53+
#Mandatory
54+
install_params = InstallParams()
55+
install_params_dict = [{'app_specific_params': None}]
56+
57+
#Conditional
58+
if non_volatile_memory_quota and volatile_memory_quota and stk_parameter:
59+
system_specific_params = []
60+
#Optional
61+
if non_volatile_memory_quota:
62+
system_specific_params += [{'non_volatile_memory_quota': non_volatile_memory_quota}]
63+
#Optional
64+
if volatile_memory_quota:
65+
system_specific_params += [{'volatile_memory_quota': volatile_memory_quota}]
66+
#Optional
67+
if stk_parameter:
68+
system_specific_params += [{'stk_parameter': stk_parameter}]
69+
install_params_dict += [{'system_specific_params': system_specific_params}]
70+
71+
install_params.from_dict(install_params_dict)
72+
return b2h(install_params.to_bytes())

tests/unittests/test_globalplatform.py

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from pySim.global_platform import *
2323
from pySim.global_platform.scp import *
24+
from pySim.global_platform.install_param import gen_install_parameters
2425

2526
KIC = h2b('100102030405060708090a0b0c0d0e0f') # enc
2627
KID = h2b('101102030405060708090a0b0c0d0e0f') # MAC
@@ -289,5 +290,13 @@ def test_kcv(self):
289290
self.assertEqual(compute_kcv('aes', KEYSET_AES128.dek), h2b('840DE5'))
290291

291292

293+
class Install_param_Test(unittest.TestCase):
294+
def test_gen_install_parameters(self):
295+
load_parameters = gen_install_parameters(256, 256, '010001001505000000000000000000000000')
296+
self.assertEqual(load_parameters, 'c900ef1cc8020100c7020100ca12010001001505000000000000000000000000')
297+
298+
load_parameters = gen_install_parameters(None, None, '')
299+
self.assertEqual(load_parameters, 'c900')
300+
292301
if __name__ == "__main__":
293302
unittest.main()

0 commit comments

Comments
 (0)