GPSHELL(1)

๐ŸŽ€ GPSHELL(1) ๐ŸŽ€

it's the gpshell page from the GPShell Documentation, bestie ๐Ÿ’– and yes it's long, so buckle up โ€” this is part one of two, don't go anywhere ๐Ÿ‘‘


๐ŸŒธ NAME ๐ŸŒธ

gpshell โ€” the command line tool for managing GlobalPlatform compliant smart cards ๐Ÿƒโœจ

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

gpshell scriptfile

โœจ DESCRIPTION โœจ

okay so gpshell can manage applications on smart cards that support the GlobalPlatform. we're talking installing and deleting applications, peeking at the applications' status, grabbing card data โ€” the whole glow-up routine ๐Ÿซถ these applications are, like, practically always Java Card applets no cap. and there are extra key management commands in the mix too ๐Ÿ”‘

the most common way to run gpshell is honestly just with a script file ๐Ÿ“œ but plot twist: if you don't hand it a script file, it'll read the commands straight from stdin instead. main character flexibility ๐Ÿ’…

inside a script, your environment variables are reachable with the syntax ${ENV_VAR_NAME} โ€” yes literally that.

reading commands from stdin lets you feed them in dynamically and use conditionals when you're working with the Expect pattern. tools exist for a whole bunch of script languages, shells and programming languages. the vibe? support environment variables, handle results and output conditionally. it's giving very versatile ๐Ÿฆ‹

there are several txt example scripts that ship with it โ€” they get installed into /usr/share/doc/gpshell/examples/ and you can find them online too ๐ŸŒˆ


๐Ÿ’… COMMANDS ๐Ÿ’…

  • # โ€” start a line with a comment ๐Ÿ’ฌ
  • // โ€” also starts a line with a comment ๐Ÿ’ฌ
  • mode_201 โ€” sets the protocol mode to OpenPlatform 2.0.1. this is the default, so if you say nothing, this is what you get โœจ
  • mode_211 โ€” sets the protocol mode to GlobalPlatform 2.1.1 and later ๐Ÿš€
  • visa_key_derivation โ€” for cards that use the VISA key derivation scheme for the key calculation โ€” think GemXpresso Pro, Palmera Protect or some JCOP cards โ€” you must set this ๐Ÿ”‘
  • emv_cps11_key_derivation โ€” for cards that use the EMV CPS 1.1 key derivation scheme for the key calculation, like a Sm@rtCafe Expert 3.0, you must set this ๐Ÿ”‘
  • enable_trace โ€” turns on the APDU trace ๐Ÿ‘€

you'll see the sent APDUs in clear text. the last two bytes of the response are the response code. a response code of 9000 means success โœจ anything else means an error. and ngl, that may be OK when you're deleting a non existing applet or package ๐Ÿคทโ€โ™€๏ธ

  • enable_timer โ€” enables the logging of the execution times of commands โฑ๏ธ
  • establish_context โ€” establishes context. this must always be executed before connecting to a card, no skips ๐Ÿšจ
  • list_readers โ€” lists the card readers ๐Ÿ“‡
  • card_connect -reader readerName -protocol protocol โ€” connect to the card in the reader with readerName. by default protocol is 0 = T0 ๐Ÿ”Œ
  • card_connect -readerNumber x -protocol protocol โ€” connect to the card in the x th reader in the system. by default protocol is 0 = T0 ๐Ÿ”Œ
  • open_sc -keyind x -keyver x -key key -mac_key mac-key -enc_key enc-key -kek_key kek-key -security securityLevel -scp protocol -scpimpl impl -keyDerivation derivation โ€” open a secure channel ๐Ÿ”

for OpenPlatform 2.0.1' cards, only -keyind, -keyver, -mac_key and -enc_key are necessary.

for GlobalPlatform 2.1.1 and later cards, -scp and -scpimpl should not be necessary to supply. you must also specify -kek_key.

if the card supports a Secure Channel Protocol Implementation with only one base key, specify this key with -key and omit the others.

if the card uses a key derivation mechanism you must enable the derivation mode with the -keyDerivation option and you must specify with -key the master (mother) key. -kek_key, -mac_key and -enc_key are not relevant in this case. see the section Options and Key Derivation ๐Ÿ‘€

โš ๏ธ NOTE: if the secure channel is going to be opened when no security domain is selected, then the command get_secure_channel_protocol_details must be executed before, so you're actually able to get the Secure Channel Protocol Implementation. no shortcuts bestie ๐Ÿ˜ค
  • select -AID AID โ€” select the AID instance ๐ŸŽฏ
  • install -file appletFile -priv privilege -sdAID sdAID -AID AIDInPkg -pkgAID packageAID -instAID instanceAID -nvCodeLimit x -nvDataLimit y -instParam installationParams -uiccSystemSpecParam uiccSystemSpecParams -simSpecParam simSpecParams โ€” loads and installs an applet in one step ๐Ÿ“ฆโœจ

the parameters -AID -instAID -pkgAID -nvCodeLimit can be detected automatically, and -AID and -instAID get set to the first applet in appletfile.

for the sdAID, the AID selected with the select command is chosen if it's not given. otherwise the default Card Manager / Security Issuer Domain AID is chosen. usually you do not have to pass it ๐Ÿ™…โ€โ™€๏ธ

-instParam specifies applet installation parameters for the install() method. -uiccSystemSpecParam specifies parameters according to ETSI TS 102 226, sect. 8.2.1.3.2.2, for using the Card Application Toolkit (CAT) functionality or to access the UICC file system. the parameters have to be already encoded without the outer tag EA. -simSpecParam specifies parameters according to ETSI TS 102 226, sect. 8.2.1.3.2.1, to use the SIM Application Toolkit (STK) functionality or to access the SIM file system. the parameters have to be already encoded without the outer tag CA. mind the tags, they're not optional ๐Ÿ’…

  • install_for_load -pkgAID packageAID -sdAID sdAID -nvCodeLimit y โ€” applet load preparation ๐Ÿ› ๏ธ

for the sdAID, the AID selected with the select command is chosen if it's not given. otherwise the default Card Manager / Security Issuer Domain AID is chosen. usually you do not have to pass it ๐Ÿ™…โ€โ™€๏ธ

this command may be needed if the combined install command does not work. lowkey a rescue mission ๐Ÿ˜ญ

  • load -file appletFile โ€” load applet ๐Ÿ“ฅ

this command may be needed if the combined install command does not work.

  • install_for_install -priv privilege -AID AIDInPkg -pkgAID pkgAID -instAID instanceAID -nvDataLimit x -instParam installationParams -uiccSystemSpecParam uiccSystemSpecParams -simSpecParam simSpecParams โ€” instantiate applet ๐ŸŒฑ

this command may be needed if the combined install command does not work. or if you want to install a pre-installed Security Domain ๐Ÿ‘‘

  • install_for_make_selectable -priv privilege -instAID instanceAID โ€” makes an installed applet instance selectable โœ…

this command may be needed if the combined install command does not work. typically this is used after an install_for_install followed by personalization.

  • card_disconnect โ€” disconnect the card ๐Ÿ‘‹
  • get_status -element e0 โ€” list applets and packages and security domains ๐Ÿ“‹
  • get_status -element 20 โ€” list packages ๐Ÿ“ฆ
  • get_status -element 40 โ€” list applets or security domains ๐Ÿ—‚๏ธ
  • get_status -element 80 โ€” list Card Manager / Security Issuer Domain ๐Ÿ‘‘
  • release_context โ€” release context ๐Ÿซณ
  • put_sc_key -keyver keyver -newkeyver newkeyver -mac_key new_MAC_key -enc_key new_ENC_key -kek_key new_KEK_key โ€” add or replace a key set version ๐Ÿ”‘๐Ÿ”

if a new key set version is to be added, keyver must be set to 0. if keyver equals newkeyver, an existing key version gets replaced. heads up: an existing key set version cannot be replaced with a key set version using a different key size ๐Ÿšซ

  • put_sc_key -keyver keyver -newkeyver newkeyver -key key -keyDerivation โ€œderivationโ€ โ€” replace key set version keyver using key derivation derivation using the master (mother) key ๐Ÿ”‘
  • put_dm_keys -keyver keyver -newkeyver newkeyver -file public_rsa_key_file -pass password -key new_receipt_generation_key โ€” add a RSA delegated management key in version newkeyver ๐Ÿ”
  • send_apdu -sc 0 -APDU apdu โ€” send APDU apdu without a secure channel ๐Ÿ“ค

the APDU is given as hex without spaces and without a leading 0x.

  • send_apdu_nostop -sc 0 -APDU apdu โ€” same as send_apdu but it does not stop in case of connection or GlobalPlatform errors ๐Ÿšจ

the APDU is given as hex without spaces and without a leading 0x.

  • get_data -identifier identifier โ€” a GET DATA command returning the data for the given identifier. see the identifier options for details ๐Ÿ‘€
  • get_key_information_templates -keyTemplate index โ€” a GET DATA command returning the key information templates in the selected security domain ๐Ÿ—๏ธ
โš ๏ธ NOTE: the security domain must be selected, and this only works outside of a secure channel.
  • get_extended_card_resources_information โ€” a GET DATA command returning the extended card resources information in the issuer security domain ๐Ÿ“Š
โš ๏ธ NOTE: the security domain must be selected, and this only works outside of a secure channel.
  • get_secure_channel_protocol_details โ€” a GET DATA command returning the secure channel protocol details and remembering them for a later open_sc ๐Ÿง โœจ
โš ๏ธ NOTE: the security domain must be selected, and this only works outside of a secure channel.
  • print โ€” prints a line of text. prints an empty line if no text is given ๐Ÿ“
  • install_for_personalization -aid AID โ€” prepare a security domain for the personalization of an applet with the following store_data commands ๐ŸŽจ
โš ๏ธ NOTE: the security domain must be selected, and this only works outside of a secure channel.
  • store_data -dataFormat format -dataEncryption encryption -data data โ€” executes a STORE DATA command passing the data to the selected applet ๐Ÿ“ฎ
  • get_card_recognition_data โ€” a GET DATA command returning the card recognition data ๐Ÿชช
โš ๏ธ NOTE: the security domain must be selected.
  • delete -AID aid โ€” deletes an applet or package with the specified AID ๐Ÿ—‘๏ธ
  • delete_key -keyver keyver -keyind keyind โ€” deletes a key set version with a DELETE command โœ‚๏ธ๐Ÿ”‘

if only the keyver is passed, the complete key set version is deleted. by default keyind is 0xFF, which deletes the complete key set version. if keyver is 0, all key sets with the passed keyind are deleted.

  • gemXpressoPro โ€” enables support for GemXPresso cards, which enables the visa2 key derivation mode ๐Ÿ’Ž
  • exit โ€” exit the shell. useful in interactive mode ๐Ÿšช๐Ÿ‘‹

okay that's the first chunk bestie, keep scrolling, there's more ๐Ÿ’–

โœจ OPTIONS โœจ

okay bestie, deep breath, this is the big list โ€” every single flag, no skipping, we're getting through all of it ๐Ÿ’ช

  • -keyind x โ€” key index x, go off
  • -keyver x โ€” key set version x
  • -newkeyver x โ€” new key set version x
  • -key key โ€” the key value, in hex
  • -mac_key key โ€” MAC key value, in hex
  • -enc_key key โ€” ENC key value, in hex
  • -kek_key key โ€” KEK key value, in hex
  • -security x โ€” the security level, and here are the takes: 0: clear, 1: MAC, 3: MAC+ENC, 51: MAC+ENC+R-MAC+E-ENC (SCP03 only), 19: MAC+ENC-R-MAC (SCP02+SCP03 only), 17: MAC+R-MAC (SCP02+SCP03 only)
  • -reader readerName โ€” the smart card reader's name
  • -readerNumber x โ€” which reader in the system to connect to, by number. Lowkey if -reader is given, this gets ignored entirely
  • -protocol x โ€” the protocol: 0:T=0, 1:T=1. Ngl you probably shouldn't have to state this explicitly
  • -AID aid โ€” Applet ID
  • -sdAID aid โ€” Security Domain AID
  • -pkgAID aid โ€” Package AID
  • -instAID aid โ€” Instance AID
  • -nvCodeLimit x โ€” non-volatile code size limit
  • -nvDataLimit x โ€” non-volatile data size limit
  • -vDataLimit x โ€” volatile data size limit
  • -file name โ€” file name
  • -instParam param โ€” installation parameter
  • -uiccSystemSpecParam param โ€” UICC System Specific Parameters, per ETSI TS 102 226, sect. 8.2.1.3.2.2.
  • -simSpecParam param โ€” SIM File Access and Toolkit Application Specific Parameters, per ETSI TS 102 226, sect. 8.2.1.3.2.1.
  • -element x โ€” which element type to list, in hex ๐ŸŒธ
    • 80 - Card Manager / Card Issuer Security Domain only.
    • 40 - Applications (and Security Domains only in GP211 and later).
    • 20 - Executable Load Files only.
    • 10 - Executable Load Files and their Executable Modules only (Only GP211 and later)
  • -format x โ€” sets the format of the response from the get_status command. This one is only for GlobalPlatform cards, and it's required, and you only need it if the default isn't supported by the smart card. Three conditions, bestie, it's giving โœจdivaโœจ
  • -dataFormat x โ€” sets the data format flag for the store_data command. Default 0.
    • 0 - No general encryption information or non - encrypted data
    • 0x20 - Application dependent encryption of the data
    • 0x40 - RFU(encryption indicator)
    • 0x60 - Encrypted data. Must be encrypted with data encryption key.
  • -dataEncryption x โ€” sets the encryption format flag for the store_data command. Heads up bestie: this is just a flag and the data must already be passed in the correct encryption. Default 0.
    • 0 - No general data structure information
    • 0x08 - DGI format of the command data field
    • 0x10 - BER-TLV format of the command data field
    • 0x18 - RFU (data structure information)
  • -responseDataExpected x โ€” sets whether response data is expected for the store_data command. 1 = yes, expecting response data. Default 0.
  • -keyTemplate x โ€” sets the key template index to return for the get_key_templates command. Default 0.
    • 0 - Deprecated format
    • 2 - New format (default)
  • -sc x โ€” Secure Channel mode (0 off, 1 on)
  • -APDU apdu โ€” the APDU to send. Has to be in hex format, e.g. 80CA00CF00.
  • -priv x โ€” Privilege. E.g. 0x04 Default Selected
  • -scp x โ€” Secure Channel Protocol (1 SCP01, 2 SCP02, 3 SCP03, default: no set). Should not be necessary to state explicitly.
  • -scpimpl x โ€” Secure Channel Implementation (default: not set). Should not be necessary to state explicitly. Peep the get_secure_channel_protocol_details command to detect the Secure Channel Protocol Implementation. NOTE: the value can be passed as a decimal value or as hex prefixed by 0x. The hexadecimal version is the common one.
  • -pass password โ€” password for key decryption
  • -identifier identifier โ€” the identifier for the tag for the get_data command. Must be in hex format, e.g. 9F7F.

There are several identifiers available, but in general not all cards support them (rude of them tbh). The GlobalPlatform specification v2.3.1 lists a few in section 11.3.3.1. It's useful to use some ASN.1 parser to interpret these data, like asn1js ๐Ÿ‘

Some useful identifiers are:

  • 9F7F - CPLC (Card Production Life Cycle) Data
  • 00E0 - Key Information Templates. Instead of the first byte 00, 01, ... can also be used to get more key information templates if available. There's a dedicated command for getting this: get_key_information_templates
  • 2F00 - List of applications
  • FF21 - Extended card resources. There's a dedicated command for getting this: get_extended_card_resources_information
  • 0066 - Card Recogniti0on Data. There's a dedicated command for getting this: get_card_recognition_data
  • -data data โ€” data in hex format for the store_data command.
  • -noStop โ€” does not stop when there's an error. Iconic, honestly.
  • -keyType keyType โ€” the type of the key for the put_sc_key command. Must be in hex format, e.g. 88.

๐Ÿ’พ Format of CPLC data

Here you can see the command trace of a GET DATA command plus the interpreted result, and it's lowkey so satisfying to watch ngl:

=> 80 CA 9F 7F 00 .....
(12102 usec)
<= 9F 7F 2A 47 90 50 40 47 91 81 02 31 00 83 58 00 ..*G.P@G...1..X.
11 68 91 45 81 48 12 83 65 00 00 00 00 01 2F 31 .h.E.H..e...../1
30 31 31 36 38 00 00 00 00 00 00 00 00 90 00 01168..........
Status: No Error
IC Fabricator : 4790
IC Type : 5040
Operating System ID : 4791
Operating System release date : 8102 (11.4.2008)
Operating System release level : 3100
IC Fabrication Date : 8358 (23.12.2008)
IC Serial Number : 00116891
IC Batch Identifier : 4581
IC Module Fabricator : 4812
IC Module Packaging Date : 8365 (30.12.2008)
ICC Manufacturer : 0000
IC Embedding Date : 0000
IC Pre-Personalizer : 012F
IC Pre-Perso. Equipment Date : 3130 (10.5.2003)
IC Pre-Perso. Equipment ID : 31313638
IC Personalizer : 0000
IC Personalization Date : 0000
IC Perso. Equipment ID : 00000000

Dates are stored as 2 bytes โ€” the first one specifies the year in the decade, and the last 3 bytes are the day within the year. Little time capsule moment ๐Ÿ•ฐ๏ธ

  • -keyDerivation derivation method โ€” the possible values are none, visa1, visa2 or emvcps11

Choose visa2 if you have a card that uses the VISA key derivation scheme for the key calculation, like GemXpresso Pro or some JCOP cards โ€” you must set this.

Choose emvcps11 if you have a card that uses the EMV CPS 1.1 key derivation scheme for the key calculation, like a Sm@rtCafe Expert 3.0 and later โ€” you must set this. Also, for put_sc_key this is necessary for Sm@rtcafe 5.0 (and earlier(?)) cards.

visa1 is an old VISA key derivation scheme and is only needed for older cards. Vintage behaviour, we respect it. ๐Ÿซถ


๐ŸŒฑ ENVIRONMENT

  • GLOBALPLATFORM_DEBUG โ€” enables debugging output from the underlying GlobalPlatform library. Turn it on when you want the tea โ˜•
  • GLOBALPLATFORM_LOGFILE โ€” sets the log file name for the debugging output.

๐Ÿ—๏ธ Key Derivation

  • visa2

For the VISA2 key derivation scheme, like used in a GemXpresso Pro or some JCOP cards.

  • emvcps11

For the key derivation according to EMV CPS 1.1 (CDK (CPG 2.04)), like Sm@rtCafe Expert 3.0 and later.

Known unsupported key derivation schemes are:

  • CDK (CPG 2.02)
  • ISK(D)

๐ŸŽ€ Supported Cards

bestie look at this roster, it's giving history museum of smart cards ๐Ÿ›๏ธโœจ

  • Gemalto IDCore 3010
  • Oberthur CosmopoliC 32K (OP201)
  • CosmopoliC 64K V5.2 (GP211, SCP01, Impl05)
  • Axalto Cyberflex e-gate 32k (OP201)
  • GemXpresso R3.2 E64
  • IBM JCOP v2.2 41 (GP211)
  • IBM JCOP 31 (36k)
  • Palmera Protect V5
  • JTopV15
  • Nokia 6131 NFC Phone (GP211)
  • Axalto Cyberflex Access 64k
  • Gemalto Generations Flexible
  • Sm@rtCafe Expert 3.0
  • Tongfang420
  • Infineon SECORAโ„ข ID S
  • JCOP4 P71
  • JCPO3 P60 EMV
  • JCOP3 P60 SecID CS
  • JCOP3 P40 EMV
  • JCOP3 P40 SecID
  • JCOP2.4.x

๐Ÿงฐ Misc

๐Ÿ› ๏ธ About install_for_load and install

For CosmopoliC 64K (tested on V5.2), you need to specify the Security Domain AID. For example,

install -file helloworld.cap -sdAID A000000003000000 -nvCodeLimit 4000

For GemXpresso R3.2 E64, you also need to specify the Security Domain AID (Card Manager AID). For example,

install -file helloworld.cap -sdAID A000000018434D00 -nvCodeLimit 4000

๐Ÿ” JCOP cards

If you cannot authenticate to your card, it might not be fused. In this case you need the transport key from the vendor (yeah, you gotta go ask, no shortcut ๐Ÿ’€). Execute the JCOP IDENTIFY command.

select -aid A000000167413000FF โ€” offset 14 (decimal) of the response has the pre-personalized state. 00h means not fused (not personalized), 01h means fused.

๐Ÿฆ‹ CyberFlex cards

For the Cyberflex you also need the CAP transformer (I believe this is a kind of obfuscator) which you must apply to the CAP file. Download it from http://www.trusted-logic.fr/down.php and use it.


๐Ÿ’€ BUGS

  • JCOP 10

install_for_load fails for unknown reason, so nothing can be installed. Not me crying about it ๐Ÿ˜ญ

Some cards are not supporting the GET DATA command. This command is used by GPShell for retrieving the secure channel parameters. So you have to pass -scp 2 -scpimpl 0x15 to the open_sc command.


๐Ÿ‘‘ AUTHOR

Karsten Ohme k_o_@users.sourceforge.net โ€” Snit Mo snitmo@gmail.com

See the file AUTHORS for a complete list.


and that's the whole page, bestie, we ate and left no crumbs ๐Ÿ“ this was the GPSHELL(1) manual, version 2.4.0 โ€” stan the karsten, go touch some smart cards, mwah ๐Ÿ’–๐ŸŽ€โœจ

๐Ÿ“ฆ where this page comes from